Changeset 5ebff60 for protocols/twitter


Ignore:
Timestamp:
2015-02-20T22:50:54Z (9 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
0b9daac, 3d45471, 7733b8c
Parents:
af359b4
git-author:
Indent <please@…> (19-02-15 05:47:20)
git-committer:
dequis <dx@…> (20-02-15 22:50:54)
Message:

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

Location:
protocols/twitter
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter.c

    raf359b4 r5ebff60  
    9191        struct twitter_data *td = c->ic->proto_data;
    9292        struct twitter_filter *tf = NULL;
    93         struct twitter_filter tfc = {type, (char*) text};
     93        struct twitter_filter tfc = { type, (char *) text };
    9494        GSList *l;
    9595
     
    9797                tf = l->data;
    9898
    99                 if (twitter_filter_cmp(tf, &tfc) == 0)
     99                if (twitter_filter_cmp(tf, &tfc) == 0) {
    100100                        break;
     101                }
    101102
    102103                tf = NULL;
     
    110111        }
    111112
    112         if (!g_slist_find(tf->groupchats, c))
     113        if (!g_slist_find(tf->groupchats, c)) {
    113114                tf->groupchats = g_slist_prepend(tf->groupchats, c);
    114 
    115         if (td->filter_update_id > 0)
     115        }
     116
     117        if (td->filter_update_id > 0) {
    116118                b_event_remove(td->filter_update_id);
     119        }
    117120
    118121        /* Wait for other possible filter changes to avoid request spam */
    119122        td->filter_update_id = b_timeout_add(TWITTER_FILTER_UPDATE_WAIT,
    120                                              twitter_filter_update, c->ic);
     123                                             twitter_filter_update, c->ic);
    121124        return tf;
    122125}
     
    149152        }
    150153
    151         if (td->filter_update_id > 0)
     154        if (td->filter_update_id > 0) {
    152155                b_event_remove(td->filter_update_id);
     156        }
    153157
    154158        /* Wait for other possible filter changes to avoid request spam */
    155159        td->filter_update_id = b_timeout_add(TWITTER_FILTER_UPDATE_WAIT,
    156                                              twitter_filter_update, c->ic);}
     160                                             twitter_filter_update, c->ic);
     161}
    157162
    158163static void twitter_filter_remove_all(struct im_connection *ic)
     
    169174                /* Build up a list of groupchats to be freed */
    170175                for (p = tf->groupchats; p; p = g_slist_next(p)) {
    171                         if (!g_slist_find(chats, p->data))
     176                        if (!g_slist_find(chats, p->data)) {
    172177                                chats = g_slist_prepend(chats, p->data);
     178                        }
    173179                }
    174180
     
    217223
    218224        for (f = fs; *f; f++) {
    219                 if ((v = strchr(*f, ':')) == NULL)
     225                if ((v = strchr(*f, ':')) == NULL) {
    220226                        continue;
     227                }
    221228
    222229                *(v++) = 0;
     
    229236                }
    230237
    231                 if (t < 0 || strlen(v) == 0)
     238                if (t < 0 || strlen(v) == 0) {
    232239                        continue;
     240                }
    233241
    234242                tf = twitter_filter_get(c, types[t], v);
     
    248256
    249257        // Check if we are still logged in...
    250         if (!g_slist_find(twitter_connections, ic))
     258        if (!g_slist_find(twitter_connections, ic)) {
    251259                return FALSE;
     260        }
    252261
    253262        // Do stuff..
     
    261270
    262271        char *last_tweet = set_getstr(&ic->acc->set, "_last_tweet");
    263         if (last_tweet)
     272
     273        if (last_tweet) {
    264274                td->timeline_id = g_ascii_strtoull(last_tweet, NULL, 0);
     275        }
    265276
    266277        /* Create the room now that we "logged in". */
    267         if (td->flags & TWITTER_MODE_CHAT)
     278        if (td->flags & TWITTER_MODE_CHAT) {
    268279                twitter_groupchat_init(ic);
     280        }
    269281
    270282        imcb_log(ic, "Getting initial statuses");
     
    273285        // stream if available).
    274286        twitter_main_loop(ic, -1, 0);
    275        
     287
    276288        if (set_getbool(&ic->acc->set, "stream")) {
    277289                /* That fetch was just to get backlog, the stream will give
    278290                   us the rest. \o/ */
    279291                twitter_open_stream(ic);
    280                
     292
    281293                /* Stream sends keepalives (empty lines) or actual data at
    282294                   least twice a minute. Disconnect if this stops. */
     
    286298                   fashioned way. :-( */
    287299                td->main_loop_id =
    288                     b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000,
    289                                   twitter_main_loop, ic);
     300                        b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000,
     301                                      twitter_main_loop, ic);
    290302        }
    291303}
     
    298310        GSList *l;
    299311
    300         if (td->timeline_gc)
     312        if (td->timeline_gc) {
    301313                return td->timeline_gc;
     314        }
    302315
    303316        td->timeline_gc = gc = imcb_chat_new(ic, "twitter/timeline");
     
    309322        for (l = ic->bee->users; l; l = l->next) {
    310323                bee_user_t *bu = l->data;
    311                 if (bu->ic == ic)
     324                if (bu->ic == ic) {
    312325                        imcb_chat_add_buddy(gc, bu->handle);
     326                }
    313327        }
    314328        imcb_chat_add_buddy(gc, ic->acc->user);
    315        
     329
    316330        return gc;
    317331}
     
    325339        td->flags &= ~TWITTER_DOING_TIMELINE;
    326340
    327         if (set_getbool(&ic->acc->set, "oauth") && !td->oauth_info)
     341        if (set_getbool(&ic->acc->set, "oauth") && !td->oauth_info) {
    328342                twitter_oauth_start(ic);
    329         else if (!(td->flags & TWITTER_MODE_ONE) &&
    330                  !(td->flags & TWITTER_HAVE_FRIENDS)) {
     343        } else if (!(td->flags & TWITTER_MODE_ONE) &&
     344                   !(td->flags & TWITTER_HAVE_FRIENDS)) {
    331345                imcb_log(ic, "Getting contact list");
    332346                twitter_get_friends_ids(ic, -1);
    333         } else
     347        } else {
    334348                twitter_main_loop_start(ic);
     349        }
    335350}
    336351
     
    357372        struct twitter_data *td = ic->proto_data;
    358373
    359         if (strstr(td->url_host, "identi.ca"))
     374        if (strstr(td->url_host, "identi.ca")) {
    360375                return &identica_oauth;
    361         else
     376        } else {
    362377                return &twitter_oauth;
     378        }
    363379
    364380        /* Could add more services, or allow configuring your own base URL +
     
    372388
    373389        imcb_log(ic, "Requesting OAuth request token");
    374        
    375         if (!strstr(url, "twitter.com") && !strstr(url, "identi.ca"))
     390
     391        if (!strstr(url, "twitter.com") && !strstr(url, "identi.ca")) {
    376392                imcb_log(ic, "Warning: OAuth only works with identi.ca and "
    377                              "Twitter.");
     393                         "Twitter.");
     394        }
    378395
    379396        td->oauth_info = oauth_request_token(get_oauth_service(ic), twitter_oauth_callback, ic);
     
    389406        struct twitter_data *td;
    390407
    391         if (!g_slist_find(twitter_connections, ic))
     408        if (!g_slist_find(twitter_connections, ic)) {
    392409                return FALSE;
     410        }
    393411
    394412        td = ic->proto_data;
     
    404422                name = g_strdup_printf("%s_%s", td->prefix, ic->acc->user);
    405423                msg = g_strdup_printf("To finish OAuth authentication, please visit "
    406                                       "%s and respond with the resulting PIN code.",
    407                                       info->auth_url);
     424                                      "%s and respond with the resulting PIN code.",
     425                                      info->auth_url);
    408426                imcb_buddy_msg(ic, name, msg, 0, 0);
    409427                g_free(name);
     
    411429        } else if (info->stage == OAUTH_ACCESS_TOKEN) {
    412430                const char *sn;
    413                
     431
    414432                if (info->token == NULL || info->token_secret == NULL) {
    415433                        imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http));
     
    417435                        return FALSE;
    418436                }
    419                
     437
    420438                if ((sn = oauth_params_get(&info->params, "screen_name"))) {
    421                         if (ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0)
     439                        if (ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0) {
    422440                                imcb_log(ic, "Warning: You logged in via OAuth as %s "
    423441                                         "instead of %s.", sn, ic->acc->user);
     442                        }
    424443                        g_free(td->user);
    425444                        td->user = g_strdup(sn);
     
    444463        GMatchInfo *match_info;
    445464
    446         if (regex == NULL)
     465        if (regex == NULL) {
    447466                regex = g_regex_new("(^|\\s)(http(s)?://[^\\s$]+)", 0, 0, NULL);
    448        
     467        }
     468
    449469        g_regex_match(regex, msg, 0, &match_info);
    450470        while (g_match_info_matches(match_info)) {
     
    452472                url_len_diff += target_len - g_utf8_strlen(url, -1);
    453473                /* Add another character for https://t.co/... URLs */
    454                 if (g_match_info_fetch(match_info, 3) != NULL)
     474                if (g_match_info_fetch(match_info, 3) != NULL) {
    455475                        url_len_diff += 1;
     476                }
    456477                g_free(url);
    457478                g_match_info_next(match_info, NULL);
     
    468489        int url_len_diff = 0;
    469490
    470         if (target_len > 0)
     491        if (target_len > 0) {
    471492                url_len_diff = twitter_url_len_diff(msg, target_len);
    472 
    473         if (max == 0 || (len = g_utf8_strlen(msg, -1) + url_len_diff) <= max)
     493        }
     494
     495        if (max == 0 || (len = g_utf8_strlen(msg, -1) + url_len_diff) <= max) {
    474496                return TRUE;
     497        }
    475498
    476499        twitter_log(ic, "Maximum message length exceeded: %d > %d", len, max);
     
    481504static char *set_eval_commands(set_t * set, char *value)
    482505{
    483         if (g_strcasecmp(value, "strict") == 0 )
     506        if (g_strcasecmp(value, "strict") == 0) {
    484507                return value;
    485         else
     508        } else {
    486509                return set_eval_bool(set, value);
     510        }
    487511}
    488512
     
    490514{
    491515        if (g_strcasecmp(value, "one") == 0 ||
    492             g_strcasecmp(value, "many") == 0 || g_strcasecmp(value, "chat") == 0)
     516            g_strcasecmp(value, "many") == 0 || g_strcasecmp(value, "chat") == 0) {
    493517                return value;
    494         else
     518        } else {
    495519                return NULL;
     520        }
    496521}
    497522
     
    507532                def_tul = "22";
    508533                def_mentions = "true";
    509         } else {                /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */
     534        } else {                /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */
    510535                def_url = IDENTICA_API_URL;
    511536                def_tul = "0";
     
    539564
    540565        s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc);
    541        
     566
    542567        s = set_add(&acc->set, "_last_tweet", "0", NULL, acc);
    543568        s->flags |= SET_HIDDEN | SET_NOSAVE;
     
    560585        url_t url;
    561586        char *s;
    562        
     587
    563588        if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) ||
    564589            (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) {
     
    571596            set_getbool(&ic->acc->set, "stream")) {
    572597                imcb_error(ic, "Warning: The streaming API is only supported by Twitter, "
    573                                "and you seem to be connecting to a different service.");
     598                           "and you seem to be connecting to a different service.");
    574599        }
    575600
     
    584609        td->url_port = url.port;
    585610        td->url_host = g_strdup(url.host);
    586         if (strcmp(url.file, "/") != 0)
     611        if (strcmp(url.file, "/") != 0) {
    587612                td->url_path = g_strdup(url.file);
    588         else {
     613        } else {
    589614                td->url_path = g_strdup("");
    590                 if (g_str_has_suffix(url.host, "twitter.com"))
     615                if (g_str_has_suffix(url.host, "twitter.com")) {
    591616                        /* May fire for people who turned on HTTPS. */
    592617                        imcb_error(ic, "Warning: Twitter requires a version number in API calls "
    593                                        "now. Try resetting the base_url account setting.");
    594         }
    595        
     618                                   "now. Try resetting the base_url account setting.");
     619                }
     620        }
     621
    596622        /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com
    597623           into twitter, and try to be sensible if we get anything else. */
    598624        td->prefix = g_strdup(url.host);
    599         if (g_str_has_suffix(td->prefix, ".com"))
     625        if (g_str_has_suffix(td->prefix, ".com")) {
    600626                td->prefix[strlen(url.host) - 4] = '\0';
     627        }
    601628        if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) {
    602629                /* If we have at least 3 chars after the last dot, cut off the rest.
     
    606633                td->prefix = s;
    607634        }
    608        
    609         if (strstr(acc->pass, "oauth_token="))
     635
     636        if (strstr(acc->pass, "oauth_token=")) {
    610637                td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic));
     638        }
    611639
    612640        sprintf(name, "%s_%s", td->prefix, acc->user);
     
    616644        td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH);
    617645        td->log_id = -1;
    618        
     646
    619647        s = set_getstr(&ic->acc->set, "mode");
    620         if (g_strcasecmp(s, "one") == 0)
     648        if (g_strcasecmp(s, "one") == 0) {
    621649                td->flags |= TWITTER_MODE_ONE;
    622         else if (g_strcasecmp(s, "many") == 0)
     650        } else if (g_strcasecmp(s, "many") == 0) {
    623651                td->flags |= TWITTER_MODE_MANY;
    624         else
     652        } else {
    625653                td->flags |= TWITTER_MODE_CHAT;
     654        }
    626655
    627656        twitter_login_finish(ic);
     
    641670        b_event_remove(td->main_loop_id);
    642671
    643         if (td->timeline_gc)
     672        if (td->timeline_gc) {
    644673                imcb_chat_free(td->timeline_gc);
     674        }
    645675
    646676        if (td) {
    647                 if (td->filter_update_id > 0)
     677                if (td->filter_update_id > 0) {
    648678                        b_event_remove(td->filter_update_id);
     679                }
    649680
    650681                http_close(td->stream);
     
    679710
    680711                        strcpy(pin, message);
    681                         for (s = pin + sizeof(pin) - 2; s > pin && g_ascii_isspace(*s); s--)
     712                        for (s = pin + sizeof(pin) - 2; s > pin && g_ascii_isspace(*s); s--) {
    682713                                *s = '\0';
     714                        }
    683715                        for (s = pin; *s && g_ascii_isspace(*s); s++) {
    684716                        }
     
    686718                        if (!oauth_access_token(s, td->oauth_info)) {
    687719                                imcb_error(ic, "OAuth error: %s",
    688                                            "Failed to send access token request");
     720                                           "Failed to send access token request");
    689721                                imc_logout(ic, TRUE);
    690722                                return FALSE;
    691723                        }
    692                 } else
     724                } else {
    693725                        twitter_handle_command(ic, message);
     726                }
    694727        } else {
    695728                twitter_direct_messages_new(ic, who, message);
     
    714747static void twitter_chat_msg(struct groupchat *c, char *message, int flags)
    715748{
    716         if (c && message)
     749        if (c && message) {
    717750                twitter_handle_command(c->ic, message);
     751        }
    718752}
    719753
     
    737771                tf = l->data;
    738772
    739                 if (topic->len > 0)
     773                if (topic->len > 0) {
    740774                        g_string_append(topic, ", ");
    741 
    742                 if (tf->type == TWITTER_FILTER_TYPE_FOLLOW)
     775                }
     776
     777                if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) {
    743778                        g_string_append_c(topic, '@');
     779                }
    744780
    745781                g_string_append(topic, tf->text);
    746782        }
    747783
    748         if (topic->len > 0)
     784        if (topic->len > 0) {
    749785                g_string_prepend(topic, "Twitter Filter: ");
     786        }
    750787
    751788        imcb_chat_topic(c, NULL, topic->str, 0);
     
    820857 *  Returns 0 if the user provides garbage.
    821858 */
    822 static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, char *arg, bee_user_t **bu_) {
     859static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, char *arg, bee_user_t **bu_)
     860{
    823861        struct twitter_data *td = ic->proto_data;
    824862        struct twitter_user_data *tud;
    825863        bee_user_t *bu = NULL;
    826864        guint64 id = 0;
    827        
    828         if (bu_)
     865
     866        if (bu_) {
    829867                *bu_ = NULL;
    830         if (!arg || !arg[0])
     868        }
     869        if (!arg || !arg[0]) {
    831870                return 0;
    832        
     871        }
     872
    833873        if (arg[0] != '#' && (bu = bee_user_by_handle(ic->bee, ic, arg))) {
    834                 if ((tud = bu->data))
     874                if ((tud = bu->data)) {
    835875                        id = tud->last_id;
     876                }
    836877        } else {
    837                 if (arg[0] == '#')
     878                if (arg[0] == '#') {
    838879                        arg++;
     880                }
    839881                if (sscanf(arg, "%" G_GINT64_MODIFIER "x", &id) == 1 &&
    840882                    id < TWITTER_LOG_LENGTH) {
     
    842884                        id = td->log[id].id;
    843885                        /* Beware of dangling pointers! */
    844                         if (!g_slist_find(ic->bee->users, bu))
     886                        if (!g_slist_find(ic->bee->users, bu)) {
    845887                                bu = NULL;
     888                        }
    846889                } else if (sscanf(arg, "%" G_GINT64_MODIFIER "d", &id) == 1) {
    847890                        /* Allow normal tweet IDs as well; not a very useful
    848891                           feature but it's always been there. Just ignore
    849892                           very low IDs to avoid accidents. */
    850                         if (id < 1000000)
     893                        if (id < 1000000) {
    851894                                id = 0;
    852                 }
    853         }
    854         if (bu_)
     895                        }
     896                }
     897        }
     898        if (bu_) {
    855899                *bu_ = bu;
     900        }
    856901        return id;
    857902}
     
    863908        guint64 in_reply_to = 0, id;
    864909        gboolean allow_post =
    865                 g_strcasecmp(set_getstr(&ic->acc->set, "commands"), "strict") != 0;
     910                g_strcasecmp(set_getstr(&ic->acc->set, "commands"), "strict") != 0;
    866911        bee_user_t *bu = NULL;
    867912
     
    874919                /* Not supporting commands if "commands" is set to true/strict. */
    875920        } else if (g_strcasecmp(cmd[0], "undo") == 0) {
    876                 if (cmd[1] == NULL)
     921                if (cmd[1] == NULL) {
    877922                        twitter_status_destroy(ic, td->last_status_id);
    878                 else if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL)))
     923                } else if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL))) {
    879924                        twitter_status_destroy(ic, id);
    880                 else
     925                } else {
    881926                        twitter_log(ic, "Could not undo last action");
     927                }
    882928
    883929                goto eof;
    884930        } else if ((g_strcasecmp(cmd[0], "favourite") == 0 ||
    885                     g_strcasecmp(cmd[0], "favorite") == 0 ||
    886                     g_strcasecmp(cmd[0], "fav") == 0) && cmd[1]) {
     931                    g_strcasecmp(cmd[0], "favorite") == 0 ||
     932                    g_strcasecmp(cmd[0], "fav") == 0) && cmd[1]) {
    887933                if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL))) {
    888934                        twitter_favourite_tweet(ic, id);
     
    900946                    g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) {
    901947                char *screen_name;
    902                
     948
    903949                /* Report nominally works on users but look up the user who
    904950                   posted the given ID if the user wants to do it that way */
    905951                twitter_message_id_from_command_arg(ic, cmd[1], &bu);
    906                 if (bu)
     952                if (bu) {
    907953                        screen_name = bu->handle;
    908                 else
     954                } else {
    909955                        screen_name = cmd[1];
    910                
     956                }
     957
    911958                twitter_report_spam(ic, screen_name);
    912959                goto eof;
     
    915962
    916963                td->last_status_id = 0;
    917                 if (id)
     964                if (id) {
    918965                        twitter_status_retweet(ic, id);
    919                 else
     966                } else {
    920967                        twitter_log(ic, "User `%s' does not exist or didn't "
    921                                     "post any statuses recently", cmd[1]);
     968                                    "post any statuses recently", cmd[1]);
     969                }
    922970
    923971                goto eof;
     
    926974                if (!id || !bu) {
    927975                        twitter_log(ic, "User `%s' does not exist or didn't "
    928                                     "post any statuses recently", cmd[1]);
     976                                    "post any statuses recently", cmd[1]);
    929977                        goto eof;
    930978                }
     
    949997                char *s;
    950998
    951                 if (!twitter_length_check(ic, message))
     999                if (!twitter_length_check(ic, message)) {
    9521000                        goto eof;
     1001                }
    9531002
    9541003                s = cmd[0] + strlen(cmd[0]) - 1;
     
    9601009
    9611010                                new = g_strdup_printf("@%s %s", bu->handle,
    962                                                       message + (s - cmd[0]) + 2);
     1011                                                      message + (s - cmd[0]) + 2);
    9631012                                message = new;
    9641013
    9651014                                if (time(NULL) < tud->last_time +
    966                                     set_getint(&ic->acc->set, "auto_reply_timeout"))
     1015                                    set_getint(&ic->acc->set, "auto_reply_timeout")) {
    9671016                                        in_reply_to = tud->last_id;
     1017                                }
    9681018                        }
    9691019                }
     
    9811031}
    9821032
    983 void twitter_log(struct im_connection *ic, char *format, ... )
     1033void twitter_log(struct im_connection *ic, char *format, ...)
    9841034{
    9851035        struct twitter_data *td = ic->proto_data;
    9861036        va_list params;
    9871037        char *text;
    988        
     1038
    9891039        va_start(params, format);
    9901040        text = g_strdup_vprintf(format, params);
    9911041        va_end(params);
    992        
    993         if (td->timeline_gc)
     1042
     1043        if (td->timeline_gc) {
    9941044                imcb_chat_log(td->timeline_gc, "%s", text);
    995         else
     1045        } else {
    9961046                imcb_log(ic, "%s", text);
    997        
     1047        }
     1048
    9981049        g_free(text);
    9991050}
  • protocols/twitter/twitter.h

    raf359b4 r5ebff60  
    2929
    3030#ifdef DEBUG_TWITTER
    31 #define debug( text... ) imcb_log( ic, text );
     31#define debug(text ...) imcb_log(ic, text);
    3232#else
    33 #define debug( text... )
     33#define debug(text ...)
    3434#endif
    3535
    36 typedef enum
    37 {
     36typedef enum {
    3837        TWITTER_HAVE_FRIENDS   = 0x00001,
    3938        TWITTER_MODE_ONE       = 0x00002,
     
    4544} twitter_flags_t;
    4645
    47 typedef enum
    48 {
     46typedef enum {
    4947        TWITTER_FILTER_TYPE_FOLLOW = 0,
    5048        TWITTER_FILTER_TYPE_TRACK
     
    5351struct twitter_log_data;
    5452
    55 struct twitter_data
    56 {
     53struct twitter_data {
    5754        char* user;
    5855        struct oauth_info *oauth_info;
     
    6562        GSList *follow_ids;
    6663        GSList *filters;
    67        
     64
    6865        guint64 last_status_id; /* For undo */
    6966        gint main_loop_id;
     
    7471        gint http_fails;
    7572        twitter_flags_t flags;
    76        
     73
    7774        /* set base_url */
    7875        gboolean url_ssl;
     
    8279
    8380        char *prefix; /* Used to generate contact + channel name. */
    84        
     81
    8582        /* set show_ids */
    8683        struct twitter_log_data *log;
     
    8986
    9087#define TWITTER_FILTER_UPDATE_WAIT 3000
    91 struct twitter_filter
    92 {
     88struct twitter_filter {
    9389        twitter_filter_type_t type;
    9490        char *text;
     
    9793};
    9894
    99 struct twitter_user_data
    100 {
     95struct twitter_user_data {
    10196        guint64 last_id;
    10297        time_t last_time;
     
    10499
    105100#define TWITTER_LOG_LENGTH 256
    106 struct twitter_log_data
    107 {
     101struct twitter_log_data {
    108102        guint64 id;
    109103        struct bee_user *bu; /* DANGER: can be a dead pointer. Check it first. */
     
    111105
    112106/**
    113  * This has the same function as the msn_connections GSList. We use this to 
     107 * This has the same function as the msn_connections GSList. We use this to
    114108 * make sure the connection is still alive in callbacks before we do anything
    115109 * else.
     
    117111extern GSList *twitter_connections;
    118112
    119 void twitter_login_finish( struct im_connection *ic );
     113void twitter_login_finish(struct im_connection *ic);
    120114
    121115struct http_request;
    122 char *twitter_parse_error( struct http_request *req );
     116char *twitter_parse_error(struct http_request *req);
    123117
    124 void twitter_log(struct im_connection *ic, char *format, ... );
     118void twitter_log(struct im_connection *ic, char *format, ...);
    125119struct groupchat *twitter_groupchat_init(struct im_connection *ic);
    126120
  • protocols/twitter/twitter_http.c

    raf359b4 r5ebff60  
    4848 */
    4949struct http_request *twitter_http(struct im_connection *ic, char *url_string, http_input_function func,
    50                                   gpointer data, int is_post, char **arguments, int arguments_len)
     50                                  gpointer data, int is_post, char **arguments, int arguments_len)
    5151{
    5252        struct twitter_data *td = ic->proto_data;
     
    6868                }
    6969        }
    70        
     70
    7171        if (strstr(url_string, "://")) {
    7272                base_url = g_new0(url_t, 1);
     
    7676                }
    7777        }
    78        
     78
    7979        // Make the request.
    8080        g_string_printf(request, "%s %s%s%s%s HTTP/1.1\r\n"
    81                         "Host: %s\r\n"
    82                         "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
    83                         is_post ? "POST" : "GET",
    84                         base_url ? base_url->file : td->url_path,
    85                         base_url ? "" : url_string,
    86                         is_post ? "" : "?", is_post ? "" : url_arguments,
    87                         base_url ? base_url->host : td->url_host);
     81                        "Host: %s\r\n"
     82                        "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
     83                        is_post ? "POST" : "GET",
     84                        base_url ? base_url->file : td->url_path,
     85                        base_url ? "" : url_string,
     86                        is_post ? "" : "?", is_post ? "" : url_arguments,
     87                        base_url ? base_url->host : td->url_host);
    8888
    8989        // If a pass and user are given we append them to the request.
     
    9292                char *full_url;
    9393
    94                 if (base_url)
     94                if (base_url) {
    9595                        full_url = g_strdup(url_string);
    96                 else
     96                } else {
    9797                        full_url = g_strconcat(set_getstr(&ic->acc->set, "base_url"), url_string, NULL);
     98                }
    9899                full_header = oauth_http_header(td->oauth_info, is_post ? "POST" : "GET",
    99                                                 full_url, url_arguments);
     100                                                full_url, url_arguments);
    100101
    101102                g_string_append_printf(request, "Authorization: %s\r\n", full_header);
     
    116117                // Append the Content-Type and url-encoded arguments.
    117118                g_string_append_printf(request,
    118                                        "Content-Type: application/x-www-form-urlencoded\r\n"
    119                                        "Content-Length: %zd\r\n\r\n%s",
    120                                        strlen(url_arguments), url_arguments);
     119                                       "Content-Type: application/x-www-form-urlencoded\r\n"
     120                                       "Content-Length: %zd\r\n\r\n%s",
     121                                       strlen(url_arguments), url_arguments);
    121122        } else {
    122123                // Append an extra \r\n to end the request...
     
    124125        }
    125126
    126         if (base_url)
    127                 ret = http_dorequest(base_url->host, base_url->port, base_url->proto == PROTO_HTTPS, request->str, func, data);
    128         else
     127        if (base_url) {
     128                ret = http_dorequest(base_url->host, base_url->port, base_url->proto == PROTO_HTTPS, request->str, func,
     129                                     data);
     130        } else {
    129131                ret = http_dorequest(td->url_host, td->url_port, td->url_ssl, request->str, func, data);
     132        }
    130133
    131134        g_free(url_arguments);
     
    136139
    137140struct http_request *twitter_http_f(struct im_connection *ic, char *url_string, http_input_function func,
    138                                     gpointer data, int is_post, char **arguments, int arguments_len, twitter_http_flags_t flags)
     141                                    gpointer data, int is_post, char **arguments, int arguments_len,
     142                                    twitter_http_flags_t flags)
    139143{
    140144        struct http_request *ret = twitter_http(ic, url_string, func, data, is_post, arguments, arguments_len);
    141         if (ret)
     145
     146        if (ret) {
    142147                ret->flags |= flags;
     148        }
    143149        return ret;
    144150}
     
    147153{
    148154        char *key_encoded = g_strndup(key, 3 * strlen(key));
     155
    149156        http_encode(key_encoded);
    150157        char *value_encoded = g_strndup(value, 3 * strlen(value));
     
    152159
    153160        char *retval;
    154         if (strlen(url) != 0)
     161        if (strlen(url) != 0) {
    155162                retval = g_strdup_printf("%s&%s=%s", url, key_encoded, value_encoded);
    156         else
     163        } else {
    157164                retval = g_strdup_printf("%s=%s", key_encoded, value_encoded);
     165        }
    158166
    159167        g_free(key_encoded);
  • protocols/twitter/twitter_http.h

    raf359b4 r5ebff60  
    3939                                  gpointer data, int is_post, char** arguments, int arguments_len);
    4040struct http_request *twitter_http_f(struct im_connection *ic, char *url_string, http_input_function func,
    41                                     gpointer data, int is_post, char** arguments, int arguments_len, twitter_http_flags_t flags);
     41                                    gpointer data, int is_post, char** arguments, int arguments_len,
     42                                    twitter_http_flags_t flags);
    4243
    4344#endif //_TWITTER_HTTP_H
  • protocols/twitter/twitter_lib.c

    raf359b4 r5ebff60  
    2424
    2525/* For strptime(): */
    26 #if(__sun)
     26#if (__sun)
    2727#else
    2828#define _XOPEN_SOURCE
     
    7070static void txu_free(struct twitter_xml_user *txu)
    7171{
    72         if (txu == NULL)
    73                 return;
     72        if (txu == NULL) {
     73                return;
     74        }
    7475
    7576        g_free(txu->name);
     
    8384static void txs_free(struct twitter_xml_status *txs)
    8485{
    85         if (txs == NULL)
    86                 return;
     86        if (txs == NULL) {
     87                return;
     88        }
    8789
    8890        g_free(txs->text);
     
    98100{
    99101        GSList *l;
    100         if (txl == NULL)
    101                 return;
     102
     103        if (txl == NULL) {
     104                return;
     105        }
    102106
    103107        for (l = txl->list; l; l = g_slist_next(l)) {
     
    148152                           exact Twitter username. */
    149153                        imcb_buddy_nick_hint(ic, name, name);
    150                         if (td->timeline_gc)
     154                        if (td->timeline_gc) {
    151155                                imcb_chat_add_buddy(td->timeline_gc, name);
    152                 } else if (td->flags & TWITTER_MODE_MANY)
     156                        }
     157                } else if (td->flags & TWITTER_MODE_MANY) {
    153158                        imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL);
     159                }
    154160        }
    155161}
     
    171177                    err->type == json_object) {
    172178                        const char *msg = json_o_str(err, "message");
    173                         if (msg)
     179                        if (msg) {
    174180                                ret = g_strdup_printf("%s (%s)", req->status_string, msg);
     181                        }
    175182                }
    176183                json_value_free(root);
     
    189196        json_value *ret;
    190197        char path[64] = "", *s;
    191        
     198
    192199        if ((s = strchr(req->request, ' '))) {
    193                 path[sizeof(path)-1] = '\0';
     200                path[sizeof(path) - 1] = '\0';
    194201                strncpy(path, s + 1, sizeof(path) - 1);
    195                 if ((s = strchr(path, '?')) || (s = strchr(path, ' ')))
     202                if ((s = strchr(path, '?')) || (s = strchr(path, ' '))) {
    196203                        *s = '\0';
    197         }
    198        
     204                }
     205        }
     206
    199207        /* Kinda nasty. :-( Trying to suppress error messages, but only
    200208           for periodic (i.e. mentions/timeline) queries. */
    201209        periodic = strstr(path, "timeline") || strstr(path, "mentions");
    202        
     210
    203211        if (req->status_code == 401 && logging_in) {
    204212                /* IIRC Twitter once had an outage where they were randomly
     
    206214                   only during login. */
    207215                imcb_error(ic, "Authentication failure (%s)",
    208                                twitter_parse_error(req));
     216                           twitter_parse_error(req));
    209217                imc_logout(ic, FALSE);
    210218                return NULL;
    211219        } else if (req->status_code != 200) {
    212220                // It didn't go well, output the error and return.
    213                 if (!periodic || logging_in || ++td->http_fails >= 5)
     221                if (!periodic || logging_in || ++td->http_fails >= 5) {
    214222                        twitter_log(ic, "Error: Could not retrieve %s: %s",
    215                                     path, twitter_parse_error(req));
    216                
    217                 if (logging_in)
     223                                    path, twitter_parse_error(req));
     224                }
     225
     226                if (logging_in) {
    218227                        imc_logout(ic, TRUE);
     228                }
    219229                return NULL;
    220230        } else {
     
    224234        if ((ret = json_parse(req->reply_body, req->body_size)) == NULL) {
    225235                imcb_error(ic, "Could not retrieve %s: %s",
    226                            path, "XML parse error");
     236                           path, "XML parse error");
    227237        }
    228238        return ret;
     
    238248        // Primitive, but hey! It works...
    239249        char *args[2];
     250
    240251        args[0] = "cursor";
    241252        args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor);
     
    257268
    258269        c = json_o_get(node, "ids");
    259         if (!c || c->type != json_array)
     270        if (!c || c->type != json_array) {
    260271                return FALSE;
    261 
    262         for (i = 0; i < c->u.array.length; i ++) {
    263                 if (c->u.array.values[i]->type != json_integer)
     272        }
     273
     274        for (i = 0; i < c->u.array.length; i++) {
     275                if (c->u.array.values[i]->type != json_integer) {
    264276                        continue;
    265                
     277                }
     278
    266279                txl->list = g_slist_prepend(txl->list,
    267                         g_strdup_printf("%" PRIu64, c->u.array.values[i]->u.integer));
    268         }
    269        
     280                                            g_strdup_printf("%" PRIu64, c->u.array.values[i]->u.integer));
     281        }
     282
    270283        c = json_o_get(node, "next_cursor");
    271         if (c && c->type == json_integer)
     284        if (c && c->type == json_integer) {
    272285                txl->next_cursor = c->u.integer;
    273         else
     286        } else {
    274287                txl->next_cursor = -1;
    275        
     288        }
     289
    276290        return TRUE;
    277291}
     
    292306
    293307        // Check if the connection is still active.
    294         if (!g_slist_find(twitter_connections, ic))
    295                 return;
     308        if (!g_slist_find(twitter_connections, ic)) {
     309                return;
     310        }
    296311
    297312        td = ic->proto_data;
     
    301316
    302317        // Parse the data.
    303         if (!(parsed = twitter_parse_response(ic, req)))
    304                 return;
    305        
     318        if (!(parsed = twitter_parse_response(ic, req))) {
     319                return;
     320        }
     321
    306322        twitter_xt_get_friends_id_list(parsed, txl);
    307323        json_value_free(parsed);
    308324
    309325        td->follow_ids = txl->list;
    310         if (txl->next_cursor)
     326        if (txl->next_cursor) {
    311327                /* These were just numbers. Up to 4000 in a response AFAIK so if we get here
    312328                   we may be using a spammer account. \o/ */
    313329                twitter_get_friends_ids(ic, txl->next_cursor);
    314         else
     330        } else {
    315331                /* Now to convert all those numbers into names.. */
    316332                twitter_get_users_lookup(ic);
     333        }
    317334
    318335        txl->list = NULL;
     
    332349        GString *ids = g_string_new("");
    333350        int i;
    334        
     351
    335352        /* We can request up to 100 users at a time. */
    336         for (i = 0; i < 100 && td->follow_ids; i ++) {
    337                 g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data);
     353        for (i = 0; i < 100 && td->follow_ids; i++) {
     354                g_string_append_printf(ids, ",%s", (char *) td->follow_ids->data);
    338355                g_free(td->follow_ids->data);
    339356                td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data);
     
    354371 * Callback for getting (twitter)friends...
    355372 *
    356  * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has 
    357  * hundreds of friends?" you wonder? You probably not, since you are reading the source of 
     373 * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has
     374 * hundreds of friends?" you wonder? You probably not, since you are reading the source of
    358375 * BitlBee... Get a life and meet new people!
    359376 */
     
    367384
    368385        // Check if the connection is still active.
    369         if (!g_slist_find(twitter_connections, ic))
    370                 return;
     386        if (!g_slist_find(twitter_connections, ic)) {
     387                return;
     388        }
    371389
    372390        txl = g_new0(struct twitter_xml_list, 1);
     
    374392
    375393        // Get the user list from the parsed xml feed.
    376         if (!(parsed = twitter_parse_response(ic, req)))
    377                 return;
     394        if (!(parsed = twitter_parse_response(ic, req))) {
     395                return;
     396        }
    378397        twitter_xt_get_users(parsed, txl);
    379398        json_value_free(parsed);
     
    395414        struct twitter_xml_user *txu;
    396415        json_value *jv;
    397        
     416
    398417        txu = g_new0(struct twitter_xml_user, 1);
    399418        txu->name = g_strdup(json_o_str(node, "name"));
    400419        txu->screen_name = g_strdup(json_o_str(node, "screen_name"));
    401        
     420
    402421        jv = json_o_get(node, "id");
    403422        txu->uid = jv->u.integer;
    404        
     423
    405424        return txu;
    406425}
     
    419438        txl->type = TXL_USER;
    420439
    421         if (!node || node->type != json_array)
     440        if (!node || node->type != json_array) {
    422441                return FALSE;
     442        }
    423443
    424444        // The root <users> node should hold the list of users <user>
    425445        // Walk over the nodes children.
    426         for (i = 0; i < node->u.array.length; i ++) {
     446        for (i = 0; i < node->u.array.length; i++) {
    427447                txu = twitter_xt_get_user(node->u.array.values[i]);
    428                 if (txu)
     448                if (txu) {
    429449                        txl->list = g_slist_prepend(txl->list, txu);
     450                }
    430451        }
    431452
     
    453474        struct twitter_xml_status *txs;
    454475        const json_value *rt = NULL, *entities = NULL;
    455        
    456         if (node->type != json_object)
     476
     477        if (node->type != json_object) {
    457478                return FALSE;
     479        }
    458480        txs = g_new0(struct twitter_xml_status, 1);
    459481
    460         JSON_O_FOREACH (node, k, v) {
     482        JSON_O_FOREACH(node, k, v) {
    461483                if (strcmp("text", k) == 0 && v->type == json_string) {
    462484                        txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1);
     
    470492                           this field. :-( Also assumes the timezone used
    471493                           is UTC since C time handling functions suck. */
    472                         if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL)
     494                        if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) {
    473495                                txs->created_at = mktime_utc(&parsed);
     496                        }
    474497                } else if (strcmp("user", k) == 0 && v->type == json_object) {
    475498                        txs->user = twitter_xt_get_user(v);
     
    497520        }
    498521
    499         if (txs->text && txs->user && txs->id)
     522        if (txs->text && txs->user && txs->id) {
    500523                return txs;
    501        
     524        }
     525
    502526        txs_free(txs);
    503527        return NULL;
     
    511535        struct twitter_xml_status *txs;
    512536        const json_value *entities = NULL;
    513        
    514         if (node->type != json_object)
     537
     538        if (node->type != json_object) {
    515539                return FALSE;
     540        }
    516541        txs = g_new0(struct twitter_xml_status, 1);
    517542
    518         JSON_O_FOREACH (node, k, v) {
     543        JSON_O_FOREACH(node, k, v) {
    519544                if (strcmp("text", k) == 0 && v->type == json_string) {
    520545                        txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1);
     
    526551                           this field. :-( Also assumes the timezone used
    527552                           is UTC since C time handling functions suck. */
    528                         if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL)
     553                        if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) {
    529554                                txs->created_at = mktime_utc(&parsed);
     555                        }
    530556                } else if (strcmp("sender", k) == 0 && v->type == json_object) {
    531557                        txs->user = twitter_xt_get_user(v);
     
    539565        }
    540566
    541         if (txs->text && txs->user && txs->id)
     567        if (txs->text && txs->user && txs->id) {
    542568                return txs;
    543        
     569        }
     570
    544571        txs_free(txs);
    545572        return NULL;
    546573}
    547574
    548 static char* expand_entities(char* text, const json_value *entities) {
    549         JSON_O_FOREACH (entities, k, v) {
     575static char* expand_entities(char* text, const json_value *entities)
     576{
     577        JSON_O_FOREACH(entities, k, v) {
    550578                int i;
    551                
    552                 if (v->type != json_array)
     579
     580                if (v->type != json_array) {
    553581                        continue;
    554                 if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0)
     582                }
     583                if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0) {
    555584                        continue;
    556                
    557                 for (i = 0; i < v->u.array.length; i ++) {
    558                         if (v->u.array.values[i]->type != json_object)
     585                }
     586
     587                for (i = 0; i < v->u.array.length; i++) {
     588                        if (v->u.array.values[i]->type != json_object) {
    559589                                continue;
    560                        
     590                        }
     591
    561592                        const char *kort = json_o_str(v->u.array.values[i], "url");
    562593                        const char *disp = json_o_str(v->u.array.values[i], "display_url");
    563594                        char *pos, *new;
    564                        
    565                         if (!kort || !disp || !(pos = strstr(text, kort)))
     595
     596                        if (!kort || !disp || !(pos = strstr(text, kort))) {
    566597                                continue;
    567                        
     598                        }
     599
    568600                        *pos = '\0';
    569601                        new = g_strdup_printf("%s%s <%s>%s", text, kort,
    570602                                              disp, pos + strlen(kort));
    571                        
     603
    572604                        g_free(text);
    573605                        text = new;
    574606                }
    575607        }
    576        
     608
    577609        return text;
    578610}
     
    592624        // Set the type of the list.
    593625        txl->type = TXL_STATUS;
    594        
    595         if (node->type != json_array)
     626
     627        if (node->type != json_array) {
    596628                return FALSE;
     629        }
    597630
    598631        // The root <statuses> node should hold the list of statuses <status>
    599632        // Walk over the nodes children.
    600         for (i = 0; i < node->u.array.length; i ++) {
     633        for (i = 0; i < node->u.array.length; i++) {
    601634                txs = twitter_xt_get_status(node->u.array.values[i]);
    602                 if (!txs)
     635                if (!txs) {
    603636                        continue;
    604                
     637                }
     638
    605639                txl->list = g_slist_prepend(txl->list, txs);
    606640        }
     
    612646   Plus, show_ids is on by default and I don't see why anyone would disable it. */
    613647static char *twitter_msg_add_id(struct im_connection *ic,
    614                                 struct twitter_xml_status *txs, const char *prefix)
     648                                struct twitter_xml_status *txs, const char *prefix)
    615649{
    616650        struct twitter_data *td = ic->proto_data;
     
    620654        if (txs->reply_to) {
    621655                int i;
    622                 for (i = 0; i < TWITTER_LOG_LENGTH; i++)
     656                for (i = 0; i < TWITTER_LOG_LENGTH; i++) {
    623657                        if (td->log[i].id == txs->reply_to) {
    624658                                reply_to = i;
    625659                                break;
    626660                        }
     661                }
    627662        }
    628663
     
    636671                }
    637672        }
    638        
     673
    639674        td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH;
    640675        td->log[td->log_id].id = txs->id;
    641676        td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name);
    642        
     677
    643678        /* This is all getting hairy. :-( If we RT'ed something ourselves,
    644679           remember OUR id instead so undo will work. In other cases, the
    645680           original tweet's id should be remembered for deduplicating. */
    646         if (g_strcasecmp(txs->user->screen_name, td->user) == 0)
     681        if (g_strcasecmp(txs->user->screen_name, td->user) == 0) {
    647682                td->log[td->log_id].id = txs->rt_id;
    648        
     683        }
     684
    649685        if (set_getbool(&ic->acc->set, "show_ids")) {
    650                 if (reply_to != -1)
     686                if (reply_to != -1) {
    651687                        return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s",
    652688                                               td->log_id, reply_to, prefix, txs->text);
    653                 else
     689                } else {
    654690                        return g_strdup_printf("\002[\002%02x\002]\002 %s%s",
    655691                                               td->log_id, prefix, txs->text);
     692                }
    656693        } else {
    657                 if (*prefix)
     694                if (*prefix) {
    658695                        return g_strconcat(prefix, txs->text, NULL);
    659                 else
     696                } else {
    660697                        return NULL;
     698                }
    661699        }
    662700}
     
    678716                switch (tf->type) {
    679717                case TWITTER_FILTER_TYPE_FOLLOW:
    680                         if (status->user->uid != tf->uid)
     718                        if (status->user->uid != tf->uid) {
    681719                                continue;
     720                        }
    682721                        break;
    683722
    684723                case TWITTER_FILTER_TYPE_TRACK:
    685                         if (strcasestr(status->text, tf->text) == NULL)
     724                        if (strcasestr(status->text, tf->text) == NULL) {
    686725                                continue;
     726                        }
    687727                        break;
    688728
     
    693733                for (l = tf->groupchats; l; l = g_slist_next(l)) {
    694734                        imcb_chat_msg(l->data, status->user->screen_name,
    695                                       msg ? msg : status->text, 0, 0);
     735                                      msg ? msg : status->text, 0, 0);
    696736                }
    697737        }
     
    713753        gc = twitter_groupchat_init(ic);
    714754
    715         if (!me)
     755        if (!me) {
    716756                /* MUST be done before twitter_msg_add_id() to avoid #872. */
    717757                twitter_add_buddy(ic, status->user->screen_name, status->user->name);
     758        }
    718759        msg = twitter_msg_add_id(ic, status, "");
    719        
     760
    720761        // Say it!
    721762        if (me) {
     
    723764        } else {
    724765                imcb_chat_msg(gc, status->user->screen_name,
    725                               msg ? msg : status->text, 0, status->created_at);
     766                              msg ? msg : status->text, 0, status->created_at);
    726767        }
    727768
     
    744785        }
    745786
    746         if (td->flags & TWITTER_MODE_ONE)
     787        if (td->flags & TWITTER_MODE_ONE) {
    747788                prefix = g_strdup_printf("\002<\002%s\002>\002 ",
    748789                                         status->user->screen_name);
    749         else if (!me)
     790        } else if (!me) {
    750791                twitter_add_buddy(ic, status->user->screen_name, status->user->name);
    751         else
     792        } else {
    752793                prefix = g_strdup("You: ");
     794        }
    753795
    754796        text = twitter_msg_add_id(ic, status, prefix ? prefix : "");
     
    766808        struct twitter_data *td = ic->proto_data;
    767809        char *last_id_str;
    768        
    769         if (status->user == NULL || status->text == NULL)
    770                 return;
    771        
     810
     811        if (status->user == NULL || status->text == NULL) {
     812                return;
     813        }
     814
    772815        /* Grrrr. Would like to do this during parsing, but can't access
    773816           settings from there. */
    774         if (set_getbool(&ic->acc->set, "strip_newlines"))
     817        if (set_getbool(&ic->acc->set, "strip_newlines")) {
    775818                strip_newlines(status->text);
    776        
    777         if (status->from_filter)
     819        }
     820
     821        if (status->from_filter) {
    778822                twitter_status_show_filter(ic, status);
    779         else if (td->flags & TWITTER_MODE_CHAT)
     823        } else if (td->flags & TWITTER_MODE_CHAT) {
    780824                twitter_status_show_chat(ic, status);
    781         else
     825        } else {
    782826                twitter_status_show_msg(ic, status);
     827        }
    783828
    784829        // Update the timeline_id to hold the highest id, so that by the next request
     
    801846        char c, *nl;
    802847        gboolean from_filter;
    803        
    804         if (!g_slist_find(twitter_connections, ic))
    805                 return;
    806        
     848
     849        if (!g_slist_find(twitter_connections, ic)) {
     850                return;
     851        }
     852
    807853        ic->flags |= OPT_PONGED;
    808854        td = ic->proto_data;
    809        
     855
    810856        if ((req->flags & HTTPC_EOF) || !req->reply_body) {
    811                 if (req == td->stream)
     857                if (req == td->stream) {
    812858                        td->stream = NULL;
    813                 else if (req == td->filter_stream)
     859                } else if (req == td->filter_stream) {
    814860                        td->filter_stream = NULL;
     861                }
    815862
    816863                imcb_error(ic, "Stream closed (%s)", req->status_string);
     
    818865                return;
    819866        }
    820        
     867
    821868        /* MUST search for CRLF, not just LF:
    822869           https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */
    823         if (!(nl = strstr(req->reply_body, "\r\n")))
    824                 return;
    825        
     870        if (!(nl = strstr(req->reply_body, "\r\n"))) {
     871                return;
     872        }
     873
    826874        len = nl - req->reply_body;
    827875        if (len > 0) {
    828876                c = req->reply_body[len];
    829877                req->reply_body[len] = '\0';
    830                
     878
    831879                if ((parsed = json_parse(req->reply_body, req->body_size))) {
    832880                        from_filter = (req == td->filter_stream);
     
    836884                req->reply_body[len] = c;
    837885        }
    838        
     886
    839887        http_flush_bytes(req, len + 2);
    840        
     888
    841889        /* One notification might bring multiple events! */
    842         if (req->body_size > 0)
     890        if (req->body_size > 0) {
    843891                twitter_http_stream(req);
     892        }
    844893}
    845894
     
    852901        struct twitter_xml_status *txs;
    853902        json_value *c;
    854        
     903
    855904        if ((txs = twitter_xt_get_status(o))) {
    856905                txs->from_filter = from_filter;
     
    860909        } else if ((c = json_o_get(o, "direct_message")) &&
    861910                   (txs = twitter_xt_get_dm(c))) {
    862                 if (g_strcasecmp(txs->user->screen_name, td->user) != 0)
     911                if (g_strcasecmp(txs->user->screen_name, td->user) != 0) {
    863912                        imcb_buddy_msg(ic, txs->user->screen_name,
    864                                        txs->text, 0, txs->created_at);
     913                                       txs->text, 0, txs->created_at);
     914                }
    865915                txs_free(txs);
    866916                return TRUE;
     
    886936        struct twitter_data *td = ic->proto_data;
    887937        int i;
    888        
     938
    889939        for (i = 0; i < TWITTER_LOG_LENGTH; i++) {
    890940                if (td->log[i].id == txs->id) {
     
    893943                }
    894944        }
    895        
     945
    896946        if (!(g_strcasecmp(txs->user->screen_name, td->user) == 0 ||
    897947              set_getbool(&ic->acc->set, "fetch_mentions") ||
     
    905955                return TRUE;
    906956        }
    907        
     957
    908958        twitter_status_show(ic, txs);
    909        
     959
    910960        return TRUE;
    911961}
     
    917967        json_value *target = json_o_get(o, "target");
    918968        const char *type = json_o_str(o, "event");
    919        
     969
    920970        if (!type || !source || source->type != json_object
    921                   || !target || target->type != json_object) {
     971            || !target || target->type != json_object) {
    922972                return FALSE;
    923973        }
    924        
     974
    925975        if (strcmp(type, "follow") == 0) {
    926976                struct twitter_xml_user *us = twitter_xt_get_user(source);
     
    932982                txu_free(ut);
    933983        }
    934        
     984
    935985        return TRUE;
    936986}
     
    939989{
    940990        struct twitter_data *td = ic->proto_data;
    941         char *args[2] = {"with", "followings"};
    942        
     991        char *args[2] = { "with", "followings" };
     992
    943993        if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL,
    944994                                       twitter_http_stream, ic, 0, args, 2))) {
     
    948998                return TRUE;
    949999        }
    950        
     1000
    9511001        return FALSE;
    9521002}
     
    9551005{
    9561006        struct twitter_data *td = ic->proto_data;
    957         char *args[4] = {"follow", NULL, "track", NULL};
     1007        char *args[4] = { "follow", NULL, "track", NULL };
    9581008        GString *followstr = g_string_new("");
    9591009        GString *trackstr = g_string_new("");
     
    9671017                switch (tf->type) {
    9681018                case TWITTER_FILTER_TYPE_FOLLOW:
    969                         if (followstr->len > 0)
     1019                        if (followstr->len > 0) {
    9701020                                g_string_append_c(followstr, ',');
     1021                        }
    9711022
    9721023                        g_string_append_printf(followstr, "%" G_GUINT64_FORMAT,
    973                                                tf->uid);
     1024                                               tf->uid);
    9741025                        break;
    9751026
    9761027                case TWITTER_FILTER_TYPE_TRACK:
    977                         if (trackstr->len > 0)
     1028                        if (trackstr->len > 0) {
    9781029                                g_string_append_c(trackstr, ',');
     1030                        }
    9791031
    9801032                        g_string_append(trackstr, tf->text);
     
    9891041        args[3] = trackstr->str;
    9901042
    991         if (td->filter_stream)
     1043        if (td->filter_stream) {
    9921044                http_close(td->filter_stream);
     1045        }
    9931046
    9941047        if ((td->filter_stream = twitter_http(ic, TWITTER_FILTER_STREAM_URL,
    9951048                                              twitter_http_stream, ic, 0,
    996                                               args, 4))) {
     1049                                              args, 4))) {
    9971050                /* This flag must be enabled or we'll get no data until EOF
    9981051                   (which err, kind of, defeats the purpose of a streaming API). */
     
    10221075
    10231076        // Check if the connection is still active.
    1024         if (!g_slist_find(twitter_connections, ic))
    1025                 return;
     1077        if (!g_slist_find(twitter_connections, ic)) {
     1078                return;
     1079        }
    10261080
    10271081        td = ic->proto_data;
    10281082
    1029         if (!(parsed = twitter_parse_response(ic, req)))
    1030                 return;
     1083        if (!(parsed = twitter_parse_response(ic, req))) {
     1084                return;
     1085        }
    10311086
    10321087        for (l = td->filters; l; l = g_slist_next(l)) {
    10331088                tf = l->data;
    10341089
    1035                 if (tf->type == TWITTER_FILTER_TYPE_FOLLOW)
     1090                if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) {
    10361091                        users = g_list_prepend(users, tf);
    1037         }
    1038 
    1039         if (parsed->type != json_array)
     1092                }
     1093        }
     1094
     1095        if (parsed->type != json_array) {
    10401096                goto finish;
     1097        }
    10411098
    10421099        for (i = 0; i < parsed->u.array.length; i++) {
     
    10441101                name = json_o_str(parsed->u.array.values[i], "screen_name");
    10451102
    1046                 if (!name || !id || id->type != json_integer)
     1103                if (!name || !id || id->type != json_integer) {
    10471104                        continue;
     1105                }
    10481106
    10491107                for (u = users; u; u = g_list_next(u)) {
     
    10621120        twitter_filter_stream(ic);
    10631121
    1064         if (!users)
    1065                 return;
     1122        if (!users) {
     1123                return;
     1124        }
    10661125
    10671126        fstr = g_string_new("");
    10681127
    10691128        for (u = users; u; u = g_list_next(u)) {
    1070                 if (fstr->len > 0)
     1129                if (fstr->len > 0) {
    10711130                        g_string_append(fstr, ", ");
     1131                }
    10721132
    10731133                g_string_append(fstr, tf->text);
     
    10831143{
    10841144        struct twitter_data *td = ic->proto_data;
    1085         char *args[2] = {"screen_name", NULL};
     1145        char *args[2] = { "screen_name", NULL };
    10861146        GString *ustr = g_string_new("");
    10871147        struct twitter_filter *tf;
     
    10921152                tf = l->data;
    10931153
    1094                 if (tf->type != TWITTER_FILTER_TYPE_FOLLOW || tf->uid != 0)
     1154                if (tf->type != TWITTER_FILTER_TYPE_FOLLOW || tf->uid != 0) {
    10951155                        continue;
    1096 
    1097                 if (ustr->len > 0)
     1156                }
     1157
     1158                if (ustr->len > 0) {
    10981159                        g_string_append_c(ustr, ',');
     1160                }
    10991161
    11001162                g_string_append(ustr, tf->text);
     
    11081170        args[1] = ustr->str;
    11091171        req = twitter_http(ic, TWITTER_USERS_LOOKUP_URL,
    1110                            twitter_filter_users_post,
    1111                            ic, 0, args, 2);
     1172                           twitter_filter_users_post,
     1173                           ic, 0, args, 2);
    11121174
    11131175        g_string_free(ustr, TRUE);
     
    11411203                twitter_get_mentions(ic, next_cursor);
    11421204        }
    1143        
     1205
    11441206        return TRUE;
    11451207}
     
    11611223
    11621224        imcb_connected(ic);
    1163        
     1225
    11641226        if (!(td->flags & TWITTER_GOT_TIMELINE)) {
    11651227                return;
     
    11891251        while (output) {
    11901252                struct twitter_xml_status *txs = output->data;
    1191                 if (txs->id != last_id)
     1253                if (txs->id != last_id) {
    11921254                        twitter_status_show(ic, txs);
     1255                }
    11931256                last_id = txs->id;
    11941257                output = g_slist_remove(output, txs);
     
    12271290
    12281291        if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args,
    1229                      td->timeline_id ? 6 : 4) == NULL) {
    1230                 if (++td->http_fails >= 5)
     1292                         td->timeline_id ? 6 : 4) == NULL) {
     1293                if (++td->http_fails >= 5) {
    12311294                        imcb_error(ic, "Could not retrieve %s: %s",
    12321295                                   TWITTER_HOME_TIMELINE_URL, "connection failed");
     1296                }
    12331297                td->flags |= TWITTER_GOT_TIMELINE;
    12341298                twitter_flush_timeline(ic);
     
    12671331        if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions,
    12681332                         ic, 0, args, 6) == NULL) {
    1269                 if (++td->http_fails >= 5)
     1333                if (++td->http_fails >= 5) {
    12701334                        imcb_error(ic, "Could not retrieve %s: %s",
    12711335                                   TWITTER_MENTIONS_URL, "connection failed");
     1336                }
    12721337                td->flags |= TWITTER_GOT_MENTIONS;
    12731338                twitter_flush_timeline(ic);
     
    12891354
    12901355        // Check if the connection is still active.
    1291         if (!g_slist_find(twitter_connections, ic))
    1292                 return;
     1356        if (!g_slist_find(twitter_connections, ic)) {
     1357                return;
     1358        }
    12931359
    12941360        td = ic->proto_data;
     
    12981364
    12991365        // The root <statuses> node should hold the list of statuses <status>
    1300         if (!(parsed = twitter_parse_response(ic, req)))
     1366        if (!(parsed = twitter_parse_response(ic, req))) {
    13011367                goto end;
     1368        }
    13021369        twitter_xt_get_status_list(ic, parsed, txl);
    13031370        json_value_free(parsed);
     
    13051372        td->home_timeline_obj = txl;
    13061373
    1307       end:
    1308         if (!g_slist_find(twitter_connections, ic))
    1309                 return;
     1374end:
     1375        if (!g_slist_find(twitter_connections, ic)) {
     1376                return;
     1377        }
    13101378
    13111379        td->flags |= TWITTER_GOT_TIMELINE;
     
    13251393
    13261394        // Check if the connection is still active.
    1327         if (!g_slist_find(twitter_connections, ic))
    1328                 return;
     1395        if (!g_slist_find(twitter_connections, ic)) {
     1396                return;
     1397        }
    13291398
    13301399        td = ic->proto_data;
     
    13341403
    13351404        // The root <statuses> node should hold the list of statuses <status>
    1336         if (!(parsed = twitter_parse_response(ic, req)))
     1405        if (!(parsed = twitter_parse_response(ic, req))) {
    13371406                goto end;
     1407        }
    13381408        twitter_xt_get_status_list(ic, parsed, txl);
    13391409        json_value_free(parsed);
     
    13411411        td->mentions_obj = txl;
    13421412
    1343       end:
    1344         if (!g_slist_find(twitter_connections, ic))
    1345                 return;
     1413end:
     1414        if (!g_slist_find(twitter_connections, ic)) {
     1415                return;
     1416        }
    13461417
    13471418        td->flags |= TWITTER_GOT_MENTIONS;
     
    13611432
    13621433        // Check if the connection is still active.
    1363         if (!g_slist_find(twitter_connections, ic))
    1364                 return;
     1434        if (!g_slist_find(twitter_connections, ic)) {
     1435                return;
     1436        }
    13651437
    13661438        td = ic->proto_data;
    13671439        td->last_status_id = 0;
    13681440
    1369         if (!(parsed = twitter_parse_response(ic, req)))
    1370                 return;
    1371        
     1441        if (!(parsed = twitter_parse_response(ic, req))) {
     1442                return;
     1443        }
     1444
    13721445        if ((id = json_o_get(parsed, "id")) && id->type == json_integer) {
    13731446                td->last_status_id = id->u.integer;
    13741447        }
    1375        
     1448
    13761449        json_value_free(parsed);
    1377        
    1378         if (req->flags & TWITTER_HTTP_USER_ACK)
     1450
     1451        if (req->flags & TWITTER_HTTP_USER_ACK) {
    13791452                twitter_log(ic, "Command processed successfully");
     1453        }
    13801454}
    13811455
     
    13901464                g_strdup_printf("%" G_GUINT64_FORMAT, in_reply_to)
    13911465        };
     1466
    13921467        twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1,
    1393                      args, in_reply_to ? 4 : 2);
     1468                     args, in_reply_to ? 4 : 2);
    13941469        g_free(args[3]);
    13951470}
     
    14021477{
    14031478        char *args[4];
     1479
    14041480        args[0] = "screen_name";
    14051481        args[1] = who;
     
    14131489{
    14141490        char *args[2];
     1491
    14151492        args[0] = "screen_name";
    14161493        args[1] = who;
    14171494        twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL,
    1418                      twitter_http_post, ic, 1, args, 2);
     1495                     twitter_http_post, ic, 1, args, 2);
    14191496}
    14201497
     
    14221499{
    14231500        char *url;
     1501
    14241502        url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s",
    14251503                              TWITTER_STATUS_DESTROY_URL, id, ".json");
     
    14321510{
    14331511        char *url;
     1512
    14341513        url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s",
    14351514                              TWITTER_STATUS_RETWEET_URL, id, ".json");
     
    14481527                NULL,
    14491528        };
     1529
    14501530        args[1] = screen_name;
    14511531        twitter_http_f(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post,
     
    14621542                NULL,
    14631543        };
     1544
    14641545        args[1] = g_strdup_printf("%" G_GUINT64_FORMAT, id);
    14651546        twitter_http_f(ic, TWITTER_FAVORITE_CREATE_URL, twitter_http_post,
Note: See TracChangeset for help on using the changeset viewer.