source: protocols/twitter/twitter_lib.c @ c0f33f1

Last change on this file since c0f33f1 was c0f33f1, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-06-11T17:50:26Z

Change the default base_url to something that works. Change the default for
identi.ca to HTTPS while I'm at it. Pretty important since I can't use OAuth
for it yet.

  • Property mode set to 100644
File size: 23.2 KB
Line 
1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Simple module to facilitate twitter functionality.                       *
5*                                                                           *
6*  Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com>                 *
7*                                                                           *
8*  This library is free software; you can redistribute it and/or            *
9*  modify it under the terms of the GNU Lesser General Public               *
10*  License as published by the Free Software Foundation, version            *
11*  2.1.                                                                     *
12*                                                                           *
13*  This library is distributed in the hope that it will be useful,          *
14*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
15*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
16*  Lesser General Public License for more details.                          *
17*                                                                           *
18*  You should have received a copy of the GNU Lesser General Public License *
19*  along with this library; if not, write to the Free Software Foundation,  *
20*  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA           *
21*                                                                           *
22****************************************************************************/
23
24/* For strptime(): */
25#if(__sun)
26#else
27#define _XOPEN_SOURCE
28#endif
29
30#include "twitter_http.h"
31#include "twitter.h"
32#include "bitlbee.h"
33#include "url.h"
34#include "misc.h"
35#include "base64.h"
36#include "xmltree.h"
37#include "twitter_lib.h"
38#include <ctype.h>
39#include <errno.h>
40
41/* GLib < 2.12.0 doesn't have g_ascii_strtoll(), work around using system strtoll(). */
42/* GLib < 2.12.4 can be buggy: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=488013 */
43#if !GLIB_CHECK_VERSION(2,12,5)
44#include <stdlib.h>
45#include <limits.h>
46#define g_ascii_strtoll strtoll
47#endif
48
49#define TXL_STATUS 1
50#define TXL_USER 2
51#define TXL_ID 3
52
53struct twitter_xml_list {
54        int type;
55        gint64 next_cursor;
56        GSList *list;
57};
58
59struct twitter_xml_user {
60        char *name;
61        char *screen_name;
62};
63
64struct twitter_xml_status {
65        time_t created_at;
66        char *text;
67        struct twitter_xml_user *user;
68        guint64 id, reply_to;
69};
70
71static void twitter_groupchat_init(struct im_connection *ic);
72
73/**
74 * Frees a twitter_xml_user struct.
75 */
76static void txu_free(struct twitter_xml_user *txu)
77{
78        if (txu == NULL)
79                return;
80        g_free(txu->name);
81        g_free(txu->screen_name);
82        g_free(txu);
83}
84
85
86/**
87 * Frees a twitter_xml_status struct.
88 */
89static void txs_free(struct twitter_xml_status *txs)
90{
91        g_free(txs->text);
92        txu_free(txs->user);
93        g_free(txs);
94}
95
96/**
97 * Free a twitter_xml_list struct.
98 * type is the type of list the struct holds.
99 */
100static void txl_free(struct twitter_xml_list *txl)
101{
102        GSList *l;
103        if (txl == NULL)
104                return;
105        for (l = txl->list; l; l = g_slist_next(l))
106                if (txl->type == TXL_STATUS)
107                        txs_free((struct twitter_xml_status *) l->data);
108                else if (txl->type == TXL_ID)
109                        g_free(l->data);
110                else if (txl->type == TXL_USER)
111                        txu_free(l->data);
112        g_slist_free(txl->list);
113        g_free(txl);
114}
115
116/**
117 * Add a buddy if it is not allready added, set the status to logged in.
118 */
119static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname)
120{
121        struct twitter_data *td = ic->proto_data;
122
123        // Check if the buddy is already in the buddy list.
124        if (!bee_user_by_handle(ic->bee, ic, name)) {
125                char *mode = set_getstr(&ic->acc->set, "mode");
126
127                // The buddy is not in the list, add the buddy and set the status to logged in.
128                imcb_add_buddy(ic, name, NULL);
129                imcb_rename_buddy(ic, name, fullname);
130                if (g_strcasecmp(mode, "chat") == 0) {
131                        /* Necessary so that nicks always get translated to the
132                           exact Twitter username. */
133                        imcb_buddy_nick_hint(ic, name, name);
134                        imcb_chat_add_buddy(td->home_timeline_gc, name);
135                } else if (g_strcasecmp(mode, "many") == 0)
136                        imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL);
137        }
138}
139
140/* Warning: May return a malloc()ed value, which will be free()d on the next
141   call. Only for short-term use. NOT THREADSAFE!  */
142char *twitter_parse_error(struct http_request *req)
143{
144        static char *ret = NULL;
145        struct xt_parser *xp = NULL;
146        struct xt_node *node, *err;
147
148        g_free(ret);
149        ret = NULL;
150
151        if (req->body_size > 0) {
152                xp = xt_new(NULL, NULL);
153                xt_feed(xp, req->reply_body, req->body_size);
154               
155                for (node = xp->root; node; node = node->next)
156                        if ((err = xt_find_node(node->children, "error")) && err->text_len > 0) {
157                                ret = g_strdup_printf("%s (%s)", req->status_string, err->text);
158                                break;
159                        }
160
161                xt_free(xp);
162        }
163
164        return ret ? ret : req->status_string;
165}
166
167static void twitter_http_get_friends_ids(struct http_request *req);
168
169/**
170 * Get the friends ids.
171 */
172void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor)
173{
174        // Primitive, but hey! It works...     
175        char *args[2];
176        args[0] = "cursor";
177        args[1] = g_strdup_printf("%lld", (long long) next_cursor);
178        twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2);
179
180        g_free(args[1]);
181}
182
183/**
184 * Function to help fill a list.
185 */
186static xt_status twitter_xt_next_cursor(struct xt_node *node, struct twitter_xml_list *txl)
187{
188        char *end = NULL;
189
190        if (node->text)
191                txl->next_cursor = g_ascii_strtoll(node->text, &end, 10);
192        if (end == NULL)
193                txl->next_cursor = -1;
194
195        return XT_HANDLED;
196}
197
198/**
199 * Fill a list of ids.
200 */
201static xt_status twitter_xt_get_friends_id_list(struct xt_node *node, struct twitter_xml_list *txl)
202{
203        struct xt_node *child;
204
205        // Set the list type.
206        txl->type = TXL_ID;
207
208        // The root <statuses> node should hold the list of statuses <status>
209        // Walk over the nodes children.
210        for (child = node->children; child; child = child->next) {
211                if (g_strcasecmp("ids", child->name) == 0) {
212                        struct xt_node *idc;
213                        for (idc = child->children; idc; idc = idc->next)
214                                if (g_strcasecmp(idc->name, "id") == 0)
215                                        txl->list = g_slist_prepend(txl->list,
216                                                g_memdup(idc->text, idc->text_len + 1));
217                } else if (g_strcasecmp("next_cursor", child->name) == 0) {
218                        twitter_xt_next_cursor(child, txl);
219                }
220        }
221
222        return XT_HANDLED;
223}
224
225static void twitter_get_users_lookup(struct im_connection *ic);
226
227/**
228 * Callback for getting the friends ids.
229 */
230static void twitter_http_get_friends_ids(struct http_request *req)
231{
232        struct im_connection *ic;
233        struct xt_parser *parser;
234        struct twitter_xml_list *txl;
235        struct twitter_data *td;
236
237        ic = req->data;
238
239        // Check if the connection is still active.
240        if (!g_slist_find(twitter_connections, ic))
241                return;
242
243        td = ic->proto_data;
244
245        // Check if the HTTP request went well. More strict checks as this is
246        // the first request we do in a session.
247        if (req->status_code == 401) {
248                imcb_error(ic, "Authentication failure");
249                imc_logout(ic, FALSE);
250                return;
251        } else if (req->status_code != 200) {
252                // It didn't go well, output the error and return.
253                imcb_error(ic, "Could not retrieve %s: %s",
254                           TWITTER_FRIENDS_IDS_URL, twitter_parse_error(req));
255                imc_logout(ic, TRUE);
256                return;
257        } else {
258                td->http_fails = 0;
259        }
260
261        /* Create the room now that we "logged in". */
262        if (!td->home_timeline_gc && g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0)
263                twitter_groupchat_init(ic);
264
265        txl = g_new0(struct twitter_xml_list, 1);
266        txl->list = td->follow_ids;
267
268        // Parse the data.
269        parser = xt_new(NULL, txl);
270        xt_feed(parser, req->reply_body, req->body_size);
271        twitter_xt_get_friends_id_list(parser->root, txl);
272        xt_free(parser);
273
274        td->follow_ids = txl->list;
275        if (txl->next_cursor)
276                /* These were just numbers. Up to 4000 in a response AFAIK so if we get here
277                   we may be using a spammer account. \o/ */
278                twitter_get_friends_ids(ic, txl->next_cursor);
279        else
280                /* Now to convert all those numbers into names.. */
281                twitter_get_users_lookup(ic);
282
283        txl->list = NULL;
284        txl_free(txl);
285}
286
287static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl);
288static void twitter_http_get_users_lookup(struct http_request *req);
289
290static void twitter_get_users_lookup(struct im_connection *ic)
291{
292        struct twitter_data *td = ic->proto_data;
293        char *args[2] = {
294                "user_id",
295                NULL,
296        };
297        GString *ids = g_string_new("");
298        int i;
299       
300        /* We can request up to 100 users at a time. */
301        for (i = 0; i < 100 && td->follow_ids; i ++) {
302                g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data);
303                g_free(td->follow_ids->data);
304                td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data);
305        }
306        if (ids->len > 0) {
307                args[1] = ids->str + 1;
308                /* POST, because I think ids can be up to 1KB long. */
309                twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2);
310        } else {
311                /* We have all users. Continue with login. (Get statuses.) */
312                td->flags |= TWITTER_HAVE_FRIENDS;
313                twitter_login_finish(ic);
314        }
315        g_string_free(ids, TRUE);
316}
317
318/**
319 * Callback for getting (twitter)friends...
320 *
321 * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has
322 * hundreds of friends?" you wonder? You probably not, since you are reading the source of
323 * BitlBee... Get a life and meet new people!
324 */
325static void twitter_http_get_users_lookup(struct http_request *req)
326{
327        struct im_connection *ic = req->data;
328        struct twitter_data *td;
329        struct xt_parser *parser;
330        struct twitter_xml_list *txl;
331        GSList *l = NULL;
332        struct twitter_xml_user *user;
333
334        // Check if the connection is still active.
335        if (!g_slist_find(twitter_connections, ic))
336                return;
337
338        td = ic->proto_data;
339
340        if (req->status_code != 200) {
341                // It didn't go well, output the error and return.
342                imcb_error(ic, "Could not retrieve %s: %s",
343                           TWITTER_USERS_LOOKUP_URL, twitter_parse_error(req));
344                imc_logout(ic, TRUE);
345                return;
346        } else {
347                td->http_fails = 0;
348        }
349
350        txl = g_new0(struct twitter_xml_list, 1);
351        txl->list = NULL;
352
353        // Parse the data.
354        parser = xt_new(NULL, txl);
355        xt_feed(parser, req->reply_body, req->body_size);
356
357        // Get the user list from the parsed xml feed.
358        twitter_xt_get_users(parser->root, txl);
359        xt_free(parser);
360
361        // Add the users as buddies.
362        for (l = txl->list; l; l = g_slist_next(l)) {
363                user = l->data;
364                twitter_add_buddy(ic, user->screen_name, user->name);
365        }
366
367        // Free the structure.
368        txl_free(txl);
369
370        twitter_get_users_lookup(ic);
371}
372
373/**
374 * Function to fill a twitter_xml_user struct.
375 * It sets:
376 *  - the name and
377 *  - the screen_name.
378 */
379static xt_status twitter_xt_get_user(struct xt_node *node, struct twitter_xml_user *txu)
380{
381        struct xt_node *child;
382
383        // Walk over the nodes children.
384        for (child = node->children; child; child = child->next) {
385                if (g_strcasecmp("name", child->name) == 0) {
386                        txu->name = g_memdup(child->text, child->text_len + 1);
387                } else if (g_strcasecmp("screen_name", child->name) == 0) {
388                        txu->screen_name = g_memdup(child->text, child->text_len + 1);
389                }
390        }
391        return XT_HANDLED;
392}
393
394/**
395 * Function to fill a twitter_xml_list struct.
396 * It sets:
397 *  - all <user>s from the <users> element.
398 */
399static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl)
400{
401        struct twitter_xml_user *txu;
402        struct xt_node *child;
403
404        // Set the type of the list.
405        txl->type = TXL_USER;
406
407        // The root <users> node should hold the list of users <user>
408        // Walk over the nodes children.
409        for (child = node->children; child; child = child->next) {
410                if (g_strcasecmp("user", child->name) == 0) {
411                        txu = g_new0(struct twitter_xml_user, 1);
412                        twitter_xt_get_user(child, txu);
413                        // Put the item in the front of the list.
414                        txl->list = g_slist_prepend(txl->list, txu);
415                }
416        }
417
418        return XT_HANDLED;
419}
420
421#ifdef __GLIBC__
422#define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y"
423#else
424#define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y"
425#endif
426
427/**
428 * Function to fill a twitter_xml_status struct.
429 * It sets:
430 *  - the status text and
431 *  - the created_at timestamp and
432 *  - the status id and
433 *  - the user in a twitter_xml_user struct.
434 */
435static xt_status twitter_xt_get_status(struct xt_node *node, struct twitter_xml_status *txs)
436{
437        struct xt_node *child, *rt = NULL;
438        gboolean truncated = FALSE;
439
440        // Walk over the nodes children.
441        for (child = node->children; child; child = child->next) {
442                if (g_strcasecmp("text", child->name) == 0) {
443                        txs->text = g_memdup(child->text, child->text_len + 1);
444                } else if (g_strcasecmp("truncated", child->name) == 0 && child->text) {
445                        truncated = bool2int(child->text);
446                } else if (g_strcasecmp("retweeted_status", child->name) == 0) {
447                        rt = child;
448                } else if (g_strcasecmp("created_at", child->name) == 0) {
449                        struct tm parsed;
450
451                        /* Very sensitive to changes to the formatting of
452                           this field. :-( Also assumes the timezone used
453                           is UTC since C time handling functions suck. */
454                        if (strptime(child->text, TWITTER_TIME_FORMAT, &parsed) != NULL)
455                                txs->created_at = mktime_utc(&parsed);
456                } else if (g_strcasecmp("user", child->name) == 0) {
457                        txs->user = g_new0(struct twitter_xml_user, 1);
458                        twitter_xt_get_user(child, txs->user);
459                } else if (g_strcasecmp("id", child->name) == 0) {
460                        txs->id = g_ascii_strtoull(child->text, NULL, 10);
461                } else if (g_strcasecmp("in_reply_to_status_id", child->name) == 0) {
462                        txs->reply_to = g_ascii_strtoull(child->text, NULL, 10);
463                }
464        }
465
466        /* If it's a truncated retweet, get the original because dots suck. */
467        if (truncated && rt) {
468                struct twitter_xml_status *rtxs = g_new0(struct twitter_xml_status, 1);
469                if (twitter_xt_get_status(rt, rtxs) != XT_HANDLED) {
470                        txs_free(rtxs);
471                        return XT_HANDLED;
472                }
473
474                g_free(txs->text);
475                txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text);
476                txs_free(rtxs);
477        }
478
479        return XT_HANDLED;
480}
481
482/**
483 * Function to fill a twitter_xml_list struct.
484 * It sets:
485 *  - all <status>es within the <status> element and
486 *  - the next_cursor.
487 */
488static xt_status twitter_xt_get_status_list(struct im_connection *ic, struct xt_node *node,
489                                            struct twitter_xml_list *txl)
490{
491        struct twitter_xml_status *txs;
492        struct xt_node *child;
493        bee_user_t *bu;
494
495        // Set the type of the list.
496        txl->type = TXL_STATUS;
497
498        // The root <statuses> node should hold the list of statuses <status>
499        // Walk over the nodes children.
500        for (child = node->children; child; child = child->next) {
501                if (g_strcasecmp("status", child->name) == 0) {
502                        txs = g_new0(struct twitter_xml_status, 1);
503                        twitter_xt_get_status(child, txs);
504                        // Put the item in the front of the list.
505                        txl->list = g_slist_prepend(txl->list, txs);
506
507                        if (txs->user && txs->user->screen_name &&
508                            (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) {
509                                struct twitter_user_data *tud = bu->data;
510
511                                if (txs->id > tud->last_id) {
512                                        tud->last_id = txs->id;
513                                        tud->last_time = txs->created_at;
514                                }
515                        }
516                } else if (g_strcasecmp("next_cursor", child->name) == 0) {
517                        twitter_xt_next_cursor(child, txl);
518                }
519        }
520
521        return XT_HANDLED;
522}
523
524static void twitter_http_get_home_timeline(struct http_request *req);
525
526/**
527 * Get the timeline.
528 */
529void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor)
530{
531        struct twitter_data *td = ic->proto_data;
532
533        char *args[4];
534        args[0] = "cursor";
535        args[1] = g_strdup_printf("%lld", (long long) next_cursor);
536        if (td->home_timeline_id) {
537                args[2] = "since_id";
538                args[3] = g_strdup_printf("%llu", (long long unsigned int) td->home_timeline_id);
539        }
540
541        twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args,
542                     td->home_timeline_id ? 4 : 2);
543
544        g_free(args[1]);
545        if (td->home_timeline_id) {
546                g_free(args[3]);
547        }
548}
549
550static char *twitter_msg_add_id(struct im_connection *ic,
551                                struct twitter_xml_status *txs, const char *prefix)
552{
553        struct twitter_data *td = ic->proto_data;
554        char *ret = NULL;
555
556        if (!set_getbool(&ic->acc->set, "show_ids")) {
557                if (*prefix)
558                        return g_strconcat(prefix, txs->text, NULL);
559                else
560                        return NULL;
561        }
562
563        td->log[td->log_id].id = txs->id;
564        td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name);
565        if (txs->reply_to) {
566                int i;
567                for (i = 0; i < TWITTER_LOG_LENGTH; i++)
568                        if (td->log[i].id == txs->reply_to) {
569                                ret = g_strdup_printf("\002[\002%02d->%02d\002]\002 %s%s",
570                                                      td->log_id, i, prefix, txs->text);
571                                break;
572                        }
573        }
574        if (ret == NULL)
575                ret = g_strdup_printf("\002[\002%02d\002]\002 %s%s", td->log_id, prefix, txs->text);
576        td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH;
577
578        return ret;
579}
580
581static void twitter_groupchat_init(struct im_connection *ic)
582{
583        char *name_hint;
584        struct groupchat *gc;
585        struct twitter_data *td = ic->proto_data;
586        GSList *l;
587
588        td->home_timeline_gc = gc = imcb_chat_new(ic, "home/timeline");
589
590        name_hint = g_strdup_printf("%s_%s", td->prefix, ic->acc->user);
591        imcb_chat_name_hint(gc, name_hint);
592        g_free(name_hint);
593
594        for (l = ic->bee->users; l; l = l->next) {
595                bee_user_t *bu = l->data;
596                if (bu->ic == ic)
597                        imcb_chat_add_buddy(td->home_timeline_gc, bu->handle);
598        }
599}
600
601/**
602 * Function that is called to see the statuses in a groupchat window.
603 */
604static void twitter_groupchat(struct im_connection *ic, GSList * list)
605{
606        struct twitter_data *td = ic->proto_data;
607        GSList *l = NULL;
608        struct twitter_xml_status *status;
609        struct groupchat *gc;
610
611        // Create a new groupchat if it does not exsist.
612        if (!td->home_timeline_gc)
613                twitter_groupchat_init(ic);
614
615        gc = td->home_timeline_gc;
616        if (!gc->joined)
617                imcb_chat_add_buddy(gc, ic->acc->user);
618
619        for (l = list; l; l = g_slist_next(l)) {
620                char *msg;
621
622                status = l->data;
623                if (status->user == NULL || status->text == NULL)
624                        continue;
625
626                twitter_add_buddy(ic, status->user->screen_name, status->user->name);
627
628                strip_html(status->text);
629                msg = twitter_msg_add_id(ic, status, "");
630
631                // Say it!
632                if (g_strcasecmp(td->user, status->user->screen_name) == 0)
633                        imcb_chat_log(gc, "You: %s", msg ? msg : status->text);
634                else
635                        imcb_chat_msg(gc, status->user->screen_name,
636                                      msg ? msg : status->text, 0, status->created_at);
637
638                g_free(msg);
639
640                // Update the home_timeline_id to hold the highest id, so that by the next request
641                // we won't pick up the updates already in the list.
642                td->home_timeline_id = MAX(td->home_timeline_id, status->id);
643        }
644}
645
646/**
647 * Function that is called to see statuses as private messages.
648 */
649static void twitter_private_message_chat(struct im_connection *ic, GSList * list)
650{
651        struct twitter_data *td = ic->proto_data;
652        GSList *l = NULL;
653        struct twitter_xml_status *status;
654        char from[MAX_STRING];
655        gboolean mode_one;
656
657        mode_one = g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") == 0;
658
659        if (mode_one) {
660                g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user);
661                from[MAX_STRING - 1] = '\0';
662        }
663
664        for (l = list; l; l = g_slist_next(l)) {
665                char *prefix = NULL, *text = NULL;
666
667                status = l->data;
668
669                strip_html(status->text);
670                if (mode_one)
671                        prefix = g_strdup_printf("\002<\002%s\002>\002 ",
672                                                 status->user->screen_name);
673                else
674                        twitter_add_buddy(ic, status->user->screen_name, status->user->name);
675
676                text = twitter_msg_add_id(ic, status, prefix ? prefix : "");
677
678                imcb_buddy_msg(ic,
679                               mode_one ? from : status->user->screen_name,
680                               text ? text : status->text, 0, status->created_at);
681
682                // Update the home_timeline_id to hold the highest id, so that by the next request
683                // we won't pick up the updates already in the list.
684                td->home_timeline_id = MAX(td->home_timeline_id, status->id);
685
686                g_free(text);
687                g_free(prefix);
688        }
689}
690
691/**
692 * Callback for getting the home timeline.
693 */
694static void twitter_http_get_home_timeline(struct http_request *req)
695{
696        struct im_connection *ic = req->data;
697        struct twitter_data *td;
698        struct xt_parser *parser;
699        struct twitter_xml_list *txl;
700
701        // Check if the connection is still active.
702        if (!g_slist_find(twitter_connections, ic))
703                return;
704
705        td = ic->proto_data;
706
707        // Check if the HTTP request went well.
708        if (req->status_code == 200) {
709                td->http_fails = 0;
710                if (!(ic->flags & OPT_LOGGED_IN))
711                        imcb_connected(ic);
712        } else if (req->status_code == 401) {
713                imcb_error(ic, "Authentication failure");
714                imc_logout(ic, FALSE);
715                return;
716        } else {
717                // It didn't go well, output the error and return.
718                if (++td->http_fails >= 5)
719                        imcb_error(ic, "Could not retrieve %s: %s",
720                                   TWITTER_HOME_TIMELINE_URL, twitter_parse_error(req));
721
722                return;
723        }
724
725        txl = g_new0(struct twitter_xml_list, 1);
726        txl->list = NULL;
727
728        // Parse the data.
729        parser = xt_new(NULL, txl);
730        xt_feed(parser, req->reply_body, req->body_size);
731        // The root <statuses> node should hold the list of statuses <status>
732        twitter_xt_get_status_list(ic, parser->root, txl);
733        xt_free(parser);
734
735        // See if the user wants to see the messages in a groupchat window or as private messages.
736        if (txl->list == NULL);
737        else if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0)
738                twitter_groupchat(ic, txl->list);
739        else
740                twitter_private_message_chat(ic, txl->list);
741
742        // Free the structure. 
743        txl_free(txl);
744}
745
746/**
747 * Callback to use after sending a POST request to twitter.
748 * (Generic, used for a few kinds of queries.)
749 */
750static void twitter_http_post(struct http_request *req)
751{
752        struct im_connection *ic = req->data;
753        struct twitter_data *td;
754
755        // Check if the connection is still active.
756        if (!g_slist_find(twitter_connections, ic))
757                return;
758
759        td = ic->proto_data;
760        td->last_status_id = 0;
761
762        // Check if the HTTP request went well.
763        if (req->status_code != 200) {
764                // It didn't go well, output the error and return.
765                imcb_error(ic, "HTTP error: %s", twitter_parse_error(req));
766                return;
767        }
768
769        if (req->body_size > 0) {
770                struct xt_parser *xp = NULL;
771                struct xt_node *node;
772
773                xp = xt_new(NULL, NULL);
774                xt_feed(xp, req->reply_body, req->body_size);
775
776                if ((node = xt_find_node(xp->root, "status")) &&
777                    (node = xt_find_node(node->children, "id")) && node->text)
778                        td->last_status_id = g_ascii_strtoull(node->text, NULL, 10);
779
780                xt_free(xp);
781        }
782}
783
784/**
785 * Function to POST a new status to twitter.
786 */
787void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to)
788{
789        char *args[4] = {
790                "status", msg,
791                "in_reply_to_status_id",
792                g_strdup_printf("%llu", (unsigned long long) in_reply_to)
793        };
794        twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1,
795                     args, in_reply_to ? 4 : 2);
796        g_free(args[3]);
797}
798
799
800/**
801 * Function to POST a new message to twitter.
802 */
803void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg)
804{
805        char *args[4];
806        args[0] = "screen_name";
807        args[1] = who;
808        args[2] = "text";
809        args[3] = msg;
810        // Use the same callback as for twitter_post_status, since it does basically the same.
811        twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4);
812}
813
814void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create)
815{
816        char *args[2];
817        args[0] = "screen_name";
818        args[1] = who;
819        twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL,
820                     twitter_http_post, ic, 1, args, 2);
821}
822
823void twitter_status_destroy(struct im_connection *ic, guint64 id)
824{
825        char *url;
826        url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_DESTROY_URL,
827                              (unsigned long long) id, ".xml");
828        twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0);
829        g_free(url);
830}
831
832void twitter_status_retweet(struct im_connection *ic, guint64 id)
833{
834        char *url;
835        url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_RETWEET_URL,
836                              (unsigned long long) id, ".xml");
837        twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0);
838        g_free(url);
839}
Note: See TracBrowser for help on using the repository browser.