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