source: protocols/twitter/twitter.c @ 777461b

Last change on this file since 777461b was 777461b, checked in by Wilmer van der Gaast <wilmer@…>, at 2013-06-15T23:33:53Z

Turn off fetch_mentions by default for identi.ca connections since it's no
longer working. (Not compatible with Twitter API 1.1 from what I can see?)

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