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 | */ |
---|
34 | gboolean 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 | |
---|
54 | static 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 | |
---|
68 | static gboolean twitter_oauth_callback( struct oauth_info *info ); |
---|
69 | |
---|
70 | static 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 | |
---|
80 | static 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 | |
---|
129 | static 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 | |
---|
139 | static 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 | */ |
---|
153 | static 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 | |
---|
162 | td->user = acc->user; |
---|
163 | if( !set_getbool( &acc->set, "oauth" ) ) |
---|
164 | td->pass = g_strdup( acc->pass ); |
---|
165 | else if( strstr( acc->pass, "oauth_token=" ) ) |
---|
166 | td->oauth = g_strdup( acc->pass ); |
---|
167 | td->home_timeline_id = 0; |
---|
168 | |
---|
169 | sprintf( name, "twitter_%s", acc->user ); |
---|
170 | imcb_add_buddy( ic, name, NULL ); |
---|
171 | imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL ); |
---|
172 | |
---|
173 | if( td->pass || td->oauth ) |
---|
174 | twitter_main_loop_start( ic ); |
---|
175 | else |
---|
176 | twitter_oauth_start( ic ); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Logout method. Just free the twitter_data. |
---|
181 | */ |
---|
182 | static void twitter_logout( struct im_connection *ic ) |
---|
183 | { |
---|
184 | struct twitter_data *td = ic->proto_data; |
---|
185 | |
---|
186 | // Set the status to logged out. |
---|
187 | ic->flags = 0; |
---|
188 | |
---|
189 | // Remove the main_loop function from the function queue. |
---|
190 | b_event_remove(td->main_loop_id); |
---|
191 | |
---|
192 | if(td->home_timeline_gc) |
---|
193 | imcb_chat_free(td->home_timeline_gc); |
---|
194 | |
---|
195 | if( td ) |
---|
196 | { |
---|
197 | oauth_info_free( td->oauth_info ); |
---|
198 | |
---|
199 | g_free( td->pass ); |
---|
200 | g_free( td->oauth ); |
---|
201 | g_free( td ); |
---|
202 | } |
---|
203 | |
---|
204 | twitter_connections = g_slist_remove( twitter_connections, ic ); |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * |
---|
209 | */ |
---|
210 | static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
211 | { |
---|
212 | struct twitter_data *td = ic->proto_data; |
---|
213 | |
---|
214 | if (g_strncasecmp(who, "twitter_", 8) == 0 && |
---|
215 | g_strcasecmp(who + 8, ic->acc->user) == 0) |
---|
216 | { |
---|
217 | if( set_getbool( &ic->acc->set, "oauth" ) && td->oauth_info ) |
---|
218 | { |
---|
219 | oauth_access_token( TWITTER_OAUTH_ACCESS_TOKEN, message, td->oauth_info ); |
---|
220 | td->oauth_info = NULL; |
---|
221 | } |
---|
222 | else |
---|
223 | twitter_post_status(ic, message); |
---|
224 | } |
---|
225 | else |
---|
226 | { |
---|
227 | twitter_direct_messages_new(ic, who, message); |
---|
228 | } |
---|
229 | return( 0 ); |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * |
---|
234 | */ |
---|
235 | static void twitter_set_my_name( struct im_connection *ic, char *info ) |
---|
236 | { |
---|
237 | } |
---|
238 | |
---|
239 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
240 | { |
---|
241 | } |
---|
242 | |
---|
243 | static void twitter_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
244 | { |
---|
245 | } |
---|
246 | |
---|
247 | static void twitter_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
248 | { |
---|
249 | } |
---|
250 | |
---|
251 | static void twitter_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
252 | { |
---|
253 | if( c && message ) |
---|
254 | twitter_post_status(c->ic, message); |
---|
255 | } |
---|
256 | |
---|
257 | static void twitter_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
258 | { |
---|
259 | } |
---|
260 | |
---|
261 | static void twitter_chat_leave( struct groupchat *c ) |
---|
262 | { |
---|
263 | struct twitter_data *td = c->ic->proto_data; |
---|
264 | |
---|
265 | if( c != td->home_timeline_gc ) |
---|
266 | return; /* WTF? */ |
---|
267 | |
---|
268 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
269 | tweets come in. */ |
---|
270 | imcb_chat_free(td->home_timeline_gc); |
---|
271 | td->home_timeline_gc = NULL; |
---|
272 | } |
---|
273 | |
---|
274 | static struct groupchat *twitter_chat_with( struct im_connection *ic, char *who ) |
---|
275 | { |
---|
276 | return NULL; |
---|
277 | } |
---|
278 | |
---|
279 | static void twitter_keepalive( struct im_connection *ic ) |
---|
280 | { |
---|
281 | } |
---|
282 | |
---|
283 | static void twitter_add_permit( struct im_connection *ic, char *who ) |
---|
284 | { |
---|
285 | } |
---|
286 | |
---|
287 | static void twitter_rem_permit( struct im_connection *ic, char *who ) |
---|
288 | { |
---|
289 | } |
---|
290 | |
---|
291 | static void twitter_add_deny( struct im_connection *ic, char *who ) |
---|
292 | { |
---|
293 | } |
---|
294 | |
---|
295 | static void twitter_rem_deny( struct im_connection *ic, char *who ) |
---|
296 | { |
---|
297 | } |
---|
298 | |
---|
299 | static int twitter_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
300 | { |
---|
301 | return( 1 ); |
---|
302 | } |
---|
303 | |
---|
304 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
305 | //{ |
---|
306 | // return value; |
---|
307 | //} |
---|
308 | |
---|
309 | void twitter_initmodule() |
---|
310 | { |
---|
311 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
312 | |
---|
313 | ret->name = "twitter"; |
---|
314 | ret->login = twitter_login; |
---|
315 | ret->init = twitter_init; |
---|
316 | ret->logout = twitter_logout; |
---|
317 | ret->buddy_msg = twitter_buddy_msg; |
---|
318 | ret->get_info = twitter_get_info; |
---|
319 | ret->set_my_name = twitter_set_my_name; |
---|
320 | ret->add_buddy = twitter_add_buddy; |
---|
321 | ret->remove_buddy = twitter_remove_buddy; |
---|
322 | ret->chat_msg = twitter_chat_msg; |
---|
323 | ret->chat_invite = twitter_chat_invite; |
---|
324 | ret->chat_leave = twitter_chat_leave; |
---|
325 | ret->chat_with = twitter_chat_with; |
---|
326 | ret->keepalive = twitter_keepalive; |
---|
327 | ret->add_permit = twitter_add_permit; |
---|
328 | ret->rem_permit = twitter_rem_permit; |
---|
329 | ret->add_deny = twitter_add_deny; |
---|
330 | ret->rem_deny = twitter_rem_deny; |
---|
331 | ret->send_typing = twitter_send_typing; |
---|
332 | ret->handle_cmp = g_strcasecmp; |
---|
333 | |
---|
334 | register_protocol(ret); |
---|
335 | |
---|
336 | // Initialise the twitter_connections GSList. |
---|
337 | twitter_connections = NULL; |
---|
338 | } |
---|
339 | |
---|