source: protocols/twitter/twitter.c @ 7e84168

Last change on this file since 7e84168 was 2f9027c, checked in by Wilmer van der Gaast <wilmer@…>, at 2013-06-16T17:33:58Z

Fix cleanup failure on Twitter timeouts.

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