[1b221e0] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple module to facilitate twitter functionality. * |
---|
| 5 | * * |
---|
| 6 | * Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
| 7 | * * |
---|
| 8 | * This library is free software; you can redistribute it and/or * |
---|
| 9 | * modify it under the terms of the GNU Lesser General Public * |
---|
| 10 | * License as published by the Free Software Foundation, version * |
---|
| 11 | * 2.1. * |
---|
| 12 | * * |
---|
| 13 | * This library is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
| 16 | * Lesser General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
| 21 | * * |
---|
| 22 | ****************************************************************************/ |
---|
| 23 | |
---|
| 24 | /***************************************************************************\ |
---|
| 25 | * * |
---|
| 26 | * Some funtions within this file have been copied from other files within * |
---|
| 27 | * BitlBee. * |
---|
| 28 | * * |
---|
| 29 | ****************************************************************************/ |
---|
| 30 | |
---|
| 31 | #include "twitter.h" |
---|
| 32 | #include "bitlbee.h" |
---|
| 33 | #include "url.h" |
---|
| 34 | #include "misc.h" |
---|
| 35 | #include "base64.h" |
---|
[508c340] | 36 | #include "oauth.h" |
---|
[1b221e0] | 37 | #include <ctype.h> |
---|
| 38 | #include <errno.h> |
---|
| 39 | |
---|
[c2ecadc] | 40 | #include "twitter_http.h" |
---|
| 41 | |
---|
[1b221e0] | 42 | |
---|
[bb5ce4d1] | 43 | static char *twitter_url_append(char *url, char *key, char* value); |
---|
[1b221e0] | 44 | |
---|
| 45 | /** |
---|
| 46 | * Do a request. |
---|
| 47 | * This is actually pretty generic function... Perhaps it should move to the lib/http_client.c |
---|
| 48 | */ |
---|
[bb5ce4d1] | 49 | void *twitter_http(struct im_connection *ic, char *url_string, http_input_function func, gpointer data, int is_post, char** arguments, int arguments_len) |
---|
[1b221e0] | 50 | { |
---|
[bb5ce4d1] | 51 | struct twitter_data *td = ic->proto_data; |
---|
[1b221e0] | 52 | char *tmp; |
---|
[bb5ce4d1] | 53 | GString *request = g_string_new(""); |
---|
[1b221e0] | 54 | void *ret; |
---|
| 55 | char *url_arguments; |
---|
| 56 | |
---|
[bb5ce4d1] | 57 | url_arguments = g_strdup(""); |
---|
[1b221e0] | 58 | |
---|
| 59 | // Construct the url arguments. |
---|
| 60 | if (arguments_len != 0) |
---|
| 61 | { |
---|
| 62 | int i; |
---|
| 63 | for (i=0; i<arguments_len; i+=2) |
---|
| 64 | { |
---|
| 65 | tmp = twitter_url_append(url_arguments, arguments[i], arguments[i+1]); |
---|
| 66 | g_free(url_arguments); |
---|
| 67 | url_arguments = tmp; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | // Make the request. |
---|
[bb5ce4d1] | 72 | g_string_printf(request, "%s %s%s%s%s HTTP/1.0\r\n" |
---|
| 73 | "Host: %s\r\n" |
---|
| 74 | "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n", |
---|
| 75 | is_post ? "POST" : "GET", |
---|
| 76 | td->url_path, url_string, |
---|
| 77 | is_post ? "" : "?", is_post ? "" : url_arguments, |
---|
| 78 | td->url_host); |
---|
[1b221e0] | 79 | |
---|
| 80 | // If a pass and user are given we append them to the request. |
---|
[bb5ce4d1] | 81 | if (td->oauth_info) |
---|
[508c340] | 82 | { |
---|
| 83 | char *full_header; |
---|
[bb5ce4d1] | 84 | char *full_url; |
---|
[508c340] | 85 | |
---|
[bb5ce4d1] | 86 | full_url = g_strconcat(set_getstr(&ic->acc->set, "base_url" ), url_string, NULL); |
---|
| 87 | full_header = oauth_http_header(td->oauth_info, is_post ? "POST" : "GET", |
---|
| 88 | full_url, url_arguments); |
---|
[508c340] | 89 | |
---|
[bb5ce4d1] | 90 | g_string_append_printf(request, "Authorization: %s\r\n", full_header); |
---|
[508c340] | 91 | g_free(full_header); |
---|
[bb5ce4d1] | 92 | g_free(full_url); |
---|
[508c340] | 93 | } |
---|
[bb5ce4d1] | 94 | else |
---|
[1b221e0] | 95 | { |
---|
[bb5ce4d1] | 96 | char userpass[strlen(ic->acc->user)+2+strlen(ic->acc->pass)]; |
---|
| 97 | char *userpass_base64; |
---|
| 98 | |
---|
| 99 | g_snprintf(userpass, sizeof(userpass), "%s:%s", ic->acc->user, ic->acc->pass); |
---|
| 100 | userpass_base64 = base64_encode((unsigned char*)userpass, strlen(userpass)); |
---|
| 101 | g_string_append_printf(request, "Authorization: Basic %s\r\n", userpass_base64); |
---|
| 102 | g_free( userpass_base64 ); |
---|
[1b221e0] | 103 | } |
---|
| 104 | |
---|
| 105 | // Do POST stuff.. |
---|
| 106 | if (is_post) |
---|
| 107 | { |
---|
| 108 | // Append the Content-Type and url-encoded arguments. |
---|
[bb5ce4d1] | 109 | g_string_append_printf(request, |
---|
| 110 | "Content-Type: application/x-www-form-urlencoded\r\n" |
---|
| 111 | "Content-Length: %zd\r\n\r\n%s", |
---|
| 112 | strlen(url_arguments), url_arguments); |
---|
[1b221e0] | 113 | } else { |
---|
| 114 | // Append an extra \r\n to end the request... |
---|
[bb5ce4d1] | 115 | g_string_append(request, "\r\n"); |
---|
[1b221e0] | 116 | } |
---|
| 117 | |
---|
[bb5ce4d1] | 118 | ret = http_dorequest(td->url_host, td->url_port, td->url_ssl, request->str, func, data); |
---|
[1b221e0] | 119 | |
---|
| 120 | g_free( url_arguments ); |
---|
[bb5ce4d1] | 121 | g_string_free( request, TRUE ); |
---|
[1b221e0] | 122 | return ret; |
---|
| 123 | } |
---|
| 124 | |
---|
[bb5ce4d1] | 125 | static char *twitter_url_append(char *url, char *key, char* value) |
---|
[1b221e0] | 126 | { |
---|
[2abceca] | 127 | char *key_encoded = g_strndup(key, 3 * strlen(key)); |
---|
| 128 | http_encode(key_encoded); |
---|
| 129 | char *value_encoded = g_strndup(value, 3 * strlen(value)); |
---|
| 130 | http_encode(value_encoded); |
---|
| 131 | |
---|
[1b221e0] | 132 | char *retval; |
---|
| 133 | if (strlen(url) != 0) |
---|
| 134 | retval = g_strdup_printf("%s&%s=%s", url, key_encoded, value_encoded); |
---|
| 135 | else |
---|
| 136 | retval = g_strdup_printf("%s=%s", key_encoded, value_encoded); |
---|
| 137 | |
---|
| 138 | g_free(key_encoded); |
---|
| 139 | g_free(value_encoded); |
---|
| 140 | |
---|
| 141 | return retval; |
---|
| 142 | } |
---|