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 | static void twitter_http_get_mutes_ids(struct http_request *req); |
---|
243 | static void twitter_http_get_noretweets_ids(struct http_request *req); |
---|
244 | |
---|
245 | /** |
---|
246 | * Get the friends ids. |
---|
247 | */ |
---|
248 | void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) |
---|
249 | { |
---|
250 | // Primitive, but hey! It works... |
---|
251 | char *args[2]; |
---|
252 | |
---|
253 | args[0] = "cursor"; |
---|
254 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
255 | twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); |
---|
256 | |
---|
257 | g_free(args[1]); |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * Get the muted users ids. |
---|
262 | */ |
---|
263 | void twitter_get_mutes_ids(struct im_connection *ic, gint64 next_cursor) |
---|
264 | { |
---|
265 | char *args[2]; |
---|
266 | |
---|
267 | args[0] = "cursor"; |
---|
268 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
269 | twitter_http(ic, TWITTER_MUTES_IDS_URL, twitter_http_get_mutes_ids, ic, 0, args, 2); |
---|
270 | |
---|
271 | g_free(args[1]); |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * Get the ids for users from whom we should ignore retweets. |
---|
276 | */ |
---|
277 | void twitter_get_noretweets_ids(struct im_connection *ic, gint64 next_cursor) |
---|
278 | { |
---|
279 | char *args[2]; |
---|
280 | |
---|
281 | args[0] = "cursor"; |
---|
282 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
283 | twitter_http(ic, TWITTER_NORETWEETS_IDS_URL, twitter_http_get_noretweets_ids, ic, 0, args, 2); |
---|
284 | |
---|
285 | g_free(args[1]); |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * Fill a list of ids. |
---|
290 | */ |
---|
291 | static gboolean twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl) |
---|
292 | { |
---|
293 | json_value *c; |
---|
294 | int i; |
---|
295 | |
---|
296 | // Set the list type. |
---|
297 | txl->type = TXL_ID; |
---|
298 | |
---|
299 | c = json_o_get(node, "ids"); |
---|
300 | if (!c || c->type != json_array) { |
---|
301 | return FALSE; |
---|
302 | } |
---|
303 | |
---|
304 | for (i = 0; i < c->u.array.length; i++) { |
---|
305 | if (c->u.array.values[i]->type != json_integer) { |
---|
306 | continue; |
---|
307 | } |
---|
308 | |
---|
309 | txl->list = g_slist_prepend(txl->list, |
---|
310 | g_strdup_printf("%" PRIu64, c->u.array.values[i]->u.integer)); |
---|
311 | } |
---|
312 | |
---|
313 | c = json_o_get(node, "next_cursor"); |
---|
314 | if (c && c->type == json_integer) { |
---|
315 | txl->next_cursor = c->u.integer; |
---|
316 | } else { |
---|
317 | txl->next_cursor = -1; |
---|
318 | } |
---|
319 | |
---|
320 | return TRUE; |
---|
321 | } |
---|
322 | |
---|
323 | static void twitter_get_users_lookup(struct im_connection *ic); |
---|
324 | |
---|
325 | /** |
---|
326 | * Callback for getting the friends ids. |
---|
327 | */ |
---|
328 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
329 | { |
---|
330 | struct im_connection *ic; |
---|
331 | json_value *parsed; |
---|
332 | struct twitter_xml_list *txl; |
---|
333 | struct twitter_data *td; |
---|
334 | |
---|
335 | ic = req->data; |
---|
336 | |
---|
337 | // Check if the connection is still active. |
---|
338 | if (!g_slist_find(twitter_connections, ic)) { |
---|
339 | return; |
---|
340 | } |
---|
341 | |
---|
342 | td = ic->proto_data; |
---|
343 | |
---|
344 | // Parse the data. |
---|
345 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
346 | return; |
---|
347 | } |
---|
348 | |
---|
349 | txl = g_new0(struct twitter_xml_list, 1); |
---|
350 | txl->list = td->follow_ids; |
---|
351 | |
---|
352 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
353 | json_value_free(parsed); |
---|
354 | |
---|
355 | td->follow_ids = txl->list; |
---|
356 | if (txl->next_cursor) { |
---|
357 | /* These were just numbers. Up to 4000 in a response AFAIK so if we get here |
---|
358 | we may be using a spammer account. \o/ */ |
---|
359 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
360 | } else { |
---|
361 | /* Now to convert all those numbers into names.. */ |
---|
362 | twitter_get_users_lookup(ic); |
---|
363 | } |
---|
364 | |
---|
365 | txl->list = NULL; |
---|
366 | txl_free(txl); |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | * Callback for getting the mutes ids. |
---|
371 | */ |
---|
372 | static void twitter_http_get_mutes_ids(struct http_request *req) |
---|
373 | { |
---|
374 | struct im_connection *ic = req->data; |
---|
375 | json_value *parsed; |
---|
376 | struct twitter_xml_list *txl; |
---|
377 | struct twitter_data *td; |
---|
378 | |
---|
379 | // Check if the connection is stil active |
---|
380 | if (!g_slist_find(twitter_connections, ic)) { |
---|
381 | return; |
---|
382 | } |
---|
383 | |
---|
384 | td = ic->proto_data; |
---|
385 | |
---|
386 | // Parse the data. |
---|
387 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
388 | return; |
---|
389 | } |
---|
390 | |
---|
391 | txl = g_new0(struct twitter_xml_list, 1); |
---|
392 | txl->list = td->mutes_ids; |
---|
393 | |
---|
394 | /* mute ids API response is similar enough to friends response |
---|
395 | to reuse this method */ |
---|
396 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
397 | json_value_free(parsed); |
---|
398 | |
---|
399 | td->mutes_ids = txl->list; |
---|
400 | if (txl->next_cursor) { |
---|
401 | /* Recurse while there are still more pages */ |
---|
402 | twitter_get_mutes_ids(ic, txl->next_cursor); |
---|
403 | } |
---|
404 | |
---|
405 | txl->list = NULL; |
---|
406 | txl_free(txl); |
---|
407 | } |
---|
408 | |
---|
409 | /** |
---|
410 | * Callback for getting the no-retweets ids. |
---|
411 | */ |
---|
412 | static void twitter_http_get_noretweets_ids(struct http_request *req) |
---|
413 | { |
---|
414 | struct im_connection *ic = req->data; |
---|
415 | json_value *parsed; |
---|
416 | struct twitter_xml_list *txl; |
---|
417 | struct twitter_data *td; |
---|
418 | |
---|
419 | // Check if the connection is stil active |
---|
420 | if (!g_slist_find(twitter_connections, ic)) { |
---|
421 | return; |
---|
422 | } |
---|
423 | |
---|
424 | td = ic->proto_data; |
---|
425 | |
---|
426 | // Parse the data. |
---|
427 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
428 | return; |
---|
429 | } |
---|
430 | |
---|
431 | txl = g_new0(struct twitter_xml_list, 1); |
---|
432 | txl->list = td->noretweets_ids; |
---|
433 | |
---|
434 | // Process the retweet ids |
---|
435 | txl->type = TXL_ID; |
---|
436 | if (parsed->type == json_array) { |
---|
437 | unsigned int i; |
---|
438 | for (i = 0; i < parsed->u.array.length; i++) { |
---|
439 | json_value *c = parsed->u.array.values[i]; |
---|
440 | if (c->type != json_integer) { |
---|
441 | continue; |
---|
442 | } |
---|
443 | txl->list = g_slist_prepend(txl->list, |
---|
444 | g_strdup_printf("%"PRIu64, c->u.integer)); |
---|
445 | } |
---|
446 | } |
---|
447 | |
---|
448 | json_value_free(parsed); |
---|
449 | td->noretweets_ids = txl->list; |
---|
450 | |
---|
451 | txl->list = NULL; |
---|
452 | txl_free(txl); |
---|
453 | } |
---|
454 | |
---|
455 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl); |
---|
456 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
457 | |
---|
458 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
459 | { |
---|
460 | struct twitter_data *td = ic->proto_data; |
---|
461 | char *args[2] = { |
---|
462 | "user_id", |
---|
463 | NULL, |
---|
464 | }; |
---|
465 | GString *ids = g_string_new(""); |
---|
466 | int i; |
---|
467 | |
---|
468 | /* We can request up to 100 users at a time. */ |
---|
469 | for (i = 0; i < 100 && td->follow_ids; i++) { |
---|
470 | g_string_append_printf(ids, ",%s", (char *) td->follow_ids->data); |
---|
471 | g_free(td->follow_ids->data); |
---|
472 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
473 | } |
---|
474 | if (ids->len > 0) { |
---|
475 | args[1] = ids->str + 1; |
---|
476 | /* POST, because I think ids can be up to 1KB long. */ |
---|
477 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
478 | } else { |
---|
479 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
480 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
481 | twitter_login_finish(ic); |
---|
482 | } |
---|
483 | g_string_free(ids, TRUE); |
---|
484 | } |
---|
485 | |
---|
486 | /** |
---|
487 | * Callback for getting (twitter)friends... |
---|
488 | * |
---|
489 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
490 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
491 | * BitlBee... Get a life and meet new people! |
---|
492 | */ |
---|
493 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
494 | { |
---|
495 | struct im_connection *ic = req->data; |
---|
496 | json_value *parsed; |
---|
497 | struct twitter_xml_list *txl; |
---|
498 | GSList *l = NULL; |
---|
499 | struct twitter_xml_user *user; |
---|
500 | |
---|
501 | // Check if the connection is still active. |
---|
502 | if (!g_slist_find(twitter_connections, ic)) { |
---|
503 | return; |
---|
504 | } |
---|
505 | |
---|
506 | // Get the user list from the parsed xml feed. |
---|
507 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
508 | return; |
---|
509 | } |
---|
510 | |
---|
511 | txl = g_new0(struct twitter_xml_list, 1); |
---|
512 | txl->list = NULL; |
---|
513 | |
---|
514 | twitter_xt_get_users(parsed, txl); |
---|
515 | json_value_free(parsed); |
---|
516 | |
---|
517 | // Add the users as buddies. |
---|
518 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
519 | user = l->data; |
---|
520 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
521 | } |
---|
522 | |
---|
523 | // Free the structure. |
---|
524 | txl_free(txl); |
---|
525 | |
---|
526 | twitter_get_users_lookup(ic); |
---|
527 | } |
---|
528 | |
---|
529 | struct twitter_xml_user *twitter_xt_get_user(const json_value *node) |
---|
530 | { |
---|
531 | struct twitter_xml_user *txu; |
---|
532 | json_value *jv; |
---|
533 | |
---|
534 | txu = g_new0(struct twitter_xml_user, 1); |
---|
535 | txu->name = g_strdup(json_o_str(node, "name")); |
---|
536 | txu->screen_name = g_strdup(json_o_str(node, "screen_name")); |
---|
537 | |
---|
538 | jv = json_o_get(node, "id"); |
---|
539 | txu->uid = jv->u.integer; |
---|
540 | |
---|
541 | return txu; |
---|
542 | } |
---|
543 | |
---|
544 | /** |
---|
545 | * Function to fill a twitter_xml_list struct. |
---|
546 | * It sets: |
---|
547 | * - all <user>s from the <users> element. |
---|
548 | */ |
---|
549 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl) |
---|
550 | { |
---|
551 | struct twitter_xml_user *txu; |
---|
552 | int i; |
---|
553 | |
---|
554 | // Set the type of the list. |
---|
555 | txl->type = TXL_USER; |
---|
556 | |
---|
557 | if (!node || node->type != json_array) { |
---|
558 | return FALSE; |
---|
559 | } |
---|
560 | |
---|
561 | // The root <users> node should hold the list of users <user> |
---|
562 | // Walk over the nodes children. |
---|
563 | for (i = 0; i < node->u.array.length; i++) { |
---|
564 | txu = twitter_xt_get_user(node->u.array.values[i]); |
---|
565 | if (txu) { |
---|
566 | txl->list = g_slist_prepend(txl->list, txu); |
---|
567 | } |
---|
568 | } |
---|
569 | |
---|
570 | return TRUE; |
---|
571 | } |
---|
572 | |
---|
573 | #ifdef __GLIBC__ |
---|
574 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
575 | #else |
---|
576 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
577 | #endif |
---|
578 | |
---|
579 | static void expand_entities(char **text, const json_value *node); |
---|
580 | |
---|
581 | /** |
---|
582 | * Function to fill a twitter_xml_status struct. |
---|
583 | * It sets: |
---|
584 | * - the status text and |
---|
585 | * - the created_at timestamp and |
---|
586 | * - the status id and |
---|
587 | * - the user in a twitter_xml_user struct. |
---|
588 | */ |
---|
589 | static struct twitter_xml_status *twitter_xt_get_status(const json_value *node) |
---|
590 | { |
---|
591 | struct twitter_xml_status *txs; |
---|
592 | const json_value *rt = NULL; |
---|
593 | |
---|
594 | if (node->type != json_object) { |
---|
595 | return FALSE; |
---|
596 | } |
---|
597 | txs = g_new0(struct twitter_xml_status, 1); |
---|
598 | |
---|
599 | JSON_O_FOREACH(node, k, v) { |
---|
600 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
601 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
602 | strip_html(txs->text); |
---|
603 | } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) { |
---|
604 | rt = v; |
---|
605 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
606 | struct tm parsed; |
---|
607 | |
---|
608 | /* Very sensitive to changes to the formatting of |
---|
609 | this field. :-( Also assumes the timezone used |
---|
610 | is UTC since C time handling functions suck. */ |
---|
611 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) { |
---|
612 | txs->created_at = mktime_utc(&parsed); |
---|
613 | } |
---|
614 | } else if (strcmp("user", k) == 0 && v->type == json_object) { |
---|
615 | txs->user = twitter_xt_get_user(v); |
---|
616 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
617 | txs->rt_id = txs->id = v->u.integer; |
---|
618 | } else if (strcmp("in_reply_to_status_id", k) == 0 && v->type == json_integer) { |
---|
619 | txs->reply_to = v->u.integer; |
---|
620 | } |
---|
621 | } |
---|
622 | |
---|
623 | /* If it's a (truncated) retweet, get the original. Even if the API claims it |
---|
624 | wasn't truncated because it may be lying. */ |
---|
625 | if (rt) { |
---|
626 | struct twitter_xml_status *rtxs = twitter_xt_get_status(rt); |
---|
627 | if (rtxs) { |
---|
628 | g_free(txs->text); |
---|
629 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
630 | txs->id = rtxs->id; |
---|
631 | txs_free(rtxs); |
---|
632 | } |
---|
633 | } else { |
---|
634 | expand_entities(&txs->text, node); |
---|
635 | } |
---|
636 | |
---|
637 | if (txs->text && txs->user && txs->id) { |
---|
638 | return txs; |
---|
639 | } |
---|
640 | |
---|
641 | txs_free(txs); |
---|
642 | return NULL; |
---|
643 | } |
---|
644 | |
---|
645 | /** |
---|
646 | * Function to fill a twitter_xml_status struct (DM variant). |
---|
647 | */ |
---|
648 | static struct twitter_xml_status *twitter_xt_get_dm(const json_value *node) |
---|
649 | { |
---|
650 | struct twitter_xml_status *txs; |
---|
651 | |
---|
652 | if (node->type != json_object) { |
---|
653 | return FALSE; |
---|
654 | } |
---|
655 | txs = g_new0(struct twitter_xml_status, 1); |
---|
656 | |
---|
657 | JSON_O_FOREACH(node, k, v) { |
---|
658 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
659 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
660 | strip_html(txs->text); |
---|
661 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
662 | struct tm parsed; |
---|
663 | |
---|
664 | /* Very sensitive to changes to the formatting of |
---|
665 | this field. :-( Also assumes the timezone used |
---|
666 | is UTC since C time handling functions suck. */ |
---|
667 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) { |
---|
668 | txs->created_at = mktime_utc(&parsed); |
---|
669 | } |
---|
670 | } else if (strcmp("sender", k) == 0 && v->type == json_object) { |
---|
671 | txs->user = twitter_xt_get_user(v); |
---|
672 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
673 | txs->id = v->u.integer; |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | expand_entities(&txs->text, node); |
---|
678 | |
---|
679 | if (txs->text && txs->user && txs->id) { |
---|
680 | return txs; |
---|
681 | } |
---|
682 | |
---|
683 | txs_free(txs); |
---|
684 | return NULL; |
---|
685 | } |
---|
686 | |
---|
687 | static void expand_entities(char **text, const json_value *node) |
---|
688 | { |
---|
689 | json_value *entities, *quoted; |
---|
690 | char *quote_url = NULL, *quote_text = NULL; |
---|
691 | |
---|
692 | if (!((entities = json_o_get(node, "entities")) && entities->type == json_object)) |
---|
693 | return; |
---|
694 | if ((quoted = json_o_get(node, "quoted_status")) && quoted->type == json_object) { |
---|
695 | /* New "retweets with comments" feature. Note that this info |
---|
696 | * seems to be included in the streaming API only! Grab the |
---|
697 | * full message and try to insert it when we run into the |
---|
698 | * Tweet entity. */ |
---|
699 | struct twitter_xml_status *txs = twitter_xt_get_status(quoted); |
---|
700 | quote_text = g_strdup_printf("@%s: %s", txs->user->screen_name, txs->text); |
---|
701 | quote_url = g_strdup_printf("%s/status/%" G_GUINT64_FORMAT, txs->user->screen_name, txs->id); |
---|
702 | txs_free(txs); |
---|
703 | } else { |
---|
704 | quoted = NULL; |
---|
705 | } |
---|
706 | |
---|
707 | JSON_O_FOREACH(entities, k, v) { |
---|
708 | int i; |
---|
709 | |
---|
710 | if (v->type != json_array) { |
---|
711 | continue; |
---|
712 | } |
---|
713 | if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0) { |
---|
714 | continue; |
---|
715 | } |
---|
716 | |
---|
717 | for (i = 0; i < v->u.array.length; i++) { |
---|
718 | const char *format = "%s%s <%s>%s"; |
---|
719 | |
---|
720 | if (v->u.array.values[i]->type != json_object) { |
---|
721 | continue; |
---|
722 | } |
---|
723 | |
---|
724 | const char *kort = json_o_str(v->u.array.values[i], "url"); |
---|
725 | const char *disp = json_o_str(v->u.array.values[i], "display_url"); |
---|
726 | const char *full = json_o_str(v->u.array.values[i], "expanded_url"); |
---|
727 | char *pos, *new; |
---|
728 | |
---|
729 | if (!kort || !disp || !(pos = strstr(*text, kort))) { |
---|
730 | continue; |
---|
731 | } |
---|
732 | if (quote_url && strstr(full, quote_url)) { |
---|
733 | format = "%s<%s> [%s]%s"; |
---|
734 | disp = quote_text; |
---|
735 | } |
---|
736 | |
---|
737 | *pos = '\0'; |
---|
738 | new = g_strdup_printf(format, *text, kort, |
---|
739 | disp, pos + strlen(kort)); |
---|
740 | |
---|
741 | g_free(*text); |
---|
742 | *text = new; |
---|
743 | } |
---|
744 | } |
---|
745 | g_free(quote_text); |
---|
746 | g_free(quote_url); |
---|
747 | } |
---|
748 | |
---|
749 | /** |
---|
750 | * Function to fill a twitter_xml_list struct. |
---|
751 | * It sets: |
---|
752 | * - all <status>es within the <status> element and |
---|
753 | * - the next_cursor. |
---|
754 | */ |
---|
755 | static gboolean twitter_xt_get_status_list(struct im_connection *ic, const json_value *node, |
---|
756 | struct twitter_xml_list *txl) |
---|
757 | { |
---|
758 | struct twitter_xml_status *txs; |
---|
759 | int i; |
---|
760 | |
---|
761 | // Set the type of the list. |
---|
762 | txl->type = TXL_STATUS; |
---|
763 | |
---|
764 | if (node->type != json_array) { |
---|
765 | return FALSE; |
---|
766 | } |
---|
767 | |
---|
768 | // The root <statuses> node should hold the list of statuses <status> |
---|
769 | // Walk over the nodes children. |
---|
770 | for (i = 0; i < node->u.array.length; i++) { |
---|
771 | txs = twitter_xt_get_status(node->u.array.values[i]); |
---|
772 | if (!txs) { |
---|
773 | continue; |
---|
774 | } |
---|
775 | |
---|
776 | txl->list = g_slist_prepend(txl->list, txs); |
---|
777 | } |
---|
778 | |
---|
779 | return TRUE; |
---|
780 | } |
---|
781 | |
---|
782 | /* Will log messages either way. Need to keep track of IDs for stream deduping. |
---|
783 | Plus, show_ids is on by default and I don't see why anyone would disable it. */ |
---|
784 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
785 | struct twitter_xml_status *txs, const char *prefix) |
---|
786 | { |
---|
787 | struct twitter_data *td = ic->proto_data; |
---|
788 | int reply_to = -1; |
---|
789 | bee_user_t *bu; |
---|
790 | |
---|
791 | if (txs->reply_to) { |
---|
792 | int i; |
---|
793 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
794 | if (td->log[i].id == txs->reply_to) { |
---|
795 | reply_to = i; |
---|
796 | break; |
---|
797 | } |
---|
798 | } |
---|
799 | } |
---|
800 | |
---|
801 | if (txs->user && txs->user->screen_name && |
---|
802 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
803 | struct twitter_user_data *tud = bu->data; |
---|
804 | |
---|
805 | if (txs->id > tud->last_id) { |
---|
806 | tud->last_id = txs->id; |
---|
807 | tud->last_time = txs->created_at; |
---|
808 | } |
---|
809 | } |
---|
810 | |
---|
811 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
812 | td->log[td->log_id].id = txs->id; |
---|
813 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
814 | |
---|
815 | /* This is all getting hairy. :-( If we RT'ed something ourselves, |
---|
816 | remember OUR id instead so undo will work. In other cases, the |
---|
817 | original tweet's id should be remembered for deduplicating. */ |
---|
818 | if (g_strcasecmp(txs->user->screen_name, td->user) == 0) { |
---|
819 | td->log[td->log_id].id = txs->rt_id; |
---|
820 | /* More useful than NULL. */ |
---|
821 | td->log[td->log_id].bu = &twitter_log_local_user; |
---|
822 | } |
---|
823 | |
---|
824 | if (set_getbool(&ic->acc->set, "show_ids")) { |
---|
825 | if (reply_to != -1) { |
---|
826 | return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s", |
---|
827 | td->log_id, reply_to, prefix, txs->text); |
---|
828 | } else { |
---|
829 | return g_strdup_printf("\002[\002%02x\002]\002 %s%s", |
---|
830 | td->log_id, prefix, txs->text); |
---|
831 | } |
---|
832 | } else { |
---|
833 | if (*prefix) { |
---|
834 | return g_strconcat(prefix, txs->text, NULL); |
---|
835 | } else { |
---|
836 | return NULL; |
---|
837 | } |
---|
838 | } |
---|
839 | } |
---|
840 | |
---|
841 | /** |
---|
842 | * Function that is called to see the filter statuses in groupchat windows. |
---|
843 | */ |
---|
844 | static void twitter_status_show_filter(struct im_connection *ic, struct twitter_xml_status *status) |
---|
845 | { |
---|
846 | struct twitter_data *td = ic->proto_data; |
---|
847 | char *msg = twitter_msg_add_id(ic, status, ""); |
---|
848 | struct twitter_filter *tf; |
---|
849 | GSList *f; |
---|
850 | GSList *l; |
---|
851 | |
---|
852 | for (f = td->filters; f; f = g_slist_next(f)) { |
---|
853 | tf = f->data; |
---|
854 | |
---|
855 | switch (tf->type) { |
---|
856 | case TWITTER_FILTER_TYPE_FOLLOW: |
---|
857 | if (status->user->uid != tf->uid) { |
---|
858 | continue; |
---|
859 | } |
---|
860 | break; |
---|
861 | |
---|
862 | case TWITTER_FILTER_TYPE_TRACK: |
---|
863 | if (strcasestr(status->text, tf->text) == NULL) { |
---|
864 | continue; |
---|
865 | } |
---|
866 | break; |
---|
867 | |
---|
868 | default: |
---|
869 | continue; |
---|
870 | } |
---|
871 | |
---|
872 | for (l = tf->groupchats; l; l = g_slist_next(l)) { |
---|
873 | imcb_chat_msg(l->data, status->user->screen_name, |
---|
874 | msg ? msg : status->text, 0, 0); |
---|
875 | } |
---|
876 | } |
---|
877 | |
---|
878 | g_free(msg); |
---|
879 | } |
---|
880 | |
---|
881 | /** |
---|
882 | * Function that is called to see the statuses in a groupchat window. |
---|
883 | */ |
---|
884 | static void twitter_status_show_chat(struct im_connection *ic, struct twitter_xml_status *status) |
---|
885 | { |
---|
886 | struct twitter_data *td = ic->proto_data; |
---|
887 | struct groupchat *gc; |
---|
888 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
889 | char *msg; |
---|
890 | |
---|
891 | // Create a new groupchat if it does not exsist. |
---|
892 | gc = twitter_groupchat_init(ic); |
---|
893 | |
---|
894 | if (!me) { |
---|
895 | /* MUST be done before twitter_msg_add_id() to avoid #872. */ |
---|
896 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
897 | } |
---|
898 | msg = twitter_msg_add_id(ic, status, ""); |
---|
899 | |
---|
900 | // Say it! |
---|
901 | if (me) { |
---|
902 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
903 | } else { |
---|
904 | imcb_chat_msg(gc, status->user->screen_name, |
---|
905 | msg ? msg : status->text, 0, status->created_at); |
---|
906 | } |
---|
907 | |
---|
908 | g_free(msg); |
---|
909 | } |
---|
910 | |
---|
911 | /** |
---|
912 | * Function that is called to see statuses as private messages. |
---|
913 | */ |
---|
914 | static void twitter_status_show_msg(struct im_connection *ic, struct twitter_xml_status *status) |
---|
915 | { |
---|
916 | struct twitter_data *td = ic->proto_data; |
---|
917 | char from[MAX_STRING] = ""; |
---|
918 | char *prefix = NULL, *text = NULL; |
---|
919 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
920 | |
---|
921 | if (td->flags & TWITTER_MODE_ONE) { |
---|
922 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
923 | from[MAX_STRING - 1] = '\0'; |
---|
924 | } |
---|
925 | |
---|
926 | if (td->flags & TWITTER_MODE_ONE) { |
---|
927 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
928 | status->user->screen_name); |
---|
929 | } else if (!me) { |
---|
930 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
931 | } else { |
---|
932 | prefix = g_strdup("You: "); |
---|
933 | } |
---|
934 | |
---|
935 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
936 | |
---|
937 | imcb_buddy_msg(ic, |
---|
938 | *from ? from : status->user->screen_name, |
---|
939 | text ? text : status->text, 0, status->created_at); |
---|
940 | |
---|
941 | g_free(text); |
---|
942 | g_free(prefix); |
---|
943 | } |
---|
944 | |
---|
945 | static void twitter_status_show(struct im_connection *ic, struct twitter_xml_status *status) |
---|
946 | { |
---|
947 | struct twitter_data *td = ic->proto_data; |
---|
948 | char *last_id_str; |
---|
949 | char *uid_str; |
---|
950 | |
---|
951 | if (status->user == NULL || status->text == NULL) { |
---|
952 | return; |
---|
953 | } |
---|
954 | |
---|
955 | /* Check this is not a tweet that should be muted */ |
---|
956 | uid_str = g_strdup_printf("%" PRIu64, status->user->uid); |
---|
957 | |
---|
958 | if (g_slist_find_custom(td->mutes_ids, uid_str, (GCompareFunc)strcmp)) { |
---|
959 | g_free(uid_str); |
---|
960 | return; |
---|
961 | } |
---|
962 | if (status->id != status->rt_id && g_slist_find_custom(td->noretweets_ids, uid_str, (GCompareFunc)strcmp)) { |
---|
963 | g_free(uid_str); |
---|
964 | return; |
---|
965 | } |
---|
966 | |
---|
967 | /* Grrrr. Would like to do this during parsing, but can't access |
---|
968 | settings from there. */ |
---|
969 | if (set_getbool(&ic->acc->set, "strip_newlines")) { |
---|
970 | strip_newlines(status->text); |
---|
971 | } |
---|
972 | |
---|
973 | if (status->from_filter) { |
---|
974 | twitter_status_show_filter(ic, status); |
---|
975 | } else if (td->flags & TWITTER_MODE_CHAT) { |
---|
976 | twitter_status_show_chat(ic, status); |
---|
977 | } else { |
---|
978 | twitter_status_show_msg(ic, status); |
---|
979 | } |
---|
980 | |
---|
981 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
982 | // we won't pick up the updates already in the list. |
---|
983 | td->timeline_id = MAX(td->timeline_id, status->rt_id); |
---|
984 | |
---|
985 | last_id_str = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
986 | set_setstr(&ic->acc->set, "_last_tweet", last_id_str); |
---|
987 | g_free(last_id_str); |
---|
988 | g_free(uid_str); |
---|
989 | } |
---|
990 | |
---|
991 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o, gboolean from_filter); |
---|
992 | |
---|
993 | static void twitter_http_stream(struct http_request *req) |
---|
994 | { |
---|
995 | struct im_connection *ic = req->data; |
---|
996 | struct twitter_data *td; |
---|
997 | json_value *parsed; |
---|
998 | int len = 0; |
---|
999 | char c, *nl; |
---|
1000 | gboolean from_filter; |
---|
1001 | |
---|
1002 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1003 | return; |
---|
1004 | } |
---|
1005 | |
---|
1006 | ic->flags |= OPT_PONGED; |
---|
1007 | td = ic->proto_data; |
---|
1008 | |
---|
1009 | if ((req->flags & HTTPC_EOF) || !req->reply_body) { |
---|
1010 | if (req == td->stream) { |
---|
1011 | td->stream = NULL; |
---|
1012 | } else if (req == td->filter_stream) { |
---|
1013 | td->filter_stream = NULL; |
---|
1014 | } |
---|
1015 | |
---|
1016 | imcb_error(ic, "Stream closed (%s)", req->status_string); |
---|
1017 | if (req->status_code == 401) { |
---|
1018 | imcb_error(ic, "Check your system clock."); |
---|
1019 | } |
---|
1020 | imc_logout(ic, TRUE); |
---|
1021 | return; |
---|
1022 | } |
---|
1023 | |
---|
1024 | /* MUST search for CRLF, not just LF: |
---|
1025 | https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */ |
---|
1026 | if (!(nl = strstr(req->reply_body, "\r\n"))) { |
---|
1027 | return; |
---|
1028 | } |
---|
1029 | |
---|
1030 | len = nl - req->reply_body; |
---|
1031 | if (len > 0) { |
---|
1032 | c = req->reply_body[len]; |
---|
1033 | req->reply_body[len] = '\0'; |
---|
1034 | |
---|
1035 | if ((parsed = json_parse(req->reply_body, req->body_size))) { |
---|
1036 | from_filter = (req == td->filter_stream); |
---|
1037 | twitter_stream_handle_object(ic, parsed, from_filter); |
---|
1038 | } |
---|
1039 | json_value_free(parsed); |
---|
1040 | req->reply_body[len] = c; |
---|
1041 | } |
---|
1042 | |
---|
1043 | http_flush_bytes(req, len + 2); |
---|
1044 | |
---|
1045 | /* One notification might bring multiple events! */ |
---|
1046 | if (req->body_size > 0) { |
---|
1047 | twitter_http_stream(req); |
---|
1048 | } |
---|
1049 | } |
---|
1050 | |
---|
1051 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o); |
---|
1052 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs); |
---|
1053 | |
---|
1054 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o, gboolean from_filter) |
---|
1055 | { |
---|
1056 | struct twitter_data *td = ic->proto_data; |
---|
1057 | struct twitter_xml_status *txs; |
---|
1058 | json_value *c; |
---|
1059 | |
---|
1060 | if ((txs = twitter_xt_get_status(o))) { |
---|
1061 | txs->from_filter = from_filter; |
---|
1062 | gboolean ret = twitter_stream_handle_status(ic, txs); |
---|
1063 | txs_free(txs); |
---|
1064 | return ret; |
---|
1065 | } else if ((c = json_o_get(o, "direct_message")) && |
---|
1066 | (txs = twitter_xt_get_dm(c))) { |
---|
1067 | if (g_strcasecmp(txs->user->screen_name, td->user) != 0) { |
---|
1068 | imcb_buddy_msg(ic, txs->user->screen_name, |
---|
1069 | txs->text, 0, txs->created_at); |
---|
1070 | } |
---|
1071 | txs_free(txs); |
---|
1072 | return TRUE; |
---|
1073 | } else if ((c = json_o_get(o, "event")) && c->type == json_string) { |
---|
1074 | twitter_stream_handle_event(ic, o); |
---|
1075 | return TRUE; |
---|
1076 | } else if ((c = json_o_get(o, "disconnect")) && c->type == json_object) { |
---|
1077 | /* HACK: Because we're inside an event handler, we can't just |
---|
1078 | disconnect here. Instead, just change the HTTP status string |
---|
1079 | into a Twitter status string. */ |
---|
1080 | char *reason = json_o_strdup(c, "reason"); |
---|
1081 | if (reason) { |
---|
1082 | g_free(td->stream->status_string); |
---|
1083 | td->stream->status_string = reason; |
---|
1084 | } |
---|
1085 | return TRUE; |
---|
1086 | } |
---|
1087 | return FALSE; |
---|
1088 | } |
---|
1089 | |
---|
1090 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs) |
---|
1091 | { |
---|
1092 | struct twitter_data *td = ic->proto_data; |
---|
1093 | int i; |
---|
1094 | |
---|
1095 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
1096 | if (td->log[i].id == txs->id) { |
---|
1097 | /* Got a duplicate (RT, probably). Drop it. */ |
---|
1098 | return TRUE; |
---|
1099 | } |
---|
1100 | } |
---|
1101 | |
---|
1102 | if (!(g_strcasecmp(txs->user->screen_name, td->user) == 0 || |
---|
1103 | set_getbool(&ic->acc->set, "fetch_mentions") || |
---|
1104 | bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
1105 | /* Tweet is from an unknown person and the user does not want |
---|
1106 | to see @mentions, so drop it. twitter_stream_handle_event() |
---|
1107 | picks up new follows so this simple filter should be safe. */ |
---|
1108 | /* TODO: The streaming API seems to do poor @mention matching. |
---|
1109 | I.e. I'm getting mentions for @WilmerSomething, not just for |
---|
1110 | @Wilmer. But meh. You want spam, you get spam. */ |
---|
1111 | return TRUE; |
---|
1112 | } |
---|
1113 | |
---|
1114 | twitter_status_show(ic, txs); |
---|
1115 | |
---|
1116 | return TRUE; |
---|
1117 | } |
---|
1118 | |
---|
1119 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o) |
---|
1120 | { |
---|
1121 | struct twitter_data *td = ic->proto_data; |
---|
1122 | json_value *source = json_o_get(o, "source"); |
---|
1123 | json_value *target = json_o_get(o, "target"); |
---|
1124 | const char *type = json_o_str(o, "event"); |
---|
1125 | struct twitter_xml_user *us = NULL; |
---|
1126 | struct twitter_xml_user *ut = NULL; |
---|
1127 | |
---|
1128 | if (!type || !source || source->type != json_object |
---|
1129 | || !target || target->type != json_object) { |
---|
1130 | return FALSE; |
---|
1131 | } |
---|
1132 | |
---|
1133 | if (strcmp(type, "follow") == 0) { |
---|
1134 | us = twitter_xt_get_user(source); |
---|
1135 | ut = twitter_xt_get_user(target); |
---|
1136 | if (g_strcasecmp(us->screen_name, td->user) == 0) { |
---|
1137 | twitter_add_buddy(ic, ut->screen_name, ut->name); |
---|
1138 | } |
---|
1139 | } else if (strcmp(type, "mute") == 0) { |
---|
1140 | GSList *found; |
---|
1141 | char *uid_str; |
---|
1142 | ut = twitter_xt_get_user(target); |
---|
1143 | uid_str = g_strdup_printf("%" PRIu64, ut->uid); |
---|
1144 | if (!(found = g_slist_find_custom(td->mutes_ids, uid_str, |
---|
1145 | (GCompareFunc)strcmp))) { |
---|
1146 | td->mutes_ids = g_slist_prepend(td->mutes_ids, uid_str); |
---|
1147 | } |
---|
1148 | twitter_log(ic, "Muted user %s", ut->screen_name); |
---|
1149 | if (getenv("BITLBEE_DEBUG")) { |
---|
1150 | fprintf(stderr, "New mute: %s %"PRIu64"\n", |
---|
1151 | ut->screen_name, ut->uid); |
---|
1152 | } |
---|
1153 | } else if (strcmp(type, "unmute") == 0) { |
---|
1154 | GSList *found; |
---|
1155 | char *uid_str; |
---|
1156 | ut = twitter_xt_get_user(target); |
---|
1157 | uid_str = g_strdup_printf("%" PRIu64, ut->uid); |
---|
1158 | if ((found = g_slist_find_custom(td->mutes_ids, uid_str, |
---|
1159 | (GCompareFunc)strcmp))) { |
---|
1160 | char *found_str = found->data; |
---|
1161 | td->mutes_ids = g_slist_delete_link(td->mutes_ids, found); |
---|
1162 | g_free(found_str); |
---|
1163 | } |
---|
1164 | g_free(uid_str); |
---|
1165 | twitter_log(ic, "Unmuted user %s", ut->screen_name); |
---|
1166 | if (getenv("BITLBEE_DEBUG")) { |
---|
1167 | fprintf(stderr, "New unmute: %s %"PRIu64"\n", |
---|
1168 | ut->screen_name, ut->uid); |
---|
1169 | } |
---|
1170 | } |
---|
1171 | |
---|
1172 | txu_free(us); |
---|
1173 | txu_free(ut); |
---|
1174 | |
---|
1175 | return TRUE; |
---|
1176 | } |
---|
1177 | |
---|
1178 | gboolean twitter_open_stream(struct im_connection *ic) |
---|
1179 | { |
---|
1180 | struct twitter_data *td = ic->proto_data; |
---|
1181 | char *args[2] = { "with", "followings" }; |
---|
1182 | |
---|
1183 | if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL, |
---|
1184 | twitter_http_stream, ic, 0, args, 2))) { |
---|
1185 | /* This flag must be enabled or we'll get no data until EOF |
---|
1186 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
1187 | td->stream->flags |= HTTPC_STREAMING; |
---|
1188 | return TRUE; |
---|
1189 | } |
---|
1190 | |
---|
1191 | return FALSE; |
---|
1192 | } |
---|
1193 | |
---|
1194 | static gboolean twitter_filter_stream(struct im_connection *ic) |
---|
1195 | { |
---|
1196 | struct twitter_data *td = ic->proto_data; |
---|
1197 | char *args[4] = { "follow", NULL, "track", NULL }; |
---|
1198 | GString *followstr = g_string_new(""); |
---|
1199 | GString *trackstr = g_string_new(""); |
---|
1200 | gboolean ret = FALSE; |
---|
1201 | struct twitter_filter *tf; |
---|
1202 | GSList *l; |
---|
1203 | |
---|
1204 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
1205 | tf = l->data; |
---|
1206 | |
---|
1207 | switch (tf->type) { |
---|
1208 | case TWITTER_FILTER_TYPE_FOLLOW: |
---|
1209 | if (followstr->len > 0) { |
---|
1210 | g_string_append_c(followstr, ','); |
---|
1211 | } |
---|
1212 | |
---|
1213 | g_string_append_printf(followstr, "%" G_GUINT64_FORMAT, |
---|
1214 | tf->uid); |
---|
1215 | break; |
---|
1216 | |
---|
1217 | case TWITTER_FILTER_TYPE_TRACK: |
---|
1218 | if (trackstr->len > 0) { |
---|
1219 | g_string_append_c(trackstr, ','); |
---|
1220 | } |
---|
1221 | |
---|
1222 | g_string_append(trackstr, tf->text); |
---|
1223 | break; |
---|
1224 | |
---|
1225 | default: |
---|
1226 | continue; |
---|
1227 | } |
---|
1228 | } |
---|
1229 | |
---|
1230 | args[1] = followstr->str; |
---|
1231 | args[3] = trackstr->str; |
---|
1232 | |
---|
1233 | if (td->filter_stream) { |
---|
1234 | http_close(td->filter_stream); |
---|
1235 | } |
---|
1236 | |
---|
1237 | if ((td->filter_stream = twitter_http(ic, TWITTER_FILTER_STREAM_URL, |
---|
1238 | twitter_http_stream, ic, 0, |
---|
1239 | args, 4))) { |
---|
1240 | /* This flag must be enabled or we'll get no data until EOF |
---|
1241 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
1242 | td->filter_stream->flags |= HTTPC_STREAMING; |
---|
1243 | ret = TRUE; |
---|
1244 | } |
---|
1245 | |
---|
1246 | g_string_free(followstr, TRUE); |
---|
1247 | g_string_free(trackstr, TRUE); |
---|
1248 | |
---|
1249 | return ret; |
---|
1250 | } |
---|
1251 | |
---|
1252 | static void twitter_filter_users_post(struct http_request *req) |
---|
1253 | { |
---|
1254 | struct im_connection *ic = req->data; |
---|
1255 | struct twitter_data *td; |
---|
1256 | struct twitter_filter *tf; |
---|
1257 | GList *users = NULL; |
---|
1258 | json_value *parsed; |
---|
1259 | json_value *id; |
---|
1260 | const char *name; |
---|
1261 | GString *fstr; |
---|
1262 | GSList *l; |
---|
1263 | GList *u; |
---|
1264 | int i; |
---|
1265 | |
---|
1266 | // Check if the connection is still active. |
---|
1267 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1268 | return; |
---|
1269 | } |
---|
1270 | |
---|
1271 | td = ic->proto_data; |
---|
1272 | |
---|
1273 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1274 | return; |
---|
1275 | } |
---|
1276 | |
---|
1277 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
1278 | tf = l->data; |
---|
1279 | |
---|
1280 | if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) { |
---|
1281 | users = g_list_prepend(users, tf); |
---|
1282 | } |
---|
1283 | } |
---|
1284 | |
---|
1285 | if (parsed->type != json_array) { |
---|
1286 | goto finish; |
---|
1287 | } |
---|
1288 | |
---|
1289 | for (i = 0; i < parsed->u.array.length; i++) { |
---|
1290 | id = json_o_get(parsed->u.array.values[i], "id"); |
---|
1291 | name = json_o_str(parsed->u.array.values[i], "screen_name"); |
---|
1292 | |
---|
1293 | if (!name || !id || id->type != json_integer) { |
---|
1294 | continue; |
---|
1295 | } |
---|
1296 | |
---|
1297 | for (u = users; u; u = g_list_next(u)) { |
---|
1298 | tf = u->data; |
---|
1299 | |
---|
1300 | if (g_strcasecmp(tf->text, name) == 0) { |
---|
1301 | tf->uid = id->u.integer; |
---|
1302 | users = g_list_delete_link(users, u); |
---|
1303 | break; |
---|
1304 | } |
---|
1305 | } |
---|
1306 | } |
---|
1307 | |
---|
1308 | finish: |
---|
1309 | json_value_free(parsed); |
---|
1310 | twitter_filter_stream(ic); |
---|
1311 | |
---|
1312 | if (!users) { |
---|
1313 | return; |
---|
1314 | } |
---|
1315 | |
---|
1316 | fstr = g_string_new(""); |
---|
1317 | |
---|
1318 | for (u = users; u; u = g_list_next(u)) { |
---|
1319 | if (fstr->len > 0) { |
---|
1320 | g_string_append(fstr, ", "); |
---|
1321 | } |
---|
1322 | |
---|
1323 | g_string_append(fstr, tf->text); |
---|
1324 | } |
---|
1325 | |
---|
1326 | imcb_error(ic, "Failed UID acquisitions: %s", fstr->str); |
---|
1327 | |
---|
1328 | g_string_free(fstr, TRUE); |
---|
1329 | g_list_free(users); |
---|
1330 | } |
---|
1331 | |
---|
1332 | gboolean twitter_open_filter_stream(struct im_connection *ic) |
---|
1333 | { |
---|
1334 | struct twitter_data *td = ic->proto_data; |
---|
1335 | char *args[2] = { "screen_name", NULL }; |
---|
1336 | GString *ustr = g_string_new(""); |
---|
1337 | struct twitter_filter *tf; |
---|
1338 | struct http_request *req; |
---|
1339 | GSList *l; |
---|
1340 | |
---|
1341 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
1342 | tf = l->data; |
---|
1343 | |
---|
1344 | if (tf->type != TWITTER_FILTER_TYPE_FOLLOW || tf->uid != 0) { |
---|
1345 | continue; |
---|
1346 | } |
---|
1347 | |
---|
1348 | if (ustr->len > 0) { |
---|
1349 | g_string_append_c(ustr, ','); |
---|
1350 | } |
---|
1351 | |
---|
1352 | g_string_append(ustr, tf->text); |
---|
1353 | } |
---|
1354 | |
---|
1355 | if (ustr->len == 0) { |
---|
1356 | g_string_free(ustr, TRUE); |
---|
1357 | return twitter_filter_stream(ic); |
---|
1358 | } |
---|
1359 | |
---|
1360 | args[1] = ustr->str; |
---|
1361 | req = twitter_http(ic, TWITTER_USERS_LOOKUP_URL, |
---|
1362 | twitter_filter_users_post, |
---|
1363 | ic, 0, args, 2); |
---|
1364 | |
---|
1365 | g_string_free(ustr, TRUE); |
---|
1366 | return req != NULL; |
---|
1367 | } |
---|
1368 | |
---|
1369 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor); |
---|
1370 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor); |
---|
1371 | |
---|
1372 | /** |
---|
1373 | * Get the timeline with optionally mentions |
---|
1374 | */ |
---|
1375 | gboolean twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
1376 | { |
---|
1377 | struct twitter_data *td = ic->proto_data; |
---|
1378 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
1379 | |
---|
1380 | if (td->flags & TWITTER_DOING_TIMELINE) { |
---|
1381 | if (++td->http_fails >= 5) { |
---|
1382 | imcb_error(ic, "Fetch timeout (%d)", td->flags); |
---|
1383 | imc_logout(ic, TRUE); |
---|
1384 | return FALSE; |
---|
1385 | } |
---|
1386 | } |
---|
1387 | |
---|
1388 | td->flags |= TWITTER_DOING_TIMELINE; |
---|
1389 | |
---|
1390 | twitter_get_home_timeline(ic, next_cursor); |
---|
1391 | |
---|
1392 | if (include_mentions) { |
---|
1393 | twitter_get_mentions(ic, next_cursor); |
---|
1394 | } |
---|
1395 | |
---|
1396 | return TRUE; |
---|
1397 | } |
---|
1398 | |
---|
1399 | /** |
---|
1400 | * Call this one after receiving timeline/mentions. Show to user once we have |
---|
1401 | * both. |
---|
1402 | */ |
---|
1403 | void twitter_flush_timeline(struct im_connection *ic) |
---|
1404 | { |
---|
1405 | struct twitter_data *td = ic->proto_data; |
---|
1406 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
1407 | int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions"); |
---|
1408 | struct twitter_xml_list *home_timeline = td->home_timeline_obj; |
---|
1409 | struct twitter_xml_list *mentions = td->mentions_obj; |
---|
1410 | guint64 last_id = 0; |
---|
1411 | GSList *output = NULL; |
---|
1412 | GSList *l; |
---|
1413 | |
---|
1414 | imcb_connected(ic); |
---|
1415 | |
---|
1416 | if (!(td->flags & TWITTER_GOT_TIMELINE)) { |
---|
1417 | return; |
---|
1418 | } |
---|
1419 | |
---|
1420 | if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) { |
---|
1421 | return; |
---|
1422 | } |
---|
1423 | |
---|
1424 | if (home_timeline && home_timeline->list) { |
---|
1425 | for (l = home_timeline->list; l; l = g_slist_next(l)) { |
---|
1426 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
1427 | } |
---|
1428 | } |
---|
1429 | |
---|
1430 | if (include_mentions && mentions && mentions->list) { |
---|
1431 | for (l = mentions->list; l; l = g_slist_next(l)) { |
---|
1432 | if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) { |
---|
1433 | continue; |
---|
1434 | } |
---|
1435 | |
---|
1436 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
1437 | } |
---|
1438 | } |
---|
1439 | |
---|
1440 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
1441 | while (output) { |
---|
1442 | struct twitter_xml_status *txs = output->data; |
---|
1443 | if (txs->id != last_id) { |
---|
1444 | twitter_status_show(ic, txs); |
---|
1445 | } |
---|
1446 | last_id = txs->id; |
---|
1447 | output = g_slist_remove(output, txs); |
---|
1448 | } |
---|
1449 | |
---|
1450 | txl_free(home_timeline); |
---|
1451 | txl_free(mentions); |
---|
1452 | |
---|
1453 | td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS); |
---|
1454 | td->home_timeline_obj = td->mentions_obj = NULL; |
---|
1455 | } |
---|
1456 | |
---|
1457 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
1458 | static void twitter_http_get_mentions(struct http_request *req); |
---|
1459 | |
---|
1460 | /** |
---|
1461 | * Get the timeline. |
---|
1462 | */ |
---|
1463 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
1464 | { |
---|
1465 | struct twitter_data *td = ic->proto_data; |
---|
1466 | |
---|
1467 | txl_free(td->home_timeline_obj); |
---|
1468 | td->home_timeline_obj = NULL; |
---|
1469 | td->flags &= ~TWITTER_GOT_TIMELINE; |
---|
1470 | |
---|
1471 | char *args[6]; |
---|
1472 | args[0] = "cursor"; |
---|
1473 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
1474 | args[2] = "include_entities"; |
---|
1475 | args[3] = "true"; |
---|
1476 | if (td->timeline_id) { |
---|
1477 | args[4] = "since_id"; |
---|
1478 | args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
1479 | } |
---|
1480 | |
---|
1481 | if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, |
---|
1482 | td->timeline_id ? 6 : 4) == NULL) { |
---|
1483 | if (++td->http_fails >= 5) { |
---|
1484 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
1485 | TWITTER_HOME_TIMELINE_URL, "connection failed"); |
---|
1486 | } |
---|
1487 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
1488 | twitter_flush_timeline(ic); |
---|
1489 | } |
---|
1490 | |
---|
1491 | g_free(args[1]); |
---|
1492 | if (td->timeline_id) { |
---|
1493 | g_free(args[5]); |
---|
1494 | } |
---|
1495 | } |
---|
1496 | |
---|
1497 | /** |
---|
1498 | * Get mentions. |
---|
1499 | */ |
---|
1500 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) |
---|
1501 | { |
---|
1502 | struct twitter_data *td = ic->proto_data; |
---|
1503 | |
---|
1504 | txl_free(td->mentions_obj); |
---|
1505 | td->mentions_obj = NULL; |
---|
1506 | td->flags &= ~TWITTER_GOT_MENTIONS; |
---|
1507 | |
---|
1508 | char *args[6]; |
---|
1509 | args[0] = "cursor"; |
---|
1510 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
1511 | args[2] = "include_entities"; |
---|
1512 | args[3] = "true"; |
---|
1513 | if (td->timeline_id) { |
---|
1514 | args[4] = "since_id"; |
---|
1515 | args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
1516 | } else { |
---|
1517 | args[4] = "count"; |
---|
1518 | args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions")); |
---|
1519 | } |
---|
1520 | |
---|
1521 | if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions, |
---|
1522 | ic, 0, args, 6) == NULL) { |
---|
1523 | if (++td->http_fails >= 5) { |
---|
1524 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
1525 | TWITTER_MENTIONS_URL, "connection failed"); |
---|
1526 | } |
---|
1527 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
1528 | twitter_flush_timeline(ic); |
---|
1529 | } |
---|
1530 | |
---|
1531 | g_free(args[1]); |
---|
1532 | g_free(args[5]); |
---|
1533 | } |
---|
1534 | |
---|
1535 | /** |
---|
1536 | * Callback for getting the home timeline. |
---|
1537 | */ |
---|
1538 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
1539 | { |
---|
1540 | struct im_connection *ic = req->data; |
---|
1541 | struct twitter_data *td; |
---|
1542 | json_value *parsed; |
---|
1543 | struct twitter_xml_list *txl; |
---|
1544 | |
---|
1545 | // Check if the connection is still active. |
---|
1546 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1547 | return; |
---|
1548 | } |
---|
1549 | |
---|
1550 | td = ic->proto_data; |
---|
1551 | |
---|
1552 | // The root <statuses> node should hold the list of statuses <status> |
---|
1553 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1554 | goto end; |
---|
1555 | } |
---|
1556 | |
---|
1557 | txl = g_new0(struct twitter_xml_list, 1); |
---|
1558 | txl->list = NULL; |
---|
1559 | |
---|
1560 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
1561 | json_value_free(parsed); |
---|
1562 | |
---|
1563 | td->home_timeline_obj = txl; |
---|
1564 | |
---|
1565 | end: |
---|
1566 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1567 | return; |
---|
1568 | } |
---|
1569 | |
---|
1570 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
1571 | |
---|
1572 | twitter_flush_timeline(ic); |
---|
1573 | } |
---|
1574 | |
---|
1575 | /** |
---|
1576 | * Callback for getting mentions. |
---|
1577 | */ |
---|
1578 | static void twitter_http_get_mentions(struct http_request *req) |
---|
1579 | { |
---|
1580 | struct im_connection *ic = req->data; |
---|
1581 | struct twitter_data *td; |
---|
1582 | json_value *parsed; |
---|
1583 | struct twitter_xml_list *txl; |
---|
1584 | |
---|
1585 | // Check if the connection is still active. |
---|
1586 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1587 | return; |
---|
1588 | } |
---|
1589 | |
---|
1590 | td = ic->proto_data; |
---|
1591 | |
---|
1592 | // The root <statuses> node should hold the list of statuses <status> |
---|
1593 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1594 | goto end; |
---|
1595 | } |
---|
1596 | |
---|
1597 | txl = g_new0(struct twitter_xml_list, 1); |
---|
1598 | txl->list = NULL; |
---|
1599 | |
---|
1600 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
1601 | json_value_free(parsed); |
---|
1602 | |
---|
1603 | td->mentions_obj = txl; |
---|
1604 | |
---|
1605 | end: |
---|
1606 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1607 | return; |
---|
1608 | } |
---|
1609 | |
---|
1610 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
1611 | |
---|
1612 | twitter_flush_timeline(ic); |
---|
1613 | } |
---|
1614 | |
---|
1615 | /** |
---|
1616 | * Callback to use after sending a POST request to twitter. |
---|
1617 | * (Generic, used for a few kinds of queries.) |
---|
1618 | */ |
---|
1619 | static void twitter_http_post(struct http_request *req) |
---|
1620 | { |
---|
1621 | struct im_connection *ic = req->data; |
---|
1622 | struct twitter_data *td; |
---|
1623 | json_value *parsed, *id; |
---|
1624 | |
---|
1625 | // Check if the connection is still active. |
---|
1626 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1627 | return; |
---|
1628 | } |
---|
1629 | |
---|
1630 | td = ic->proto_data; |
---|
1631 | td->last_status_id = 0; |
---|
1632 | |
---|
1633 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1634 | return; |
---|
1635 | } |
---|
1636 | |
---|
1637 | if ((id = json_o_get(parsed, "id")) && id->type == json_integer) { |
---|
1638 | td->last_status_id = id->u.integer; |
---|
1639 | } |
---|
1640 | |
---|
1641 | json_value_free(parsed); |
---|
1642 | |
---|
1643 | if (req->flags & TWITTER_HTTP_USER_ACK) { |
---|
1644 | twitter_log(ic, "Command processed successfully"); |
---|
1645 | } |
---|
1646 | } |
---|
1647 | |
---|
1648 | /** |
---|
1649 | * Function to POST a new status to twitter. |
---|
1650 | */ |
---|
1651 | void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to) |
---|
1652 | { |
---|
1653 | char *args[4] = { |
---|
1654 | "status", msg, |
---|
1655 | "in_reply_to_status_id", |
---|
1656 | g_strdup_printf("%" G_GUINT64_FORMAT, in_reply_to) |
---|
1657 | }; |
---|
1658 | |
---|
1659 | twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, |
---|
1660 | args, in_reply_to ? 4 : 2); |
---|
1661 | g_free(args[3]); |
---|
1662 | } |
---|
1663 | |
---|
1664 | |
---|
1665 | /** |
---|
1666 | * Function to POST a new message to twitter. |
---|
1667 | */ |
---|
1668 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
1669 | { |
---|
1670 | char *args[4]; |
---|
1671 | |
---|
1672 | args[0] = "screen_name"; |
---|
1673 | args[1] = who; |
---|
1674 | args[2] = "text"; |
---|
1675 | args[3] = msg; |
---|
1676 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
1677 | twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); |
---|
1678 | } |
---|
1679 | |
---|
1680 | void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) |
---|
1681 | { |
---|
1682 | char *args[2]; |
---|
1683 | |
---|
1684 | args[0] = "screen_name"; |
---|
1685 | args[1] = who; |
---|
1686 | twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, |
---|
1687 | twitter_http_post, ic, 1, args, 2); |
---|
1688 | } |
---|
1689 | |
---|
1690 | /** |
---|
1691 | * Mute or unmute a user |
---|
1692 | */ |
---|
1693 | void twitter_mute_create_destroy(struct im_connection *ic, char *who, int create) |
---|
1694 | { |
---|
1695 | char *args[2]; |
---|
1696 | |
---|
1697 | args[0] = "screen_name"; |
---|
1698 | args[1] = who; |
---|
1699 | twitter_http(ic, create ? TWITTER_MUTES_CREATE_URL : TWITTER_MUTES_DESTROY_URL, |
---|
1700 | twitter_http_post, ic, 1, args, 2); |
---|
1701 | } |
---|
1702 | |
---|
1703 | void twitter_status_destroy(struct im_connection *ic, guint64 id) |
---|
1704 | { |
---|
1705 | char *url; |
---|
1706 | |
---|
1707 | url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", |
---|
1708 | TWITTER_STATUS_DESTROY_URL, id, ".json"); |
---|
1709 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
1710 | TWITTER_HTTP_USER_ACK); |
---|
1711 | g_free(url); |
---|
1712 | } |
---|
1713 | |
---|
1714 | void twitter_status_retweet(struct im_connection *ic, guint64 id) |
---|
1715 | { |
---|
1716 | char *url; |
---|
1717 | |
---|
1718 | url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", |
---|
1719 | TWITTER_STATUS_RETWEET_URL, id, ".json"); |
---|
1720 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
1721 | TWITTER_HTTP_USER_ACK); |
---|
1722 | g_free(url); |
---|
1723 | } |
---|
1724 | |
---|
1725 | /** |
---|
1726 | * Report a user for sending spam. |
---|
1727 | */ |
---|
1728 | void twitter_report_spam(struct im_connection *ic, char *screen_name) |
---|
1729 | { |
---|
1730 | char *args[2] = { |
---|
1731 | "screen_name", |
---|
1732 | NULL, |
---|
1733 | }; |
---|
1734 | |
---|
1735 | args[1] = screen_name; |
---|
1736 | twitter_http_f(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post, |
---|
1737 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
1738 | } |
---|
1739 | |
---|
1740 | /** |
---|
1741 | * Favourite a tweet. |
---|
1742 | */ |
---|
1743 | void twitter_favourite_tweet(struct im_connection *ic, guint64 id) |
---|
1744 | { |
---|
1745 | char *args[2] = { |
---|
1746 | "id", |
---|
1747 | NULL, |
---|
1748 | }; |
---|
1749 | |
---|
1750 | args[1] = g_strdup_printf("%" G_GUINT64_FORMAT, id); |
---|
1751 | twitter_http_f(ic, TWITTER_FAVORITE_CREATE_URL, twitter_http_post, |
---|
1752 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
1753 | g_free(args[1]); |
---|
1754 | } |
---|
1755 | |
---|
1756 | static void twitter_http_status_show_url(struct http_request *req) |
---|
1757 | { |
---|
1758 | struct im_connection *ic = req->data; |
---|
1759 | json_value *parsed, *id; |
---|
1760 | const char *name; |
---|
1761 | |
---|
1762 | // Check if the connection is still active. |
---|
1763 | if (!g_slist_find(twitter_connections, ic)) { |
---|
1764 | return; |
---|
1765 | } |
---|
1766 | |
---|
1767 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
1768 | return; |
---|
1769 | } |
---|
1770 | |
---|
1771 | /* for the parson branch: |
---|
1772 | name = json_object_dotget_string(json_object(parsed), "user.screen_name"); |
---|
1773 | id = json_object_get_integer(json_object(parsed), "id"); |
---|
1774 | */ |
---|
1775 | |
---|
1776 | name = json_o_str(json_o_get(parsed, "user"), "screen_name"); |
---|
1777 | id = json_o_get(parsed, "id"); |
---|
1778 | |
---|
1779 | if (name && id && id->type == json_integer) { |
---|
1780 | twitter_log(ic, "https://twitter.com/%s/status/%" G_GUINT64_FORMAT, name, id->u.integer); |
---|
1781 | } else { |
---|
1782 | twitter_log(ic, "Error: could not fetch tweet url."); |
---|
1783 | } |
---|
1784 | |
---|
1785 | json_value_free(parsed); |
---|
1786 | } |
---|
1787 | |
---|
1788 | void twitter_status_show_url(struct im_connection *ic, guint64 id) |
---|
1789 | { |
---|
1790 | char *url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", TWITTER_STATUS_SHOW_URL, id, ".json"); |
---|
1791 | twitter_http(ic, url, twitter_http_status_show_url, ic, 0, NULL, 0); |
---|
1792 | g_free(url); |
---|
1793 | } |
---|