source: protocols/twitter/twitter.c @ 96dd574

Last change on this file since 96dd574 was 96dd574, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-25T13:11:19Z

s/twitter_msg/twitter_log/ and use it in a few more places.

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