source: protocols/twitter/twitter.c @ 7549d00

Last change on this file since 7549d00 was 7549d00, checked in by dequis <dx@…>, at 2015-01-16T19:50:24Z

twitter: start stream from last tweet on connect/reconnect

This works by setting the last_tweet hidden account setting to the ID of
the last shown tweet.

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