source: protocols/twitter/twitter.c @ f5c0d8e

Last change on this file since f5c0d8e was f5c0d8e, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-31T20:06:14Z

Merge mainline stuff.

  • Property mode set to 100644
File size: 14.4 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#include "nogaim.h"
25#include "oauth.h"
26#include "twitter.h"
27#include "twitter_http.h"
28#include "twitter_lib.h"
29#include "url.h"
30
31/**
32 * Main loop function
33 */
34gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond)
35{
36        struct im_connection *ic = data;
37       
38        // Check if we are still logged in...
39        if (!g_slist_find( twitter_connections, ic ))
40                return 0;
41
42        // Do stuff..
43        twitter_get_home_timeline(ic, -1);
44
45        // If we are still logged in run this function again after timeout.
46        return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN;
47}
48
49static void twitter_main_loop_start( struct im_connection *ic )
50{
51        struct twitter_data *td = ic->proto_data;
52       
53        imcb_log( ic, "Getting initial statuses" );
54
55        // Run this once. After this queue the main loop function.
56        twitter_main_loop(ic, -1, 0);
57
58        // Queue the main_loop
59        // Save the return value, so we can remove the timeout on logout.
60        td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);
61}
62
63static void twitter_oauth_start( struct im_connection *ic );
64
65void twitter_login_finish( struct im_connection *ic )
66{
67        struct twitter_data *td = ic->proto_data;
68       
69        if( set_getbool( &ic->acc->set, "oauth" ) && !td->oauth_info )
70                twitter_oauth_start( ic );
71        else if( g_strcasecmp( set_getstr( &ic->acc->set, "mode" ), "one" ) != 0 &&
72                 !( td->flags & TWITTER_HAVE_FRIENDS ) )
73        {
74                imcb_log( ic, "Getting contact list" );
75                twitter_get_statuses_friends( ic, -1 );
76        }
77        else
78                twitter_main_loop_start( ic );
79}
80
81static const struct oauth_service twitter_oauth =
82{
83        "http://api.twitter.com/oauth/request_token",
84        "http://api.twitter.com/oauth/access_token",
85        "https://api.twitter.com/oauth/authorize",
86        .consumer_key = "xsDNKJuNZYkZyMcu914uEA",
87        .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo",
88};
89
90static gboolean twitter_oauth_callback( struct oauth_info *info );
91
92static void twitter_oauth_start( struct im_connection *ic )
93{
94        struct twitter_data *td = ic->proto_data;
95       
96        imcb_log( ic, "Requesting OAuth request token" );
97
98        td->oauth_info = oauth_request_token( &twitter_oauth, twitter_oauth_callback, ic );
99}
100
101static gboolean twitter_oauth_callback( struct oauth_info *info )
102{
103        struct im_connection *ic = info->data;
104        struct twitter_data *td;
105       
106        if( !g_slist_find( twitter_connections, ic ) )
107                return FALSE;
108       
109        td = ic->proto_data;
110        if( info->stage == OAUTH_REQUEST_TOKEN )
111        {
112                char name[strlen(ic->acc->user)+9], *msg;
113               
114                if( info->request_token == NULL )
115                {
116                        imcb_error( ic, "OAuth error: %s", info->http->status_string );
117                        imc_logout( ic, TRUE );
118                        return FALSE;
119                }
120               
121                sprintf( name, "%s_%s", td->prefix, ic->acc->user );
122                msg = g_strdup_printf( "To finish OAuth authentication, please visit "
123                                       "%s and respond with the resulting PIN code.",
124                                       info->auth_url );
125                imcb_buddy_msg( ic, name, msg, 0, 0 );
126                g_free( msg );
127        }
128        else if( info->stage == OAUTH_ACCESS_TOKEN )
129        {
130                if( info->token == NULL || info->token_secret == NULL )
131                {
132                        imcb_error( ic, "OAuth error: %s", info->http->status_string );
133                        imc_logout( ic, TRUE );
134                        return FALSE;
135                }
136               
137                /* IM mods didn't do this so far and it's ugly but I should
138                   be able to get away with it... */
139                g_free( ic->acc->pass );
140                ic->acc->pass = oauth_to_string( info );
141               
142                twitter_login_finish( ic );
143        }
144       
145        return TRUE;
146}
147
148
149static char *set_eval_mode( set_t *set, char *value )
150{
151        if( g_strcasecmp( value, "one" ) == 0 ||
152            g_strcasecmp( value, "many" ) == 0 ||
153            g_strcasecmp( value, "chat" ) == 0 )
154                return value;
155        else
156                return NULL;
157}
158
159static gboolean twitter_length_check( struct im_connection *ic, gchar *msg )
160{
161        int max = set_getint( &ic->acc->set, "message_length" ), len;
162       
163        if( max == 0 || ( len = g_utf8_strlen( msg, -1 ) ) <= max )
164                return TRUE;
165       
166        imcb_error( ic, "Maximum message length exceeded: %d > %d", len, max );
167       
168        return FALSE;
169}
170
171static void twitter_init( account_t *acc )
172{
173        set_t *s;
174        char *def_url;
175        char *def_oauth;
176       
177        if( strcmp( acc->prpl->name, "twitter" ) == 0 )
178        {
179                def_url = TWITTER_API_URL;
180                def_oauth = "true";
181        }
182        else /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */
183        {
184                def_url = IDENTICA_API_URL;
185                def_oauth = "false";
186        }
187       
188        s = set_add( &acc->set, "auto_reply_timeout", "10800", set_eval_int, acc );
189       
190        s = set_add( &acc->set, "base_url", def_url, NULL, acc );
191        s->flags |= ACC_SET_OFFLINE_ONLY;
192       
193        s = set_add( &acc->set, "commands", "true", set_eval_bool, acc );
194       
195        s = set_add( &acc->set, "message_length", "140", set_eval_int, acc );
196       
197        s = set_add( &acc->set, "mode", "chat", set_eval_mode, acc );
198        s->flags |= ACC_SET_OFFLINE_ONLY;
199       
200        s = set_add( &acc->set, "oauth", def_oauth, set_eval_bool, acc );
201}
202
203/**
204 * Login method. Since the twitter API works with seperate HTTP request we
205 * only save the user and pass to the twitter_data object.
206 */
207static void twitter_login( account_t *acc )
208{
209        struct im_connection *ic = imcb_new( acc );
210        struct twitter_data *td;
211        char name[strlen(acc->user)+9];
212        url_t url;
213
214        if( !url_set( &url, set_getstr( &ic->acc->set, "base_url" ) ) ||
215            ( url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS ) )
216        {
217                imcb_error( ic, "Incorrect API base URL: %s", set_getstr( &ic->acc->set, "base_url" ) );
218                imc_logout( ic, FALSE );
219                return;
220        }
221       
222        twitter_connections = g_slist_append( twitter_connections, ic );
223        td = g_new0( struct twitter_data, 1 );
224        ic->proto_data = td;
225       
226        td->url_ssl = url.proto == PROTO_HTTPS;
227        td->url_port = url.port;
228        td->url_host = g_strdup( url.host );
229        if( strcmp( url.file, "/" ) != 0 )
230                td->url_path = g_strdup( url.file );
231        else
232                td->url_path = g_strdup( "" );
233        if( g_str_has_suffix( url.host, ".com" ) )
234                td->prefix = g_strndup( url.host, strlen( url.host ) - 4 );
235        else
236                td->prefix = g_strdup( url.host );
237       
238        td->user = acc->user;
239        if( strstr( acc->pass, "oauth_token=" ) )
240                td->oauth_info = oauth_from_string( acc->pass, &twitter_oauth );
241       
242        sprintf( name, "%s_%s", td->prefix, acc->user );
243        imcb_add_buddy( ic, name, NULL );
244        imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL );
245       
246        imcb_log( ic, "Connecting" );
247       
248        twitter_login_finish( ic );
249}
250
251/**
252 * Logout method. Just free the twitter_data.
253 */
254static void twitter_logout( struct im_connection *ic )
255{
256        struct twitter_data *td = ic->proto_data;
257       
258        // Set the status to logged out.
259        ic->flags &= ~ OPT_LOGGED_IN;
260
261        // Remove the main_loop function from the function queue.
262        b_event_remove(td->main_loop_id);
263
264        if(td->home_timeline_gc)
265                imcb_chat_free(td->home_timeline_gc);
266
267        if( td )
268        {
269                oauth_info_free( td->oauth_info );
270                g_free( td->prefix );
271                g_free( td->url_host );
272                g_free( td->url_path );
273                g_free( td->pass );
274                g_free( td );
275        }
276
277        twitter_connections = g_slist_remove( twitter_connections, ic );
278}
279
280static void twitter_handle_command( struct im_connection *ic, char *message );
281
282/**
283 *
284 */
285static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
286{
287        struct twitter_data *td = ic->proto_data;
288        int plen = strlen( td->prefix );
289       
290        if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' &&
291            g_strcasecmp(who + plen + 1, ic->acc->user) == 0)
292        {
293                if( set_getbool( &ic->acc->set, "oauth" ) &&
294                    td->oauth_info && td->oauth_info->token == NULL )
295                {
296                        char pin[strlen(message)+1], *s;
297                       
298                        strcpy( pin, message );
299                        for( s = pin + sizeof( pin ) - 2; s > pin && isspace( *s ); s -- )
300                                *s = '\0';
301                        for( s = pin; *s && isspace( *s ); s ++ ) {}
302                       
303                        if( !oauth_access_token( s, td->oauth_info ) )
304                        {
305                                imcb_error( ic, "OAuth error: %s", "Failed to send access token request" );
306                                imc_logout( ic, TRUE );
307                                return FALSE;
308                        }
309                }
310                else
311                        twitter_handle_command(ic, message);
312        }
313        else
314        {
315                twitter_direct_messages_new(ic, who, message);
316        }
317        return( 0 );
318}
319
320/**
321 *
322 */
323static void twitter_set_my_name( struct im_connection *ic, char *info )
324{
325}
326
327static void twitter_get_info(struct im_connection *ic, char *who) 
328{
329}
330
331static void twitter_add_buddy( struct im_connection *ic, char *who, char *group )
332{
333        twitter_friendships_create_destroy(ic, who, 1);
334}
335
336static void twitter_remove_buddy( struct im_connection *ic, char *who, char *group )
337{
338        twitter_friendships_create_destroy(ic, who, 0);
339}
340
341static void twitter_chat_msg( struct groupchat *c, char *message, int flags )
342{
343        if( c && message )
344                twitter_handle_command( c->ic, message );
345}
346
347static void twitter_chat_invite( struct groupchat *c, char *who, char *message )
348{
349}
350
351static void twitter_chat_leave( struct groupchat *c )
352{
353        struct twitter_data *td = c->ic->proto_data;
354       
355        if( c != td->home_timeline_gc )
356                return; /* WTF? */
357       
358        /* If the user leaves the channel: Fine. Rejoin him/her once new
359           tweets come in. */
360        imcb_chat_free(td->home_timeline_gc);
361        td->home_timeline_gc = NULL;
362}
363
364static struct groupchat *twitter_chat_with( struct im_connection *ic, char *who )
365{
366        return NULL;
367}
368
369static void twitter_keepalive( struct im_connection *ic )
370{
371}
372
373static void twitter_add_permit( struct im_connection *ic, char *who )
374{
375}
376
377static void twitter_rem_permit( struct im_connection *ic, char *who )
378{
379}
380
381static void twitter_add_deny( struct im_connection *ic, char *who )
382{
383}
384
385static void twitter_rem_deny( struct im_connection *ic, char *who )
386{
387}
388
389static int twitter_send_typing( struct im_connection *ic, char *who, int typing )
390{
391        return( 1 );
392}
393
394//static char *twitter_set_display_name( set_t *set, char *value )
395//{
396//      return value;
397//}
398
399static void twitter_buddy_data_add( struct bee_user *bu )
400{
401        bu->data = g_new0( struct twitter_user_data, 1 );
402}
403
404static void twitter_buddy_data_free( struct bee_user *bu )
405{
406        g_free( bu->data );
407}
408
409static void twitter_handle_command( struct im_connection *ic, char *message )
410{
411        struct twitter_data *td = ic->proto_data;
412        char *cmds, **cmd;
413       
414        cmds = g_strdup( message );
415        cmd = split_command_parts( cmds );
416       
417        if( cmd[0] == NULL )
418        {
419                g_free( cmds );
420                return;
421        }
422        else if( !set_getbool( &ic->acc->set, "commands" ) )
423        {
424                /* Not supporting commands. */
425        }
426        else if( g_strcasecmp( cmd[0], "undo" ) == 0 )
427        {
428                guint64 id;
429               
430                if( cmd[1] )
431                        id = g_ascii_strtoull( cmd[1], NULL, 10 );
432                else
433                        id = td->last_status_id;
434               
435                /* TODO: User feedback. */
436                if( id )
437                        twitter_status_destroy( ic, id );
438               
439                g_free( cmds );
440                return;
441        }
442        else if( g_strcasecmp( cmd[0], "follow" ) == 0 && cmd[1] )
443        {
444                twitter_add_buddy( ic, cmd[1], NULL );
445                g_free( cmds );
446                return;
447        }
448        else if( g_strcasecmp( cmd[0], "unfollow" ) == 0 && cmd[1] )
449        {
450                twitter_remove_buddy( ic, cmd[1], NULL );
451                g_free( cmds );
452                return;
453        }
454        else if( g_strcasecmp( cmd[0], "rt" ) == 0 && cmd[1] )
455        {
456                struct twitter_user_data *tud;
457                bee_user_t *bu;
458                guint64 id;
459               
460                if( ( bu = bee_user_by_handle( ic->bee, ic, cmd[1] ) ) &&
461                    ( tud = bu->data ) && tud->last_id )
462                        id = tud->last_id;
463                else
464                        id = g_ascii_strtoull( cmd[1], NULL, 10 );
465               
466                td->last_status_id = 0;
467                if( id )
468                        twitter_status_retweet( ic, id );
469               
470                g_free( cmds );
471                return;
472        }
473        else if( g_strcasecmp( cmd[0], "post" ) == 0 )
474        {
475                message += 5;
476        }
477       
478        {
479                guint64 in_reply_to = 0;
480                char *s, *new = NULL;
481                bee_user_t *bu;
482               
483                if( !twitter_length_check( ic, message ) )
484                {
485                        g_free( cmds );
486                        return;
487                }
488               
489                s = cmd[0] + strlen( cmd[0] ) - 1;
490                if( s > cmd[0] && ( *s == ':' || *s == ',' ) )
491                {
492                        *s = '\0';
493                       
494                        if( ( bu = bee_user_by_handle( ic->bee, ic, cmd[0] ) ) )
495                        {
496                                struct twitter_user_data *tud = bu->data;
497                               
498                                new = g_strdup_printf( "@%s %s", bu->handle,
499                                                       message + ( s - cmd[0] ) + 2 );
500                                message = new;
501                               
502                                if( time( NULL ) < tud->last_time +
503                                    set_getint( &ic->acc->set, "auto_reply_timeout" ) )
504                                        in_reply_to = tud->last_id;
505                        }
506                }
507               
508                /* If the user runs undo between this request and its response
509                   this would delete the second-last Tweet. Prevent that. */
510                td->last_status_id = 0;
511                twitter_post_status( ic, message, in_reply_to );
512                g_free( new );
513        }
514        g_free( cmds );
515}
516
517void twitter_initmodule()
518{
519        struct prpl *ret = g_new0(struct prpl, 1);
520       
521        ret->options = OPT_NOOTR;
522        ret->name = "twitter";
523        ret->login = twitter_login;
524        ret->init = twitter_init;
525        ret->logout = twitter_logout;
526        ret->buddy_msg = twitter_buddy_msg;
527        ret->get_info = twitter_get_info;
528        ret->set_my_name = twitter_set_my_name;
529        ret->add_buddy = twitter_add_buddy;
530        ret->remove_buddy = twitter_remove_buddy;
531        ret->chat_msg = twitter_chat_msg;
532        ret->chat_invite = twitter_chat_invite;
533        ret->chat_leave = twitter_chat_leave;
534        ret->chat_with = twitter_chat_with;
535        ret->keepalive = twitter_keepalive;
536        ret->add_permit = twitter_add_permit;
537        ret->rem_permit = twitter_rem_permit;
538        ret->add_deny = twitter_add_deny;
539        ret->rem_deny = twitter_rem_deny;
540        ret->send_typing = twitter_send_typing;
541        ret->buddy_data_add = twitter_buddy_data_add;
542        ret->buddy_data_free = twitter_buddy_data_free;
543        ret->handle_cmp = g_strcasecmp;
544       
545        register_protocol(ret);
546
547        /* And an identi.ca variant: */
548        ret = g_memdup(ret, sizeof(struct prpl));
549        ret->name = "identica";
550        register_protocol(ret);
551
552        // Initialise the twitter_connections GSList.
553        twitter_connections = NULL;
554}
Note: See TracBrowser for help on using the repository browser.