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); \
77 struct bti_curl_buffer {
83 static void display_help(void)
85 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n");
86 fprintf(stdout, "Version: " BTI_VERSION "\n");
87 fprintf(stdout, "Usage:\n");
88 fprintf(stdout, " bti [options]\n");
89 fprintf(stdout, "options are:\n");
90 fprintf(stdout, " --account accountname\n");
91 fprintf(stdout, " --password password\n");
92 fprintf(stdout, " --action action\n");
93 fprintf(stdout, " ('update', 'friends', 'public', 'replies' or 'user')\n");
94 fprintf(stdout, " --user screenname\n");
95 fprintf(stdout, " --proxy PROXY:PORT\n");
96 fprintf(stdout, " --host HOST\n");
97 fprintf(stdout, " --logfile logfile\n");
98 fprintf(stdout, " --bash\n");
99 fprintf(stdout, " --debug\n");
100 fprintf(stdout, " --version\n");
101 fprintf(stdout, " --help\n");
104 static void display_version(void)
106 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
109 static struct session *session_alloc(void)
111 struct session *session;
113 session = zalloc(sizeof(*session));
119 static void session_free(struct session *session)
123 free(session->password);
124 free(session->account);
125 free(session->tweet);
126 free(session->proxy);
128 free(session->homedir);
133 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
135 struct bti_curl_buffer *buffer;
137 buffer = zalloc(sizeof(*buffer));
141 /* start out with a data buffer of 1 byte to
142 * make the buffer fill logic simpler */
143 buffer->data = zalloc(1);
149 buffer->action = action;
153 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
161 static const char *twitter_user_url = "http://twitter.com/statuses/user_timeline/";
162 static const char *twitter_update_url = "https://twitter.com/statuses/update.xml";
163 static const char *twitter_public_url = "http://twitter.com/statuses/public_timeline.xml";
164 static const char *twitter_friends_url = "https://twitter.com/statuses/friends_timeline.xml";
165 static const char *twitter_replies_url = "http://twitter.com/statuses/replies.xml";
167 static const char *identica_user_url = "http://identi.ca/api/statuses/user_timeline/";
168 static const char *identica_update_url = "http://identi.ca/api/statuses/update.xml";
169 static const char *identica_public_url = "http://identi.ca/api/statuses/public_timeline.xml";
170 static const char *identica_friends_url = "http://identi.ca/api/statuses/friends_timeline.xml";
171 static const char *identica_replies_url = "http://identi.ca/api/statuses/replies.xml";
173 static CURL *curl_init(void)
177 curl = curl_easy_init();
179 fprintf(stderr, "Can not init CURL!\n");
182 /* some ssl sanity checks on the connection we are making */
183 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
184 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
188 void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
190 xmlChar *text = NULL;
191 xmlChar *user = NULL;
194 current = current->xmlChildrenNode;
195 while (current != NULL) {
196 if (current->type == XML_ELEMENT_NODE) {
197 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
198 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
199 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
200 userinfo = current->xmlChildrenNode;
201 while (userinfo != NULL) {
202 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
205 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
207 userinfo = userinfo->next;
211 printf("[%s] %s\n", user, text);
218 current = current->next;
224 static void parse_timeline(char *document)
228 doc = xmlReadMemory(document, strlen(document), "timeline.xml", NULL, XML_PARSE_NOERROR);
233 current = xmlDocGetRootElement(doc);
234 if (current == NULL) {
235 fprintf(stderr, "empty document\n");
240 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
241 fprintf(stderr, "unexpected document type\n");
246 current = current->xmlChildrenNode;
247 while (current != NULL) {
248 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
249 parse_statuses(doc, current);
250 current = current->next;
257 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
259 struct bti_curl_buffer *curl_buf = userp;
260 size_t buffer_size = size * nmemb;
263 if ((!buffer) || (!buffer_size) || (!curl_buf))
266 /* add to the data we already have */
267 temp = zalloc(curl_buf->length + buffer_size + 1);
271 memcpy(temp, curl_buf->data, curl_buf->length);
272 free(curl_buf->data);
273 curl_buf->data = temp;
274 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
275 curl_buf->length += buffer_size;
276 if (curl_buf->action)
277 parse_timeline(curl_buf->data);
279 dbg("%s\n", curl_buf->data);
284 static int send_request(struct session *session)
286 char user_password[500];
288 /* is there usernames longer than 22 chars? */
290 struct bti_curl_buffer *curl_buf;
293 struct curl_httppost *formpost = NULL;
294 struct curl_httppost *lastptr = NULL;
295 struct curl_slist *slist = NULL;
300 curl_buf = bti_curl_buffer_alloc(session->action);
308 switch (session->action) {
310 snprintf(user_password, sizeof(user_password), "%s:%s",
311 session->account, session->password);
312 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
313 curl_formadd(&formpost, &lastptr,
314 CURLFORM_COPYNAME, "status",
315 CURLFORM_COPYCONTENTS, session->tweet,
318 curl_formadd(&formpost, &lastptr,
319 CURLFORM_COPYNAME, "source",
320 CURLFORM_COPYCONTENTS, "bti",
323 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
324 slist = curl_slist_append(slist, "Expect:");
325 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
326 switch (session->host) {
328 curl_easy_setopt(curl, CURLOPT_URL, twitter_update_url);
331 curl_easy_setopt(curl, CURLOPT_URL, identica_update_url);
334 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
338 snprintf(user_password, sizeof(user_password), "%s:%s",
339 session->account, session->password);
340 switch (session->host) {
342 sprintf(user_url, "%s?page=%d", twitter_friends_url, session->page);
343 curl_easy_setopt(curl, CURLOPT_URL, user_url);
346 sprintf(user_url, "%s?page=%d", identica_friends_url, session->page);
347 curl_easy_setopt(curl, CURLOPT_URL, user_url);
350 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
354 switch (session->host) {
356 sprintf(user_url, "%s%s.xml?page=%d", twitter_user_url, session->user, session->page);
357 curl_easy_setopt(curl, CURLOPT_URL, user_url);
360 sprintf(user_url, "%s%s.xml?page=%d", identica_user_url, session->user, session->page);
361 curl_easy_setopt(curl, CURLOPT_URL, user_url);
367 snprintf(user_password, sizeof(user_password), "%s:%s",
368 session->account, session->password);
369 switch (session->host) {
371 sprintf(user_url, "%s?page=%d", twitter_replies_url, session->page);
372 curl_easy_setopt(curl, CURLOPT_URL, user_url);
375 sprintf(user_url, "%s?page=%d", identica_replies_url, session->page);
376 curl_easy_setopt(curl, CURLOPT_URL, user_url);
379 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
383 switch (session->host) {
385 sprintf(user_url, "%s?page=%d", twitter_public_url, session->page);
386 curl_easy_setopt(curl, CURLOPT_URL, user_url);
389 sprintf(user_url, "%s?page=%d", identica_public_url, session->page);
390 curl_easy_setopt(curl, CURLOPT_URL, user_url);
400 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
403 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
405 dbg("user_password = %s\n", user_password);
406 dbg("data = %s\n", data);
407 dbg("proxy = %s\n", session->proxy);
409 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
410 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
411 res = curl_easy_perform(curl);
412 if (res && !session->bash) {
413 fprintf(stderr, "error(%d) trying to perform operation\n", res);
417 curl_easy_cleanup(curl);
418 if (session->action == ACTION_UPDATE)
419 curl_formfree(formpost);
420 bti_curl_buffer_free(curl_buf);
424 static void parse_configfile(struct session *session)
429 char *account = NULL;
430 char *password = NULL;
433 char *logfile = NULL;
438 /* config file is ~/.bti */
439 file = alloca(strlen(session->homedir) + 7);
441 sprintf(file, "%s/.bti", session->homedir);
443 config_file = fopen(file, "r");
445 /* No error if file does not exist or is unreadable. */
446 if (config_file == NULL)
450 ssize_t n = getline(&line, &len, config_file);
453 if (line[n - 1] == '\n')
455 /* Parse file. Format is the usual value pairs:
458 # is a comment character
460 *strchrnul(line, '#') = '\0';
464 /* Ignore blank lines. */
468 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
472 } else if (!strncasecmp(c, "password", 8) &&
476 password = strdup(c);
477 } else if (!strncasecmp(c, "host", 4) &&
482 } else if (!strncasecmp(c, "proxy", 5) &&
487 } else if (!strncasecmp(c, "logfile", 7) &&
492 } else if (!strncasecmp(c, "action", 6) &&
497 } else if (!strncasecmp(c, "user", 4) &&
503 } while (!feof(config_file));
506 session->password = password;
508 session->account = account;
510 if (strcasecmp(host, "twitter") == 0)
511 session->host = HOST_TWITTER;
512 if (strcasecmp(host, "identica") == 0)
513 session->host = HOST_IDENTICA;
518 free(session->proxy);
519 session->proxy = proxy;
522 session->logfile = logfile;
524 if (strcasecmp(action, "update") == 0)
525 session->action = ACTION_UPDATE;
526 else if (strcasecmp(action, "friends") == 0)
527 session->action = ACTION_FRIENDS;
528 else if (strcasecmp(action, "user") == 0)
529 session->action = ACTION_USER;
530 else if (strcasecmp(action, "replies") == 0)
531 session->action = ACTION_REPLIES;
532 else if (strcasecmp(action, "public") == 0)
533 session->action = ACTION_PUBLIC;
535 session->action = ACTION_UNKNOWN;
539 session->user = user;
542 /* Free buffer and close file. */
547 static void log_session(struct session *session, int retval)
553 /* Only log something if we have a log file set */
554 if (!session->logfile)
557 filename = alloca(strlen(session->homedir) +
558 strlen(session->logfile) + 3);
560 sprintf(filename, "%s/%s", session->homedir, session->logfile);
562 log_file = fopen(filename, "a+");
563 if (log_file == NULL)
565 switch (session->host) {
577 switch (session->action) {
580 fprintf(log_file, "%s: host=%s tweet failed\n",
581 session->time, host);
583 fprintf(log_file, "%s: host=%s tweet=%s\n",
584 session->time, host, session->tweet);
587 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
588 session->time, host);
591 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
592 session->time, host, session->user);
595 fprintf(log_file, "%s: host=%s retrieving replies\n",
596 session->time, host);
599 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
600 session->time, host);
609 static char *get_string_from_stdin(void)
614 string = zalloc(1000);
618 if (!fgets(string, 999, stdin))
620 temp = strchr(string, '\n');
625 int main(int argc, char *argv[], char *envp[])
627 static const struct option options[] = {
628 { "debug", 0, NULL, 'd' },
629 { "account", 1, NULL, 'a' },
630 { "password", 1, NULL, 'p' },
631 { "host", 1, NULL, 'H' },
632 { "proxy", 1, NULL, 'P' },
633 { "action", 1, NULL, 'A' },
634 { "user", 1, NULL, 'u' },
635 { "logfile", 1, NULL, 'L' },
636 { "help", 0, NULL, 'h' },
637 { "bash", 0, NULL, 'b' },
638 { "page", 1, NULL, 'g' },
639 { "version", 0, NULL, 'v' },
642 struct session *session;
652 rl_bind_key('\t', rl_insert);
654 session = session_alloc();
656 fprintf(stderr, "no more memory...\n");
660 /* get the current time so that we can log it later */
662 session->time = strdup(ctime(&t));
663 session->time[strlen(session->time)-1] = 0x00;
665 session->homedir = strdup(getenv("HOME"));
667 curl_global_init(CURL_GLOBAL_ALL);
669 /* Set environment variables first, before reading command line options
670 * or config file values. */
671 http_proxy = getenv("http_proxy");
674 free(session->proxy);
675 session->proxy = strdup(http_proxy);
676 dbg("http_proxy = %s\n", session->proxy);
679 parse_configfile(session);
682 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:A:u:hg:",
691 if (session->account)
692 free(session->account);
693 session->account = strdup(optarg);
694 dbg("account = %s\n", session->account);
697 page_nr = atoi(optarg);
698 dbg("page = %d\n", page_nr);
699 session->page = page_nr;
702 if (session->password)
703 free(session->password);
704 session->password = strdup(optarg);
705 dbg("password = %s\n", session->password);
709 free(session->proxy);
710 session->proxy = strdup(optarg);
711 dbg("proxy = %s\n", session->proxy);
714 if (strcasecmp(optarg, "update") == 0)
715 session->action = ACTION_UPDATE;
716 else if (strcasecmp(optarg, "friends") == 0)
717 session->action = ACTION_FRIENDS;
718 else if (strcasecmp(optarg, "user") == 0)
719 session->action = ACTION_USER;
720 else if (strcasecmp(optarg, "replies") == 0)
721 session->action = ACTION_REPLIES;
722 else if (strcasecmp(optarg, "public") == 0)
723 session->action = ACTION_PUBLIC;
725 session->action = ACTION_UNKNOWN;
726 dbg("action = %d\n", session->action);
731 session->user = strdup(optarg);
732 dbg("user = %s\n", session->user);
735 if (session->logfile)
736 free(session->logfile);
737 session->logfile = strdup(optarg);
738 dbg("logfile = %s\n", session->logfile);
741 if (strcasecmp(optarg, "twitter") == 0)
742 session->host = HOST_TWITTER;
743 if (strcasecmp(optarg, "identica") == 0)
744 session->host = HOST_IDENTICA;
745 dbg("host = %d\n", session->host);
762 if (session->action == ACTION_UNKNOWN) {
763 fprintf(stderr, "Unknown action, valid actions are:\n");
764 fprintf(stderr, "'update', 'friends', 'public', 'replies' or 'user'.\n");
768 if (!session->account) {
769 fprintf(stdout, "Enter twitter account: ");
770 session->account = readline(NULL);
773 if (!session->password) {
774 fprintf(stdout, "Enter twitter password: ");
775 session->password = readline(NULL);
778 if (session->action == ACTION_UPDATE) {
780 tweet = get_string_from_stdin();
782 tweet = readline("tweet: ");
783 if (!tweet || strlen(tweet) == 0) {
788 session->tweet = zalloc(strlen(tweet) + 10);
790 sprintf(session->tweet, "$ %s", tweet);
792 sprintf(session->tweet, "%s", tweet);
795 dbg("tweet = %s\n", session->tweet);
799 session->user = strdup(session->account);
801 if (session->page == 0)
803 dbg("account = %s\n", session->account);
804 dbg("password = %s\n", session->password);
805 dbg("host = %d\n", session->host);
806 dbg("action = %d\n", session->action);
808 /* fork ourself so that the main shell can get on
809 * with it's life as we try to connect and handle everything
814 dbg("child is %d\n", child);
819 retval = send_request(session);
820 if (retval && !session->bash)
821 fprintf(stderr, "operation failed\n");
823 log_session(session, retval);
825 session_free(session);