source: protocols/twitter/twitter.c @ c42e8b9

Last change on this file since c42e8b9 was c42e8b9, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-26T22:40:11Z

OAuth, it lives!

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[1b221e0]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"
[713d611]25#include "oauth.h"
[1b221e0]26#include "twitter.h"
27#include "twitter_http.h"
28#include "twitter_lib.h"
29
30
31/**
[62d2cfb]32 * Main loop function
33 */
[1b221e0]34gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond)
35{
36        struct im_connection *ic = data;
[3e69802]37       
[1b221e0]38        // Check if we are still logged in...
[3bd4a93]39        if (!g_slist_find( twitter_connections, ic ))
[1b221e0]40                return 0;
41
[62d2cfb]42        // If the user uses multiple private message windows we need to get the
43        // users buddies.
[e88fbe27]44        if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "many") == 0)
[62d2cfb]45                twitter_get_statuses_friends(ic, -1);
46
[1b221e0]47        // Do stuff..
48        twitter_get_home_timeline(ic, -1);
49
50        // If we are still logged in run this function again after timeout.
51        return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN;
52}
53
[713d611]54static void twitter_main_loop_start( struct im_connection *ic )
55{
56        struct twitter_data *td = ic->proto_data;
57       
58        imcb_log( ic, "Connecting to Twitter" );
59
60        // Run this once. After this queue the main loop function.
61        twitter_main_loop(ic, -1, 0);
62
63        // Queue the main_loop
64        // Save the return value, so we can remove the timeout on logout.
65        td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);
66}
67
68static void twitter_oauth_callback( struct oauth_info *info );
69
70static void twitter_oauth_start( struct im_connection *ic )
71{
[c42e8b9]72        imcb_log( ic, "Requesting OAuth request token" );
73
[713d611]74        oauth_request_token( TWITTER_OAUTH_REQUEST_TOKEN, twitter_oauth_callback, ic );
75}
76
77static void twitter_oauth_callback( struct oauth_info *info )
78{
79        struct im_connection *ic = info->data;
[c42e8b9]80        struct twitter_data *td = ic->proto_data;
[713d611]81       
[c42e8b9]82        if( info->stage == OAUTH_REQUEST_TOKEN )
[713d611]83        {
84                char name[strlen(ic->acc->user)+9], *msg;
85               
[c42e8b9]86                if( info->request_token == NULL )
87                {
88                        imcb_error( ic, "OAuth error: %s", info->http->status_string );
89                        imc_logout( ic, TRUE );
90                        return;
91                }
92               
93                td->oauth_info = info;
94               
[713d611]95                sprintf( name, "twitter_%s", ic->acc->user );
96                msg = g_strdup_printf( "To finish OAuth authentication, please visit "
97                                       "%s?%s and respond with the resulting PIN code.",
98                                       TWITTER_OAUTH_AUTHORIZE, info->auth_params );
99                imcb_buddy_msg( ic, name, msg, 0, 0 );
100                g_free( msg );
101        }
[c42e8b9]102        else if( info->stage == OAUTH_ACCESS_TOKEN )
103        {
104                if( info->access_token == NULL )
105                {
106                        imcb_error( ic, "OAuth error: %s", info->http->status_string );
107                        imc_logout( ic, TRUE );
108                        return;
109                }
110               
111                td->oauth = g_strdup( info->access_token );
112               
113                twitter_main_loop_start( ic );
114        }
[713d611]115}
116
[e88fbe27]117static char *set_eval_mode( set_t *set, char *value )
118{
119        if( g_strcasecmp( value, "one" ) == 0 ||
120            g_strcasecmp( value, "many" ) == 0 ||
[db57e7c]121            g_strcasecmp( value, "chat" ) == 0 )
[e88fbe27]122                return value;
123        else
124                return NULL;
125}
[1b221e0]126
127static void twitter_init( account_t *acc )
128{
[62d2cfb]129        set_t *s;
[e88fbe27]130       
131        s = set_add( &acc->set, "mode", "one", set_eval_mode, acc );
[2abceca]132        s->flags |= ACC_SET_OFFLINE_ONLY;
[713d611]133       
134        s = set_add( &acc->set, "oauth", "true", set_eval_bool, acc );
[1b221e0]135}
136
137/**
138 * Login method. Since the twitter API works with seperate HTTP request we
139 * only save the user and pass to the twitter_data object.
140 */
141static void twitter_login( account_t *acc )
142{
143        struct im_connection *ic = imcb_new( acc );
144        struct twitter_data *td = g_new0( struct twitter_data, 1 );
[e88fbe27]145        char name[strlen(acc->user)+9];
[62d2cfb]146
[d569019]147        twitter_connections = g_slist_append( twitter_connections, ic );
[713d611]148        ic->proto_data = td;
149       
[1b221e0]150        td->user = acc->user;
[713d611]151        if( !set_getbool( &acc->set, "oauth" ) )
[508c340]152                td->pass = g_strdup( acc->pass );
[713d611]153        else if( strstr( acc->pass, "oauth_token=" ) )
[508c340]154                td->oauth = g_strdup( acc->pass );
[1b221e0]155        td->home_timeline_id = 0;
[e88fbe27]156       
157        sprintf( name, "twitter_%s", acc->user );
158        imcb_add_buddy( ic, name, NULL );
159        imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL );
[713d611]160       
161        if( td->pass || td->oauth )
162                twitter_main_loop_start( ic );
163        else
164                twitter_oauth_start( ic );
[1b221e0]165}
166
167/**
168 * Logout method. Just free the twitter_data.
169 */
170static void twitter_logout( struct im_connection *ic )
171{
172        struct twitter_data *td = ic->proto_data;
173       
174        // Set the status to logged out.
175        ic->flags = 0;
176
[2abceca]177        // Remove the main_loop function from the function queue.
178        b_event_remove(td->main_loop_id);
179
[37d84b3]180        if(td->home_timeline_gc)
181                imcb_chat_free(td->home_timeline_gc);
[1014cab]182
[1b221e0]183        if( td )
184        {
[508c340]185                g_free( td->pass );
186                g_free( td->oauth );
[1b221e0]187                g_free( td );
188        }
[62d2cfb]189
190        twitter_connections = g_slist_remove( twitter_connections, ic );
[1b221e0]191}
192
193/**
194 *
195 */
196static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
197{
[c42e8b9]198        struct twitter_data *td = ic->proto_data;
199       
[e88fbe27]200        if (g_strncasecmp(who, "twitter_", 8) == 0 &&
201            g_strcasecmp(who + 8, ic->acc->user) == 0)
[c42e8b9]202        {
203                if( set_getbool( &ic->acc->set, "oauth" ) && td->oauth == NULL )
204                        oauth_access_token( TWITTER_OAUTH_ACCESS_TOKEN, message, td->oauth_info );
205                else
206                        twitter_post_status(ic, message);
207        }
[e88fbe27]208        else
[c42e8b9]209        {
[e88fbe27]210                twitter_direct_messages_new(ic, who, message);
[c42e8b9]211        }
[1b221e0]212        return( 0 );
213}
214
215/**
216 *
217 */
218static void twitter_set_my_name( struct im_connection *ic, char *info )
219{
220}
221
222static void twitter_get_info(struct im_connection *ic, char *who) 
223{
224}
225
226static void twitter_add_buddy( struct im_connection *ic, char *who, char *group )
227{
228}
229
230static void twitter_remove_buddy( struct im_connection *ic, char *who, char *group )
231{
232}
233
234static void twitter_chat_msg( struct groupchat *c, char *message, int flags )
235{
[2abceca]236        if( c && message )
237                twitter_post_status(c->ic, message);
[1b221e0]238}
239
240static void twitter_chat_invite( struct groupchat *c, char *who, char *message )
241{
242}
243
244static void twitter_chat_leave( struct groupchat *c )
245{
[16592d8]246        struct twitter_data *td = c->ic->proto_data;
247       
248        if( c != td->home_timeline_gc )
249                return; /* WTF? */
250       
251        /* If the user leaves the channel: Fine. Rejoin him/her once new
252           tweets come in. */
253        imcb_chat_free(td->home_timeline_gc);
254        td->home_timeline_gc = NULL;
[1b221e0]255}
256
257static struct groupchat *twitter_chat_with( struct im_connection *ic, char *who )
258{
259        return NULL;
260}
261
262static void twitter_keepalive( struct im_connection *ic )
263{
264}
265
266static void twitter_add_permit( struct im_connection *ic, char *who )
267{
268}
269
270static void twitter_rem_permit( struct im_connection *ic, char *who )
271{
272}
273
274static void twitter_add_deny( struct im_connection *ic, char *who )
275{
276}
277
278static void twitter_rem_deny( struct im_connection *ic, char *who )
279{
280}
281
282static int twitter_send_typing( struct im_connection *ic, char *who, int typing )
283{
284        return( 1 );
285}
286
287//static char *twitter_set_display_name( set_t *set, char *value )
288//{
289//      return value;
290//}
291
292void twitter_initmodule()
293{
294        struct prpl *ret = g_new0(struct prpl, 1);
295       
296        ret->name = "twitter";
297        ret->login = twitter_login;
298        ret->init = twitter_init;
299        ret->logout = twitter_logout;
300        ret->buddy_msg = twitter_buddy_msg;
301        ret->get_info = twitter_get_info;
302        ret->set_my_name = twitter_set_my_name;
303        ret->add_buddy = twitter_add_buddy;
304        ret->remove_buddy = twitter_remove_buddy;
305        ret->chat_msg = twitter_chat_msg;
306        ret->chat_invite = twitter_chat_invite;
307        ret->chat_leave = twitter_chat_leave;
308        ret->chat_with = twitter_chat_with;
309        ret->keepalive = twitter_keepalive;
310        ret->add_permit = twitter_add_permit;
311        ret->rem_permit = twitter_rem_permit;
312        ret->add_deny = twitter_add_deny;
313        ret->rem_deny = twitter_rem_deny;
314        ret->send_typing = twitter_send_typing;
315        ret->handle_cmp = g_strcasecmp;
316
317        register_protocol(ret);
[62d2cfb]318
319        // Initialise the twitter_connections GSList.
320        twitter_connections = NULL;
[1b221e0]321}
322
Note: See TracBrowser for help on using the repository browser.