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 | /* For strptime(): */ |
---|
25 | #define _XOPEN_SOURCE |
---|
26 | |
---|
27 | #include "twitter_http.h" |
---|
28 | #include "twitter.h" |
---|
29 | #include "bitlbee.h" |
---|
30 | #include "url.h" |
---|
31 | #include "misc.h" |
---|
32 | #include "base64.h" |
---|
33 | #include "xmltree.h" |
---|
34 | #include "twitter_lib.h" |
---|
35 | #include <ctype.h> |
---|
36 | #include <errno.h> |
---|
37 | |
---|
38 | #define TXL_STATUS 1 |
---|
39 | #define TXL_USER 2 |
---|
40 | #define TXL_ID 3 |
---|
41 | |
---|
42 | struct twitter_xml_list { |
---|
43 | int type; |
---|
44 | int next_cursor; |
---|
45 | GSList *list; |
---|
46 | gpointer data; |
---|
47 | }; |
---|
48 | |
---|
49 | struct twitter_xml_user { |
---|
50 | char *name; |
---|
51 | char *screen_name; |
---|
52 | }; |
---|
53 | |
---|
54 | struct twitter_xml_status { |
---|
55 | time_t created_at; |
---|
56 | char *text; |
---|
57 | struct twitter_xml_user *user; |
---|
58 | guint64 id; |
---|
59 | }; |
---|
60 | |
---|
61 | /** |
---|
62 | * Frees a twitter_xml_user struct. |
---|
63 | */ |
---|
64 | static void txu_free(struct twitter_xml_user *txu) |
---|
65 | { |
---|
66 | g_free(txu->name); |
---|
67 | g_free(txu->screen_name); |
---|
68 | g_free(txu); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | * Frees a twitter_xml_status struct. |
---|
74 | */ |
---|
75 | static void txs_free(struct twitter_xml_status *txs) |
---|
76 | { |
---|
77 | g_free(txs->text); |
---|
78 | txu_free(txs->user); |
---|
79 | g_free(txs); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Free a twitter_xml_list struct. |
---|
84 | * type is the type of list the struct holds. |
---|
85 | */ |
---|
86 | static void txl_free(struct twitter_xml_list *txl) |
---|
87 | { |
---|
88 | GSList *l; |
---|
89 | for ( l = txl->list; l ; l = g_slist_next(l) ) |
---|
90 | if (txl->type == TXL_STATUS) |
---|
91 | txs_free((struct twitter_xml_status *)l->data); |
---|
92 | else if (txl->type == TXL_ID) |
---|
93 | g_free(l->data); |
---|
94 | g_slist_free(txl->list); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Add a buddy if it is not allready added, set the status to logged in. |
---|
99 | */ |
---|
100 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
101 | { |
---|
102 | struct twitter_data *td = ic->proto_data; |
---|
103 | |
---|
104 | // Check if the buddy is allready in the buddy list. |
---|
105 | if (!imcb_find_buddy( ic, name )) |
---|
106 | { |
---|
107 | char *mode = set_getstr(&ic->acc->set, "mode"); |
---|
108 | |
---|
109 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
110 | imcb_add_buddy( ic, name, NULL ); |
---|
111 | imcb_rename_buddy( ic, name, fullname ); |
---|
112 | if (g_strcasecmp(mode, "chat") == 0) |
---|
113 | imcb_chat_add_buddy( td->home_timeline_gc, name ); |
---|
114 | else if (g_strcasecmp(mode, "many") == 0) |
---|
115 | imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL ); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
120 | |
---|
121 | /** |
---|
122 | * Get the friends ids. |
---|
123 | */ |
---|
124 | void twitter_get_friends_ids(struct im_connection *ic, int next_cursor) |
---|
125 | { |
---|
126 | struct twitter_data *td = ic->proto_data; |
---|
127 | |
---|
128 | // Primitive, but hey! It works... |
---|
129 | char* args[2]; |
---|
130 | args[0] = "cursor"; |
---|
131 | args[1] = g_strdup_printf ("%d", next_cursor); |
---|
132 | twitter_http(TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, td->user, td->pass, td->oauth_info, args, 2); |
---|
133 | |
---|
134 | g_free(args[1]); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Function to help fill a list. |
---|
139 | */ |
---|
140 | static xt_status twitter_xt_next_cursor( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
141 | { |
---|
142 | // Do something with the cursor. |
---|
143 | txl->next_cursor = node->text != NULL ? atoi(node->text) : -1; |
---|
144 | |
---|
145 | return XT_HANDLED; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Fill a list of ids. |
---|
150 | */ |
---|
151 | static xt_status twitter_xt_get_friends_id_list( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
152 | { |
---|
153 | struct xt_node *child; |
---|
154 | |
---|
155 | // Set the list type. |
---|
156 | txl->type = TXL_ID; |
---|
157 | |
---|
158 | // The root <statuses> node should hold the list of statuses <status> |
---|
159 | // Walk over the nodes children. |
---|
160 | for( child = node->children ; child ; child = child->next ) |
---|
161 | { |
---|
162 | if ( g_strcasecmp( "id", child->name ) == 0) |
---|
163 | { |
---|
164 | // Add the item to the list. |
---|
165 | txl->list = g_slist_append (txl->list, g_memdup( child->text, child->text_len + 1 )); |
---|
166 | } |
---|
167 | else if ( g_strcasecmp( "next_cursor", child->name ) == 0) |
---|
168 | { |
---|
169 | twitter_xt_next_cursor(child, txl); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | return XT_HANDLED; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * Callback for getting the friends ids. |
---|
178 | */ |
---|
179 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
180 | { |
---|
181 | struct im_connection *ic; |
---|
182 | struct xt_parser *parser; |
---|
183 | struct twitter_xml_list *txl; |
---|
184 | struct twitter_data *td; |
---|
185 | |
---|
186 | ic = req->data; |
---|
187 | |
---|
188 | // Check if the connection is still active. |
---|
189 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
190 | return; |
---|
191 | |
---|
192 | td = ic->proto_data; |
---|
193 | |
---|
194 | // Check if the HTTP request went well. |
---|
195 | if (req->status_code != 200) { |
---|
196 | // It didn't go well, output the error and return. |
---|
197 | if (++td->http_fails >= 5) |
---|
198 | imcb_error(ic, "Could not retrieve friends. HTTP STATUS: %d", req->status_code); |
---|
199 | |
---|
200 | return; |
---|
201 | } else { |
---|
202 | td->http_fails = 0; |
---|
203 | } |
---|
204 | |
---|
205 | txl = g_new0(struct twitter_xml_list, 1); |
---|
206 | |
---|
207 | // Parse the data. |
---|
208 | parser = xt_new( NULL, txl ); |
---|
209 | xt_feed( parser, req->reply_body, req->body_size ); |
---|
210 | twitter_xt_get_friends_id_list(parser->root, txl); |
---|
211 | xt_free( parser ); |
---|
212 | |
---|
213 | if (txl->next_cursor) |
---|
214 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
215 | |
---|
216 | txl_free(txl); |
---|
217 | g_free(txl); |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * Function to fill a twitter_xml_user struct. |
---|
222 | * It sets: |
---|
223 | * - the name and |
---|
224 | * - the screen_name. |
---|
225 | */ |
---|
226 | static xt_status twitter_xt_get_user( struct xt_node *node, struct twitter_xml_user *txu ) |
---|
227 | { |
---|
228 | struct xt_node *child; |
---|
229 | |
---|
230 | // Walk over the nodes children. |
---|
231 | for( child = node->children ; child ; child = child->next ) |
---|
232 | { |
---|
233 | if ( g_strcasecmp( "name", child->name ) == 0) |
---|
234 | { |
---|
235 | txu->name = g_memdup( child->text, child->text_len + 1 ); |
---|
236 | } |
---|
237 | else if (g_strcasecmp( "screen_name", child->name ) == 0) |
---|
238 | { |
---|
239 | txu->screen_name = g_memdup( child->text, child->text_len + 1 ); |
---|
240 | } |
---|
241 | } |
---|
242 | return XT_HANDLED; |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * Function to fill a twitter_xml_list struct. |
---|
247 | * It sets: |
---|
248 | * - all <user>s from the <users> element. |
---|
249 | */ |
---|
250 | static xt_status twitter_xt_get_users( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
251 | { |
---|
252 | struct twitter_xml_user *txu; |
---|
253 | struct xt_node *child; |
---|
254 | |
---|
255 | // Set the type of the list. |
---|
256 | txl->type = TXL_USER; |
---|
257 | |
---|
258 | // The root <users> node should hold the list of users <user> |
---|
259 | // Walk over the nodes children. |
---|
260 | for( child = node->children ; child ; child = child->next ) |
---|
261 | { |
---|
262 | if ( g_strcasecmp( "user", child->name ) == 0) |
---|
263 | { |
---|
264 | txu = g_new0(struct twitter_xml_user, 1); |
---|
265 | twitter_xt_get_user(child, txu); |
---|
266 | // Put the item in the front of the list. |
---|
267 | txl->list = g_slist_prepend (txl->list, txu); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | return XT_HANDLED; |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * Function to fill a twitter_xml_list struct. |
---|
276 | * It calls twitter_xt_get_users to get the <user>s from a <users> element. |
---|
277 | * It sets: |
---|
278 | * - the next_cursor. |
---|
279 | */ |
---|
280 | static xt_status twitter_xt_get_user_list( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
281 | { |
---|
282 | struct xt_node *child; |
---|
283 | |
---|
284 | // Set the type of the list. |
---|
285 | txl->type = TXL_USER; |
---|
286 | |
---|
287 | // The root <user_list> node should hold a users <users> element |
---|
288 | // Walk over the nodes children. |
---|
289 | for( child = node->children ; child ; child = child->next ) |
---|
290 | { |
---|
291 | if ( g_strcasecmp( "users", child->name ) == 0) |
---|
292 | { |
---|
293 | twitter_xt_get_users(child, txl); |
---|
294 | } |
---|
295 | else if ( g_strcasecmp( "next_cursor", child->name ) == 0) |
---|
296 | { |
---|
297 | twitter_xt_next_cursor(child, txl); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | return XT_HANDLED; |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | /** |
---|
306 | * Function to fill a twitter_xml_status struct. |
---|
307 | * It sets: |
---|
308 | * - the status text and |
---|
309 | * - the created_at timestamp and |
---|
310 | * - the status id and |
---|
311 | * - the user in a twitter_xml_user struct. |
---|
312 | */ |
---|
313 | static xt_status twitter_xt_get_status( struct xt_node *node, struct twitter_xml_status *txs ) |
---|
314 | { |
---|
315 | struct xt_node *child; |
---|
316 | |
---|
317 | // Walk over the nodes children. |
---|
318 | for( child = node->children ; child ; child = child->next ) |
---|
319 | { |
---|
320 | if ( g_strcasecmp( "text", child->name ) == 0) |
---|
321 | { |
---|
322 | txs->text = g_memdup( child->text, child->text_len + 1 ); |
---|
323 | } |
---|
324 | else if (g_strcasecmp( "created_at", child->name ) == 0) |
---|
325 | { |
---|
326 | struct tm parsed; |
---|
327 | |
---|
328 | /* Very sensitive to changes to the formatting of |
---|
329 | this field. :-( Also assumes the timezone used |
---|
330 | is UTC since C time handling functions suck. */ |
---|
331 | if( strptime( child->text, "%a %b %d %H:%M:%S %z %Y", &parsed ) != NULL ) |
---|
332 | txs->created_at = mktime_utc( &parsed ); |
---|
333 | } |
---|
334 | else if (g_strcasecmp( "user", child->name ) == 0) |
---|
335 | { |
---|
336 | txs->user = g_new0(struct twitter_xml_user, 1); |
---|
337 | twitter_xt_get_user( child, txs->user ); |
---|
338 | } |
---|
339 | else if (g_strcasecmp( "id", child->name ) == 0) |
---|
340 | { |
---|
341 | txs->id = g_ascii_strtoull (child->text, NULL, 10); |
---|
342 | } |
---|
343 | } |
---|
344 | return XT_HANDLED; |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * Function to fill a twitter_xml_list struct. |
---|
349 | * It sets: |
---|
350 | * - all <status>es within the <status> element and |
---|
351 | * - the next_cursor. |
---|
352 | */ |
---|
353 | static xt_status twitter_xt_get_status_list( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
354 | { |
---|
355 | struct twitter_xml_status *txs; |
---|
356 | struct xt_node *child; |
---|
357 | |
---|
358 | // Set the type of the list. |
---|
359 | txl->type = TXL_STATUS; |
---|
360 | |
---|
361 | // The root <statuses> node should hold the list of statuses <status> |
---|
362 | // Walk over the nodes children. |
---|
363 | for( child = node->children ; child ; child = child->next ) |
---|
364 | { |
---|
365 | if ( g_strcasecmp( "status", child->name ) == 0) |
---|
366 | { |
---|
367 | txs = g_new0(struct twitter_xml_status, 1); |
---|
368 | twitter_xt_get_status(child, txs); |
---|
369 | // Put the item in the front of the list. |
---|
370 | txl->list = g_slist_prepend (txl->list, txs); |
---|
371 | } |
---|
372 | else if ( g_strcasecmp( "next_cursor", child->name ) == 0) |
---|
373 | { |
---|
374 | twitter_xt_next_cursor(child, txl); |
---|
375 | } |
---|
376 | } |
---|
377 | |
---|
378 | return XT_HANDLED; |
---|
379 | } |
---|
380 | |
---|
381 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
382 | |
---|
383 | /** |
---|
384 | * Get the timeline. |
---|
385 | */ |
---|
386 | void twitter_get_home_timeline(struct im_connection *ic, int next_cursor) |
---|
387 | { |
---|
388 | struct twitter_data *td = ic->proto_data; |
---|
389 | |
---|
390 | char* args[4]; |
---|
391 | args[0] = "cursor"; |
---|
392 | args[1] = g_strdup_printf ("%d", next_cursor); |
---|
393 | if (td->home_timeline_id) { |
---|
394 | args[2] = "since_id"; |
---|
395 | args[3] = g_strdup_printf ("%llu", (long long unsigned int) td->home_timeline_id); |
---|
396 | } |
---|
397 | |
---|
398 | twitter_http(TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, td->user, td->pass, td->oauth_info, args, td->home_timeline_id ? 4 : 2); |
---|
399 | |
---|
400 | g_free(args[1]); |
---|
401 | if (td->home_timeline_id) { |
---|
402 | g_free(args[3]); |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | /** |
---|
407 | * Function that is called to see the statuses in a groupchat window. |
---|
408 | */ |
---|
409 | static void twitter_groupchat(struct im_connection *ic, GSList *list) |
---|
410 | { |
---|
411 | struct twitter_data *td = ic->proto_data; |
---|
412 | GSList *l = NULL; |
---|
413 | struct twitter_xml_status *status; |
---|
414 | struct groupchat *gc; |
---|
415 | |
---|
416 | // Create a new groupchat if it does not exsist. |
---|
417 | if (!td->home_timeline_gc) |
---|
418 | { |
---|
419 | char *name_hint = g_strdup_printf( "Twitter_%s", ic->acc->user ); |
---|
420 | td->home_timeline_gc = gc = imcb_chat_new( ic, "home/timeline" ); |
---|
421 | imcb_chat_name_hint( gc, name_hint ); |
---|
422 | g_free( name_hint ); |
---|
423 | // Add the current user to the chat... |
---|
424 | imcb_chat_add_buddy( gc, ic->acc->user ); |
---|
425 | } |
---|
426 | else |
---|
427 | { |
---|
428 | gc = td->home_timeline_gc; |
---|
429 | } |
---|
430 | |
---|
431 | for ( l = list; l ; l = g_slist_next(l) ) |
---|
432 | { |
---|
433 | status = l->data; |
---|
434 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
435 | |
---|
436 | // Say it! |
---|
437 | if (g_strcasecmp(td->user, status->user->screen_name) == 0) |
---|
438 | imcb_chat_log (gc, "Your Tweet: %s", status->text); |
---|
439 | else |
---|
440 | imcb_chat_msg (gc, status->user->screen_name, status->text, 0, status->created_at ); |
---|
441 | |
---|
442 | // Update the home_timeline_id to hold the highest id, so that by the next request |
---|
443 | // we won't pick up the updates allready in the list. |
---|
444 | td->home_timeline_id = td->home_timeline_id < status->id ? status->id : td->home_timeline_id; |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | /** |
---|
449 | * Function that is called to see statuses as private messages. |
---|
450 | */ |
---|
451 | static void twitter_private_message_chat(struct im_connection *ic, GSList *list) |
---|
452 | { |
---|
453 | struct twitter_data *td = ic->proto_data; |
---|
454 | GSList *l = NULL; |
---|
455 | struct twitter_xml_status *status; |
---|
456 | char from[MAX_STRING]; |
---|
457 | gboolean mode_one; |
---|
458 | |
---|
459 | mode_one = g_strcasecmp( set_getstr( &ic->acc->set, "mode" ), "one" ) == 0; |
---|
460 | |
---|
461 | if( mode_one ) |
---|
462 | { |
---|
463 | g_snprintf( from, sizeof( from ) - 1, "twitter_%s", ic->acc->user ); |
---|
464 | from[MAX_STRING-1] = '\0'; |
---|
465 | } |
---|
466 | |
---|
467 | for ( l = list; l ; l = g_slist_next(l) ) |
---|
468 | { |
---|
469 | char *text = NULL; |
---|
470 | |
---|
471 | status = l->data; |
---|
472 | |
---|
473 | if( mode_one ) |
---|
474 | text = g_strdup_printf( "\002<\002%s\002>\002 %s", |
---|
475 | status->user->screen_name, status->text ); |
---|
476 | else |
---|
477 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
478 | |
---|
479 | imcb_buddy_msg( ic, |
---|
480 | mode_one ? from : status->user->screen_name, |
---|
481 | mode_one ? text : status->text, |
---|
482 | 0, status->created_at ); |
---|
483 | |
---|
484 | // Update the home_timeline_id to hold the highest id, so that by the next request |
---|
485 | // we won't pick up the updates allready in the list. |
---|
486 | td->home_timeline_id = td->home_timeline_id < status->id ? status->id : td->home_timeline_id; |
---|
487 | |
---|
488 | g_free( text ); |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | /** |
---|
493 | * Callback for getting the home timeline. |
---|
494 | */ |
---|
495 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
496 | { |
---|
497 | struct im_connection *ic = req->data; |
---|
498 | struct twitter_data *td; |
---|
499 | struct xt_parser *parser; |
---|
500 | struct twitter_xml_list *txl; |
---|
501 | |
---|
502 | // Check if the connection is still active. |
---|
503 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
504 | return; |
---|
505 | |
---|
506 | td = ic->proto_data; |
---|
507 | |
---|
508 | // Check if the HTTP request went well. |
---|
509 | if (req->status_code == 200) |
---|
510 | { |
---|
511 | td->http_fails = 0; |
---|
512 | if (!(ic->flags & OPT_LOGGED_IN)) |
---|
513 | imcb_connected(ic); |
---|
514 | } |
---|
515 | else if (req->status_code == 401) |
---|
516 | { |
---|
517 | imcb_error( ic, "Authentication failure" ); |
---|
518 | imc_logout( ic, FALSE ); |
---|
519 | return; |
---|
520 | } |
---|
521 | else |
---|
522 | { |
---|
523 | // It didn't go well, output the error and return. |
---|
524 | if (++td->http_fails >= 5) |
---|
525 | imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL ". HTTP STATUS: %d", req->status_code); |
---|
526 | |
---|
527 | return; |
---|
528 | } |
---|
529 | |
---|
530 | txl = g_new0(struct twitter_xml_list, 1); |
---|
531 | txl->list = NULL; |
---|
532 | |
---|
533 | // Parse the data. |
---|
534 | parser = xt_new( NULL, txl ); |
---|
535 | xt_feed( parser, req->reply_body, req->body_size ); |
---|
536 | // The root <statuses> node should hold the list of statuses <status> |
---|
537 | twitter_xt_get_status_list(parser->root, txl); |
---|
538 | xt_free( parser ); |
---|
539 | |
---|
540 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
541 | if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0) |
---|
542 | twitter_groupchat(ic, txl->list); |
---|
543 | else |
---|
544 | twitter_private_message_chat(ic, txl->list); |
---|
545 | |
---|
546 | // Free the structure. |
---|
547 | txl_free(txl); |
---|
548 | g_free(txl); |
---|
549 | } |
---|
550 | |
---|
551 | /** |
---|
552 | * Callback for getting (twitter)friends... |
---|
553 | * |
---|
554 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
555 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
556 | * BitlBee... Get a life and meet new people! |
---|
557 | */ |
---|
558 | static void twitter_http_get_statuses_friends(struct http_request *req) |
---|
559 | { |
---|
560 | struct im_connection *ic = req->data; |
---|
561 | struct twitter_data *td; |
---|
562 | struct xt_parser *parser; |
---|
563 | struct twitter_xml_list *txl; |
---|
564 | GSList *l = NULL; |
---|
565 | struct twitter_xml_user *user; |
---|
566 | |
---|
567 | // Check if the connection is still active. |
---|
568 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
569 | return; |
---|
570 | |
---|
571 | td = ic->proto_data; |
---|
572 | |
---|
573 | // Check if the HTTP request went well. |
---|
574 | if (req->status_code != 200) { |
---|
575 | // It didn't go well, output the error and return. |
---|
576 | if (++td->http_fails >= 5) |
---|
577 | imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL " HTTP STATUS: %d", req->status_code); |
---|
578 | |
---|
579 | return; |
---|
580 | } else { |
---|
581 | td->http_fails = 0; |
---|
582 | } |
---|
583 | |
---|
584 | txl = g_new0(struct twitter_xml_list, 1); |
---|
585 | txl->list = NULL; |
---|
586 | |
---|
587 | // Parse the data. |
---|
588 | parser = xt_new( NULL, txl ); |
---|
589 | xt_feed( parser, req->reply_body, req->body_size ); |
---|
590 | |
---|
591 | // Get the user list from the parsed xml feed. |
---|
592 | twitter_xt_get_user_list(parser->root, txl); |
---|
593 | xt_free( parser ); |
---|
594 | |
---|
595 | // Add the users as buddies. |
---|
596 | for ( l = txl->list; l ; l = g_slist_next(l) ) |
---|
597 | { |
---|
598 | user = l->data; |
---|
599 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
600 | } |
---|
601 | |
---|
602 | // if the next_cursor is set to something bigger then 0 there are more friends to gather. |
---|
603 | if (txl->next_cursor > 0) |
---|
604 | twitter_get_statuses_friends(ic, txl->next_cursor); |
---|
605 | |
---|
606 | // Free the structure. |
---|
607 | txl_free(txl); |
---|
608 | g_free(txl); |
---|
609 | } |
---|
610 | |
---|
611 | /** |
---|
612 | * Get the friends. |
---|
613 | */ |
---|
614 | void twitter_get_statuses_friends(struct im_connection *ic, int next_cursor) |
---|
615 | { |
---|
616 | struct twitter_data *td = ic->proto_data; |
---|
617 | |
---|
618 | char* args[2]; |
---|
619 | args[0] = "cursor"; |
---|
620 | args[1] = g_strdup_printf ("%d", next_cursor); |
---|
621 | |
---|
622 | twitter_http(TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, td->user, td->pass, td->oauth_info, args, 2); |
---|
623 | |
---|
624 | g_free(args[1]); |
---|
625 | } |
---|
626 | |
---|
627 | /** |
---|
628 | * Callback after sending a new update to twitter. |
---|
629 | */ |
---|
630 | static void twitter_http_post_status(struct http_request *req) |
---|
631 | { |
---|
632 | struct im_connection *ic = req->data; |
---|
633 | |
---|
634 | // Check if the connection is still active. |
---|
635 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
636 | return; |
---|
637 | |
---|
638 | // Check if the HTTP request went well. |
---|
639 | if (req->status_code != 200) { |
---|
640 | // It didn't go well, output the error and return. |
---|
641 | imcb_error(ic, "Could not post message... HTTP STATUS: %d", req->status_code); |
---|
642 | return; |
---|
643 | } |
---|
644 | } |
---|
645 | |
---|
646 | /** |
---|
647 | * Function to POST a new status to twitter. |
---|
648 | */ |
---|
649 | void twitter_post_status(struct im_connection *ic, char* msg) |
---|
650 | { |
---|
651 | struct twitter_data *td = ic->proto_data; |
---|
652 | |
---|
653 | char* args[2]; |
---|
654 | args[0] = "status"; |
---|
655 | args[1] = msg; |
---|
656 | twitter_http(TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, td->user, td->pass, td->oauth_info, args, 2); |
---|
657 | // g_free(args[1]); |
---|
658 | } |
---|
659 | |
---|
660 | |
---|
661 | /** |
---|
662 | * Function to POST a new message to twitter. |
---|
663 | */ |
---|
664 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
665 | { |
---|
666 | struct twitter_data *td = ic->proto_data; |
---|
667 | |
---|
668 | char* args[4]; |
---|
669 | args[0] = "screen_name"; |
---|
670 | args[1] = who; |
---|
671 | args[2] = "text"; |
---|
672 | args[3] = msg; |
---|
673 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
674 | twitter_http(TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post_status, ic, 1, td->user, td->pass, td->oauth_info, args, 4); |
---|
675 | // g_free(args[1]); |
---|
676 | // g_free(args[3]); |
---|
677 | } |
---|