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