source: protocols/twitter/twitter.c @ 2ca933c

Last change on this file since 2ca933c was 2ca933c, checked in by dequis <dx@…>, at 2015-05-31T02:38:55Z

twitter: add twitter_log_local_user back (oops)

Accidentally nuked it while resolving merge conflicts of a different
branch.

  • Property mode set to 100644
File size: 28.8 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;
[73ee390]33
34static int twitter_filter_cmp(struct twitter_filter *tf1,
35                              struct twitter_filter *tf2)
36{
37        int i1 = 0;
38        int i2 = 0;
39        int i;
40
41        static const twitter_filter_type_t types[] = {
42                /* Order of the types */
43                TWITTER_FILTER_TYPE_FOLLOW,
44                TWITTER_FILTER_TYPE_TRACK
45        };
46
47        for (i = 0; i < G_N_ELEMENTS(types); i++) {
48                if (types[i] == tf1->type) {
49                        i1 = i + 1;
50                        break;
51                }
52        }
53
54        for (i = 0; i < G_N_ELEMENTS(types); i++) {
55                if (types[i] == tf2->type) {
56                        i2 = i + 1;
57                        break;
58                }
59        }
60
61        if (i1 != i2) {
62                /* With different types, return their difference */
63                return i1 - i2;
64        }
65
66        /* With the same type, return the text comparison */
67        return g_strcasecmp(tf1->text, tf2->text);
68}
69
70static gboolean twitter_filter_update(gpointer data, gint fd,
71                                      b_input_condition cond)
72{
73        struct im_connection *ic = data;
74        struct twitter_data *td = ic->proto_data;
75
76        if (td->filters) {
77                twitter_open_filter_stream(ic);
78        } else if (td->filter_stream) {
79                http_close(td->filter_stream);
80                td->filter_stream = NULL;
81        }
82
83        td->filter_update_id = 0;
84        return FALSE;
85}
86
87static struct twitter_filter *twitter_filter_get(struct groupchat *c,
88                                                 twitter_filter_type_t type,
89                                                 const char *text)
90{
91        struct twitter_data *td = c->ic->proto_data;
92        struct twitter_filter *tf = NULL;
[5ebff60]93        struct twitter_filter tfc = { type, (char *) text };
[73ee390]94        GSList *l;
95
96        for (l = td->filters; l; l = g_slist_next(l)) {
97                tf = l->data;
98
[5ebff60]99                if (twitter_filter_cmp(tf, &tfc) == 0) {
[73ee390]100                        break;
[5ebff60]101                }
[73ee390]102
103                tf = NULL;
104        }
105
106        if (!tf) {
107                tf = g_new0(struct twitter_filter, 1);
108                tf->type = type;
109                tf->text = g_strdup(text);
110                td->filters = g_slist_prepend(td->filters, tf);
111        }
112
[5ebff60]113        if (!g_slist_find(tf->groupchats, c)) {
[73ee390]114                tf->groupchats = g_slist_prepend(tf->groupchats, c);
[5ebff60]115        }
[73ee390]116
[5ebff60]117        if (td->filter_update_id > 0) {
[73ee390]118                b_event_remove(td->filter_update_id);
[5ebff60]119        }
[73ee390]120
121        /* Wait for other possible filter changes to avoid request spam */
122        td->filter_update_id = b_timeout_add(TWITTER_FILTER_UPDATE_WAIT,
[5ebff60]123                                             twitter_filter_update, c->ic);
[73ee390]124        return tf;
125}
126
127static void twitter_filter_free(struct twitter_filter *tf)
128{
129        g_slist_free(tf->groupchats);
130        g_free(tf->text);
131        g_free(tf);
132}
133
134static void twitter_filter_remove(struct groupchat *c)
135{
136        struct twitter_data *td = c->ic->proto_data;
137        struct twitter_filter *tf;
138        GSList *l = td->filters;
139        GSList *p;
140
141        while (l != NULL) {
142                tf = l->data;
143                tf->groupchats = g_slist_remove(tf->groupchats, c);
144
145                p = l;
146                l = g_slist_next(l);
147
148                if (!tf->groupchats) {
149                        twitter_filter_free(tf);
150                        td->filters = g_slist_delete_link(td->filters, p);
151                }
152        }
153
[5ebff60]154        if (td->filter_update_id > 0) {
[73ee390]155                b_event_remove(td->filter_update_id);
[5ebff60]156        }
[73ee390]157
158        /* Wait for other possible filter changes to avoid request spam */
159        td->filter_update_id = b_timeout_add(TWITTER_FILTER_UPDATE_WAIT,
[5ebff60]160                                             twitter_filter_update, c->ic);
161}
[73ee390]162
163static void twitter_filter_remove_all(struct im_connection *ic)
164{
165        struct twitter_data *td = ic->proto_data;
166        GSList *chats = NULL;
167        struct twitter_filter *tf;
168        GSList *l = td->filters;
169        GSList *p;
170
171        while (l != NULL) {
172                tf = l->data;
173
174                /* Build up a list of groupchats to be freed */
175                for (p = tf->groupchats; p; p = g_slist_next(p)) {
[5ebff60]176                        if (!g_slist_find(chats, p->data)) {
[73ee390]177                                chats = g_slist_prepend(chats, p->data);
[5ebff60]178                        }
[73ee390]179                }
180
181                p = l;
182                l = g_slist_next(l);
183                twitter_filter_free(p->data);
184                td->filters = g_slist_delete_link(td->filters, p);
185        }
186
187        l = chats;
188
189        while (l != NULL) {
190                p = l;
191                l = g_slist_next(l);
192
193                /* Freed each remaining groupchat */
194                imcb_chat_free(p->data);
195                chats = g_slist_delete_link(chats, p);
196        }
197
198        if (td->filter_stream) {
199                http_close(td->filter_stream);
200                td->filter_stream = NULL;
201        }
202}
203
204static GSList *twitter_filter_parse(struct groupchat *c, const char *text)
205{
206        char **fs = g_strsplit(text, ";", 0);
207        GSList *ret = NULL;
208        struct twitter_filter *tf;
209        char **f;
210        char *v;
211        int i;
212        int t;
213
214        static const twitter_filter_type_t types[] = {
215                TWITTER_FILTER_TYPE_FOLLOW,
216                TWITTER_FILTER_TYPE_TRACK
217        };
218
219        static const char *typestrs[] = {
220                "follow",
221                "track"
222        };
223
224        for (f = fs; *f; f++) {
[5ebff60]225                if ((v = strchr(*f, ':')) == NULL) {
[73ee390]226                        continue;
[5ebff60]227                }
[73ee390]228
229                *(v++) = 0;
230
231                for (t = -1, i = 0; i < G_N_ELEMENTS(types); i++) {
232                        if (g_strcasecmp(typestrs[i], *f) == 0) {
233                                t = i;
234                                break;
235                        }
236                }
237
[5ebff60]238                if (t < 0 || strlen(v) == 0) {
[73ee390]239                        continue;
[5ebff60]240                }
[73ee390]241
242                tf = twitter_filter_get(c, types[t], v);
243                ret = g_slist_prepend(ret, tf);
244        }
245
246        g_strfreev(fs);
247        return ret;
248}
249
[1b221e0]250/**
[62d2cfb]251 * Main loop function
252 */
[1b221e0]253gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond)
254{
255        struct im_connection *ic = data;
[5983eca]256
[1b221e0]257        // Check if we are still logged in...
[5ebff60]258        if (!g_slist_find(twitter_connections, ic)) {
[2f9027c]259                return FALSE;
[5ebff60]260        }
[1b221e0]261
262        // Do stuff..
[2f9027c]263        return twitter_get_timeline(ic, -1) &&
264               ((ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN);
[1b221e0]265}
266
[5983eca]267static void twitter_main_loop_start(struct im_connection *ic)
[713d611]268{
269        struct twitter_data *td = ic->proto_data;
[5983eca]270
[eb4ad8d]271        char *last_tweet = set_getstr(&ic->acc->set, "_last_tweet");
[5ebff60]272
273        if (last_tweet) {
[7549d00]274                td->timeline_id = g_ascii_strtoull(last_tweet, NULL, 0);
[5ebff60]275        }
[7549d00]276
[631ec80]277        /* Create the room now that we "logged in". */
[5ebff60]278        if (td->flags & TWITTER_MODE_CHAT) {
[631ec80]279                twitter_groupchat_init(ic);
[5ebff60]280        }
[631ec80]281
[5983eca]282        imcb_log(ic, "Getting initial statuses");
[713d611]283
[f26d9a3e]284        // Run this once. After this queue the main loop function (or open the
285        // stream if available).
[713d611]286        twitter_main_loop(ic, -1, 0);
[5ebff60]287
[dff0e0b]288        if (set_getbool(&ic->acc->set, "stream")) {
289                /* That fetch was just to get backlog, the stream will give
290                   us the rest. \o/ */
291                twitter_open_stream(ic);
[5ebff60]292
[f26d9a3e]293                /* Stream sends keepalives (empty lines) or actual data at
294                   least twice a minute. Disconnect if this stops. */
[e132b60]295                ic->flags |= OPT_PONGS;
[dff0e0b]296        } else {
297                /* Not using the streaming API, so keep polling the old-
298                   fashioned way. :-( */
299                td->main_loop_id =
[5ebff60]300                        b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000,
301                                      twitter_main_loop, ic);
[dff0e0b]302        }
[713d611]303}
304
[631ec80]305struct groupchat *twitter_groupchat_init(struct im_connection *ic)
306{
307        char *name_hint;
308        struct groupchat *gc;
309        struct twitter_data *td = ic->proto_data;
310        GSList *l;
311
[5ebff60]312        if (td->timeline_gc) {
[631ec80]313                return td->timeline_gc;
[5ebff60]314        }
[631ec80]315
316        td->timeline_gc = gc = imcb_chat_new(ic, "twitter/timeline");
317
318        name_hint = g_strdup_printf("%s_%s", td->prefix, ic->acc->user);
319        imcb_chat_name_hint(gc, name_hint);
320        g_free(name_hint);
321
322        for (l = ic->bee->users; l; l = l->next) {
323                bee_user_t *bu = l->data;
[5ebff60]324                if (bu->ic == ic) {
[29f72b7]325                        imcb_chat_add_buddy(gc, bu->handle);
[5ebff60]326                }
[631ec80]327        }
328        imcb_chat_add_buddy(gc, ic->acc->user);
[5ebff60]329
[631ec80]330        return gc;
331}
332
[5983eca]333static void twitter_oauth_start(struct im_connection *ic);
[d6aa6dd]334
[5983eca]335void twitter_login_finish(struct im_connection *ic)
[d6aa6dd]336{
337        struct twitter_data *td = ic->proto_data;
[5983eca]338
[2322a9f]339        td->flags &= ~TWITTER_DOING_TIMELINE;
340
[5ebff60]341        if (set_getbool(&ic->acc->set, "oauth") && !td->oauth_info) {
[5983eca]342                twitter_oauth_start(ic);
[5ebff60]343        } else if (!(td->flags & TWITTER_MODE_ONE) &&
344                   !(td->flags & TWITTER_HAVE_FRIENDS)) {
[5983eca]345                imcb_log(ic, "Getting contact list");
[de923d5]346                twitter_get_friends_ids(ic, -1);
[5ebff60]347        } else {
[5983eca]348                twitter_main_loop_start(ic);
[5ebff60]349        }
[d6aa6dd]350}
[c2ecadc]351
[5983eca]352static const struct oauth_service twitter_oauth = {
[3f808ca]353        "https://api.twitter.com/oauth/request_token",
354        "https://api.twitter.com/oauth/access_token",
[c01bbd1]355        "https://api.twitter.com/oauth/authorize",
[c2ecadc]356        .consumer_key = "xsDNKJuNZYkZyMcu914uEA",
357        .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo",
358};
359
[5983eca]360static const struct oauth_service identica_oauth = {
[3f808ca]361        "https://identi.ca/api/oauth/request_token",
362        "https://identi.ca/api/oauth/access_token",
[ce617f0]363        "https://identi.ca/api/oauth/authorize",
364        .consumer_key = "e147ff789fcbd8a5a07963afbb43f9da",
365        .consumer_secret = "c596267f277457ec0ce1ab7bb788d828",
366};
367
[5983eca]368static gboolean twitter_oauth_callback(struct oauth_info *info);
[713d611]369
[5983eca]370static const struct oauth_service *get_oauth_service(struct im_connection *ic)
[ce617f0]371{
372        struct twitter_data *td = ic->proto_data;
[5983eca]373
[5ebff60]374        if (strstr(td->url_host, "identi.ca")) {
[ce617f0]375                return &identica_oauth;
[5ebff60]376        } else {
[ce617f0]377                return &twitter_oauth;
[5ebff60]378        }
[5983eca]379
[ce617f0]380        /* Could add more services, or allow configuring your own base URL +
381           API keys. */
382}
383
[5983eca]384static void twitter_oauth_start(struct im_connection *ic)
[713d611]385{
[18dbb20]386        struct twitter_data *td = ic->proto_data;
[f2d8aa2]387        const char *url = set_getstr(&ic->acc->set, "base_url");
[c42e8b9]388
[5983eca]389        imcb_log(ic, "Requesting OAuth request token");
[5ebff60]390
391        if (!strstr(url, "twitter.com") && !strstr(url, "identi.ca")) {
[f2d8aa2]392                imcb_log(ic, "Warning: OAuth only works with identi.ca and "
[5ebff60]393                         "Twitter.");
394        }
[5983eca]395
396        td->oauth_info = oauth_request_token(get_oauth_service(ic), twitter_oauth_callback, ic);
397
[748bcdd]398        /* We need help from the user to complete OAuth login, so don't time
399           out on this login. */
400        ic->flags |= OPT_SLOW_LOGIN;
[713d611]401}
402
[5983eca]403static gboolean twitter_oauth_callback(struct oauth_info *info)
[713d611]404{
405        struct im_connection *ic = info->data;
[18dbb20]406        struct twitter_data *td;
[5983eca]407
[5ebff60]408        if (!g_slist_find(twitter_connections, ic)) {
[18dbb20]409                return FALSE;
[5ebff60]410        }
[5983eca]411
[18dbb20]412        td = ic->proto_data;
[5983eca]413        if (info->stage == OAUTH_REQUEST_TOKEN) {
[f2d8aa2]414                char *name, *msg;
[5983eca]415
416                if (info->request_token == NULL) {
417                        imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http));
418                        imc_logout(ic, TRUE);
[18dbb20]419                        return FALSE;
[c42e8b9]420                }
[5983eca]421
[f2d8aa2]422                name = g_strdup_printf("%s_%s", td->prefix, ic->acc->user);
[5983eca]423                msg = g_strdup_printf("To finish OAuth authentication, please visit "
[5ebff60]424                                      "%s and respond with the resulting PIN code.",
425                                      info->auth_url);
[5983eca]426                imcb_buddy_msg(ic, name, msg, 0, 0);
[f2d8aa2]427                g_free(name);
[5983eca]428                g_free(msg);
429        } else if (info->stage == OAUTH_ACCESS_TOKEN) {
[cfbecc9]430                const char *sn;
[5ebff60]431
[5983eca]432                if (info->token == NULL || info->token_secret == NULL) {
433                        imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http));
434                        imc_logout(ic, TRUE);
[18dbb20]435                        return FALSE;
[cfbecc9]436                }
[5ebff60]437
[cfbecc9]438                if ((sn = oauth_params_get(&info->params, "screen_name"))) {
[5ebff60]439                        if (ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0) {
[5983eca]440                                imcb_log(ic, "Warning: You logged in via OAuth as %s "
[cfbecc9]441                                         "instead of %s.", sn, ic->acc->user);
[5ebff60]442                        }
[de923d5]443                        g_free(td->user);
444                        td->user = g_strdup(sn);
[93cc86f]445                }
[5983eca]446
[288b215]447                /* IM mods didn't do this so far and it's ugly but I should
448                   be able to get away with it... */
[5983eca]449                g_free(ic->acc->pass);
450                ic->acc->pass = oauth_to_string(info);
451
452                twitter_login_finish(ic);
[c42e8b9]453        }
[5983eca]454
[18dbb20]455        return TRUE;
[713d611]456}
457
[7d27962]458int twitter_url_len_diff(gchar *msg, unsigned int target_len)
459{
460        int url_len_diff = 0;
461
462        static GRegex *regex = NULL;
463        GMatchInfo *match_info;
464
[5ebff60]465        if (regex == NULL) {
[7d27962]466                regex = g_regex_new("(^|\\s)(http(s)?://[^\\s$]+)", 0, 0, NULL);
[5ebff60]467        }
468
[7d27962]469        g_regex_match(regex, msg, 0, &match_info);
470        while (g_match_info_matches(match_info)) {
471                gchar *url = g_match_info_fetch(match_info, 2);
472                url_len_diff += target_len - g_utf8_strlen(url, -1);
[a685409]473                /* Add another character for https://t.co/... URLs */
[5ebff60]474                if (g_match_info_fetch(match_info, 3) != NULL) {
[7d27962]475                        url_len_diff += 1;
[5ebff60]476                }
[7d27962]477                g_free(url);
478                g_match_info_next(match_info, NULL);
479        }
480        g_match_info_free(match_info);
481
482        return url_len_diff;
483}
484
[33528bd]485int twitter_message_len(gchar *msg, int target_len)
[9997691]486{
[7d27962]487        int url_len_diff = 0;
[a685409]488
[5ebff60]489        if (target_len > 0) {
[7d27962]490                url_len_diff = twitter_url_len_diff(msg, target_len);
[5ebff60]491        }
[5983eca]492
[33528bd]493        return g_utf8_strlen(msg, -1) + url_len_diff;
494}
495
496static gboolean twitter_length_check(struct im_connection *ic, gchar * msg)
497{
498        int max = set_getint(&ic->acc->set, "message_length");
499        int target_len = set_getint(&ic->acc->set, "target_url_length");
500        int len = twitter_message_len(msg, target_len);
501
502        if (max == 0 || len <= max) {
[9997691]503                return TRUE;
[5ebff60]504        }
[5983eca]505
[96dd574]506        twitter_log(ic, "Maximum message length exceeded: %d > %d", len, max);
[5983eca]507
[9997691]508        return FALSE;
509}
510
[3592b95]511static char *set_eval_commands(set_t * set, char *value)
512{
[5ebff60]513        if (g_strcasecmp(value, "strict") == 0) {
[3592b95]514                return value;
[5ebff60]515        } else {
[3592b95]516                return set_eval_bool(set, value);
[5ebff60]517        }
[3592b95]518}
519
520static char *set_eval_mode(set_t * set, char *value)
521{
522        if (g_strcasecmp(value, "one") == 0 ||
[5ebff60]523            g_strcasecmp(value, "many") == 0 || g_strcasecmp(value, "chat") == 0) {
[3592b95]524                return value;
[5ebff60]525        } else {
[3592b95]526                return NULL;
[5ebff60]527        }
[3592b95]528}
529
[5983eca]530static void twitter_init(account_t * acc)
[1b221e0]531{
[62d2cfb]532        set_t *s;
[ffcdf13]533        char *def_url;
[7d27962]534        char *def_tul;
[777461b]535        char *def_mentions;
[5983eca]536
537        if (strcmp(acc->prpl->name, "twitter") == 0) {
[ffcdf13]538                def_url = TWITTER_API_URL;
[a685409]539                def_tul = "22";
[777461b]540                def_mentions = "true";
[5ebff60]541        } else {                /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */
[ffcdf13]542                def_url = IDENTICA_API_URL;
[7d27962]543                def_tul = "0";
[777461b]544                def_mentions = "false";
[ffcdf13]545        }
[5983eca]546
547        s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc);
548
549        s = set_add(&acc->set, "base_url", def_url, NULL, acc);
[bb5ce4d1]550        s->flags |= ACC_SET_OFFLINE_ONLY;
[5983eca]551
[3592b95]552        s = set_add(&acc->set, "commands", "true", set_eval_commands, acc);
[5983eca]553
[2322a9f]554        s = set_add(&acc->set, "fetch_interval", "60", set_eval_int, acc);
555        s->flags |= ACC_SET_OFFLINE_ONLY;
556
[777461b]557        s = set_add(&acc->set, "fetch_mentions", def_mentions, set_eval_bool, acc);
[2322a9f]558
[5983eca]559        s = set_add(&acc->set, "message_length", "140", set_eval_int, acc);
560
[7d27962]561        s = set_add(&acc->set, "target_url_length", def_tul, set_eval_int, acc);
562
[5983eca]563        s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc);
[2abceca]564        s->flags |= ACC_SET_OFFLINE_ONLY;
[5983eca]565
[b3d99e3]566        s = set_add(&acc->set, "oauth", "true", set_eval_oauth, acc);
[ce199b7]567
[e93fa05]568        s = set_add(&acc->set, "show_ids", "true", set_eval_bool, acc);
[5983eca]569
[f75b3a7]570        s = set_add(&acc->set, "show_old_mentions", "0", set_eval_int, acc);
[948abab]571
572        s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc);
[5ebff60]573
[eb4ad8d]574        s = set_add(&acc->set, "_last_tweet", "0", NULL, acc);
575        s->flags |= SET_HIDDEN | SET_NOSAVE;
[7549d00]576
[f26d9a3e]577        if (strcmp(acc->prpl->name, "twitter") == 0) {
578                s = set_add(&acc->set, "stream", "true", set_eval_bool, acc);
579                s->flags |= ACC_SET_OFFLINE_ONLY;
580        }
[1b221e0]581}
582
583/**
[f26d9a3e]584 * Login method. Since the twitter API works with separate HTTP request we
[1b221e0]585 * only save the user and pass to the twitter_data object.
586 */
[5983eca]587static void twitter_login(account_t * acc)
[1b221e0]588{
[5983eca]589        struct im_connection *ic = imcb_new(acc);
[bb5ce4d1]590        struct twitter_data *td;
[5983eca]591        char name[strlen(acc->user) + 9];
[bb5ce4d1]592        url_t url;
[c0f33f1]593        char *s;
[5ebff60]594
[5983eca]595        if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) ||
596            (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) {
597                imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url"));
598                imc_logout(ic, FALSE);
[bb5ce4d1]599                return;
600        }
[5983eca]601
[f26d9a3e]602        if (!strstr(url.host, "twitter.com") &&
603            set_getbool(&ic->acc->set, "stream")) {
604                imcb_error(ic, "Warning: The streaming API is only supported by Twitter, "
[5ebff60]605                           "and you seem to be connecting to a different service.");
[f26d9a3e]606        }
607
[c0f33f1]608        imcb_log(ic, "Connecting");
609
[5983eca]610        twitter_connections = g_slist_append(twitter_connections, ic);
611        td = g_new0(struct twitter_data, 1);
[713d611]612        ic->proto_data = td;
[de923d5]613        td->user = g_strdup(acc->user);
[5983eca]614
[bb5ce4d1]615        td->url_ssl = url.proto == PROTO_HTTPS;
616        td->url_port = url.port;
[5983eca]617        td->url_host = g_strdup(url.host);
[5ebff60]618        if (strcmp(url.file, "/") != 0) {
[5983eca]619                td->url_path = g_strdup(url.file);
[5ebff60]620        } else {
[5983eca]621                td->url_path = g_strdup("");
[5ebff60]622                if (g_str_has_suffix(url.host, "twitter.com")) {
[c0f33f1]623                        /* May fire for people who turned on HTTPS. */
624                        imcb_error(ic, "Warning: Twitter requires a version number in API calls "
[5ebff60]625                                   "now. Try resetting the base_url account setting.");
626                }
[c0f33f1]627        }
[5ebff60]628
[c0f33f1]629        /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com
630           into twitter, and try to be sensible if we get anything else. */
631        td->prefix = g_strdup(url.host);
[5ebff60]632        if (g_str_has_suffix(td->prefix, ".com")) {
[c0f33f1]633                td->prefix[strlen(url.host) - 4] = '\0';
[5ebff60]634        }
[4bc66ae]635        if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) {
636                /* If we have at least 3 chars after the last dot, cut off the rest.
637                   (mostly a www/api prefix or sth) */
[c0f33f1]638                s = g_strdup(s + 1);
639                g_free(td->prefix);
640                td->prefix = s;
641        }
[5ebff60]642
643        if (strstr(acc->pass, "oauth_token=")) {
[5983eca]644                td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic));
[5ebff60]645        }
[5983eca]646
647        sprintf(name, "%s_%s", td->prefix, acc->user);
648        imcb_add_buddy(ic, name, NULL);
649        imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL);
650
[c9b5817]651        td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH);
652        td->log_id = -1;
[5ebff60]653
[631ec80]654        s = set_getstr(&ic->acc->set, "mode");
[5ebff60]655        if (g_strcasecmp(s, "one") == 0) {
[631ec80]656                td->flags |= TWITTER_MODE_ONE;
[5ebff60]657        } else if (g_strcasecmp(s, "many") == 0) {
[631ec80]658                td->flags |= TWITTER_MODE_MANY;
[5ebff60]659        } else {
[631ec80]660                td->flags |= TWITTER_MODE_CHAT;
[5ebff60]661        }
[5983eca]662
663        twitter_login_finish(ic);
[1b221e0]664}
665
666/**
667 * Logout method. Just free the twitter_data.
668 */
[5983eca]669static void twitter_logout(struct im_connection *ic)
[1b221e0]670{
671        struct twitter_data *td = ic->proto_data;
[5983eca]672
[1b221e0]673        // Set the status to logged out.
[5983eca]674        ic->flags &= ~OPT_LOGGED_IN;
[1b221e0]675
[2abceca]676        // Remove the main_loop function from the function queue.
677        b_event_remove(td->main_loop_id);
678
[5ebff60]679        if (td->timeline_gc) {
[2322a9f]680                imcb_chat_free(td->timeline_gc);
[5ebff60]681        }
[1014cab]682
[5983eca]683        if (td) {
[5ebff60]684                if (td->filter_update_id > 0) {
[73ee390]685                        b_event_remove(td->filter_update_id);
[5ebff60]686                }
[73ee390]687
[1388d30]688                http_close(td->stream);
[73ee390]689                twitter_filter_remove_all(ic);
[5983eca]690                oauth_info_free(td->oauth_info);
[de923d5]691                g_free(td->user);
[5983eca]692                g_free(td->prefix);
693                g_free(td->url_host);
694                g_free(td->url_path);
695                g_free(td->log);
696                g_free(td);
[1b221e0]697        }
[62d2cfb]698
[5983eca]699        twitter_connections = g_slist_remove(twitter_connections, ic);
[1b221e0]700}
701
[5983eca]702static void twitter_handle_command(struct im_connection *ic, char *message);
[7b87539]703
[1b221e0]704/**
705 *
706 */
[5983eca]707static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away)
[1b221e0]708{
[c42e8b9]709        struct twitter_data *td = ic->proto_data;
[5983eca]710        int plen = strlen(td->prefix);
711
[ffcdf13]712        if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' &&
[5983eca]713            g_strcasecmp(who + plen + 1, ic->acc->user) == 0) {
714                if (set_getbool(&ic->acc->set, "oauth") &&
715                    td->oauth_info && td->oauth_info->token == NULL) {
716                        char pin[strlen(message) + 1], *s;
717
718                        strcpy(pin, message);
[5ebff60]719                        for (s = pin + sizeof(pin) - 2; s > pin && g_ascii_isspace(*s); s--) {
[64f8c425]720                                *s = '\0';
[5ebff60]721                        }
[6b13103]722                        for (s = pin; *s && g_ascii_isspace(*s); s++) {
[5983eca]723                        }
724
725                        if (!oauth_access_token(s, td->oauth_info)) {
726                                imcb_error(ic, "OAuth error: %s",
[5ebff60]727                                           "Failed to send access token request");
[5983eca]728                                imc_logout(ic, TRUE);
[c2ecadc]729                                return FALSE;
730                        }
[5ebff60]731                } else {
[7b87539]732                        twitter_handle_command(ic, message);
[5ebff60]733                }
[5983eca]734        } else {
[e88fbe27]735                twitter_direct_messages_new(ic, who, message);
[c42e8b9]736        }
[5983eca]737        return (0);
[1b221e0]738}
739
[5983eca]740static void twitter_get_info(struct im_connection *ic, char *who)
[1b221e0]741{
742}
743
[5983eca]744static void twitter_add_buddy(struct im_connection *ic, char *who, char *group)
[1b221e0]745{
[7d53efb]746        twitter_friendships_create_destroy(ic, who, 1);
[1b221e0]747}
748
[5983eca]749static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group)
[1b221e0]750{
[7d53efb]751        twitter_friendships_create_destroy(ic, who, 0);
[1b221e0]752}
753
[5983eca]754static void twitter_chat_msg(struct groupchat *c, char *message, int flags)
[1b221e0]755{
[5ebff60]756        if (c && message) {
[5983eca]757                twitter_handle_command(c->ic, message);
[5ebff60]758        }
[1b221e0]759}
760
[5983eca]761static void twitter_chat_invite(struct groupchat *c, char *who, char *message)
[1b221e0]762{
763}
764
[73ee390]765static struct groupchat *twitter_chat_join(struct im_connection *ic,
766                                           const char *room, const char *nick,
767                                           const char *password, set_t **sets)
768{
769        struct groupchat *c = imcb_chat_new(ic, room);
770        GSList *fs = twitter_filter_parse(c, room);
771        GString *topic = g_string_new("");
772        struct twitter_filter *tf;
773        GSList *l;
774
775        fs = g_slist_sort(fs, (GCompareFunc) twitter_filter_cmp);
776
777        for (l = fs; l; l = g_slist_next(l)) {
778                tf = l->data;
779
[5ebff60]780                if (topic->len > 0) {
[73ee390]781                        g_string_append(topic, ", ");
[5ebff60]782                }
[73ee390]783
[5ebff60]784                if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) {
[73ee390]785                        g_string_append_c(topic, '@');
[5ebff60]786                }
[73ee390]787
788                g_string_append(topic, tf->text);
789        }
790
[5ebff60]791        if (topic->len > 0) {
[73ee390]792                g_string_prepend(topic, "Twitter Filter: ");
[5ebff60]793        }
[73ee390]794
795        imcb_chat_topic(c, NULL, topic->str, 0);
796        imcb_chat_add_buddy(c, ic->acc->user);
797
798        if (topic->len == 0) {
799                imcb_error(ic, "Failed to handle any filters");
800                imcb_chat_free(c);
801                c = NULL;
802        }
803
804        g_string_free(topic, TRUE);
805        g_slist_free(fs);
806
807        return c;
808}
809
[5983eca]810static void twitter_chat_leave(struct groupchat *c)
[1b221e0]811{
[16592d8]812        struct twitter_data *td = c->ic->proto_data;
[5983eca]813
[73ee390]814        if (c != td->timeline_gc) {
815                twitter_filter_remove(c);
816                imcb_chat_free(c);
817                return;
818        }
[5983eca]819
[16592d8]820        /* If the user leaves the channel: Fine. Rejoin him/her once new
821           tweets come in. */
[2322a9f]822        imcb_chat_free(td->timeline_gc);
823        td->timeline_gc = NULL;
[1b221e0]824}
825
[5983eca]826static void twitter_keepalive(struct im_connection *ic)
[1b221e0]827{
828}
829
[5983eca]830static void twitter_add_permit(struct im_connection *ic, char *who)
[1b221e0]831{
832}
833
[5983eca]834static void twitter_rem_permit(struct im_connection *ic, char *who)
[1b221e0]835{
836}
837
[5983eca]838static void twitter_add_deny(struct im_connection *ic, char *who)
[1b221e0]839{
840}
841
[5983eca]842static void twitter_rem_deny(struct im_connection *ic, char *who)
[1b221e0]843{
844}
845
846//static char *twitter_set_display_name( set_t *set, char *value )
847//{
[5983eca]848//      return value;
[1b221e0]849//}
[7b87539]850
[5983eca]851static void twitter_buddy_data_add(struct bee_user *bu)
[203a2d2]852{
[5983eca]853        bu->data = g_new0(struct twitter_user_data, 1);
[203a2d2]854}
855
[5983eca]856static void twitter_buddy_data_free(struct bee_user *bu)
[203a2d2]857{
[5983eca]858        g_free(bu->data);
[203a2d2]859}
860
[2ca933c]861bee_user_t twitter_log_local_user;
862
[b61c74c]863/** Convert the given bitlbee tweet ID, bitlbee username, or twitter tweet ID
864 *  into a twitter tweet ID.
865 *
866 *  Returns 0 if the user provides garbage.
867 */
[5ebff60]868static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, char *arg, bee_user_t **bu_)
869{
[3ca001b]870        struct twitter_data *td = ic->proto_data;
[b61c74c]871        struct twitter_user_data *tud;
[3ca001b]872        bee_user_t *bu = NULL;
[b61c74c]873        guint64 id = 0;
[5ebff60]874
875        if (bu_) {
[3ca001b]876                *bu_ = NULL;
[5ebff60]877        }
878        if (!arg || !arg[0]) {
[3ca001b]879                return 0;
[5ebff60]880        }
881
[3ca001b]882        if (arg[0] != '#' && (bu = bee_user_by_handle(ic->bee, ic, arg))) {
[5ebff60]883                if ((tud = bu->data)) {
[3ca001b]884                        id = tud->last_id;
[5ebff60]885                }
[3ca001b]886        } else {
[5ebff60]887                if (arg[0] == '#') {
[3ca001b]888                        arg++;
[5ebff60]889                }
[73f0a01]890                if (parse_int64(arg, 16, &id) && id < TWITTER_LOG_LENGTH) {
[3ca001b]891                        bu = td->log[id].bu;
[b61c74c]892                        id = td->log[id].id;
[73f0a01]893                } else if (parse_int64(arg, 10, &id)) {
[3ca001b]894                        /* Allow normal tweet IDs as well; not a very useful
895                           feature but it's always been there. Just ignore
896                           very low IDs to avoid accidents. */
[5ebff60]897                        if (id < 1000000) {
[3ca001b]898                                id = 0;
[5ebff60]899                        }
[3ca001b]900                }
[b61c74c]901        }
[5ebff60]902        if (bu_) {
[c43146d]903                if (bu == &twitter_log_local_user) {
904                        /* HACK alert. There's no bee_user object for the local
905                         * user so just fake one for the few cmds that need it. */
906                        twitter_log_local_user.handle = td->user;
907                } else {
908                        /* Beware of dangling pointers! */
909                        if (!g_slist_find(ic->bee->users, bu)) {
910                                bu = NULL;
911                        }
912                }
[3ca001b]913                *bu_ = bu;
[5ebff60]914        }
[b61c74c]915        return id;
916}
917
[5983eca]918static void twitter_handle_command(struct im_connection *ic, char *message)
[7b87539]919{
920        struct twitter_data *td = ic->proto_data;
[15bc063]921        char *cmds, **cmd, *new = NULL;
[3ca001b]922        guint64 in_reply_to = 0, id;
923        gboolean allow_post =
[5ebff60]924                g_strcasecmp(set_getstr(&ic->acc->set, "commands"), "strict") != 0;
[3ca001b]925        bee_user_t *bu = NULL;
[5983eca]926
927        cmds = g_strdup(message);
[269580c]928        cmd = split_command_parts(cmds, 2);
[5983eca]929
930        if (cmd[0] == NULL) {
[3ca001b]931                goto eof;
932        } else if (!set_getbool(&ic->acc->set, "commands") && allow_post) {
933                /* Not supporting commands if "commands" is set to true/strict. */
[5983eca]934        } else if (g_strcasecmp(cmd[0], "undo") == 0) {
[5ebff60]935                if (cmd[1] == NULL) {
[aa2f575]936                        twitter_status_destroy(ic, td->last_status_id);
[5ebff60]937                } else if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL))) {
[5983eca]938                        twitter_status_destroy(ic, id);
[5ebff60]939                } else {
[96dd574]940                        twitter_log(ic, "Could not undo last action");
[5ebff60]941                }
[5983eca]942
[3ca001b]943                goto eof;
[c203533]944        } else if ((g_strcasecmp(cmd[0], "favourite") == 0 ||
[5ebff60]945                    g_strcasecmp(cmd[0], "favorite") == 0 ||
946                    g_strcasecmp(cmd[0], "fav") == 0) && cmd[1]) {
[3ca001b]947                if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL))) {
[b61c74c]948                        twitter_favourite_tweet(ic, id);
949                } else {
[96dd574]950                        twitter_log(ic, "Please provide a message ID or username.");
[b61c74c]951                }
[3ca001b]952                goto eof;
[5983eca]953        } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) {
954                twitter_add_buddy(ic, cmd[1], NULL);
[3ca001b]955                goto eof;
[5983eca]956        } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) {
957                twitter_remove_buddy(ic, cmd[1], NULL);
[3ca001b]958                goto eof;
[d18dee42]959        } else if ((g_strcasecmp(cmd[0], "report") == 0 ||
960                    g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) {
[3ca001b]961                char *screen_name;
[5ebff60]962
[d18dee42]963                /* Report nominally works on users but look up the user who
964                   posted the given ID if the user wants to do it that way */
[3ca001b]965                twitter_message_id_from_command_arg(ic, cmd[1], &bu);
[5ebff60]966                if (bu) {
[3ca001b]967                        screen_name = bu->handle;
[5ebff60]968                } else {
[3ca001b]969                        screen_name = cmd[1];
[5ebff60]970                }
971
[d18dee42]972                twitter_report_spam(ic, screen_name);
[3ca001b]973                goto eof;
[5983eca]974        } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) {
[3ca001b]975                id = twitter_message_id_from_command_arg(ic, cmd[1], NULL);
[5983eca]976
[b890626]977                td->last_status_id = 0;
[5ebff60]978                if (id) {
[5983eca]979                        twitter_status_retweet(ic, id);
[5ebff60]980                } else {
[96dd574]981                        twitter_log(ic, "User `%s' does not exist or didn't "
[5ebff60]982                                    "post any statuses recently", cmd[1]);
983                }
[5983eca]984
[3ca001b]985                goto eof;
[5983eca]986        } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) {
[3ca001b]987                id = twitter_message_id_from_command_arg(ic, cmd[1], &bu);
[5983eca]988                if (!id || !bu) {
[96dd574]989                        twitter_log(ic, "User `%s' does not exist or didn't "
[5ebff60]990                                    "post any statuses recently", cmd[1]);
[3ca001b]991                        goto eof;
[15bc063]992                }
[269580c]993                message = new = g_strdup_printf("@%s %s", bu->handle, cmd[2]);
[15bc063]994                in_reply_to = id;
[3ca001b]995                allow_post = TRUE;
[dfaa46d]996        } else if (g_strcasecmp(cmd[0], "rawreply") == 0 && cmd[1] && cmd[2]) {
997                id = twitter_message_id_from_command_arg(ic, cmd[1], NULL);
998                if (!id) {
999                        twitter_log(ic, "Tweet `%s' does not exist", cmd[1]);
1000                        goto eof;
1001                }
1002                message = cmd[2];
1003                in_reply_to = id;
1004                allow_post = TRUE;
[9ed8175]1005        } else if (g_strcasecmp(cmd[0], "url") == 0) {
1006                id = twitter_message_id_from_command_arg(ic, cmd[1], &bu);
1007                if (!id) {
1008                        twitter_log(ic, "Tweet `%s' does not exist", cmd[1]);
1009                } else {
1010                        /* More common link is twitter.com/$UID/status/$ID (and that's
1011                         * what this will 302 to) but can't generate that since for RTs,
1012                         * bu here points at the retweeter while id contains the id of
1013                         * the original message. */
1014                        twitter_log(ic, "https://twitter.com/statuses/%lld", id);
1015                }
1016                goto eof;
1017
[5983eca]1018        } else if (g_strcasecmp(cmd[0], "post") == 0) {
[7b87539]1019                message += 5;
[3ca001b]1020                allow_post = TRUE;
[7b87539]1021        }
[5983eca]1022
[3ca001b]1023        if (allow_post) {
[15bc063]1024                char *s;
[5983eca]1025
[5ebff60]1026                if (!twitter_length_check(ic, message)) {
[3ca001b]1027                        goto eof;
[5ebff60]1028                }
[5983eca]1029
1030                s = cmd[0] + strlen(cmd[0]) - 1;
1031                if (!new && s > cmd[0] && (*s == ':' || *s == ',')) {
[7b87539]1032                        *s = '\0';
[5983eca]1033
1034                        if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) {
[b890626]1035                                struct twitter_user_data *tud = bu->data;
[5983eca]1036
1037                                new = g_strdup_printf("@%s %s", bu->handle,
[5ebff60]1038                                                      message + (s - cmd[0]) + 2);
[7b87539]1039                                message = new;
[5983eca]1040
1041                                if (time(NULL) < tud->last_time +
[5ebff60]1042                                    set_getint(&ic->acc->set, "auto_reply_timeout")) {
[b890626]1043                                        in_reply_to = tud->last_id;
[5ebff60]1044                                }
[7b87539]1045                        }
1046                }
[5983eca]1047
[b890626]1048                /* If the user runs undo between this request and its response
1049                   this would delete the second-last Tweet. Prevent that. */
1050                td->last_status_id = 0;
[5983eca]1051                twitter_post_status(ic, message, in_reply_to);
[3ca001b]1052        } else {
1053                twitter_log(ic, "Unknown command: %s", cmd[0]);
[7b87539]1054        }
[3ca001b]1055eof:
1056        g_free(new);
[5983eca]1057        g_free(cmds);
[7b87539]1058}
[1b221e0]1059
[5ebff60]1060void twitter_log(struct im_connection *ic, char *format, ...)
[96dd574]1061{
1062        struct twitter_data *td = ic->proto_data;
1063        va_list params;
1064        char *text;
[5ebff60]1065
[96dd574]1066        va_start(params, format);
1067        text = g_strdup_vprintf(format, params);
1068        va_end(params);
[5ebff60]1069
1070        if (td->timeline_gc) {
[96dd574]1071                imcb_chat_log(td->timeline_gc, "%s", text);
[5ebff60]1072        } else {
[96dd574]1073                imcb_log(ic, "%s", text);
[5ebff60]1074        }
1075
[96dd574]1076        g_free(text);
1077}
1078
1079
[1b221e0]1080void twitter_initmodule()
1081{
1082        struct prpl *ret = g_new0(struct prpl, 1);
[5983eca]1083
[1dd3470]1084        ret->options = OPT_NOOTR;
[1b221e0]1085        ret->name = "twitter";
1086        ret->login = twitter_login;
1087        ret->init = twitter_init;
1088        ret->logout = twitter_logout;
1089        ret->buddy_msg = twitter_buddy_msg;
1090        ret->get_info = twitter_get_info;
1091        ret->add_buddy = twitter_add_buddy;
1092        ret->remove_buddy = twitter_remove_buddy;
1093        ret->chat_msg = twitter_chat_msg;
1094        ret->chat_invite = twitter_chat_invite;
[73ee390]1095        ret->chat_join = twitter_chat_join;
[1b221e0]1096        ret->chat_leave = twitter_chat_leave;
1097        ret->keepalive = twitter_keepalive;
1098        ret->add_permit = twitter_add_permit;
1099        ret->rem_permit = twitter_rem_permit;
1100        ret->add_deny = twitter_add_deny;
1101        ret->rem_deny = twitter_rem_deny;
[203a2d2]1102        ret->buddy_data_add = twitter_buddy_data_add;
1103        ret->buddy_data_free = twitter_buddy_data_free;
[1b221e0]1104        ret->handle_cmp = g_strcasecmp;
[5983eca]1105
[ffcdf13]1106        register_protocol(ret);
[1b221e0]1107
[ffcdf13]1108        /* And an identi.ca variant: */
1109        ret = g_memdup(ret, sizeof(struct prpl));
1110        ret->name = "identica";
[1b221e0]1111        register_protocol(ret);
1112}
Note: See TracBrowser for help on using the repository browser.