source: protocols/twitter/twitter_lib.c @ 6eca2eb

Last change on this file since 6eca2eb was 6eca2eb, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-04-18T14:14:08Z

Try to show better Twitter error messages. Sadly this doesn't always work
since Twitter can't seem to make up their mind on the formatting of their
error responses, sometimes using XML and sometimes plain text.

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