source: protocols/twitter/twitter.c @ f26d9a3e

Last change on this file since f26d9a3e was f26d9a3e, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-24T14:10:34Z

Add help info for "stream" setting. Also, disable it for non-Twitter accounts.

  • 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);
[ce81acd]288        s->flags |= ACC_SET_OFFLINE_ONLY;
[5983eca]289
[b5fe39b]290        s = set_add(&acc->set, "show_old_mentions", "20", set_eval_int, acc);
[948abab]291
292        s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc);
[dff0e0b]293       
[f26d9a3e]294        if (strcmp(acc->prpl->name, "twitter") == 0) {
295                s = set_add(&acc->set, "stream", "true", set_eval_bool, acc);
296                s->flags |= ACC_SET_OFFLINE_ONLY;
297        }
[1b221e0]298}
299
300/**
[f26d9a3e]301 * Login method. Since the twitter API works with separate HTTP request we
[1b221e0]302 * only save the user and pass to the twitter_data object.
303 */
[5983eca]304static void twitter_login(account_t * acc)
[1b221e0]305{
[5983eca]306        struct im_connection *ic = imcb_new(acc);
[bb5ce4d1]307        struct twitter_data *td;
[5983eca]308        char name[strlen(acc->user) + 9];
[bb5ce4d1]309        url_t url;
[c0f33f1]310        char *s;
311       
[5983eca]312        if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) ||
313            (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) {
314                imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url"));
315                imc_logout(ic, FALSE);
[bb5ce4d1]316                return;
317        }
[5983eca]318
[f26d9a3e]319        if (!strstr(url.host, "twitter.com") &&
320            set_getbool(&ic->acc->set, "stream")) {
321                imcb_error(ic, "Warning: The streaming API is only supported by Twitter, "
322                               "and you seem to be connecting to a different service.");
323        }
324
[c0f33f1]325        imcb_log(ic, "Connecting");
326
[5983eca]327        twitter_connections = g_slist_append(twitter_connections, ic);
328        td = g_new0(struct twitter_data, 1);
[713d611]329        ic->proto_data = td;
[de923d5]330        td->user = g_strdup(acc->user);
[5983eca]331
[bb5ce4d1]332        td->url_ssl = url.proto == PROTO_HTTPS;
333        td->url_port = url.port;
[5983eca]334        td->url_host = g_strdup(url.host);
335        if (strcmp(url.file, "/") != 0)
336                td->url_path = g_strdup(url.file);
[c0f33f1]337        else {
[5983eca]338                td->url_path = g_strdup("");
[c0f33f1]339                if (g_str_has_suffix(url.host, "twitter.com"))
340                        /* May fire for people who turned on HTTPS. */
341                        imcb_error(ic, "Warning: Twitter requires a version number in API calls "
342                                       "now. Try resetting the base_url account setting.");
343        }
344       
345        /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com
346           into twitter, and try to be sensible if we get anything else. */
347        td->prefix = g_strdup(url.host);
348        if (g_str_has_suffix(td->prefix, ".com"))
349                td->prefix[strlen(url.host) - 4] = '\0';
[4bc66ae]350        if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) {
351                /* If we have at least 3 chars after the last dot, cut off the rest.
352                   (mostly a www/api prefix or sth) */
[c0f33f1]353                s = g_strdup(s + 1);
354                g_free(td->prefix);
355                td->prefix = s;
356        }
357       
[5983eca]358        if (strstr(acc->pass, "oauth_token="))
359                td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic));
360
361        sprintf(name, "%s_%s", td->prefix, acc->user);
362        imcb_add_buddy(ic, name, NULL);
363        imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL);
364
365        if (set_getbool(&acc->set, "show_ids"))
366                td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH);
367
368        twitter_login_finish(ic);
[1b221e0]369}
370
371/**
372 * Logout method. Just free the twitter_data.
373 */
[5983eca]374static void twitter_logout(struct im_connection *ic)
[1b221e0]375{
376        struct twitter_data *td = ic->proto_data;
[5983eca]377
[1b221e0]378        // Set the status to logged out.
[5983eca]379        ic->flags &= ~OPT_LOGGED_IN;
[1b221e0]380
[2abceca]381        // Remove the main_loop function from the function queue.
382        b_event_remove(td->main_loop_id);
383
[2322a9f]384        if (td->timeline_gc)
385                imcb_chat_free(td->timeline_gc);
[1014cab]386
[5983eca]387        if (td) {
[1388d30]388                http_close(td->stream);
[5983eca]389                oauth_info_free(td->oauth_info);
[de923d5]390                g_free(td->user);
[5983eca]391                g_free(td->prefix);
392                g_free(td->url_host);
393                g_free(td->url_path);
394                g_free(td->log);
395                g_free(td);
[1b221e0]396        }
[62d2cfb]397
[5983eca]398        twitter_connections = g_slist_remove(twitter_connections, ic);
[1b221e0]399}
400
[5983eca]401static void twitter_handle_command(struct im_connection *ic, char *message);
[7b87539]402
[1b221e0]403/**
404 *
405 */
[5983eca]406static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away)
[1b221e0]407{
[c42e8b9]408        struct twitter_data *td = ic->proto_data;
[5983eca]409        int plen = strlen(td->prefix);
410
[ffcdf13]411        if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' &&
[5983eca]412            g_strcasecmp(who + plen + 1, ic->acc->user) == 0) {
413                if (set_getbool(&ic->acc->set, "oauth") &&
414                    td->oauth_info && td->oauth_info->token == NULL) {
415                        char pin[strlen(message) + 1], *s;
416
417                        strcpy(pin, message);
418                        for (s = pin + sizeof(pin) - 2; s > pin && isspace(*s); s--)
[64f8c425]419                                *s = '\0';
[5983eca]420                        for (s = pin; *s && isspace(*s); s++) {
421                        }
422
423                        if (!oauth_access_token(s, td->oauth_info)) {
424                                imcb_error(ic, "OAuth error: %s",
425                                           "Failed to send access token request");
426                                imc_logout(ic, TRUE);
[c2ecadc]427                                return FALSE;
428                        }
[5983eca]429                } else
[7b87539]430                        twitter_handle_command(ic, message);
[5983eca]431        } else {
[e88fbe27]432                twitter_direct_messages_new(ic, who, message);
[c42e8b9]433        }
[5983eca]434        return (0);
[1b221e0]435}
436
437/**
438 *
439 */
[5983eca]440static void twitter_set_my_name(struct im_connection *ic, char *info)
[1b221e0]441{
442}
443
[5983eca]444static void twitter_get_info(struct im_connection *ic, char *who)
[1b221e0]445{
446}
447
[5983eca]448static void twitter_add_buddy(struct im_connection *ic, char *who, char *group)
[1b221e0]449{
[7d53efb]450        twitter_friendships_create_destroy(ic, who, 1);
[1b221e0]451}
452
[5983eca]453static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group)
[1b221e0]454{
[7d53efb]455        twitter_friendships_create_destroy(ic, who, 0);
[1b221e0]456}
457
[5983eca]458static void twitter_chat_msg(struct groupchat *c, char *message, int flags)
[1b221e0]459{
[5983eca]460        if (c && message)
461                twitter_handle_command(c->ic, message);
[1b221e0]462}
463
[5983eca]464static void twitter_chat_invite(struct groupchat *c, char *who, char *message)
[1b221e0]465{
466}
467
[5983eca]468static void twitter_chat_leave(struct groupchat *c)
[1b221e0]469{
[16592d8]470        struct twitter_data *td = c->ic->proto_data;
[5983eca]471
[2322a9f]472        if (c != td->timeline_gc)
[5983eca]473                return;         /* WTF? */
474
[16592d8]475        /* If the user leaves the channel: Fine. Rejoin him/her once new
476           tweets come in. */
[2322a9f]477        imcb_chat_free(td->timeline_gc);
478        td->timeline_gc = NULL;
[1b221e0]479}
480
[5983eca]481static void twitter_keepalive(struct im_connection *ic)
[1b221e0]482{
483}
484
[5983eca]485static void twitter_add_permit(struct im_connection *ic, char *who)
[1b221e0]486{
487}
488
[5983eca]489static void twitter_rem_permit(struct im_connection *ic, char *who)
[1b221e0]490{
491}
492
[5983eca]493static void twitter_add_deny(struct im_connection *ic, char *who)
[1b221e0]494{
495}
496
[5983eca]497static void twitter_rem_deny(struct im_connection *ic, char *who)
[1b221e0]498{
499}
500
501//static char *twitter_set_display_name( set_t *set, char *value )
502//{
[5983eca]503//      return value;
[1b221e0]504//}
[7b87539]505
[5983eca]506static void twitter_buddy_data_add(struct bee_user *bu)
[203a2d2]507{
[5983eca]508        bu->data = g_new0(struct twitter_user_data, 1);
[203a2d2]509}
510
[5983eca]511static void twitter_buddy_data_free(struct bee_user *bu)
[203a2d2]512{
[5983eca]513        g_free(bu->data);
[203a2d2]514}
515
[b61c74c]516/** Convert the given bitlbee tweet ID, bitlbee username, or twitter tweet ID
517 *  into a twitter tweet ID.
518 *
519 *  Returns 0 if the user provides garbage.
520 */
521static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, struct twitter_data *td, char *arg) {
522        struct twitter_user_data *tud;
523        bee_user_t *bu;
524        guint64 id = 0;
525        if (g_str_has_prefix(arg, "#") &&
526                sscanf(arg + 1, "%" G_GUINT64_FORMAT, &id) == 1) {
527                if (id < TWITTER_LOG_LENGTH && td->log)
528                        id = td->log[id].id;
529        } else if ((bu = bee_user_by_handle(ic->bee, ic, arg)) &&
530                (tud = bu->data) && tud->last_id)
531                id = tud->last_id;
532        else if (sscanf(arg, "%" G_GUINT64_FORMAT, &id) == 1){
533                if (id < TWITTER_LOG_LENGTH && td->log)
534                        id = td->log[id].id;
535        }
536        return id;
537}
538
[5983eca]539static void twitter_handle_command(struct im_connection *ic, char *message)
[7b87539]540{
541        struct twitter_data *td = ic->proto_data;
[15bc063]542        char *cmds, **cmd, *new = NULL;
543        guint64 in_reply_to = 0;
[5983eca]544
545        cmds = g_strdup(message);
546        cmd = split_command_parts(cmds);
547
548        if (cmd[0] == NULL) {
549                g_free(cmds);
[7b87539]550                return;
[5983eca]551        } else if (!set_getbool(&ic->acc->set, "commands")) {
[7b87539]552                /* Not supporting commands. */
[5983eca]553        } else if (g_strcasecmp(cmd[0], "undo") == 0) {
[7b87539]554                guint64 id;
[5983eca]555
[aa2f575]556                if (cmd[1] == NULL)
557                        twitter_status_destroy(ic, td->last_status_id);
558                else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1) {
559                        if (id < TWITTER_LOG_LENGTH && td->log)
560                                id = td->log[id].id;
561                       
[5983eca]562                        twitter_status_destroy(ic, id);
[aa2f575]563                } else
[5983eca]564                        twitter_msg(ic, "Could not undo last action");
565
[b61c74c]566                g_free(cmds);
567                return;
568        } else if (g_strcasecmp(cmd[0], "favourite") == 0 && cmd[1]) {
569                guint64 id;
570                if ((id = twitter_message_id_from_command_arg(ic, td, cmd[1]))) {
571                        twitter_favourite_tweet(ic, id);
572                } else {
573                        twitter_msg(ic, "Please provide a message ID or username.");
574                }
[5983eca]575                g_free(cmds);
[7b87539]576                return;
[5983eca]577        } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) {
578                twitter_add_buddy(ic, cmd[1], NULL);
579                g_free(cmds);
[b890626]580                return;
[5983eca]581        } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) {
582                twitter_remove_buddy(ic, cmd[1], NULL);
583                g_free(cmds);
[b890626]584                return;
[d18dee42]585        } else if ((g_strcasecmp(cmd[0], "report") == 0 ||
586                    g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) {
587                char * screen_name;
588                guint64 id;
[7de784c]589                screen_name = cmd[1];
[d18dee42]590                /* Report nominally works on users but look up the user who
591                   posted the given ID if the user wants to do it that way */
592                if (g_str_has_prefix(cmd[1], "#") &&
593                    sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) {
594                        if (id < TWITTER_LOG_LENGTH && td->log) {
595                                if (g_slist_find(ic->bee->users, td->log[id].bu)) {
596                                        screen_name = td->log[id].bu->handle;
597                                }
598                        }
599                }
600                twitter_report_spam(ic, screen_name);
601                g_free(cmds);
602                return;
[5983eca]603        } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) {
[b61c74c]604                guint64 id = twitter_message_id_from_command_arg(ic, td, cmd[1]);
[5983eca]605
[b890626]606                td->last_status_id = 0;
[5983eca]607                if (id)
608                        twitter_status_retweet(ic, id);
[665c24f]609                else
[5983eca]610                        twitter_msg(ic, "User `%s' does not exist or didn't "
611                                    "post any statuses recently", cmd[1]);
612
613                g_free(cmds);
[b890626]614                return;
[5983eca]615        } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) {
[15bc063]616                struct twitter_user_data *tud;
617                bee_user_t *bu = NULL;
618                guint64 id = 0;
[5983eca]619
[aa2f575]620                if (g_str_has_prefix(cmd[1], "#") &&
621                    sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1 &&
622                    (id < TWITTER_LOG_LENGTH) && td->log) {
623                        bu = td->log[id].bu;
624                        if (g_slist_find(ic->bee->users, bu))
625                                id = td->log[id].id;
626                        else
627                                bu = NULL;
628                } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) &&
[5983eca]629                    (tud = bu->data) && tud->last_id) {
[15bc063]630                        id = tud->last_id;
[7ceb6b2]631                } else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1 &&
632                           (id < TWITTER_LOG_LENGTH) && td->log) {
[15bc063]633                        bu = td->log[id].bu;
[5983eca]634                        if (g_slist_find(ic->bee->users, bu))
[15bc063]635                                id = td->log[id].id;
636                        else
637                                bu = NULL;
638                }
[aa2f575]639
[5983eca]640                if (!id || !bu) {
641                        twitter_msg(ic, "User `%s' does not exist or didn't "
642                                    "post any statuses recently", cmd[1]);
[d3bfe3d]643                        g_free(cmds);
[15bc063]644                        return;
645                }
[5983eca]646                message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0]));
[15bc063]647                in_reply_to = id;
[5983eca]648        } else if (g_strcasecmp(cmd[0], "post") == 0) {
[7b87539]649                message += 5;
650        }
[5983eca]651
[7b87539]652        {
[15bc063]653                char *s;
[7b87539]654                bee_user_t *bu;
[5983eca]655
656                if (!twitter_length_check(ic, message)) {
657                        g_free(new);
658                        g_free(cmds);
659                        return;
[7b87539]660                }
[5983eca]661
662                s = cmd[0] + strlen(cmd[0]) - 1;
663                if (!new && s > cmd[0] && (*s == ':' || *s == ',')) {
[7b87539]664                        *s = '\0';
[5983eca]665
666                        if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) {
[b890626]667                                struct twitter_user_data *tud = bu->data;
[5983eca]668
669                                new = g_strdup_printf("@%s %s", bu->handle,
670                                                      message + (s - cmd[0]) + 2);
[7b87539]671                                message = new;
[5983eca]672
673                                if (time(NULL) < tud->last_time +
674                                    set_getint(&ic->acc->set, "auto_reply_timeout"))
[b890626]675                                        in_reply_to = tud->last_id;
[7b87539]676                        }
677                }
[5983eca]678
[b890626]679                /* If the user runs undo between this request and its response
680                   this would delete the second-last Tweet. Prevent that. */
681                td->last_status_id = 0;
[5983eca]682                twitter_post_status(ic, message, in_reply_to);
683                g_free(new);
[7b87539]684        }
[5983eca]685        g_free(cmds);
[7b87539]686}
[1b221e0]687
688void twitter_initmodule()
689{
690        struct prpl *ret = g_new0(struct prpl, 1);
[5983eca]691
[1dd3470]692        ret->options = OPT_NOOTR;
[1b221e0]693        ret->name = "twitter";
694        ret->login = twitter_login;
695        ret->init = twitter_init;
696        ret->logout = twitter_logout;
697        ret->buddy_msg = twitter_buddy_msg;
698        ret->get_info = twitter_get_info;
699        ret->set_my_name = twitter_set_my_name;
700        ret->add_buddy = twitter_add_buddy;
701        ret->remove_buddy = twitter_remove_buddy;
702        ret->chat_msg = twitter_chat_msg;
703        ret->chat_invite = twitter_chat_invite;
704        ret->chat_leave = twitter_chat_leave;
705        ret->keepalive = twitter_keepalive;
706        ret->add_permit = twitter_add_permit;
707        ret->rem_permit = twitter_rem_permit;
708        ret->add_deny = twitter_add_deny;
709        ret->rem_deny = twitter_rem_deny;
[203a2d2]710        ret->buddy_data_add = twitter_buddy_data_add;
711        ret->buddy_data_free = twitter_buddy_data_free;
[1b221e0]712        ret->handle_cmp = g_strcasecmp;
[5983eca]713
[ffcdf13]714        register_protocol(ret);
[1b221e0]715
[ffcdf13]716        /* And an identi.ca variant: */
717        ret = g_memdup(ret, sizeof(struct prpl));
718        ret->name = "identica";
[1b221e0]719        register_protocol(ret);
720}
Note: See TracBrowser for help on using the repository browser.