source: protocols/twitter/twitter_lib.c @ 0ca1d79

Last change on this file since 0ca1d79 was 0ca1d79, checked in by dequis <dx@…>, at 2015-02-20T22:47:33Z

Revert "Twitter format strings"

This reverts commit ce402b20d82ec323e6bd5e306de934773590742d.

See discussion in PR #10 for more details.

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