source: protocols/twitter/twitter_lib.c @ 2cd8540

Last change on this file since 2cd8540 was 2cd8540, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-25T12:05:48Z

Show DMs the right way. With mode=many and show_ids=off they'll look just
like tweets but that's what you get with bad settings.. Also, don't show
DMs from ourselves.

  • Property mode set to 100644
File size: 34.8 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>            *
[2a6da96]7*  Copyright 2010-2012 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
[e4e0b37]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
[1b221e0]50#define TXL_STATUS 1
[62d2cfb]51#define TXL_USER 2
52#define TXL_ID 3
53
[1b221e0]54struct twitter_xml_list {
[62d2cfb]55        int type;
[8203da9]56        gint64 next_cursor;
[1b221e0]57        GSList *list;
58};
59
60struct twitter_xml_user {
61        char *name;
62        char *screen_name;
63};
64
65struct twitter_xml_status {
[08579a1]66        time_t created_at;
[1b221e0]67        char *text;
68        struct twitter_xml_user *user;
[ce81acd]69        guint64 id, reply_to;
[1b221e0]70};
71
[d6aa6dd]72static void twitter_groupchat_init(struct im_connection *ic);
73
[62d2cfb]74/**
75 * Frees a twitter_xml_user struct.
76 */
77static void txu_free(struct twitter_xml_user *txu)
78{
[a26af5c]79        if (txu == NULL)
80                return;
[2322a9f]81
[62d2cfb]82        g_free(txu->name);
83        g_free(txu->screen_name);
[2abceca]84        g_free(txu);
[62d2cfb]85}
86
87/**
88 * Frees a twitter_xml_status struct.
89 */
90static void txs_free(struct twitter_xml_status *txs)
91{
[2322a9f]92        if (txs == NULL)
93                return;
94
[62d2cfb]95        g_free(txs->text);
96        txu_free(txs->user);
[2abceca]97        g_free(txs);
[62d2cfb]98}
99
100/**
101 * Free a twitter_xml_list struct.
102 * type is the type of list the struct holds.
103 */
104static void txl_free(struct twitter_xml_list *txl)
105{
106        GSList *l;
[a26af5c]107        if (txl == NULL)
108                return;
[2322a9f]109
110        for (l = txl->list; l; l = g_slist_next(l)) {
111                if (txl->type == TXL_STATUS) {
[5983eca]112                        txs_free((struct twitter_xml_status *) l->data);
[2322a9f]113                } else if (txl->type == TXL_ID) {
[62d2cfb]114                        g_free(l->data);
[2322a9f]115                } else if (txl->type == TXL_USER) {
[de923d5]116                        txu_free(l->data);
[2322a9f]117                }
118        }
119
[62d2cfb]120        g_slist_free(txl->list);
[fd65edb]121        g_free(txl);
[62d2cfb]122}
123
124/**
[2322a9f]125 * Compare status elements
126 */
127static 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.
[62d2cfb]143 */
[3e69802]144static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname)
[62d2cfb]145{
[1014cab]146        struct twitter_data *td = ic->proto_data;
147
[de923d5]148        // Check if the buddy is already in the buddy list.
[5983eca]149        if (!bee_user_by_handle(ic->bee, ic, name)) {
[e88fbe27]150                char *mode = set_getstr(&ic->acc->set, "mode");
[5983eca]151
[62d2cfb]152                // The buddy is not in the list, add the buddy and set the status to logged in.
[5983eca]153                imcb_add_buddy(ic, name, NULL);
154                imcb_rename_buddy(ic, name, fullname);
155                if (g_strcasecmp(mode, "chat") == 0) {
[5c18a76]156                        /* Necessary so that nicks always get translated to the
157                           exact Twitter username. */
[5983eca]158                        imcb_buddy_nick_hint(ic, name, name);
[2322a9f]159                        imcb_chat_add_buddy(td->timeline_gc, name);
[5983eca]160                } else if (g_strcasecmp(mode, "many") == 0)
161                        imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL);
[62d2cfb]162        }
163}
[1b221e0]164
[a7b9ec7]165/* Warning: May return a malloc()ed value, which will be free()d on the next
[de923d5]166   call. Only for short-term use. NOT THREADSAFE!  */
[6eca2eb]167char *twitter_parse_error(struct http_request *req)
[a7b9ec7]168{
169        static char *ret = NULL;
[5246133]170        json_value *root, *err;
[5983eca]171
[a7b9ec7]172        g_free(ret);
173        ret = NULL;
[5983eca]174
175        if (req->body_size > 0) {
[5246133]176                root = json_parse(req->reply_body);
177                err = json_o_get(root, "errors");
[dff0e0b]178                if (err && err->type == json_array && (err = err->u.array.values[0]) &&
[5246133]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);
[a7b9ec7]185        }
[5983eca]186
[6eca2eb]187        return ret ? ret : req->status_string;
[a7b9ec7]188}
189
[fb351ce]190/* WATCH OUT: This function might or might not destroy your connection.
191   Sub-optimal indeed, but just be careful when this returns NULL! */
[e08ae0c]192static json_value *twitter_parse_response(struct im_connection *ic, struct http_request *req)
[2a6da96]193{
194        gboolean logging_in = !(ic->flags & OPT_LOGGED_IN);
195        gboolean periodic;
196        struct twitter_data *td = ic->proto_data;
[e08ae0c]197        json_value *ret;
[2a6da96]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. */
[5246133]215                imcb_error(ic, "Authentication failure (%s)",
216                               twitter_parse_error(req));
[2a6da96]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
[e08ae0c]232        if ((ret = json_parse(req->reply_body)) == NULL) {
[2a6da96]233                imcb_error(ic, "Could not retrieve %s: %s",
234                           path, "XML parse error");
235        }
236        return ret;
237}
238
[1b221e0]239static void twitter_http_get_friends_ids(struct http_request *req);
240
241/**
242 * Get the friends ids.
243 */
[8203da9]244void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor)
[1b221e0]245{
[5983eca]246        // Primitive, but hey! It works...     
247        char *args[2];
[1b221e0]248        args[0] = "cursor";
[5983eca]249        args[1] = g_strdup_printf("%lld", (long long) next_cursor);
[bb5ce4d1]250        twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2);
[1b221e0]251
252        g_free(args[1]);
253}
254
255/**
256 * Fill a list of ids.
257 */
[9e8c945]258static gboolean twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl)
[1b221e0]259{
[e08ae0c]260        json_value *c;
261        int i;
[5983eca]262
[62d2cfb]263        // Set the list type.
264        txl->type = TXL_ID;
[1b221e0]265
[e08ae0c]266        c = json_o_get(node, "ids");
267        if (!c || c->type != json_array)
[9e8c945]268                return FALSE;
[1b221e0]269
[e08ae0c]270        for (i = 0; i < c->u.array.length; i ++) {
[0688e99]271                if (c->u.array.values[i]->type != json_integer)
272                        continue;
273               
[e08ae0c]274                txl->list = g_slist_prepend(txl->list,
[8e3b7ac]275                        g_strdup_printf("%lld", c->u.array.values[i]->u.integer));
[e08ae0c]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       
[9e8c945]285        return TRUE;
[1b221e0]286}
287
[de923d5]288static void twitter_get_users_lookup(struct im_connection *ic);
289
[1b221e0]290/**
291 * Callback for getting the friends ids.
292 */
293static void twitter_http_get_friends_ids(struct http_request *req)
294{
295        struct im_connection *ic;
[e08ae0c]296        json_value *parsed;
[1b221e0]297        struct twitter_xml_list *txl;
[3bd4a93]298        struct twitter_data *td;
[1b221e0]299
300        ic = req->data;
301
[62d2cfb]302        // Check if the connection is still active.
[5983eca]303        if (!g_slist_find(twitter_connections, ic))
[62d2cfb]304                return;
[5983eca]305
[37aa317]306        td = ic->proto_data;
[62d2cfb]307
[de923d5]308        /* Create the room now that we "logged in". */
[2322a9f]309        if (!td->timeline_gc && g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0)
[de923d5]310                twitter_groupchat_init(ic);
311
[1b221e0]312        txl = g_new0(struct twitter_xml_list, 1);
[de923d5]313        txl->list = td->follow_ids;
[1b221e0]314
315        // Parse the data.
[2a6da96]316        if (!(parsed = twitter_parse_response(ic, req)))
317                return;
[e08ae0c]318       
[d0752e8]319        twitter_xt_get_friends_id_list(parsed, txl);
[e08ae0c]320        json_value_free(parsed);
[1b221e0]321
[de923d5]322        td->follow_ids = txl->list;
[1b221e0]323        if (txl->next_cursor)
[de923d5]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/ */
[1b221e0]326                twitter_get_friends_ids(ic, txl->next_cursor);
[de923d5]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
[9e8c945]335static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl);
[de923d5]336static void twitter_http_get_users_lookup(struct http_request *req);
337
338static 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 */
373static void twitter_http_get_users_lookup(struct http_request *req)
374{
375        struct im_connection *ic = req->data;
[e08ae0c]376        json_value *parsed;
[de923d5]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;
[1b221e0]387
[de923d5]388        // Get the user list from the parsed xml feed.
[2a6da96]389        if (!(parsed = twitter_parse_response(ic, req)))
390                return;
[d0752e8]391        twitter_xt_get_users(parsed, txl);
[e08ae0c]392        json_value_free(parsed);
[de923d5]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.
[62d2cfb]401        txl_free(txl);
[de923d5]402
403        twitter_get_users_lookup(ic);
[1b221e0]404}
405
[8e3b7ac]406struct 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
[62d2cfb]417/**
418 * Function to fill a twitter_xml_list struct.
419 * It sets:
420 *  - all <user>s from the <users> element.
421 */
[9e8c945]422static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl)
[62d2cfb]423{
424        struct twitter_xml_user *txu;
[e08ae0c]425        int i;
[62d2cfb]426
427        // Set the type of the list.
428        txl->type = TXL_USER;
429
[e08ae0c]430        if (!node || node->type != json_array)
[9e8c945]431                return FALSE;
[e08ae0c]432
[62d2cfb]433        // The root <users> node should hold the list of users <user>
434        // Walk over the nodes children.
[e08ae0c]435        for (i = 0; i < node->u.array.length; i ++) {
[8e3b7ac]436                txu = twitter_xt_get_user(node->u.array.values[i]);
437                if (txu)
438                        txl->list = g_slist_prepend(txl->list, txu);
[62d2cfb]439        }
440
[9e8c945]441        return TRUE;
[62d2cfb]442}
443
[2b02617]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
[62d2cfb]449
[d1356cb]450static char* expand_entities(char* text, const json_value *entities);
451
[1b221e0]452/**
453 * Function to fill a twitter_xml_status struct.
454 * It sets:
455 *  - the status text and
456 *  - the created_at timestamp and
457 *  - the status id and
458 *  - the user in a twitter_xml_user struct.
459 */
[c9b5817]460static struct twitter_xml_status *twitter_xt_get_status(const json_value *node)
[1b221e0]461{
[c9b5817]462        struct twitter_xml_status *txs;
[fb351ce]463        const json_value *rt = NULL, *entities = NULL;
[8e3b7ac]464       
465        if (node->type != json_object)
[9e8c945]466                return FALSE;
[c9b5817]467        txs = g_new0(struct twitter_xml_status, 1);
[1b221e0]468
[9e8c945]469        JSON_O_FOREACH (node, k, v) {
[8e3b7ac]470                if (strcmp("text", k) == 0 && v->type == json_string) {
[fb351ce]471                        txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1);
[8e3b7ac]472                } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) {
473                        rt = v;
474                } else if (strcmp("created_at", k) == 0 && v->type == json_string) {
[08579a1]475                        struct tm parsed;
[5983eca]476
[08579a1]477                        /* Very sensitive to changes to the formatting of
478                           this field. :-( Also assumes the timezone used
479                           is UTC since C time handling functions suck. */
[8e3b7ac]480                        if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL)
[5983eca]481                                txs->created_at = mktime_utc(&parsed);
[8e3b7ac]482                } else if (strcmp("user", k) == 0 && v->type == json_object) {
483                        txs->user = twitter_xt_get_user(v);
484                } else if (strcmp("id", k) == 0 && v->type == json_integer) {
485                        txs->id = v->u.integer;
486                } else if (strcmp("in_reply_to_status_id", k) == 0 && v->type == json_integer) {
487                        txs->reply_to = v->u.integer;
488                } else if (strcmp("entities", k) == 0 && v->type == json_object) {
[fb351ce]489                        entities = v;
[ce81acd]490                }
[1b221e0]491        }
[5983eca]492
[0fff0b2]493        /* If it's a (truncated) retweet, get the original. Even if the API claims it
494           wasn't truncated because it may be lying. */
495        if (rt) {
[c9b5817]496                struct twitter_xml_status *rtxs = twitter_xt_get_status(rt);
497                if (rtxs) {
498                        g_free(txs->text);
499                        txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text);
500                        txs->id = rtxs->id;
[e193aeb]501                        txs_free(rtxs);
502                }
[fb351ce]503        } else if (entities) {
[d1356cb]504                txs->text = expand_entities(txs->text, entities);
505        }
506
[c9b5817]507        if (txs->text && txs->user && txs->id)
508                return txs;
509       
510        txs_free(txs);
511        return NULL;
[d1356cb]512}
513
514/**
515 * Function to fill a twitter_xml_status struct (DM variant).
516 */
[2cd8540]517static struct twitter_xml_status *twitter_xt_get_dm(const json_value *node)
[d1356cb]518{
[2cd8540]519        struct twitter_xml_status *txs;
[d1356cb]520        const json_value *entities = NULL;
521       
522        if (node->type != json_object)
523                return FALSE;
[2cd8540]524        txs = g_new0(struct twitter_xml_status, 1);
[d1356cb]525
526        JSON_O_FOREACH (node, k, v) {
527                if (strcmp("text", k) == 0 && v->type == json_string) {
528                        txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1);
529                } else if (strcmp("created_at", k) == 0 && v->type == json_string) {
530                        struct tm parsed;
531
532                        /* Very sensitive to changes to the formatting of
533                           this field. :-( Also assumes the timezone used
534                           is UTC since C time handling functions suck. */
535                        if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL)
536                                txs->created_at = mktime_utc(&parsed);
537                } else if (strcmp("sender", k) == 0 && v->type == json_object) {
538                        txs->user = twitter_xt_get_user(v);
539                } else if (strcmp("id", k) == 0 && v->type == json_integer) {
540                        txs->id = v->u.integer;
541                }
542        }
543
544        if (entities) {
545                txs->text = expand_entities(txs->text, entities);
546        }
547
[2cd8540]548        if (txs->text && txs->user && txs->id)
549                return txs;
550       
551        txs_free(txs);
552        return NULL;
[d1356cb]553}
554
555static char* expand_entities(char* text, const json_value *entities) {
556        JSON_O_FOREACH (entities, k, v) {
557                int i;
558               
559                if (v->type != json_array)
560                        continue;
561                if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0)
562                        continue;
563               
564                for (i = 0; i < v->u.array.length; i ++) {
565                        if (v->u.array.values[i]->type != json_object)
[8e3b7ac]566                                continue;
[d1356cb]567                       
568                        const char *kort = json_o_str(v->u.array.values[i], "url");
569                        const char *disp = json_o_str(v->u.array.values[i], "display_url");
570                        char *pos, *new;
571                       
572                        if (!kort || !disp || !(pos = strstr(text, kort)))
[429a9b1]573                                continue;
574                       
[d1356cb]575                        *pos = '\0';
576                        new = g_strdup_printf("%s%s &lt;%s&gt;%s", text, kort,
577                                              disp, pos + strlen(kort));
578                       
579                        g_free(text);
580                        text = new;
[429a9b1]581                }
[e193aeb]582        }
[d1356cb]583       
584        return text;
[1b221e0]585}
586
587/**
588 * Function to fill a twitter_xml_list struct.
589 * It sets:
590 *  - all <status>es within the <status> element and
591 *  - the next_cursor.
592 */
[9e8c945]593static gboolean twitter_xt_get_status_list(struct im_connection *ic, const json_value *node,
594                                           struct twitter_xml_list *txl)
[1b221e0]595{
596        struct twitter_xml_status *txs;
[8e3b7ac]597        int i;
[1b221e0]598
[62d2cfb]599        // Set the type of the list.
600        txl->type = TXL_STATUS;
[8e3b7ac]601       
602        if (node->type != json_array)
[9e8c945]603                return FALSE;
[62d2cfb]604
[1b221e0]605        // The root <statuses> node should hold the list of statuses <status>
606        // Walk over the nodes children.
[8e3b7ac]607        for (i = 0; i < node->u.array.length; i ++) {
[c9b5817]608                txs = twitter_xt_get_status(node->u.array.values[i]);
609                if (!txs)
610                        continue;
611               
[8e3b7ac]612                txl->list = g_slist_prepend(txl->list, txs);
[1b221e0]613        }
614
[9e8c945]615        return TRUE;
[1b221e0]616}
617
[c9b5817]618/* Will log messages either way. Need to keep track of IDs for stream deduping.
619   Plus, show_ids is on by default and I don't see why anyone would disable it. */
[ce81acd]620static char *twitter_msg_add_id(struct im_connection *ic,
[5983eca]621                                struct twitter_xml_status *txs, const char *prefix)
[ce81acd]622{
623        struct twitter_data *td = ic->proto_data;
[c9b5817]624        int reply_to = -1;
625        bee_user_t *bu;
[5983eca]626
627        if (txs->reply_to) {
[ce81acd]628                int i;
[5983eca]629                for (i = 0; i < TWITTER_LOG_LENGTH; i++)
630                        if (td->log[i].id == txs->reply_to) {
[c9b5817]631                                reply_to = i;
[ce81acd]632                                break;
633                        }
634        }
[5983eca]635
[c9b5817]636        if (txs->user && txs->user->screen_name &&
637            (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) {
638                struct twitter_user_data *tud = bu->data;
639
640                if (txs->id > tud->last_id) {
641                        tud->last_id = txs->id;
642                        tud->last_time = txs->created_at;
643                }
644        }
645       
646        td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH;
647        td->log[td->log_id].id = txs->id;
648        td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name);
649       
650        if (set_getbool(&ic->acc->set, "show_ids")) {
651                if (reply_to != -1)
652                        return g_strdup_printf("\002[\002%02d->%02d\002]\002 %s%s",
653                                               td->log_id, reply_to, prefix, txs->text);
654                else
655                        return g_strdup_printf("\002[\002%02d\002]\002 %s%s",
656                                               td->log_id, prefix, txs->text);
657        } else {
658                if (*prefix)
659                        return g_strconcat(prefix, txs->text, NULL);
660                else
661                        return NULL;
662        }
[ce81acd]663}
664
[d6aa6dd]665static void twitter_groupchat_init(struct im_connection *ic)
666{
667        char *name_hint;
668        struct groupchat *gc;
669        struct twitter_data *td = ic->proto_data;
[fd65edb]670        GSList *l;
[5983eca]671
[2322a9f]672        td->timeline_gc = gc = imcb_chat_new(ic, "twitter/timeline");
[5983eca]673
674        name_hint = g_strdup_printf("%s_%s", td->prefix, ic->acc->user);
675        imcb_chat_name_hint(gc, name_hint);
676        g_free(name_hint);
677
678        for (l = ic->bee->users; l; l = l->next) {
[fd65edb]679                bee_user_t *bu = l->data;
[5983eca]680                if (bu->ic == ic)
[2322a9f]681                        imcb_chat_add_buddy(td->timeline_gc, bu->handle);
[fd65edb]682        }
[d6aa6dd]683}
684
[62d2cfb]685/**
686 * Function that is called to see the statuses in a groupchat window.
687 */
[5983eca]688static void twitter_groupchat(struct im_connection *ic, GSList * list)
[62d2cfb]689{
690        struct twitter_data *td = ic->proto_data;
691        GSList *l = NULL;
692        struct twitter_xml_status *status;
693        struct groupchat *gc;
[2322a9f]694        guint64 last_id = 0;
[62d2cfb]695
696        // Create a new groupchat if it does not exsist.
[2322a9f]697        if (!td->timeline_gc)
[d6aa6dd]698                twitter_groupchat_init(ic);
[5983eca]699
[2322a9f]700        gc = td->timeline_gc;
[d6aa6dd]701        if (!gc->joined)
[5983eca]702                imcb_chat_add_buddy(gc, ic->acc->user);
[62d2cfb]703
[5983eca]704        for (l = list; l; l = g_slist_next(l)) {
[ce81acd]705                char *msg;
[5983eca]706
[62d2cfb]707                status = l->data;
[2322a9f]708                if (status->user == NULL || status->text == NULL || last_id == status->id)
[a26af5c]709                        continue;
710
[2322a9f]711                last_id = status->id;
[5983eca]712
[0b3ffb1]713                strip_html(status->text);
[2322a9f]714
[948abab]715                if (set_getbool(&ic->acc->set, "strip_newlines"))
[cf0dbdd]716                        strip_newlines(status->text);
[948abab]717
[ce81acd]718                msg = twitter_msg_add_id(ic, status, "");
[5983eca]719
[62d2cfb]720                // Say it!
[2322a9f]721                if (g_strcasecmp(td->user, status->user->screen_name) == 0) {
[ce81acd]722                        imcb_chat_log(gc, "You: %s", msg ? msg : status->text);
[2322a9f]723                } else {
724                        twitter_add_buddy(ic, status->user->screen_name, status->user->name);
725
[ce81acd]726                        imcb_chat_msg(gc, status->user->screen_name,
[5983eca]727                                      msg ? msg : status->text, 0, status->created_at);
[2322a9f]728                }
[5983eca]729
[ce81acd]730                g_free(msg);
[5983eca]731
[2322a9f]732                // Update the timeline_id to hold the highest id, so that by the next request
[ce81acd]733                // we won't pick up the updates already in the list.
[2322a9f]734                td->timeline_id = MAX(td->timeline_id, status->id);
[62d2cfb]735        }
736}
737
738/**
739 * Function that is called to see statuses as private messages.
740 */
[5983eca]741static void twitter_private_message_chat(struct im_connection *ic, GSList * list)
[62d2cfb]742{
743        struct twitter_data *td = ic->proto_data;
744        GSList *l = NULL;
745        struct twitter_xml_status *status;
[e88fbe27]746        char from[MAX_STRING];
747        gboolean mode_one;
[2322a9f]748        guint64 last_id = 0;
[62d2cfb]749
[5983eca]750        mode_one = g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") == 0;
751
752        if (mode_one) {
753                g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user);
754                from[MAX_STRING - 1] = '\0';
[e88fbe27]755        }
[5983eca]756
757        for (l = list; l; l = g_slist_next(l)) {
[ce81acd]758                char *prefix = NULL, *text = NULL;
[5983eca]759
[62d2cfb]760                status = l->data;
[2322a9f]761                if (status->user == NULL || status->text == NULL || last_id == status->id)
762                        continue;
763
764                last_id = status->id;
[5983eca]765
766                strip_html(status->text);
767                if (mode_one)
[ce81acd]768                        prefix = g_strdup_printf("\002<\002%s\002>\002 ",
[5983eca]769                                                 status->user->screen_name);
[55b1e69]770                else
771                        twitter_add_buddy(ic, status->user->screen_name, status->user->name);
[5983eca]772
[ce81acd]773                text = twitter_msg_add_id(ic, status, prefix ? prefix : "");
[5983eca]774
775                imcb_buddy_msg(ic,
776                               mode_one ? from : status->user->screen_name,
777                               text ? text : status->text, 0, status->created_at);
778
[2322a9f]779                // Update the timeline_id to hold the highest id, so that by the next request
[ce81acd]780                // we won't pick up the updates already in the list.
[2322a9f]781                td->timeline_id = MAX(td->timeline_id, status->id);
[5983eca]782
783                g_free(text);
784                g_free(prefix);
[62d2cfb]785        }
786}
787
[dff0e0b]788static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o);
[ddc2de5]789
790static void twitter_http_stream(struct http_request *req)
791{
[dff0e0b]792        struct im_connection *ic = req->data;
[dd672e2]793        struct twitter_data *td;
[dff0e0b]794        json_value *parsed;
[62f6b45]795        int len = 0;
796        char c, *nl;
[dff0e0b]797       
798        if (!g_slist_find(twitter_connections, ic))
799                return;
[ddc2de5]800       
[e132b60]801        ic->flags |= OPT_PONGED;
[dd672e2]802        td = ic->proto_data;
803       
804        if ((req->flags & HTTPC_EOF) || !req->reply_body) {
805                td->stream = NULL;
806                imcb_error(ic, "Stream closed (%s)", req->status_string);
807                imc_logout(ic, TRUE);
808                return;
809        }
810       
[ddc2de5]811        printf( "%d bytes in stream\n", req->body_size );
812       
[62f6b45]813        /* MUST search for CRLF, not just LF:
814           https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */
815        nl = strstr(req->reply_body, "\r\n");
[ddc2de5]816       
[62f6b45]817        if (!nl) {
818                printf("Incomplete data\n");
[ddc2de5]819                return;
820        }
821       
[62f6b45]822        len = nl - req->reply_body;
823        if (len > 0) {
824                c = req->reply_body[len];
825                req->reply_body[len] = '\0';
826               
827                printf("JSON: %s\n", req->reply_body);
828                printf("parsed: %p\n", (parsed = json_parse(req->reply_body)));
829                if (parsed) {
830                        twitter_stream_handle_object(ic, parsed);
831                }
832                json_value_free(parsed);
833                req->reply_body[len] = c;
[dff0e0b]834        }
[ddc2de5]835       
[62f6b45]836        http_flush_bytes(req, len + 2);
[dff0e0b]837       
838        /* One notification might bring multiple events! */
839        if (req->body_size > 0)
840                twitter_http_stream(req);
[ddc2de5]841}
[2322a9f]842
[5aa96fc8]843static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o);
[c9b5817]844static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs);
[5aa96fc8]845
[dff0e0b]846static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o)
847{
[5aa96fc8]848        struct twitter_data *td = ic->proto_data;
[c9b5817]849        struct twitter_xml_status *txs;
[d1356cb]850        json_value *c;
[dff0e0b]851       
[c9b5817]852        if ((txs = twitter_xt_get_status(o))) {
853                return twitter_stream_handle_status(ic, txs);
[d1356cb]854        } else if ((c = json_o_get(o, "direct_message")) &&
[2cd8540]855                   (txs = twitter_xt_get_dm(c))) {
856                if (strcmp(txs->user->screen_name, td->user) != 0)
857                        imcb_buddy_msg(ic, txs->user->screen_name,
858                                       txs->text, 0, txs->created_at);
[d1356cb]859                txs_free(txs);
860                return TRUE;
[5aa96fc8]861        } else if ((c = json_o_get(o, "event")) && c->type == json_string) {
862                twitter_stream_handle_event(ic, o);
863                return TRUE;
864        } else if ((c = json_o_get(o, "disconnect")) && c->type == json_object) {
865                /* HACK: Because we're inside an event handler, we can't just
866                   disconnect here. Instead, just change the HTTP status string
867                   into a Twitter status string. */
868                char *reason = json_o_strdup(c, "reason");
869                if (reason) {
870                        g_free(td->stream->status_string);
871                        td->stream->status_string = reason;
872                }
873                return TRUE;
[dff0e0b]874        }
875        return FALSE;
876}
877
[c9b5817]878static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs)
879{
880        struct twitter_data *td = ic->proto_data;
881        int i;
882       
883        for (i = 0; i < TWITTER_LOG_LENGTH; i++) {
884                if (td->log[i].id == txs->id) {
885                        /* Got a duplicate (RT, surely). Drop it. */
886                        txs_free(txs);
887                        return TRUE;
888                }
889        }
890       
891        if (!(set_getbool(&ic->acc->set, "fetch_mentions") ||
892              bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) {
893                /* Tweet is from an unknown person and the user does not want
894                   to see @mentions, so drop it. twitter_stream_handle_event()
895                   picks up new follows so this simple filter should be safe. */
896                /* TODO: The streaming API seems to do poor @mention matching.
897                   I.e. I'm getting mentions for @WilmerSomething, not just for
898                   @Wilmer. But meh. You want spam, you get spam. */
899                return TRUE;
900        }
901       
902        GSList *output = g_slist_append(NULL, txs);
903        twitter_groupchat(ic, output);
904        txs_free(txs);
905        g_slist_free(output);
906        return TRUE;
907}
908
[5aa96fc8]909static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o)
910{
911        struct twitter_data *td = ic->proto_data;
912        json_value *source = json_o_get(o, "source");
913        json_value *target = json_o_get(o, "target");
914        const char *type = json_o_str(o, "event");
915       
916        if (!type || !source || source->type != json_object
917                  || !target || target->type != json_object) {
918                return FALSE;
919        }
920       
921        if (strcmp(type, "follow") == 0) {
922                struct twitter_xml_user *us = twitter_xt_get_user(source);
923                struct twitter_xml_user *ut = twitter_xt_get_user(target);
924                if (strcmp(us->screen_name, td->user) == 0) {
925                        twitter_add_buddy(ic, ut->screen_name, ut->name);
926                }
927                txu_free(us);
928                txu_free(ut);
929        }
930       
931        return TRUE;
932}
933
[dff0e0b]934gboolean twitter_open_stream(struct im_connection *ic)
935{
936        struct twitter_data *td = ic->proto_data;
[62f6b45]937        char *args[2] = {"with", "followings"};
[dff0e0b]938       
939        if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL,
[62f6b45]940                                       twitter_http_stream, ic, 0, args, 2))) {
[dff0e0b]941                /* This flag must be enabled or we'll get no data until EOF
942                   (which err, kind of, defeats the purpose of a streaming API). */
943                td->stream->flags |= HTTPC_STREAMING;
944                return TRUE;
945        }
946       
947        return FALSE;
948}
949
950static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor);
951static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor);
952
[2322a9f]953/**
954 * Get the timeline with optionally mentions
955 */
956void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor)
957{
958        struct twitter_data *td = ic->proto_data;
959        gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions");
960
961        if (td->flags & TWITTER_DOING_TIMELINE) {
[11ec078]962                if (++td->http_fails >= 5) {
963                        imcb_error(ic, "Fetch timeout (%d)", td->flags);
964                        imc_logout(ic, TRUE);
965                }
[2322a9f]966        }
967
968        td->flags |= TWITTER_DOING_TIMELINE;
969
970        twitter_get_home_timeline(ic, next_cursor);
971
972        if (include_mentions) {
973                twitter_get_mentions(ic, next_cursor);
974        }
975}
976
977/**
978 * Call this one after receiving timeline/mentions. Show to user once we have
979 * both.
980 */
981void twitter_flush_timeline(struct im_connection *ic)
982{
983        struct twitter_data *td = ic->proto_data;
984        gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions");
[b5fe39b]985        int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions");
[2322a9f]986        struct twitter_xml_list *home_timeline = td->home_timeline_obj;
987        struct twitter_xml_list *mentions = td->mentions_obj;
988        GSList *output = NULL;
989        GSList *l;
990
991        if (!(td->flags & TWITTER_GOT_TIMELINE)) {
992                return;
993        }
994
995        if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) {
996                return;
997        }
998
999        if (home_timeline && home_timeline->list) {
1000                for (l = home_timeline->list; l; l = g_slist_next(l)) {
1001                        output = g_slist_insert_sorted(output, l->data, twitter_compare_elements);
1002                }
1003        }
1004
1005        if (include_mentions && mentions && mentions->list) {
1006                for (l = mentions->list; l; l = g_slist_next(l)) {
[b5fe39b]1007                        if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) {
[2322a9f]1008                                continue;
1009                        }
1010
1011                        output = g_slist_insert_sorted(output, l->data, twitter_compare_elements);
1012                }
1013        }
[2a6da96]1014       
1015        if (!(ic->flags & OPT_LOGGED_IN))
1016                imcb_connected(ic);
[2322a9f]1017
1018        // See if the user wants to see the messages in a groupchat window or as private messages.
1019        if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0)
1020                twitter_groupchat(ic, output);
1021        else
1022                twitter_private_message_chat(ic, output);
1023
1024        g_slist_free(output);
1025
[199fea6]1026        txl_free(home_timeline);
1027        txl_free(mentions);
[2322a9f]1028
1029        td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS);
[199fea6]1030        td->home_timeline_obj = td->mentions_obj = NULL;
[2322a9f]1031}
1032
[ddc2de5]1033static void twitter_http_get_home_timeline(struct http_request *req);
1034static void twitter_http_get_mentions(struct http_request *req);
1035
[2322a9f]1036/**
1037 * Get the timeline.
1038 */
[ddc2de5]1039static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor)
[2322a9f]1040{
1041        struct twitter_data *td = ic->proto_data;
1042
[199fea6]1043        txl_free(td->home_timeline_obj);
[2322a9f]1044        td->home_timeline_obj = NULL;
1045        td->flags &= ~TWITTER_GOT_TIMELINE;
1046
[429a9b1]1047        char *args[6];
[2322a9f]1048        args[0] = "cursor";
1049        args[1] = g_strdup_printf("%lld", (long long) next_cursor);
[429a9b1]1050        args[2] = "include_entities";
1051        args[3] = "true";
[2322a9f]1052        if (td->timeline_id) {
[429a9b1]1053                args[4] = "since_id";
1054                args[5] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id);
[2322a9f]1055        }
1056
[90fc864]1057        if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args,
1058                     td->timeline_id ? 6 : 4) == NULL) {
1059                if (++td->http_fails >= 5)
1060                        imcb_error(ic, "Could not retrieve %s: %s",
1061                                   TWITTER_HOME_TIMELINE_URL, "connection failed");
1062                td->flags |= TWITTER_GOT_TIMELINE;
1063                twitter_flush_timeline(ic);
1064        }
[2322a9f]1065
1066        g_free(args[1]);
1067        if (td->timeline_id) {
[429a9b1]1068                g_free(args[5]);
[2322a9f]1069        }
1070}
1071
1072/**
1073 * Get mentions.
1074 */
[ddc2de5]1075static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor)
[2322a9f]1076{
1077        struct twitter_data *td = ic->proto_data;
1078
[199fea6]1079        txl_free(td->mentions_obj);
[2322a9f]1080        td->mentions_obj = NULL;
1081        td->flags &= ~TWITTER_GOT_MENTIONS;
1082
[429a9b1]1083        char *args[6];
[2322a9f]1084        args[0] = "cursor";
1085        args[1] = g_strdup_printf("%lld", (long long) next_cursor);
[429a9b1]1086        args[2] = "include_entities";
1087        args[3] = "true";
[2322a9f]1088        if (td->timeline_id) {
[429a9b1]1089                args[4] = "since_id";
1090                args[5] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id);
[b5fe39b]1091        } else {
1092                args[4] = "count";
1093                args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions"));
[2322a9f]1094        }
1095
[b5fe39b]1096        if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions,
1097                         ic, 0, args, 6) == NULL) {
[90fc864]1098                if (++td->http_fails >= 5)
1099                        imcb_error(ic, "Could not retrieve %s: %s",
1100                                   TWITTER_MENTIONS_URL, "connection failed");
1101                td->flags |= TWITTER_GOT_MENTIONS;
1102                twitter_flush_timeline(ic);
1103        }
[2322a9f]1104
1105        g_free(args[1]);
[2fb1262]1106        g_free(args[5]);
[2322a9f]1107}
1108
[1b221e0]1109/**
1110 * Callback for getting the home timeline.
1111 */
1112static void twitter_http_get_home_timeline(struct http_request *req)
1113{
[62d2cfb]1114        struct im_connection *ic = req->data;
[37aa317]1115        struct twitter_data *td;
[8e3b7ac]1116        json_value *parsed;
[1b221e0]1117        struct twitter_xml_list *txl;
[62d2cfb]1118
1119        // Check if the connection is still active.
[5983eca]1120        if (!g_slist_find(twitter_connections, ic))
[62d2cfb]1121                return;
[5983eca]1122
[37aa317]1123        td = ic->proto_data;
[1b221e0]1124
[2322a9f]1125        txl = g_new0(struct twitter_xml_list, 1);
1126        txl->list = NULL;
1127
1128        // The root <statuses> node should hold the list of statuses <status>
[2a6da96]1129        if (!(parsed = twitter_parse_response(ic, req)))
1130                goto end;
[d0752e8]1131        twitter_xt_get_status_list(ic, parsed, txl);
[2fb1262]1132        json_value_free(parsed);
[2322a9f]1133
1134        td->home_timeline_obj = txl;
1135
1136      end:
[fb351ce]1137        if (!g_slist_find(twitter_connections, ic))
1138                return;
1139
[2322a9f]1140        td->flags |= TWITTER_GOT_TIMELINE;
1141
1142        twitter_flush_timeline(ic);
1143}
1144
1145/**
1146 * Callback for getting mentions.
1147 */
1148static void twitter_http_get_mentions(struct http_request *req)
1149{
1150        struct im_connection *ic = req->data;
1151        struct twitter_data *td;
[8e3b7ac]1152        json_value *parsed;
[2322a9f]1153        struct twitter_xml_list *txl;
1154
1155        // Check if the connection is still active.
1156        if (!g_slist_find(twitter_connections, ic))
[1b221e0]1157                return;
[2322a9f]1158
1159        td = ic->proto_data;
1160
[1b221e0]1161        txl = g_new0(struct twitter_xml_list, 1);
1162        txl->list = NULL;
[62d2cfb]1163
[1b221e0]1164        // The root <statuses> node should hold the list of statuses <status>
[2a6da96]1165        if (!(parsed = twitter_parse_response(ic, req)))
1166                goto end;
[d0752e8]1167        twitter_xt_get_status_list(ic, parsed, txl);
[2fb1262]1168        json_value_free(parsed);
[1b221e0]1169
[2322a9f]1170        td->mentions_obj = txl;
[1b221e0]1171
[2322a9f]1172      end:
[fb351ce]1173        if (!g_slist_find(twitter_connections, ic))
1174                return;
1175
[2322a9f]1176        td->flags |= TWITTER_GOT_MENTIONS;
1177
1178        twitter_flush_timeline(ic);
[1b221e0]1179}
1180
1181/**
[de923d5]1182 * Callback to use after sending a POST request to twitter.
1183 * (Generic, used for a few kinds of queries.)
[1b221e0]1184 */
[7d53efb]1185static void twitter_http_post(struct http_request *req)
[1b221e0]1186{
1187        struct im_connection *ic = req->data;
[7b87539]1188        struct twitter_data *td;
[24132ec]1189        json_value *parsed, *id;
[1b221e0]1190
[62d2cfb]1191        // Check if the connection is still active.
[5983eca]1192        if (!g_slist_find(twitter_connections, ic))
[62d2cfb]1193                return;
1194
[7b87539]1195        td = ic->proto_data;
1196        td->last_status_id = 0;
[5983eca]1197
[2a6da96]1198        if (!(parsed = twitter_parse_response(ic, req)))
[1b221e0]1199                return;
[2a6da96]1200       
[24132ec]1201        if ((id = json_o_get(parsed, "id")) && id->type == json_integer)
1202                td->last_status_id = id->u.integer;
[c751e51]1203       
1204        json_value_free(parsed);
[1b221e0]1205}
1206
1207/**
1208 * Function to POST a new status to twitter.
[5983eca]1209 */
[b890626]1210void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to)
[1b221e0]1211{
[5983eca]1212        char *args[4] = {
[b890626]1213                "status", msg,
1214                "in_reply_to_status_id",
1215                g_strdup_printf("%llu", (unsigned long long) in_reply_to)
1216        };
1217        twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1,
[5983eca]1218                     args, in_reply_to ? 4 : 2);
[b890626]1219        g_free(args[3]);
[1b221e0]1220}
1221
1222
[62d2cfb]1223/**
1224 * Function to POST a new message to twitter.
1225 */
1226void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg)
1227{
[5983eca]1228        char *args[4];
[62d2cfb]1229        args[0] = "screen_name";
1230        args[1] = who;
1231        args[2] = "text";
1232        args[3] = msg;
1233        // Use the same callback as for twitter_post_status, since it does basically the same.
[ba3233c]1234        twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4);
[62d2cfb]1235}
[7d53efb]1236
1237void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create)
1238{
[5983eca]1239        char *args[2];
[7d53efb]1240        args[0] = "screen_name";
1241        args[1] = who;
[5983eca]1242        twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL,
1243                     twitter_http_post, ic, 1, args, 2);
[a26af5c]1244}
[7b87539]1245
1246void twitter_status_destroy(struct im_connection *ic, guint64 id)
1247{
1248        char *url;
[de923d5]1249        url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_DESTROY_URL,
[3b4a22a]1250                              (unsigned long long) id, ".json");
[7b87539]1251        twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0);
1252        g_free(url);
1253}
[b890626]1254
1255void twitter_status_retweet(struct im_connection *ic, guint64 id)
1256{
1257        char *url;
[de923d5]1258        url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_RETWEET_URL,
[3b4a22a]1259                              (unsigned long long) id, ".json");
[b890626]1260        twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0);
1261        g_free(url);
1262}
[d18dee42]1263
1264/**
1265 * Report a user for sending spam.
1266 */
1267void twitter_report_spam(struct im_connection *ic, char *screen_name)
1268{
1269        char *args[2] = {
1270                "screen_name",
1271                NULL,
1272        };
1273        args[1] = screen_name;
1274        twitter_http(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post,
1275                     ic, 1, args, 2);
1276}
[b61c74c]1277
1278/**
1279 * Favourite a tweet.
1280 */
1281void twitter_favourite_tweet(struct im_connection *ic, guint64 id)
1282{
1283        char *url;
1284        url = g_strdup_printf("%s%llu%s", TWITTER_FAVORITE_CREATE_URL,
[3b4a22a]1285                              (unsigned long long) id, ".json");
[b61c74c]1286        twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0);
1287        g_free(url);
1288}
Note: See TracBrowser for help on using the repository browser.