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 | }; |
---|
58 | |
---|
59 | struct twitter_xml_user { |
---|
60 | char *name; |
---|
61 | char *screen_name; |
---|
62 | }; |
---|
63 | |
---|
64 | struct twitter_xml_status { |
---|
65 | time_t created_at; |
---|
66 | char *text; |
---|
67 | struct twitter_xml_user *user; |
---|
68 | guint64 id, reply_to; |
---|
69 | }; |
---|
70 | |
---|
71 | static void twitter_groupchat_init(struct im_connection *ic); |
---|
72 | |
---|
73 | /** |
---|
74 | * Frees a twitter_xml_user struct. |
---|
75 | */ |
---|
76 | static void txu_free(struct twitter_xml_user *txu) |
---|
77 | { |
---|
78 | if (txu == NULL) |
---|
79 | return; |
---|
80 | |
---|
81 | g_free(txu->name); |
---|
82 | g_free(txu->screen_name); |
---|
83 | g_free(txu); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Frees a twitter_xml_status struct. |
---|
88 | */ |
---|
89 | static void txs_free(struct twitter_xml_status *txs) |
---|
90 | { |
---|
91 | if (txs == NULL) |
---|
92 | return; |
---|
93 | |
---|
94 | g_free(txs->text); |
---|
95 | txu_free(txs->user); |
---|
96 | g_free(txs); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Free a twitter_xml_list struct. |
---|
101 | * type is the type of list the struct holds. |
---|
102 | */ |
---|
103 | static void txl_free(struct twitter_xml_list *txl) |
---|
104 | { |
---|
105 | GSList *l; |
---|
106 | if (txl == NULL) |
---|
107 | return; |
---|
108 | |
---|
109 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
110 | if (txl->type == TXL_STATUS) { |
---|
111 | txs_free((struct twitter_xml_status *) l->data); |
---|
112 | } else if (txl->type == TXL_ID) { |
---|
113 | g_free(l->data); |
---|
114 | } else if (txl->type == TXL_USER) { |
---|
115 | txu_free(l->data); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | g_slist_free(txl->list); |
---|
120 | g_free(txl); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Compare status elements |
---|
125 | */ |
---|
126 | static gint twitter_compare_elements(gconstpointer a, gconstpointer b) |
---|
127 | { |
---|
128 | struct twitter_xml_status *a_status = (struct twitter_xml_status *) a; |
---|
129 | struct twitter_xml_status *b_status = (struct twitter_xml_status *) b; |
---|
130 | |
---|
131 | if (a_status->created_at < b_status->created_at) { |
---|
132 | return -1; |
---|
133 | } else if (a_status->created_at > b_status->created_at) { |
---|
134 | return 1; |
---|
135 | } else { |
---|
136 | return 0; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * Add a buddy if it is not already added, set the status to logged in. |
---|
142 | */ |
---|
143 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
144 | { |
---|
145 | struct twitter_data *td = ic->proto_data; |
---|
146 | |
---|
147 | // Check if the buddy is already in the buddy list. |
---|
148 | if (!bee_user_by_handle(ic->bee, ic, name)) { |
---|
149 | char *mode = set_getstr(&ic->acc->set, "mode"); |
---|
150 | |
---|
151 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
152 | imcb_add_buddy(ic, name, NULL); |
---|
153 | imcb_rename_buddy(ic, name, fullname); |
---|
154 | if (g_strcasecmp(mode, "chat") == 0) { |
---|
155 | /* Necessary so that nicks always get translated to the |
---|
156 | exact Twitter username. */ |
---|
157 | imcb_buddy_nick_hint(ic, name, name); |
---|
158 | imcb_chat_add_buddy(td->timeline_gc, name); |
---|
159 | } else if (g_strcasecmp(mode, "many") == 0) |
---|
160 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | /* Warning: May return a malloc()ed value, which will be free()d on the next |
---|
165 | call. Only for short-term use. NOT THREADSAFE! */ |
---|
166 | char *twitter_parse_error(struct http_request *req) |
---|
167 | { |
---|
168 | static char *ret = NULL; |
---|
169 | struct xt_parser *xp = NULL; |
---|
170 | struct xt_node *node, *err; |
---|
171 | |
---|
172 | g_free(ret); |
---|
173 | ret = NULL; |
---|
174 | |
---|
175 | if (req->body_size > 0) { |
---|
176 | xp = xt_new(NULL, NULL); |
---|
177 | xt_feed(xp, req->reply_body, req->body_size); |
---|
178 | |
---|
179 | for (node = xp->root; node; node = node->next) |
---|
180 | if ((err = xt_find_node(node->children, "error")) && err->text_len > 0) { |
---|
181 | ret = g_strdup_printf("%s (%s)", req->status_string, err->text); |
---|
182 | break; |
---|
183 | } |
---|
184 | |
---|
185 | xt_free(xp); |
---|
186 | } |
---|
187 | |
---|
188 | return ret ? ret : req->status_string; |
---|
189 | } |
---|
190 | |
---|
191 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
192 | |
---|
193 | /** |
---|
194 | * Get the friends ids. |
---|
195 | */ |
---|
196 | void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) |
---|
197 | { |
---|
198 | // Primitive, but hey! It works... |
---|
199 | char *args[2]; |
---|
200 | args[0] = "cursor"; |
---|
201 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
202 | twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); |
---|
203 | |
---|
204 | g_free(args[1]); |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Function to help fill a list. |
---|
209 | */ |
---|
210 | static xt_status twitter_xt_next_cursor(struct xt_node *node, struct twitter_xml_list *txl) |
---|
211 | { |
---|
212 | char *end = NULL; |
---|
213 | |
---|
214 | if (node->text) |
---|
215 | txl->next_cursor = g_ascii_strtoll(node->text, &end, 10); |
---|
216 | if (end == NULL) |
---|
217 | txl->next_cursor = -1; |
---|
218 | |
---|
219 | return XT_HANDLED; |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * Fill a list of ids. |
---|
224 | */ |
---|
225 | static xt_status twitter_xt_get_friends_id_list(struct xt_node *node, struct twitter_xml_list *txl) |
---|
226 | { |
---|
227 | struct xt_node *child; |
---|
228 | |
---|
229 | // Set the list type. |
---|
230 | txl->type = TXL_ID; |
---|
231 | |
---|
232 | // The root <statuses> node should hold the list of statuses <status> |
---|
233 | // Walk over the nodes children. |
---|
234 | for (child = node->children; child; child = child->next) { |
---|
235 | if (g_strcasecmp("ids", child->name) == 0) { |
---|
236 | struct xt_node *idc; |
---|
237 | for (idc = child->children; idc; idc = idc->next) |
---|
238 | if (g_strcasecmp(idc->name, "id") == 0) |
---|
239 | txl->list = g_slist_prepend(txl->list, |
---|
240 | g_memdup(idc->text, idc->text_len + 1)); |
---|
241 | } else if (g_strcasecmp("next_cursor", child->name) == 0) { |
---|
242 | twitter_xt_next_cursor(child, txl); |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | return XT_HANDLED; |
---|
247 | } |
---|
248 | |
---|
249 | static void twitter_get_users_lookup(struct im_connection *ic); |
---|
250 | |
---|
251 | /** |
---|
252 | * Callback for getting the friends ids. |
---|
253 | */ |
---|
254 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
255 | { |
---|
256 | struct im_connection *ic; |
---|
257 | struct xt_parser *parser; |
---|
258 | struct twitter_xml_list *txl; |
---|
259 | struct twitter_data *td; |
---|
260 | |
---|
261 | ic = req->data; |
---|
262 | |
---|
263 | // Check if the connection is still active. |
---|
264 | if (!g_slist_find(twitter_connections, ic)) |
---|
265 | return; |
---|
266 | |
---|
267 | td = ic->proto_data; |
---|
268 | |
---|
269 | // Check if the HTTP request went well. More strict checks as this is |
---|
270 | // the first request we do in a session. |
---|
271 | if (req->status_code == 401) { |
---|
272 | imcb_error(ic, "Authentication failure"); |
---|
273 | imc_logout(ic, FALSE); |
---|
274 | return; |
---|
275 | } else if (req->status_code != 200) { |
---|
276 | // It didn't go well, output the error and return. |
---|
277 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
278 | TWITTER_FRIENDS_IDS_URL, twitter_parse_error(req)); |
---|
279 | imc_logout(ic, TRUE); |
---|
280 | return; |
---|
281 | } else { |
---|
282 | td->http_fails = 0; |
---|
283 | } |
---|
284 | |
---|
285 | /* Create the room now that we "logged in". */ |
---|
286 | if (!td->timeline_gc && g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0) |
---|
287 | twitter_groupchat_init(ic); |
---|
288 | |
---|
289 | txl = g_new0(struct twitter_xml_list, 1); |
---|
290 | txl->list = td->follow_ids; |
---|
291 | |
---|
292 | // Parse the data. |
---|
293 | parser = xt_new(NULL, txl); |
---|
294 | xt_feed(parser, req->reply_body, req->body_size); |
---|
295 | twitter_xt_get_friends_id_list(parser->root, txl); |
---|
296 | xt_free(parser); |
---|
297 | |
---|
298 | td->follow_ids = txl->list; |
---|
299 | if (txl->next_cursor) |
---|
300 | /* These were just numbers. Up to 4000 in a response AFAIK so if we get here |
---|
301 | we may be using a spammer account. \o/ */ |
---|
302 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
303 | else |
---|
304 | /* Now to convert all those numbers into names.. */ |
---|
305 | twitter_get_users_lookup(ic); |
---|
306 | |
---|
307 | txl->list = NULL; |
---|
308 | txl_free(txl); |
---|
309 | } |
---|
310 | |
---|
311 | static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl); |
---|
312 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
313 | |
---|
314 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
315 | { |
---|
316 | struct twitter_data *td = ic->proto_data; |
---|
317 | char *args[2] = { |
---|
318 | "user_id", |
---|
319 | NULL, |
---|
320 | }; |
---|
321 | GString *ids = g_string_new(""); |
---|
322 | int i; |
---|
323 | |
---|
324 | /* We can request up to 100 users at a time. */ |
---|
325 | for (i = 0; i < 100 && td->follow_ids; i ++) { |
---|
326 | g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data); |
---|
327 | g_free(td->follow_ids->data); |
---|
328 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
329 | } |
---|
330 | if (ids->len > 0) { |
---|
331 | args[1] = ids->str + 1; |
---|
332 | /* POST, because I think ids can be up to 1KB long. */ |
---|
333 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
334 | } else { |
---|
335 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
336 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
337 | twitter_login_finish(ic); |
---|
338 | } |
---|
339 | g_string_free(ids, TRUE); |
---|
340 | } |
---|
341 | |
---|
342 | /** |
---|
343 | * Callback for getting (twitter)friends... |
---|
344 | * |
---|
345 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
346 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
347 | * BitlBee... Get a life and meet new people! |
---|
348 | */ |
---|
349 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
350 | { |
---|
351 | struct im_connection *ic = req->data; |
---|
352 | struct twitter_data *td; |
---|
353 | struct xt_parser *parser; |
---|
354 | struct twitter_xml_list *txl; |
---|
355 | GSList *l = NULL; |
---|
356 | struct twitter_xml_user *user; |
---|
357 | |
---|
358 | // Check if the connection is still active. |
---|
359 | if (!g_slist_find(twitter_connections, ic)) |
---|
360 | return; |
---|
361 | |
---|
362 | td = ic->proto_data; |
---|
363 | |
---|
364 | if (req->status_code != 200) { |
---|
365 | // It didn't go well, output the error and return. |
---|
366 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
367 | TWITTER_USERS_LOOKUP_URL, twitter_parse_error(req)); |
---|
368 | imc_logout(ic, TRUE); |
---|
369 | return; |
---|
370 | } else { |
---|
371 | td->http_fails = 0; |
---|
372 | } |
---|
373 | |
---|
374 | txl = g_new0(struct twitter_xml_list, 1); |
---|
375 | txl->list = NULL; |
---|
376 | |
---|
377 | // Parse the data. |
---|
378 | parser = xt_new(NULL, txl); |
---|
379 | xt_feed(parser, req->reply_body, req->body_size); |
---|
380 | |
---|
381 | // Get the user list from the parsed xml feed. |
---|
382 | twitter_xt_get_users(parser->root, txl); |
---|
383 | xt_free(parser); |
---|
384 | |
---|
385 | // Add the users as buddies. |
---|
386 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
387 | user = l->data; |
---|
388 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
389 | } |
---|
390 | |
---|
391 | // Free the structure. |
---|
392 | txl_free(txl); |
---|
393 | |
---|
394 | twitter_get_users_lookup(ic); |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * Function to fill a twitter_xml_user struct. |
---|
399 | * It sets: |
---|
400 | * - the name and |
---|
401 | * - the screen_name. |
---|
402 | */ |
---|
403 | static xt_status twitter_xt_get_user(struct xt_node *node, struct twitter_xml_user *txu) |
---|
404 | { |
---|
405 | struct xt_node *child; |
---|
406 | |
---|
407 | // Walk over the nodes children. |
---|
408 | for (child = node->children; child; child = child->next) { |
---|
409 | if (g_strcasecmp("name", child->name) == 0) { |
---|
410 | txu->name = g_memdup(child->text, child->text_len + 1); |
---|
411 | } else if (g_strcasecmp("screen_name", child->name) == 0) { |
---|
412 | txu->screen_name = g_memdup(child->text, child->text_len + 1); |
---|
413 | } |
---|
414 | } |
---|
415 | return XT_HANDLED; |
---|
416 | } |
---|
417 | |
---|
418 | /** |
---|
419 | * Function to fill a twitter_xml_list struct. |
---|
420 | * It sets: |
---|
421 | * - all <user>s from the <users> element. |
---|
422 | */ |
---|
423 | static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl) |
---|
424 | { |
---|
425 | struct twitter_xml_user *txu; |
---|
426 | struct xt_node *child; |
---|
427 | |
---|
428 | // Set the type of the list. |
---|
429 | txl->type = TXL_USER; |
---|
430 | |
---|
431 | // The root <users> node should hold the list of users <user> |
---|
432 | // Walk over the nodes children. |
---|
433 | for (child = node->children; child; child = child->next) { |
---|
434 | if (g_strcasecmp("user", child->name) == 0) { |
---|
435 | txu = g_new0(struct twitter_xml_user, 1); |
---|
436 | twitter_xt_get_user(child, txu); |
---|
437 | // Put the item in the front of the list. |
---|
438 | txl->list = g_slist_prepend(txl->list, txu); |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | return XT_HANDLED; |
---|
443 | } |
---|
444 | |
---|
445 | #ifdef __GLIBC__ |
---|
446 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
447 | #else |
---|
448 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
449 | #endif |
---|
450 | |
---|
451 | /** |
---|
452 | * Function to fill a twitter_xml_status struct. |
---|
453 | * It sets: |
---|
454 | * - the status text and |
---|
455 | * - the created_at timestamp and |
---|
456 | * - the status id and |
---|
457 | * - the user in a twitter_xml_user struct. |
---|
458 | */ |
---|
459 | static xt_status twitter_xt_get_status(struct xt_node *node, struct twitter_xml_status *txs) |
---|
460 | { |
---|
461 | struct xt_node *child, *rt = NULL; |
---|
462 | gboolean truncated = FALSE; |
---|
463 | |
---|
464 | // Walk over the nodes children. |
---|
465 | for (child = node->children; child; child = child->next) { |
---|
466 | if (g_strcasecmp("text", child->name) == 0) { |
---|
467 | txs->text = g_memdup(child->text, child->text_len + 1); |
---|
468 | } else if (g_strcasecmp("truncated", child->name) == 0 && child->text) { |
---|
469 | truncated = bool2int(child->text); |
---|
470 | } else if (g_strcasecmp("retweeted_status", child->name) == 0) { |
---|
471 | rt = child; |
---|
472 | } else if (g_strcasecmp("created_at", child->name) == 0) { |
---|
473 | struct tm parsed; |
---|
474 | |
---|
475 | /* Very sensitive to changes to the formatting of |
---|
476 | this field. :-( Also assumes the timezone used |
---|
477 | is UTC since C time handling functions suck. */ |
---|
478 | if (strptime(child->text, TWITTER_TIME_FORMAT, &parsed) != NULL) |
---|
479 | txs->created_at = mktime_utc(&parsed); |
---|
480 | } else if (g_strcasecmp("user", child->name) == 0) { |
---|
481 | txs->user = g_new0(struct twitter_xml_user, 1); |
---|
482 | twitter_xt_get_user(child, txs->user); |
---|
483 | } else if (g_strcasecmp("id", child->name) == 0) { |
---|
484 | txs->id = g_ascii_strtoull(child->text, NULL, 10); |
---|
485 | } else if (g_strcasecmp("in_reply_to_status_id", child->name) == 0) { |
---|
486 | txs->reply_to = g_ascii_strtoull(child->text, NULL, 10); |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | /* If it's a truncated retweet, get the original because dots suck. */ |
---|
491 | if (truncated && rt) { |
---|
492 | struct twitter_xml_status *rtxs = g_new0(struct twitter_xml_status, 1); |
---|
493 | if (twitter_xt_get_status(rt, rtxs) != XT_HANDLED) { |
---|
494 | txs_free(rtxs); |
---|
495 | return XT_HANDLED; |
---|
496 | } |
---|
497 | |
---|
498 | g_free(txs->text); |
---|
499 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
500 | txs_free(rtxs); |
---|
501 | } |
---|
502 | |
---|
503 | return XT_HANDLED; |
---|
504 | } |
---|
505 | |
---|
506 | /** |
---|
507 | * Function to fill a twitter_xml_list struct. |
---|
508 | * It sets: |
---|
509 | * - all <status>es within the <status> element and |
---|
510 | * - the next_cursor. |
---|
511 | */ |
---|
512 | static xt_status twitter_xt_get_status_list(struct im_connection *ic, struct xt_node *node, |
---|
513 | struct twitter_xml_list *txl) |
---|
514 | { |
---|
515 | struct twitter_xml_status *txs; |
---|
516 | struct xt_node *child; |
---|
517 | bee_user_t *bu; |
---|
518 | |
---|
519 | // Set the type of the list. |
---|
520 | txl->type = TXL_STATUS; |
---|
521 | |
---|
522 | // The root <statuses> node should hold the list of statuses <status> |
---|
523 | // Walk over the nodes children. |
---|
524 | for (child = node->children; child; child = child->next) { |
---|
525 | if (g_strcasecmp("status", child->name) == 0) { |
---|
526 | txs = g_new0(struct twitter_xml_status, 1); |
---|
527 | twitter_xt_get_status(child, txs); |
---|
528 | // Put the item in the front of the list. |
---|
529 | txl->list = g_slist_prepend(txl->list, txs); |
---|
530 | |
---|
531 | if (txs->user && txs->user->screen_name && |
---|
532 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
533 | struct twitter_user_data *tud = bu->data; |
---|
534 | |
---|
535 | if (txs->id > tud->last_id) { |
---|
536 | tud->last_id = txs->id; |
---|
537 | tud->last_time = txs->created_at; |
---|
538 | } |
---|
539 | } |
---|
540 | } else if (g_strcasecmp("next_cursor", child->name) == 0) { |
---|
541 | twitter_xt_next_cursor(child, txl); |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | return XT_HANDLED; |
---|
546 | } |
---|
547 | |
---|
548 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
549 | struct twitter_xml_status *txs, const char *prefix) |
---|
550 | { |
---|
551 | struct twitter_data *td = ic->proto_data; |
---|
552 | char *ret = NULL; |
---|
553 | |
---|
554 | if (!set_getbool(&ic->acc->set, "show_ids")) { |
---|
555 | if (*prefix) |
---|
556 | return g_strconcat(prefix, txs->text, NULL); |
---|
557 | else |
---|
558 | return NULL; |
---|
559 | } |
---|
560 | |
---|
561 | td->log[td->log_id].id = txs->id; |
---|
562 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
563 | if (txs->reply_to) { |
---|
564 | int i; |
---|
565 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) |
---|
566 | if (td->log[i].id == txs->reply_to) { |
---|
567 | ret = g_strdup_printf("\002[\002%02d->%02d\002]\002 %s%s", |
---|
568 | td->log_id, i, prefix, txs->text); |
---|
569 | break; |
---|
570 | } |
---|
571 | } |
---|
572 | if (ret == NULL) |
---|
573 | ret = g_strdup_printf("\002[\002%02d\002]\002 %s%s", td->log_id, prefix, txs->text); |
---|
574 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
575 | |
---|
576 | return ret; |
---|
577 | } |
---|
578 | |
---|
579 | static void twitter_groupchat_init(struct im_connection *ic) |
---|
580 | { |
---|
581 | char *name_hint; |
---|
582 | struct groupchat *gc; |
---|
583 | struct twitter_data *td = ic->proto_data; |
---|
584 | GSList *l; |
---|
585 | |
---|
586 | td->timeline_gc = gc = imcb_chat_new(ic, "twitter/timeline"); |
---|
587 | |
---|
588 | name_hint = g_strdup_printf("%s_%s", td->prefix, ic->acc->user); |
---|
589 | imcb_chat_name_hint(gc, name_hint); |
---|
590 | g_free(name_hint); |
---|
591 | |
---|
592 | for (l = ic->bee->users; l; l = l->next) { |
---|
593 | bee_user_t *bu = l->data; |
---|
594 | if (bu->ic == ic) |
---|
595 | imcb_chat_add_buddy(td->timeline_gc, bu->handle); |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | /** |
---|
600 | * Function that is called to see the statuses in a groupchat window. |
---|
601 | */ |
---|
602 | static void twitter_groupchat(struct im_connection *ic, GSList * list) |
---|
603 | { |
---|
604 | struct twitter_data *td = ic->proto_data; |
---|
605 | GSList *l = NULL; |
---|
606 | struct twitter_xml_status *status; |
---|
607 | struct groupchat *gc; |
---|
608 | guint64 last_id = 0; |
---|
609 | |
---|
610 | // Create a new groupchat if it does not exsist. |
---|
611 | if (!td->timeline_gc) |
---|
612 | twitter_groupchat_init(ic); |
---|
613 | |
---|
614 | gc = td->timeline_gc; |
---|
615 | if (!gc->joined) |
---|
616 | imcb_chat_add_buddy(gc, ic->acc->user); |
---|
617 | |
---|
618 | for (l = list; l; l = g_slist_next(l)) { |
---|
619 | char *msg; |
---|
620 | |
---|
621 | status = l->data; |
---|
622 | if (status->user == NULL || status->text == NULL || last_id == status->id) |
---|
623 | continue; |
---|
624 | |
---|
625 | last_id = status->id; |
---|
626 | |
---|
627 | strip_html(status->text); |
---|
628 | |
---|
629 | msg = twitter_msg_add_id(ic, status, ""); |
---|
630 | |
---|
631 | // Say it! |
---|
632 | if (g_strcasecmp(td->user, status->user->screen_name) == 0) { |
---|
633 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
634 | } else { |
---|
635 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
636 | |
---|
637 | imcb_chat_msg(gc, status->user->screen_name, |
---|
638 | msg ? msg : status->text, 0, status->created_at); |
---|
639 | } |
---|
640 | |
---|
641 | g_free(msg); |
---|
642 | |
---|
643 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
644 | // we won't pick up the updates already in the list. |
---|
645 | td->timeline_id = MAX(td->timeline_id, status->id); |
---|
646 | } |
---|
647 | } |
---|
648 | |
---|
649 | /** |
---|
650 | * Function that is called to see statuses as private messages. |
---|
651 | */ |
---|
652 | static void twitter_private_message_chat(struct im_connection *ic, GSList * list) |
---|
653 | { |
---|
654 | struct twitter_data *td = ic->proto_data; |
---|
655 | GSList *l = NULL; |
---|
656 | struct twitter_xml_status *status; |
---|
657 | char from[MAX_STRING]; |
---|
658 | gboolean mode_one; |
---|
659 | guint64 last_id = 0; |
---|
660 | |
---|
661 | mode_one = g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") == 0; |
---|
662 | |
---|
663 | if (mode_one) { |
---|
664 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
665 | from[MAX_STRING - 1] = '\0'; |
---|
666 | } |
---|
667 | |
---|
668 | for (l = list; l; l = g_slist_next(l)) { |
---|
669 | char *prefix = NULL, *text = NULL; |
---|
670 | |
---|
671 | status = l->data; |
---|
672 | if (status->user == NULL || status->text == NULL || last_id == status->id) |
---|
673 | continue; |
---|
674 | |
---|
675 | last_id = status->id; |
---|
676 | |
---|
677 | strip_html(status->text); |
---|
678 | if (mode_one) |
---|
679 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
680 | status->user->screen_name); |
---|
681 | else |
---|
682 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
683 | |
---|
684 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
685 | |
---|
686 | imcb_buddy_msg(ic, |
---|
687 | mode_one ? from : status->user->screen_name, |
---|
688 | text ? text : status->text, 0, status->created_at); |
---|
689 | |
---|
690 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
691 | // we won't pick up the updates already in the list. |
---|
692 | td->timeline_id = MAX(td->timeline_id, status->id); |
---|
693 | |
---|
694 | g_free(text); |
---|
695 | g_free(prefix); |
---|
696 | } |
---|
697 | } |
---|
698 | |
---|
699 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
700 | static void twitter_http_get_mentions(struct http_request *req); |
---|
701 | |
---|
702 | /** |
---|
703 | * Get the timeline with optionally mentions |
---|
704 | */ |
---|
705 | void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
706 | { |
---|
707 | struct twitter_data *td = ic->proto_data; |
---|
708 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
709 | |
---|
710 | if (td->flags & TWITTER_DOING_TIMELINE) { |
---|
711 | return; |
---|
712 | } |
---|
713 | |
---|
714 | td->flags |= TWITTER_DOING_TIMELINE; |
---|
715 | |
---|
716 | twitter_get_home_timeline(ic, next_cursor); |
---|
717 | |
---|
718 | if (include_mentions) { |
---|
719 | twitter_get_mentions(ic, next_cursor); |
---|
720 | } |
---|
721 | } |
---|
722 | |
---|
723 | /** |
---|
724 | * Call this one after receiving timeline/mentions. Show to user once we have |
---|
725 | * both. |
---|
726 | */ |
---|
727 | void twitter_flush_timeline(struct im_connection *ic) |
---|
728 | { |
---|
729 | struct twitter_data *td = ic->proto_data; |
---|
730 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
731 | gboolean show_old_mentions = set_getbool(&ic->acc->set, "show_old_mentions"); |
---|
732 | struct twitter_xml_list *home_timeline = td->home_timeline_obj; |
---|
733 | struct twitter_xml_list *mentions = td->mentions_obj; |
---|
734 | GSList *output = NULL; |
---|
735 | GSList *l; |
---|
736 | |
---|
737 | if (!(td->flags & TWITTER_GOT_TIMELINE)) { |
---|
738 | return; |
---|
739 | } |
---|
740 | |
---|
741 | if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) { |
---|
742 | return; |
---|
743 | } |
---|
744 | |
---|
745 | if (home_timeline && home_timeline->list) { |
---|
746 | for (l = home_timeline->list; l; l = g_slist_next(l)) { |
---|
747 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
748 | } |
---|
749 | } |
---|
750 | |
---|
751 | if (include_mentions && mentions && mentions->list) { |
---|
752 | for (l = mentions->list; l; l = g_slist_next(l)) { |
---|
753 | if (!show_old_mentions && output && twitter_compare_elements(l->data, output->data) < 0) { |
---|
754 | continue; |
---|
755 | } |
---|
756 | |
---|
757 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
758 | } |
---|
759 | } |
---|
760 | |
---|
761 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
762 | if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0) |
---|
763 | twitter_groupchat(ic, output); |
---|
764 | else |
---|
765 | twitter_private_message_chat(ic, output); |
---|
766 | |
---|
767 | g_slist_free(output); |
---|
768 | |
---|
769 | if (home_timeline && home_timeline->list) { |
---|
770 | txl_free(home_timeline); |
---|
771 | } |
---|
772 | |
---|
773 | if (mentions && mentions->list) { |
---|
774 | txl_free(mentions); |
---|
775 | } |
---|
776 | |
---|
777 | td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS); |
---|
778 | } |
---|
779 | |
---|
780 | /** |
---|
781 | * Get the timeline. |
---|
782 | */ |
---|
783 | void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
784 | { |
---|
785 | struct twitter_data *td = ic->proto_data; |
---|
786 | |
---|
787 | td->home_timeline_obj = NULL; |
---|
788 | td->flags &= ~TWITTER_GOT_TIMELINE; |
---|
789 | |
---|
790 | char *args[4]; |
---|
791 | args[0] = "cursor"; |
---|
792 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
793 | if (td->timeline_id) { |
---|
794 | args[2] = "since_id"; |
---|
795 | args[3] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id); |
---|
796 | } |
---|
797 | |
---|
798 | twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, |
---|
799 | td->timeline_id ? 4 : 2); |
---|
800 | |
---|
801 | g_free(args[1]); |
---|
802 | if (td->timeline_id) { |
---|
803 | g_free(args[3]); |
---|
804 | } |
---|
805 | } |
---|
806 | |
---|
807 | /** |
---|
808 | * Get mentions. |
---|
809 | */ |
---|
810 | void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) |
---|
811 | { |
---|
812 | struct twitter_data *td = ic->proto_data; |
---|
813 | |
---|
814 | td->mentions_obj = NULL; |
---|
815 | td->flags &= ~TWITTER_GOT_MENTIONS; |
---|
816 | |
---|
817 | char *args[4]; |
---|
818 | args[0] = "cursor"; |
---|
819 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
820 | if (td->timeline_id) { |
---|
821 | args[2] = "since_id"; |
---|
822 | args[3] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id); |
---|
823 | } |
---|
824 | |
---|
825 | twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions, ic, 0, args, |
---|
826 | td->timeline_id ? 4 : 2); |
---|
827 | |
---|
828 | g_free(args[1]); |
---|
829 | if (td->timeline_id) { |
---|
830 | g_free(args[3]); |
---|
831 | } |
---|
832 | } |
---|
833 | |
---|
834 | /** |
---|
835 | * Callback for getting the home timeline. |
---|
836 | */ |
---|
837 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
838 | { |
---|
839 | struct im_connection *ic = req->data; |
---|
840 | struct twitter_data *td; |
---|
841 | struct xt_parser *parser; |
---|
842 | struct twitter_xml_list *txl; |
---|
843 | |
---|
844 | // Check if the connection is still active. |
---|
845 | if (!g_slist_find(twitter_connections, ic)) |
---|
846 | return; |
---|
847 | |
---|
848 | td = ic->proto_data; |
---|
849 | |
---|
850 | // Check if the HTTP request went well. |
---|
851 | if (req->status_code == 200) { |
---|
852 | td->http_fails = 0; |
---|
853 | if (!(ic->flags & OPT_LOGGED_IN)) |
---|
854 | imcb_connected(ic); |
---|
855 | } else if (req->status_code == 401) { |
---|
856 | imcb_error(ic, "Authentication failure"); |
---|
857 | imc_logout(ic, FALSE); |
---|
858 | goto end; |
---|
859 | } else { |
---|
860 | // It didn't go well, output the error and return. |
---|
861 | if (++td->http_fails >= 5) |
---|
862 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
863 | TWITTER_HOME_TIMELINE_URL, twitter_parse_error(req)); |
---|
864 | |
---|
865 | goto end; |
---|
866 | } |
---|
867 | |
---|
868 | txl = g_new0(struct twitter_xml_list, 1); |
---|
869 | txl->list = NULL; |
---|
870 | |
---|
871 | // Parse the data. |
---|
872 | parser = xt_new(NULL, txl); |
---|
873 | xt_feed(parser, req->reply_body, req->body_size); |
---|
874 | // The root <statuses> node should hold the list of statuses <status> |
---|
875 | twitter_xt_get_status_list(ic, parser->root, txl); |
---|
876 | xt_free(parser); |
---|
877 | |
---|
878 | td->home_timeline_obj = txl; |
---|
879 | |
---|
880 | end: |
---|
881 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
882 | |
---|
883 | twitter_flush_timeline(ic); |
---|
884 | } |
---|
885 | |
---|
886 | /** |
---|
887 | * Callback for getting mentions. |
---|
888 | */ |
---|
889 | static void twitter_http_get_mentions(struct http_request *req) |
---|
890 | { |
---|
891 | struct im_connection *ic = req->data; |
---|
892 | struct twitter_data *td; |
---|
893 | struct xt_parser *parser; |
---|
894 | struct twitter_xml_list *txl; |
---|
895 | |
---|
896 | // Check if the connection is still active. |
---|
897 | if (!g_slist_find(twitter_connections, ic)) |
---|
898 | return; |
---|
899 | |
---|
900 | td = ic->proto_data; |
---|
901 | |
---|
902 | // Check if the HTTP request went well. |
---|
903 | if (req->status_code == 200) { |
---|
904 | td->http_fails = 0; |
---|
905 | if (!(ic->flags & OPT_LOGGED_IN)) |
---|
906 | imcb_connected(ic); |
---|
907 | } else if (req->status_code == 401) { |
---|
908 | imcb_error(ic, "Authentication failure"); |
---|
909 | imc_logout(ic, FALSE); |
---|
910 | goto end; |
---|
911 | } else { |
---|
912 | // It didn't go well, output the error and return. |
---|
913 | if (++td->http_fails >= 5) |
---|
914 | imcb_error(ic, "Could not retrieve " TWITTER_MENTIONS_URL ": %s", |
---|
915 | twitter_parse_error(req)); |
---|
916 | |
---|
917 | goto end; |
---|
918 | } |
---|
919 | |
---|
920 | txl = g_new0(struct twitter_xml_list, 1); |
---|
921 | txl->list = NULL; |
---|
922 | |
---|
923 | // Parse the data. |
---|
924 | parser = xt_new(NULL, txl); |
---|
925 | xt_feed(parser, req->reply_body, req->body_size); |
---|
926 | // The root <statuses> node should hold the list of statuses <status> |
---|
927 | twitter_xt_get_status_list(ic, parser->root, txl); |
---|
928 | xt_free(parser); |
---|
929 | |
---|
930 | td->mentions_obj = txl; |
---|
931 | |
---|
932 | end: |
---|
933 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
934 | |
---|
935 | twitter_flush_timeline(ic); |
---|
936 | } |
---|
937 | |
---|
938 | /** |
---|
939 | * Callback to use after sending a POST request to twitter. |
---|
940 | * (Generic, used for a few kinds of queries.) |
---|
941 | */ |
---|
942 | static void twitter_http_post(struct http_request *req) |
---|
943 | { |
---|
944 | struct im_connection *ic = req->data; |
---|
945 | struct twitter_data *td; |
---|
946 | |
---|
947 | // Check if the connection is still active. |
---|
948 | if (!g_slist_find(twitter_connections, ic)) |
---|
949 | return; |
---|
950 | |
---|
951 | td = ic->proto_data; |
---|
952 | td->last_status_id = 0; |
---|
953 | |
---|
954 | // Check if the HTTP request went well. |
---|
955 | if (req->status_code != 200) { |
---|
956 | // It didn't go well, output the error and return. |
---|
957 | imcb_error(ic, "HTTP error: %s", twitter_parse_error(req)); |
---|
958 | return; |
---|
959 | } |
---|
960 | |
---|
961 | if (req->body_size > 0) { |
---|
962 | struct xt_parser *xp = NULL; |
---|
963 | struct xt_node *node; |
---|
964 | |
---|
965 | xp = xt_new(NULL, NULL); |
---|
966 | xt_feed(xp, req->reply_body, req->body_size); |
---|
967 | |
---|
968 | if ((node = xt_find_node(xp->root, "status")) && |
---|
969 | (node = xt_find_node(node->children, "id")) && node->text) |
---|
970 | td->last_status_id = g_ascii_strtoull(node->text, NULL, 10); |
---|
971 | |
---|
972 | xt_free(xp); |
---|
973 | } |
---|
974 | } |
---|
975 | |
---|
976 | /** |
---|
977 | * Function to POST a new status to twitter. |
---|
978 | */ |
---|
979 | void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to) |
---|
980 | { |
---|
981 | char *args[4] = { |
---|
982 | "status", msg, |
---|
983 | "in_reply_to_status_id", |
---|
984 | g_strdup_printf("%llu", (unsigned long long) in_reply_to) |
---|
985 | }; |
---|
986 | twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, |
---|
987 | args, in_reply_to ? 4 : 2); |
---|
988 | g_free(args[3]); |
---|
989 | } |
---|
990 | |
---|
991 | |
---|
992 | /** |
---|
993 | * Function to POST a new message to twitter. |
---|
994 | */ |
---|
995 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
996 | { |
---|
997 | char *args[4]; |
---|
998 | args[0] = "screen_name"; |
---|
999 | args[1] = who; |
---|
1000 | args[2] = "text"; |
---|
1001 | args[3] = msg; |
---|
1002 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
1003 | twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); |
---|
1004 | } |
---|
1005 | |
---|
1006 | void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) |
---|
1007 | { |
---|
1008 | char *args[2]; |
---|
1009 | args[0] = "screen_name"; |
---|
1010 | args[1] = who; |
---|
1011 | twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, |
---|
1012 | twitter_http_post, ic, 1, args, 2); |
---|
1013 | } |
---|
1014 | |
---|
1015 | void twitter_status_destroy(struct im_connection *ic, guint64 id) |
---|
1016 | { |
---|
1017 | char *url; |
---|
1018 | url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_DESTROY_URL, |
---|
1019 | (unsigned long long) id, ".xml"); |
---|
1020 | twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0); |
---|
1021 | g_free(url); |
---|
1022 | } |
---|
1023 | |
---|
1024 | void twitter_status_retweet(struct im_connection *ic, guint64 id) |
---|
1025 | { |
---|
1026 | char *url; |
---|
1027 | url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_RETWEET_URL, |
---|
1028 | (unsigned long long) id, ".xml"); |
---|
1029 | twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0); |
---|
1030 | g_free(url); |
---|
1031 | } |
---|