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