diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2012-09-15 17:24:52 +0100 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2012-09-15 17:24:52 +0100 |
commit | b61c74c0e0941577b551cf54d8965893090ca282 (patch) | |
tree | 096d21c2b75370ffa4e5a5af503c5191d1cd338c | |
parent | b3d99e347e66e1559b86e7d3bf78ed8b087ec947 (diff) |
Merge Twitter favourite command from Flexo/#983. Leaving out the unfavourite
command for reasons given there.
At this point there are loads of command and stuff is getting a little messy
maybe.. :-/
-rw-r--r-- | doc/user-guide/commands.xml | 1 | ||||
-rw-r--r-- | protocols/twitter/twitter.c | 48 | ||||
-rw-r--r-- | protocols/twitter/twitter_lib.c | 12 | ||||
-rw-r--r-- | protocols/twitter/twitter_lib.h | 1 |
4 files changed, 47 insertions, 15 deletions
diff --git a/doc/user-guide/commands.xml b/doc/user-guide/commands.xml index 4a6b3266..7a4baa73 100644 --- a/doc/user-guide/commands.xml +++ b/doc/user-guide/commands.xml @@ -847,6 +847,7 @@ <varlistentry><term>report <screenname|#id></term><listitem><para>Report the given user (or the user who posted the tweet with the given ID) for sending spam. This will also block them.</para></listitem></varlistentry> <varlistentry><term>follow <screenname></term><listitem><para>Start following a person</para></listitem></varlistentry> <varlistentry><term>unfollow <screenname></term><listitem><para>Stop following a person</para></listitem></varlistentry> + <varlistentry><term>favourite <screenname|#id></term><listitem><para>Favo<emphasis>u</emphasis>rite the given user's most recent tweet, or the given tweet ID.</para></listitem></varlistentry> <varlistentry><term>post <message></term><listitem><para>Post a tweet</para></listitem></varlistentry> </variablelist> diff --git a/protocols/twitter/twitter.c b/protocols/twitter/twitter.c index c0811a71..93ef4ae2 100644 --- a/protocols/twitter/twitter.c +++ b/protocols/twitter/twitter.c @@ -489,6 +489,29 @@ static void twitter_buddy_data_free(struct bee_user *bu) g_free(bu->data); } +/** Convert the given bitlbee tweet ID, bitlbee username, or twitter tweet ID + * into a twitter tweet ID. + * + * Returns 0 if the user provides garbage. + */ +static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, struct twitter_data *td, char *arg) { + struct twitter_user_data *tud; + bee_user_t *bu; + guint64 id = 0; + if (g_str_has_prefix(arg, "#") && + sscanf(arg + 1, "%" G_GUINT64_FORMAT, &id) == 1) { + if (id < TWITTER_LOG_LENGTH && td->log) + id = td->log[id].id; + } else if ((bu = bee_user_by_handle(ic->bee, ic, arg)) && + (tud = bu->data) && tud->last_id) + id = tud->last_id; + else if (sscanf(arg, "%" G_GUINT64_FORMAT, &id) == 1){ + if (id < TWITTER_LOG_LENGTH && td->log) + id = td->log[id].id; + } + return id; +} + static void twitter_handle_command(struct im_connection *ic, char *message) { struct twitter_data *td = ic->proto_data; @@ -518,6 +541,15 @@ static void twitter_handle_command(struct im_connection *ic, char *message) g_free(cmds); return; + } else if (g_strcasecmp(cmd[0], "favourite") == 0 && cmd[1]) { + guint64 id; + if ((id = twitter_message_id_from_command_arg(ic, td, cmd[1]))) { + twitter_favourite_tweet(ic, id); + } else { + twitter_msg(ic, "Please provide a message ID or username."); + } + g_free(cmds); + return; } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) { twitter_add_buddy(ic, cmd[1], NULL); g_free(cmds); @@ -545,21 +577,7 @@ static void twitter_handle_command(struct im_connection *ic, char *message) g_free(cmds); return; } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) { - struct twitter_user_data *tud; - bee_user_t *bu; - guint64 id; - - if (g_str_has_prefix(cmd[1], "#") && - sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) { - if (id < TWITTER_LOG_LENGTH && td->log) - id = td->log[id].id; - } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && - (tud = bu->data) && tud->last_id) - id = tud->last_id; - else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1){ - if (id < TWITTER_LOG_LENGTH && td->log) - id = td->log[id].id; - } + guint64 id = twitter_message_id_from_command_arg(ic, td, cmd[1]); td->last_status_id = 0; if (id) diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index 01e8f538..f1483035 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -1082,3 +1082,15 @@ void twitter_report_spam(struct im_connection *ic, char *screen_name) twitter_http(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post, ic, 1, args, 2); } + +/** + * Favourite a tweet. + */ +void twitter_favourite_tweet(struct im_connection *ic, guint64 id) +{ + char *url; + url = g_strdup_printf("%s%llu%s", TWITTER_FAVORITE_CREATE_URL, + (unsigned long long) id, ".xml"); + twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0); + g_free(url); +} diff --git a/protocols/twitter/twitter_lib.h b/protocols/twitter/twitter_lib.h index 09b91e5b..2404e4eb 100644 --- a/protocols/twitter/twitter_lib.h +++ b/protocols/twitter/twitter_lib.h @@ -90,6 +90,7 @@ void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int void twitter_status_destroy(struct im_connection *ic, guint64 id); void twitter_status_retweet(struct im_connection *ic, guint64 id); void twitter_report_spam(struct im_connection *ic, char *screen_name); +void twitter_favourite_tweet(struct im_connection *ic, guint64 id); #endif //_TWITTER_LIB_H |