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 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
289 | |
---|
290 | s = set_add(&acc->set, "show_old_mentions", "20", set_eval_int, acc); |
---|
291 | |
---|
292 | s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc); |
---|
293 | |
---|
294 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
295 | s = set_add(&acc->set, "stream", "true", set_eval_bool, acc); |
---|
296 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * Login method. Since the twitter API works with separate HTTP request we |
---|
302 | * only save the user and pass to the twitter_data object. |
---|
303 | */ |
---|
304 | static void twitter_login(account_t * acc) |
---|
305 | { |
---|
306 | struct im_connection *ic = imcb_new(acc); |
---|
307 | struct twitter_data *td; |
---|
308 | char name[strlen(acc->user) + 9]; |
---|
309 | url_t url; |
---|
310 | char *s; |
---|
311 | |
---|
312 | if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) || |
---|
313 | (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) { |
---|
314 | imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url")); |
---|
315 | imc_logout(ic, FALSE); |
---|
316 | return; |
---|
317 | } |
---|
318 | |
---|
319 | if (!strstr(url.host, "twitter.com") && |
---|
320 | set_getbool(&ic->acc->set, "stream")) { |
---|
321 | imcb_error(ic, "Warning: The streaming API is only supported by Twitter, " |
---|
322 | "and you seem to be connecting to a different service."); |
---|
323 | } |
---|
324 | |
---|
325 | imcb_log(ic, "Connecting"); |
---|
326 | |
---|
327 | twitter_connections = g_slist_append(twitter_connections, ic); |
---|
328 | td = g_new0(struct twitter_data, 1); |
---|
329 | ic->proto_data = td; |
---|
330 | td->user = g_strdup(acc->user); |
---|
331 | |
---|
332 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
333 | td->url_port = url.port; |
---|
334 | td->url_host = g_strdup(url.host); |
---|
335 | if (strcmp(url.file, "/") != 0) |
---|
336 | td->url_path = g_strdup(url.file); |
---|
337 | else { |
---|
338 | td->url_path = g_strdup(""); |
---|
339 | if (g_str_has_suffix(url.host, "twitter.com")) |
---|
340 | /* May fire for people who turned on HTTPS. */ |
---|
341 | imcb_error(ic, "Warning: Twitter requires a version number in API calls " |
---|
342 | "now. Try resetting the base_url account setting."); |
---|
343 | } |
---|
344 | |
---|
345 | /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com |
---|
346 | into twitter, and try to be sensible if we get anything else. */ |
---|
347 | td->prefix = g_strdup(url.host); |
---|
348 | if (g_str_has_suffix(td->prefix, ".com")) |
---|
349 | td->prefix[strlen(url.host) - 4] = '\0'; |
---|
350 | if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) { |
---|
351 | /* If we have at least 3 chars after the last dot, cut off the rest. |
---|
352 | (mostly a www/api prefix or sth) */ |
---|
353 | s = g_strdup(s + 1); |
---|
354 | g_free(td->prefix); |
---|
355 | td->prefix = s; |
---|
356 | } |
---|
357 | |
---|
358 | if (strstr(acc->pass, "oauth_token=")) |
---|
359 | td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic)); |
---|
360 | |
---|
361 | sprintf(name, "%s_%s", td->prefix, acc->user); |
---|
362 | imcb_add_buddy(ic, name, NULL); |
---|
363 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
364 | |
---|
365 | if (set_getbool(&acc->set, "show_ids")) |
---|
366 | td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH); |
---|
367 | |
---|
368 | twitter_login_finish(ic); |
---|
369 | } |
---|
370 | |
---|
371 | /** |
---|
372 | * Logout method. Just free the twitter_data. |
---|
373 | */ |
---|
374 | static void twitter_logout(struct im_connection *ic) |
---|
375 | { |
---|
376 | struct twitter_data *td = ic->proto_data; |
---|
377 | |
---|
378 | // Set the status to logged out. |
---|
379 | ic->flags &= ~OPT_LOGGED_IN; |
---|
380 | |
---|
381 | // Remove the main_loop function from the function queue. |
---|
382 | b_event_remove(td->main_loop_id); |
---|
383 | |
---|
384 | if (td->timeline_gc) |
---|
385 | imcb_chat_free(td->timeline_gc); |
---|
386 | |
---|
387 | if (td) { |
---|
388 | http_close(td->stream); |
---|
389 | oauth_info_free(td->oauth_info); |
---|
390 | g_free(td->user); |
---|
391 | g_free(td->prefix); |
---|
392 | g_free(td->url_host); |
---|
393 | g_free(td->url_path); |
---|
394 | g_free(td->log); |
---|
395 | g_free(td); |
---|
396 | } |
---|
397 | |
---|
398 | twitter_connections = g_slist_remove(twitter_connections, ic); |
---|
399 | } |
---|
400 | |
---|
401 | static void twitter_handle_command(struct im_connection *ic, char *message); |
---|
402 | |
---|
403 | /** |
---|
404 | * |
---|
405 | */ |
---|
406 | static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
407 | { |
---|
408 | struct twitter_data *td = ic->proto_data; |
---|
409 | int plen = strlen(td->prefix); |
---|
410 | |
---|
411 | if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' && |
---|
412 | g_strcasecmp(who + plen + 1, ic->acc->user) == 0) { |
---|
413 | if (set_getbool(&ic->acc->set, "oauth") && |
---|
414 | td->oauth_info && td->oauth_info->token == NULL) { |
---|
415 | char pin[strlen(message) + 1], *s; |
---|
416 | |
---|
417 | strcpy(pin, message); |
---|
418 | for (s = pin + sizeof(pin) - 2; s > pin && isspace(*s); s--) |
---|
419 | *s = '\0'; |
---|
420 | for (s = pin; *s && isspace(*s); s++) { |
---|
421 | } |
---|
422 | |
---|
423 | if (!oauth_access_token(s, td->oauth_info)) { |
---|
424 | imcb_error(ic, "OAuth error: %s", |
---|
425 | "Failed to send access token request"); |
---|
426 | imc_logout(ic, TRUE); |
---|
427 | return FALSE; |
---|
428 | } |
---|
429 | } else |
---|
430 | twitter_handle_command(ic, message); |
---|
431 | } else { |
---|
432 | twitter_direct_messages_new(ic, who, message); |
---|
433 | } |
---|
434 | return (0); |
---|
435 | } |
---|
436 | |
---|
437 | /** |
---|
438 | * |
---|
439 | */ |
---|
440 | static void twitter_set_my_name(struct im_connection *ic, char *info) |
---|
441 | { |
---|
442 | } |
---|
443 | |
---|
444 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
445 | { |
---|
446 | } |
---|
447 | |
---|
448 | static void twitter_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
449 | { |
---|
450 | twitter_friendships_create_destroy(ic, who, 1); |
---|
451 | } |
---|
452 | |
---|
453 | static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
454 | { |
---|
455 | twitter_friendships_create_destroy(ic, who, 0); |
---|
456 | } |
---|
457 | |
---|
458 | static void twitter_chat_msg(struct groupchat *c, char *message, int flags) |
---|
459 | { |
---|
460 | if (c && message) |
---|
461 | twitter_handle_command(c->ic, message); |
---|
462 | } |
---|
463 | |
---|
464 | static void twitter_chat_invite(struct groupchat *c, char *who, char *message) |
---|
465 | { |
---|
466 | } |
---|
467 | |
---|
468 | static void twitter_chat_leave(struct groupchat *c) |
---|
469 | { |
---|
470 | struct twitter_data *td = c->ic->proto_data; |
---|
471 | |
---|
472 | if (c != td->timeline_gc) |
---|
473 | return; /* WTF? */ |
---|
474 | |
---|
475 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
476 | tweets come in. */ |
---|
477 | imcb_chat_free(td->timeline_gc); |
---|
478 | td->timeline_gc = NULL; |
---|
479 | } |
---|
480 | |
---|
481 | static void twitter_keepalive(struct im_connection *ic) |
---|
482 | { |
---|
483 | } |
---|
484 | |
---|
485 | static void twitter_add_permit(struct im_connection *ic, char *who) |
---|
486 | { |
---|
487 | } |
---|
488 | |
---|
489 | static void twitter_rem_permit(struct im_connection *ic, char *who) |
---|
490 | { |
---|
491 | } |
---|
492 | |
---|
493 | static void twitter_add_deny(struct im_connection *ic, char *who) |
---|
494 | { |
---|
495 | } |
---|
496 | |
---|
497 | static void twitter_rem_deny(struct im_connection *ic, char *who) |
---|
498 | { |
---|
499 | } |
---|
500 | |
---|
501 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
502 | //{ |
---|
503 | // return value; |
---|
504 | //} |
---|
505 | |
---|
506 | static void twitter_buddy_data_add(struct bee_user *bu) |
---|
507 | { |
---|
508 | bu->data = g_new0(struct twitter_user_data, 1); |
---|
509 | } |
---|
510 | |
---|
511 | static void twitter_buddy_data_free(struct bee_user *bu) |
---|
512 | { |
---|
513 | g_free(bu->data); |
---|
514 | } |
---|
515 | |
---|
516 | /** Convert the given bitlbee tweet ID, bitlbee username, or twitter tweet ID |
---|
517 | * into a twitter tweet ID. |
---|
518 | * |
---|
519 | * Returns 0 if the user provides garbage. |
---|
520 | */ |
---|
521 | static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, struct twitter_data *td, char *arg) { |
---|
522 | struct twitter_user_data *tud; |
---|
523 | bee_user_t *bu; |
---|
524 | guint64 id = 0; |
---|
525 | if (g_str_has_prefix(arg, "#") && |
---|
526 | sscanf(arg + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
527 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
528 | id = td->log[id].id; |
---|
529 | } else if ((bu = bee_user_by_handle(ic->bee, ic, arg)) && |
---|
530 | (tud = bu->data) && tud->last_id) |
---|
531 | id = tud->last_id; |
---|
532 | else if (sscanf(arg, "%" G_GUINT64_FORMAT, &id) == 1){ |
---|
533 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
534 | id = td->log[id].id; |
---|
535 | } |
---|
536 | return id; |
---|
537 | } |
---|
538 | |
---|
539 | static void twitter_handle_command(struct im_connection *ic, char *message) |
---|
540 | { |
---|
541 | struct twitter_data *td = ic->proto_data; |
---|
542 | char *cmds, **cmd, *new = NULL; |
---|
543 | guint64 in_reply_to = 0; |
---|
544 | |
---|
545 | cmds = g_strdup(message); |
---|
546 | cmd = split_command_parts(cmds); |
---|
547 | |
---|
548 | if (cmd[0] == NULL) { |
---|
549 | g_free(cmds); |
---|
550 | return; |
---|
551 | } else if (!set_getbool(&ic->acc->set, "commands")) { |
---|
552 | /* Not supporting commands. */ |
---|
553 | } else if (g_strcasecmp(cmd[0], "undo") == 0) { |
---|
554 | guint64 id; |
---|
555 | |
---|
556 | if (cmd[1] == NULL) |
---|
557 | twitter_status_destroy(ic, td->last_status_id); |
---|
558 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
559 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
560 | id = td->log[id].id; |
---|
561 | |
---|
562 | twitter_status_destroy(ic, id); |
---|
563 | } else |
---|
564 | twitter_msg(ic, "Could not undo last action"); |
---|
565 | |
---|
566 | g_free(cmds); |
---|
567 | return; |
---|
568 | } else if (g_strcasecmp(cmd[0], "favourite") == 0 && cmd[1]) { |
---|
569 | guint64 id; |
---|
570 | if ((id = twitter_message_id_from_command_arg(ic, td, cmd[1]))) { |
---|
571 | twitter_favourite_tweet(ic, id); |
---|
572 | } else { |
---|
573 | twitter_msg(ic, "Please provide a message ID or username."); |
---|
574 | } |
---|
575 | g_free(cmds); |
---|
576 | return; |
---|
577 | } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) { |
---|
578 | twitter_add_buddy(ic, cmd[1], NULL); |
---|
579 | g_free(cmds); |
---|
580 | return; |
---|
581 | } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) { |
---|
582 | twitter_remove_buddy(ic, cmd[1], NULL); |
---|
583 | g_free(cmds); |
---|
584 | return; |
---|
585 | } else if ((g_strcasecmp(cmd[0], "report") == 0 || |
---|
586 | g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) { |
---|
587 | char * screen_name; |
---|
588 | guint64 id; |
---|
589 | screen_name = cmd[1]; |
---|
590 | /* Report nominally works on users but look up the user who |
---|
591 | posted the given ID if the user wants to do it that way */ |
---|
592 | if (g_str_has_prefix(cmd[1], "#") && |
---|
593 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
594 | if (id < TWITTER_LOG_LENGTH && td->log) { |
---|
595 | if (g_slist_find(ic->bee->users, td->log[id].bu)) { |
---|
596 | screen_name = td->log[id].bu->handle; |
---|
597 | } |
---|
598 | } |
---|
599 | } |
---|
600 | twitter_report_spam(ic, screen_name); |
---|
601 | g_free(cmds); |
---|
602 | return; |
---|
603 | } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) { |
---|
604 | guint64 id = twitter_message_id_from_command_arg(ic, td, cmd[1]); |
---|
605 | |
---|
606 | td->last_status_id = 0; |
---|
607 | if (id) |
---|
608 | twitter_status_retweet(ic, id); |
---|
609 | else |
---|
610 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
611 | "post any statuses recently", cmd[1]); |
---|
612 | |
---|
613 | g_free(cmds); |
---|
614 | return; |
---|
615 | } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) { |
---|
616 | struct twitter_user_data *tud; |
---|
617 | bee_user_t *bu = NULL; |
---|
618 | guint64 id = 0; |
---|
619 | |
---|
620 | if (g_str_has_prefix(cmd[1], "#") && |
---|
621 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
622 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
623 | bu = td->log[id].bu; |
---|
624 | if (g_slist_find(ic->bee->users, bu)) |
---|
625 | id = td->log[id].id; |
---|
626 | else |
---|
627 | bu = NULL; |
---|
628 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
629 | (tud = bu->data) && tud->last_id) { |
---|
630 | id = tud->last_id; |
---|
631 | } else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
632 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
633 | bu = td->log[id].bu; |
---|
634 | if (g_slist_find(ic->bee->users, bu)) |
---|
635 | id = td->log[id].id; |
---|
636 | else |
---|
637 | bu = NULL; |
---|
638 | } |
---|
639 | |
---|
640 | if (!id || !bu) { |
---|
641 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
642 | "post any statuses recently", cmd[1]); |
---|
643 | g_free(cmds); |
---|
644 | return; |
---|
645 | } |
---|
646 | message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0])); |
---|
647 | in_reply_to = id; |
---|
648 | } else if (g_strcasecmp(cmd[0], "post") == 0) { |
---|
649 | message += 5; |
---|
650 | } |
---|
651 | |
---|
652 | { |
---|
653 | char *s; |
---|
654 | bee_user_t *bu; |
---|
655 | |
---|
656 | if (!twitter_length_check(ic, message)) { |
---|
657 | g_free(new); |
---|
658 | g_free(cmds); |
---|
659 | return; |
---|
660 | } |
---|
661 | |
---|
662 | s = cmd[0] + strlen(cmd[0]) - 1; |
---|
663 | if (!new && s > cmd[0] && (*s == ':' || *s == ',')) { |
---|
664 | *s = '\0'; |
---|
665 | |
---|
666 | if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) { |
---|
667 | struct twitter_user_data *tud = bu->data; |
---|
668 | |
---|
669 | new = g_strdup_printf("@%s %s", bu->handle, |
---|
670 | message + (s - cmd[0]) + 2); |
---|
671 | message = new; |
---|
672 | |
---|
673 | if (time(NULL) < tud->last_time + |
---|
674 | set_getint(&ic->acc->set, "auto_reply_timeout")) |
---|
675 | in_reply_to = tud->last_id; |
---|
676 | } |
---|
677 | } |
---|
678 | |
---|
679 | /* If the user runs undo between this request and its response |
---|
680 | this would delete the second-last Tweet. Prevent that. */ |
---|
681 | td->last_status_id = 0; |
---|
682 | twitter_post_status(ic, message, in_reply_to); |
---|
683 | g_free(new); |
---|
684 | } |
---|
685 | g_free(cmds); |
---|
686 | } |
---|
687 | |
---|
688 | void twitter_initmodule() |
---|
689 | { |
---|
690 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
691 | |
---|
692 | ret->options = OPT_NOOTR; |
---|
693 | ret->name = "twitter"; |
---|
694 | ret->login = twitter_login; |
---|
695 | ret->init = twitter_init; |
---|
696 | ret->logout = twitter_logout; |
---|
697 | ret->buddy_msg = twitter_buddy_msg; |
---|
698 | ret->get_info = twitter_get_info; |
---|
699 | ret->set_my_name = twitter_set_my_name; |
---|
700 | ret->add_buddy = twitter_add_buddy; |
---|
701 | ret->remove_buddy = twitter_remove_buddy; |
---|
702 | ret->chat_msg = twitter_chat_msg; |
---|
703 | ret->chat_invite = twitter_chat_invite; |
---|
704 | ret->chat_leave = twitter_chat_leave; |
---|
705 | ret->keepalive = twitter_keepalive; |
---|
706 | ret->add_permit = twitter_add_permit; |
---|
707 | ret->rem_permit = twitter_rem_permit; |
---|
708 | ret->add_deny = twitter_add_deny; |
---|
709 | ret->rem_deny = twitter_rem_deny; |
---|
710 | ret->buddy_data_add = twitter_buddy_data_add; |
---|
711 | ret->buddy_data_free = twitter_buddy_data_free; |
---|
712 | ret->handle_cmp = g_strcasecmp; |
---|
713 | |
---|
714 | register_protocol(ret); |
---|
715 | |
---|
716 | /* And an identi.ca variant: */ |
---|
717 | ret = g_memdup(ret, sizeof(struct prpl)); |
---|
718 | ret->name = "identica"; |
---|
719 | register_protocol(ret); |
---|
720 | } |
---|