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