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