source: protocols/twitter/twitter.c @ 3b878a1

Last change on this file since 3b878a1 was 3b878a1, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-02T21:20:09Z

OAuth sanity fix: Twitter-specific stuff should *not* be in lib/oauth.c.
Somewhat intrusive, should've done this right immediately. :-/

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