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); \
75 struct bti_curl_buffer {
81 static void display_help(void)
83 fprintf(stdout, "bti - send tweet to twitter\n");
84 fprintf(stdout, "Version: " BTI_VERSION "\n");
85 fprintf(stdout, "Usage:\n");
86 fprintf(stdout, " bti [options]\n");
87 fprintf(stdout, "options are:\n");
88 fprintf(stdout, " --account accountname\n");
89 fprintf(stdout, " --password password\n");
90 fprintf(stdout, " --proxy PROXY:PORT\n");
91 fprintf(stdout, " --host HOST\n");
92 fprintf(stdout, " --logfile logfile\n");
93 fprintf(stdout, " --bash\n");
94 fprintf(stdout, " --action action\n");
95 fprintf(stdout, " --user screenname\n");
96 fprintf(stdout, " --debug\n");
97 fprintf(stdout, " --version\n");
98 fprintf(stdout, " --help\n");
101 static void display_version(void)
103 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
106 static struct session *session_alloc(void)
108 struct session *session;
110 session = zalloc(sizeof(*session));
116 static void session_free(struct session *session)
120 free(session->password);
121 free(session->account);
122 free(session->tweet);
123 free(session->proxy);
125 free(session->homedir);
130 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
132 struct bti_curl_buffer *buffer;
134 buffer = zalloc(sizeof(*buffer));
138 /* start out with a data buffer of 1 byte to
139 * make the buffer fill logic simpler */
140 buffer->data = zalloc(1);
146 buffer->action = action;
150 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
158 static const char *twitter_user_url = "http://twitter.com/statuses/user_timeline/";
159 static const char *twitter_update_url = "https://twitter.com/statuses/update.xml";
160 static const char *twitter_public_url = "http://twitter.com/statuses/public_timeline.xml";
161 static const char *twitter_friends_url = "https://twitter.com/statuses/friends_timeline.xml";
162 static const char *twitter_replies_url = "http://twitter.com/statuses/replies.xml";
164 static const char *identica_user_url = "http://identi.ca/api/statuses/user_timeline/";
165 static const char *identica_update_url = "http://identi.ca/api/statuses/update.xml";
166 static const char *identica_public_url = "http://identi.ca/api/statuses/public_timeline.xml";
167 static const char *identica_friends_url = "http://identi.ca/api/statuses/friends_timeline.xml";
168 static const char *identica_replies_url = "http://identi.ca/api/statuses/replies.xml";
170 static CURL *curl_init(void)
174 curl = curl_easy_init();
176 fprintf(stderr, "Can not init CURL!\n");
179 /* some ssl sanity checks on the connection we are making */
180 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
181 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
185 void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
187 xmlChar *text = NULL;
188 xmlChar *user = NULL;
191 current = current->xmlChildrenNode;
192 while (current != NULL) {
193 if (current->type == XML_ELEMENT_NODE) {
194 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
195 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
196 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
197 userinfo = current->xmlChildrenNode;
198 while (userinfo != NULL) {
199 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
202 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
204 userinfo = userinfo->next;
208 printf("[%s] %s\n", user, text);
215 current = current->next;
221 static void parse_timeline(char *document)
225 doc = xmlReadMemory(document, strlen(document), "timeline.xml", NULL, XML_PARSE_NOERROR);
230 current = xmlDocGetRootElement(doc);
231 if (current == NULL) {
232 fprintf(stderr, "empty document\n");
237 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
238 fprintf(stderr, "unexpected document type\n");
243 current = current->xmlChildrenNode;
244 while (current != NULL) {
245 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
246 parse_statuses(doc, current);
247 current = current->next;
254 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
256 struct bti_curl_buffer *curl_buf = userp;
257 size_t buffer_size = size * nmemb;
260 if ((!buffer) || (!buffer_size) || (!curl_buf))
263 /* add to the data we already have */
264 temp = zalloc(curl_buf->length + buffer_size + 1);
268 memcpy(temp, curl_buf->data, curl_buf->length);
269 free(curl_buf->data);
270 curl_buf->data = temp;
271 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
272 curl_buf->length += buffer_size;
273 if (curl_buf->action)
274 parse_timeline(curl_buf->data);
276 dbg("%s\n", curl_buf->data);
281 static int send_request(struct session *session)
283 char user_password[500];
285 /* is there usernames longer than 22 chars? */
287 struct bti_curl_buffer *curl_buf;
290 struct curl_httppost *formpost = NULL;
291 struct curl_httppost *lastptr = NULL;
292 struct curl_slist *slist = NULL;
297 curl_buf = bti_curl_buffer_alloc(session->action);
305 switch (session->action) {
307 snprintf(user_password, sizeof(user_password), "%s:%s",
308 session->account, session->password);
309 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
310 curl_formadd(&formpost, &lastptr,
311 CURLFORM_COPYNAME, "status",
312 CURLFORM_COPYCONTENTS, session->tweet,
315 curl_formadd(&formpost, &lastptr,
316 CURLFORM_COPYNAME, "source",
317 CURLFORM_COPYCONTENTS, "bti",
320 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
321 slist = curl_slist_append(slist, "Expect:");
322 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
323 switch (session->host) {
325 curl_easy_setopt(curl, CURLOPT_URL, twitter_update_url);
328 curl_easy_setopt(curl, CURLOPT_URL, identica_update_url);
331 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
335 snprintf(user_password, sizeof(user_password), "%s:%s",
336 session->account, session->password);
337 switch (session->host) {
339 curl_easy_setopt(curl, CURLOPT_URL, twitter_friends_url);
342 curl_easy_setopt(curl, CURLOPT_URL, identica_friends_url);
345 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
349 switch (session->host) {
351 sprintf(user_url, "%s%s.xml", twitter_user_url, session->user);
352 curl_easy_setopt(curl, CURLOPT_URL, user_url);
355 sprintf(user_url, "%s%s.xml", identica_user_url, session->user);
356 curl_easy_setopt(curl, CURLOPT_URL, user_url);
362 snprintf(user_password, sizeof(user_password), "%s:%s",
363 session->account, session->password);
364 switch (session->host) {
366 curl_easy_setopt(curl, CURLOPT_URL, twitter_replies_url);
369 curl_easy_setopt(curl, CURLOPT_URL, identica_replies_url);
372 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
376 switch (session->host) {
378 curl_easy_setopt(curl, CURLOPT_URL, twitter_public_url);
381 curl_easy_setopt(curl, CURLOPT_URL, identica_public_url);
389 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
392 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
394 dbg("user_password = %s\n", user_password);
395 dbg("data = %s\n", data);
396 dbg("proxy = %s\n", session->proxy);
398 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
399 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
400 res = curl_easy_perform(curl);
401 if (res && !session->bash) {
402 fprintf(stderr, "error(%d) trying to perform operation\n", res);
406 curl_easy_cleanup(curl);
407 if (session->action == ACTION_UPDATE)
408 curl_formfree(formpost);
409 bti_curl_buffer_free(curl_buf);
413 static void parse_configfile(struct session *session)
418 char *account = NULL;
419 char *password = NULL;
422 char *logfile = NULL;
427 /* config file is ~/.bti */
428 file = alloca(strlen(session->homedir) + 7);
430 sprintf(file, "%s/.bti", session->homedir);
432 config_file = fopen(file, "r");
434 /* No error if file does not exist or is unreadable. */
435 if (config_file == NULL)
439 ssize_t n = getline(&line, &len, config_file);
442 if (line[n - 1] == '\n')
444 /* Parse file. Format is the usual value pairs:
447 # is a comment character
449 *strchrnul(line, '#') = '\0';
453 /* Ignore blank lines. */
457 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
461 } else if (!strncasecmp(c, "password", 8) &&
465 password = strdup(c);
466 } else if (!strncasecmp(c, "host", 4) &&
471 } else if (!strncasecmp(c, "proxy", 5) &&
476 } else if (!strncasecmp(c, "logfile", 7) &&
481 } else if (!strncasecmp(c, "action", 6) &&
486 } else if (!strncasecmp(c, "user", 4) &&
492 } while (!feof(config_file));
495 session->password = password;
497 session->account = account;
499 if (strcasecmp(host, "twitter") == 0)
500 session->host = HOST_TWITTER;
501 if (strcasecmp(host, "identica") == 0)
502 session->host = HOST_IDENTICA;
507 free(session->proxy);
508 session->proxy = proxy;
511 session->logfile = logfile;
513 if (strcasecmp(action, "update") == 0)
514 session->action = ACTION_UPDATE;
515 if (strcasecmp(action, "friends") == 0)
516 session->action = ACTION_FRIENDS;
517 if (strcasecmp(action, "user") == 0)
518 session->action = ACTION_USER;
519 if (strcasecmp(action, "replies") == 0)
520 session->action = ACTION_REPLIES;
521 if (strcasecmp(action, "public") == 0)
522 session->action = ACTION_PUBLIC;
526 session->user = user;
529 /* Free buffer and close file. */
534 static void log_session(struct session *session, int retval)
540 /* Only log something if we have a log file set */
541 if (!session->logfile)
544 filename = alloca(strlen(session->homedir) +
545 strlen(session->logfile) + 3);
547 sprintf(filename, "%s/%s", session->homedir, session->logfile);
549 log_file = fopen(filename, "a+");
550 if (log_file == NULL)
552 switch (session->host) {
564 switch (session->action) {
567 fprintf(log_file, "%s: host=%s tweet failed\n",
568 session->time, host);
570 fprintf(log_file, "%s: host=%s tweet=%s\n",
571 session->time, host, session->tweet);
574 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
575 session->time, host);
578 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
579 session->time, host, session->user);
582 fprintf(log_file, "%s: host=%s retrieving replies\n",
583 session->time, host);
586 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
587 session->time, host);
594 int main(int argc, char *argv[], char *envp[])
596 static const struct option options[] = {
597 { "debug", 0, NULL, 'd' },
598 { "account", 1, NULL, 'a' },
599 { "password", 1, NULL, 'p' },
600 { "host", 1, NULL, 'H' },
601 { "proxy", 1, NULL, 'P' },
602 { "action", 1, NULL, 'A' },
603 { "user", 1, NULL, 'u' },
604 { "logfile", 1, NULL, 'L' },
605 { "help", 0, NULL, 'h' },
606 { "bash", 0, NULL, 'b' },
607 { "version", 0, NULL, 'v' },
610 struct session *session;
619 rl_bind_key('\t', rl_insert);
621 session = session_alloc();
623 fprintf(stderr, "no more memory...\n");
627 /* get the current time so that we can log it later */
629 session->time = strdup(ctime(&t));
630 session->time[strlen(session->time)-1] = 0x00;
632 session->homedir = strdup(getenv("HOME"));
634 curl_global_init(CURL_GLOBAL_ALL);
636 /* Set environment variables first, before reading command line options
637 * or config file values. */
638 http_proxy = getenv("http_proxy");
641 free(session->proxy);
642 session->proxy = strdup(http_proxy);
643 dbg("http_proxy = %s\n", session->proxy);
646 parse_configfile(session);
649 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:A:u:h",
658 if (session->account)
659 free(session->account);
660 session->account = strdup(optarg);
661 dbg("account = %s\n", session->account);
664 if (session->password)
665 free(session->password);
666 session->password = strdup(optarg);
667 dbg("password = %s\n", session->password);
671 free(session->proxy);
672 session->proxy = strdup(optarg);
673 dbg("proxy = %s\n", session->proxy);
676 if (strcasecmp(optarg, "update") == 0)
677 session->action = ACTION_UPDATE;
678 if (strcasecmp(optarg, "friends") == 0)
679 session->action = ACTION_FRIENDS;
680 if (strcasecmp(optarg, "user") == 0)
681 session->action = ACTION_USER;
682 if (strcasecmp(optarg, "replies") == 0)
683 session->action = ACTION_REPLIES;
684 if (strcasecmp(optarg, "public") == 0)
685 session->action = ACTION_PUBLIC;
686 dbg("action = %d\n", session->action);
691 session->user = strdup(optarg);
692 dbg("user = %s\n", session->user);
695 if (session->logfile)
696 free(session->logfile);
697 session->logfile = strdup(optarg);
698 dbg("logfile = %s\n", session->logfile);
701 if (strcasecmp(optarg, "twitter") == 0)
702 session->host = HOST_TWITTER;
703 if (strcasecmp(optarg, "identica") == 0)
704 session->host = HOST_IDENTICA;
705 dbg("host = %d\n", session->host);
722 if (!session->account) {
723 fprintf(stdout, "Enter twitter account: ");
724 session->account = readline(NULL);
727 if (!session->password) {
728 fprintf(stdout, "Enter twitter password: ");
729 session->password = readline(NULL);
732 if (session->action == ACTION_UPDATE) {
734 tweet = readline(NULL);
736 tweet = readline("tweet: ");
737 if (!tweet || strlen(tweet) == 0) {
742 session->tweet = zalloc(strlen(tweet) + 10);
744 sprintf(session->tweet, "$ %s", tweet);
746 sprintf(session->tweet, "%s", tweet);
749 dbg("tweet = %s\n", session->tweet);
753 session->user = session->account;
755 dbg("account = %s\n", session->account);
756 dbg("password = %s\n", session->password);
757 dbg("host = %d\n", session->host);
758 dbg("action = %d\n", session->action);
760 /* fork ourself so that the main shell can get on
761 * with it's life as we try to connect and handle everything
766 dbg("child is %d\n", child);
771 retval = send_request(session);
772 if (retval && !session->bash)
773 fprintf(stderr, "operation failed\n");
775 log_session(session, retval);
777 session_free(session);