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