source: protocols/twitter/twitter_lib.c @ d0752e8

Last change on this file since d0752e8 was d0752e8, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-09-22T12:12:12Z

Little cleanup. Use xt_from_string() where possible.

  • Property mode set to 100644
File size: 29.4 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>            *
7*  Copyright 2010-2011 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 "xmltree.h"
38#include "twitter_lib.h"
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;
[d0752e8]170        struct xt_node *root, *node, *err;
[5983eca]171
[a7b9ec7]172        g_free(ret);
173        ret = NULL;
[5983eca]174
175        if (req->body_size > 0) {
[d0752e8]176                root = xt_from_string(req->reply_body, req->body_size);
[de923d5]177               
[d0752e8]178                for (node = root; node; node = node->next)
[c0f33f1]179                        if ((err = xt_find_node(node->children, "error")) && err->text_len > 0) {
180                                ret = g_strdup_printf("%s (%s)", req->status_string, err->text);
[de923d5]181                                break;
182                        }
[5983eca]183
[d0752e8]184                xt_free_node(root);
[a7b9ec7]185        }
[5983eca]186
[6eca2eb]187        return ret ? ret : req->status_string;
[a7b9ec7]188}
189
[1b221e0]190static void twitter_http_get_friends_ids(struct http_request *req);
191
192/**
193 * Get the friends ids.
194 */
[8203da9]195void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor)
[1b221e0]196{
[5983eca]197        // Primitive, but hey! It works...     
198        char *args[2];
[1b221e0]199        args[0] = "cursor";
[5983eca]200        args[1] = g_strdup_printf("%lld", (long long) next_cursor);
[bb5ce4d1]201        twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2);
[1b221e0]202
203        g_free(args[1]);
204}
205
206/**
207 * Function to help fill a list.
208 */
[5983eca]209static xt_status twitter_xt_next_cursor(struct xt_node *node, struct twitter_xml_list *txl)
[1b221e0]210{
[bd64716]211        char *end = NULL;
[5983eca]212
213        if (node->text)
214                txl->next_cursor = g_ascii_strtoll(node->text, &end, 10);
215        if (end == NULL)
[bd64716]216                txl->next_cursor = -1;
[1b221e0]217
218        return XT_HANDLED;
219}
220
221/**
222 * Fill a list of ids.
223 */
[5983eca]224static xt_status twitter_xt_get_friends_id_list(struct xt_node *node, struct twitter_xml_list *txl)
[1b221e0]225{
226        struct xt_node *child;
[5983eca]227
[62d2cfb]228        // Set the list type.
229        txl->type = TXL_ID;
[1b221e0]230
231        // The root <statuses> node should hold the list of statuses <status>
232        // Walk over the nodes children.
[5983eca]233        for (child = node->children; child; child = child->next) {
[de923d5]234                if (g_strcasecmp("ids", child->name) == 0) {
235                        struct xt_node *idc;
236                        for (idc = child->children; idc; idc = idc->next)
237                                if (g_strcasecmp(idc->name, "id") == 0)
238                                        txl->list = g_slist_prepend(txl->list,
239                                                g_memdup(idc->text, idc->text_len + 1));
[5983eca]240                } else if (g_strcasecmp("next_cursor", child->name) == 0) {
[1b221e0]241                        twitter_xt_next_cursor(child, txl);
242                }
243        }
244
245        return XT_HANDLED;
246}
247
[de923d5]248static void twitter_get_users_lookup(struct im_connection *ic);
249
[1b221e0]250/**
251 * Callback for getting the friends ids.
252 */
253static void twitter_http_get_friends_ids(struct http_request *req)
254{
255        struct im_connection *ic;
[d0752e8]256        struct xt_node *parsed;
[1b221e0]257        struct twitter_xml_list *txl;
[3bd4a93]258        struct twitter_data *td;
[1b221e0]259
260        ic = req->data;
261
[62d2cfb]262        // Check if the connection is still active.
[5983eca]263        if (!g_slist_find(twitter_connections, ic))
[62d2cfb]264                return;
[5983eca]265
[37aa317]266        td = ic->proto_data;
[62d2cfb]267
[de923d5]268        // Check if the HTTP request went well. More strict checks as this is
269        // the first request we do in a session.
270        if (req->status_code == 401) {
271                imcb_error(ic, "Authentication failure");
272                imc_logout(ic, FALSE);
273                return;
274        } else if (req->status_code != 200) {
[1b221e0]275                // It didn't go well, output the error and return.
[de923d5]276                imcb_error(ic, "Could not retrieve %s: %s",
277                           TWITTER_FRIENDS_IDS_URL, twitter_parse_error(req));
278                imc_logout(ic, TRUE);
[1b221e0]279                return;
[3bd4a93]280        } else {
281                td->http_fails = 0;
[1b221e0]282        }
283
[de923d5]284        /* Create the room now that we "logged in". */
[2322a9f]285        if (!td->timeline_gc && g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0)
[de923d5]286                twitter_groupchat_init(ic);
287
[1b221e0]288        txl = g_new0(struct twitter_xml_list, 1);
[de923d5]289        txl->list = td->follow_ids;
[1b221e0]290
291        // Parse the data.
[d0752e8]292        parsed = xt_from_string(req->reply_body, req->body_size);
293        twitter_xt_get_friends_id_list(parsed, txl);
294        xt_free_node(parsed);
[1b221e0]295
[de923d5]296        td->follow_ids = txl->list;
[1b221e0]297        if (txl->next_cursor)
[de923d5]298                /* These were just numbers. Up to 4000 in a response AFAIK so if we get here
299                   we may be using a spammer account. \o/ */
[1b221e0]300                twitter_get_friends_ids(ic, txl->next_cursor);
[de923d5]301        else
302                /* Now to convert all those numbers into names.. */
303                twitter_get_users_lookup(ic);
304
305        txl->list = NULL;
306        txl_free(txl);
307}
308
309static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl);
310static void twitter_http_get_users_lookup(struct http_request *req);
311
312static void twitter_get_users_lookup(struct im_connection *ic)
313{
314        struct twitter_data *td = ic->proto_data;
315        char *args[2] = {
316                "user_id",
317                NULL,
318        };
319        GString *ids = g_string_new("");
320        int i;
321       
322        /* We can request up to 100 users at a time. */
323        for (i = 0; i < 100 && td->follow_ids; i ++) {
324                g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data);
325                g_free(td->follow_ids->data);
326                td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data);
327        }
328        if (ids->len > 0) {
329                args[1] = ids->str + 1;
330                /* POST, because I think ids can be up to 1KB long. */
331                twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2);
332        } else {
333                /* We have all users. Continue with login. (Get statuses.) */
334                td->flags |= TWITTER_HAVE_FRIENDS;
335                twitter_login_finish(ic);
336        }
337        g_string_free(ids, TRUE);
338}
339
340/**
341 * Callback for getting (twitter)friends...
342 *
343 * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has
344 * hundreds of friends?" you wonder? You probably not, since you are reading the source of
345 * BitlBee... Get a life and meet new people!
346 */
347static void twitter_http_get_users_lookup(struct http_request *req)
348{
349        struct im_connection *ic = req->data;
350        struct twitter_data *td;
[d0752e8]351        struct xt_node *parsed;
[de923d5]352        struct twitter_xml_list *txl;
353        GSList *l = NULL;
354        struct twitter_xml_user *user;
355
356        // Check if the connection is still active.
357        if (!g_slist_find(twitter_connections, ic))
358                return;
359
360        td = ic->proto_data;
361
362        if (req->status_code != 200) {
363                // It didn't go well, output the error and return.
364                imcb_error(ic, "Could not retrieve %s: %s",
365                           TWITTER_USERS_LOOKUP_URL, twitter_parse_error(req));
366                imc_logout(ic, TRUE);
367                return;
368        } else {
369                td->http_fails = 0;
370        }
371
372        txl = g_new0(struct twitter_xml_list, 1);
373        txl->list = NULL;
[1b221e0]374
[de923d5]375        // Parse the data.
[d0752e8]376        parsed = xt_from_string(req->reply_body, req->body_size);
[de923d5]377
378        // Get the user list from the parsed xml feed.
[d0752e8]379        twitter_xt_get_users(parsed, txl);
380        xt_free_node(parsed);
[de923d5]381
382        // Add the users as buddies.
383        for (l = txl->list; l; l = g_slist_next(l)) {
384                user = l->data;
385                twitter_add_buddy(ic, user->screen_name, user->name);
386        }
387
388        // Free the structure.
[62d2cfb]389        txl_free(txl);
[de923d5]390
391        twitter_get_users_lookup(ic);
[1b221e0]392}
393
394/**
395 * Function to fill a twitter_xml_user struct.
396 * It sets:
397 *  - the name and
398 *  - the screen_name.
399 */
[5983eca]400static xt_status twitter_xt_get_user(struct xt_node *node, struct twitter_xml_user *txu)
[1b221e0]401{
402        struct xt_node *child;
403
404        // Walk over the nodes children.
[5983eca]405        for (child = node->children; child; child = child->next) {
406                if (g_strcasecmp("name", child->name) == 0) {
407                        txu->name = g_memdup(child->text, child->text_len + 1);
408                } else if (g_strcasecmp("screen_name", child->name) == 0) {
409                        txu->screen_name = g_memdup(child->text, child->text_len + 1);
[1b221e0]410                }
411        }
412        return XT_HANDLED;
413}
414
[62d2cfb]415/**
416 * Function to fill a twitter_xml_list struct.
417 * It sets:
418 *  - all <user>s from the <users> element.
419 */
[5983eca]420static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl)
[62d2cfb]421{
422        struct twitter_xml_user *txu;
423        struct xt_node *child;
424
425        // Set the type of the list.
426        txl->type = TXL_USER;
427
428        // The root <users> node should hold the list of users <user>
429        // Walk over the nodes children.
[5983eca]430        for (child = node->children; child; child = child->next) {
431                if (g_strcasecmp("user", child->name) == 0) {
[62d2cfb]432                        txu = g_new0(struct twitter_xml_user, 1);
433                        twitter_xt_get_user(child, txu);
434                        // Put the item in the front of the list.
[5983eca]435                        txl->list = g_slist_prepend(txl->list, txu);
[62d2cfb]436                }
437        }
438
439        return XT_HANDLED;
440}
441
[2b02617]442#ifdef __GLIBC__
443#define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y"
444#else
445#define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y"
446#endif
[62d2cfb]447
[1b221e0]448/**
449 * Function to fill a twitter_xml_status struct.
450 * It sets:
451 *  - the status text and
452 *  - the created_at timestamp and
453 *  - the status id and
454 *  - the user in a twitter_xml_user struct.
455 */
[5983eca]456static xt_status twitter_xt_get_status(struct xt_node *node, struct twitter_xml_status *txs)
[1b221e0]457{
[bd599b9]458        struct xt_node *child, *rt = NULL;
[1b221e0]459
460        // Walk over the nodes children.
[5983eca]461        for (child = node->children; child; child = child->next) {
462                if (g_strcasecmp("text", child->name) == 0) {
463                        txs->text = g_memdup(child->text, child->text_len + 1);
464                } else if (g_strcasecmp("retweeted_status", child->name) == 0) {
[e193aeb]465                        rt = child;
[5983eca]466                } else if (g_strcasecmp("created_at", child->name) == 0) {
[08579a1]467                        struct tm parsed;
[5983eca]468
[08579a1]469                        /* Very sensitive to changes to the formatting of
470                           this field. :-( Also assumes the timezone used
471                           is UTC since C time handling functions suck. */
[5983eca]472                        if (strptime(child->text, TWITTER_TIME_FORMAT, &parsed) != NULL)
473                                txs->created_at = mktime_utc(&parsed);
474                } else if (g_strcasecmp("user", child->name) == 0) {
[1b221e0]475                        txs->user = g_new0(struct twitter_xml_user, 1);
[5983eca]476                        twitter_xt_get_user(child, txs->user);
477                } else if (g_strcasecmp("id", child->name) == 0) {
478                        txs->id = g_ascii_strtoull(child->text, NULL, 10);
479                } else if (g_strcasecmp("in_reply_to_status_id", child->name) == 0) {
480                        txs->reply_to = g_ascii_strtoull(child->text, NULL, 10);
[ce81acd]481                }
[1b221e0]482        }
[5983eca]483
[0fff0b2]484        /* If it's a (truncated) retweet, get the original. Even if the API claims it
485           wasn't truncated because it may be lying. */
486        if (rt) {
[e193aeb]487                struct twitter_xml_status *rtxs = g_new0(struct twitter_xml_status, 1);
[5983eca]488                if (twitter_xt_get_status(rt, rtxs) != XT_HANDLED) {
[e193aeb]489                        txs_free(rtxs);
490                        return XT_HANDLED;
491                }
[5983eca]492
[e193aeb]493                g_free(txs->text);
494                txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text);
495                txs_free(rtxs);
[429a9b1]496        } else {
497                struct xt_node *urls, *url;
498               
[7d2ce9a]499                urls = xt_find_path(node, "entities");
500                if (urls != NULL)
501                        urls = urls->children;
502                for (; urls; urls = urls->next) {
503                        if (strcmp(urls->name, "urls") != 0 && strcmp(urls->name, "media") != 0)
[429a9b1]504                                continue;
505                       
[7d2ce9a]506                        for (url = urls ? urls->children : NULL; url; url = url->next) {
507                                /* "short" is a reserved word. :-P */
508                                struct xt_node *kort = xt_find_node(url->children, "url");
509                                struct xt_node *disp = xt_find_node(url->children, "display_url");
510                                char *pos, *new;
511                               
512                                if (!kort || !kort->text || !disp || !disp->text ||
513                                    !(pos = strstr(txs->text, kort->text)))
514                                        continue;
515                               
516                                *pos = '\0';
517                                new = g_strdup_printf("%s%s &lt;%s&gt;%s", txs->text, kort->text,
518                                                      disp->text, pos + strlen(kort->text));
519                               
520                                g_free(txs->text);
521                                txs->text = new;
522                        }
[429a9b1]523                }
[e193aeb]524        }
[5983eca]525
[1b221e0]526        return XT_HANDLED;
527}
528
529/**
530 * Function to fill a twitter_xml_list struct.
531 * It sets:
532 *  - all <status>es within the <status> element and
533 *  - the next_cursor.
534 */
[5983eca]535static xt_status twitter_xt_get_status_list(struct im_connection *ic, struct xt_node *node,
536                                            struct twitter_xml_list *txl)
[1b221e0]537{
538        struct twitter_xml_status *txs;
539        struct xt_node *child;
[203a2d2]540        bee_user_t *bu;
[1b221e0]541
[62d2cfb]542        // Set the type of the list.
543        txl->type = TXL_STATUS;
544
[1b221e0]545        // The root <statuses> node should hold the list of statuses <status>
546        // Walk over the nodes children.
[5983eca]547        for (child = node->children; child; child = child->next) {
548                if (g_strcasecmp("status", child->name) == 0) {
[1b221e0]549                        txs = g_new0(struct twitter_xml_status, 1);
550                        twitter_xt_get_status(child, txs);
551                        // Put the item in the front of the list.
[5983eca]552                        txl->list = g_slist_prepend(txl->list, txs);
553
[203a2d2]554                        if (txs->user && txs->user->screen_name &&
[5983eca]555                            (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) {
[203a2d2]556                                struct twitter_user_data *tud = bu->data;
[5983eca]557
558                                if (txs->id > tud->last_id) {
[203a2d2]559                                        tud->last_id = txs->id;
560                                        tud->last_time = txs->created_at;
561                                }
562                        }
[5983eca]563                } else if (g_strcasecmp("next_cursor", child->name) == 0) {
[1b221e0]564                        twitter_xt_next_cursor(child, txl);
565                }
566        }
567
568        return XT_HANDLED;
569}
570
[ce81acd]571static char *twitter_msg_add_id(struct im_connection *ic,
[5983eca]572                                struct twitter_xml_status *txs, const char *prefix)
[ce81acd]573{
574        struct twitter_data *td = ic->proto_data;
575        char *ret = NULL;
[5983eca]576
577        if (!set_getbool(&ic->acc->set, "show_ids")) {
[ce81acd]578                if (*prefix)
579                        return g_strconcat(prefix, txs->text, NULL);
580                else
581                        return NULL;
582        }
[5983eca]583
[ce81acd]584        td->log[td->log_id].id = txs->id;
585        td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name);
[5983eca]586        if (txs->reply_to) {
[ce81acd]587                int i;
[5983eca]588                for (i = 0; i < TWITTER_LOG_LENGTH; i++)
589                        if (td->log[i].id == txs->reply_to) {
590                                ret = g_strdup_printf("\002[\002%02d->%02d\002]\002 %s%s",
591                                                      td->log_id, i, prefix, txs->text);
[ce81acd]592                                break;
593                        }
594        }
595        if (ret == NULL)
[5983eca]596                ret = g_strdup_printf("\002[\002%02d\002]\002 %s%s", td->log_id, prefix, txs->text);
[ce81acd]597        td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH;
[5983eca]598
[ce81acd]599        return ret;
600}
601
[d6aa6dd]602static void twitter_groupchat_init(struct im_connection *ic)
603{
604        char *name_hint;
605        struct groupchat *gc;
606        struct twitter_data *td = ic->proto_data;
[fd65edb]607        GSList *l;
[5983eca]608
[2322a9f]609        td->timeline_gc = gc = imcb_chat_new(ic, "twitter/timeline");
[5983eca]610
611        name_hint = g_strdup_printf("%s_%s", td->prefix, ic->acc->user);
612        imcb_chat_name_hint(gc, name_hint);
613        g_free(name_hint);
614
615        for (l = ic->bee->users; l; l = l->next) {
[fd65edb]616                bee_user_t *bu = l->data;
[5983eca]617                if (bu->ic == ic)
[2322a9f]618                        imcb_chat_add_buddy(td->timeline_gc, bu->handle);
[fd65edb]619        }
[d6aa6dd]620}
621
[62d2cfb]622/**
623 * Function that is called to see the statuses in a groupchat window.
624 */
[5983eca]625static void twitter_groupchat(struct im_connection *ic, GSList * list)
[62d2cfb]626{
627        struct twitter_data *td = ic->proto_data;
628        GSList *l = NULL;
629        struct twitter_xml_status *status;
630        struct groupchat *gc;
[2322a9f]631        guint64 last_id = 0;
[62d2cfb]632
633        // Create a new groupchat if it does not exsist.
[2322a9f]634        if (!td->timeline_gc)
[d6aa6dd]635                twitter_groupchat_init(ic);
[5983eca]636
[2322a9f]637        gc = td->timeline_gc;
[d6aa6dd]638        if (!gc->joined)
[5983eca]639                imcb_chat_add_buddy(gc, ic->acc->user);
[62d2cfb]640
[5983eca]641        for (l = list; l; l = g_slist_next(l)) {
[ce81acd]642                char *msg;
[5983eca]643
[62d2cfb]644                status = l->data;
[2322a9f]645                if (status->user == NULL || status->text == NULL || last_id == status->id)
[a26af5c]646                        continue;
647
[2322a9f]648                last_id = status->id;
[5983eca]649
[0b3ffb1]650                strip_html(status->text);
[2322a9f]651
[948abab]652                if (set_getbool(&ic->acc->set, "strip_newlines"))
[cf0dbdd]653                        strip_newlines(status->text);
[948abab]654
[ce81acd]655                msg = twitter_msg_add_id(ic, status, "");
[5983eca]656
[62d2cfb]657                // Say it!
[2322a9f]658                if (g_strcasecmp(td->user, status->user->screen_name) == 0) {
[ce81acd]659                        imcb_chat_log(gc, "You: %s", msg ? msg : status->text);
[2322a9f]660                } else {
661                        twitter_add_buddy(ic, status->user->screen_name, status->user->name);
662
[ce81acd]663                        imcb_chat_msg(gc, status->user->screen_name,
[5983eca]664                                      msg ? msg : status->text, 0, status->created_at);
[2322a9f]665                }
[5983eca]666
[ce81acd]667                g_free(msg);
[5983eca]668
[2322a9f]669                // Update the timeline_id to hold the highest id, so that by the next request
[ce81acd]670                // we won't pick up the updates already in the list.
[2322a9f]671                td->timeline_id = MAX(td->timeline_id, status->id);
[62d2cfb]672        }
673}
674
675/**
676 * Function that is called to see statuses as private messages.
677 */
[5983eca]678static void twitter_private_message_chat(struct im_connection *ic, GSList * list)
[62d2cfb]679{
680        struct twitter_data *td = ic->proto_data;
681        GSList *l = NULL;
682        struct twitter_xml_status *status;
[e88fbe27]683        char from[MAX_STRING];
684        gboolean mode_one;
[2322a9f]685        guint64 last_id = 0;
[62d2cfb]686
[5983eca]687        mode_one = g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") == 0;
688
689        if (mode_one) {
690                g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user);
691                from[MAX_STRING - 1] = '\0';
[e88fbe27]692        }
[5983eca]693
694        for (l = list; l; l = g_slist_next(l)) {
[ce81acd]695                char *prefix = NULL, *text = NULL;
[5983eca]696
[62d2cfb]697                status = l->data;
[2322a9f]698                if (status->user == NULL || status->text == NULL || last_id == status->id)
699                        continue;
700
701                last_id = status->id;
[5983eca]702
703                strip_html(status->text);
704                if (mode_one)
[ce81acd]705                        prefix = g_strdup_printf("\002<\002%s\002>\002 ",
[5983eca]706                                                 status->user->screen_name);
[55b1e69]707                else
708                        twitter_add_buddy(ic, status->user->screen_name, status->user->name);
[5983eca]709
[ce81acd]710                text = twitter_msg_add_id(ic, status, prefix ? prefix : "");
[5983eca]711
712                imcb_buddy_msg(ic,
713                               mode_one ? from : status->user->screen_name,
714                               text ? text : status->text, 0, status->created_at);
715
[2322a9f]716                // Update the timeline_id to hold the highest id, so that by the next request
[ce81acd]717                // we won't pick up the updates already in the list.
[2322a9f]718                td->timeline_id = MAX(td->timeline_id, status->id);
[5983eca]719
720                g_free(text);
721                g_free(prefix);
[62d2cfb]722        }
723}
724
[2322a9f]725static void twitter_http_get_home_timeline(struct http_request *req);
726static void twitter_http_get_mentions(struct http_request *req);
727
728/**
729 * Get the timeline with optionally mentions
730 */
731void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor)
732{
733        struct twitter_data *td = ic->proto_data;
734        gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions");
735
736        if (td->flags & TWITTER_DOING_TIMELINE) {
737                return;
738        }
739
740        td->flags |= TWITTER_DOING_TIMELINE;
741
742        twitter_get_home_timeline(ic, next_cursor);
743
744        if (include_mentions) {
745                twitter_get_mentions(ic, next_cursor);
746        }
747}
748
749/**
750 * Call this one after receiving timeline/mentions. Show to user once we have
751 * both.
752 */
753void twitter_flush_timeline(struct im_connection *ic)
754{
755        struct twitter_data *td = ic->proto_data;
756        gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions");
[b5fe39b]757        int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions");
[2322a9f]758        struct twitter_xml_list *home_timeline = td->home_timeline_obj;
759        struct twitter_xml_list *mentions = td->mentions_obj;
760        GSList *output = NULL;
761        GSList *l;
762
763        if (!(td->flags & TWITTER_GOT_TIMELINE)) {
764                return;
765        }
766
767        if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) {
768                return;
769        }
770
771        if (home_timeline && home_timeline->list) {
772                for (l = home_timeline->list; l; l = g_slist_next(l)) {
773                        output = g_slist_insert_sorted(output, l->data, twitter_compare_elements);
774                }
775        }
776
777        if (include_mentions && mentions && mentions->list) {
778                for (l = mentions->list; l; l = g_slist_next(l)) {
[b5fe39b]779                        if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) {
[2322a9f]780                                continue;
781                        }
782
783                        output = g_slist_insert_sorted(output, l->data, twitter_compare_elements);
784                }
785        }
786
787        // See if the user wants to see the messages in a groupchat window or as private messages.
788        if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0)
789                twitter_groupchat(ic, output);
790        else
791                twitter_private_message_chat(ic, output);
792
793        g_slist_free(output);
794
[199fea6]795        txl_free(home_timeline);
796        txl_free(mentions);
[2322a9f]797
798        td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS);
[199fea6]799        td->home_timeline_obj = td->mentions_obj = NULL;
[2322a9f]800}
801
802/**
803 * Get the timeline.
804 */
805void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor)
806{
807        struct twitter_data *td = ic->proto_data;
808
[199fea6]809        txl_free(td->home_timeline_obj);
[2322a9f]810        td->home_timeline_obj = NULL;
811        td->flags &= ~TWITTER_GOT_TIMELINE;
812
[429a9b1]813        char *args[6];
[2322a9f]814        args[0] = "cursor";
815        args[1] = g_strdup_printf("%lld", (long long) next_cursor);
[429a9b1]816        args[2] = "include_entities";
817        args[3] = "true";
[2322a9f]818        if (td->timeline_id) {
[429a9b1]819                args[4] = "since_id";
820                args[5] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id);
[2322a9f]821        }
822
[90fc864]823        if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args,
824                     td->timeline_id ? 6 : 4) == NULL) {
825                if (++td->http_fails >= 5)
826                        imcb_error(ic, "Could not retrieve %s: %s",
827                                   TWITTER_HOME_TIMELINE_URL, "connection failed");
828                td->flags |= TWITTER_GOT_TIMELINE;
829                twitter_flush_timeline(ic);
830        }
[2322a9f]831
832        g_free(args[1]);
833        if (td->timeline_id) {
[429a9b1]834                g_free(args[5]);
[2322a9f]835        }
836}
837
838/**
839 * Get mentions.
840 */
841void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor)
842{
843        struct twitter_data *td = ic->proto_data;
844
[199fea6]845        txl_free(td->mentions_obj);
[2322a9f]846        td->mentions_obj = NULL;
847        td->flags &= ~TWITTER_GOT_MENTIONS;
848
[429a9b1]849        char *args[6];
[2322a9f]850        args[0] = "cursor";
851        args[1] = g_strdup_printf("%lld", (long long) next_cursor);
[429a9b1]852        args[2] = "include_entities";
853        args[3] = "true";
[2322a9f]854        if (td->timeline_id) {
[429a9b1]855                args[4] = "since_id";
856                args[5] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id);
[b5fe39b]857        } else {
858                args[4] = "count";
859                args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions"));
[2322a9f]860        }
861
[b5fe39b]862        if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions,
863                         ic, 0, args, 6) == NULL) {
[90fc864]864                if (++td->http_fails >= 5)
865                        imcb_error(ic, "Could not retrieve %s: %s",
866                                   TWITTER_MENTIONS_URL, "connection failed");
867                td->flags |= TWITTER_GOT_MENTIONS;
868                twitter_flush_timeline(ic);
869        }
[2322a9f]870
871        g_free(args[1]);
872        if (td->timeline_id) {
[429a9b1]873                g_free(args[5]);
[2322a9f]874        }
875}
876
[1b221e0]877/**
878 * Callback for getting the home timeline.
879 */
880static void twitter_http_get_home_timeline(struct http_request *req)
881{
[62d2cfb]882        struct im_connection *ic = req->data;
[37aa317]883        struct twitter_data *td;
[d0752e8]884        struct xt_node *parsed;
[1b221e0]885        struct twitter_xml_list *txl;
[62d2cfb]886
887        // Check if the connection is still active.
[5983eca]888        if (!g_slist_find(twitter_connections, ic))
[62d2cfb]889                return;
[5983eca]890
[37aa317]891        td = ic->proto_data;
[1b221e0]892
893        // Check if the HTTP request went well.
[5983eca]894        if (req->status_code == 200) {
[3bd4a93]895                td->http_fails = 0;
[c2ecadc]896                if (!(ic->flags & OPT_LOGGED_IN))
[3bd4a93]897                        imcb_connected(ic);
[5983eca]898        } else {
[1b221e0]899                // It didn't go well, output the error and return.
[3bd4a93]900                if (++td->http_fails >= 5)
[de923d5]901                        imcb_error(ic, "Could not retrieve %s: %s",
902                                   TWITTER_HOME_TIMELINE_URL, twitter_parse_error(req));
[5983eca]903
[2322a9f]904                goto end;
905        }
906
907        txl = g_new0(struct twitter_xml_list, 1);
908        txl->list = NULL;
909
910        // Parse the data.
[d0752e8]911        parsed = xt_from_string(req->reply_body, req->body_size);
[2322a9f]912        // The root <statuses> node should hold the list of statuses <status>
[d0752e8]913        twitter_xt_get_status_list(ic, parsed, txl);
914        xt_free_node(parsed);
[2322a9f]915
916        td->home_timeline_obj = txl;
917
918      end:
919        td->flags |= TWITTER_GOT_TIMELINE;
920
921        twitter_flush_timeline(ic);
922}
923
924/**
925 * Callback for getting mentions.
926 */
927static void twitter_http_get_mentions(struct http_request *req)
928{
929        struct im_connection *ic = req->data;
930        struct twitter_data *td;
[d0752e8]931        struct xt_node *parsed;
[2322a9f]932        struct twitter_xml_list *txl;
933
934        // Check if the connection is still active.
935        if (!g_slist_find(twitter_connections, ic))
[1b221e0]936                return;
[2322a9f]937
938        td = ic->proto_data;
939
940        // Check if the HTTP request went well.
941        if (req->status_code == 200) {
942                td->http_fails = 0;
943                if (!(ic->flags & OPT_LOGGED_IN))
944                        imcb_connected(ic);
945        } else {
946                // It didn't go well, output the error and return.
947                if (++td->http_fails >= 5)
[90fc864]948                        imcb_error(ic, "Could not retrieve %s: %s",
949                                   TWITTER_MENTIONS_URL, twitter_parse_error(req));
[2322a9f]950
951                goto end;
[1b221e0]952        }
953
954        txl = g_new0(struct twitter_xml_list, 1);
955        txl->list = NULL;
[62d2cfb]956
[1b221e0]957        // Parse the data.
[d0752e8]958        parsed = xt_from_string(req->reply_body, req->body_size);
[1b221e0]959        // The root <statuses> node should hold the list of statuses <status>
[d0752e8]960        twitter_xt_get_status_list(ic, parsed, txl);
961        xt_free_node(parsed);
[1b221e0]962
[2322a9f]963        td->mentions_obj = txl;
[1b221e0]964
[2322a9f]965      end:
966        td->flags |= TWITTER_GOT_MENTIONS;
967
968        twitter_flush_timeline(ic);
[1b221e0]969}
970
971/**
[de923d5]972 * Callback to use after sending a POST request to twitter.
973 * (Generic, used for a few kinds of queries.)
[1b221e0]974 */
[7d53efb]975static void twitter_http_post(struct http_request *req)
[1b221e0]976{
977        struct im_connection *ic = req->data;
[7b87539]978        struct twitter_data *td;
[1b221e0]979
[62d2cfb]980        // Check if the connection is still active.
[5983eca]981        if (!g_slist_find(twitter_connections, ic))
[62d2cfb]982                return;
983
[7b87539]984        td = ic->proto_data;
985        td->last_status_id = 0;
[5983eca]986
[1b221e0]987        // Check if the HTTP request went well.
988        if (req->status_code != 200) {
989                // It didn't go well, output the error and return.
[ba3233c]990                imcb_error(ic, "HTTP error: %s", twitter_parse_error(req));
[1b221e0]991                return;
992        }
[5983eca]993
994        if (req->body_size > 0) {
[d0752e8]995                struct xt_node *parsed, *node;
[5983eca]996
[d0752e8]997                parsed = xt_from_string(req->reply_body, req->body_size);
[5983eca]998
[d0752e8]999                if ((node = xt_find_node(parsed, "status")) &&
[7b87539]1000                    (node = xt_find_node(node->children, "id")) && node->text)
[5983eca]1001                        td->last_status_id = g_ascii_strtoull(node->text, NULL, 10);
1002
[d0752e8]1003                xt_free_node(parsed);
[7b87539]1004        }
[1b221e0]1005}
1006
1007/**
1008 * Function to POST a new status to twitter.
[5983eca]1009 */
[b890626]1010void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to)
[1b221e0]1011{
[5983eca]1012        char *args[4] = {
[b890626]1013                "status", msg,
1014                "in_reply_to_status_id",
1015                g_strdup_printf("%llu", (unsigned long long) in_reply_to)
1016        };
1017        twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1,
[5983eca]1018                     args, in_reply_to ? 4 : 2);
[b890626]1019        g_free(args[3]);
[1b221e0]1020}
1021
1022
[62d2cfb]1023/**
1024 * Function to POST a new message to twitter.
1025 */
1026void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg)
1027{
[5983eca]1028        char *args[4];
[62d2cfb]1029        args[0] = "screen_name";
1030        args[1] = who;
1031        args[2] = "text";
1032        args[3] = msg;
1033        // Use the same callback as for twitter_post_status, since it does basically the same.
[ba3233c]1034        twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4);
[62d2cfb]1035}
[7d53efb]1036
1037void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create)
1038{
[5983eca]1039        char *args[2];
[7d53efb]1040        args[0] = "screen_name";
1041        args[1] = who;
[5983eca]1042        twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL,
1043                     twitter_http_post, ic, 1, args, 2);
[a26af5c]1044}
[7b87539]1045
1046void twitter_status_destroy(struct im_connection *ic, guint64 id)
1047{
1048        char *url;
[de923d5]1049        url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_DESTROY_URL,
1050                              (unsigned long long) id, ".xml");
[7b87539]1051        twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0);
1052        g_free(url);
1053}
[b890626]1054
1055void twitter_status_retweet(struct im_connection *ic, guint64 id)
1056{
1057        char *url;
[de923d5]1058        url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_RETWEET_URL,
1059                              (unsigned long long) id, ".xml");
[b890626]1060        twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0);
1061        g_free(url);
1062}
[d18dee42]1063
1064/**
1065 * Report a user for sending spam.
1066 */
1067void twitter_report_spam(struct im_connection *ic, char *screen_name)
1068{
1069        char *args[2] = {
1070                "screen_name",
1071                NULL,
1072        };
1073        args[1] = screen_name;
1074        twitter_http(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post,
1075                     ic, 1, args, 2);
1076}
[b61c74c]1077
1078/**
1079 * Favourite a tweet.
1080 */
1081void twitter_favourite_tweet(struct im_connection *ic, guint64 id)
1082{
1083        char *url;
1084        url = g_strdup_printf("%s%llu%s", TWITTER_FAVORITE_CREATE_URL,
1085                              (unsigned long long) id, ".xml");
1086        twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0);
1087        g_free(url);
1088}
Note: See TracBrowser for help on using the repository browser.