[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_http.h" |
---|
| 32 | #include "twitter.h" |
---|
| 33 | #include "bitlbee.h" |
---|
| 34 | #include "url.h" |
---|
| 35 | #include "misc.h" |
---|
| 36 | #include "base64.h" |
---|
| 37 | #include <ctype.h> |
---|
| 38 | #include <errno.h> |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | char *twitter_url_append(char *url, char *key, char* value); |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Do a request. |
---|
| 45 | * This is actually pretty generic function... Perhaps it should move to the lib/http_client.c |
---|
| 46 | */ |
---|
| 47 | void *twitter_http(char *url_string, http_input_function func, gpointer data, int is_post, char* user, char* pass, char** arguments, int arguments_len) |
---|
| 48 | { |
---|
| 49 | url_t *url = g_new0( url_t, 1 ); |
---|
| 50 | char *tmp; |
---|
| 51 | char *request; |
---|
| 52 | void *ret; |
---|
| 53 | char *userpass = NULL; |
---|
| 54 | char *userpass_base64; |
---|
| 55 | char *url_arguments; |
---|
| 56 | |
---|
| 57 | // Fill the url structure. |
---|
| 58 | if( !url_set( url, url_string ) ) |
---|
| 59 | { |
---|
| 60 | g_free( url ); |
---|
| 61 | return NULL; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | if( url->proto != PROTO_HTTP && url->proto != PROTO_HTTPS ) |
---|
| 65 | { |
---|
| 66 | g_free( url ); |
---|
| 67 | return NULL; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // Concatenate user and pass |
---|
| 71 | if (user && pass) { |
---|
| 72 | userpass = g_strdup_printf("%s:%s", user, pass); |
---|
| 73 | userpass_base64 = base64_encode((unsigned char*)userpass, strlen(userpass)); |
---|
| 74 | } else { |
---|
| 75 | userpass_base64 = NULL; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | url_arguments = g_malloc(1); |
---|
| 79 | url_arguments[0] = '\0'; |
---|
| 80 | |
---|
| 81 | // Construct the url arguments. |
---|
| 82 | if (arguments_len != 0) |
---|
| 83 | { |
---|
| 84 | int i; |
---|
| 85 | for (i=0; i<arguments_len; i+=2) |
---|
| 86 | { |
---|
| 87 | tmp = twitter_url_append(url_arguments, arguments[i], arguments[i+1]); |
---|
| 88 | g_free(url_arguments); |
---|
| 89 | url_arguments = tmp; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | // Do GET stuff... |
---|
| 94 | if (!is_post) |
---|
| 95 | { |
---|
| 96 | // Find the char-pointer of the end of the string. |
---|
| 97 | tmp = url->file + strlen(url->file); |
---|
| 98 | tmp[0] = '?'; |
---|
| 99 | // append the url_arguments to the end of the url->file. |
---|
| 100 | // TODO GM: Check the length? |
---|
| 101 | g_stpcpy (tmp+1, url_arguments); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | // Make the request. |
---|
| 106 | request = g_strdup_printf( "%s %s HTTP/1.0\r\n" |
---|
[2abceca] | 107 | "Host: %s\r\n" |
---|
| 108 | "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n", |
---|
| 109 | is_post ? "POST" : "GET", url->file, url->host ); |
---|
[1b221e0] | 110 | |
---|
| 111 | // If a pass and user are given we append them to the request. |
---|
| 112 | if (userpass_base64) |
---|
| 113 | { |
---|
| 114 | tmp = g_strdup_printf("%sAuthorization: Basic %s\r\n", request, userpass_base64); |
---|
| 115 | g_free(request); |
---|
| 116 | request = tmp; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | // Do POST stuff.. |
---|
| 120 | if (is_post) |
---|
| 121 | { |
---|
| 122 | // Append the Content-Type and url-encoded arguments. |
---|
[0519b0a] | 123 | tmp = g_strdup_printf("%sContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %zd\r\n\r\n%s", |
---|
[1b221e0] | 124 | request, strlen(url_arguments), url_arguments); |
---|
| 125 | g_free(request); |
---|
| 126 | request = tmp; |
---|
| 127 | } else { |
---|
| 128 | // Append an extra \r\n to end the request... |
---|
| 129 | tmp = g_strdup_printf("%s\r\n", request); |
---|
| 130 | g_free(request); |
---|
| 131 | request = tmp; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | ret = http_dorequest( url->host, url->port, url->proto == PROTO_HTTPS, request, func, data ); |
---|
| 135 | |
---|
| 136 | g_free( url ); |
---|
| 137 | g_free( userpass ); |
---|
| 138 | g_free( userpass_base64 ); |
---|
| 139 | g_free( url_arguments ); |
---|
| 140 | g_free( request ); |
---|
| 141 | return ret; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | char *twitter_url_append(char *url, char *key, char* value) |
---|
| 145 | { |
---|
[2abceca] | 146 | char *key_encoded = g_strndup(key, 3 * strlen(key)); |
---|
| 147 | http_encode(key_encoded); |
---|
| 148 | char *value_encoded = g_strndup(value, 3 * strlen(value)); |
---|
| 149 | http_encode(value_encoded); |
---|
| 150 | |
---|
[1b221e0] | 151 | char *retval; |
---|
| 152 | if (strlen(url) != 0) |
---|
| 153 | retval = g_strdup_printf("%s&%s=%s", url, key_encoded, value_encoded); |
---|
| 154 | else |
---|
| 155 | retval = g_strdup_printf("%s=%s", key_encoded, value_encoded); |
---|
| 156 | |
---|
| 157 | g_free(key_encoded); |
---|
| 158 | g_free(value_encoded); |
---|
| 159 | |
---|
| 160 | return retval; |
---|
| 161 | } |
---|