source: protocols/twitter/twitter_lib.c @ 537d9b9

Last change on this file since 537d9b9 was 537d9b9, checked in by dequis <dx@…>, at 2016-11-20T08:40:36Z

Merge master up to commit '9f03c47' into parson

  • Property mode set to 100644
File size: 47.0 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(): */
[5ebff60]26#if (__sun)
[daae10f]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"
[f81d8b8]38#include "parson.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
[f81d8b8]67#define JSON_O_FOREACH(o, k, v) \
68    const char *k; const JSON_Value *v; int __i; \
69    for (__i = 0; json_object_get_tuple(o, __i, &k, &v); __i++)
70
[62d2cfb]71/**
72 * Frees a twitter_xml_user struct.
73 */
74static void txu_free(struct twitter_xml_user *txu)
75{
[5ebff60]76        if (txu == NULL) {
[a26af5c]77                return;
[5ebff60]78        }
[2322a9f]79
[62d2cfb]80        g_free(txu->name);
81        g_free(txu->screen_name);
[2abceca]82        g_free(txu);
[62d2cfb]83}
84
85/**
86 * Frees a twitter_xml_status struct.
87 */
88static void txs_free(struct twitter_xml_status *txs)
89{
[5ebff60]90        if (txs == NULL) {
[2322a9f]91                return;
[5ebff60]92        }
[2322a9f]93
[62d2cfb]94        g_free(txs->text);
95        txu_free(txs->user);
[2abceca]96        g_free(txs);
[62d2cfb]97}
98
99/**
100 * Free a twitter_xml_list struct.
101 * type is the type of list the struct holds.
102 */
103static void txl_free(struct twitter_xml_list *txl)
104{
105        GSList *l;
[5ebff60]106
107        if (txl == NULL) {
[a26af5c]108                return;
[5ebff60]109        }
[2322a9f]110
111        for (l = txl->list; l; l = g_slist_next(l)) {
112                if (txl->type == TXL_STATUS) {
[5983eca]113                        txs_free((struct twitter_xml_status *) l->data);
[2322a9f]114                } else if (txl->type == TXL_ID) {
[62d2cfb]115                        g_free(l->data);
[2322a9f]116                } else if (txl->type == TXL_USER) {
[de923d5]117                        txu_free(l->data);
[2322a9f]118                }
119        }
120
[62d2cfb]121        g_slist_free(txl->list);
[fd65edb]122        g_free(txl);
[62d2cfb]123}
124
125/**
[2322a9f]126 * Compare status elements
127 */
128static gint twitter_compare_elements(gconstpointer a, gconstpointer b)
129{
130        struct twitter_xml_status *a_status = (struct twitter_xml_status *) a;
131        struct twitter_xml_status *b_status = (struct twitter_xml_status *) b;
132
133        if (a_status->created_at < b_status->created_at) {
134                return -1;
135        } else if (a_status->created_at > b_status->created_at) {
136                return 1;
137        } else {
138                return 0;
139        }
140}
141
142/**
143 * Add a buddy if it is not already added, set the status to logged in.
[62d2cfb]144 */
[3e69802]145static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname)
[62d2cfb]146{
[1014cab]147        struct twitter_data *td = ic->proto_data;
148
[de923d5]149        // Check if the buddy is already in the buddy list.
[5983eca]150        if (!bee_user_by_handle(ic->bee, ic, name)) {
[62d2cfb]151                // The buddy is not in the list, add the buddy and set the status to logged in.
[5983eca]152                imcb_add_buddy(ic, name, NULL);
153                imcb_rename_buddy(ic, name, fullname);
[631ec80]154                if (td->flags & TWITTER_MODE_CHAT) {
[5c18a76]155                        /* Necessary so that nicks always get translated to the
156                           exact Twitter username. */
[5983eca]157                        imcb_buddy_nick_hint(ic, name, name);
[5ebff60]158                        if (td->timeline_gc) {
[7f557d5]159                                imcb_chat_add_buddy(td->timeline_gc, name);
[5ebff60]160                        }
161                } else if (td->flags & TWITTER_MODE_MANY) {
[5983eca]162                        imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL);
[5ebff60]163                }
[62d2cfb]164        }
165}
[1b221e0]166
[a7b9ec7]167/* Warning: May return a malloc()ed value, which will be free()d on the next
[de923d5]168   call. Only for short-term use. NOT THREADSAFE!  */
[6eca2eb]169char *twitter_parse_error(struct http_request *req)
[a7b9ec7]170{
171        static char *ret = NULL;
[f81d8b8]172        JSON_Value *root, *err;
[5983eca]173
[a7b9ec7]174        g_free(ret);
175        ret = NULL;
[5983eca]176
177        if (req->body_size > 0) {
[f81d8b8]178                root = json_parse_string(req->reply_body);
179                err = json_object_get_value(json_object(root), "errors");
180                if (err && json_type(err) == JSONArray &&
181                    (err = json_array_get_value(json_array(err), 0)) &&
182                    json_type(err) == JSONObject) {
183                        const char *msg = json_object_get_string(json_object(err), "message");
[5ebff60]184                        if (msg) {
[5246133]185                                ret = g_strdup_printf("%s (%s)", req->status_string, msg);
[5ebff60]186                        }
[5246133]187                }
188                json_value_free(root);
[a7b9ec7]189        }
[5983eca]190
[6eca2eb]191        return ret ? ret : req->status_string;
[a7b9ec7]192}
193
[fb351ce]194/* WATCH OUT: This function might or might not destroy your connection.
195   Sub-optimal indeed, but just be careful when this returns NULL! */
[f81d8b8]196static JSON_Value *twitter_parse_response(struct im_connection *ic, struct http_request *req)
[2a6da96]197{
198        gboolean logging_in = !(ic->flags & OPT_LOGGED_IN);
199        gboolean periodic;
200        struct twitter_data *td = ic->proto_data;
[f81d8b8]201        JSON_Value *ret;
[2a6da96]202        char path[64] = "", *s;
[5ebff60]203
[2a6da96]204        if ((s = strchr(req->request, ' '))) {
[5ebff60]205                path[sizeof(path) - 1] = '\0';
[2a6da96]206                strncpy(path, s + 1, sizeof(path) - 1);
[5ebff60]207                if ((s = strchr(path, '?')) || (s = strchr(path, ' '))) {
[2a6da96]208                        *s = '\0';
[5ebff60]209                }
[2a6da96]210        }
[5ebff60]211
[2a6da96]212        /* Kinda nasty. :-( Trying to suppress error messages, but only
213           for periodic (i.e. mentions/timeline) queries. */
214        periodic = strstr(path, "timeline") || strstr(path, "mentions");
[5ebff60]215
[2a6da96]216        if (req->status_code == 401 && logging_in) {
217                /* IIRC Twitter once had an outage where they were randomly
218                   throwing 401s so I'll keep treating this one as fatal
219                   only during login. */
[5246133]220                imcb_error(ic, "Authentication failure (%s)",
[5ebff60]221                           twitter_parse_error(req));
[2a6da96]222                imc_logout(ic, FALSE);
223                return NULL;
224        } else if (req->status_code != 200) {
225                // It didn't go well, output the error and return.
[5ebff60]226                if (!periodic || logging_in || ++td->http_fails >= 5) {
[96dd574]227                        twitter_log(ic, "Error: Could not retrieve %s: %s",
[5ebff60]228                                    path, twitter_parse_error(req));
229                }
230
231                if (logging_in) {
[2a6da96]232                        imc_logout(ic, TRUE);
[5ebff60]233                }
[2a6da96]234                return NULL;
235        } else {
236                td->http_fails = 0;
237        }
238
[f81d8b8]239        if ((ret = json_parse_string(req->reply_body)) == NULL) {
[2a6da96]240                imcb_error(ic, "Could not retrieve %s: %s",
[5ebff60]241                           path, "XML parse error");
[2a6da96]242        }
243        return ret;
244}
245
[1b221e0]246static void twitter_http_get_friends_ids(struct http_request *req);
[fb62f132]247static void twitter_http_get_mutes_ids(struct http_request *req);
248static void twitter_http_get_noretweets_ids(struct http_request *req);
[1b221e0]249
250/**
251 * Get the friends ids.
252 */
[8203da9]253void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor)
[1b221e0]254{
[85c3004]255        // Primitive, but hey! It works...
[5983eca]256        char *args[2];
[5ebff60]257
[1b221e0]258        args[0] = "cursor";
[85c3004]259        args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor);
[bb5ce4d1]260        twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2);
[1b221e0]261
262        g_free(args[1]);
263}
264
[fb62f132]265/**
266 * Get the muted users ids.
267 */
268void twitter_get_mutes_ids(struct im_connection *ic, gint64 next_cursor)
269{
270        char *args[2];
271
272        args[0] = "cursor";
273        args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor);
274        twitter_http(ic, TWITTER_MUTES_IDS_URL, twitter_http_get_mutes_ids, ic, 0, args, 2);
275
276        g_free(args[1]);
277}
278
279/**
280 * Get the ids for users from whom we should ignore retweets.
281 */
282void twitter_get_noretweets_ids(struct im_connection *ic, gint64 next_cursor)
283{
284        char *args[2];
285
286        args[0] = "cursor";
287        args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor);
288        twitter_http(ic, TWITTER_NORETWEETS_IDS_URL, twitter_http_get_noretweets_ids, ic, 0, args, 2);
289
290        g_free(args[1]);
291}
292
[1b221e0]293/**
294 * Fill a list of ids.
295 */
[f81d8b8]296static gboolean twitter_xt_get_friends_id_list(JSON_Value *node, struct twitter_xml_list *txl)
[1b221e0]297{
[f81d8b8]298        JSON_Array *c;
[e08ae0c]299        int i;
[5983eca]300
[62d2cfb]301        // Set the list type.
302        txl->type = TXL_ID;
[1b221e0]303
[f81d8b8]304        if (!(c = json_object_get_array(json_object(node), "ids"))) {
[9e8c945]305                return FALSE;
[5ebff60]306        }
[1b221e0]307
[f81d8b8]308        for (i = 0; i < json_array_get_count(c); i++) {
309                jint id = json_array_get_integer(c, i);
[5ebff60]310
[e08ae0c]311                txl->list = g_slist_prepend(txl->list,
[f81d8b8]312                                            g_strdup_printf("%lld", id));
[e08ae0c]313        }
[5ebff60]314
[f81d8b8]315        JSON_Value *next = json_object_get_value(json_object(node), "next_cursor");
316        if (next && json_type(next) == JSONInteger) {
317                txl->next_cursor = json_integer(next);
[5ebff60]318        } else {
[e08ae0c]319                txl->next_cursor = -1;
[5ebff60]320        }
321
[9e8c945]322        return TRUE;
[1b221e0]323}
324
[de923d5]325static void twitter_get_users_lookup(struct im_connection *ic);
326
[1b221e0]327/**
328 * Callback for getting the friends ids.
329 */
330static void twitter_http_get_friends_ids(struct http_request *req)
331{
332        struct im_connection *ic;
[f81d8b8]333        JSON_Value *parsed;
[1b221e0]334        struct twitter_xml_list *txl;
[3bd4a93]335        struct twitter_data *td;
[1b221e0]336
337        ic = req->data;
338
[62d2cfb]339        // Check if the connection is still active.
[5ebff60]340        if (!g_slist_find(twitter_connections, ic)) {
[62d2cfb]341                return;
[5ebff60]342        }
[5983eca]343
[37aa317]344        td = ic->proto_data;
[62d2cfb]345
[1b221e0]346        // Parse the data.
[5ebff60]347        if (!(parsed = twitter_parse_response(ic, req))) {
[2a6da96]348                return;
[5ebff60]349        }
350
[bbff22d]351        txl = g_new0(struct twitter_xml_list, 1);
352        txl->list = td->follow_ids;
353
[d0752e8]354        twitter_xt_get_friends_id_list(parsed, txl);
[e08ae0c]355        json_value_free(parsed);
[1b221e0]356
[de923d5]357        td->follow_ids = txl->list;
[5ebff60]358        if (txl->next_cursor) {
[de923d5]359                /* These were just numbers. Up to 4000 in a response AFAIK so if we get here
360                   we may be using a spammer account. \o/ */
[1b221e0]361                twitter_get_friends_ids(ic, txl->next_cursor);
[5ebff60]362        } else {
[de923d5]363                /* Now to convert all those numbers into names.. */
364                twitter_get_users_lookup(ic);
[5ebff60]365        }
[de923d5]366
367        txl->list = NULL;
368        txl_free(txl);
369}
370
[fb62f132]371/**
372 * Callback for getting the mutes ids.
373 */
374static void twitter_http_get_mutes_ids(struct http_request *req)
375{
376        struct im_connection *ic = req->data;
[537d9b9]377        JSON_Value *parsed;
[fb62f132]378        struct twitter_xml_list *txl;
379        struct twitter_data *td;
380
381        // Check if the connection is stil active
382        if (!g_slist_find(twitter_connections, ic)) {
383                return;
384        }
385
386        td = ic->proto_data;
387
[dc45a85]388        if (req->status_code != 200) {
389                /* Fail silently */
390                return;
391        }
392
[fb62f132]393        // Parse the data.
394        if (!(parsed = twitter_parse_response(ic, req))) {
395                return;
396        }
397
398        txl = g_new0(struct twitter_xml_list, 1);
399        txl->list = td->mutes_ids;
400
401        /* mute ids API response is similar enough to friends response
402           to reuse this method */
403        twitter_xt_get_friends_id_list(parsed, txl);
404        json_value_free(parsed);
405
406        td->mutes_ids = txl->list;
407        if (txl->next_cursor) {
408                /* Recurse while there are still more pages */
409                twitter_get_mutes_ids(ic, txl->next_cursor);
410        }
411
412        txl->list = NULL;
413        txl_free(txl);
414}
415
416/**
417 * Callback for getting the no-retweets ids.
418 */
419static void twitter_http_get_noretweets_ids(struct http_request *req)
420{
421        struct im_connection *ic = req->data;
[537d9b9]422        JSON_Value *parsed;
[fb62f132]423        struct twitter_xml_list *txl;
424        struct twitter_data *td;
425
426        // Check if the connection is stil active
427        if (!g_slist_find(twitter_connections, ic)) {
428                return;
429        }
430
[dc45a85]431        if (req->status_code != 200) {
432                /* Fail silently */
433                return;
434        }
435
[fb62f132]436        td = ic->proto_data;
437
438        // Parse the data.
439        if (!(parsed = twitter_parse_response(ic, req))) {
440                return;
441        }
442
443        txl = g_new0(struct twitter_xml_list, 1);
444        txl->list = td->noretweets_ids;
445       
446        // Process the retweet ids
447        txl->type = TXL_ID;
[537d9b9]448        if (json_type(parsed) == JSONArray) {
449                JSON_Array *arr = json_array(parsed);
[fb62f132]450                unsigned int i;
[537d9b9]451                for (i = 0; i < json_array_get_count(arr); i++) {
452                        jint id = json_array_get_integer(arr, i);
[fb62f132]453                        txl->list = g_slist_prepend(txl->list,
[537d9b9]454                                                    g_strdup_printf("%lld", id));
[fb62f132]455                }
456        }
457
458        json_value_free(parsed);
459        td->noretweets_ids = txl->list;
460
461        txl->list = NULL;
462        txl_free(txl);
463}
464
[f81d8b8]465static gboolean twitter_xt_get_users(JSON_Value *node, struct twitter_xml_list *txl);
[de923d5]466static void twitter_http_get_users_lookup(struct http_request *req);
467
468static void twitter_get_users_lookup(struct im_connection *ic)
469{
470        struct twitter_data *td = ic->proto_data;
471        char *args[2] = {
472                "user_id",
473                NULL,
474        };
475        GString *ids = g_string_new("");
476        int i;
[5ebff60]477
[de923d5]478        /* We can request up to 100 users at a time. */
[5ebff60]479        for (i = 0; i < 100 && td->follow_ids; i++) {
480                g_string_append_printf(ids, ",%s", (char *) td->follow_ids->data);
[de923d5]481                g_free(td->follow_ids->data);
482                td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data);
483        }
484        if (ids->len > 0) {
485                args[1] = ids->str + 1;
486                /* POST, because I think ids can be up to 1KB long. */
487                twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2);
488        } else {
489                /* We have all users. Continue with login. (Get statuses.) */
490                td->flags |= TWITTER_HAVE_FRIENDS;
491                twitter_login_finish(ic);
492        }
493        g_string_free(ids, TRUE);
494}
495
496/**
497 * Callback for getting (twitter)friends...
498 *
[5ebff60]499 * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has
500 * hundreds of friends?" you wonder? You probably not, since you are reading the source of
[de923d5]501 * BitlBee... Get a life and meet new people!
502 */
503static void twitter_http_get_users_lookup(struct http_request *req)
504{
505        struct im_connection *ic = req->data;
[f81d8b8]506        JSON_Value *parsed;
[de923d5]507        struct twitter_xml_list *txl;
508        GSList *l = NULL;
509        struct twitter_xml_user *user;
510
511        // Check if the connection is still active.
[5ebff60]512        if (!g_slist_find(twitter_connections, ic)) {
[de923d5]513                return;
[5ebff60]514        }
[de923d5]515
516        // Get the user list from the parsed xml feed.
[5ebff60]517        if (!(parsed = twitter_parse_response(ic, req))) {
[2a6da96]518                return;
[5ebff60]519        }
[bbff22d]520
521        txl = g_new0(struct twitter_xml_list, 1);
522        txl->list = NULL;
523
[d0752e8]524        twitter_xt_get_users(parsed, txl);
[e08ae0c]525        json_value_free(parsed);
[de923d5]526
527        // Add the users as buddies.
528        for (l = txl->list; l; l = g_slist_next(l)) {
529                user = l->data;
530                twitter_add_buddy(ic, user->screen_name, user->name);
531        }
532
533        // Free the structure.
[62d2cfb]534        txl_free(txl);
[de923d5]535
536        twitter_get_users_lookup(ic);
[1b221e0]537}
538
[f81d8b8]539struct twitter_xml_user *twitter_xt_get_user(const JSON_Object *node)
[8e3b7ac]540{
541        struct twitter_xml_user *txu;
[f81d8b8]542       
543        if (!node)
544                return NULL;
[5ebff60]545
[8e3b7ac]546        txu = g_new0(struct twitter_xml_user, 1);
[f81d8b8]547        txu->name = g_strdup(json_object_get_string(node, "name"));
548        txu->screen_name = g_strdup(json_object_get_string(node, "screen_name"));
549        txu->uid = json_object_get_integer(node, "id");
[5ebff60]550
[8e3b7ac]551        return txu;
552}
553
[62d2cfb]554/**
555 * Function to fill a twitter_xml_list struct.
556 * It sets:
557 *  - all <user>s from the <users> element.
558 */
[f81d8b8]559static gboolean twitter_xt_get_users(JSON_Value *node, struct twitter_xml_list *txl)
[62d2cfb]560{
561        struct twitter_xml_user *txu;
[e08ae0c]562        int i;
[62d2cfb]563
564        // Set the type of the list.
565        txl->type = TXL_USER;
566
[f81d8b8]567        if (json_type(node) != JSONArray) {
[9e8c945]568                return FALSE;
[5ebff60]569        }
[f81d8b8]570       
[62d2cfb]571        // The root <users> node should hold the list of users <user>
572        // Walk over the nodes children.
[f81d8b8]573        JSON_Array *arr = json_array(node);
574        for (i = 0; i < json_array_get_count(arr); i++) {
575                JSON_Object *o = json_array_get_object(arr, i);
576                if (!o)
577                        continue;
578                txu = twitter_xt_get_user(o);
[5ebff60]579                if (txu) {
[8e3b7ac]580                        txl->list = g_slist_prepend(txl->list, txu);
[5ebff60]581                }
[62d2cfb]582        }
583
[9e8c945]584        return TRUE;
[62d2cfb]585}
586
[2b02617]587#ifdef __GLIBC__
588#define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y"
589#else
590#define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y"
591#endif
[62d2cfb]592
[537d9b9]593static void expand_entities(char **text, const JSON_Object *node, const JSON_Object *extended_node);
[d1356cb]594
[1b221e0]595/**
596 * Function to fill a twitter_xml_status struct.
597 * It sets:
598 *  - the status text and
599 *  - the created_at timestamp and
600 *  - the status id and
601 *  - the user in a twitter_xml_user struct.
602 */
[f81d8b8]603static struct twitter_xml_status *twitter_xt_get_status(const JSON_Object *node)
[1b221e0]604{
[ad54149]605        struct twitter_xml_status *txs = {0};
[f81d8b8]606        const JSON_Object *rt = NULL;
[537d9b9]607        const JSON_Value *text_value = NULL;
608        const JSON_Object *extended_node = NULL;
[5ebff60]609
[f81d8b8]610        if (!node) {
[9e8c945]611                return FALSE;
[5ebff60]612        }
[c9b5817]613        txs = g_new0(struct twitter_xml_status, 1);
[1b221e0]614
[5ebff60]615        JSON_O_FOREACH(node, k, v) {
[537d9b9]616                if (strcmp("text", k) == 0 && json_type(v) == JSONString && text_value == NULL) {
[ad54149]617                        text_value = v;
[537d9b9]618                } else if (strcmp("full_text", k) == 0 && json_type(v) == JSONString) {
[ad54149]619                        text_value = v;
[537d9b9]620                } else if (strcmp("extended_tweet", k) == 0 && json_type(v) == JSONObject) {
621                        text_value = json_object_get_value(json_object(v), "full_text");
622                        extended_node = json_object(v);
[f81d8b8]623                } else if (strcmp("retweeted_status", k) == 0 && (rt = json_object(v))) {
624                        // Handling below.
625                } else if (strcmp("created_at", k) == 0 && json_type(v) == JSONString) {
[08579a1]626                        struct tm parsed;
[5983eca]627
[08579a1]628                        /* Very sensitive to changes to the formatting of
629                           this field. :-( Also assumes the timezone used
630                           is UTC since C time handling functions suck. */
[f81d8b8]631                        if (strptime(json_string(v), TWITTER_TIME_FORMAT, &parsed) != NULL) {
[5983eca]632                                txs->created_at = mktime_utc(&parsed);
[5ebff60]633                        }
[f81d8b8]634                } else if (strcmp("user", k) == 0 && json_type(v) == JSONObject) {
635                        txs->user = twitter_xt_get_user(json_object(v));
636                } else if (strcmp("id", k) == 0 && json_type(v) == JSONInteger) {
637                        txs->rt_id = txs->id = json_integer(v);
638                } else if (strcmp("in_reply_to_status_id", k) == 0 && json_type(v) == JSONInteger) {
639                        txs->reply_to = json_integer(v);
[ce81acd]640                }
[1b221e0]641        }
[5983eca]642
[0fff0b2]643        /* If it's a (truncated) retweet, get the original. Even if the API claims it
644           wasn't truncated because it may be lying. */
645        if (rt) {
[c9b5817]646                struct twitter_xml_status *rtxs = twitter_xt_get_status(rt);
647                if (rtxs) {
[0ca1d79]648                        txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text);
[c9b5817]649                        txs->id = rtxs->id;
[0ca1d79]650                        txs_free(rtxs);
[e193aeb]651                }
[537d9b9]652        } else if (text_value && json_type(text_value) == JSONString) {
653                txs->text = g_strdup(json_string(text_value));
[ad54149]654                strip_html(txs->text);
[58d285a]655                expand_entities(&txs->text, node, extended_node);
[d1356cb]656        }
657
[5ebff60]658        if (txs->text && txs->user && txs->id) {
[c9b5817]659                return txs;
[5ebff60]660        }
661
[c9b5817]662        txs_free(txs);
663        return NULL;
[d1356cb]664}
665
666/**
667 * Function to fill a twitter_xml_status struct (DM variant).
668 */
[f81d8b8]669static struct twitter_xml_status *twitter_xt_get_dm(const JSON_Object *node)
[d1356cb]670{
[2cd8540]671        struct twitter_xml_status *txs;
[5ebff60]672
[f81d8b8]673        if (!node) {
[d1356cb]674                return FALSE;
[5ebff60]675        }
[2cd8540]676        txs = g_new0(struct twitter_xml_status, 1);
[d1356cb]677
[5ebff60]678        JSON_O_FOREACH(node, k, v) {
[f81d8b8]679                if (strcmp("text", k) == 0 && (txs->text = g_strdup(json_string(v)))) {
[29f72b7]680                        strip_html(txs->text);
[f81d8b8]681                } else if (strcmp("created_at", k) == 0 && json_type(v) == JSONString) {
[d1356cb]682                        struct tm parsed;
683
684                        /* Very sensitive to changes to the formatting of
685                           this field. :-( Also assumes the timezone used
686                           is UTC since C time handling functions suck. */
[f81d8b8]687                        if (strptime(json_string(v), TWITTER_TIME_FORMAT, &parsed) != NULL) {
[d1356cb]688                                txs->created_at = mktime_utc(&parsed);
[5ebff60]689                        }
[f81d8b8]690                } else if (strcmp("sender", k) == 0 && json_type(v) == JSONObject) {
691                        txs->user = twitter_xt_get_user(json_object(v));
692                } else if (strcmp("id", k) == 0 && json_type(v) == JSONInteger) {
693                        txs->id = json_integer(v);
[d1356cb]694                }
695        }
696
[58d285a]697        expand_entities(&txs->text, node, NULL);
[d1356cb]698
[5ebff60]699        if (txs->text && txs->user && txs->id) {
[2cd8540]700                return txs;
[5ebff60]701        }
702
[2cd8540]703        txs_free(txs);
704        return NULL;
[d1356cb]705}
706
[537d9b9]707static void expand_entities(char **text, const JSON_Object *node, const JSON_Object *extended_node)
[5ebff60]708{
[537d9b9]709        JSON_Object *entities, *extended_entities, *quoted;
[cd60710]710        char *quote_url = NULL, *quote_text = NULL;
711
[f81d8b8]712        if (!(entities = json_object_get_object(node, "entities")))
[cd60710]713                return;
[f81d8b8]714        if ((quoted = json_object_get_object(node, "quoted_status"))) {
[cd60710]715                /* New "retweets with comments" feature. Note that this info
716                 * seems to be included in the streaming API only! Grab the
717                 * full message and try to insert it when we run into the
718                 * Tweet entity. */
719                struct twitter_xml_status *txs = twitter_xt_get_status(quoted);
720                quote_text = g_strdup_printf("@%s: %s", txs->user->screen_name, txs->text);
721                quote_url = g_strdup_printf("%s/status/%" G_GUINT64_FORMAT, txs->user->screen_name, txs->id);
722                txs_free(txs);
723        } else {
724                quoted = NULL;
725        }
726
[58d285a]727        if (extended_node) {
[537d9b9]728                extended_entities = json_object_get_object(extended_node, "entities");
729                if (extended_entities) {
[58d285a]730                        entities = extended_entities;
731                }
732        }
733
[5ebff60]734        JSON_O_FOREACH(entities, k, v) {
[d1356cb]735                int i;
[5ebff60]736
[f81d8b8]737                if (json_type(v) != JSONArray) {
[d1356cb]738                        continue;
[5ebff60]739                }
740                if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0) {
[d1356cb]741                        continue;
[5ebff60]742                }
743
[f81d8b8]744                for (i = 0; i < json_array_get_count(json_array(v)); i++) {
[cd60710]745                        const char *format = "%s%s <%s>%s";
[f81d8b8]746                        JSON_Object *r = json_array_get_object(json_array(v), i);
[cd60710]747
[f81d8b8]748                        if (!r) {
[8e3b7ac]749                                continue;
[5ebff60]750                        }
751
[f81d8b8]752                        const char *kort = json_object_get_string(r, "url");
753                        const char *disp = json_object_get_string(r, "display_url");
754                        const char *full = json_object_get_string(r, "expanded_url");
[d1356cb]755                        char *pos, *new;
[5ebff60]756
[cd60710]757                        if (!kort || !disp || !(pos = strstr(*text, kort))) {
[429a9b1]758                                continue;
[5ebff60]759                        }
[cd60710]760                        if (quote_url && strstr(full, quote_url)) {
761                                format = "%s<%s> [%s]%s";
762                                disp = quote_text;
763                        }
[5ebff60]764
[d1356cb]765                        *pos = '\0';
[cd60710]766                        new = g_strdup_printf(format, *text, kort,
[d1356cb]767                                              disp, pos + strlen(kort));
[5ebff60]768
[cd60710]769                        g_free(*text);
770                        *text = new;
[429a9b1]771                }
[e193aeb]772        }
[cd60710]773        g_free(quote_text);
774        g_free(quote_url);
[1b221e0]775}
776
777/**
778 * Function to fill a twitter_xml_list struct.
779 * It sets:
780 *  - all <status>es within the <status> element and
781 *  - the next_cursor.
782 */
[f81d8b8]783static gboolean twitter_xt_get_status_list(struct im_connection *ic, const JSON_Value *node,
[9e8c945]784                                           struct twitter_xml_list *txl)
[1b221e0]785{
786        struct twitter_xml_status *txs;
[8e3b7ac]787        int i;
[1b221e0]788
[62d2cfb]789        // Set the type of the list.
790        txl->type = TXL_STATUS;
[5ebff60]791
[f81d8b8]792        if (json_type(node) != JSONArray) {
[9e8c945]793                return FALSE;
[5ebff60]794        }
[62d2cfb]795
[1b221e0]796        // The root <statuses> node should hold the list of statuses <status>
797        // Walk over the nodes children.
[f81d8b8]798        for (i = 0; i < json_array_get_count(json_array(node)); i++) {
799                txs = twitter_xt_get_status(json_array_get_object(json_array(node), i));
[5ebff60]800                if (!txs) {
[c9b5817]801                        continue;
[5ebff60]802                }
803
[8e3b7ac]804                txl->list = g_slist_prepend(txl->list, txs);
[1b221e0]805        }
806
[9e8c945]807        return TRUE;
[1b221e0]808}
809
[c9b5817]810/* Will log messages either way. Need to keep track of IDs for stream deduping.
811   Plus, show_ids is on by default and I don't see why anyone would disable it. */
[ce81acd]812static char *twitter_msg_add_id(struct im_connection *ic,
[5ebff60]813                                struct twitter_xml_status *txs, const char *prefix)
[ce81acd]814{
815        struct twitter_data *td = ic->proto_data;
[c9b5817]816        int reply_to = -1;
817        bee_user_t *bu;
[5983eca]818
819        if (txs->reply_to) {
[ce81acd]820                int i;
[5ebff60]821                for (i = 0; i < TWITTER_LOG_LENGTH; i++) {
[5983eca]822                        if (td->log[i].id == txs->reply_to) {
[c9b5817]823                                reply_to = i;
[ce81acd]824                                break;
825                        }
[5ebff60]826                }
[ce81acd]827        }
[5983eca]828
[c9b5817]829        if (txs->user && txs->user->screen_name &&
830            (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) {
831                struct twitter_user_data *tud = bu->data;
832
833                if (txs->id > tud->last_id) {
834                        tud->last_id = txs->id;
835                        tud->last_time = txs->created_at;
836                }
837        }
[5ebff60]838
[c9b5817]839        td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH;
840        td->log[td->log_id].id = txs->id;
841        td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name);
[5ebff60]842
[67f6828]843        /* This is all getting hairy. :-( If we RT'ed something ourselves,
844           remember OUR id instead so undo will work. In other cases, the
845           original tweet's id should be remembered for deduplicating. */
[5ebff60]846        if (g_strcasecmp(txs->user->screen_name, td->user) == 0) {
[67f6828]847                td->log[td->log_id].id = txs->rt_id;
[c43146d]848                /* More useful than NULL. */
849                td->log[td->log_id].bu = &twitter_log_local_user;
[5ebff60]850        }
851
[0ca1d79]852        if (set_getbool(&ic->acc->set, "show_ids")) {
[5ebff60]853                if (reply_to != -1) {
[0ca1d79]854                        return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s",
855                                               td->log_id, reply_to, prefix, txs->text);
[5ebff60]856                } else {
[0ca1d79]857                        return g_strdup_printf("\002[\002%02x\002]\002 %s%s",
858                                               td->log_id, prefix, txs->text);
[5ebff60]859                }
[0ca1d79]860        } else {
[5ebff60]861                if (*prefix) {
[0ca1d79]862                        return g_strconcat(prefix, txs->text, NULL);
[5ebff60]863                } else {
[0ca1d79]864                        return NULL;
[5ebff60]865                }
[0ca1d79]866        }
[ce81acd]867}
868
[73ee390]869/**
870 * Function that is called to see the filter statuses in groupchat windows.
871 */
872static void twitter_status_show_filter(struct im_connection *ic, struct twitter_xml_status *status)
873{
874        struct twitter_data *td = ic->proto_data;
875        char *msg = twitter_msg_add_id(ic, status, "");
876        struct twitter_filter *tf;
877        GSList *f;
878        GSList *l;
879
880        for (f = td->filters; f; f = g_slist_next(f)) {
881                tf = f->data;
882
883                switch (tf->type) {
884                case TWITTER_FILTER_TYPE_FOLLOW:
[5ebff60]885                        if (status->user->uid != tf->uid) {
[73ee390]886                                continue;
[5ebff60]887                        }
[73ee390]888                        break;
889
890                case TWITTER_FILTER_TYPE_TRACK:
[5ebff60]891                        if (strcasestr(status->text, tf->text) == NULL) {
[73ee390]892                                continue;
[5ebff60]893                        }
[73ee390]894                        break;
895
896                default:
897                        continue;
898                }
899
900                for (l = tf->groupchats; l; l = g_slist_next(l)) {
901                        imcb_chat_msg(l->data, status->user->screen_name,
[5ebff60]902                                      msg ? msg : status->text, 0, 0);
[73ee390]903                }
904        }
905
906        g_free(msg);
907}
908
[62d2cfb]909/**
910 * Function that is called to see the statuses in a groupchat window.
911 */
[29f72b7]912static void twitter_status_show_chat(struct im_connection *ic, struct twitter_xml_status *status)
[62d2cfb]913{
914        struct twitter_data *td = ic->proto_data;
915        struct groupchat *gc;
[29f72b7]916        gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0;
917        char *msg;
[62d2cfb]918
919        // Create a new groupchat if it does not exsist.
[631ec80]920        gc = twitter_groupchat_init(ic);
[62d2cfb]921
[5ebff60]922        if (!me) {
[29f72b7]923                /* MUST be done before twitter_msg_add_id() to avoid #872. */
924                twitter_add_buddy(ic, status->user->screen_name, status->user->name);
[5ebff60]925        }
[29f72b7]926        msg = twitter_msg_add_id(ic, status, "");
[5ebff60]927
[29f72b7]928        // Say it!
929        if (me) {
930                imcb_chat_log(gc, "You: %s", msg ? msg : status->text);
931        } else {
932                imcb_chat_msg(gc, status->user->screen_name,
[5ebff60]933                              msg ? msg : status->text, 0, status->created_at);
[62d2cfb]934        }
[29f72b7]935
936        g_free(msg);
[62d2cfb]937}
938
939/**
940 * Function that is called to see statuses as private messages.
941 */
[29f72b7]942static void twitter_status_show_msg(struct im_connection *ic, struct twitter_xml_status *status)
[62d2cfb]943{
944        struct twitter_data *td = ic->proto_data;
[631ec80]945        char from[MAX_STRING] = "";
[29f72b7]946        char *prefix = NULL, *text = NULL;
947        gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0;
[62d2cfb]948
[631ec80]949        if (td->flags & TWITTER_MODE_ONE) {
[5983eca]950                g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user);
951                from[MAX_STRING - 1] = '\0';
[e88fbe27]952        }
[5983eca]953
[5ebff60]954        if (td->flags & TWITTER_MODE_ONE) {
[29f72b7]955                prefix = g_strdup_printf("\002<\002%s\002>\002 ",
956                                         status->user->screen_name);
[5ebff60]957        } else if (!me) {
[29f72b7]958                twitter_add_buddy(ic, status->user->screen_name, status->user->name);
[5ebff60]959        } else {
[29f72b7]960                prefix = g_strdup("You: ");
[5ebff60]961        }
[5983eca]962
[29f72b7]963        text = twitter_msg_add_id(ic, status, prefix ? prefix : "");
[5983eca]964
[29f72b7]965        imcb_buddy_msg(ic,
966                       *from ? from : status->user->screen_name,
967                       text ? text : status->text, 0, status->created_at);
[5983eca]968
[29f72b7]969        g_free(text);
970        g_free(prefix);
971}
[5983eca]972
[29f72b7]973static void twitter_status_show(struct im_connection *ic, struct twitter_xml_status *status)
974{
975        struct twitter_data *td = ic->proto_data;
[7549d00]976        char *last_id_str;
[fb62f132]977        char *uid_str;
[5ebff60]978
979        if (status->user == NULL || status->text == NULL) {
[29f72b7]980                return;
[5ebff60]981        }
[fb62f132]982       
983        /* Check this is not a tweet that should be muted */
[537d9b9]984        uid_str = g_strdup_printf("%" G_GUINT64_FORMAT, status->user->uid);
[a244877]985
[fb62f132]986        if (g_slist_find_custom(td->mutes_ids, uid_str, (GCompareFunc)strcmp)) {
987                g_free(uid_str);
988                return;
989        }
990        if (status->id != status->rt_id && g_slist_find_custom(td->noretweets_ids, uid_str, (GCompareFunc)strcmp)) {
991                g_free(uid_str);
992                return;
993        }
[5ebff60]994
[29f72b7]995        /* Grrrr. Would like to do this during parsing, but can't access
996           settings from there. */
[5ebff60]997        if (set_getbool(&ic->acc->set, "strip_newlines")) {
[29f72b7]998                strip_newlines(status->text);
[5ebff60]999        }
1000
1001        if (status->from_filter) {
[73ee390]1002                twitter_status_show_filter(ic, status);
[5ebff60]1003        } else if (td->flags & TWITTER_MODE_CHAT) {
[29f72b7]1004                twitter_status_show_chat(ic, status);
[5ebff60]1005        } else {
[29f72b7]1006                twitter_status_show_msg(ic, status);
[5ebff60]1007        }
[5983eca]1008
[29f72b7]1009        // Update the timeline_id to hold the highest id, so that by the next request
1010        // we won't pick up the updates already in the list.
[573e274]1011        td->timeline_id = MAX(td->timeline_id, status->rt_id);
[7549d00]1012
1013        last_id_str = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id);
[fedc8f1]1014        set_setstr(&ic->acc->set, "_last_tweet", last_id_str);
[7549d00]1015        g_free(last_id_str);
[fb62f132]1016        g_free(uid_str);
[62d2cfb]1017}
1018
[f81d8b8]1019static gboolean twitter_stream_handle_object(struct im_connection *ic, JSON_Object *o, gboolean from_filter);
[ddc2de5]1020
1021static void twitter_http_stream(struct http_request *req)
1022{
[dff0e0b]1023        struct im_connection *ic = req->data;
[dd672e2]1024        struct twitter_data *td;
[f81d8b8]1025        JSON_Value *parsed;
[62f6b45]1026        int len = 0;
1027        char c, *nl;
[73ee390]1028        gboolean from_filter;
[5ebff60]1029
1030        if (!g_slist_find(twitter_connections, ic)) {
[dff0e0b]1031                return;
[5ebff60]1032        }
1033
[dd672e2]1034        td = ic->proto_data;
[5ebff60]1035
[dd672e2]1036        if ((req->flags & HTTPC_EOF) || !req->reply_body) {
[5ebff60]1037                if (req == td->stream) {
[73ee390]1038                        td->stream = NULL;
[5ebff60]1039                } else if (req == td->filter_stream) {
[73ee390]1040                        td->filter_stream = NULL;
[5ebff60]1041                }
[73ee390]1042
[dd672e2]1043                imcb_error(ic, "Stream closed (%s)", req->status_string);
[7320610]1044                if (req->status_code == 401) {
1045                        imcb_error(ic, "Check your system clock.");
1046                }
[dd672e2]1047                imc_logout(ic, TRUE);
1048                return;
1049        }
[5ebff60]1050
[985d66d]1051        if (req == td->stream) {
1052                ic->flags |= OPT_PONGED;
1053        }
1054
[62f6b45]1055        /* MUST search for CRLF, not just LF:
1056           https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */
[5ebff60]1057        if (!(nl = strstr(req->reply_body, "\r\n"))) {
[ddc2de5]1058                return;
[5ebff60]1059        }
1060
[62f6b45]1061        len = nl - req->reply_body;
1062        if (len > 0) {
1063                c = req->reply_body[len];
1064                req->reply_body[len] = '\0';
[5ebff60]1065
[f81d8b8]1066                if ((parsed = json_parse_string(req->reply_body))) {
[73ee390]1067                        from_filter = (req == td->filter_stream);
[f81d8b8]1068                        twitter_stream_handle_object(ic, json_object(parsed), from_filter);
[62f6b45]1069                }
1070                json_value_free(parsed);
1071                req->reply_body[len] = c;
[dff0e0b]1072        }
[5ebff60]1073
[62f6b45]1074        http_flush_bytes(req, len + 2);
[5ebff60]1075
[dff0e0b]1076        /* One notification might bring multiple events! */
[5ebff60]1077        if (req->body_size > 0) {
[dff0e0b]1078                twitter_http_stream(req);
[5ebff60]1079        }
[ddc2de5]1080}
[2322a9f]1081
[f81d8b8]1082static gboolean twitter_stream_handle_event(struct im_connection *ic, JSON_Object *o);
[c9b5817]1083static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs);
[5aa96fc8]1084
[f81d8b8]1085static gboolean twitter_stream_handle_object(struct im_connection *ic, JSON_Object *o, gboolean from_filter)
[dff0e0b]1086{
[5aa96fc8]1087        struct twitter_data *td = ic->proto_data;
[c9b5817]1088        struct twitter_xml_status *txs;
[f81d8b8]1089        JSON_Object *c;
[5ebff60]1090
[c9b5817]1091        if ((txs = twitter_xt_get_status(o))) {
[73ee390]1092                txs->from_filter = from_filter;
[2dcde94]1093                gboolean ret = twitter_stream_handle_status(ic, txs);
1094                txs_free(txs);
1095                return ret;
[f81d8b8]1096        } else if ((c = json_object_get_object(o, "direct_message")) &&
[2cd8540]1097                   (txs = twitter_xt_get_dm(c))) {
[5ebff60]1098                if (g_strcasecmp(txs->user->screen_name, td->user) != 0) {
[2cd8540]1099                        imcb_buddy_msg(ic, txs->user->screen_name,
[5ebff60]1100                                       txs->text, 0, txs->created_at);
1101                }
[d1356cb]1102                txs_free(txs);
1103                return TRUE;
[f81d8b8]1104        } else if (json_object_get_string(o, "event")) {
[5aa96fc8]1105                twitter_stream_handle_event(ic, o);
1106                return TRUE;
[f81d8b8]1107        } else if ((c = json_object_get_object(o, "disconnect"))) {
[5aa96fc8]1108                /* HACK: Because we're inside an event handler, we can't just
1109                   disconnect here. Instead, just change the HTTP status string
1110                   into a Twitter status string. */
[f81d8b8]1111                char *reason = g_strdup(json_object_get_string(c, "reason"));
[5aa96fc8]1112                if (reason) {
1113                        g_free(td->stream->status_string);
1114                        td->stream->status_string = reason;
1115                }
1116                return TRUE;
[dff0e0b]1117        }
1118        return FALSE;
1119}
1120
[c9b5817]1121static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs)
1122{
1123        struct twitter_data *td = ic->proto_data;
1124        int i;
[5ebff60]1125
[c9b5817]1126        for (i = 0; i < TWITTER_LOG_LENGTH; i++) {
1127                if (td->log[i].id == txs->id) {
[29f72b7]1128                        /* Got a duplicate (RT, probably). Drop it. */
[c9b5817]1129                        return TRUE;
1130                }
1131        }
[5ebff60]1132
[9f8bb17]1133        if (!(g_strcasecmp(txs->user->screen_name, td->user) == 0 ||
[2dcde94]1134              set_getbool(&ic->acc->set, "fetch_mentions") ||
[c9b5817]1135              bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) {
1136                /* Tweet is from an unknown person and the user does not want
1137                   to see @mentions, so drop it. twitter_stream_handle_event()
1138                   picks up new follows so this simple filter should be safe. */
1139                /* TODO: The streaming API seems to do poor @mention matching.
1140                   I.e. I'm getting mentions for @WilmerSomething, not just for
1141                   @Wilmer. But meh. You want spam, you get spam. */
1142                return TRUE;
1143        }
[5ebff60]1144
[29f72b7]1145        twitter_status_show(ic, txs);
[5ebff60]1146
[c9b5817]1147        return TRUE;
1148}
1149
[f81d8b8]1150static gboolean twitter_stream_handle_event(struct im_connection *ic, JSON_Object *o)
[5aa96fc8]1151{
1152        struct twitter_data *td = ic->proto_data;
[f81d8b8]1153        JSON_Object *source = json_object_get_object(o, "source");
1154        JSON_Object *target = json_object_get_object(o, "target");
1155        const char *type = json_object_get_string(o, "event");
[0d581bd]1156        struct twitter_xml_user *us = NULL;
1157        struct twitter_xml_user *ut = NULL;
[5ebff60]1158
[f81d8b8]1159        if (!type || !source || !target) {
[5aa96fc8]1160                return FALSE;
1161        }
[5ebff60]1162
[5aa96fc8]1163        if (strcmp(type, "follow") == 0) {
[0d581bd]1164                us = twitter_xt_get_user(source);
1165                ut = twitter_xt_get_user(target);
[9f8bb17]1166                if (g_strcasecmp(us->screen_name, td->user) == 0) {
[5aa96fc8]1167                        twitter_add_buddy(ic, ut->screen_name, ut->name);
1168                }
[0d581bd]1169        } else if (strcmp(type, "mute") == 0) {
[166a571]1170                GSList *found;
1171                char *uid_str;
[0d581bd]1172                ut = twitter_xt_get_user(target);
[537d9b9]1173                uid_str = g_strdup_printf("%" G_GUINT64_FORMAT, ut->uid);
[166a571]1174                if (!(found = g_slist_find_custom(td->mutes_ids, uid_str,
1175                                                  (GCompareFunc)strcmp))) {
1176                        td->mutes_ids = g_slist_prepend(td->mutes_ids, uid_str);
1177                }
1178                twitter_log(ic, "Muted user %s", ut->screen_name);
1179                if (getenv("BITLBEE_DEBUG")) {
[537d9b9]1180                        fprintf(stderr, "New mute: %s %"G_GUINT64_FORMAT"\n",
[166a571]1181                                ut->screen_name, ut->uid);
1182                }
[0d581bd]1183        } else if (strcmp(type, "unmute") == 0) {
1184                GSList *found;
1185                char *uid_str;
1186                ut = twitter_xt_get_user(target);
[537d9b9]1187                uid_str = g_strdup_printf("%" G_GUINT64_FORMAT, ut->uid);
[0d581bd]1188                if ((found = g_slist_find_custom(td->mutes_ids, uid_str,
1189                                                (GCompareFunc)strcmp))) {
[a244877]1190                        char *found_str = found->data;
1191                        td->mutes_ids = g_slist_delete_link(td->mutes_ids, found);
1192                        g_free(found_str);
[0d581bd]1193                }
1194                g_free(uid_str);
[166a571]1195                twitter_log(ic, "Unmuted user %s", ut->screen_name);
1196                if (getenv("BITLBEE_DEBUG")) {
[537d9b9]1197                        fprintf(stderr, "New unmute: %s %"G_GUINT64_FORMAT"\n",
[166a571]1198                                ut->screen_name, ut->uid);
1199                }
[5aa96fc8]1200        }
[5ebff60]1201
[0d581bd]1202        txu_free(us);
1203        txu_free(ut);
1204
[5aa96fc8]1205        return TRUE;
1206}
1207
[dff0e0b]1208gboolean twitter_open_stream(struct im_connection *ic)
1209{
1210        struct twitter_data *td = ic->proto_data;
[5ebff60]1211        char *args[2] = { "with", "followings" };
1212
[dff0e0b]1213        if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL,
[62f6b45]1214                                       twitter_http_stream, ic, 0, args, 2))) {
[dff0e0b]1215                /* This flag must be enabled or we'll get no data until EOF
1216                   (which err, kind of, defeats the purpose of a streaming API). */
1217                td->stream->flags |= HTTPC_STREAMING;
1218                return TRUE;
1219        }
[5ebff60]1220
[dff0e0b]1221        return FALSE;
1222}
1223
[73ee390]1224static gboolean twitter_filter_stream(struct im_connection *ic)
1225{
1226        struct twitter_data *td = ic->proto_data;
[5ebff60]1227        char *args[4] = { "follow", NULL, "track", NULL };
[73ee390]1228        GString *followstr = g_string_new("");
1229        GString *trackstr = g_string_new("");
1230        gboolean ret = FALSE;
1231        struct twitter_filter *tf;
1232        GSList *l;
1233
1234        for (l = td->filters; l; l = g_slist_next(l)) {
1235                tf = l->data;
1236
1237                switch (tf->type) {
1238                case TWITTER_FILTER_TYPE_FOLLOW:
[5ebff60]1239                        if (followstr->len > 0) {
[73ee390]1240                                g_string_append_c(followstr, ',');
[5ebff60]1241                        }
[73ee390]1242
1243                        g_string_append_printf(followstr, "%" G_GUINT64_FORMAT,
[5ebff60]1244                                               tf->uid);
[73ee390]1245                        break;
1246
1247                case TWITTER_FILTER_TYPE_TRACK:
[5ebff60]1248                        if (trackstr->len > 0) {
[73ee390]1249                                g_string_append_c(trackstr, ',');
[5ebff60]1250                        }
[73ee390]1251
1252                        g_string_append(trackstr, tf->text);
1253                        break;
1254
1255                default:
1256                        continue;
1257                }
1258        }
1259
1260        args[1] = followstr->str;
1261        args[3] = trackstr->str;
1262
[5ebff60]1263        if (td->filter_stream) {
[73ee390]1264                http_close(td->filter_stream);
[5ebff60]1265        }
[73ee390]1266
1267        if ((td->filter_stream = twitter_http(ic, TWITTER_FILTER_STREAM_URL,
1268                                              twitter_http_stream, ic, 0,
[5ebff60]1269                                              args, 4))) {
[73ee390]1270                /* This flag must be enabled or we'll get no data until EOF
1271                   (which err, kind of, defeats the purpose of a streaming API). */
1272                td->filter_stream->flags |= HTTPC_STREAMING;
1273                ret = TRUE;
1274        }
1275
1276        g_string_free(followstr, TRUE);
1277        g_string_free(trackstr, TRUE);
1278
1279        return ret;
1280}
1281
1282static void twitter_filter_users_post(struct http_request *req)
1283{
1284        struct im_connection *ic = req->data;
1285        struct twitter_data *td;
1286        struct twitter_filter *tf;
1287        GList *users = NULL;
[f81d8b8]1288        JSON_Value *parsed;
[73ee390]1289        GString *fstr;
1290        GSList *l;
1291        GList *u;
1292        int i;
1293
1294        // Check if the connection is still active.
[5ebff60]1295        if (!g_slist_find(twitter_connections, ic)) {
[73ee390]1296                return;
[5ebff60]1297        }
[73ee390]1298
1299        td = ic->proto_data;
1300
[5ebff60]1301        if (!(parsed = twitter_parse_response(ic, req))) {
[73ee390]1302                return;
[5ebff60]1303        }
[73ee390]1304
1305        for (l = td->filters; l; l = g_slist_next(l)) {
1306                tf = l->data;
1307
[5ebff60]1308                if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) {
[73ee390]1309                        users = g_list_prepend(users, tf);
[5ebff60]1310                }
[73ee390]1311        }
1312
[f81d8b8]1313        if (json_type(parsed) != JSONArray) {
[73ee390]1314                goto finish;
[5ebff60]1315        }
[73ee390]1316
[f81d8b8]1317        for (i = 0; i < json_array_get_count(json_array(parsed)); i++) {
1318                JSON_Object *o = json_array_get_object(json_array(parsed), i);
1319                jint id = json_object_get_integer(o, "id");
1320                const char *name = json_object_get_string(o, "screen_name");
[73ee390]1321
[f81d8b8]1322                if (!name || !id) {
[73ee390]1323                        continue;
[5ebff60]1324                }
[73ee390]1325
1326                for (u = users; u; u = g_list_next(u)) {
1327                        tf = u->data;
1328
1329                        if (g_strcasecmp(tf->text, name) == 0) {
[f81d8b8]1330                                tf->uid = id;
[73ee390]1331                                users = g_list_delete_link(users, u);
1332                                break;
1333                        }
1334                }
1335        }
1336
1337finish:
1338        json_value_free(parsed);
1339        twitter_filter_stream(ic);
1340
[5ebff60]1341        if (!users) {
[73ee390]1342                return;
[5ebff60]1343        }
[73ee390]1344
1345        fstr = g_string_new("");
1346
1347        for (u = users; u; u = g_list_next(u)) {
[5ebff60]1348                if (fstr->len > 0) {
[73ee390]1349                        g_string_append(fstr, ", ");
[5ebff60]1350                }
[73ee390]1351
1352                g_string_append(fstr, tf->text);
1353        }
1354
1355        imcb_error(ic, "Failed UID acquisitions: %s", fstr->str);
1356
1357        g_string_free(fstr, TRUE);
1358        g_list_free(users);
1359}
1360
1361gboolean twitter_open_filter_stream(struct im_connection *ic)
1362{
1363        struct twitter_data *td = ic->proto_data;
[5ebff60]1364        char *args[2] = { "screen_name", NULL };
[73ee390]1365        GString *ustr = g_string_new("");
1366        struct twitter_filter *tf;
1367        struct http_request *req;
1368        GSList *l;
1369
1370        for (l = td->filters; l; l = g_slist_next(l)) {
1371                tf = l->data;
1372
[5ebff60]1373                if (tf->type != TWITTER_FILTER_TYPE_FOLLOW || tf->uid != 0) {
[73ee390]1374                        continue;
[5ebff60]1375                }
[73ee390]1376
[5ebff60]1377                if (ustr->len > 0) {
[73ee390]1378                        g_string_append_c(ustr, ',');
[5ebff60]1379                }
[73ee390]1380
1381                g_string_append(ustr, tf->text);
1382        }
1383
1384        if (ustr->len == 0) {
1385                g_string_free(ustr, TRUE);
1386                return twitter_filter_stream(ic);
1387        }
1388
1389        args[1] = ustr->str;
1390        req = twitter_http(ic, TWITTER_USERS_LOOKUP_URL,
[5ebff60]1391                           twitter_filter_users_post,
1392                           ic, 0, args, 2);
[73ee390]1393
1394        g_string_free(ustr, TRUE);
1395        return req != NULL;
1396}
1397
[dff0e0b]1398static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor);
1399static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor);
1400
[2322a9f]1401/**
1402 * Get the timeline with optionally mentions
1403 */
[2f9027c]1404gboolean twitter_get_timeline(struct im_connection *ic, gint64 next_cursor)
[2322a9f]1405{
1406        struct twitter_data *td = ic->proto_data;
1407        gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions");
1408
1409        if (td->flags & TWITTER_DOING_TIMELINE) {
[11ec078]1410                if (++td->http_fails >= 5) {
1411                        imcb_error(ic, "Fetch timeout (%d)", td->flags);
1412                        imc_logout(ic, TRUE);
[2f9027c]1413                        return FALSE;
[11ec078]1414                }
[2322a9f]1415        }
1416
1417        td->flags |= TWITTER_DOING_TIMELINE;
1418
1419        twitter_get_home_timeline(ic, next_cursor);
1420
1421        if (include_mentions) {
1422                twitter_get_mentions(ic, next_cursor);
1423        }
[5ebff60]1424
[2f9027c]1425        return TRUE;
[2322a9f]1426}
1427
1428/**
1429 * Call this one after receiving timeline/mentions. Show to user once we have
1430 * both.
1431 */
1432void twitter_flush_timeline(struct im_connection *ic)
1433{
1434        struct twitter_data *td = ic->proto_data;
1435        gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions");
[b5fe39b]1436        int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions");
[2322a9f]1437        struct twitter_xml_list *home_timeline = td->home_timeline_obj;
1438        struct twitter_xml_list *mentions = td->mentions_obj;
[29f72b7]1439        guint64 last_id = 0;
[2322a9f]1440        GSList *output = NULL;
1441        GSList *l;
1442
[29f72b7]1443        imcb_connected(ic);
[5ebff60]1444
[2322a9f]1445        if (!(td->flags & TWITTER_GOT_TIMELINE)) {
1446                return;
1447        }
1448
1449        if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) {
1450                return;
1451        }
1452
1453        if (home_timeline && home_timeline->list) {
1454                for (l = home_timeline->list; l; l = g_slist_next(l)) {
1455                        output = g_slist_insert_sorted(output, l->data, twitter_compare_elements);
1456                }
1457        }
1458
1459        if (include_mentions && mentions && mentions->list) {
1460                for (l = mentions->list; l; l = g_slist_next(l)) {
[b5fe39b]1461                        if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) {
[2322a9f]1462                                continue;
1463                        }
1464
1465                        output = g_slist_insert_sorted(output, l->data, twitter_compare_elements);
1466                }
1467        }
1468
1469        // See if the user wants to see the messages in a groupchat window or as private messages.
[29f72b7]1470        while (output) {
1471                struct twitter_xml_status *txs = output->data;
[5ebff60]1472                if (txs->id != last_id) {
[29f72b7]1473                        twitter_status_show(ic, txs);
[5ebff60]1474                }
[29f72b7]1475                last_id = txs->id;
1476                output = g_slist_remove(output, txs);
1477        }
[2322a9f]1478
[199fea6]1479        txl_free(home_timeline);
1480        txl_free(mentions);
[2322a9f]1481
1482        td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS);
[199fea6]1483        td->home_timeline_obj = td->mentions_obj = NULL;
[2322a9f]1484}
1485
[ddc2de5]1486static void twitter_http_get_home_timeline(struct http_request *req);
1487static void twitter_http_get_mentions(struct http_request *req);
1488
[2322a9f]1489/**
1490 * Get the timeline.
1491 */
[ddc2de5]1492static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor)
[2322a9f]1493{
1494        struct twitter_data *td = ic->proto_data;
1495
[199fea6]1496        txl_free(td->home_timeline_obj);
[2322a9f]1497        td->home_timeline_obj = NULL;
1498        td->flags &= ~TWITTER_GOT_TIMELINE;
1499
[ad54149]1500        char *args[8];
[2322a9f]1501        args[0] = "cursor";
[85c3004]1502        args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor);
[429a9b1]1503        args[2] = "include_entities";
1504        args[3] = "true";
[ad54149]1505        args[4] = "tweet_mode";
1506        args[5] = "extended";
[2322a9f]1507        if (td->timeline_id) {
[ad54149]1508                args[6] = "since_id";
1509                args[7] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id);
[2322a9f]1510        }
1511
[90fc864]1512        if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args,
[ad54149]1513                         td->timeline_id ? 8 : 6) == NULL) {
[5ebff60]1514                if (++td->http_fails >= 5) {
[90fc864]1515                        imcb_error(ic, "Could not retrieve %s: %s",
1516                                   TWITTER_HOME_TIMELINE_URL, "connection failed");
[5ebff60]1517                }
[90fc864]1518                td->flags |= TWITTER_GOT_TIMELINE;
1519                twitter_flush_timeline(ic);
1520        }
[2322a9f]1521
1522        g_free(args[1]);
1523        if (td->timeline_id) {
[ad54149]1524                g_free(args[7]);
[2322a9f]1525        }
1526}
1527
1528/**
1529 * Get mentions.
1530 */
[ddc2de5]1531static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor)
[2322a9f]1532{
1533        struct twitter_data *td = ic->proto_data;
1534
[199fea6]1535        txl_free(td->mentions_obj);
[2322a9f]1536        td->mentions_obj = NULL;
1537        td->flags &= ~TWITTER_GOT_MENTIONS;
1538
[ad54149]1539        char *args[8];
[2322a9f]1540        args[0] = "cursor";
[85c3004]1541        args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor);
[429a9b1]1542        args[2] = "include_entities";
1543        args[3] = "true";
[2322a9f]1544        if (td->timeline_id) {
[429a9b1]1545                args[4] = "since_id";
[85c3004]1546                args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id);
[b5fe39b]1547        } else {
1548                args[4] = "count";
1549                args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions"));
[2322a9f]1550        }
[ad54149]1551        args[6] = "tweet_mode";
1552        args[7] = "extended";
[2322a9f]1553
[b5fe39b]1554        if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions,
[ad54149]1555                         ic, 0, args, 8) == NULL) {
[5ebff60]1556                if (++td->http_fails >= 5) {
[90fc864]1557                        imcb_error(ic, "Could not retrieve %s: %s",
1558                                   TWITTER_MENTIONS_URL, "connection failed");
[5ebff60]1559                }
[90fc864]1560                td->flags |= TWITTER_GOT_MENTIONS;
1561                twitter_flush_timeline(ic);
1562        }
[2322a9f]1563
1564        g_free(args[1]);
[2fb1262]1565        g_free(args[5]);
[2322a9f]1566}
1567
[1b221e0]1568/**
1569 * Callback for getting the home timeline.
1570 */
1571static void twitter_http_get_home_timeline(struct http_request *req)
1572{
[62d2cfb]1573        struct im_connection *ic = req->data;
[37aa317]1574        struct twitter_data *td;
[f81d8b8]1575        JSON_Value *parsed;
[1b221e0]1576        struct twitter_xml_list *txl;
[62d2cfb]1577
1578        // Check if the connection is still active.
[5ebff60]1579        if (!g_slist_find(twitter_connections, ic)) {
[62d2cfb]1580                return;
[5ebff60]1581        }
[5983eca]1582
[37aa317]1583        td = ic->proto_data;
[1b221e0]1584
[2322a9f]1585        // The root <statuses> node should hold the list of statuses <status>
[5ebff60]1586        if (!(parsed = twitter_parse_response(ic, req))) {
[2a6da96]1587                goto end;
[5ebff60]1588        }
[bbff22d]1589
1590        txl = g_new0(struct twitter_xml_list, 1);
1591        txl->list = NULL;
1592
[d0752e8]1593        twitter_xt_get_status_list(ic, parsed, txl);
[2fb1262]1594        json_value_free(parsed);
[2322a9f]1595
1596        td->home_timeline_obj = txl;
1597
[5ebff60]1598end:
1599        if (!g_slist_find(twitter_connections, ic)) {
[fb351ce]1600                return;
[5ebff60]1601        }
[fb351ce]1602
[2322a9f]1603        td->flags |= TWITTER_GOT_TIMELINE;
1604
1605        twitter_flush_timeline(ic);
1606}
1607
1608/**
1609 * Callback for getting mentions.
1610 */
1611static void twitter_http_get_mentions(struct http_request *req)
1612{
1613        struct im_connection *ic = req->data;
1614        struct twitter_data *td;
[f81d8b8]1615        JSON_Value *parsed;
[2322a9f]1616        struct twitter_xml_list *txl;
1617
1618        // Check if the connection is still active.
[5ebff60]1619        if (!g_slist_find(twitter_connections, ic)) {
[1b221e0]1620                return;
[5ebff60]1621        }
[2322a9f]1622
1623        td = ic->proto_data;
1624
[1b221e0]1625        // The root <statuses> node should hold the list of statuses <status>
[5ebff60]1626        if (!(parsed = twitter_parse_response(ic, req))) {
[2a6da96]1627                goto end;
[5ebff60]1628        }
[bbff22d]1629
1630        txl = g_new0(struct twitter_xml_list, 1);
1631        txl->list = NULL;
1632
[d0752e8]1633        twitter_xt_get_status_list(ic, parsed, txl);
[2fb1262]1634        json_value_free(parsed);
[1b221e0]1635
[2322a9f]1636        td->mentions_obj = txl;
[1b221e0]1637
[5ebff60]1638end:
1639        if (!g_slist_find(twitter_connections, ic)) {
[fb351ce]1640                return;
[5ebff60]1641        }
[fb351ce]1642
[2322a9f]1643        td->flags |= TWITTER_GOT_MENTIONS;
1644
1645        twitter_flush_timeline(ic);
[1b221e0]1646}
1647
1648/**
[de923d5]1649 * Callback to use after sending a POST request to twitter.
1650 * (Generic, used for a few kinds of queries.)
[1b221e0]1651 */
[7d53efb]1652static void twitter_http_post(struct http_request *req)
[1b221e0]1653{
1654        struct im_connection *ic = req->data;
[7b87539]1655        struct twitter_data *td;
[f81d8b8]1656        JSON_Value *parsed;
1657        jint id;
[1b221e0]1658
[62d2cfb]1659        // Check if the connection is still active.
[5ebff60]1660        if (!g_slist_find(twitter_connections, ic)) {
[62d2cfb]1661                return;
[5ebff60]1662        }
[62d2cfb]1663
[7b87539]1664        td = ic->proto_data;
1665        td->last_status_id = 0;
[5983eca]1666
[5ebff60]1667        if (!(parsed = twitter_parse_response(ic, req))) {
[1b221e0]1668                return;
[5ebff60]1669        }
1670
[f81d8b8]1671        if ((id = json_object_get_integer(json_object(parsed), "id"))) {
1672                td->last_status_id = id;
[67f6828]1673        }
[5ebff60]1674
[c751e51]1675        json_value_free(parsed);
[5ebff60]1676
1677        if (req->flags & TWITTER_HTTP_USER_ACK) {
[b235228]1678                twitter_log(ic, "Command processed successfully");
[5ebff60]1679        }
[1b221e0]1680}
1681
1682/**
1683 * Function to POST a new status to twitter.
[5983eca]1684 */
[b890626]1685void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to)
[1b221e0]1686{
[5983eca]1687        char *args[4] = {
[b890626]1688                "status", msg,
1689                "in_reply_to_status_id",
[85c3004]1690                g_strdup_printf("%" G_GUINT64_FORMAT, in_reply_to)
[b890626]1691        };
[5ebff60]1692
[b890626]1693        twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1,
[5ebff60]1694                     args, in_reply_to ? 4 : 2);
[b890626]1695        g_free(args[3]);
[1b221e0]1696}
1697
1698
[62d2cfb]1699/**
1700 * Function to POST a new message to twitter.
1701 */
1702void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg)
1703{
[5983eca]1704        char *args[4];
[5ebff60]1705
[62d2cfb]1706        args[0] = "screen_name";
1707        args[1] = who;
1708        args[2] = "text";
1709        args[3] = msg;
1710        // Use the same callback as for twitter_post_status, since it does basically the same.
[ba3233c]1711        twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4);
[62d2cfb]1712}
[7d53efb]1713
1714void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create)
1715{
[5983eca]1716        char *args[2];
[5ebff60]1717
[7d53efb]1718        args[0] = "screen_name";
1719        args[1] = who;
[5983eca]1720        twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL,
[5ebff60]1721                     twitter_http_post, ic, 1, args, 2);
[a26af5c]1722}
[7b87539]1723
[9cf63ac]1724/**
1725 * Mute or unmute a user
1726 */
1727void twitter_mute_create_destroy(struct im_connection *ic, char *who, int create)
1728{
1729        char *args[2];
1730
1731        args[0] = "screen_name";
1732        args[1] = who;
1733        twitter_http(ic, create ? TWITTER_MUTES_CREATE_URL : TWITTER_MUTES_DESTROY_URL,
1734                     twitter_http_post, ic, 1, args, 2);
1735}
1736
[7b87539]1737void twitter_status_destroy(struct im_connection *ic, guint64 id)
1738{
1739        char *url;
[5ebff60]1740
[85c3004]1741        url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s",
1742                              TWITTER_STATUS_DESTROY_URL, id, ".json");
[b235228]1743        twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0,
1744                       TWITTER_HTTP_USER_ACK);
[7b87539]1745        g_free(url);
1746}
[b890626]1747
1748void twitter_status_retweet(struct im_connection *ic, guint64 id)
1749{
1750        char *url;
[5ebff60]1751
[85c3004]1752        url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s",
1753                              TWITTER_STATUS_RETWEET_URL, id, ".json");
[b235228]1754        twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0,
1755                       TWITTER_HTTP_USER_ACK);
[b890626]1756        g_free(url);
1757}
[d18dee42]1758
1759/**
1760 * Report a user for sending spam.
1761 */
1762void twitter_report_spam(struct im_connection *ic, char *screen_name)
1763{
1764        char *args[2] = {
1765                "screen_name",
1766                NULL,
1767        };
[5ebff60]1768
[d18dee42]1769        args[1] = screen_name;
[b235228]1770        twitter_http_f(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post,
1771                       ic, 1, args, 2, TWITTER_HTTP_USER_ACK);
[d18dee42]1772}
[b61c74c]1773
1774/**
1775 * Favourite a tweet.
1776 */
1777void twitter_favourite_tweet(struct im_connection *ic, guint64 id)
1778{
[75bda8b]1779        char *args[2] = {
1780                "id",
1781                NULL,
1782        };
[5ebff60]1783
[85c3004]1784        args[1] = g_strdup_printf("%" G_GUINT64_FORMAT, id);
[75bda8b]1785        twitter_http_f(ic, TWITTER_FAVORITE_CREATE_URL, twitter_http_post,
1786                       ic, 1, args, 2, TWITTER_HTTP_USER_ACK);
1787        g_free(args[1]);
[b61c74c]1788}
[1201fcb]1789
1790static void twitter_http_status_show_url(struct http_request *req)
1791{
1792        struct im_connection *ic = req->data;
[ed83279]1793        JSON_Value *parsed;
1794        uint64_t id;
[1201fcb]1795        const char *name;
1796
1797        // Check if the connection is still active.
1798        if (!g_slist_find(twitter_connections, ic)) {
1799                return;
1800        }
1801
1802        if (!(parsed = twitter_parse_response(ic, req))) {
1803                return;
1804        }
1805
1806        name = json_object_dotget_string(json_object(parsed), "user.screen_name");
1807        id = json_object_get_integer(json_object(parsed), "id");
1808
[ed83279]1809        if (name && id) {
1810                twitter_log(ic, "https://twitter.com/%s/status/%" G_GUINT64_FORMAT, name, id);
[1201fcb]1811        } else {
1812                twitter_log(ic, "Error: could not fetch tweet url.");
1813        }
1814
1815        json_value_free(parsed);
1816}
1817
1818void twitter_status_show_url(struct im_connection *ic, guint64 id)
1819{
1820        char *url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", TWITTER_STATUS_SHOW_URL, id, ".json");
1821        twitter_http(ic, url, twitter_http_status_show_url, ic, 0, NULL, 0);
1822        g_free(url);
1823}
Note: See TracBrowser for help on using the repository browser.