source: protocols/twitter/twitter_lib.c @ 40bc82d

Last change on this file since 40bc82d was 37aa317, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-13T22:09:40Z

Small Valgrind noise fix. (Check if the conn is still alive before getting
its private data.)

  • Property mode set to 100644
File size: 17.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#define _XOPEN_SOURCE
26
27#include "twitter_http.h"
28#include "twitter.h"
29#include "bitlbee.h"
30#include "url.h"
31#include "misc.h"
32#include "base64.h"
33#include "xmltree.h"
34#include "twitter_lib.h"
35#include <ctype.h>
36#include <errno.h>
37
38#define TXL_STATUS 1
39#define TXL_USER 2
40#define TXL_ID 3
41
42struct twitter_xml_list {
43        int type;
44        int next_cursor;
45        GSList *list;
46        gpointer data;
47};
48
49struct twitter_xml_user {
50        char *name;
51        char *screen_name;
52};
53
54struct twitter_xml_status {
55        time_t created_at;
56        char *text;
57        struct twitter_xml_user *user;
58        guint64 id;
59};
60
61/**
62 * Frees a twitter_xml_user struct.
63 */
64static void txu_free(struct twitter_xml_user *txu)
65{
66        g_free(txu->name);
67        g_free(txu->screen_name);
68        g_free(txu);
69}
70
71
72/**
73 * Frees a twitter_xml_status struct.
74 */
75static void txs_free(struct twitter_xml_status *txs)
76{
77        g_free(txs->text);
78        txu_free(txs->user);
79        g_free(txs);
80}
81
82/**
83 * Free a twitter_xml_list struct.
84 * type is the type of list the struct holds.
85 */
86static void txl_free(struct twitter_xml_list *txl)
87{
88        GSList *l;
89        for ( l = txl->list; l ; l = g_slist_next(l) )
90                if (txl->type == TXL_STATUS)
91                        txs_free((struct twitter_xml_status *)l->data);
92                else if (txl->type == TXL_ID)
93                        g_free(l->data);
94        g_slist_free(txl->list);
95}
96
97/**
98 * Add a buddy if it is not allready added, set the status to logged in.
99 */
100static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname)
101{
102        struct twitter_data *td = ic->proto_data;
103
104        // Check if the buddy is allready in the buddy list.
105        if (!imcb_find_buddy( ic, name ))
106        {
107                // The buddy is not in the list, add the buddy and set the status to logged in.
108                imcb_add_buddy( ic, name, NULL );
109                imcb_rename_buddy( ic, name, fullname );
110                if (set_getbool( &ic->acc->set, "use_groupchat" ))
111                        imcb_chat_add_buddy( td->home_timeline_gc, name );
112                else
113                        imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL );
114        }
115}
116
117static void twitter_http_get_friends_ids(struct http_request *req);
118
119/**
120 * Get the friends ids.
121 */
122void twitter_get_friends_ids(struct im_connection *ic, int next_cursor)
123{
124        struct twitter_data *td = ic->proto_data;
125
126        // Primitive, but hey! It works...     
127        char* args[2];
128        args[0] = "cursor";
129        args[1] = g_strdup_printf ("%d", next_cursor);
130        twitter_http(TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, td->user, td->pass, args, 2);
131
132        g_free(args[1]);
133}
134
135/**
136 * Function to help fill a list.
137 */
138static xt_status twitter_xt_next_cursor( struct xt_node *node, struct twitter_xml_list *txl )
139{
140        // Do something with the cursor.
141        txl->next_cursor = node->text != NULL ? atoi(node->text) : -1;
142
143        return XT_HANDLED;
144}
145
146/**
147 * Fill a list of ids.
148 */
149static xt_status twitter_xt_get_friends_id_list( struct xt_node *node, struct twitter_xml_list *txl )
150{
151        struct xt_node *child;
152       
153        // Set the list type.
154        txl->type = TXL_ID;
155
156        // The root <statuses> node should hold the list of statuses <status>
157        // Walk over the nodes children.
158        for( child = node->children ; child ; child = child->next )
159        {
160                if ( g_strcasecmp( "id", child->name ) == 0)
161                {
162                        // Add the item to the list.
163                        txl->list = g_slist_append (txl->list, g_memdup( child->text, child->text_len + 1 ));
164                }
165                else if ( g_strcasecmp( "next_cursor", child->name ) == 0)
166                {
167                        twitter_xt_next_cursor(child, txl);
168                }
169        }
170
171        return XT_HANDLED;
172}
173
174/**
175 * Callback for getting the friends ids.
176 */
177static void twitter_http_get_friends_ids(struct http_request *req)
178{
179        struct im_connection *ic;
180        struct xt_parser *parser;
181        struct twitter_xml_list *txl;
182        struct twitter_data *td;
183
184        ic = req->data;
185
186        // Check if the connection is still active.
187        if( !g_slist_find( twitter_connections, ic ) )
188                return;
189       
190        td = ic->proto_data;
191
192        // Check if the HTTP request went well.
193        if (req->status_code != 200) {
194                // It didn't go well, output the error and return.
195                if (++td->http_fails >= 5)
196                        imcb_error(ic, "Could not retrieve friends. HTTP STATUS: %d", req->status_code);
197               
198                return;
199        } else {
200                td->http_fails = 0;
201        }
202
203        txl = g_new0(struct twitter_xml_list, 1);
204
205        // Parse the data.
206        parser = xt_new( NULL, txl );
207        xt_feed( parser, req->reply_body, req->body_size );
208        twitter_xt_get_friends_id_list(parser->root, txl);
209        xt_free( parser );
210
211        if (txl->next_cursor)
212                twitter_get_friends_ids(ic, txl->next_cursor);
213
214        txl_free(txl);
215        g_free(txl);
216}
217
218/**
219 * Function to fill a twitter_xml_user struct.
220 * It sets:
221 *  - the name and
222 *  - the screen_name.
223 */
224static xt_status twitter_xt_get_user( struct xt_node *node, struct twitter_xml_user *txu )
225{
226        struct xt_node *child;
227
228        // Walk over the nodes children.
229        for( child = node->children ; child ; child = child->next )
230        {
231                if ( g_strcasecmp( "name", child->name ) == 0)
232                {
233                        txu->name = g_memdup( child->text, child->text_len + 1 );
234                }
235                else if (g_strcasecmp( "screen_name", child->name ) == 0)
236                {
237                        txu->screen_name = g_memdup( child->text, child->text_len + 1 );
238                }
239        }
240        return XT_HANDLED;
241}
242
243/**
244 * Function to fill a twitter_xml_list struct.
245 * It sets:
246 *  - all <user>s from the <users> element.
247 */
248static xt_status twitter_xt_get_users( struct xt_node *node, struct twitter_xml_list *txl )
249{
250        struct twitter_xml_user *txu;
251        struct xt_node *child;
252
253        // Set the type of the list.
254        txl->type = TXL_USER;
255
256        // The root <users> node should hold the list of users <user>
257        // Walk over the nodes children.
258        for( child = node->children ; child ; child = child->next )
259        {
260                if ( g_strcasecmp( "user", child->name ) == 0)
261                {
262                        txu = g_new0(struct twitter_xml_user, 1);
263                        twitter_xt_get_user(child, txu);
264                        // Put the item in the front of the list.
265                        txl->list = g_slist_prepend (txl->list, txu);
266                }
267        }
268
269        return XT_HANDLED;
270}
271
272/**
273 * Function to fill a twitter_xml_list struct.
274 * It calls twitter_xt_get_users to get the <user>s from a <users> element.
275 * It sets:
276 *  - the next_cursor.
277 */
278static xt_status twitter_xt_get_user_list( struct xt_node *node, struct twitter_xml_list *txl )
279{
280        struct xt_node *child;
281
282        // Set the type of the list.
283        txl->type = TXL_USER;
284
285        // The root <user_list> node should hold a users <users> element
286        // Walk over the nodes children.
287        for( child = node->children ; child ; child = child->next )
288        {
289                if ( g_strcasecmp( "users", child->name ) == 0)
290                {
291                        twitter_xt_get_users(child, txl);
292                }
293                else if ( g_strcasecmp( "next_cursor", child->name ) == 0)
294                {
295                        twitter_xt_next_cursor(child, txl);
296                }
297        }
298
299        return XT_HANDLED;
300}
301
302
303/**
304 * Function to fill a twitter_xml_status struct.
305 * It sets:
306 *  - the status text and
307 *  - the created_at timestamp and
308 *  - the status id and
309 *  - the user in a twitter_xml_user struct.
310 */
311static xt_status twitter_xt_get_status( struct xt_node *node, struct twitter_xml_status *txs )
312{
313        struct xt_node *child;
314
315        // Walk over the nodes children.
316        for( child = node->children ; child ; child = child->next )
317        {
318                if ( g_strcasecmp( "text", child->name ) == 0)
319                {
320                        txs->text = g_memdup( child->text, child->text_len + 1 );
321                }
322                else if (g_strcasecmp( "created_at", child->name ) == 0)
323                {
324                        struct tm parsed;
325                       
326                        /* Very sensitive to changes to the formatting of
327                           this field. :-( Also assumes the timezone used
328                           is UTC since C time handling functions suck. */
329                        if( strptime( child->text, "%a %b %d %H:%M:%S %z %Y", &parsed ) != NULL )
330                                txs->created_at = mktime_utc( &parsed );
331                }
332                else if (g_strcasecmp( "user", child->name ) == 0)
333                {
334                        txs->user = g_new0(struct twitter_xml_user, 1);
335                        twitter_xt_get_user( child, txs->user );
336                }
337                else if (g_strcasecmp( "id", child->name ) == 0)
338                {
339                        txs->id = g_ascii_strtoull (child->text, NULL, 10);
340                }
341        }
342        return XT_HANDLED;
343}
344
345/**
346 * Function to fill a twitter_xml_list struct.
347 * It sets:
348 *  - all <status>es within the <status> element and
349 *  - the next_cursor.
350 */
351static xt_status twitter_xt_get_status_list( struct xt_node *node, struct twitter_xml_list *txl )
352{
353        struct twitter_xml_status *txs;
354        struct xt_node *child;
355
356        // Set the type of the list.
357        txl->type = TXL_STATUS;
358
359        // The root <statuses> node should hold the list of statuses <status>
360        // Walk over the nodes children.
361        for( child = node->children ; child ; child = child->next )
362        {
363                if ( g_strcasecmp( "status", child->name ) == 0)
364                {
365                        txs = g_new0(struct twitter_xml_status, 1);
366                        twitter_xt_get_status(child, txs);
367                        // Put the item in the front of the list.
368                        txl->list = g_slist_prepend (txl->list, txs);
369                }
370                else if ( g_strcasecmp( "next_cursor", child->name ) == 0)
371                {
372                        twitter_xt_next_cursor(child, txl);
373                }
374        }
375
376        return XT_HANDLED;
377}
378
379static void twitter_http_get_home_timeline(struct http_request *req);
380
381/**
382 * Get the timeline.
383 */
384void twitter_get_home_timeline(struct im_connection *ic, int next_cursor)
385{
386        struct twitter_data *td = ic->proto_data;
387
388        char* args[4];
389        args[0] = "cursor";
390        args[1] = g_strdup_printf ("%d", next_cursor);
391        if (td->home_timeline_id) {
392                args[2] = "since_id";
393                args[3] = g_strdup_printf ("%llu", (long long unsigned int) td->home_timeline_id);
394        }
395
396        twitter_http(TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, td->user, td->pass, args, td->home_timeline_id ? 4 : 2);
397
398        g_free(args[1]);
399        if (td->home_timeline_id) {
400                g_free(args[3]);
401        }
402}
403
404/**
405 * Function that is called to see the statuses in a groupchat window.
406 */
407static void twitter_groupchat(struct im_connection *ic, GSList *list)
408{
409        struct twitter_data *td = ic->proto_data;
410        GSList *l = NULL;
411        struct twitter_xml_status *status;
412        struct groupchat *gc;
413
414        // Create a new groupchat if it does not exsist.
415        if (!td->home_timeline_gc)
416        {   
417                char *name_hint = g_strdup_printf( "Twitter_%s", ic->acc->user );
418                td->home_timeline_gc = gc = imcb_chat_new( ic, "home/timeline" );
419                imcb_chat_name_hint( gc, name_hint );
420                g_free( name_hint );
421                // Add the current user to the chat...
422                imcb_chat_add_buddy( gc, ic->acc->user );
423        }
424        else
425        {   
426                gc = td->home_timeline_gc;
427        }
428
429        for ( l = list; l ; l = g_slist_next(l) )
430        {
431                status = l->data;
432                twitter_add_buddy(ic, status->user->screen_name, status->user->name);
433               
434                // Say it!
435                if (g_strcasecmp(td->user, status->user->screen_name) == 0)
436                        imcb_chat_log (gc, "Your Tweet: %s", status->text);
437                else
438                        imcb_chat_msg (gc, status->user->screen_name, status->text, 0, status->created_at );
439               
440                // Update the home_timeline_id to hold the highest id, so that by the next request
441                // we won't pick up the updates allready in the list.
442                td->home_timeline_id = td->home_timeline_id < status->id ? status->id : td->home_timeline_id;
443        }
444}
445
446/**
447 * Function that is called to see statuses as private messages.
448 */
449static void twitter_private_message_chat(struct im_connection *ic, GSList *list)
450{
451        struct twitter_data *td = ic->proto_data;
452        GSList *l = NULL;
453        struct twitter_xml_status *status;
454
455        for ( l = list; l ; l = g_slist_next(l) )
456        {
457                status = l->data;
458                imcb_buddy_msg( ic, status->user->screen_name, status->text, 0, status->created_at );
459                // Update the home_timeline_id to hold the highest id, so that by the next request
460                // we won't pick up the updates allready in the list.
461                td->home_timeline_id = td->home_timeline_id < status->id ? status->id : td->home_timeline_id;
462        }
463}
464
465/**
466 * Callback for getting the home timeline.
467 */
468static void twitter_http_get_home_timeline(struct http_request *req)
469{
470        struct im_connection *ic = req->data;
471        struct twitter_data *td;
472        struct xt_parser *parser;
473        struct twitter_xml_list *txl;
474
475        // Check if the connection is still active.
476        if( !g_slist_find( twitter_connections, ic ) )
477                return;
478       
479        td = ic->proto_data;
480
481        // Check if the HTTP request went well.
482        if (req->status_code == 200)
483        {
484                td->http_fails = 0;
485                if (!ic->flags & OPT_LOGGED_IN)
486                        imcb_connected(ic);
487        }
488        else if (req->status_code == 401)
489        {
490                imcb_error( ic, "Authentication failure" );
491                imc_logout( ic, FALSE );
492                return;
493        }
494        else
495        {
496                // It didn't go well, output the error and return.
497                if (++td->http_fails >= 5)
498                        imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL ". HTTP STATUS: %d", req->status_code);
499               
500                return;
501        }
502
503        txl = g_new0(struct twitter_xml_list, 1);
504        txl->list = NULL;
505
506        // Parse the data.
507        parser = xt_new( NULL, txl );
508        xt_feed( parser, req->reply_body, req->body_size );
509        // The root <statuses> node should hold the list of statuses <status>
510        twitter_xt_get_status_list(parser->root, txl);
511        xt_free( parser );
512
513        // See if the user wants to see the messages in a groupchat window or as private messages.
514        if (set_getbool( &ic->acc->set, "use_groupchat" ))
515                twitter_groupchat(ic, txl->list);
516        else
517                twitter_private_message_chat(ic, txl->list);
518
519        // Free the structure. 
520        txl_free(txl);
521        g_free(txl);
522}
523
524/**
525 * Callback for getting (twitter)friends...
526 *
527 * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has
528 * hundreds of friends?" you wonder? You probably not, since you are reading the source of
529 * BitlBee... Get a life and meet new people!
530 */
531static void twitter_http_get_statuses_friends(struct http_request *req)
532{
533        struct im_connection *ic = req->data;
534        struct twitter_data *td;
535        struct xt_parser *parser;
536        struct twitter_xml_list *txl;
537        GSList *l = NULL;
538        struct twitter_xml_user *user;
539
540        // Check if the connection is still active.
541        if( !g_slist_find( twitter_connections, ic ) )
542                return;
543       
544        td = ic->proto_data;
545       
546        // Check if the HTTP request went well.
547        if (req->status_code != 200) {
548                // It didn't go well, output the error and return.
549                if (++td->http_fails >= 5)
550                        imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL " HTTP STATUS: %d", req->status_code);
551               
552                return;
553        } else {
554                td->http_fails = 0;
555        }
556
557        txl = g_new0(struct twitter_xml_list, 1);
558        txl->list = NULL;
559
560        // Parse the data.
561        parser = xt_new( NULL, txl );
562        xt_feed( parser, req->reply_body, req->body_size );
563
564        // Get the user list from the parsed xml feed.
565        twitter_xt_get_user_list(parser->root, txl);
566        xt_free( parser );
567
568        // Add the users as buddies.
569        for ( l = txl->list; l ; l = g_slist_next(l) )
570        {
571                user = l->data;
572                twitter_add_buddy(ic, user->screen_name, user->name);
573        }
574
575        // if the next_cursor is set to something bigger then 0 there are more friends to gather.
576        if (txl->next_cursor > 0)
577                twitter_get_statuses_friends(ic, txl->next_cursor);
578
579        // Free the structure.
580        txl_free(txl);
581        g_free(txl);
582}
583
584/**
585 * Get the friends.
586 */
587void twitter_get_statuses_friends(struct im_connection *ic, int next_cursor)
588{
589        struct twitter_data *td = ic->proto_data;
590
591        char* args[2];
592        args[0] = "cursor";
593        args[1] = g_strdup_printf ("%d", next_cursor);
594
595        twitter_http(TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, td->user, td->pass, args, 2);
596
597        g_free(args[1]);
598}
599
600/**
601 * Callback after sending a new update to twitter.
602 */
603static void twitter_http_post_status(struct http_request *req)
604{
605        struct im_connection *ic = req->data;
606
607        // Check if the connection is still active.
608        if( !g_slist_find( twitter_connections, ic ) )
609                return;
610
611        // Check if the HTTP request went well.
612        if (req->status_code != 200) {
613                // It didn't go well, output the error and return.
614                imcb_error(ic, "Could not post tweet... HTTP STATUS: %d", req->status_code);
615                return;
616        }
617}
618
619/**
620 * Function to POST a new status to twitter.
621 */ 
622void twitter_post_status(struct im_connection *ic, char* msg)
623{
624        struct twitter_data *td = ic->proto_data;
625
626        char* args[2];
627        args[0] = "status";
628        args[1] = msg;
629        twitter_http(TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, td->user, td->pass, args, 2);
630//      g_free(args[1]);
631}
632
633
634/**
635 * Function to POST a new message to twitter.
636 */
637void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg)
638{
639        struct twitter_data *td = ic->proto_data;
640
641        char* args[4];
642        args[0] = "screen_name";
643        args[1] = who;
644        args[2] = "text";
645        args[3] = msg;
646        // Use the same callback as for twitter_post_status, since it does basically the same.
647        twitter_http(TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post_status, ic, 1, td->user, td->pass, args, 4);
648//      g_free(args[1]);
649//      g_free(args[3]);
650}
Note: See TracBrowser for help on using the repository browser.