Changeset 3ca001b


Ignore:
Timestamp:
2012-12-02T12:53:12Z (11 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
2dcde94
Parents:
e8161ec
Message:

Extend twitter_message_id_from_command_arg() a little bit and use it for all
commands. Also fixed reply command in strict commands mode.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter.c

    re8161ec r3ca001b  
    553553 *  Returns 0 if the user provides garbage.
    554554 */
    555 static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, struct twitter_data *td, char *arg) {
     555static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, char *arg, bee_user_t **bu_) {
     556        struct twitter_data *td = ic->proto_data;
    556557        struct twitter_user_data *tud;
    557         bee_user_t *bu;
     558        bee_user_t *bu = NULL;
    558559        guint64 id = 0;
    559         if (g_str_has_prefix(arg, "#") &&
    560             sscanf(arg + 1, "%" G_GINT64_MODIFIER "x", &id) == 1) {
    561                 if (id < TWITTER_LOG_LENGTH && td->log)
     560       
     561        if (bu_)
     562                *bu_ = NULL;
     563        if (!arg || !arg[0])
     564                return 0;
     565       
     566        if (arg[0] != '#' && (bu = bee_user_by_handle(ic->bee, ic, arg))) {
     567                if ((tud = bu->data))
     568                        id = tud->last_id;
     569        } else {
     570                if (arg[0] == '#')
     571                        arg++;
     572                if (sscanf(arg, "%" G_GINT64_MODIFIER "x", &id) == 1 &&
     573                    id < TWITTER_LOG_LENGTH) {
     574                        bu = td->log[id].bu;
    562575                        id = td->log[id].id;
    563         } else if ((bu = bee_user_by_handle(ic->bee, ic, arg)) &&
    564                 (tud = bu->data) && tud->last_id)
    565                 id = tud->last_id;
    566         else if (sscanf(arg, "%" G_GINT64_MODIFIER "x", &id) == 1){
    567                 if (id < TWITTER_LOG_LENGTH && td->log)
    568                         id = td->log[id].id;
    569         }
     576                        /* Beware of dangling pointers! */
     577                        if (!g_slist_find(ic->bee->users, bu))
     578                                bu = NULL;
     579                } else if (sscanf(arg, "%" G_GINT64_MODIFIER "d", &id) == 1) {
     580                        /* Allow normal tweet IDs as well; not a very useful
     581                           feature but it's always been there. Just ignore
     582                           very low IDs to avoid accidents. */
     583                        if (id < 1000000)
     584                                id = 0;
     585                }
     586        }
     587        if (bu_)
     588                *bu_ = bu;
    570589        return id;
    571590}
     
    575594        struct twitter_data *td = ic->proto_data;
    576595        char *cmds, **cmd, *new = NULL;
    577         guint64 in_reply_to = 0;
    578         gboolean strict_commands =
    579                 g_strcasecmp(set_getstr(&ic->acc->set, "commands"), "strict") == 0;
     596        guint64 in_reply_to = 0, id;
     597        gboolean allow_post =
     598                g_strcasecmp(set_getstr(&ic->acc->set, "commands"), "strict") != 0;
     599        bee_user_t *bu = NULL;
    580600
    581601        cmds = g_strdup(message);
     
    583603
    584604        if (cmd[0] == NULL) {
    585                 g_free(cmds);
    586                 return;
    587         } else if (!(strict_commands || set_getbool(&ic->acc->set, "commands"))) {
    588                 /* Not supporting commands. */
     605                goto eof;
     606        } else if (!set_getbool(&ic->acc->set, "commands") && allow_post) {
     607                /* Not supporting commands if "commands" is set to true/strict. */
    589608        } else if (g_strcasecmp(cmd[0], "undo") == 0) {
    590                 guint64 id;
    591 
    592609                if (cmd[1] == NULL)
    593610                        twitter_status_destroy(ic, td->last_status_id);
    594                 else if (sscanf(cmd[1], "%" G_GINT64_MODIFIER "x", &id) == 1) {
    595                         if (id < TWITTER_LOG_LENGTH && td->log)
    596                                 id = td->log[id].id;
    597                        
     611                else if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL)))
    598612                        twitter_status_destroy(ic, id);
    599                 } else
     613                else
    600614                        twitter_log(ic, "Could not undo last action");
    601615
    602                 g_free(cmds);
    603                 return;
     616                goto eof;
    604617        } else if (g_strcasecmp(cmd[0], "favourite") == 0 && cmd[1]) {
    605                 guint64 id;
    606                 if ((id = twitter_message_id_from_command_arg(ic, td, cmd[1]))) {
     618                if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL))) {
    607619                        twitter_favourite_tweet(ic, id);
    608620                } else {
    609621                        twitter_log(ic, "Please provide a message ID or username.");
    610622                }
    611                 g_free(cmds);
    612                 return;
     623                goto eof;
    613624        } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) {
    614625                twitter_add_buddy(ic, cmd[1], NULL);
    615                 g_free(cmds);
    616                 return;
     626                goto eof;
    617627        } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) {
    618628                twitter_remove_buddy(ic, cmd[1], NULL);
    619                 g_free(cmds);
    620                 return;
     629                goto eof;
    621630        } else if ((g_strcasecmp(cmd[0], "report") == 0 ||
    622631                    g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) {
    623                 char * screen_name;
    624                 guint64 id;
    625                 screen_name = cmd[1];
     632                char *screen_name;
     633               
    626634                /* Report nominally works on users but look up the user who
    627635                   posted the given ID if the user wants to do it that way */
    628                 if (g_str_has_prefix(cmd[1], "#") &&
    629                     sscanf(cmd[1] + 1, "%" G_GINT64_MODIFIER "x", &id) == 1) {
    630                         if (id < TWITTER_LOG_LENGTH && td->log) {
    631                                 if (g_slist_find(ic->bee->users, td->log[id].bu)) {
    632                                         screen_name = td->log[id].bu->handle;
    633                                 }
    634                         }
    635                 }
     636                twitter_message_id_from_command_arg(ic, cmd[1], &bu);
     637                if (bu)
     638                        screen_name = bu->handle;
     639                else
     640                        screen_name = cmd[1];
     641               
    636642                twitter_report_spam(ic, screen_name);
    637                 g_free(cmds);
    638                 return;
     643                goto eof;
    639644        } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) {
    640                 guint64 id = twitter_message_id_from_command_arg(ic, td, cmd[1]);
     645                id = twitter_message_id_from_command_arg(ic, cmd[1], NULL);
    641646
    642647                td->last_status_id = 0;
     
    647652                                    "post any statuses recently", cmd[1]);
    648653
    649                 g_free(cmds);
    650                 return;
     654                goto eof;
    651655        } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) {
    652                 struct twitter_user_data *tud;
    653                 bee_user_t *bu = NULL;
    654                 guint64 id = 0;
    655 
    656                 if (g_str_has_prefix(cmd[1], "#") &&
    657                     sscanf(cmd[1] + 1, "%" G_GINT64_MODIFIER "x", &id) == 1 &&
    658                     (id < TWITTER_LOG_LENGTH) && td->log) {
    659                         bu = td->log[id].bu;
    660                         if (g_slist_find(ic->bee->users, bu))
    661                                 id = td->log[id].id;
    662                         else
    663                                 bu = NULL;
    664                 } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) &&
    665                     (tud = bu->data) && tud->last_id) {
    666                         id = tud->last_id;
    667                 } else if (sscanf(cmd[1], "%" G_GINT64_MODIFIER "x", &id) == 1 &&
    668                            (id < TWITTER_LOG_LENGTH) && td->log) {
    669                         bu = td->log[id].bu;
    670                         if (g_slist_find(ic->bee->users, bu))
    671                                 id = td->log[id].id;
    672                         else
    673                                 bu = NULL;
    674                 }
    675 
     656                id = twitter_message_id_from_command_arg(ic, cmd[1], &bu);
    676657                if (!id || !bu) {
    677658                        twitter_log(ic, "User `%s' does not exist or didn't "
    678659                                    "post any statuses recently", cmd[1]);
    679                         g_free(cmds);
    680                         return;
     660                        goto eof;
    681661                }
    682662                message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0]));
    683663                in_reply_to = id;
     664                allow_post = TRUE;
    684665        } else if (g_strcasecmp(cmd[0], "post") == 0) {
    685666                message += 5;
    686                 strict_commands = FALSE;
    687         }
    688 
    689         if (strict_commands) {
    690                 twitter_log(ic, "Unknown command: %s", cmd[0]);
    691         } else {
     667                allow_post = TRUE;
     668        }
     669
     670        if (allow_post) {
    692671                char *s;
    693                 bee_user_t *bu;
    694 
    695                 if (!twitter_length_check(ic, message)) {
    696                         g_free(new);
    697                         g_free(cmds);
    698                         return;
    699                 }
     672
     673                if (!twitter_length_check(ic, message))
     674                        goto eof;
    700675
    701676                s = cmd[0] + strlen(cmd[0]) - 1;
     
    720695                td->last_status_id = 0;
    721696                twitter_post_status(ic, message, in_reply_to);
    722                 g_free(new);
    723         }
     697        } else {
     698                twitter_log(ic, "Unknown command: %s", cmd[0]);
     699        }
     700eof:
     701        g_free(new);
    724702        g_free(cmds);
    725703}
Note: See TracChangeset for help on using the changeset viewer.