Merge branch 'master' into gregoa
authorgregor herrmann <gregoa@debian.org>
Fri, 17 Dec 2010 00:56:24 +0000 (01:56 +0100)
committergregor herrmann <gregoa@debian.org>
Fri, 17 Dec 2010 00:56:24 +0000 (01:56 +0100)
bti-shrink-urls
bti.c

index aa9eb57db7f94a16a8f2bd9035b9112c2dac5848..36a45de35843b97b118c16c2003825416a0939ff 100755 (executable)
@@ -16,6 +16,9 @@
 
 needs_escape=true
 
+shrink_host=2tu.us
+test -f ~/.bti && . ~/.bti
+
 while test -n "$1" ; do
         word="$1"
         shift
@@ -29,7 +32,7 @@ bti-shrink-urls - convert URLs to a shorter form using a web service
 
     $0 [--escaped] [<url>]
 
-Currently only http://2tu.us/ is supported.
+Currently supported: 2tu.us (default), bit.ly, j.mp.
 END
                 exit 0
                 ;;
@@ -69,10 +72,25 @@ function convert_url() {
                                         -e 's/]/%5D/g')
         fi
 
-        # http://2tu.us/
-        local submit="http://2tu.us/?save=y&url=$url"
+        case $shrink_host in
+        2tu.us)
+            local submit="http://2tu.us/?save=y&url=$url"
+            local res=$(wget -q -O - "$submit" | awk -F"'" '/Your tight URL is:/ { print $2 }')
+            ;;
+        bit.ly|j.mp)
+            if [ -z "$shrink_bitly_login" -o -z "$shrink_bitly_key" ]; then
+                echo "To use $shrink_host you must set 'shrink_bitly_login' and 'shrink_bitly_key' in ~/.bti" >&2
+                exit 1
+            fi
+            local submit="http://api.bit.ly/v3/shorten?format=txt&login=$shrink_bitly_login&apiKey=$shrink_bitly_key&domain=$shrink_host&longUrl=$url"
+            local res=$(wget -q -O - "$submit")
+            ;;
+        *)
+            echo "Shrinking with $shrink_host is not supported." >&2
+            exit 1
+            ;;
+        esac
 
-        local res=$(wget -q -O - "$submit" | awk -F"'" '/Your tight URL is:/ { print $2 }')
         if test "${res%%:*}" = 'http' -a "${#res}" -lt "$urllen" ; then
                 echo $res
                 return 0
diff --git a/bti.c b/bti.c
index 23de0e77c51ea8a49653b89890da244515c410ae..0229a9a75fdf7a09fa0bc86a5fb177aa72909ae2 100644 (file)
--- a/bti.c
+++ b/bti.c
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 2008-2010 Greg Kroah-Hartman <greg@kroah.com>
  * Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
- * Copyright (C) 2009 Amir Mohammad Saied <amirsaied@gmail.com>
+ * Copyright (C) 2009-2010 Amir Mohammad Saied <amirsaied@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -68,7 +68,8 @@ enum action {
        ACTION_REPLIES = 4,
        ACTION_PUBLIC  = 8,
        ACTION_GROUP   = 16,
-       ACTION_UNKNOWN = 32
+       ACTION_RETWEET = 32,
+       ACTION_UNKNOWN = 64
 };
 
 struct session {
@@ -89,6 +90,7 @@ struct session {
        char *hostname;
        char *configfile;
        char *replyto;
+       char *retweet;
        int bash;
        int background;
        int interactive;
@@ -126,6 +128,7 @@ static void display_help(void)
                "  --logfile logfile\n"
                "  --config configfile\n"
                "  --replyto ID\n"
+               "  --retweet ID\n"
                "  --shrink-urls\n"
                "  --page PAGENUMBER\n"
                "  --bash\n"
@@ -252,6 +255,7 @@ static void session_free(struct session *session)
 {
        if (!session)
                return;
+       free(session->retweet);
        free(session->replyto);
        free(session->password);
        free(session->account);
@@ -307,7 +311,7 @@ static const char identica_name[] = "identi.ca";
 static const char twitter_request_token_uri[]  = "http://twitter.com/oauth/request_token";
 static const char twitter_access_token_uri[]   = "http://twitter.com/oauth/access_token";
 static const char twitter_authorize_uri[]      = "http://twitter.com/oauth/authorize?oauth_token=";
-static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token";
+static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token?oauth_callback=oob";
 static const char identica_access_token_uri[]  = "http://identi.ca/api/oauth/access_token";
 static const char identica_authorize_uri[]     = "http://identi.ca/api/oauth/authorize?oauth_token=";
 
@@ -317,6 +321,7 @@ static const char public_uri[]   = "/public_timeline.xml";
 static const char friends_uri[]  = "/friends_timeline.xml";
 static const char mentions_uri[] = "/mentions.xml";
 static const char replies_uri[]  = "/replies.xml";
+static const char retweet_uri[]  = "/retweet/";
 static const char group_uri[]    = "/../statusnet/groups/timeline/";
 
 static CURL *curl_init(void)
@@ -490,7 +495,7 @@ static int request_access_token(struct session *session)
 {
        char *post_params = NULL;
        char *request_url = NULL;
-       char *reply    = NULL;
+       char *reply       = NULL;
        char *at_key      = NULL;
        char *at_secret   = NULL;
        char *verifier    = NULL;
@@ -700,8 +705,17 @@ static int send_request(struct session *session)
                switch (session->action) {
                case ACTION_UPDATE:
                        escaped_tweet = oauth_url_escape(session->tweet);
-                       sprintf(endpoint, "%s%s?status=%s", session->hosturl,
-                               update_uri, escaped_tweet);
+                       if (session->replyto) {
+                               sprintf(endpoint,
+                                       "%s%s?status=%s&in_reply_to_status_id=%s",
+                                       session->hosturl, update_uri,
+                                       escaped_tweet, session->replyto);
+                       } else {
+                               sprintf(endpoint, "%s%s?status=%s",
+                                       session->hosturl, update_uri,
+                                       escaped_tweet);
+                       }
+
                        is_post = 1;
                        break;
                case ACTION_USER:
@@ -726,6 +740,11 @@ static int send_request(struct session *session)
                        sprintf(endpoint, "%s%s?page=%d", session->hosturl,
                                friends_uri, session->page);
                        break;
+               case ACTION_RETWEET:
+                       sprintf(endpoint, "%s%s%s.xml", session->hosturl,
+                               retweet_uri, session->retweet);
+                       is_post = 1;
+                       break;
                default:
                        break;
                }
@@ -751,7 +770,8 @@ static int send_request(struct session *session)
                if (req_url)
                        free(req_url);
 
-               if (session->action != ACTION_UPDATE)
+               if ((session->action != ACTION_UPDATE) &&
+                               (session->action != ACTION_RETWEET))
                        parse_timeline(reply);
        }
        return 0;
@@ -774,6 +794,7 @@ static void parse_configfile(struct session *session)
        char *action = NULL;
        char *user = NULL;
        char *replyto = NULL;
+       char *retweet = NULL;
        int shrink_urls = 0;
 
        config_file = fopen(session->configfile, "r");
@@ -872,6 +893,11 @@ static void parse_configfile(struct session *session)
                        if (!strncasecmp(c, "true", 4) ||
                                        !strncasecmp(c, "yes", 3))
                                verbose = 1;
+               } else if (!strncasecmp(c,"retweet", 7) &&
+                               (c[7] == '=')) {
+                       c += 8;
+                       if (c[0] != '\0')
+                               retweet = strdup(c);
                }
        } while (!feof(config_file));
 
@@ -912,6 +938,8 @@ static void parse_configfile(struct session *session)
                session->logfile = logfile;
        if (replyto)
                session->replyto = replyto;
+       if (retweet)
+               session->retweet = retweet;
        if (action) {
                if (strcasecmp(action, "update") == 0)
                        session->action = ACTION_UPDATE;
@@ -1320,11 +1348,13 @@ int main(int argc, char *argv[], char *envp[])
                { "version", 0, NULL, 'v' },
                { "config", 1, NULL, 'c' },
                { "replyto", 1, NULL, 'r' },
+               { "retweet", 1, NULL, 'w' },
                { }
        };
        struct session *session;
        pid_t child;
        char *tweet;
+       char *retweet;
        static char password[80];
        int retval = 0;
        int option;
@@ -1393,6 +1423,10 @@ int main(int argc, char *argv[], char *envp[])
                        session->replyto = strdup(optarg);
                        dbg("in_reply_to_status_id = %s\n", session->replyto);
                        break;
+               case 'w':
+                       session->retweet = strdup(optarg);
+                       dbg("Retweet ID = %s\n", session->retweet);
+                       break;
                case 'p':
                        if (session->password)
                                free(session->password);
@@ -1418,6 +1452,8 @@ int main(int argc, char *argv[], char *envp[])
                                session->action = ACTION_PUBLIC;
                        else if (strcasecmp(optarg, "group") == 0)
                                session->action = ACTION_GROUP;
+                       else if (strcasecmp(optarg,"retweet") == 0)
+                               session->action = ACTION_RETWEET;
                        else
                                session->action = ACTION_UNKNOWN;
                        dbg("action = %d\n", session->action);
@@ -1553,6 +1589,21 @@ int main(int argc, char *argv[], char *envp[])
                session->group = session->readline(NULL);
        }
 
+       if (session->action == ACTION_RETWEET) {
+               fprintf(stdout, "Status ID to retweet: ");
+               retweet = get_string_from_stdin();
+
+               if (!retweet || strlen(retweet) == 0) {
+                       dbg("no retweet?\n");
+                       return -1;
+               }
+
+               session->retweet = zalloc(strlen(retweet) + 10);
+               sprintf(session->retweet,"%s", retweet);
+               free(retweet);
+               dbg("retweet ID = %s\n", session->retweet);
+       }
+
        if (session->action == ACTION_UPDATE) {
                if (session->background || !session->interactive)
                        tweet = get_string_from_stdin();