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