Merge tag 'upstream/033'
authorgregor herrmann <gregoa@debian.org>
Thu, 27 Jun 2013 22:37:17 +0000 (00:37 +0200)
committergregor herrmann <gregoa@debian.org>
Thu, 27 Jun 2013 22:37:17 +0000 (00:37 +0200)
Upstream version 033

Conflicts:
bti.1

ChangeLog
Makefile.am
RELEASE-NOTES
bti-bashcompletion
bti-shrink-urls
bti.1 [new file with mode: 0644]
bti.c
bti.h
bti.xml
config.c
configure.ac

index 3bb02dd96e80e666a106fa1fd5260551d42c2510..280888c41815142fb6d93140a79c0e2ccb45f6f1 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 904a0581009539333744399ea6f64c14a4665114..400e6ea9c390b815f8eca6702ca13984f151bb47 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 ec788b97f2fef8cceceb23dbb67003aee66a9cb0..8e1ba0987f2d6a02ae753bb329c92f11d7829792 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 e627ae21c19195aa04b563a196b797d0476a74e8..fa55d758cb3f343f1e0a6b80bd4872917da33aea 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 36a45de35843b97b118c16c2003825416a0939ff..0729eba0ed5508a3d57db1a565fe75077d5b02b2 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
new file mode 100644 (file)
index 0000000..b4b9198
--- /dev/null
+++ b/bti.1
@@ -0,0 +1,241 @@
+'\" t
+.\"     Title: bti
+.\"    Author: [see the "AUTHOR" section]
+.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
+.\"      Date: May 2008
+.\"    Manual: bti
+.\"    Source: bti
+.\"  Language: English
+.\"
+.TH "BTI" "1" "May 2008" "bti" "bti"
+.\" -----------------------------------------------------------------
+.\" * Define some portability stuff
+.\" -----------------------------------------------------------------
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.\" http://bugs.debian.org/507673
+.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.ie \n(.g .ds Aq \(aq
+.el       .ds Aq '
+.\" -----------------------------------------------------------------
+.\" * set default formatting
+.\" -----------------------------------------------------------------
+.\" disable hyphenation
+.nh
+.\" disable justification (adjust text to left margin only)
+.ad l
+.\" -----------------------------------------------------------------
+.\" * MAIN CONTENT STARTS HERE *
+.\" -----------------------------------------------------------------
+.SH "NAME"
+bti \- send a tweet to twitter\&.com or identi\&.ca from the command line
+.SH "SYNOPSIS"
+.HP \w'\fBbti\fR\ 'u
+\fBbti\fR [\fB\-\-account\ account\fR] [\fB\-\-password\ password\fR] [\fB\-\-action\ action\fR] [\fB\-\-user\ screenname\fR] [\fB\-\-host\ HOST_NAME\fR] [\fB\-\-proxy\ PROXY:PORT\fR] [\fB\-\-logfile\ LOGFILE\fR] [\fB\-\-config\ CONFIGFILE\fR] [\fB\-\-replyto\ ID\fR] [\fB\-\-retweet\ ID\fR] [\fB\-\-page\ PAGENUMBER\fR] [\fB\-\-bash\fR] [\fB\-\-shrink\-urls\fR] [\fB\-\-debug\fR] [\fB\-\-dry\-run\fR] [\fB\-\-verbose\fR] [\fB\-\-version\fR] [\fB\-\-help\fR]
+.SH "DESCRIPTION"
+.PP
+bti sends a tweet message to twitter\&.com or identi\&.ca\&.
+.SH "OPTIONS"
+.PP
+\fB\-\-account account\fR
+.RS 4
+Specify the twitter\&.com or identi\&.ca account name\&.
+.RE
+.PP
+\fB\-\-password password\fR
+.RS 4
+Specify the password of your twitter\&.com or identi\&.ca account\&.
+.RE
+.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, "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", 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
+.RS 4
+Specify the host which you want to send your message to\&. Valid options are "twitter" to send to twitter\&.com and "identica" to send to identi\&.ca\&. If you want to send the message to a custom StatusNet installation, you should specify the API URI\&. For example identi\&.ca\*(Aqs URI is: https://identi\&.ca/api/statuses
+.sp
+If no host is specified, the default is to send to twitter\&.com\&.
+.RE
+.PP
+\fB\-\-proxy PROXY:PORT\fR
+.RS 4
+Specify a http proxy value\&. This is not a required option, and only needed by systems that are behind a http proxy\&.
+.sp
+If \-\-proxy is not specified but the environment variable \*(Aqhttp_proxy\*(Aq is set the latter will be used\&.
+.RE
+.PP
+\fB\-\-logfile LOGFILE\fR
+.RS 4
+Specify a logfile for bti to write status messages to\&. LOGFILE is in relation to the user\*(Aqs home directory, not an absolute path to a file\&.
+.RE
+.PP
+\fB\-\-config CONFIGFILE\fR
+.RS 4
+Specify a config file for bti to read from\&. By default, bti looks in the ~/\&.bti file for config values\&. This default location can be overridden by setting a specific file with this option\&.
+.RE
+.PP
+\fB\-\-replyto ID\fR
+.RS 4
+Status ID of a single post to which you want to create a threaded reply to\&.
+.sp
+For twitter, this is ignored unless the message starts with the @name of the owner of the post with the status ID\&.
+.sp
+For status\&.net, this can link any two messages into context with each other\&. Status\&.net will also link a message that contains an @name without this without regard to context\&.
+.RE
+.PP
+\fB\-\-retweet ID\fR
+.RS 4
+Status ID of a single post which you want to retweet\&.
+.RE
+.PP
+\fB\-\-shrink\-urls\fR
+.RS 4
+Scans the tweet text for valid URL patterns and passes each through the supplied bti\-shrink\-urls script\&. The script will pass the URL to a web service that shrinks the URLs, making it more suitable for micro\-blogging\&.
+.sp
+The following URL shrinking services are available: http://2tu\&.us/ (default) and http://bit\&.ly / http://j\&.mp
+.sp
+See the documentation for bti\-shrink\-urls for the configuration options\&.
+.RE
+.PP
+\fB\-\-debug\fR
+.RS 4
+Print a whole bunch of debugging messages to stdout\&.
+.RE
+.PP
+\fB\-\-page PAGENUMBER\fR
+.RS 4
+When the action is to retrieve updates, it usually retrieves only one page\&. If this option is used, the page number can be specified\&.
+.RE
+.PP
+\fB\-\-dry\-run\fR
+.RS 4
+Performs all steps that would normally be done for a given action, but will not connect to the service to post or retrieve data\&.
+.RE
+.PP
+\fB\-\-verbose\fR
+.RS 4
+Verbose mode\&. Print status IDs and timestamps\&.
+.RE
+.PP
+\fB\-\-bash\fR
+.RS 4
+Add the working directory and a \*(Aq$\*(Aq in the tweet message to help specify it is coming from a command line\&. Don\*(Aqt put the working directory and the \*(Aq$\*(Aq in the tweet message\&.
+.sp
+This option implies
+\fB\-\-background\fR\&.
+.RE
+.PP
+\fB\-\-background\fR
+.RS 4
+Do not report back any errors that might have happened when sending the message, and send it in the background, returning immediately, allowing the user to continue on\&.
+.RE
+.PP
+\fB\-\-version\fR
+.RS 4
+Print version number\&.
+.RE
+.PP
+\fB\-\-help\fR
+.RS 4
+Print help text\&.
+.RE
+.SH "DESCRIPTION"
+.PP
+bti provides an easy way to send tweet messages direct from the command line or any script\&. It reads the message on standard input and uses the account and password settings either from the command line options, or from a config file, to send the message out\&.
+.PP
+Its primary focus is to allow you to log everything that you type into a bash shell, in a crazy, "this is what I\*(Aqm doing right now!" type of way, letting the world follow along with you constant moving between directories and refreshing your email queue to see if there\*(Aqs anything interesting going on\&.
+.PP
+To hook bti up to your bash shell, export the following variable:
+.PP
+
+PROMPT_COMMAND=\*(Aqhistory 1 | sed \-e "s/^\es*[0\-9]*\es*//" | bti \-\-bash\*(Aq
+.PP
+This example assumes that you have the
+~/\&.bti
+set up with your account and password information already in it, otherwise you can specify them as an option\&.
+.SH "CONFIGURATION"
+.PP
+The account and password can be stored in a configuration file in the users home directory in a file named
+\&.bti\&. The structure of this file is as follows:
+.PP
+\fBaccount\fR
+.RS 4
+The twitter\&.com or identi\&.ca account name you wish to use to send this message with\&.
+.RE
+.PP
+\fBpassword\fR
+.RS 4
+The twitter\&.com or identi\&.ca password for the account you wish to use to send this message with\&.
+.RE
+.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\&.
+.RE
+.PP
+\fB\-\-user screenname\fR
+.RS 4
+Specify the user you want to see his/her messages while the action is "user"\&.
+.RE
+.PP
+\fBhost\fR
+.RS 4
+The host you want to use to send the message to\&. Valid options are either "twitter" or "identica" to send to twitter\&.com or identi\&.ca respectively\&. If you want to send the message to a custom StatusNet installation, you should specify the API URI\&. For example identi\&.ca\*(Aqs URI is: https://identi\&.ca/api/statuses\&.
+.RE
+.PP
+\fBproxy\fR
+.RS 4
+The http proxy needed to send data out to the Internet\&.
+.RE
+.PP
+\fBlogfile\fR
+.RS 4
+The logfile name for bti to write what happened to\&. This file is relative to the user\*(Aqs home directory\&. If this file is not specified here or on the command line, no logging will be written to the disk\&.
+.RE
+.PP
+\fBreplyto\fR
+.RS 4
+The status ID to which all notices will be linked to\&.
+.sp
+There is no sane reason for a need to have this set in a config file\&. One such reason is to have all your messages as children to a particular status\&.
+.RE
+.PP
+\fBshrink\-urls\fR
+.RS 4
+Setting this variable to \*(Aqtrue\*(Aq or \*(Aqyes\*(Aq will enable the URL shrinking feature\&. This is equivalent to using the \-\-shrink\-urls option\&.
+.RE
+.PP
+\fBverbose\fR
+.RS 4
+Setting this variable to \*(Aqtrue\*(Aq or \*(Aqyes\*(Aq will enable the verbose mode\&.
+.RE
+.PP
+There is an example config file called
+bti\&.example
+in the source tree that shows the structure of the file if you need an example to work off of\&.
+.PP
+Configuration options have the following priority:
+.PP
+.RS 4
+command line option
+.RE
+.PP
+.RS 4
+config file option
+.RE
+.PP
+.RS 4
+environment variables
+.RE
+.PP
+For example, command line options always override any config file option, or any environment variables\&. Unless a config file is specified by the command line\&. At that point, the new config file is read, and any previous options set by a command line option, would be overridden\&.
+.SH "AUTHOR"
+.PP
+Written by Greg Kroah\-Hartman <greg@kroah\&.com> and Amir Mohammad Saied <amirsaied@gmail\&.com>\&.
diff --git a/bti.c b/bti.c
index a31d582b0a468a7ebddcab2dc9b3da45afb40751..3138e02d8777894a2d16d571f5b38868a2eee40a 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 a8ee16fff3647efe5ba526468b6ffa289710e356..62ab23f9e9ef3b26738ba8515e18c9a7cc664bb3 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 7bc1e855662ac6e02cc52ee852d2869dd79a8a68..05b8181a3950a2cf082ce6634400d9a65a7d5fa0 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 2b510b90276d8a31bfa9c9ccbfaa23c12bfb65f0..dd1b38fd5cf9c455d48e045709befe65bd6f5dcf 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 96cf93e74f7448d4e4db2a4cb8e6cd2425f622b3..49234805761b996312ca80d99373ad500726e015 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])