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