source: protocols/twitter/twitter.c @ e9bdfbc

Last change on this file since e9bdfbc was c9b5817, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-25T00:55:47Z

Minor rework: Always fill td->log now and use it not just for show_ids, but
also for stream deduplication. Also, drop tweets from unknown people unless
fetch_mentions is set. The stream will feed us that spam either way but not
everyone wants to see it.

Last, fixing a bug where in streaming mode, per-user last tweet times were
no longer getting tracked.

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