source: protocols/twitter/twitter.c @ a7c6d0e

Last change on this file since a7c6d0e was a7c6d0e, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-30T22:51:18Z

Set HTML-flag on Twitter accounts so &lt;/&gt; and all get converted back.
(Probably also fixes potential problems with outgoing messages.)

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