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