source: protocols/twitter/twitter.c @ 948abab

Last change on this file since 948abab was 948abab, checked in by Daniel Albers <daniel@…>, at 2012-01-13T17:49:23Z

add 'strip_newlines' setting to Twitter

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