Imported Upstream version 033 upstream/033
authorgregor herrmann <gregoa@debian.org>
Thu, 27 Jun 2013 22:36:17 +0000 (00:36 +0200)
committergregor herrmann <gregoa@debian.org>
Thu, 27 Jun 2013 22:36:17 +0000 (00:36 +0200)
ChangeLog
Makefile.am
RELEASE-NOTES
bti-bashcompletion
bti-shrink-urls
bti.1
bti.c
bti.h
bti.xml
config.c
configure.ac

index 3bb02dd..280888c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,40 @@
+Summary of changes from v032 to v033
+============================================
+
+Greg Kroah-Hartman (9):
+      Merge pull request #24 from pferor/master
+      Fix build issue
+      rework finding the config file.
+      Minor coding style cleanups.
+      Remove the FSF's address, as we don't care about that.
+      Error out if the command line option for a config file fails.
+      Merge pull request #23 from maneulyori/master
+      coding style cleanups
+      fix up coding style differences generated in the json parsing functions
+
+Paul Salanitri (3):
+      Add direct message capability.     Also adds rudimentry error checking on both updates and direct messages.
+      Update the man page to show direct message ability.
+      Add direct message action.
+
+Pferor (1):
+      Add XDG compilance (~/.config/bti) support
+
+maneulyori (3):
+      increased size of char endpoint to 2KByte. It will fix bti crashes while tweeting long unicode string.
+      use snprintf instead of sprintf. It will prevent BOF.
+      Stop tweeting when tweet length is over 140 character.
+
+paulrho (7):
+      Merge https://github.com/gregkh/bti
+      Better error checking     Use twitter to detect >140 chars
+      Typo fix
+      write status update and direct message with json url     read json error messages
+      include JSON library     guard against a pre 0.10 json library bug
+      another json pre10 issue to work around
+      Enable showing timeline: friends, user     (public does not work)
+
+
 Summary of changes from v031 to v032
 ============================================
 
index 904a058..400e6ea 100644 (file)
@@ -12,12 +12,14 @@ bti_SOURCES = \
 bti_CFLAGS = \
        $(LIBCURL_CFLAGS) \
        $(XML_CFLAGS) \
+       $(JSON_CFLAGS) \
        $(LIBPCRE_CFLAGS) \
        $(LIBOAUTH_CFLAGS)
 
 bti_LDADD = \
        $(LIBCURL_LIBS) \
        $(XML_LIBS) \
+       $(JSON_LIBS) \
        $(LIBPCRE_LIBS) \
        $(LIBOAUTH_LIBS)
 
index ec788b9..8e1ba09 100644 (file)
@@ -1,3 +1,10 @@
+bti 033
+=============
+- fixes due to Twitter turning off old XML interface thanks to Paul Salanitri
+- direct message capability thanks to Paul Salanitri
+- XDG compliance for configuration file
+- other minor bugfixes
+
 bti 032
 =============
 - oauth for custom StatusNet installations now works properly.
index e627ae2..fa55d75 100644 (file)
@@ -16,7 +16,7 @@ _bti()
        fi
 
        if [[ "${prev}" == "--action" ]] ; then
-               COMPREPLY=( $(compgen -W "friends public update user replies" -- ${cur} ) )
+               COMPREPLY=( $(compgen -W "friends public update user replies direct" -- ${cur} ) )
        fi
 
        return 0
index 36a45de..0729eba 100755 (executable)
@@ -17,7 +17,7 @@
 needs_escape=true
 
 shrink_host=2tu.us
-test -f ~/.bti && . ~/.bti
+test -f ~/.bti && source ~/.bti || test -f ~/.config/bti && source ~/.config/bti
 
 while test -n "$1" ; do
         word="$1"
diff --git a/bti.1 b/bti.1
index d68fdd2..b4b9198 100644 (file)
--- a/bti.1
+++ b/bti.1
@@ -49,12 +49,12 @@ Specify the password of your twitter\&.com or identi\&.ca account\&.
 .PP
 \fB\-\-action action\fR
 .RS 4
-Specify the action which you want to perform\&. Valid options are "update" to send a message, "friends" to see your friends timeline, "public" to track public timeline, "replies" to see replies to your messages and "user" to see a specific user\*(Aqs timeline\&. Default is "update"\&.
+Specify the action which you want to perform\&. Valid options are "update" to send a message, "friends" to see your friends timeline, "public" to track public timeline, "replies" to see replies to your messages, "user" to see a specific user\*(Aqs timeline and "direct" to send a direct message to a friend\&. Default is "update"\&.
 .RE
 .PP
 \fB\-\-user screenname\fR
 .RS 4
-Specify the user whose messages you want to see when the action is "user"\&.
+Specify the user whose messages you want to see when the action is "user", and the reciever of the direct message when the action is "direct" (the sender must be following the receiver)\&.
 .RE
 .PP
 \fB\-\-host HOST_NAME\fR
diff --git a/bti.c b/bti.c
index a31d582..3138e02 100644 (file)
--- a/bti.c
+++ b/bti.c
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 
 #define _GNU_SOURCE
@@ -36,6 +32,7 @@
 #include <libxml/xmlmemory.h>
 #include <libxml/parser.h>
 #include <libxml/tree.h>
+#include <json/json.h>
 #include <pcre.h>
 #include <termios.h>
 #include <dlfcn.h>
@@ -64,7 +61,7 @@ static void display_help(void)
                "  --account accountname\n"
                "  --password password\n"
                "  --action action\n"
-               "    ('update', 'friends', 'public', 'replies', or 'user')\n"
+               "    ('update', 'friends', 'public', 'replies', 'user', or 'direct')\n"
                "  --user screenname\n"
                "  --group groupname\n"
                "  --proxy PROXY:PORT\n"
@@ -85,6 +82,17 @@ static void display_help(void)
                "  --help\n", VERSION);
 }
 
+static int strlen_utf8(char *s)
+{
+       int i = 0, j = 0;
+       while (s[i]) {
+               if ((s[i] & 0xc0) != 0x80)
+                       j++;
+               i++;
+       }
+       return j;
+}
+
 static void display_version(void)
 {
        fprintf(stdout, "bti - version %s\n", VERSION);
@@ -250,14 +258,16 @@ static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
        free(buffer);
 }
 
-const char twitter_host[]  = "http://api.twitter.com/1/statuses";
+const char twitter_host[]  = "http://api.twitter.com/1.1/statuses";
+const char twitter_host_stream[]  = "https://stream.twitter.com/1.1/statuses"; /*this is not reset, and doesnt work */
+const char twitter_host_simple[]  = "http://api.twitter.com/1.1";
 const char identica_host[] = "https://identi.ca/api/statuses";
 const char twitter_name[]  = "twitter";
 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 twitter_request_token_uri[]  = "https://twitter.com/oauth/request_token";
+static const char twitter_access_token_uri[]   = "https://twitter.com/oauth/access_token";
+static const char twitter_authorize_uri[]      = "https://twitter.com/oauth/authorize?oauth_token=";
 static const char identica_request_token_uri[] = "https://identi.ca/api/oauth/request_token?oauth_callback=oob";
 static const char identica_access_token_uri[]  = "https://identi.ca/api/oauth/access_token";
 static const char identica_authorize_uri[]     = "https://identi.ca/api/oauth/authorize?oauth_token=";
@@ -265,18 +275,22 @@ static const char custom_request_token_uri[]   = "/../oauth/request_token?oauth_
 static const char custom_access_token_uri[]    = "/../oauth/access_token";
 static const char custom_authorize_uri[]       = "/../oauth/authorize?oauth_token=";
 
-static const char user_uri[]     = "/user_timeline/";
-static const char update_uri[]   = "/update.xml";
-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 user_uri[]     = "/user_timeline.json";
+static const char update_uri[]   = "/update.json";
+static const char public_uri[]   = "/sample.json";
+static const char friends_uri[]  = "/home_timeline.json";
+static const char mentions_uri[] = "/mentions_timeline.json";
 static const char replies_uri[]  = "/replies.xml";
 static const char retweet_uri[]  = "/retweet/";
 static const char group_uri[]    = "/../statusnet/groups/timeline/";
+/*static const char direct_uri[]   = "/direct_messages/new.xml";*/
+static const char direct_uri[]   = "/direct_messages/new.json";
 
 static const char config_default[]     = "/etc/bti";
+static const char config_xdg_default[] = ".config/bti";
 static const char config_user_default[]        = ".bti";
 
+
 static CURL *curl_init(void)
 {
        CURL *curl;
@@ -292,6 +306,58 @@ static CURL *curl_init(void)
        return curl;
 }
 
+static void find_config_file(struct session *session)
+{
+       struct stat s;
+       char *home;
+       char *file;
+       int homedir_size;
+
+       /*
+        * Get the home directory so we can try to find a config file.
+        * If we have no home dir set up, look in /etc/bti
+        */
+       home = getenv("HOME");
+       if (!home) {
+               /* No home dir, so take the defaults and get out of here */
+               session->homedir = strdup("");
+               session->configfile = strdup(config_default);
+               return;
+       }
+
+       /* We have a home dir, so this might be a user */
+       session->homedir = strdup(home);
+       homedir_size = strlen(session->homedir);
+
+       /*
+        * Try to find a config file, we do so in this order:
+        * ~/.bti               old-school config file
+        * ~/.config/bti        new-school config file
+        */
+       file = zalloc(homedir_size + strlen(config_user_default) + 7);
+       sprintf(file, "%s/%s", home, config_user_default);
+       if (stat(file, &s) == 0) {
+               /* Found the config file at ~/.bti */
+               session->configfile = strdup(file);
+               free(file);
+               return;
+       }
+
+       free(file);
+       file = zalloc(homedir_size + strlen(config_xdg_default) + 7);
+       sprintf(file, "%s/%s", home, config_xdg_default);
+       if (stat(file, &s) == 0) {
+               /* config file is at ~/.config/bti */
+               session->configfile = strdup(file);
+               free(file);
+               return;
+       }
+
+       /* No idea where the config file is, so punt */
+       free(file);
+       session->configfile = strdup("");
+}
+
 /* The final place data is sent to the screen/pty/tty */
 static void bti_output_line(struct session *session, xmlChar *user,
                            xmlChar *id, xmlChar *created, xmlChar *text)
@@ -386,6 +452,329 @@ static void parse_timeline(char *document, struct session *session)
        return;
 }
 
+
+/* avoids the c99 option */
+#define json_object_object_foreach_alt(obj,key,val)            \
+       char *key;                                              \
+       struct json_object *val;                                \
+       struct lh_entry *entry;                                 \
+       for (entry = json_object_get_object(obj)->head;         \
+               ({ if(entry && !is_error(entry)) {              \
+                       key = (char*)entry->k;                  \
+                       val = (struct json_object*)entry->v;    \
+               } ; entry; });                                  \
+               entry = entry->next )
+
+
+/* Forward Declaration */
+static void json_parse(json_object * jobj, int nestlevel);
+
+static void print_json_value(json_object *jobj, int nestlevel)
+{
+       enum json_type type;
+
+       type = json_object_get_type(jobj);
+       switch (type) {
+       case json_type_boolean:
+               printf("boolean   ");
+               printf("value: %s\n", json_object_get_boolean(jobj)? "true": "false");
+               break;
+       case json_type_double:
+               printf("double    ");
+               printf("value: %lf\n", json_object_get_double(jobj));
+               break;
+       case json_type_int:
+               printf("int       ");
+               printf("value: %d\n", json_object_get_int(jobj));
+               break;
+       case json_type_string:
+               printf("string    ");
+               printf("value: %s\n", json_object_get_string(jobj));
+               break;
+       default:
+               break;
+       }
+}
+
+#define MAXKEYSTACK 20
+char *keystack[MAXKEYSTACK];
+
+static void json_parse_array(json_object *jobj, char *key, int nestlevel)
+{
+       enum json_type type;
+
+       nestlevel++;
+       /* Simply get the array */
+       json_object *jarray = jobj;
+       if (key) {
+               /* Get the array if it is a key value pair */
+               jarray = json_object_object_get(jobj, key);
+       }
+
+       /* Get the length of the array */
+       int arraylen = json_object_array_length(jarray);
+       if (debug)
+               printf("Array Length: %d\n",arraylen);
+       int i;
+       json_object *jvalue;
+
+       for (i = 0; i < arraylen; i++) {
+               if (debug) {
+                       int j;
+                       for (j=0; j < nestlevel; ++j)
+                               printf("  ");
+                       printf("element[%d]\n",i);
+               }
+
+               /* Get the array element at position i */
+               jvalue = json_object_array_get_idx(jarray, i);
+               type = json_object_get_type(jvalue);
+               if (type == json_type_array) {
+                       json_parse_array(jvalue, NULL, nestlevel);
+               } else if (type != json_type_object) {
+                       if (debug) {
+                               printf("value[%d]: ", i);
+                               print_json_value(jvalue,nestlevel);
+                       }
+               } else {
+                       /* printf("obj: "); */
+                       keystack[nestlevel%MAXKEYSTACK]="[]";
+                       json_parse(jvalue,nestlevel);
+               }
+       }
+}
+
+
+struct results {
+       int code;
+       char *message;
+} results;
+
+struct session *store_session;
+struct tweetdetail {
+       char *id;
+       char *text;
+       char *screen_name;
+       char *created_at;
+} tweetdetail;
+
+static void json_interpret(json_object *jobj, int nestlevel)
+{
+       if (nestlevel == 3 &&
+           strcmp(keystack[1], "errors") == 0 &&
+           strcmp(keystack[2], "[]") == 0) {
+               if (strcmp(keystack[3], "message") == 0) {
+                       if (json_object_get_type(jobj) == json_type_string)
+                               results.message = (char *)json_object_get_string(jobj);
+               }
+               if (strcmp(keystack[3], "code") == 0) {
+                       if (json_object_get_type(jobj) == json_type_int)
+                               results.code = json_object_get_int(jobj);
+               }
+       }
+
+       if (nestlevel >= 2 &&
+           strcmp(keystack[1],"[]") == 0) {
+               if (strcmp(keystack[2], "created_at") == 0) {
+                       if (debug)
+                               printf("%s : %s\n", keystack[2], json_object_get_string(jobj));
+                       tweetdetail.created_at = (char *)json_object_get_string(jobj);
+               }
+               if (strcmp(keystack[2], "text") == 0) {
+                       if (debug)
+                               printf("%s : %s\n", keystack[2], json_object_get_string(jobj));
+                       tweetdetail.text = (char *)json_object_get_string(jobj);
+               }
+               if (strcmp(keystack[2], "id") == 0) {
+                       if (debug)
+                               printf("%s : %s\n", keystack[2], json_object_get_string(jobj));
+                       tweetdetail.id = (char *)json_object_get_string(jobj);
+               }
+               if (nestlevel >= 3 &&
+                   strcmp(keystack[2], "user") == 0) {
+                       if (strcmp(keystack[3], "screen_name") == 0) {
+                               if (debug)
+                                       printf("%s->%s : %s\n", keystack[2], keystack[3], json_object_get_string(jobj));
+                               tweetdetail.screen_name=(char *)json_object_get_string(jobj);
+                               bti_output_line(store_session,
+                                               (xmlChar *)tweetdetail.screen_name,
+                                               (xmlChar *)tweetdetail.id,
+                                               (xmlChar *)tweetdetail.created_at,
+                                               (xmlChar *)tweetdetail.text);
+                               }
+               }
+       }
+}
+
+/* Parsing the json object */
+static void json_parse(json_object * jobj, int nestlevel)
+{
+       int i;
+
+       if (jobj==NULL) {
+               fprintf(stderr,"jobj null\n");
+               return;
+       }
+       nestlevel++;
+       enum json_type type;
+       json_object_object_foreach_alt(jobj, key, val) {
+               /* work around pre10 */
+               if (val)
+                       type = json_object_get_type(val);
+               else
+                       type=json_type_null;
+               if (debug)
+                       for (i = 0; i < nestlevel; ++i)
+                               printf("  ");
+               if (debug)
+                       printf("key %-34s ", key);
+               if (debug)
+                       for (i = 0; i < 8 - nestlevel; ++i)
+                               printf("  ");
+               switch (type) {
+               case json_type_boolean:
+               case json_type_double:
+               case json_type_int:
+               case json_type_string:
+                       if (debug) print_json_value(val,nestlevel);
+                       if (debug) for (i=0; i<nestlevel+1; ++i) printf("  ");
+                       if (debug) printf("(");
+                       if (debug) for (i=1; i<nestlevel; ++i) { printf("%s->",keystack[i]); }
+                       if (debug) printf("%s)\n",key);
+                       keystack[nestlevel%MAXKEYSTACK] = key;
+                       json_interpret(val,nestlevel);
+                       break;
+               case json_type_object:
+                       if (debug) printf("json_type_object\n");
+                       keystack[nestlevel%MAXKEYSTACK] = key;
+                       json_parse(json_object_object_get(jobj, key), nestlevel);
+                       break;
+               case json_type_array:
+                       if (debug) printf("json_type_array, ");
+                       keystack[nestlevel%MAXKEYSTACK] = key;
+                       json_parse_array(jobj, key, nestlevel);
+                       break;
+               case json_type_null:
+                       if (debug) printf("null\n");
+                       break;
+               default:
+                       if (debug) printf("\n");
+                       break;
+               }
+       }
+}
+
+static int parse_response_json(char *document, struct session *session)
+{
+       dbg("Got this json response:\n");
+       dbg("%s\n",document);
+
+       results.code=0;
+       results.message=NULL;
+       json_object *jobj = json_tokener_parse(document);
+
+       /* make global for now */
+       store_session = session;
+       if (!is_error(jobj)) {
+               /* guards against a json pre 0.10 bug */
+               json_parse(jobj,0);
+       }
+       if (results.code && results.message != NULL) {
+               if (debug)
+                       printf("Got an error code:\n  code=%d\n  message=%s\n",
+                               results.code, results.message);
+               fprintf(stderr, "error condition detected: %d = %s\n",
+                       results.code, results.message);
+               return -EREMOTEIO;
+       }
+       return 0;
+}
+
+static void parse_timeline_json(char *document, struct session *session)
+{
+       dbg("Got this json response:\n");
+       dbg("%s\n",document);
+       results.code = 0;
+       results.message = NULL;
+       json_object *jobj = json_tokener_parse(document);
+
+       /* make global for now */
+       store_session = session;
+       if (!is_error(jobj)) {
+               /* guards against a json pre 0.10 bug */
+               if (json_object_get_type(jobj)==json_type_array) {
+                       json_parse_array(jobj, NULL, 0);
+               } else {
+                       json_parse(jobj,0);
+               }
+       }
+       if (results.code && results.message != NULL) {
+               if (debug)
+                       printf("Got an error code:\n  code=%d\n  message=%s\n",
+                               results.code, results.message);
+               fprintf(stderr, "error condition detected: %d = %s\n",
+                       results.code, results.message);
+       }
+}
+
+#ifdef OLDXML
+static int parse_response_xml(char *document, struct session *session)
+{
+       xmlDocPtr doc;
+       xmlNodePtr current;
+
+       doc = xmlReadMemory(document, strlen(document),
+                               "response.xml", NULL,
+                               XML_PARSE_NOERROR);
+       if (doc == NULL)
+               return -EREMOTEIO;
+
+       current = xmlDocGetRootElement(doc);
+       if (current == NULL) {
+               fprintf(stderr, "empty document\n");
+               xmlFreeDoc(doc);
+               return -EREMOTEIO;
+       }
+
+       if (xmlStrcmp(current->name, (const xmlChar *) "status")) {
+               if (xmlStrcmp(current->name, (const xmlChar *) "direct_message")) {
+                       if (xmlStrcmp(current->name, (const xmlChar *) "hash")
+                               && xmlStrcmp(current->name, (const xmlChar *) "errors")) {
+                               fprintf(stderr, "unexpected document type\n");
+                               xmlFreeDoc(doc);
+                               return -EREMOTEIO;
+                       } else {
+                               xmlChar *text=NULL;
+                               while (current != NULL) {
+                                       if (current->type == XML_ELEMENT_NODE)
+                                               if (!xmlStrcmp(current->name, (const xmlChar *)"error")) {
+                                                       text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
+                                                       break;
+                                               }
+                                       if (current->children)
+                                               current = current->children;
+                                       else
+                                               current = current->next;
+                               }
+
+                               if (text) {
+                                       fprintf(stderr, "error condition detected = %s\n", text);
+                                       xmlFree(text);
+                               } else
+                                       fprintf(stderr, "unknown error condition\n");
+
+                               xmlFreeDoc(doc);
+                               return -EREMOTEIO;
+                       }
+               }
+       }
+
+       xmlFreeDoc(doc);
+
+       return 0;
+}
+#endif
+
 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
                            void *userp)
 {
@@ -547,7 +936,8 @@ static int request_access_token(struct session *session)
 
 static int send_request(struct session *session)
 {
-       char endpoint[500];
+       const int endpoint_size = 2000;
+       char endpoint[endpoint_size];
        char user_password[500];
        char data[500];
        struct bti_curl_buffer *curl_buf;
@@ -611,7 +1001,7 @@ static int send_request(struct session *session)
                        slist = curl_slist_append(slist, "Expect:");
                        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
 
-                       sprintf(endpoint, "%s%s", session->hosturl, update_uri);
+                       snprintf(endpoint, endpoint_size, "%s%s", session->hosturl, update_uri);
                        curl_easy_setopt(curl, CURLOPT_URL, endpoint);
                        curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
                        break;
@@ -619,14 +1009,14 @@ static int send_request(struct session *session)
                case ACTION_FRIENDS:
                        snprintf(user_password, sizeof(user_password), "%s:%s",
                                 session->account, session->password);
-                       sprintf(endpoint, "%s%s?page=%d", session->hosturl,
+                       snprintf(endpoint, endpoint_size, "%s%s?page=%d", session->hosturl,
                                        friends_uri, session->page);
                        curl_easy_setopt(curl, CURLOPT_URL, endpoint);
                        curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
                        break;
 
                case ACTION_USER:
-                       sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
+                       snprintf(endpoint, endpoint_size, "%s%s%s.xml?page=%d", session->hosturl,
                                user_uri, session->user, session->page);
                        curl_easy_setopt(curl, CURLOPT_URL, endpoint);
                        break;
@@ -634,25 +1024,30 @@ static int send_request(struct session *session)
                case ACTION_REPLIES:
                        snprintf(user_password, sizeof(user_password), "%s:%s",
                                 session->account, session->password);
-                       sprintf(endpoint, "%s%s?page=%d", session->hosturl,
+                       snprintf(endpoint, endpoint_size, "%s%s?page=%d", session->hosturl,
                                replies_uri, session->page);
                        curl_easy_setopt(curl, CURLOPT_URL, endpoint);
                        curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
                        break;
 
                case ACTION_PUBLIC:
-                       sprintf(endpoint, "%s%s?page=%d", session->hosturl,
-                               public_uri, session->page);
+                       /*snprintf(endpoint, endpoint_size, "%s%s?page=%d", session->hosturl,*/
+                       snprintf(endpoint, endpoint_size, "%s%s", twitter_host_stream,
+                               public_uri);
                        curl_easy_setopt(curl, CURLOPT_URL, endpoint);
                        break;
 
                case ACTION_GROUP:
-                       sprintf(endpoint, "%s%s%s.xml?page=%d",
+                       snprintf(endpoint, endpoint_size, "%s%s%s.xml?page=%d",
                                session->hosturl, group_uri, session->group,
                                session->page);
                        curl_easy_setopt(curl, CURLOPT_URL, endpoint);
                        break;
 
+               case ACTION_DIRECT:
+                   /* NOT IMPLEMENTED - twitter requires authentication anyway */
+                       break;
+
                default:
                        break;
                }
@@ -676,8 +1071,9 @@ static int send_request(struct session *session)
                                xmlNodePtr current;
 
                                if (res) {
-                                       fprintf(stderr, "error(%d) trying to "
-                                               "perform operation\n", res);
+                                       fprintf(stderr,
+                                               "error(%d) trying to perform operation\n",
+                                               res);
                                        curl_easy_cleanup(curl);
                                        if (session->action == ACTION_UPDATE)
                                                curl_formfree(formpost);
@@ -729,6 +1125,13 @@ static int send_request(struct session *session)
        } else {
                switch (session->action) {
                case ACTION_UPDATE:
+                       /* dont test it here, let twitter return an error that we show */
+                       if (strlen_utf8(session->tweet) > 140 + 1000 ) {
+                               printf("E: tweet is too long!\n");
+                               goto skip_tweet;
+                       }
+
+                       /* TODO: add tweet crunching function. */
                        escaped_tweet = oauth_url_escape(session->tweet);
                        if (session->replyto) {
                                sprintf(endpoint,
@@ -744,7 +1147,7 @@ static int send_request(struct session *session)
                        is_post = 1;
                        break;
                case ACTION_USER:
-                       sprintf(endpoint, "%s%s%s.xml?page=%d",
+                       sprintf(endpoint, "%s%s?screen_name=%s&page=%d",
                                session->hosturl, user_uri, session->user,
                                session->page);
                        break;
@@ -753,8 +1156,8 @@ static int send_request(struct session *session)
                                mentions_uri, session->page);
                        break;
                case ACTION_PUBLIC:
-                       sprintf(endpoint, "%s%s?page=%d", session->hosturl,
-                               public_uri, session->page);
+                       sprintf(endpoint, "%s%s", twitter_host_stream,
+                               public_uri);
                        break;
                case ACTION_GROUP:
                        sprintf(endpoint, "%s%s%s.xml?page=%d",
@@ -770,6 +1173,12 @@ static int send_request(struct session *session)
                                retweet_uri, session->retweet);
                        is_post = 1;
                        break;
+               case ACTION_DIRECT:
+                       escaped_tweet = oauth_url_escape(session->tweet);
+                       sprintf(endpoint, "%s%s?user=%s&text=%s", twitter_host_simple,
+                               direct_uri, session->user, escaped_tweet);
+                       is_post = 1;
+                       break;
                default:
                        break;
                }
@@ -792,8 +1201,8 @@ static int send_request(struct session *session)
                                reply = oauth_http_get(req_url, postarg);
                        }
 
-                       dbg("%s\n", req_url);
-                       dbg("%s\n", reply);
+                       dbg("req_url:%s\n", req_url);
+                       dbg("reply:%s\n", reply);
                        if (req_url)
                                free(req_url);
                }
@@ -803,9 +1212,20 @@ static int send_request(struct session *session)
                        return -EIO;
                }
 
-               if ((session->action != ACTION_UPDATE) &&
-                               (session->action != ACTION_RETWEET))
-                       parse_timeline(reply, session);
+               if (!session->dry_run) {
+                       if ((session->action != ACTION_UPDATE) &&
+                                       (session->action != ACTION_RETWEET) &&
+                                       (session->action != ACTION_DIRECT))
+                               parse_timeline_json(reply, session);
+
+                       if ((session->action == ACTION_UPDATE) ||
+                                       (session->action == ACTION_DIRECT))
+                               /*return parse_response_xml(reply, session);*/
+                               return parse_response_json(reply, session);
+               }
+
+               skip_tweet: ;
+
        }
        return 0;
 }
@@ -858,6 +1278,15 @@ static void log_session(struct session *session, int retval)
                fprintf(log_file, "%s: host=%s retrieving group timeline\n",
                        session->time, session->hostname);
                break;
+       case ACTION_DIRECT:
+               if (retval)
+                       fprintf(log_file, "%s: host=%s tweet failed\n",
+                               session->time, session->hostname);
+               else
+                       fprintf(log_file, "%s: host=%s tweet=%s\n",
+                               session->time, session->hostname,
+                               session->tweet);
+               break;
        default:
                break;
        }
@@ -1203,6 +1632,7 @@ int main(int argc, char *argv[], char *envp[])
                { "retweet", 1, NULL, 'w' },
                { }
        };
+       struct stat s;
        struct session *session;
        pid_t child;
        char *tweet;
@@ -1210,8 +1640,6 @@ int main(int argc, char *argv[], char *envp[])
        int retval = 0;
        int option;
        char *http_proxy;
-       char *home;
-       const char *config_file;
        time_t t;
        int page_nr;
 
@@ -1228,23 +1656,7 @@ int main(int argc, char *argv[], char *envp[])
        session->time = strdup(ctime(&t));
        session->time[strlen(session->time)-1] = 0x00;
 
-       /*
-        * Get the home directory so we can try to find a config file.
-        * If we have no home dir set up, look in /etc/bti
-        */
-       home = getenv("HOME");
-       if (home) {
-               /* We have a home dir, so this might be a user */
-               session->homedir = strdup(home);
-               config_file = config_user_default;
-       } else {
-               session->homedir = strdup("");
-               config_file = config_default;
-       }
-
-       /* set up a default config file location (traditionally ~/.bti) */
-       session->configfile = zalloc(strlen(session->homedir) + strlen(config_file) + 7);
-       sprintf(session->configfile, "%s/%s", session->homedir, config_file);
+       find_config_file(session);
 
        /* Set environment variables first, before reading command line options
         * or config file values. */
@@ -1321,6 +1733,8 @@ int main(int argc, char *argv[], char *envp[])
                                session->action = ACTION_GROUP;
                        else if (strcasecmp(optarg, "retweet") == 0)
                                session->action = ACTION_RETWEET;
+                       else if (strcasecmp(optarg, "direct") == 0)
+                               session->action = ACTION_DIRECT;
                        else
                                session->action = ACTION_UNKNOWN;
                        dbg("action = %d\n", session->action);
@@ -1378,6 +1792,12 @@ int main(int argc, char *argv[], char *envp[])
                                free(session->configfile);
                        session->configfile = strdup(optarg);
                        dbg("configfile = %s\n", session->configfile);
+                       if (stat(session->configfile, &s) == -1) {
+                               fprintf(stderr,
+                                       "Config file '%s' is not found.\n",
+                                       session->configfile);
+                               goto exit;
+                       }
 
                        /*
                         * read the config file now.  Yes, this could override
@@ -1456,7 +1876,7 @@ int main(int argc, char *argv[], char *envp[])
 
        if (session->action == ACTION_UNKNOWN) {
                fprintf(stderr, "Unknown action, valid actions are:\n"
-                       "'update', 'friends', 'public', 'replies', 'group' or 'user'.\n");
+                       "'update', 'friends', 'public', 'replies', 'group', 'user' or 'direct'.\n");
                goto exit;
        }
 
@@ -1484,7 +1904,7 @@ int main(int argc, char *argv[], char *envp[])
                dbg("retweet ID = %s\n", session->retweet);
        }
 
-       if (session->action == ACTION_UPDATE) {
+       if (session->action == ACTION_UPDATE || session->action == ACTION_DIRECT) {
                if (session->background || !session->interactive)
                        tweet = get_string_from_stdin();
                else
@@ -1533,5 +1953,5 @@ int main(int argc, char *argv[], char *envp[])
 exit:
        session_readline_cleanup(session);
        session_free(session);
-       return retval;;
+       return retval;
 }
diff --git a/bti.h b/bti.h
index a8ee16f..62ab23f 100644 (file)
--- a/bti.h
+++ b/bti.h
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 
 #ifndef __BTI_H
@@ -34,7 +30,8 @@ enum action {
        ACTION_PUBLIC  = 8,
        ACTION_GROUP   = 16,
        ACTION_RETWEET = 32,
-       ACTION_UNKNOWN = 64
+       ACTION_DIRECT  = 64,
+       ACTION_UNKNOWN = 128
 };
 
 struct session {
diff --git a/bti.xml b/bti.xml
index 58d05ae..a2efd60 100644 (file)
--- a/bti.xml
+++ b/bti.xml
@@ -78,8 +78,9 @@
                Specify the action which you want to perform.  Valid options
                are "update" to send a message, "friends" to see your friends
                timeline, "public" to track public timeline, "replies" to see
-               replies to your messages and "user" to see a specific user's
-               timeline.  Default is "update".
+               replies to your messages, "user" to see a specific user's
+               timeline and "direct" to send a direct message to a friend.
+               Default is "update".
               </para>
             </listitem>
           </varlistentry>
@@ -88,7 +89,9 @@
             <listitem>
               <para>
                Specify the user whose messages you want to see when the
-               action is "user".
+               action is "user", and the reciever of the direct message when
+               the action is "direct" (the sender must be following the 
+               receiver).
               </para>
             </listitem>
           </varlistentry>
index 2b510b9..dd1b38f 100644 (file)
--- a/config.c
+++ b/config.c
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 
 #define _GNU_SOURCE
@@ -254,7 +250,7 @@ static int action_callback(struct session *session, char *value)
        else if (strcasecmp(value, "group") == 0)
                session->action = ACTION_GROUP;
        else
-               session->action= ACTION_UNKNOWN;
+               session->action = ACTION_UNKNOWN;
        return 0;
 }
 
@@ -311,7 +307,7 @@ static void process_line(struct session *session, char *key, char *value)
                if (strncasecmp(item->key, key, strlen(item->key)) == 0) {
                        /*
                         * printf("calling %p, for key = '%s' and value = * '%s'\n",
-                        *        item->callback, key, value);
+                        *        item->callback, key, value);
                         */
                        result = item->callback(session, value);
                        if (!result)
index 96cf93e..4923480 100644 (file)
@@ -1,7 +1,7 @@
-AC_INIT([bti], [032], [greg@kroah.com])
+AC_INIT([bti], [033], [greg@kroah.com])
 AC_PREREQ(2.60)
 
-AM_INIT_AUTOMAKE(bti, 032)
+AM_INIT_AUTOMAKE(bti, 033)
 
 m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
 
@@ -18,6 +18,7 @@ PKG_CHECK_MODULES(LIBOAUTH, oauth)
 PKG_CHECK_MODULES(LIBPCRE, libpcre)
 PKG_CHECK_MODULES([LIBCURL], [libcurl])
 PKG_CHECK_MODULES([XML], [libxml-2.0])
+PKG_CHECK_MODULES([JSON], [json])
 
 AC_SEARCH_LIBS([dlopen], [dl])