source: protocols/twitter/twitter.c @ 62f6b45

Last change on this file since 62f6b45 was dff0e0b, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-11T14:42:20Z

Showing tweets now, and leaking less memory. Still lots of cleanup left
to do.

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