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