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 | static gboolean twitter_length_check(struct im_connection *ic, gchar * msg) |
---|
200 | { |
---|
201 | int max = set_getint(&ic->acc->set, "message_length"), len; |
---|
202 | |
---|
203 | if (max == 0 || (len = g_utf8_strlen(msg, -1)) <= max) |
---|
204 | return TRUE; |
---|
205 | |
---|
206 | imcb_error(ic, "Maximum message length exceeded: %d > %d", len, max); |
---|
207 | |
---|
208 | return FALSE; |
---|
209 | } |
---|
210 | |
---|
211 | static void twitter_init(account_t * acc) |
---|
212 | { |
---|
213 | set_t *s; |
---|
214 | char *def_url; |
---|
215 | char *def_oauth; |
---|
216 | |
---|
217 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
218 | def_url = TWITTER_API_URL; |
---|
219 | def_oauth = "true"; |
---|
220 | } else { /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */ |
---|
221 | def_url = IDENTICA_API_URL; |
---|
222 | def_oauth = "true"; |
---|
223 | } |
---|
224 | |
---|
225 | s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc); |
---|
226 | |
---|
227 | s = set_add(&acc->set, "base_url", def_url, NULL, acc); |
---|
228 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
229 | |
---|
230 | s = set_add(&acc->set, "commands", "true", set_eval_bool, acc); |
---|
231 | |
---|
232 | s = set_add(&acc->set, "fetch_interval", "60", set_eval_int, acc); |
---|
233 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
234 | |
---|
235 | s = set_add(&acc->set, "fetch_mentions", "true", set_eval_bool, acc); |
---|
236 | |
---|
237 | s = set_add(&acc->set, "message_length", "140", set_eval_int, acc); |
---|
238 | |
---|
239 | s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc); |
---|
240 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
241 | |
---|
242 | s = set_add(&acc->set, "oauth", def_oauth, set_eval_oauth, acc); |
---|
243 | |
---|
244 | s = set_add(&acc->set, "show_ids", "false", set_eval_bool, acc); |
---|
245 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
246 | |
---|
247 | s = set_add(&acc->set, "show_old_mentions", "true", set_eval_bool, acc); |
---|
248 | |
---|
249 | s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc); |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * Login method. Since the twitter API works with seperate HTTP request we |
---|
254 | * only save the user and pass to the twitter_data object. |
---|
255 | */ |
---|
256 | static void twitter_login(account_t * acc) |
---|
257 | { |
---|
258 | struct im_connection *ic = imcb_new(acc); |
---|
259 | struct twitter_data *td; |
---|
260 | char name[strlen(acc->user) + 9]; |
---|
261 | url_t url; |
---|
262 | char *s; |
---|
263 | |
---|
264 | if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) || |
---|
265 | (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) { |
---|
266 | imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url")); |
---|
267 | imc_logout(ic, FALSE); |
---|
268 | return; |
---|
269 | } |
---|
270 | |
---|
271 | imcb_log(ic, "Connecting"); |
---|
272 | |
---|
273 | twitter_connections = g_slist_append(twitter_connections, ic); |
---|
274 | td = g_new0(struct twitter_data, 1); |
---|
275 | ic->proto_data = td; |
---|
276 | td->user = g_strdup(acc->user); |
---|
277 | |
---|
278 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
279 | td->url_port = url.port; |
---|
280 | td->url_host = g_strdup(url.host); |
---|
281 | if (strcmp(url.file, "/") != 0) |
---|
282 | td->url_path = g_strdup(url.file); |
---|
283 | else { |
---|
284 | td->url_path = g_strdup(""); |
---|
285 | if (g_str_has_suffix(url.host, "twitter.com")) |
---|
286 | /* May fire for people who turned on HTTPS. */ |
---|
287 | imcb_error(ic, "Warning: Twitter requires a version number in API calls " |
---|
288 | "now. Try resetting the base_url account setting."); |
---|
289 | } |
---|
290 | |
---|
291 | /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com |
---|
292 | into twitter, and try to be sensible if we get anything else. */ |
---|
293 | td->prefix = g_strdup(url.host); |
---|
294 | if (g_str_has_suffix(td->prefix, ".com")) |
---|
295 | td->prefix[strlen(url.host) - 4] = '\0'; |
---|
296 | if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) { |
---|
297 | /* If we have at least 3 chars after the last dot, cut off the rest. |
---|
298 | (mostly a www/api prefix or sth) */ |
---|
299 | s = g_strdup(s + 1); |
---|
300 | g_free(td->prefix); |
---|
301 | td->prefix = s; |
---|
302 | } |
---|
303 | |
---|
304 | if (strstr(acc->pass, "oauth_token=")) |
---|
305 | td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic)); |
---|
306 | |
---|
307 | sprintf(name, "%s_%s", td->prefix, acc->user); |
---|
308 | imcb_add_buddy(ic, name, NULL); |
---|
309 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
310 | |
---|
311 | if (set_getbool(&acc->set, "show_ids")) |
---|
312 | td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH); |
---|
313 | |
---|
314 | twitter_login_finish(ic); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * Logout method. Just free the twitter_data. |
---|
319 | */ |
---|
320 | static void twitter_logout(struct im_connection *ic) |
---|
321 | { |
---|
322 | struct twitter_data *td = ic->proto_data; |
---|
323 | |
---|
324 | // Set the status to logged out. |
---|
325 | ic->flags &= ~OPT_LOGGED_IN; |
---|
326 | |
---|
327 | // Remove the main_loop function from the function queue. |
---|
328 | b_event_remove(td->main_loop_id); |
---|
329 | |
---|
330 | if (td->timeline_gc) |
---|
331 | imcb_chat_free(td->timeline_gc); |
---|
332 | |
---|
333 | if (td) { |
---|
334 | oauth_info_free(td->oauth_info); |
---|
335 | g_free(td->user); |
---|
336 | g_free(td->prefix); |
---|
337 | g_free(td->url_host); |
---|
338 | g_free(td->url_path); |
---|
339 | g_free(td->log); |
---|
340 | g_free(td); |
---|
341 | } |
---|
342 | |
---|
343 | twitter_connections = g_slist_remove(twitter_connections, ic); |
---|
344 | } |
---|
345 | |
---|
346 | static void twitter_handle_command(struct im_connection *ic, char *message); |
---|
347 | |
---|
348 | /** |
---|
349 | * |
---|
350 | */ |
---|
351 | static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
352 | { |
---|
353 | struct twitter_data *td = ic->proto_data; |
---|
354 | int plen = strlen(td->prefix); |
---|
355 | |
---|
356 | if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' && |
---|
357 | g_strcasecmp(who + plen + 1, ic->acc->user) == 0) { |
---|
358 | if (set_getbool(&ic->acc->set, "oauth") && |
---|
359 | td->oauth_info && td->oauth_info->token == NULL) { |
---|
360 | char pin[strlen(message) + 1], *s; |
---|
361 | |
---|
362 | strcpy(pin, message); |
---|
363 | for (s = pin + sizeof(pin) - 2; s > pin && isspace(*s); s--) |
---|
364 | *s = '\0'; |
---|
365 | for (s = pin; *s && isspace(*s); s++) { |
---|
366 | } |
---|
367 | |
---|
368 | if (!oauth_access_token(s, td->oauth_info)) { |
---|
369 | imcb_error(ic, "OAuth error: %s", |
---|
370 | "Failed to send access token request"); |
---|
371 | imc_logout(ic, TRUE); |
---|
372 | return FALSE; |
---|
373 | } |
---|
374 | } else |
---|
375 | twitter_handle_command(ic, message); |
---|
376 | } else { |
---|
377 | twitter_direct_messages_new(ic, who, message); |
---|
378 | } |
---|
379 | return (0); |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * |
---|
384 | */ |
---|
385 | static void twitter_set_my_name(struct im_connection *ic, char *info) |
---|
386 | { |
---|
387 | } |
---|
388 | |
---|
389 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
390 | { |
---|
391 | } |
---|
392 | |
---|
393 | static void twitter_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
394 | { |
---|
395 | twitter_friendships_create_destroy(ic, who, 1); |
---|
396 | } |
---|
397 | |
---|
398 | static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
399 | { |
---|
400 | twitter_friendships_create_destroy(ic, who, 0); |
---|
401 | } |
---|
402 | |
---|
403 | static void twitter_chat_msg(struct groupchat *c, char *message, int flags) |
---|
404 | { |
---|
405 | if (c && message) |
---|
406 | twitter_handle_command(c->ic, message); |
---|
407 | } |
---|
408 | |
---|
409 | static void twitter_chat_invite(struct groupchat *c, char *who, char *message) |
---|
410 | { |
---|
411 | } |
---|
412 | |
---|
413 | static void twitter_chat_leave(struct groupchat *c) |
---|
414 | { |
---|
415 | struct twitter_data *td = c->ic->proto_data; |
---|
416 | |
---|
417 | if (c != td->timeline_gc) |
---|
418 | return; /* WTF? */ |
---|
419 | |
---|
420 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
421 | tweets come in. */ |
---|
422 | imcb_chat_free(td->timeline_gc); |
---|
423 | td->timeline_gc = NULL; |
---|
424 | } |
---|
425 | |
---|
426 | static void twitter_keepalive(struct im_connection *ic) |
---|
427 | { |
---|
428 | } |
---|
429 | |
---|
430 | static void twitter_add_permit(struct im_connection *ic, char *who) |
---|
431 | { |
---|
432 | } |
---|
433 | |
---|
434 | static void twitter_rem_permit(struct im_connection *ic, char *who) |
---|
435 | { |
---|
436 | } |
---|
437 | |
---|
438 | static void twitter_add_deny(struct im_connection *ic, char *who) |
---|
439 | { |
---|
440 | } |
---|
441 | |
---|
442 | static void twitter_rem_deny(struct im_connection *ic, char *who) |
---|
443 | { |
---|
444 | } |
---|
445 | |
---|
446 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
447 | //{ |
---|
448 | // return value; |
---|
449 | //} |
---|
450 | |
---|
451 | static void twitter_buddy_data_add(struct bee_user *bu) |
---|
452 | { |
---|
453 | bu->data = g_new0(struct twitter_user_data, 1); |
---|
454 | } |
---|
455 | |
---|
456 | static void twitter_buddy_data_free(struct bee_user *bu) |
---|
457 | { |
---|
458 | g_free(bu->data); |
---|
459 | } |
---|
460 | |
---|
461 | static void twitter_handle_command(struct im_connection *ic, char *message) |
---|
462 | { |
---|
463 | struct twitter_data *td = ic->proto_data; |
---|
464 | char *cmds, **cmd, *new = NULL; |
---|
465 | guint64 in_reply_to = 0; |
---|
466 | |
---|
467 | cmds = g_strdup(message); |
---|
468 | cmd = split_command_parts(cmds); |
---|
469 | |
---|
470 | if (cmd[0] == NULL) { |
---|
471 | g_free(cmds); |
---|
472 | return; |
---|
473 | } else if (!set_getbool(&ic->acc->set, "commands")) { |
---|
474 | /* Not supporting commands. */ |
---|
475 | } else if (g_strcasecmp(cmd[0], "undo") == 0) { |
---|
476 | guint64 id; |
---|
477 | |
---|
478 | if (cmd[1] == NULL) |
---|
479 | twitter_status_destroy(ic, td->last_status_id); |
---|
480 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
481 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
482 | id = td->log[id].id; |
---|
483 | |
---|
484 | twitter_status_destroy(ic, id); |
---|
485 | } else |
---|
486 | twitter_msg(ic, "Could not undo last action"); |
---|
487 | |
---|
488 | g_free(cmds); |
---|
489 | return; |
---|
490 | } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) { |
---|
491 | twitter_add_buddy(ic, cmd[1], NULL); |
---|
492 | g_free(cmds); |
---|
493 | return; |
---|
494 | } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) { |
---|
495 | twitter_remove_buddy(ic, cmd[1], NULL); |
---|
496 | g_free(cmds); |
---|
497 | return; |
---|
498 | } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) { |
---|
499 | struct twitter_user_data *tud; |
---|
500 | bee_user_t *bu; |
---|
501 | guint64 id; |
---|
502 | |
---|
503 | if (g_str_has_prefix(cmd[1], "#") && |
---|
504 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
505 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
506 | id = td->log[id].id; |
---|
507 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
508 | (tud = bu->data) && tud->last_id) |
---|
509 | id = tud->last_id; |
---|
510 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1){ |
---|
511 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
512 | id = td->log[id].id; |
---|
513 | } |
---|
514 | |
---|
515 | td->last_status_id = 0; |
---|
516 | if (id) |
---|
517 | twitter_status_retweet(ic, id); |
---|
518 | else |
---|
519 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
520 | "post any statuses recently", cmd[1]); |
---|
521 | |
---|
522 | g_free(cmds); |
---|
523 | return; |
---|
524 | } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) { |
---|
525 | struct twitter_user_data *tud; |
---|
526 | bee_user_t *bu = NULL; |
---|
527 | guint64 id = 0; |
---|
528 | |
---|
529 | if (g_str_has_prefix(cmd[1], "#") && |
---|
530 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
531 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
532 | bu = td->log[id].bu; |
---|
533 | if (g_slist_find(ic->bee->users, bu)) |
---|
534 | id = td->log[id].id; |
---|
535 | else |
---|
536 | bu = NULL; |
---|
537 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
538 | (tud = bu->data) && tud->last_id) { |
---|
539 | id = tud->last_id; |
---|
540 | } else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
541 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
542 | bu = td->log[id].bu; |
---|
543 | if (g_slist_find(ic->bee->users, bu)) |
---|
544 | id = td->log[id].id; |
---|
545 | else |
---|
546 | bu = NULL; |
---|
547 | } |
---|
548 | |
---|
549 | if (!id || !bu) { |
---|
550 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
551 | "post any statuses recently", cmd[1]); |
---|
552 | return; |
---|
553 | } |
---|
554 | message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0])); |
---|
555 | in_reply_to = id; |
---|
556 | } else if (g_strcasecmp(cmd[0], "post") == 0) { |
---|
557 | message += 5; |
---|
558 | } |
---|
559 | |
---|
560 | { |
---|
561 | char *s; |
---|
562 | bee_user_t *bu; |
---|
563 | |
---|
564 | if (!twitter_length_check(ic, message)) { |
---|
565 | g_free(new); |
---|
566 | g_free(cmds); |
---|
567 | return; |
---|
568 | } |
---|
569 | |
---|
570 | s = cmd[0] + strlen(cmd[0]) - 1; |
---|
571 | if (!new && s > cmd[0] && (*s == ':' || *s == ',')) { |
---|
572 | *s = '\0'; |
---|
573 | |
---|
574 | if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) { |
---|
575 | struct twitter_user_data *tud = bu->data; |
---|
576 | |
---|
577 | new = g_strdup_printf("@%s %s", bu->handle, |
---|
578 | message + (s - cmd[0]) + 2); |
---|
579 | message = new; |
---|
580 | |
---|
581 | if (time(NULL) < tud->last_time + |
---|
582 | set_getint(&ic->acc->set, "auto_reply_timeout")) |
---|
583 | in_reply_to = tud->last_id; |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | /* If the user runs undo between this request and its response |
---|
588 | this would delete the second-last Tweet. Prevent that. */ |
---|
589 | td->last_status_id = 0; |
---|
590 | twitter_post_status(ic, message, in_reply_to); |
---|
591 | g_free(new); |
---|
592 | } |
---|
593 | g_free(cmds); |
---|
594 | } |
---|
595 | |
---|
596 | void twitter_initmodule() |
---|
597 | { |
---|
598 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
599 | |
---|
600 | ret->options = OPT_NOOTR; |
---|
601 | ret->name = "twitter"; |
---|
602 | ret->login = twitter_login; |
---|
603 | ret->init = twitter_init; |
---|
604 | ret->logout = twitter_logout; |
---|
605 | ret->buddy_msg = twitter_buddy_msg; |
---|
606 | ret->get_info = twitter_get_info; |
---|
607 | ret->set_my_name = twitter_set_my_name; |
---|
608 | ret->add_buddy = twitter_add_buddy; |
---|
609 | ret->remove_buddy = twitter_remove_buddy; |
---|
610 | ret->chat_msg = twitter_chat_msg; |
---|
611 | ret->chat_invite = twitter_chat_invite; |
---|
612 | ret->chat_leave = twitter_chat_leave; |
---|
613 | ret->keepalive = twitter_keepalive; |
---|
614 | ret->add_permit = twitter_add_permit; |
---|
615 | ret->rem_permit = twitter_rem_permit; |
---|
616 | ret->add_deny = twitter_add_deny; |
---|
617 | ret->rem_deny = twitter_rem_deny; |
---|
618 | ret->buddy_data_add = twitter_buddy_data_add; |
---|
619 | ret->buddy_data_free = twitter_buddy_data_free; |
---|
620 | ret->handle_cmp = g_strcasecmp; |
---|
621 | |
---|
622 | register_protocol(ret); |
---|
623 | |
---|
624 | /* And an identi.ca variant: */ |
---|
625 | ret = g_memdup(ret, sizeof(struct prpl)); |
---|
626 | ret->name = "identica"; |
---|
627 | register_protocol(ret); |
---|
628 | } |
---|