1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Simple module to facilitate twitter functionality. * |
---|
5 | * * |
---|
6 | * Copyright 2009-2010 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
7 | * Copyright 2010-2013 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
8 | * * |
---|
9 | * This library is free software; you can redistribute it and/or * |
---|
10 | * modify it under the terms of the GNU Lesser General Public * |
---|
11 | * License as published by the Free Software Foundation, version * |
---|
12 | * 2.1. * |
---|
13 | * * |
---|
14 | * This library is distributed in the hope that it will be useful, * |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
17 | * Lesser General Public License for more details. * |
---|
18 | * * |
---|
19 | * You should have received a copy of the GNU Lesser General Public License * |
---|
20 | * along with this library; if not, write to the Free Software Foundation, * |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
22 | * * |
---|
23 | ****************************************************************************/ |
---|
24 | |
---|
25 | /* For strptime(): */ |
---|
26 | #if (__sun) |
---|
27 | #else |
---|
28 | #define _XOPEN_SOURCE |
---|
29 | #endif |
---|
30 | |
---|
31 | #include "twitter_http.h" |
---|
32 | #include "twitter.h" |
---|
33 | #include "bitlbee.h" |
---|
34 | #include "url.h" |
---|
35 | #include "misc.h" |
---|
36 | #include "base64.h" |
---|
37 | #include "twitter_lib.h" |
---|
38 | #include "json_util.h" |
---|
39 | #include <ctype.h> |
---|
40 | #include <errno.h> |
---|
41 | |
---|
42 | #define TXL_STATUS 1 |
---|
43 | #define TXL_USER 2 |
---|
44 | #define TXL_ID 3 |
---|
45 | |
---|
46 | struct twitter_xml_list { |
---|
47 | int type; |
---|
48 | gint64 next_cursor; |
---|
49 | GSList *list; |
---|
50 | }; |
---|
51 | |
---|
52 | struct twitter_xml_user { |
---|
53 | guint64 uid; |
---|
54 | char *name; |
---|
55 | char *screen_name; |
---|
56 | }; |
---|
57 | |
---|
58 | struct twitter_xml_status { |
---|
59 | time_t created_at; |
---|
60 | char *text; |
---|
61 | struct twitter_xml_user *user; |
---|
62 | guint64 id, rt_id; /* Usually equal, with RTs id == *original* id */ |
---|
63 | guint64 reply_to; |
---|
64 | gboolean from_filter; |
---|
65 | }; |
---|
66 | |
---|
67 | /** |
---|
68 | * Frees a twitter_xml_user struct. |
---|
69 | */ |
---|
70 | static void txu_free(struct twitter_xml_user *txu) |
---|
71 | { |
---|
72 | if (txu == NULL) { |
---|
73 | return; |
---|
74 | } |
---|
75 | |
---|
76 | g_free(txu->name); |
---|
77 | g_free(txu->screen_name); |
---|
78 | g_free(txu); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Frees a twitter_xml_status struct. |
---|
83 | */ |
---|
84 | static void txs_free(struct twitter_xml_status *txs) |
---|
85 | { |
---|
86 | if (txs == NULL) { |
---|
87 | return; |
---|
88 | } |
---|
89 | |
---|
90 | g_free(txs->text); |
---|
91 | txu_free(txs->user); |
---|
92 | g_free(txs); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Free a twitter_xml_list struct. |
---|
97 | * type is the type of list the struct holds. |
---|
98 | */ |
---|
99 | static void txl_free(struct twitter_xml_list *txl) |
---|
100 | { |
---|
101 | GSList *l; |
---|
102 | |
---|
103 | if (txl == NULL) { |
---|
104 | return; |
---|
105 | } |
---|
106 | |
---|
107 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
108 | if (txl->type == TXL_STATUS) { |
---|
109 | txs_free((struct twitter_xml_status *) l->data); |
---|
110 | } else if (txl->type == TXL_ID) { |
---|
111 | g_free(l->data); |
---|
112 | } else if (txl->type == TXL_USER) { |
---|
113 | txu_free(l->data); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | g_slist_free(txl->list); |
---|
118 | g_free(txl); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Compare status elements |
---|
123 | */ |
---|
124 | static gint twitter_compare_elements(gconstpointer a, gconstpointer b) |
---|
125 | { |
---|
126 | struct twitter_xml_status *a_status = (struct twitter_xml_status *) a; |
---|
127 | struct twitter_xml_status *b_status = (struct twitter_xml_status *) b; |
---|
128 | |
---|
129 | if (a_status->created_at < b_status->created_at) { |
---|
130 | return -1; |
---|
131 | } else if (a_status->created_at > b_status->created_at) { |
---|
132 | return 1; |
---|
133 | } else { |
---|
134 | return 0; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Add a buddy if it is not already added, set the status to logged in. |
---|
140 | */ |
---|
141 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
142 | { |
---|
143 | struct twitter_data *td = ic->proto_data; |
---|
144 | |
---|
145 | // Check if the buddy is already in the buddy list. |
---|
146 | if (!bee_user_by_handle(ic->bee, ic, name)) { |
---|
147 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
148 | imcb_add_buddy(ic, name, NULL); |
---|
149 | imcb_rename_buddy(ic, name, fullname); |
---|
150 | if (td->flags & TWITTER_MODE_CHAT) { |
---|
151 | /* Necessary so that nicks always get translated to the |
---|
152 | exact Twitter username. */ |
---|
153 | imcb_buddy_nick_hint(ic, name, name); |
---|
154 | if (td->timeline_gc) { |
---|
155 | imcb_chat_add_buddy(td->timeline_gc, name); |
---|
156 | } |
---|
157 | } else if (td->flags & TWITTER_MODE_MANY) { |
---|
158 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /* Warning: May return a malloc()ed value, which will be free()d on the next |
---|
164 | call. Only for short-term use. NOT THREADSAFE! */ |
---|
165 | char *twitter_parse_error(struct http_request *req) |
---|
166 | { |
---|
167 | static char *ret = NULL; |
---|
168 | json_value *root, *err; |
---|
169 | |
---|
170 | g_free(ret); |
---|
171 | ret = NULL; |
---|
172 | |
---|
173 | if (req->body_size > 0) { |
---|
174 | root = json_parse(req->reply_body, req->body_size); |
---|
175 | err = json_o_get(root, "errors"); |
---|
176 | if (err && err->type == json_array && (err = err->u.array.values[0]) && |
---|
177 | err->type == json_object) { |
---|
178 | const char *msg = json_o_str(err, "message"); |
---|
179 | if (msg) { |
---|
180 | ret = g_strdup_printf("%s (%s)", req->status_string, msg); |
---|
181 | } |
---|
182 | } |
---|
183 | json_value_free(root); |
---|
184 | } |
---|
185 | |
---|
186 | return ret ? ret : req->status_string; |
---|
187 | } |
---|
188 | |
---|
189 | /* WATCH OUT: This function might or might not destroy your connection. |
---|
190 | Sub-optimal indeed, but just be careful when this returns NULL! */ |
---|
191 | static json_value *twitter_parse_response(struct im_connection *ic, struct http_request *req) |
---|
192 | { |
---|
193 | gboolean logging_in = !(ic->flags & OPT_LOGGED_IN); |
---|
194 | gboolean periodic; |
---|
195 | struct twitter_data *td = ic->proto_data; |
---|
196 | json_value *ret; |
---|
197 | char path[64] = "", *s; |
---|
198 | |
---|
199 | if ((s = strchr(req->request, ' '))) { |
---|
200 | path[sizeof(path) - 1] = '\0'; |
---|
201 | strncpy(path, s + 1, sizeof(path) - 1); |
---|
202 | if ((s = strchr(path, '?')) || (s = strchr(path, ' '))) { |
---|
203 | *s = '\0'; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | /* Kinda nasty. :-( Trying to suppress error messages, but only |
---|
208 | for periodic (i.e. mentions/timeline) queries. */ |
---|
209 | periodic = strstr(path, "timeline") || strstr(path, "mentions"); |
---|
210 | |
---|
211 | if (req->status_code == 401 && logging_in) { |
---|
212 | /* IIRC Twitter once had an outage where they were randomly |
---|
213 | throwing 401s so I'll keep treating this one as fatal |
---|
214 | only during login. */ |
---|
215 | imcb_error(ic, "Authentication failure (%s)", |
---|
216 | twitter_parse_error(req)); |
---|
217 | imc_logout(ic, FALSE); |
---|
218 | return NULL; |
---|
219 | } else if (req->status_code != 200) { |
---|
220 | // It didn't go well, output the error and return. |
---|
221 | if (!periodic || logging_in || ++td->http_fails >= 5) { |
---|
222 | twitter_log(ic, "Error: Could not retrieve %s: %s", |
---|
223 | path, twitter_parse_error(req)); |
---|
224 | } |
---|
225 | |
---|
226 | if (logging_in) { |
---|
227 | imc_logout(ic, TRUE); |
---|
228 | } |
---|
229 | return NULL; |
---|
230 | } else { |
---|
231 | td->http_fails = 0; |
---|
232 | } |
---|
233 | |
---|
234 | if ((ret = json_parse(req->reply_body, req->body_size)) == NULL) { |
---|
235 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
236 | path, "XML parse error"); |
---|
237 | } |
---|
238 | return ret; |
---|
239 | } |
---|
240 | |
---|
241 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
242 | |
---|
243 | /** |
---|
244 | * Get the friends ids. |
---|
245 | */ |
---|
246 | void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) |
---|
247 | { |
---|
248 | // Primitive, but hey! It works... |
---|
249 | char *args[2]; |
---|
250 | |
---|
251 | args[0] = "cursor"; |
---|
252 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
253 | twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); |
---|
254 | |
---|
255 | g_free(args[1]); |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * Fill a list of ids. |
---|
260 | */ |
---|
261 | static gboolean twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl) |
---|
262 | { |
---|
263 | json_value *c; |
---|
264 | int i; |
---|
265 | |
---|
266 | // Set the list type. |
---|
267 | txl->type = TXL_ID; |
---|
268 | |
---|
269 | c = json_o_get(node, "ids"); |
---|
270 | if (!c || c->type != json_array) { |
---|
271 | return FALSE; |
---|
272 | } |
---|
273 | |
---|
274 | for (i = 0; i < c->u.array.length; i++) { |
---|
275 | if (c->u.array.values[i]->type != json_integer) { |
---|
276 | continue; |
---|
277 | } |
---|
278 | |
---|
279 | txl->list = g_slist_prepend(txl->list, |
---|
280 | g_strdup_printf("%" PRIu64, c->u.array.values[i]->u.integer)); |
---|
281 | } |
---|
282 | |
---|
283 | c = json_o_get(node, "next_cursor"); |
---|
284 | if (c && c->type == json_integer) { |
---|
285 | txl->next_cursor = c->u.integer; |
---|
286 | } else { |
---|
287 | txl->next_cursor = -1; |
---|
288 | } |
---|
289 | |
---|
290 | return TRUE; |
---|
291 | } |
---|
292 | |
---|
293 | static void twitter_get_users_lookup(struct im_connection *ic); |
---|
294 | |
---|
295 | /** |
---|
296 | * Callback for getting the friends ids. |
---|
297 | */ |
---|
298 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
299 | { |
---|
300 | struct im_connection *ic; |
---|
301 | json_value *parsed; |
---|
302 | struct twitter_xml_list *txl; |
---|
303 | struct twitter_data *td; |
---|
304 | |
---|
305 | ic = req->data; |
---|
306 | |
---|
307 | // Check if the connection is still active. |
---|
308 | if (!g_slist_find(twitter_connections, ic)) { |
---|
309 | return; |
---|
310 | } |
---|
311 | |
---|
312 | td = ic->proto_data; |
---|
313 | |
---|
314 | txl = g_new0(struct twitter_xml_list, 1); |
---|
315 | txl->list = td->follow_ids; |
---|
316 | |
---|
317 | // Parse the data. |
---|
318 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
319 | return; |
---|
320 | } |
---|
321 | |
---|
322 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
323 | json_value_free(parsed); |
---|
324 | |
---|
325 | td->follow_ids = txl->list; |
---|
326 | if (txl->next_cursor) { |
---|
327 | /* These were just numbers. Up to 4000 in a response AFAIK so if we get here |
---|
328 | we may be using a spammer account. \o/ */ |
---|
329 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
330 | } else { |
---|
331 | /* Now to convert all those numbers into names.. */ |
---|
332 | twitter_get_users_lookup(ic); |
---|
333 | } |
---|
334 | |
---|
335 | txl->list = NULL; |
---|
336 | txl_free(txl); |
---|
337 | } |
---|
338 | |
---|
339 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl); |
---|
340 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
341 | |
---|
342 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
343 | { |
---|
344 | struct twitter_data *td = ic->proto_data; |
---|
345 | char *args[2] = { |
---|
346 | "user_id", |
---|
347 | NULL, |
---|
348 | }; |
---|
349 | GString *ids = g_string_new(""); |
---|
350 | int i; |
---|
351 | |
---|
352 | /* We can request up to 100 users at a time. */ |
---|
353 | for (i = 0; i < 100 && td->follow_ids; i++) { |
---|
354 | g_string_append_printf(ids, ",%s", (char *) td->follow_ids->data); |
---|
355 | g_free(td->follow_ids->data); |
---|
356 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
357 | } |
---|
358 | if (ids->len > 0) { |
---|
359 | args[1] = ids->str + 1; |
---|
360 | /* POST, because I think ids can be up to 1KB long. */ |
---|
361 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
362 | } else { |
---|
363 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
364 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
365 | twitter_login_finish(ic); |
---|
366 | } |
---|
367 | g_string_free(ids, TRUE); |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * Callback for getting (twitter)friends... |
---|
372 | * |
---|
373 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
374 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
375 | * BitlBee... Get a life and meet new people! |
---|
376 | */ |
---|
377 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
378 | { |
---|
379 | struct im_connection *ic = req->data; |
---|
380 | json_value *parsed; |
---|
381 | struct twitter_xml_list *txl; |
---|
382 | GSList *l = NULL; |
---|
383 | struct twitter_xml_user *user; |
---|
384 | |
---|
385 | // Check if the connection is still active. |
---|
386 | if (!g_slist_find(twitter_connections, ic)) { |
---|
387 | return; |
---|
388 | } |
---|
389 | |
---|
390 | txl = g_new0(struct twitter_xml_list, 1); |
---|
391 | txl->list = NULL; |
---|
392 | |
---|
393 | // Get the user list from the parsed xml feed. |
---|
394 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
395 | return; |
---|
396 | } |
---|
397 | twitter_xt_get_users(parsed, txl); |
---|
398 | json_value_free(parsed); |
---|
399 | |
---|
400 | // Add the users as buddies. |
---|
401 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
402 | user = l->data; |
---|
403 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
404 | } |
---|
405 | |
---|
406 | // Free the structure. |
---|
407 | txl_free(txl); |
---|
408 | |
---|
409 | twitter_get_users_lookup(ic); |
---|
410 | } |
---|
411 | |
---|
412 | struct twitter_xml_user *twitter_xt_get_user(const json_value *node) |
---|
413 | { |
---|
414 | struct twitter_xml_user *txu; |
---|
415 | json_value *jv; |
---|
416 | |
---|
417 | txu = g_new0(struct twitter_xml_user, 1); |
---|
418 | txu->name = g_strdup(json_o_str(node, "name")); |
---|
419 | txu->screen_name = g_strdup(json_o_str(node, "screen_name")); |
---|
420 | |
---|
421 | jv = json_o_get(node, "id"); |
---|
422 | txu->uid = jv->u.integer; |
---|
423 | |
---|
424 | return txu; |
---|
425 | } |
---|
426 | |
---|
427 | /** |
---|
428 | * Function to fill a twitter_xml_list struct. |
---|
429 | * It sets: |
---|
430 | * - all <user>s from the <users> element. |
---|
431 | */ |
---|
432 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl) |
---|
433 | { |
---|
434 | struct twitter_xml_user *txu; |
---|
435 | int i; |
---|
436 | |
---|
437 | // Set the type of the list. |
---|
438 | txl->type = TXL_USER; |
---|
439 | |
---|
440 | if (!node || node->type != json_array) { |
---|
441 | return FALSE; |
---|
442 | } |
---|
443 | |
---|
444 | // The root <users> node should hold the list of users <user> |
---|
445 | // Walk over the nodes children. |
---|
446 | for (i = 0; i < node->u.array.length; i++) { |
---|
447 | txu = twitter_xt_get_user(node->u.array.values[i]); |
---|
448 | if (txu) { |
---|
449 | txl->list = g_slist_prepend(txl->list, txu); |
---|
450 | } |
---|
451 | } |
---|
452 | |
---|
453 | return TRUE; |
---|
454 | } |
---|
455 | |
---|
456 | #ifdef __GLIBC__ |
---|
457 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
458 | #else |
---|
459 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
460 | #endif |
---|
461 | |
---|
462 | static char* expand_entities(char* text, const json_value *entities); |
---|
463 | |
---|
464 | /** |
---|
465 | * Function to fill a twitter_xml_status struct. |
---|
466 | * It sets: |
---|
467 | * - the status text and |
---|
468 | * - the created_at timestamp and |
---|
469 | * - the status id and |
---|
470 | * - the user in a twitter_xml_user struct. |
---|
471 | */ |
---|
472 | static struct twitter_xml_status *twitter_xt_get_status(const json_value *node) |
---|
473 | { |
---|
474 | struct twitter_xml_status *txs; |
---|
475 | const json_value *rt = NULL, *entities = NULL; |
---|
476 | |
---|
477 | if (node->type != json_object) { |
---|
478 | return FALSE; |
---|
479 | } |
---|
480 | txs = g_new0(struct twitter_xml_status, 1); |
---|
481 | |
---|
482 | JSON_O_FOREACH(node, k, v) { |
---|
483 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
484 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
485 | strip_html(txs->text); |
---|
486 | } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) { |
---|
487 | rt = v; |
---|
488 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
489 | struct tm parsed; |
---|
490 | |
---|
491 | /* Very sensitive to changes to the formatting of |
---|
492 | this field. :-( Also assumes the timezone used |
---|
493 | is UTC since C time handling functions suck. */ |
---|
494 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) { |
---|
495 | txs->created_at = mktime_utc(&parsed); |
---|
496 | } |
---|
497 | } else if (strcmp("user", k) == 0 && v->type == json_object) { |
---|
498 | txs->user = twitter_xt_get_user(v); |
---|
499 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
500 | txs->rt_id = txs->id = v->u.integer; |
---|
501 | } else if (strcmp("in_reply_to_status_id", k) == 0 && v->type == json_integer) { |
---|
502 | txs->reply_to = v->u.integer; |
---|
503 | } else if (strcmp("entities", k) == 0 && v->type == json_object) { |
---|
504 | entities = v; |
---|
505 | } |
---|
506 | } |
---|
507 | |
---|
508 | /* If it's a (truncated) retweet, get the original. Even if the API claims it |
---|
509 | wasn't truncated because it may be lying. */ |
---|
510 | if (rt) { |
---|
511 | struct twitter_xml_status *rtxs = twitter_xt_get_status(rt); |
---|
512 | if (rtxs) { |
---|
513 | g_free(txs->text); |
---|
514 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
515 | txs->id = rtxs->id; |
---|
516 | txs_free(rtxs); |
---|
517 | } |
---|
518 | } else if (entities) { |
---|
519 | txs->text = expand_entities(txs->text, entities); |
---|
520 | } |
---|
521 | |
---|
522 | if (txs->text && txs->user && txs->id) { |
---|
523 | return txs; |
---|
524 | } |
---|
525 | |
---|
526 | txs_free(txs); |
---|
527 | return NULL; |
---|
528 | } |
---|
529 | |
---|
530 | /** |
---|
531 | * Function to fill a twitter_xml_status struct (DM variant). |
---|
532 | */ |
---|
533 | static struct twitter_xml_status *twitter_xt_get_dm(const json_value *node) |
---|
534 | { |
---|
535 | struct twitter_xml_status *txs; |
---|
536 | const json_value *entities = NULL; |
---|
537 | |
---|
538 | if (node->type != json_object) { |
---|
539 | return FALSE; |
---|
540 | } |
---|
541 | txs = g_new0(struct twitter_xml_status, 1); |
---|
542 | |
---|
543 | JSON_O_FOREACH(node, k, v) { |
---|
544 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
545 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
546 | strip_html(txs->text); |
---|
547 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
548 | struct tm parsed; |
---|
549 | |
---|
550 | /* Very sensitive to changes to the formatting of |
---|
551 | this field. :-( Also assumes the timezone used |
---|
552 | is UTC since C time handling functions suck. */ |
---|
553 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) { |
---|
554 | txs->created_at = mktime_utc(&parsed); |
---|
555 | } |
---|
556 | } else if (strcmp("sender", k) == 0 && v->type == json_object) { |
---|
557 | txs->user = twitter_xt_get_user(v); |
---|
558 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
559 | txs->id = v->u.integer; |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | if (entities) { |
---|
564 | txs->text = expand_entities(txs->text, entities); |
---|
565 | } |
---|
566 | |
---|
567 | if (txs->text && txs->user && txs->id) { |
---|
568 | return txs; |
---|
569 | } |
---|
570 | |
---|
571 | txs_free(txs); |
---|
572 | return NULL; |
---|
573 | } |
---|
574 | |
---|
575 | static char* expand_entities(char* text, const json_value *entities) |
---|
576 | { |
---|
577 | JSON_O_FOREACH(entities, k, v) { |
---|
578 | int i; |
---|
579 | |
---|
580 | if (v->type != json_array) { |
---|
581 | continue; |
---|
582 | } |
---|
583 | if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0) { |
---|
584 | continue; |
---|
585 | } |
---|
586 | |
---|
587 | for (i = 0; i < v->u.array.length; i++) { |
---|
588 | if (v->u.array.values[i]->type != json_object) { |
---|
589 | continue; |
---|
590 | } |
---|
591 | |
---|
592 | const char *kort = json_o_str(v->u.array.values[i], "url"); |
---|
593 | const char *disp = json_o_str(v->u.array.values[i], "display_url"); |
---|
594 | char *pos, *new; |
---|
595 | |
---|
596 | if (!kort || !disp || !(pos = strstr(text, kort))) { |
---|
597 | continue; |
---|
598 | } |
---|
599 | |
---|
600 | *pos = '\0'; |
---|
601 | new = g_strdup_printf("%s%s <%s>%s", text, kort, |
---|
602 | disp, pos + strlen(kort)); |
---|
603 | |
---|
604 | g_free(text); |
---|
605 | text = new; |
---|
606 | } |
---|
607 | } |
---|
608 | |
---|
609 | return text; |
---|
610 | } |
---|
611 | |
---|
612 | /** |
---|
613 | * Function to fill a twitter_xml_list struct. |
---|
614 | * It sets: |
---|
615 | * - all <status>es within the <status> element and |
---|
616 | * - the next_cursor. |
---|
617 | */ |
---|
618 | static gboolean twitter_xt_get_status_list(struct im_connection *ic, const json_value *node, |
---|
619 | struct twitter_xml_list *txl) |
---|
620 | { |
---|
621 | struct twitter_xml_status *txs; |
---|
622 | int i; |
---|
623 | |
---|
624 | // Set the type of the list. |
---|
625 | txl->type = TXL_STATUS; |
---|
626 | |
---|
627 | if (node->type != json_array) { |
---|
628 | return FALSE; |
---|
629 | } |
---|
630 | |
---|
631 | // The root <statuses> node should hold the list of statuses <status> |
---|
632 | // Walk over the nodes children. |
---|
633 | for (i = 0; i < node->u.array.length; i++) { |
---|
634 | txs = twitter_xt_get_status(node->u.array.values[i]); |
---|
635 | if (!txs) { |
---|
636 | continue; |
---|
637 | } |
---|
638 | |
---|
639 | txl->list = g_slist_prepend(txl->list, txs); |
---|
640 | } |
---|
641 | |
---|
642 | return TRUE; |
---|
643 | } |
---|
644 | |
---|
645 | /* Will log messages either way. Need to keep track of IDs for stream deduping. |
---|
646 | Plus, show_ids is on by default and I don't see why anyone would disable it. */ |
---|
647 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
648 | struct twitter_xml_status *txs, const char *prefix) |
---|
649 | { |
---|
650 | struct twitter_data *td = ic->proto_data; |
---|
651 | int reply_to = -1; |
---|
652 | bee_user_t *bu; |
---|
653 | |
---|
654 | if (txs->reply_to) { |
---|
655 | int i; |
---|
656 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
657 | if (td->log[i].id == txs->reply_to) { |
---|
658 | reply_to = i; |
---|
659 | break; |
---|
660 | } |
---|
661 | } |
---|
662 | } |
---|
663 | |
---|
664 | if (txs->user && txs->user->screen_name && |
---|
665 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
666 | struct twitter_user_data *tud = bu->data; |
---|
667 | |
---|
668 | if (txs->id > tud->last_id) { |
---|
669 | tud->last_id = txs->id; |
---|
670 | tud->last_time = txs->created_at; |
---|
671 | } |
---|
672 | } |
---|
673 | |
---|
674 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
675 | td->log[td->log_id].id = txs->id; |
---|
676 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
677 | |
---|
678 | /* This is all getting hairy. :-( If we RT'ed something ourselves, |
---|
679 | remember OUR id instead so undo will work. In other cases, the |
---|
680 | original tweet's id should be remembered for deduplicating. */ |
---|
681 | if (g_strcasecmp(txs->user->screen_name, td->user) == 0) { |
---|
682 | td->log[td->log_id].id = txs->rt_id; |
---|
683 | } |
---|
684 | |
---|
685 | if (set_getbool(&ic->acc->set, "show_ids")) { |
---|
686 | if (reply_to != -1) { |
---|
687 | return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s", |
---|
688 | td->log_id, reply_to, prefix, txs->text); |
---|
689 | } else { |
---|
690 | return g_strdup_printf("\002[\002%02x\002]\002 %s%s", |
---|
691 | td->log_id, prefix, txs->text); |
---|
692 | } |
---|
693 | } else { |
---|
694 | if (*prefix) { |
---|
695 | return g_strconcat(prefix, txs->text, NULL); |
---|
696 | } else { |
---|
697 | return NULL; |
---|
698 | } |
---|
699 | } |
---|
700 | } |
---|
701 | |
---|
702 | /** |
---|
703 | * Function that is called to see the filter statuses in groupchat windows. |
---|
704 | */ |
---|
705 | static void twitter_status_show_filter(struct im_connection *ic, struct twitter_xml_status *status) |
---|
706 | { |
---|
707 | struct twitter_data *td = ic->proto_data; |
---|
708 | char *msg = twitter_msg_add_id(ic, status, ""); |
---|
709 | struct twitter_filter *tf; |
---|
710 | GSList *f; |
---|
711 | GSList *l; |
---|
712 | |
---|
713 | for (f = td->filters; f; f = g_slist_next(f)) { |
---|
714 | tf = f->data; |
---|
715 | |
---|
716 | switch (tf->type) { |
---|
717 | case TWITTER_FILTER_TYPE_FOLLOW: |
---|
718 | if (status->user->uid != tf->uid) { |
---|
719 | continue; |
---|
720 | } |
---|
721 | break; |
---|
722 | |
---|
723 | case TWITTER_FILTER_TYPE_TRACK: |
---|
724 | if (strcasestr(status->text, tf->text) == NULL) { |
---|
725 | continue; |
---|
726 | } |
---|
727 | break; |
---|
728 | |
---|
729 | default: |
---|
730 | continue; |
---|
731 | } |
---|
732 | |
---|
733 | for (l = tf->groupchats; l; l = g_slist_next(l)) { |
---|
734 | imcb_chat_msg(l->data, status->user->screen_name, |
---|
735 | msg ? msg : status->text, 0, 0); |
---|
736 | } |
---|
737 | } |
---|
738 | |
---|
739 | g_free(msg); |
---|
740 | } |
---|
741 | |
---|
742 | /** |
---|
743 | * Function that is called to see the statuses in a groupchat window. |
---|
744 | */ |
---|
745 | static void twitter_status_show_chat(struct im_connection *ic, struct twitter_xml_status *status) |
---|
746 | { |
---|
747 | struct twitter_data *td = ic->proto_data; |
---|
748 | struct groupchat *gc; |
---|
749 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
750 | char *msg; |
---|
751 | |
---|
752 | // Create a new groupchat if it does not exsist. |
---|
753 | gc = twitter_groupchat_init(ic); |
---|
754 | |
---|
755 | if (!me) { |
---|
756 | /* MUST be done before twitter_msg_add_id() to avoid #872. */ |
---|
757 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
758 | } |
---|
759 | msg = twitter_msg_add_id(ic, status, ""); |
---|
760 | |
---|
761 | // Say it! |
---|
762 | if (me) { |
---|
763 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
764 | } else { |
---|
765 | imcb_chat_msg(gc, status->user->screen_name, |
---|
766 | msg ? msg : status->text, 0, status->created_at); |
---|
767 | } |
---|
768 | |
---|
769 | g_free(msg); |
---|
770 | } |
---|
771 | |
---|
772 | /** |
---|
773 | * Function that is called to see statuses as private messages. |
---|
774 | */ |
---|
775 | static void twitter_status_show_msg(struct im_connection *ic, struct twitter_xml_status *status) |
---|
776 | { |
---|
777 | struct twitter_data *td = ic->proto_data; |
---|
778 | char from[MAX_STRING] = ""; |
---|
779 | char *prefix = NULL, *text = NULL; |
---|
780 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
781 | |
---|
782 | if (td->flags & TWITTER_MODE_ONE) { |
---|
783 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
784 | from[MAX_STRING - 1] = '\0'; |
---|
785 | } |
---|
786 | |
---|
787 | if (td->flags & TWITTER_MODE_ONE) { |
---|
788 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
789 | status->user->screen_name); |
---|
790 | } else if (!me) { |
---|
791 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
792 | } else { |
---|
793 | prefix = g_strdup("You: "); |
---|
794 | } |
---|
795 | |
---|
796 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
797 | |
---|
798 | imcb_buddy_msg(ic, |
---|
799 | *from ? from : status->user->screen_name, |
---|
800 | text ? text : status->text, 0, status->created_at); |
---|
801 | |
---|
802 | g_free(text); |
---|
803 | g_free(prefix); |
---|
804 | } |
---|
805 | |
---|
806 | static void twitter_status_show(struct im_connection *ic, struct twitter_xml_status *status) |
---|
807 | { |
---|
808 | struct twitter_data *td = ic->proto_data; |
---|
809 | char *last_id_str; |
---|
810 | |
---|
811 | if (status->user == NULL || status->text == NULL) { |
---|
812 | return; |
---|
813 | } |
---|
814 | |
---|
815 | /* Grrrr. Would like to do this during parsing, but can't access |
---|
816 | settings from there. */ |
---|
817 | if (set_getbool(&ic->acc->set, "strip_newlines")) { |
---|
818 | strip_newlines(status->text); |
---|
819 | } |
---|
820 | |
---|
821 | if (status->from_filter) { |
---|
822 | twitter_status_show_filter(ic, status); |
---|
823 | } else if (td->flags & TWITTER_MODE_CHAT) { |
---|
824 | twitter_status_show_chat(ic, status); |
---|
825 | } else { |
---|
826 | twitter_status_show_msg(ic, status); |
---|
827 | } |
---|
828 | |
---|
829 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
830 | // we won't pick up the updates already in the list. |
---|
831 | td->timeline_id = MAX(td->timeline_id, status->rt_id); |
---|
832 | |
---|
833 | last_id_str = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
834 | set_setstr(&ic->acc->set, "last_tweet", last_id_str); |
---|
835 | g_free(last_id_str); |
---|
836 | } |
---|
837 | |
---|
838 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o, gboolean from_filter); |
---|
839 | |
---|
840 | static void twitter_http_stream(struct http_request *req) |
---|
841 | { |
---|
842 | struct im_connection *ic = req->data; |
---|
843 | struct twitter_data *td; |
---|
844 | json_value *parsed; |
---|
845 | int len = 0; |
---|
846 | char c, *nl; |
---|
847 | gboolean from_filter; |
---|
848 | |
---|
849 | if (!g_slist_find(twitter_connections, ic)) { |
---|
850 | return; |
---|
851 | } |
---|
852 | |
---|
853 | ic->flags |= OPT_PONGED; |
---|
854 | td = ic->proto_data; |
---|
855 | |
---|
856 | if ((req->flags & HTTPC_EOF) || !req->reply_body) { |
---|
857 | if (req == td->stream) { |
---|
858 | td->stream = NULL; |
---|
859 | } else if (req == td->filter_stream) { |
---|
860 | td->filter_stream = NULL; |
---|
861 | } |
---|
862 | |
---|
863 | imcb_error(ic, "Stream closed (%s)", req->status_string); |
---|
864 | if (req->status_code == 401) { |
---|
865 | imcb_error(ic, "Check your system clock."); |
---|
866 | } |
---|
867 | imc_logout(ic, TRUE); |
---|
868 | return; |
---|
869 | } |
---|
870 | |
---|
871 | /* MUST search for CRLF, not just LF: |
---|
872 | https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */ |
---|
873 | if (!(nl = strstr(req->reply_body, "\r\n"))) { |
---|
874 | return; |
---|
875 | } |
---|
876 | |
---|
877 | len = nl - req->reply_body; |
---|
878 | if (len > 0) { |
---|
879 | c = req->reply_body[len]; |
---|
880 | req->reply_body[len] = '\0'; |
---|
881 | |
---|
882 | if ((parsed = json_parse(req->reply_body, req->body_size))) { |
---|
883 | from_filter = (req == td->filter_stream); |
---|
884 | twitter_stream_handle_object(ic, parsed, from_filter); |
---|
885 | } |
---|
886 | json_value_free(parsed); |
---|
887 | req->reply_body[len] = c; |
---|
888 | } |
---|
889 | |
---|
890 | http_flush_bytes(req, len + 2); |
---|
891 | |
---|
892 | /* One notification might bring multiple events! */ |
---|
893 | if (req->body_size > 0) { |
---|
894 | twitter_http_stream(req); |
---|
895 | } |
---|
896 | } |
---|
897 | |
---|
898 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o); |
---|
899 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs); |
---|
900 | |
---|
901 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o, gboolean from_filter) |
---|
902 | { |
---|
903 | struct twitter_data *td = ic->proto_data; |
---|
904 | struct twitter_xml_status *txs; |
---|
905 | json_value *c; |
---|
906 | |
---|
907 | if ((txs = twitter_xt_get_status(o))) { |
---|
908 | txs->from_filter = from_filter; |
---|
909 | gboolean ret = twitter_stream_handle_status(ic, txs); |
---|
910 | txs_free(txs); |
---|
911 | return ret; |
---|
912 | } else if ((c = json_o_get(o, "direct_message")) && |
---|
913 | (txs = twitter_xt_get_dm(c))) { |
---|
914 | if (g_strcasecmp(txs->user->screen_name, td->user) != 0) { |
---|
915 | imcb_buddy_msg(ic, txs->user->screen_name, |
---|
916 | txs->text, 0, txs->created_at); |
---|
917 | } |
---|
918 | txs_free(txs); |
---|
919 | return TRUE; |
---|
920 | } else if ((c = json_o_get(o, "event")) && c->type == json_string) { |
---|
921 | twitter_stream_handle_event(ic, o); |
---|
922 | return TRUE; |
---|
923 | } else if ((c = json_o_get(o, "disconnect")) && c->type == json_object) { |
---|
924 | /* HACK: Because we're inside an event handler, we can't just |
---|
925 | disconnect here. Instead, just change the HTTP status string |
---|
926 | into a Twitter status string. */ |
---|
927 | char *reason = json_o_strdup(c, "reason"); |
---|
928 | if (reason) { |
---|
929 | g_free(td->stream->status_string); |
---|
930 | td->stream->status_string = reason; |
---|
931 | } |
---|
932 | return TRUE; |
---|
933 | } |
---|
934 | return FALSE; |
---|
935 | } |
---|
936 | |
---|
937 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs) |
---|
938 | { |
---|
939 | struct twitter_data *td = ic->proto_data; |
---|
940 | int i; |
---|
941 | |
---|
942 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
943 | if (td->log[i].id == txs->id) { |
---|
944 | /* Got a duplicate (RT, probably). Drop it. */ |
---|
945 | return TRUE; |
---|
946 | } |
---|
947 | } |
---|
948 | |
---|
949 | if (!(g_strcasecmp(txs->user->screen_name, td->user) == 0 || |
---|
950 | set_getbool(&ic->acc->set, "fetch_mentions") || |
---|
951 | bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
952 | /* Tweet is from an unknown person and the user does not want |
---|
953 | to see @mentions, so drop it. twitter_stream_handle_event() |
---|
954 | picks up new follows so this simple filter should be safe. */ |
---|
955 | /* TODO: The streaming API seems to do poor @mention matching. |
---|
956 | I.e. I'm getting mentions for @WilmerSomething, not just for |
---|
957 | @Wilmer. But meh. You want spam, you get spam. */ |
---|
958 | return TRUE; |
---|
959 | } |
---|
960 | |
---|
961 | twitter_status_show(ic, txs); |
---|
962 | |
---|
963 | return TRUE; |
---|
964 | } |
---|
965 | |
---|
966 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o) |
---|
967 | { |
---|
968 | struct twitter_data *td = ic->proto_data; |
---|
969 | json_value *source = json_o_get(o, "source"); |
---|
970 | json_value *target = json_o_get(o, "target"); |
---|
971 | const char *type = json_o_str(o, "event"); |
---|
972 | |
---|
973 | if (!type || !source || source->type != json_object |
---|
974 | || !target || target->type != json_object) { |
---|
975 | return FALSE; |
---|
976 | } |
---|
977 | |
---|
978 | if (strcmp(type, "follow") == 0) { |
---|
979 | struct twitter_xml_user *us = twitter_xt_get_user(source); |
---|
980 | struct twitter_xml_user *ut = twitter_xt_get_user(target); |
---|
981 | if (g_strcasecmp(us->screen_name, td->user) == 0) { |
---|
982 | twitter_add_buddy(ic, ut->screen_name, ut->name); |
---|
983 | } |
---|
984 | txu_free(us); |
---|
985 | txu_free(ut); |
---|
986 | } |
---|
987 | |
---|
988 | return TRUE; |
---|
989 | } |
---|
990 | |
---|
991 | gboolean twitter_open_stream(struct im_connection *ic) |
---|
992 | { |
---|
993 | struct twitter_data *td = ic->proto_data; |
---|
994 | char *args[2] = { "with", "followings" }; |
---|
995 | |
---|
996 | if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL, |
---|
997 | twitter_http_stream, ic, 0, args, 2))) { |
---|
998 | /* This flag must be enabled or we'll get no data until EOF |
---|
999 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
1000 | td->stream->flags |= HTTPC_STREAMING; |
---|
1001 | return TRUE; |
---|
1002 | } |
---|
1003 | |
---|
1004 | return FALSE; |
---|
1005 | } |
---|
1006 | |
---|
1007 | static gboolean twitter_filter_stream(struct im_connection *ic) |
---|
1008 | { |
---|
1009 | struct twitter_data *td = ic->proto_data; |
---|
1010 | char *args[4] = { "follow", NULL, "track", NULL }; |
---|
1011 | GString *followstr = g_string_new(""); |
---|
1012 | GString *trackstr = g_string_new(""); |
---|
1013 | gboolean ret = FALSE; |
---|
1014 | struct twitter_filter *tf; |
---|
1015 | GSList *l; |
---|
1016 | |
---|
1017 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
1018 | tf = l->data; |
---|
1019 | |
---|
1020 | switch (tf->type) { |
---|
1021 | case TWITTER_FILTER_TYPE_FOLLOW: |
---|
1022 | if (followstr->len > 0) { |
---|
1023 | g_string_append_c(followstr, ','); |
---|
1024 | } |
---|
1025 | |
---|
1026 | g_string_append_printf(followstr, "%" G_GUINT64_FORMAT, |
---|
1027 | tf->uid); |
---|
1028 | break; |
---|
1029 | |
---|
1030 | case TWITTER_FILTER_TYPE_TRACK: |
---|
1031 | if (trackstr->len > 0) { |
---|
1032 | g_string_append_c(trackstr, ','); |
---|
1033 | } |
---|
1034 | |
---|
1035 | g_string_append(trackstr, tf->text); |
---|
1036 | break; |
---|
1037 | |
---|
1038 | default: |
---|
1039 | continue; |
---|
1040 | } |
---|
1041 | } |
---|
1042 | |
---|
1043 | args[1] = followstr->str; |
---|
1044 | args[3] = trackstr->str; |
---|
1045 | |
---|
1046 | if (td->filter_stream) { |
---|
1047 | http_close(td->filter_stream); |
---|
1048 | } |
---|
1049 | |
---|
1050 | if ((td->filter_stream = twitter_http(ic, TWITTER_FILTER_STREAM_URL, |
---|
1051 | twitter_http_stream, ic, 0, |
---|
1052 | args, 4))) { |
---|
1053 | /* This flag must be enabled or we'll get no data until EOF |
---|
1054 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
1055 | td->filter_stream->flags |= HTTPC_STREAMING; |
---|
1056 | ret = TRUE; |
---|
1057 | } |
---|
1058 | |
---|
1059 | g_string_free(followstr, TRUE); |
---|
1060 | g_string_free(trackstr, TRUE); |
---|
1061 | |
---|
1062 | return ret; |
---|
1063 | } |
---|
1064 | |
---|
1065 | static void twitter_filter_users_post(struct http_request *req) |
---|
1066 | { |
---|
1067 | struct im_connection *ic = req->data; |
---|
1068 | struct twitter_data *td; |
---|
1069 | struct twitter_filter *tf; |
---|
1070 | GList *users = NULL; |
---|
1071 | json_value *parsed; |
---|
1072 | json_value *id; |
---|
1073 | const char *name; |
---|
1074 | GString *fstr; |
---|
1075 | GSList *l; |
---|
1076 | GList *u; |
---|
1077 | int i; |
---|
1078 | |
---|
1079 | // Check if the connection is still active. |
---|
1080 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1081 | return; |
---|
1082 | } |
---|
1083 | |
---|
1084 | td = ic->proto_data; |
---|
1085 | |
---|
1086 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1087 | return; |
---|
1088 | } |
---|
1089 | |
---|
1090 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
1091 | tf = l->data; |
---|
1092 | |
---|
1093 | if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) { |
---|
1094 | users = g_list_prepend(users, tf); |
---|
1095 | } |
---|
1096 | } |
---|
1097 | |
---|
1098 | if (parsed->type != json_array) { |
---|
1099 | goto finish; |
---|
1100 | } |
---|
1101 | |
---|
1102 | for (i = 0; i < parsed->u.array.length; i++) { |
---|
1103 | id = json_o_get(parsed->u.array.values[i], "id"); |
---|
1104 | name = json_o_str(parsed->u.array.values[i], "screen_name"); |
---|
1105 | |
---|
1106 | if (!name || !id || id->type != json_integer) { |
---|
1107 | continue; |
---|
1108 | } |
---|
1109 | |
---|
1110 | for (u = users; u; u = g_list_next(u)) { |
---|
1111 | tf = u->data; |
---|
1112 | |
---|
1113 | if (g_strcasecmp(tf->text, name) == 0) { |
---|
1114 | tf->uid = id->u.integer; |
---|
1115 | users = g_list_delete_link(users, u); |
---|
1116 | break; |
---|
1117 | } |
---|
1118 | } |
---|
1119 | } |
---|
1120 | |
---|
1121 | finish: |
---|
1122 | json_value_free(parsed); |
---|
1123 | twitter_filter_stream(ic); |
---|
1124 | |
---|
1125 | if (!users) { |
---|
1126 | return; |
---|
1127 | } |
---|
1128 | |
---|
1129 | fstr = g_string_new(""); |
---|
1130 | |
---|
1131 | for (u = users; u; u = g_list_next(u)) { |
---|
1132 | if (fstr->len > 0) { |
---|
1133 | g_string_append(fstr, ", "); |
---|
1134 | } |
---|
1135 | |
---|
1136 | g_string_append(fstr, tf->text); |
---|
1137 | } |
---|
1138 | |
---|
1139 | imcb_error(ic, "Failed UID acquisitions: %s", fstr->str); |
---|
1140 | |
---|
1141 | g_string_free(fstr, TRUE); |
---|
1142 | g_list_free(users); |
---|
1143 | } |
---|
1144 | |
---|
1145 | gboolean twitter_open_filter_stream(struct im_connection *ic) |
---|
1146 | { |
---|
1147 | struct twitter_data *td = ic->proto_data; |
---|
1148 | char *args[2] = { "screen_name", NULL }; |
---|
1149 | GString *ustr = g_string_new(""); |
---|
1150 | struct twitter_filter *tf; |
---|
1151 | struct http_request *req; |
---|
1152 | GSList *l; |
---|
1153 | |
---|
1154 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
1155 | tf = l->data; |
---|
1156 | |
---|
1157 | if (tf->type != TWITTER_FILTER_TYPE_FOLLOW || tf->uid != 0) { |
---|
1158 | continue; |
---|
1159 | } |
---|
1160 | |
---|
1161 | if (ustr->len > 0) { |
---|
1162 | g_string_append_c(ustr, ','); |
---|
1163 | } |
---|
1164 | |
---|
1165 | g_string_append(ustr, tf->text); |
---|
1166 | } |
---|
1167 | |
---|
1168 | if (ustr->len == 0) { |
---|
1169 | g_string_free(ustr, TRUE); |
---|
1170 | return twitter_filter_stream(ic); |
---|
1171 | } |
---|
1172 | |
---|
1173 | args[1] = ustr->str; |
---|
1174 | req = twitter_http(ic, TWITTER_USERS_LOOKUP_URL, |
---|
1175 | twitter_filter_users_post, |
---|
1176 | ic, 0, args, 2); |
---|
1177 | |
---|
1178 | g_string_free(ustr, TRUE); |
---|
1179 | return req != NULL; |
---|
1180 | } |
---|
1181 | |
---|
1182 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor); |
---|
1183 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor); |
---|
1184 | |
---|
1185 | /** |
---|
1186 | * Get the timeline with optionally mentions |
---|
1187 | */ |
---|
1188 | gboolean twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
1189 | { |
---|
1190 | struct twitter_data *td = ic->proto_data; |
---|
1191 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
1192 | |
---|
1193 | if (td->flags & TWITTER_DOING_TIMELINE) { |
---|
1194 | if (++td->http_fails >= 5) { |
---|
1195 | imcb_error(ic, "Fetch timeout (%d)", td->flags); |
---|
1196 | imc_logout(ic, TRUE); |
---|
1197 | return FALSE; |
---|
1198 | } |
---|
1199 | } |
---|
1200 | |
---|
1201 | td->flags |= TWITTER_DOING_TIMELINE; |
---|
1202 | |
---|
1203 | twitter_get_home_timeline(ic, next_cursor); |
---|
1204 | |
---|
1205 | if (include_mentions) { |
---|
1206 | twitter_get_mentions(ic, next_cursor); |
---|
1207 | } |
---|
1208 | |
---|
1209 | return TRUE; |
---|
1210 | } |
---|
1211 | |
---|
1212 | /** |
---|
1213 | * Call this one after receiving timeline/mentions. Show to user once we have |
---|
1214 | * both. |
---|
1215 | */ |
---|
1216 | void twitter_flush_timeline(struct im_connection *ic) |
---|
1217 | { |
---|
1218 | struct twitter_data *td = ic->proto_data; |
---|
1219 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
1220 | int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions"); |
---|
1221 | struct twitter_xml_list *home_timeline = td->home_timeline_obj; |
---|
1222 | struct twitter_xml_list *mentions = td->mentions_obj; |
---|
1223 | guint64 last_id = 0; |
---|
1224 | GSList *output = NULL; |
---|
1225 | GSList *l; |
---|
1226 | |
---|
1227 | imcb_connected(ic); |
---|
1228 | |
---|
1229 | if (!(td->flags & TWITTER_GOT_TIMELINE)) { |
---|
1230 | return; |
---|
1231 | } |
---|
1232 | |
---|
1233 | if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) { |
---|
1234 | return; |
---|
1235 | } |
---|
1236 | |
---|
1237 | if (home_timeline && home_timeline->list) { |
---|
1238 | for (l = home_timeline->list; l; l = g_slist_next(l)) { |
---|
1239 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
1240 | } |
---|
1241 | } |
---|
1242 | |
---|
1243 | if (include_mentions && mentions && mentions->list) { |
---|
1244 | for (l = mentions->list; l; l = g_slist_next(l)) { |
---|
1245 | if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) { |
---|
1246 | continue; |
---|
1247 | } |
---|
1248 | |
---|
1249 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
1250 | } |
---|
1251 | } |
---|
1252 | |
---|
1253 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
1254 | while (output) { |
---|
1255 | struct twitter_xml_status *txs = output->data; |
---|
1256 | if (txs->id != last_id) { |
---|
1257 | twitter_status_show(ic, txs); |
---|
1258 | } |
---|
1259 | last_id = txs->id; |
---|
1260 | output = g_slist_remove(output, txs); |
---|
1261 | } |
---|
1262 | |
---|
1263 | txl_free(home_timeline); |
---|
1264 | txl_free(mentions); |
---|
1265 | |
---|
1266 | td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS); |
---|
1267 | td->home_timeline_obj = td->mentions_obj = NULL; |
---|
1268 | } |
---|
1269 | |
---|
1270 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
1271 | static void twitter_http_get_mentions(struct http_request *req); |
---|
1272 | |
---|
1273 | /** |
---|
1274 | * Get the timeline. |
---|
1275 | */ |
---|
1276 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
1277 | { |
---|
1278 | struct twitter_data *td = ic->proto_data; |
---|
1279 | |
---|
1280 | txl_free(td->home_timeline_obj); |
---|
1281 | td->home_timeline_obj = NULL; |
---|
1282 | td->flags &= ~TWITTER_GOT_TIMELINE; |
---|
1283 | |
---|
1284 | char *args[6]; |
---|
1285 | args[0] = "cursor"; |
---|
1286 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
1287 | args[2] = "include_entities"; |
---|
1288 | args[3] = "true"; |
---|
1289 | if (td->timeline_id) { |
---|
1290 | args[4] = "since_id"; |
---|
1291 | args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
1292 | } |
---|
1293 | |
---|
1294 | if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, |
---|
1295 | td->timeline_id ? 6 : 4) == NULL) { |
---|
1296 | if (++td->http_fails >= 5) { |
---|
1297 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
1298 | TWITTER_HOME_TIMELINE_URL, "connection failed"); |
---|
1299 | } |
---|
1300 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
1301 | twitter_flush_timeline(ic); |
---|
1302 | } |
---|
1303 | |
---|
1304 | g_free(args[1]); |
---|
1305 | if (td->timeline_id) { |
---|
1306 | g_free(args[5]); |
---|
1307 | } |
---|
1308 | } |
---|
1309 | |
---|
1310 | /** |
---|
1311 | * Get mentions. |
---|
1312 | */ |
---|
1313 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) |
---|
1314 | { |
---|
1315 | struct twitter_data *td = ic->proto_data; |
---|
1316 | |
---|
1317 | txl_free(td->mentions_obj); |
---|
1318 | td->mentions_obj = NULL; |
---|
1319 | td->flags &= ~TWITTER_GOT_MENTIONS; |
---|
1320 | |
---|
1321 | char *args[6]; |
---|
1322 | args[0] = "cursor"; |
---|
1323 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
1324 | args[2] = "include_entities"; |
---|
1325 | args[3] = "true"; |
---|
1326 | if (td->timeline_id) { |
---|
1327 | args[4] = "since_id"; |
---|
1328 | args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
1329 | } else { |
---|
1330 | args[4] = "count"; |
---|
1331 | args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions")); |
---|
1332 | } |
---|
1333 | |
---|
1334 | if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions, |
---|
1335 | ic, 0, args, 6) == NULL) { |
---|
1336 | if (++td->http_fails >= 5) { |
---|
1337 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
1338 | TWITTER_MENTIONS_URL, "connection failed"); |
---|
1339 | } |
---|
1340 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
1341 | twitter_flush_timeline(ic); |
---|
1342 | } |
---|
1343 | |
---|
1344 | g_free(args[1]); |
---|
1345 | g_free(args[5]); |
---|
1346 | } |
---|
1347 | |
---|
1348 | /** |
---|
1349 | * Callback for getting the home timeline. |
---|
1350 | */ |
---|
1351 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
1352 | { |
---|
1353 | struct im_connection *ic = req->data; |
---|
1354 | struct twitter_data *td; |
---|
1355 | json_value *parsed; |
---|
1356 | struct twitter_xml_list *txl; |
---|
1357 | |
---|
1358 | // Check if the connection is still active. |
---|
1359 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1360 | return; |
---|
1361 | } |
---|
1362 | |
---|
1363 | td = ic->proto_data; |
---|
1364 | |
---|
1365 | txl = g_new0(struct twitter_xml_list, 1); |
---|
1366 | txl->list = NULL; |
---|
1367 | |
---|
1368 | // The root <statuses> node should hold the list of statuses <status> |
---|
1369 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1370 | goto end; |
---|
1371 | } |
---|
1372 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
1373 | json_value_free(parsed); |
---|
1374 | |
---|
1375 | td->home_timeline_obj = txl; |
---|
1376 | |
---|
1377 | end: |
---|
1378 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1379 | return; |
---|
1380 | } |
---|
1381 | |
---|
1382 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
1383 | |
---|
1384 | twitter_flush_timeline(ic); |
---|
1385 | } |
---|
1386 | |
---|
1387 | /** |
---|
1388 | * Callback for getting mentions. |
---|
1389 | */ |
---|
1390 | static void twitter_http_get_mentions(struct http_request *req) |
---|
1391 | { |
---|
1392 | struct im_connection *ic = req->data; |
---|
1393 | struct twitter_data *td; |
---|
1394 | json_value *parsed; |
---|
1395 | struct twitter_xml_list *txl; |
---|
1396 | |
---|
1397 | // Check if the connection is still active. |
---|
1398 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1399 | return; |
---|
1400 | } |
---|
1401 | |
---|
1402 | td = ic->proto_data; |
---|
1403 | |
---|
1404 | txl = g_new0(struct twitter_xml_list, 1); |
---|
1405 | txl->list = NULL; |
---|
1406 | |
---|
1407 | // The root <statuses> node should hold the list of statuses <status> |
---|
1408 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1409 | goto end; |
---|
1410 | } |
---|
1411 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
1412 | json_value_free(parsed); |
---|
1413 | |
---|
1414 | td->mentions_obj = txl; |
---|
1415 | |
---|
1416 | end: |
---|
1417 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1418 | return; |
---|
1419 | } |
---|
1420 | |
---|
1421 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
1422 | |
---|
1423 | twitter_flush_timeline(ic); |
---|
1424 | } |
---|
1425 | |
---|
1426 | /** |
---|
1427 | * Callback to use after sending a POST request to twitter. |
---|
1428 | * (Generic, used for a few kinds of queries.) |
---|
1429 | */ |
---|
1430 | static void twitter_http_post(struct http_request *req) |
---|
1431 | { |
---|
1432 | struct im_connection *ic = req->data; |
---|
1433 | struct twitter_data *td; |
---|
1434 | json_value *parsed, *id; |
---|
1435 | |
---|
1436 | // Check if the connection is still active. |
---|
1437 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1438 | return; |
---|
1439 | } |
---|
1440 | |
---|
1441 | td = ic->proto_data; |
---|
1442 | td->last_status_id = 0; |
---|
1443 | |
---|
1444 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1445 | return; |
---|
1446 | } |
---|
1447 | |
---|
1448 | if ((id = json_o_get(parsed, "id")) && id->type == json_integer) { |
---|
1449 | td->last_status_id = id->u.integer; |
---|
1450 | } |
---|
1451 | |
---|
1452 | json_value_free(parsed); |
---|
1453 | |
---|
1454 | if (req->flags & TWITTER_HTTP_USER_ACK) { |
---|
1455 | twitter_log(ic, "Command processed successfully"); |
---|
1456 | } |
---|
1457 | } |
---|
1458 | |
---|
1459 | /** |
---|
1460 | * Function to POST a new status to twitter. |
---|
1461 | */ |
---|
1462 | void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to) |
---|
1463 | { |
---|
1464 | char *args[4] = { |
---|
1465 | "status", msg, |
---|
1466 | "in_reply_to_status_id", |
---|
1467 | g_strdup_printf("%" G_GUINT64_FORMAT, in_reply_to) |
---|
1468 | }; |
---|
1469 | |
---|
1470 | twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, |
---|
1471 | args, in_reply_to ? 4 : 2); |
---|
1472 | g_free(args[3]); |
---|
1473 | } |
---|
1474 | |
---|
1475 | |
---|
1476 | /** |
---|
1477 | * Function to POST a new message to twitter. |
---|
1478 | */ |
---|
1479 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
1480 | { |
---|
1481 | char *args[4]; |
---|
1482 | |
---|
1483 | args[0] = "screen_name"; |
---|
1484 | args[1] = who; |
---|
1485 | args[2] = "text"; |
---|
1486 | args[3] = msg; |
---|
1487 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
1488 | twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); |
---|
1489 | } |
---|
1490 | |
---|
1491 | void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) |
---|
1492 | { |
---|
1493 | char *args[2]; |
---|
1494 | |
---|
1495 | args[0] = "screen_name"; |
---|
1496 | args[1] = who; |
---|
1497 | twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, |
---|
1498 | twitter_http_post, ic, 1, args, 2); |
---|
1499 | } |
---|
1500 | |
---|
1501 | void twitter_status_destroy(struct im_connection *ic, guint64 id) |
---|
1502 | { |
---|
1503 | char *url; |
---|
1504 | |
---|
1505 | url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", |
---|
1506 | TWITTER_STATUS_DESTROY_URL, id, ".json"); |
---|
1507 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
1508 | TWITTER_HTTP_USER_ACK); |
---|
1509 | g_free(url); |
---|
1510 | } |
---|
1511 | |
---|
1512 | void twitter_status_retweet(struct im_connection *ic, guint64 id) |
---|
1513 | { |
---|
1514 | char *url; |
---|
1515 | |
---|
1516 | url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", |
---|
1517 | TWITTER_STATUS_RETWEET_URL, id, ".json"); |
---|
1518 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
1519 | TWITTER_HTTP_USER_ACK); |
---|
1520 | g_free(url); |
---|
1521 | } |
---|
1522 | |
---|
1523 | /** |
---|
1524 | * Report a user for sending spam. |
---|
1525 | */ |
---|
1526 | void twitter_report_spam(struct im_connection *ic, char *screen_name) |
---|
1527 | { |
---|
1528 | char *args[2] = { |
---|
1529 | "screen_name", |
---|
1530 | NULL, |
---|
1531 | }; |
---|
1532 | |
---|
1533 | args[1] = screen_name; |
---|
1534 | twitter_http_f(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post, |
---|
1535 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
1536 | } |
---|
1537 | |
---|
1538 | /** |
---|
1539 | * Favourite a tweet. |
---|
1540 | */ |
---|
1541 | void twitter_favourite_tweet(struct im_connection *ic, guint64 id) |
---|
1542 | { |
---|
1543 | char *args[2] = { |
---|
1544 | "id", |
---|
1545 | NULL, |
---|
1546 | }; |
---|
1547 | |
---|
1548 | args[1] = g_strdup_printf("%" G_GUINT64_FORMAT, id); |
---|
1549 | twitter_http_f(ic, TWITTER_FAVORITE_CREATE_URL, twitter_http_post, |
---|
1550 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
1551 | g_free(args[1]); |
---|
1552 | } |
---|