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 | * Copyright 2010-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
8 | * * |
---|
9 | * This library is free software; you can redistribute it and/or * |
---|
10 | * modify it under the terms of the GNU Lesser General Public * |
---|
11 | * License as published by the Free Software Foundation, version * |
---|
12 | * 2.1. * |
---|
13 | * * |
---|
14 | * This library is distributed in the hope that it will be useful, * |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
17 | * Lesser General Public License for more details. * |
---|
18 | * * |
---|
19 | * You should have received a copy of the GNU Lesser General Public License * |
---|
20 | * along with this library; if not, write to the Free Software Foundation, * |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
22 | * * |
---|
23 | ****************************************************************************/ |
---|
24 | |
---|
25 | #include "nogaim.h" |
---|
26 | #include "oauth.h" |
---|
27 | #include "twitter.h" |
---|
28 | #include "twitter_http.h" |
---|
29 | #include "twitter_lib.h" |
---|
30 | #include "url.h" |
---|
31 | |
---|
32 | #define twitter_msg( ic, fmt... ) \ |
---|
33 | do { \ |
---|
34 | struct twitter_data *td = ic->proto_data; \ |
---|
35 | if( td->timeline_gc ) \ |
---|
36 | imcb_chat_log( td->timeline_gc, fmt ); \ |
---|
37 | else \ |
---|
38 | imcb_log( ic, fmt ); \ |
---|
39 | } while( 0 ); |
---|
40 | |
---|
41 | GSList *twitter_connections = NULL; |
---|
42 | |
---|
43 | /** |
---|
44 | * Main loop function |
---|
45 | */ |
---|
46 | gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond) |
---|
47 | { |
---|
48 | struct im_connection *ic = data; |
---|
49 | |
---|
50 | // Check if we are still logged in... |
---|
51 | if (!g_slist_find(twitter_connections, ic)) |
---|
52 | return 0; |
---|
53 | |
---|
54 | // Do stuff.. |
---|
55 | twitter_get_timeline(ic, -1); |
---|
56 | |
---|
57 | // If we are still logged in run this function again after timeout. |
---|
58 | return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN; |
---|
59 | } |
---|
60 | |
---|
61 | static void twitter_main_loop_start(struct im_connection *ic) |
---|
62 | { |
---|
63 | struct twitter_data *td = ic->proto_data; |
---|
64 | |
---|
65 | imcb_log(ic, "Getting initial statuses"); |
---|
66 | |
---|
67 | // Run this once. After this queue the main loop function (or open the |
---|
68 | // stream if available). |
---|
69 | twitter_main_loop(ic, -1, 0); |
---|
70 | |
---|
71 | if (set_getbool(&ic->acc->set, "stream")) { |
---|
72 | /* That fetch was just to get backlog, the stream will give |
---|
73 | us the rest. \o/ */ |
---|
74 | twitter_open_stream(ic); |
---|
75 | |
---|
76 | /* Stream sends keepalives (empty lines) or actual data at |
---|
77 | least twice a minute. Disconnect if this stops. */ |
---|
78 | ic->flags |= OPT_PONGS; |
---|
79 | } else { |
---|
80 | /* Not using the streaming API, so keep polling the old- |
---|
81 | fashioned way. :-( */ |
---|
82 | td->main_loop_id = |
---|
83 | b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000, |
---|
84 | twitter_main_loop, ic); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | static void twitter_oauth_start(struct im_connection *ic); |
---|
89 | |
---|
90 | void twitter_login_finish(struct im_connection *ic) |
---|
91 | { |
---|
92 | struct twitter_data *td = ic->proto_data; |
---|
93 | |
---|
94 | td->flags &= ~TWITTER_DOING_TIMELINE; |
---|
95 | |
---|
96 | if (set_getbool(&ic->acc->set, "oauth") && !td->oauth_info) |
---|
97 | twitter_oauth_start(ic); |
---|
98 | else if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") != 0 && |
---|
99 | !(td->flags & TWITTER_HAVE_FRIENDS)) { |
---|
100 | imcb_log(ic, "Getting contact list"); |
---|
101 | twitter_get_friends_ids(ic, -1); |
---|
102 | } else |
---|
103 | twitter_main_loop_start(ic); |
---|
104 | } |
---|
105 | |
---|
106 | static const struct oauth_service twitter_oauth = { |
---|
107 | "https://api.twitter.com/oauth/request_token", |
---|
108 | "https://api.twitter.com/oauth/access_token", |
---|
109 | "https://api.twitter.com/oauth/authorize", |
---|
110 | .consumer_key = "xsDNKJuNZYkZyMcu914uEA", |
---|
111 | .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo", |
---|
112 | }; |
---|
113 | |
---|
114 | static const struct oauth_service identica_oauth = { |
---|
115 | "https://identi.ca/api/oauth/request_token", |
---|
116 | "https://identi.ca/api/oauth/access_token", |
---|
117 | "https://identi.ca/api/oauth/authorize", |
---|
118 | .consumer_key = "e147ff789fcbd8a5a07963afbb43f9da", |
---|
119 | .consumer_secret = "c596267f277457ec0ce1ab7bb788d828", |
---|
120 | }; |
---|
121 | |
---|
122 | static gboolean twitter_oauth_callback(struct oauth_info *info); |
---|
123 | |
---|
124 | static const struct oauth_service *get_oauth_service(struct im_connection *ic) |
---|
125 | { |
---|
126 | struct twitter_data *td = ic->proto_data; |
---|
127 | |
---|
128 | if (strstr(td->url_host, "identi.ca")) |
---|
129 | return &identica_oauth; |
---|
130 | else |
---|
131 | return &twitter_oauth; |
---|
132 | |
---|
133 | /* Could add more services, or allow configuring your own base URL + |
---|
134 | API keys. */ |
---|
135 | } |
---|
136 | |
---|
137 | static void twitter_oauth_start(struct im_connection *ic) |
---|
138 | { |
---|
139 | struct twitter_data *td = ic->proto_data; |
---|
140 | |
---|
141 | imcb_log(ic, "Requesting OAuth request token"); |
---|
142 | |
---|
143 | td->oauth_info = oauth_request_token(get_oauth_service(ic), twitter_oauth_callback, ic); |
---|
144 | |
---|
145 | /* We need help from the user to complete OAuth login, so don't time |
---|
146 | out on this login. */ |
---|
147 | ic->flags |= OPT_SLOW_LOGIN; |
---|
148 | } |
---|
149 | |
---|
150 | static gboolean twitter_oauth_callback(struct oauth_info *info) |
---|
151 | { |
---|
152 | struct im_connection *ic = info->data; |
---|
153 | struct twitter_data *td; |
---|
154 | |
---|
155 | if (!g_slist_find(twitter_connections, ic)) |
---|
156 | return FALSE; |
---|
157 | |
---|
158 | td = ic->proto_data; |
---|
159 | if (info->stage == OAUTH_REQUEST_TOKEN) { |
---|
160 | char name[strlen(ic->acc->user) + 9], *msg; |
---|
161 | |
---|
162 | if (info->request_token == NULL) { |
---|
163 | imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); |
---|
164 | imc_logout(ic, TRUE); |
---|
165 | return FALSE; |
---|
166 | } |
---|
167 | |
---|
168 | sprintf(name, "%s_%s", td->prefix, ic->acc->user); |
---|
169 | msg = g_strdup_printf("To finish OAuth authentication, please visit " |
---|
170 | "%s and respond with the resulting PIN code.", |
---|
171 | info->auth_url); |
---|
172 | imcb_buddy_msg(ic, name, msg, 0, 0); |
---|
173 | g_free(msg); |
---|
174 | } else if (info->stage == OAUTH_ACCESS_TOKEN) { |
---|
175 | if (info->token == NULL || info->token_secret == NULL) { |
---|
176 | imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); |
---|
177 | imc_logout(ic, TRUE); |
---|
178 | return FALSE; |
---|
179 | } else { |
---|
180 | const char *sn = oauth_params_get(&info->params, "screen_name"); |
---|
181 | |
---|
182 | if (sn != NULL && ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0) { |
---|
183 | imcb_log(ic, "Warning: You logged in via OAuth as %s " |
---|
184 | "instead of %s.", sn, ic->acc->user); |
---|
185 | } |
---|
186 | g_free(td->user); |
---|
187 | td->user = g_strdup(sn); |
---|
188 | } |
---|
189 | |
---|
190 | /* IM mods didn't do this so far and it's ugly but I should |
---|
191 | be able to get away with it... */ |
---|
192 | g_free(ic->acc->pass); |
---|
193 | ic->acc->pass = oauth_to_string(info); |
---|
194 | |
---|
195 | twitter_login_finish(ic); |
---|
196 | } |
---|
197 | |
---|
198 | return TRUE; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | static char *set_eval_mode(set_t * set, char *value) |
---|
203 | { |
---|
204 | if (g_strcasecmp(value, "one") == 0 || |
---|
205 | g_strcasecmp(value, "many") == 0 || g_strcasecmp(value, "chat") == 0) |
---|
206 | return value; |
---|
207 | else |
---|
208 | return NULL; |
---|
209 | } |
---|
210 | |
---|
211 | int twitter_url_len_diff(gchar *msg, unsigned int target_len) |
---|
212 | { |
---|
213 | int url_len_diff = 0; |
---|
214 | |
---|
215 | static GRegex *regex = NULL; |
---|
216 | GMatchInfo *match_info; |
---|
217 | |
---|
218 | if (regex == NULL) |
---|
219 | regex = g_regex_new("(^|\\s)(http(s)?://[^\\s$]+)", 0, 0, NULL); |
---|
220 | |
---|
221 | g_regex_match(regex, msg, 0, &match_info); |
---|
222 | while (g_match_info_matches(match_info)) { |
---|
223 | gchar *url = g_match_info_fetch(match_info, 2); |
---|
224 | url_len_diff += target_len - g_utf8_strlen(url, -1); |
---|
225 | if (g_match_info_fetch(match_info, 3) != NULL) |
---|
226 | url_len_diff += 1; |
---|
227 | g_free(url); |
---|
228 | g_match_info_next(match_info, NULL); |
---|
229 | } |
---|
230 | g_match_info_free(match_info); |
---|
231 | |
---|
232 | return url_len_diff; |
---|
233 | } |
---|
234 | |
---|
235 | static gboolean twitter_length_check(struct im_connection *ic, gchar * msg) |
---|
236 | { |
---|
237 | int max = set_getint(&ic->acc->set, "message_length"), len; |
---|
238 | int target_len = set_getint(&ic->acc->set, "target_url_length"); |
---|
239 | int url_len_diff = 0; |
---|
240 | |
---|
241 | if (target_len > 0) |
---|
242 | url_len_diff = twitter_url_len_diff(msg, target_len); |
---|
243 | |
---|
244 | if (max == 0 || (len = g_utf8_strlen(msg, -1) + url_len_diff) <= max) |
---|
245 | return TRUE; |
---|
246 | |
---|
247 | imcb_error(ic, "Maximum message length exceeded: %d > %d", len, max); |
---|
248 | |
---|
249 | return FALSE; |
---|
250 | } |
---|
251 | |
---|
252 | static void twitter_init(account_t * acc) |
---|
253 | { |
---|
254 | set_t *s; |
---|
255 | char *def_url; |
---|
256 | char *def_tul; |
---|
257 | |
---|
258 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
259 | def_url = TWITTER_API_URL; |
---|
260 | def_tul = "20"; |
---|
261 | } else { /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */ |
---|
262 | def_url = IDENTICA_API_URL; |
---|
263 | def_tul = "0"; |
---|
264 | } |
---|
265 | |
---|
266 | s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc); |
---|
267 | |
---|
268 | s = set_add(&acc->set, "base_url", def_url, NULL, acc); |
---|
269 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
270 | |
---|
271 | s = set_add(&acc->set, "commands", "true", set_eval_bool, acc); |
---|
272 | |
---|
273 | s = set_add(&acc->set, "fetch_interval", "60", set_eval_int, acc); |
---|
274 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
275 | |
---|
276 | s = set_add(&acc->set, "fetch_mentions", "true", set_eval_bool, acc); |
---|
277 | |
---|
278 | s = set_add(&acc->set, "message_length", "140", set_eval_int, acc); |
---|
279 | |
---|
280 | s = set_add(&acc->set, "target_url_length", def_tul, set_eval_int, acc); |
---|
281 | |
---|
282 | s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc); |
---|
283 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
284 | |
---|
285 | s = set_add(&acc->set, "oauth", "true", set_eval_oauth, acc); |
---|
286 | |
---|
287 | s = set_add(&acc->set, "show_ids", "true", set_eval_bool, acc); |
---|
288 | |
---|
289 | s = set_add(&acc->set, "show_old_mentions", "20", set_eval_int, acc); |
---|
290 | |
---|
291 | s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc); |
---|
292 | |
---|
293 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
294 | s = set_add(&acc->set, "stream", "true", set_eval_bool, acc); |
---|
295 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | * Login method. Since the twitter API works with separate HTTP request we |
---|
301 | * only save the user and pass to the twitter_data object. |
---|
302 | */ |
---|
303 | static void twitter_login(account_t * acc) |
---|
304 | { |
---|
305 | struct im_connection *ic = imcb_new(acc); |
---|
306 | struct twitter_data *td; |
---|
307 | char name[strlen(acc->user) + 9]; |
---|
308 | url_t url; |
---|
309 | char *s; |
---|
310 | |
---|
311 | if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) || |
---|
312 | (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) { |
---|
313 | imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url")); |
---|
314 | imc_logout(ic, FALSE); |
---|
315 | return; |
---|
316 | } |
---|
317 | |
---|
318 | if (!strstr(url.host, "twitter.com") && |
---|
319 | set_getbool(&ic->acc->set, "stream")) { |
---|
320 | imcb_error(ic, "Warning: The streaming API is only supported by Twitter, " |
---|
321 | "and you seem to be connecting to a different service."); |
---|
322 | } |
---|
323 | |
---|
324 | imcb_log(ic, "Connecting"); |
---|
325 | |
---|
326 | twitter_connections = g_slist_append(twitter_connections, ic); |
---|
327 | td = g_new0(struct twitter_data, 1); |
---|
328 | ic->proto_data = td; |
---|
329 | td->user = g_strdup(acc->user); |
---|
330 | |
---|
331 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
332 | td->url_port = url.port; |
---|
333 | td->url_host = g_strdup(url.host); |
---|
334 | if (strcmp(url.file, "/") != 0) |
---|
335 | td->url_path = g_strdup(url.file); |
---|
336 | else { |
---|
337 | td->url_path = g_strdup(""); |
---|
338 | if (g_str_has_suffix(url.host, "twitter.com")) |
---|
339 | /* May fire for people who turned on HTTPS. */ |
---|
340 | imcb_error(ic, "Warning: Twitter requires a version number in API calls " |
---|
341 | "now. Try resetting the base_url account setting."); |
---|
342 | } |
---|
343 | |
---|
344 | /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com |
---|
345 | into twitter, and try to be sensible if we get anything else. */ |
---|
346 | td->prefix = g_strdup(url.host); |
---|
347 | if (g_str_has_suffix(td->prefix, ".com")) |
---|
348 | td->prefix[strlen(url.host) - 4] = '\0'; |
---|
349 | if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) { |
---|
350 | /* If we have at least 3 chars after the last dot, cut off the rest. |
---|
351 | (mostly a www/api prefix or sth) */ |
---|
352 | s = g_strdup(s + 1); |
---|
353 | g_free(td->prefix); |
---|
354 | td->prefix = s; |
---|
355 | } |
---|
356 | |
---|
357 | if (strstr(acc->pass, "oauth_token=")) |
---|
358 | td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic)); |
---|
359 | |
---|
360 | sprintf(name, "%s_%s", td->prefix, acc->user); |
---|
361 | imcb_add_buddy(ic, name, NULL); |
---|
362 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
363 | |
---|
364 | td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH); |
---|
365 | td->log_id = -1; |
---|
366 | |
---|
367 | twitter_login_finish(ic); |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * Logout method. Just free the twitter_data. |
---|
372 | */ |
---|
373 | static void twitter_logout(struct im_connection *ic) |
---|
374 | { |
---|
375 | struct twitter_data *td = ic->proto_data; |
---|
376 | |
---|
377 | // Set the status to logged out. |
---|
378 | ic->flags &= ~OPT_LOGGED_IN; |
---|
379 | |
---|
380 | // Remove the main_loop function from the function queue. |
---|
381 | b_event_remove(td->main_loop_id); |
---|
382 | |
---|
383 | if (td->timeline_gc) |
---|
384 | imcb_chat_free(td->timeline_gc); |
---|
385 | |
---|
386 | if (td) { |
---|
387 | http_close(td->stream); |
---|
388 | oauth_info_free(td->oauth_info); |
---|
389 | g_free(td->user); |
---|
390 | g_free(td->prefix); |
---|
391 | g_free(td->url_host); |
---|
392 | g_free(td->url_path); |
---|
393 | g_free(td->log); |
---|
394 | g_free(td); |
---|
395 | } |
---|
396 | |
---|
397 | twitter_connections = g_slist_remove(twitter_connections, ic); |
---|
398 | } |
---|
399 | |
---|
400 | static void twitter_handle_command(struct im_connection *ic, char *message); |
---|
401 | |
---|
402 | /** |
---|
403 | * |
---|
404 | */ |
---|
405 | static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
406 | { |
---|
407 | struct twitter_data *td = ic->proto_data; |
---|
408 | int plen = strlen(td->prefix); |
---|
409 | |
---|
410 | if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' && |
---|
411 | g_strcasecmp(who + plen + 1, ic->acc->user) == 0) { |
---|
412 | if (set_getbool(&ic->acc->set, "oauth") && |
---|
413 | td->oauth_info && td->oauth_info->token == NULL) { |
---|
414 | char pin[strlen(message) + 1], *s; |
---|
415 | |
---|
416 | strcpy(pin, message); |
---|
417 | for (s = pin + sizeof(pin) - 2; s > pin && isspace(*s); s--) |
---|
418 | *s = '\0'; |
---|
419 | for (s = pin; *s && isspace(*s); s++) { |
---|
420 | } |
---|
421 | |
---|
422 | if (!oauth_access_token(s, td->oauth_info)) { |
---|
423 | imcb_error(ic, "OAuth error: %s", |
---|
424 | "Failed to send access token request"); |
---|
425 | imc_logout(ic, TRUE); |
---|
426 | return FALSE; |
---|
427 | } |
---|
428 | } else |
---|
429 | twitter_handle_command(ic, message); |
---|
430 | } else { |
---|
431 | twitter_direct_messages_new(ic, who, message); |
---|
432 | } |
---|
433 | return (0); |
---|
434 | } |
---|
435 | |
---|
436 | /** |
---|
437 | * |
---|
438 | */ |
---|
439 | static void twitter_set_my_name(struct im_connection *ic, char *info) |
---|
440 | { |
---|
441 | } |
---|
442 | |
---|
443 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
444 | { |
---|
445 | } |
---|
446 | |
---|
447 | static void twitter_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
448 | { |
---|
449 | twitter_friendships_create_destroy(ic, who, 1); |
---|
450 | } |
---|
451 | |
---|
452 | static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
453 | { |
---|
454 | twitter_friendships_create_destroy(ic, who, 0); |
---|
455 | } |
---|
456 | |
---|
457 | static void twitter_chat_msg(struct groupchat *c, char *message, int flags) |
---|
458 | { |
---|
459 | if (c && message) |
---|
460 | twitter_handle_command(c->ic, message); |
---|
461 | } |
---|
462 | |
---|
463 | static void twitter_chat_invite(struct groupchat *c, char *who, char *message) |
---|
464 | { |
---|
465 | } |
---|
466 | |
---|
467 | static void twitter_chat_leave(struct groupchat *c) |
---|
468 | { |
---|
469 | struct twitter_data *td = c->ic->proto_data; |
---|
470 | |
---|
471 | if (c != td->timeline_gc) |
---|
472 | return; /* WTF? */ |
---|
473 | |
---|
474 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
475 | tweets come in. */ |
---|
476 | imcb_chat_free(td->timeline_gc); |
---|
477 | td->timeline_gc = NULL; |
---|
478 | } |
---|
479 | |
---|
480 | static void twitter_keepalive(struct im_connection *ic) |
---|
481 | { |
---|
482 | } |
---|
483 | |
---|
484 | static void twitter_add_permit(struct im_connection *ic, char *who) |
---|
485 | { |
---|
486 | } |
---|
487 | |
---|
488 | static void twitter_rem_permit(struct im_connection *ic, char *who) |
---|
489 | { |
---|
490 | } |
---|
491 | |
---|
492 | static void twitter_add_deny(struct im_connection *ic, char *who) |
---|
493 | { |
---|
494 | } |
---|
495 | |
---|
496 | static void twitter_rem_deny(struct im_connection *ic, char *who) |
---|
497 | { |
---|
498 | } |
---|
499 | |
---|
500 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
501 | //{ |
---|
502 | // return value; |
---|
503 | //} |
---|
504 | |
---|
505 | static void twitter_buddy_data_add(struct bee_user *bu) |
---|
506 | { |
---|
507 | bu->data = g_new0(struct twitter_user_data, 1); |
---|
508 | } |
---|
509 | |
---|
510 | static void twitter_buddy_data_free(struct bee_user *bu) |
---|
511 | { |
---|
512 | g_free(bu->data); |
---|
513 | } |
---|
514 | |
---|
515 | /** Convert the given bitlbee tweet ID, bitlbee username, or twitter tweet ID |
---|
516 | * into a twitter tweet ID. |
---|
517 | * |
---|
518 | * Returns 0 if the user provides garbage. |
---|
519 | */ |
---|
520 | static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, struct twitter_data *td, char *arg) { |
---|
521 | struct twitter_user_data *tud; |
---|
522 | bee_user_t *bu; |
---|
523 | guint64 id = 0; |
---|
524 | if (g_str_has_prefix(arg, "#") && |
---|
525 | sscanf(arg + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
526 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
527 | id = td->log[id].id; |
---|
528 | } else if ((bu = bee_user_by_handle(ic->bee, ic, arg)) && |
---|
529 | (tud = bu->data) && tud->last_id) |
---|
530 | id = tud->last_id; |
---|
531 | else if (sscanf(arg, "%" G_GUINT64_FORMAT, &id) == 1){ |
---|
532 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
533 | id = td->log[id].id; |
---|
534 | } |
---|
535 | return id; |
---|
536 | } |
---|
537 | |
---|
538 | static void twitter_handle_command(struct im_connection *ic, char *message) |
---|
539 | { |
---|
540 | struct twitter_data *td = ic->proto_data; |
---|
541 | char *cmds, **cmd, *new = NULL; |
---|
542 | guint64 in_reply_to = 0; |
---|
543 | |
---|
544 | cmds = g_strdup(message); |
---|
545 | cmd = split_command_parts(cmds); |
---|
546 | |
---|
547 | if (cmd[0] == NULL) { |
---|
548 | g_free(cmds); |
---|
549 | return; |
---|
550 | } else if (!set_getbool(&ic->acc->set, "commands")) { |
---|
551 | /* Not supporting commands. */ |
---|
552 | } else if (g_strcasecmp(cmd[0], "undo") == 0) { |
---|
553 | guint64 id; |
---|
554 | |
---|
555 | if (cmd[1] == NULL) |
---|
556 | twitter_status_destroy(ic, td->last_status_id); |
---|
557 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
558 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
559 | id = td->log[id].id; |
---|
560 | |
---|
561 | twitter_status_destroy(ic, id); |
---|
562 | } else |
---|
563 | twitter_msg(ic, "Could not undo last action"); |
---|
564 | |
---|
565 | g_free(cmds); |
---|
566 | return; |
---|
567 | } else if (g_strcasecmp(cmd[0], "favourite") == 0 && cmd[1]) { |
---|
568 | guint64 id; |
---|
569 | if ((id = twitter_message_id_from_command_arg(ic, td, cmd[1]))) { |
---|
570 | twitter_favourite_tweet(ic, id); |
---|
571 | } else { |
---|
572 | twitter_msg(ic, "Please provide a message ID or username."); |
---|
573 | } |
---|
574 | g_free(cmds); |
---|
575 | return; |
---|
576 | } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) { |
---|
577 | twitter_add_buddy(ic, cmd[1], NULL); |
---|
578 | g_free(cmds); |
---|
579 | return; |
---|
580 | } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) { |
---|
581 | twitter_remove_buddy(ic, cmd[1], NULL); |
---|
582 | g_free(cmds); |
---|
583 | return; |
---|
584 | } else if ((g_strcasecmp(cmd[0], "report") == 0 || |
---|
585 | g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) { |
---|
586 | char * screen_name; |
---|
587 | guint64 id; |
---|
588 | screen_name = cmd[1]; |
---|
589 | /* Report nominally works on users but look up the user who |
---|
590 | posted the given ID if the user wants to do it that way */ |
---|
591 | if (g_str_has_prefix(cmd[1], "#") && |
---|
592 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
593 | if (id < TWITTER_LOG_LENGTH && td->log) { |
---|
594 | if (g_slist_find(ic->bee->users, td->log[id].bu)) { |
---|
595 | screen_name = td->log[id].bu->handle; |
---|
596 | } |
---|
597 | } |
---|
598 | } |
---|
599 | twitter_report_spam(ic, screen_name); |
---|
600 | g_free(cmds); |
---|
601 | return; |
---|
602 | } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) { |
---|
603 | guint64 id = twitter_message_id_from_command_arg(ic, td, cmd[1]); |
---|
604 | |
---|
605 | td->last_status_id = 0; |
---|
606 | if (id) |
---|
607 | twitter_status_retweet(ic, id); |
---|
608 | else |
---|
609 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
610 | "post any statuses recently", cmd[1]); |
---|
611 | |
---|
612 | g_free(cmds); |
---|
613 | return; |
---|
614 | } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) { |
---|
615 | struct twitter_user_data *tud; |
---|
616 | bee_user_t *bu = NULL; |
---|
617 | guint64 id = 0; |
---|
618 | |
---|
619 | if (g_str_has_prefix(cmd[1], "#") && |
---|
620 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
621 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
622 | bu = td->log[id].bu; |
---|
623 | if (g_slist_find(ic->bee->users, bu)) |
---|
624 | id = td->log[id].id; |
---|
625 | else |
---|
626 | bu = NULL; |
---|
627 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
628 | (tud = bu->data) && tud->last_id) { |
---|
629 | id = tud->last_id; |
---|
630 | } else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
631 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
632 | bu = td->log[id].bu; |
---|
633 | if (g_slist_find(ic->bee->users, bu)) |
---|
634 | id = td->log[id].id; |
---|
635 | else |
---|
636 | bu = NULL; |
---|
637 | } |
---|
638 | |
---|
639 | if (!id || !bu) { |
---|
640 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
641 | "post any statuses recently", cmd[1]); |
---|
642 | g_free(cmds); |
---|
643 | return; |
---|
644 | } |
---|
645 | message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0])); |
---|
646 | in_reply_to = id; |
---|
647 | } else if (g_strcasecmp(cmd[0], "post") == 0) { |
---|
648 | message += 5; |
---|
649 | } |
---|
650 | |
---|
651 | { |
---|
652 | char *s; |
---|
653 | bee_user_t *bu; |
---|
654 | |
---|
655 | if (!twitter_length_check(ic, message)) { |
---|
656 | g_free(new); |
---|
657 | g_free(cmds); |
---|
658 | return; |
---|
659 | } |
---|
660 | |
---|
661 | s = cmd[0] + strlen(cmd[0]) - 1; |
---|
662 | if (!new && s > cmd[0] && (*s == ':' || *s == ',')) { |
---|
663 | *s = '\0'; |
---|
664 | |
---|
665 | if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) { |
---|
666 | struct twitter_user_data *tud = bu->data; |
---|
667 | |
---|
668 | new = g_strdup_printf("@%s %s", bu->handle, |
---|
669 | message + (s - cmd[0]) + 2); |
---|
670 | message = new; |
---|
671 | |
---|
672 | if (time(NULL) < tud->last_time + |
---|
673 | set_getint(&ic->acc->set, "auto_reply_timeout")) |
---|
674 | in_reply_to = tud->last_id; |
---|
675 | } |
---|
676 | } |
---|
677 | |
---|
678 | /* If the user runs undo between this request and its response |
---|
679 | this would delete the second-last Tweet. Prevent that. */ |
---|
680 | td->last_status_id = 0; |
---|
681 | twitter_post_status(ic, message, in_reply_to); |
---|
682 | g_free(new); |
---|
683 | } |
---|
684 | g_free(cmds); |
---|
685 | } |
---|
686 | |
---|
687 | void twitter_initmodule() |
---|
688 | { |
---|
689 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
690 | |
---|
691 | ret->options = OPT_NOOTR; |
---|
692 | ret->name = "twitter"; |
---|
693 | ret->login = twitter_login; |
---|
694 | ret->init = twitter_init; |
---|
695 | ret->logout = twitter_logout; |
---|
696 | ret->buddy_msg = twitter_buddy_msg; |
---|
697 | ret->get_info = twitter_get_info; |
---|
698 | ret->set_my_name = twitter_set_my_name; |
---|
699 | ret->add_buddy = twitter_add_buddy; |
---|
700 | ret->remove_buddy = twitter_remove_buddy; |
---|
701 | ret->chat_msg = twitter_chat_msg; |
---|
702 | ret->chat_invite = twitter_chat_invite; |
---|
703 | ret->chat_leave = twitter_chat_leave; |
---|
704 | ret->keepalive = twitter_keepalive; |
---|
705 | ret->add_permit = twitter_add_permit; |
---|
706 | ret->rem_permit = twitter_rem_permit; |
---|
707 | ret->add_deny = twitter_add_deny; |
---|
708 | ret->rem_deny = twitter_rem_deny; |
---|
709 | ret->buddy_data_add = twitter_buddy_data_add; |
---|
710 | ret->buddy_data_free = twitter_buddy_data_free; |
---|
711 | ret->handle_cmp = g_strcasecmp; |
---|
712 | |
---|
713 | register_protocol(ret); |
---|
714 | |
---|
715 | /* And an identi.ca variant: */ |
---|
716 | ret = g_memdup(ret, sizeof(struct prpl)); |
---|
717 | ret->name = "identica"; |
---|
718 | register_protocol(ret); |
---|
719 | } |
---|