source: protocols/twitter/twitter.c @ 713d611

Last change on this file since 713d611 was 713d611, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-26T21:50:48Z

Twitter module now generates authorize URLs.

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