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