2 * Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <curl/curl.h>
30 #include <readline/readline.h>
31 #include <libxml/xmlmemory.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34 #include "bti_version.h"
37 #define zalloc(size) calloc(size, 1)
39 #define dbg(format, arg...) \
42 printf("%s: " format , __func__ , ## arg); \
76 struct bti_curl_buffer {
82 static void display_help(void)
84 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n");
85 fprintf(stdout, "Version: " BTI_VERSION "\n");
86 fprintf(stdout, "Usage:\n");
87 fprintf(stdout, " bti [options]\n");
88 fprintf(stdout, "options are:\n");
89 fprintf(stdout, " --account accountname\n");
90 fprintf(stdout, " --password password\n");
91 fprintf(stdout, " --action action\n");
92 fprintf(stdout, " ('update', 'friends', 'public', 'replies' or 'user')\n");
93 fprintf(stdout, " --user screenname\n");
94 fprintf(stdout, " --proxy PROXY:PORT\n");
95 fprintf(stdout, " --host HOST\n");
96 fprintf(stdout, " --logfile logfile\n");
97 fprintf(stdout, " --bash\n");
98 fprintf(stdout, " --debug\n");
99 fprintf(stdout, " --version\n");
100 fprintf(stdout, " --help\n");
103 static void display_version(void)
105 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
108 static struct session *session_alloc(void)
110 struct session *session;
112 session = zalloc(sizeof(*session));
118 static void session_free(struct session *session)
122 free(session->password);
123 free(session->account);
124 free(session->tweet);
125 free(session->proxy);
127 free(session->homedir);
132 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
134 struct bti_curl_buffer *buffer;
136 buffer = zalloc(sizeof(*buffer));
140 /* start out with a data buffer of 1 byte to
141 * make the buffer fill logic simpler */
142 buffer->data = zalloc(1);
148 buffer->action = action;
152 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
160 static const char *twitter_user_url = "http://twitter.com/statuses/user_timeline/";
161 static const char *twitter_update_url = "https://twitter.com/statuses/update.xml";
162 static const char *twitter_public_url = "http://twitter.com/statuses/public_timeline.xml";
163 static const char *twitter_friends_url = "https://twitter.com/statuses/friends_timeline.xml";
164 static const char *twitter_replies_url = "http://twitter.com/statuses/replies.xml";
166 static const char *identica_user_url = "http://identi.ca/api/statuses/user_timeline/";
167 static const char *identica_update_url = "http://identi.ca/api/statuses/update.xml";
168 static const char *identica_public_url = "http://identi.ca/api/statuses/public_timeline.xml";
169 static const char *identica_friends_url = "http://identi.ca/api/statuses/friends_timeline.xml";
170 static const char *identica_replies_url = "http://identi.ca/api/statuses/replies.xml";
172 static CURL *curl_init(void)
176 curl = curl_easy_init();
178 fprintf(stderr, "Can not init CURL!\n");
181 /* some ssl sanity checks on the connection we are making */
182 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
183 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
187 void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
189 xmlChar *text = NULL;
190 xmlChar *user = NULL;
193 current = current->xmlChildrenNode;
194 while (current != NULL) {
195 if (current->type == XML_ELEMENT_NODE) {
196 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
197 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
198 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
199 userinfo = current->xmlChildrenNode;
200 while (userinfo != NULL) {
201 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
204 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
206 userinfo = userinfo->next;
210 printf("[%s] %s\n", user, text);
217 current = current->next;
223 static void parse_timeline(char *document)
227 doc = xmlReadMemory(document, strlen(document), "timeline.xml", NULL, XML_PARSE_NOERROR);
232 current = xmlDocGetRootElement(doc);
233 if (current == NULL) {
234 fprintf(stderr, "empty document\n");
239 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
240 fprintf(stderr, "unexpected document type\n");
245 current = current->xmlChildrenNode;
246 while (current != NULL) {
247 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
248 parse_statuses(doc, current);
249 current = current->next;
256 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
258 struct bti_curl_buffer *curl_buf = userp;
259 size_t buffer_size = size * nmemb;
262 if ((!buffer) || (!buffer_size) || (!curl_buf))
265 /* add to the data we already have */
266 temp = zalloc(curl_buf->length + buffer_size + 1);
270 memcpy(temp, curl_buf->data, curl_buf->length);
271 free(curl_buf->data);
272 curl_buf->data = temp;
273 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
274 curl_buf->length += buffer_size;
275 if (curl_buf->action)
276 parse_timeline(curl_buf->data);
278 dbg("%s\n", curl_buf->data);
283 static int send_request(struct session *session)
285 char user_password[500];
287 /* is there usernames longer than 22 chars? */
289 struct bti_curl_buffer *curl_buf;
292 struct curl_httppost *formpost = NULL;
293 struct curl_httppost *lastptr = NULL;
294 struct curl_slist *slist = NULL;
299 curl_buf = bti_curl_buffer_alloc(session->action);
307 switch (session->action) {
309 snprintf(user_password, sizeof(user_password), "%s:%s",
310 session->account, session->password);
311 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
312 curl_formadd(&formpost, &lastptr,
313 CURLFORM_COPYNAME, "status",
314 CURLFORM_COPYCONTENTS, session->tweet,
317 curl_formadd(&formpost, &lastptr,
318 CURLFORM_COPYNAME, "source",
319 CURLFORM_COPYCONTENTS, "bti",
322 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
323 slist = curl_slist_append(slist, "Expect:");
324 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
325 switch (session->host) {
327 curl_easy_setopt(curl, CURLOPT_URL, twitter_update_url);
330 curl_easy_setopt(curl, CURLOPT_URL, identica_update_url);
333 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
337 snprintf(user_password, sizeof(user_password), "%s:%s",
338 session->account, session->password);
339 switch (session->host) {
341 curl_easy_setopt(curl, CURLOPT_URL, twitter_friends_url);
344 curl_easy_setopt(curl, CURLOPT_URL, identica_friends_url);
347 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
351 switch (session->host) {
353 sprintf(user_url, "%s%s.xml", twitter_user_url, session->user);
354 curl_easy_setopt(curl, CURLOPT_URL, user_url);
357 sprintf(user_url, "%s%s.xml", identica_user_url, session->user);
358 curl_easy_setopt(curl, CURLOPT_URL, user_url);
364 snprintf(user_password, sizeof(user_password), "%s:%s",
365 session->account, session->password);
366 switch (session->host) {
368 curl_easy_setopt(curl, CURLOPT_URL, twitter_replies_url);
371 curl_easy_setopt(curl, CURLOPT_URL, identica_replies_url);
374 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
378 switch (session->host) {
380 curl_easy_setopt(curl, CURLOPT_URL, twitter_public_url);
383 curl_easy_setopt(curl, CURLOPT_URL, identica_public_url);
393 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
396 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
398 dbg("user_password = %s\n", user_password);
399 dbg("data = %s\n", data);
400 dbg("proxy = %s\n", session->proxy);
402 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
403 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
404 res = curl_easy_perform(curl);
405 if (res && !session->bash) {
406 fprintf(stderr, "error(%d) trying to perform operation\n", res);
410 curl_easy_cleanup(curl);
411 if (session->action == ACTION_UPDATE)
412 curl_formfree(formpost);
413 bti_curl_buffer_free(curl_buf);
417 static void parse_configfile(struct session *session)
422 char *account = NULL;
423 char *password = NULL;
426 char *logfile = NULL;
431 /* config file is ~/.bti */
432 file = alloca(strlen(session->homedir) + 7);
434 sprintf(file, "%s/.bti", session->homedir);
436 config_file = fopen(file, "r");
438 /* No error if file does not exist or is unreadable. */
439 if (config_file == NULL)
443 ssize_t n = getline(&line, &len, config_file);
446 if (line[n - 1] == '\n')
448 /* Parse file. Format is the usual value pairs:
451 # is a comment character
453 *strchrnul(line, '#') = '\0';
457 /* Ignore blank lines. */
461 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
465 } else if (!strncasecmp(c, "password", 8) &&
469 password = strdup(c);
470 } else if (!strncasecmp(c, "host", 4) &&
475 } else if (!strncasecmp(c, "proxy", 5) &&
480 } else if (!strncasecmp(c, "logfile", 7) &&
485 } else if (!strncasecmp(c, "action", 6) &&
490 } else if (!strncasecmp(c, "user", 4) &&
496 } while (!feof(config_file));
499 session->password = password;
501 session->account = account;
503 if (strcasecmp(host, "twitter") == 0)
504 session->host = HOST_TWITTER;
505 if (strcasecmp(host, "identica") == 0)
506 session->host = HOST_IDENTICA;
511 free(session->proxy);
512 session->proxy = proxy;
515 session->logfile = logfile;
517 if (strcasecmp(action, "update") == 0)
518 session->action = ACTION_UPDATE;
519 else if (strcasecmp(action, "friends") == 0)
520 session->action = ACTION_FRIENDS;
521 else if (strcasecmp(action, "user") == 0)
522 session->action = ACTION_USER;
523 else if (strcasecmp(action, "replies") == 0)
524 session->action = ACTION_REPLIES;
525 else if (strcasecmp(action, "public") == 0)
526 session->action = ACTION_PUBLIC;
528 session->action = ACTION_UNKNOWN;
532 session->user = user;
535 /* Free buffer and close file. */
540 static void log_session(struct session *session, int retval)
546 /* Only log something if we have a log file set */
547 if (!session->logfile)
550 filename = alloca(strlen(session->homedir) +
551 strlen(session->logfile) + 3);
553 sprintf(filename, "%s/%s", session->homedir, session->logfile);
555 log_file = fopen(filename, "a+");
556 if (log_file == NULL)
558 switch (session->host) {
570 switch (session->action) {
573 fprintf(log_file, "%s: host=%s tweet failed\n",
574 session->time, host);
576 fprintf(log_file, "%s: host=%s tweet=%s\n",
577 session->time, host, session->tweet);
580 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
581 session->time, host);
584 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
585 session->time, host, session->user);
588 fprintf(log_file, "%s: host=%s retrieving replies\n",
589 session->time, host);
592 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
593 session->time, host);
602 static char *get_string_from_stdin(void)
607 string = zalloc(1000);
611 if (!fgets(string, 999, stdin))
613 temp = strchr(string, '\n');
618 int main(int argc, char *argv[], char *envp[])
620 static const struct option options[] = {
621 { "debug", 0, NULL, 'd' },
622 { "account", 1, NULL, 'a' },
623 { "password", 1, NULL, 'p' },
624 { "host", 1, NULL, 'H' },
625 { "proxy", 1, NULL, 'P' },
626 { "action", 1, NULL, 'A' },
627 { "user", 1, NULL, 'u' },
628 { "logfile", 1, NULL, 'L' },
629 { "help", 0, NULL, 'h' },
630 { "bash", 0, NULL, 'b' },
631 { "version", 0, NULL, 'v' },
634 struct session *session;
643 rl_bind_key('\t', rl_insert);
645 session = session_alloc();
647 fprintf(stderr, "no more memory...\n");
651 /* get the current time so that we can log it later */
653 session->time = strdup(ctime(&t));
654 session->time[strlen(session->time)-1] = 0x00;
656 session->homedir = strdup(getenv("HOME"));
658 curl_global_init(CURL_GLOBAL_ALL);
660 /* Set environment variables first, before reading command line options
661 * or config file values. */
662 http_proxy = getenv("http_proxy");
665 free(session->proxy);
666 session->proxy = strdup(http_proxy);
667 dbg("http_proxy = %s\n", session->proxy);
670 parse_configfile(session);
673 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:A:u:h",
682 if (session->account)
683 free(session->account);
684 session->account = strdup(optarg);
685 dbg("account = %s\n", session->account);
688 if (session->password)
689 free(session->password);
690 session->password = strdup(optarg);
691 dbg("password = %s\n", session->password);
695 free(session->proxy);
696 session->proxy = strdup(optarg);
697 dbg("proxy = %s\n", session->proxy);
700 if (strcasecmp(optarg, "update") == 0)
701 session->action = ACTION_UPDATE;
702 else if (strcasecmp(optarg, "friends") == 0)
703 session->action = ACTION_FRIENDS;
704 else if (strcasecmp(optarg, "user") == 0)
705 session->action = ACTION_USER;
706 else if (strcasecmp(optarg, "replies") == 0)
707 session->action = ACTION_REPLIES;
708 else if (strcasecmp(optarg, "public") == 0)
709 session->action = ACTION_PUBLIC;
711 session->action = ACTION_UNKNOWN;
712 dbg("action = %d\n", session->action);
717 session->user = strdup(optarg);
718 dbg("user = %s\n", session->user);
721 if (session->logfile)
722 free(session->logfile);
723 session->logfile = strdup(optarg);
724 dbg("logfile = %s\n", session->logfile);
727 if (strcasecmp(optarg, "twitter") == 0)
728 session->host = HOST_TWITTER;
729 if (strcasecmp(optarg, "identica") == 0)
730 session->host = HOST_IDENTICA;
731 dbg("host = %d\n", session->host);
748 if (session->action == ACTION_UNKNOWN) {
749 fprintf(stderr, "Unknown action, valid actions are:\n");
750 fprintf(stderr, "'update', 'friends', 'public', 'replies' or 'user'.\n");
754 if (!session->account) {
755 fprintf(stdout, "Enter twitter account: ");
756 session->account = readline(NULL);
759 if (!session->password) {
760 fprintf(stdout, "Enter twitter password: ");
761 session->password = readline(NULL);
764 if (session->action == ACTION_UPDATE) {
766 tweet = get_string_from_stdin();
768 tweet = readline("tweet: ");
769 if (!tweet || strlen(tweet) == 0) {
774 session->tweet = zalloc(strlen(tweet) + 10);
776 sprintf(session->tweet, "$ %s", tweet);
778 sprintf(session->tweet, "%s", tweet);
781 dbg("tweet = %s\n", session->tweet);
785 session->user = strdup(session->account);
787 dbg("account = %s\n", session->account);
788 dbg("password = %s\n", session->password);
789 dbg("host = %d\n", session->host);
790 dbg("action = %d\n", session->action);
792 /* fork ourself so that the main shell can get on
793 * with it's life as we try to connect and handle everything
798 dbg("child is %d\n", child);
803 retval = send_request(session);
804 if (retval && !session->bash)
805 fprintf(stderr, "operation failed\n");
807 log_session(session, retval);
809 session_free(session);