source: protocols/twitter/twitter.c @ de923d5

Last change on this file since de923d5 was de923d5, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-06-11T17:11:08Z

Use /friends/ids and /users/lookup instead of /statuses/friends to get a
list of contacts at login time. Still depends on adding an API version number
to base_url though.

  • Property mode set to 100644
File size: 16.3 KB
Line 
1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Simple module to facilitate twitter functionality.                       *
5*                                                                           *
6*  Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com>                 *
7*                                                                           *
8*  This library is free software; you can redistribute it and/or            *
9*  modify it under the terms of the GNU Lesser General Public               *
10*  License as published by the Free Software Foundation, version            *
11*  2.1.                                                                     *
12*                                                                           *
13*  This library is distributed in the hope that it will be useful,          *
14*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
15*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
16*  Lesser General Public License for more details.                          *
17*                                                                           *
18*  You should have received a copy of the GNU Lesser General Public License *
19*  along with this library; if not, write to the Free Software Foundation,  *
20*  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA           *
21*                                                                           *
22****************************************************************************/
23
24#include "nogaim.h"
25#include "oauth.h"
26#include "twitter.h"
27#include "twitter_http.h"
28#include "twitter_lib.h"
29#include "url.h"
30
31#define twitter_msg( ic, fmt... ) \
32        do {                                                        \
33                struct twitter_data *td = ic->proto_data;           \
34                if( td->home_timeline_gc )                          \
35                        imcb_chat_log( td->home_timeline_gc, fmt ); \
36                else                                                \
37                        imcb_log( ic, fmt );                        \
38        } while( 0 );
39
40GSList *twitter_connections = NULL;
41
42/**
43 * Main loop function
44 */
45gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond)
46{
47        struct im_connection *ic = data;
48
49        // Check if we are still logged in...
50        if (!g_slist_find(twitter_connections, ic))
51                return 0;
52
53        // Do stuff..
54        twitter_get_home_timeline(ic, -1);
55
56        // If we are still logged in run this function again after timeout.
57        return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN;
58}
59
60static void twitter_main_loop_start(struct im_connection *ic)
61{
62        struct twitter_data *td = ic->proto_data;
63
64        imcb_log(ic, "Getting initial statuses");
65
66        // Run this once. After this queue the main loop function.
67        twitter_main_loop(ic, -1, 0);
68
69        // Queue the main_loop
70        // Save the return value, so we can remove the timeout on logout.
71        td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);
72}
73
74static void twitter_oauth_start(struct im_connection *ic);
75
76void twitter_login_finish(struct im_connection *ic)
77{
78        struct twitter_data *td = ic->proto_data;
79
80        if (set_getbool(&ic->acc->set, "oauth") && !td->oauth_info)
81                twitter_oauth_start(ic);
82        else if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") != 0 &&
83                 !(td->flags & TWITTER_HAVE_FRIENDS)) {
84                imcb_log(ic, "Getting contact list");
85                twitter_get_friends_ids(ic, -1);
86                //twitter_get_statuses_friends(ic, -1);
87        } else
88                twitter_main_loop_start(ic);
89}
90
91static const struct oauth_service twitter_oauth = {
92        "http://api.twitter.com/oauth/request_token",
93        "http://api.twitter.com/oauth/access_token",
94        "https://api.twitter.com/oauth/authorize",
95        .consumer_key = "xsDNKJuNZYkZyMcu914uEA",
96        .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo",
97};
98
99static const struct oauth_service identica_oauth = {
100        "http://identi.ca/api/oauth/request_token",
101        "http://identi.ca/api/oauth/access_token",
102        "https://identi.ca/api/oauth/authorize",
103        .consumer_key = "e147ff789fcbd8a5a07963afbb43f9da",
104        .consumer_secret = "c596267f277457ec0ce1ab7bb788d828",
105};
106
107static gboolean twitter_oauth_callback(struct oauth_info *info);
108
109static const struct oauth_service *get_oauth_service(struct im_connection *ic)
110{
111        struct twitter_data *td = ic->proto_data;
112
113        if (strstr(td->url_host, "identi.ca"))
114                return &identica_oauth;
115        else
116                return &twitter_oauth;
117
118        /* Could add more services, or allow configuring your own base URL +
119           API keys. */
120}
121
122static void twitter_oauth_start(struct im_connection *ic)
123{
124        struct twitter_data *td = ic->proto_data;
125
126        imcb_log(ic, "Requesting OAuth request token");
127
128        td->oauth_info = oauth_request_token(get_oauth_service(ic), twitter_oauth_callback, ic);
129
130        /* We need help from the user to complete OAuth login, so don't time
131           out on this login. */
132        ic->flags |= OPT_SLOW_LOGIN;
133}
134
135static gboolean twitter_oauth_callback(struct oauth_info *info)
136{
137        struct im_connection *ic = info->data;
138        struct twitter_data *td;
139
140        if (!g_slist_find(twitter_connections, ic))
141                return FALSE;
142
143        td = ic->proto_data;
144        if (info->stage == OAUTH_REQUEST_TOKEN) {
145                char name[strlen(ic->acc->user) + 9], *msg;
146
147                if (info->request_token == NULL) {
148                        imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http));
149                        imc_logout(ic, TRUE);
150                        return FALSE;
151                }
152
153                sprintf(name, "%s_%s", td->prefix, ic->acc->user);
154                msg = g_strdup_printf("To finish OAuth authentication, please visit "
155                                      "%s and respond with the resulting PIN code.",
156                                      info->auth_url);
157                imcb_buddy_msg(ic, name, msg, 0, 0);
158                g_free(msg);
159        } else if (info->stage == OAUTH_ACCESS_TOKEN) {
160                if (info->token == NULL || info->token_secret == NULL) {
161                        imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http));
162                        imc_logout(ic, TRUE);
163                        return FALSE;
164                } else {
165                        const char *sn = oauth_params_get(&info->params, "screen_name");
166
167                        if (sn != NULL && ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0) {
168                                imcb_log(ic, "Warning: You logged in via OAuth as %s "
169                                         "instead of %s.", sn, ic->acc->user);
170                        }
171                        g_free(td->user);
172                        td->user = g_strdup(sn);
173                }
174
175                /* IM mods didn't do this so far and it's ugly but I should
176                   be able to get away with it... */
177                g_free(ic->acc->pass);
178                ic->acc->pass = oauth_to_string(info);
179
180                twitter_login_finish(ic);
181        }
182
183        return TRUE;
184}
185
186
187static char *set_eval_mode(set_t * set, char *value)
188{
189        if (g_strcasecmp(value, "one") == 0 ||
190            g_strcasecmp(value, "many") == 0 || g_strcasecmp(value, "chat") == 0)
191                return value;
192        else
193                return NULL;
194}
195
196static gboolean twitter_length_check(struct im_connection *ic, gchar * msg)
197{
198        int max = set_getint(&ic->acc->set, "message_length"), len;
199
200        if (max == 0 || (len = g_utf8_strlen(msg, -1)) <= max)
201                return TRUE;
202
203        imcb_error(ic, "Maximum message length exceeded: %d > %d", len, max);
204
205        return FALSE;
206}
207
208static void twitter_init(account_t * acc)
209{
210        set_t *s;
211        char *def_url;
212        char *def_oauth;
213
214        if (strcmp(acc->prpl->name, "twitter") == 0) {
215                def_url = TWITTER_API_URL;
216                def_oauth = "true";
217        } else {                /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */
218
219                def_url = IDENTICA_API_URL;
220                def_oauth = "false";
221        }
222
223        s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc);
224
225        s = set_add(&acc->set, "base_url", def_url, NULL, acc);
226        s->flags |= ACC_SET_OFFLINE_ONLY;
227
228        s = set_add(&acc->set, "commands", "true", set_eval_bool, acc);
229
230        s = set_add(&acc->set, "message_length", "140", set_eval_int, acc);
231
232        s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc);
233        s->flags |= ACC_SET_OFFLINE_ONLY;
234
235        s = set_add(&acc->set, "show_ids", "false", set_eval_bool, acc);
236        s->flags |= ACC_SET_OFFLINE_ONLY;
237
238        s = set_add(&acc->set, "oauth", def_oauth, set_eval_bool, acc);
239}
240
241/**
242 * Login method. Since the twitter API works with seperate HTTP request we
243 * only save the user and pass to the twitter_data object.
244 */
245static void twitter_login(account_t * acc)
246{
247        struct im_connection *ic = imcb_new(acc);
248        struct twitter_data *td;
249        char name[strlen(acc->user) + 9];
250        url_t url;
251
252        if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) ||
253            (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) {
254                imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url"));
255                imc_logout(ic, FALSE);
256                return;
257        }
258
259        twitter_connections = g_slist_append(twitter_connections, ic);
260        td = g_new0(struct twitter_data, 1);
261        ic->proto_data = td;
262        td->user = g_strdup(acc->user);
263
264        td->url_ssl = url.proto == PROTO_HTTPS;
265        td->url_port = url.port;
266        td->url_host = g_strdup(url.host);
267        if (strcmp(url.file, "/") != 0)
268                td->url_path = g_strdup(url.file);
269        else
270                td->url_path = g_strdup("");
271        if (g_str_has_suffix(url.host, ".com"))
272                td->prefix = g_strndup(url.host, strlen(url.host) - 4);
273        else
274                td->prefix = g_strdup(url.host);
275
276        if (strstr(acc->pass, "oauth_token="))
277                td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic));
278
279        sprintf(name, "%s_%s", td->prefix, acc->user);
280        imcb_add_buddy(ic, name, NULL);
281        imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL);
282
283        if (set_getbool(&acc->set, "show_ids"))
284                td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH);
285
286        imcb_log(ic, "Connecting");
287
288        twitter_login_finish(ic);
289}
290
291/**
292 * Logout method. Just free the twitter_data.
293 */
294static void twitter_logout(struct im_connection *ic)
295{
296        struct twitter_data *td = ic->proto_data;
297
298        // Set the status to logged out.
299        ic->flags &= ~OPT_LOGGED_IN;
300
301        // Remove the main_loop function from the function queue.
302        b_event_remove(td->main_loop_id);
303
304        if (td->home_timeline_gc)
305                imcb_chat_free(td->home_timeline_gc);
306
307        if (td) {
308                oauth_info_free(td->oauth_info);
309                g_free(td->user);
310                g_free(td->prefix);
311                g_free(td->url_host);
312                g_free(td->url_path);
313                g_free(td->log);
314                g_free(td);
315        }
316
317        twitter_connections = g_slist_remove(twitter_connections, ic);
318}
319
320static void twitter_handle_command(struct im_connection *ic, char *message);
321
322/**
323 *
324 */
325static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away)
326{
327        struct twitter_data *td = ic->proto_data;
328        int plen = strlen(td->prefix);
329
330        if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' &&
331            g_strcasecmp(who + plen + 1, ic->acc->user) == 0) {
332                if (set_getbool(&ic->acc->set, "oauth") &&
333                    td->oauth_info && td->oauth_info->token == NULL) {
334                        char pin[strlen(message) + 1], *s;
335
336                        strcpy(pin, message);
337                        for (s = pin + sizeof(pin) - 2; s > pin && isspace(*s); s--)
338                                *s = '\0';
339                        for (s = pin; *s && isspace(*s); s++) {
340                        }
341
342                        if (!oauth_access_token(s, td->oauth_info)) {
343                                imcb_error(ic, "OAuth error: %s",
344                                           "Failed to send access token request");
345                                imc_logout(ic, TRUE);
346                                return FALSE;
347                        }
348                } else
349                        twitter_handle_command(ic, message);
350        } else {
351                twitter_direct_messages_new(ic, who, message);
352        }
353        return (0);
354}
355
356/**
357 *
358 */
359static void twitter_set_my_name(struct im_connection *ic, char *info)
360{
361}
362
363static void twitter_get_info(struct im_connection *ic, char *who)
364{
365}
366
367static void twitter_add_buddy(struct im_connection *ic, char *who, char *group)
368{
369        twitter_friendships_create_destroy(ic, who, 1);
370}
371
372static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group)
373{
374        twitter_friendships_create_destroy(ic, who, 0);
375}
376
377static void twitter_chat_msg(struct groupchat *c, char *message, int flags)
378{
379        if (c && message)
380                twitter_handle_command(c->ic, message);
381}
382
383static void twitter_chat_invite(struct groupchat *c, char *who, char *message)
384{
385}
386
387static void twitter_chat_leave(struct groupchat *c)
388{
389        struct twitter_data *td = c->ic->proto_data;
390
391        if (c != td->home_timeline_gc)
392                return;         /* WTF? */
393
394        /* If the user leaves the channel: Fine. Rejoin him/her once new
395           tweets come in. */
396        imcb_chat_free(td->home_timeline_gc);
397        td->home_timeline_gc = NULL;
398}
399
400static void twitter_keepalive(struct im_connection *ic)
401{
402}
403
404static void twitter_add_permit(struct im_connection *ic, char *who)
405{
406}
407
408static void twitter_rem_permit(struct im_connection *ic, char *who)
409{
410}
411
412static void twitter_add_deny(struct im_connection *ic, char *who)
413{
414}
415
416static void twitter_rem_deny(struct im_connection *ic, char *who)
417{
418}
419
420//static char *twitter_set_display_name( set_t *set, char *value )
421//{
422//      return value;
423//}
424
425static void twitter_buddy_data_add(struct bee_user *bu)
426{
427        bu->data = g_new0(struct twitter_user_data, 1);
428}
429
430static void twitter_buddy_data_free(struct bee_user *bu)
431{
432        g_free(bu->data);
433}
434
435static void twitter_handle_command(struct im_connection *ic, char *message)
436{
437        struct twitter_data *td = ic->proto_data;
438        char *cmds, **cmd, *new = NULL;
439        guint64 in_reply_to = 0;
440
441        cmds = g_strdup(message);
442        cmd = split_command_parts(cmds);
443
444        if (cmd[0] == NULL) {
445                g_free(cmds);
446                return;
447        } else if (!set_getbool(&ic->acc->set, "commands")) {
448                /* Not supporting commands. */
449        } else if (g_strcasecmp(cmd[0], "undo") == 0) {
450                guint64 id;
451
452                if (cmd[1])
453                        id = g_ascii_strtoull(cmd[1], NULL, 10);
454                else
455                        id = td->last_status_id;
456
457                /* TODO: User feedback. */
458                if (id)
459                        twitter_status_destroy(ic, id);
460                else
461                        twitter_msg(ic, "Could not undo last action");
462
463                g_free(cmds);
464                return;
465        } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) {
466                twitter_add_buddy(ic, cmd[1], NULL);
467                g_free(cmds);
468                return;
469        } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) {
470                twitter_remove_buddy(ic, cmd[1], NULL);
471                g_free(cmds);
472                return;
473        } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) {
474                struct twitter_user_data *tud;
475                bee_user_t *bu;
476                guint64 id;
477
478                if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) &&
479                    (tud = bu->data) && tud->last_id)
480                        id = tud->last_id;
481                else {
482                        id = g_ascii_strtoull(cmd[1], NULL, 10);
483                        if (id < TWITTER_LOG_LENGTH && td->log)
484                                id = td->log[id].id;
485                }
486
487                td->last_status_id = 0;
488                if (id)
489                        twitter_status_retweet(ic, id);
490                else
491                        twitter_msg(ic, "User `%s' does not exist or didn't "
492                                    "post any statuses recently", cmd[1]);
493
494                g_free(cmds);
495                return;
496        } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) {
497                struct twitter_user_data *tud;
498                bee_user_t *bu = NULL;
499                guint64 id = 0;
500
501                if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) &&
502                    (tud = bu->data) && tud->last_id) {
503                        id = tud->last_id;
504                } else if ((id = g_ascii_strtoull(cmd[1], NULL, 10)) &&
505                           (id < TWITTER_LOG_LENGTH) && td->log) {
506                        bu = td->log[id].bu;
507                        if (g_slist_find(ic->bee->users, bu))
508                                id = td->log[id].id;
509                        else
510                                bu = NULL;
511                }
512                if (!id || !bu) {
513                        twitter_msg(ic, "User `%s' does not exist or didn't "
514                                    "post any statuses recently", cmd[1]);
515                        return;
516                }
517                message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0]));
518                in_reply_to = id;
519        } else if (g_strcasecmp(cmd[0], "post") == 0) {
520                message += 5;
521        }
522
523        {
524                char *s;
525                bee_user_t *bu;
526
527                if (!twitter_length_check(ic, message)) {
528                        g_free(new);
529                        g_free(cmds);
530                        return;
531                }
532
533                s = cmd[0] + strlen(cmd[0]) - 1;
534                if (!new && s > cmd[0] && (*s == ':' || *s == ',')) {
535                        *s = '\0';
536
537                        if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) {
538                                struct twitter_user_data *tud = bu->data;
539
540                                new = g_strdup_printf("@%s %s", bu->handle,
541                                                      message + (s - cmd[0]) + 2);
542                                message = new;
543
544                                if (time(NULL) < tud->last_time +
545                                    set_getint(&ic->acc->set, "auto_reply_timeout"))
546                                        in_reply_to = tud->last_id;
547                        }
548                }
549
550                /* If the user runs undo between this request and its response
551                   this would delete the second-last Tweet. Prevent that. */
552                td->last_status_id = 0;
553                twitter_post_status(ic, message, in_reply_to);
554                g_free(new);
555        }
556        g_free(cmds);
557}
558
559void twitter_initmodule()
560{
561        struct prpl *ret = g_new0(struct prpl, 1);
562
563        ret->options = OPT_NOOTR;
564        ret->name = "twitter";
565        ret->login = twitter_login;
566        ret->init = twitter_init;
567        ret->logout = twitter_logout;
568        ret->buddy_msg = twitter_buddy_msg;
569        ret->get_info = twitter_get_info;
570        ret->set_my_name = twitter_set_my_name;
571        ret->add_buddy = twitter_add_buddy;
572        ret->remove_buddy = twitter_remove_buddy;
573        ret->chat_msg = twitter_chat_msg;
574        ret->chat_invite = twitter_chat_invite;
575        ret->chat_leave = twitter_chat_leave;
576        ret->keepalive = twitter_keepalive;
577        ret->add_permit = twitter_add_permit;
578        ret->rem_permit = twitter_rem_permit;
579        ret->add_deny = twitter_add_deny;
580        ret->rem_deny = twitter_rem_deny;
581        ret->buddy_data_add = twitter_buddy_data_add;
582        ret->buddy_data_free = twitter_buddy_data_free;
583        ret->handle_cmp = g_strcasecmp;
584
585        register_protocol(ret);
586
587        /* And an identi.ca variant: */
588        ret = g_memdup(ret, sizeof(struct prpl));
589        ret->name = "identica";
590        register_protocol(ret);
591}
Note: See TracBrowser for help on using the repository browser.