--- /dev/null
+'\" 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>\&.
* 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
#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>
" --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"
" --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);
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=";
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;
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)
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)
{
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;
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;
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;
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;
}
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);
} 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,
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;
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",
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;
}
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);
}
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;
}
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;
}
{ "retweet", 1, NULL, 'w' },
{ }
};
+ struct stat s;
struct session *session;
pid_t child;
char *tweet;
int retval = 0;
int option;
char *http_proxy;
- char *home;
- const char *config_file;
time_t t;
int page_nr;
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. */
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);
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
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;
}
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
exit:
session_readline_cleanup(session);
session_free(session);
- return retval;;
+ return retval;
}