source: protocols/twitter/twitter.c @ a685409

Last change on this file since a685409 was a685409, checked in by Wilmer van der Gaast <wilmer@…>, at 2013-07-13T18:49:13Z

Update to deal with a not-so-recent regression in the Twitter URL lenghtening
service. https://dev.twitter.com/blog/upcoming-tco-changes #1077

Seriously guys, stop being so obnoxious. This t.co garbage lengthens pretty
much everything (that looks like a) URL at this point. Stop counting it towards
the 140-char limit.

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