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