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); \
74 struct bti_curl_buffer {
80 static void display_help(void)
82 fprintf(stdout, "bti - send tweet to twitter\n");
83 fprintf(stdout, "Version: " BTI_VERSION "\n");
84 fprintf(stdout, "Usage:\n");
85 fprintf(stdout, " bti [options]\n");
86 fprintf(stdout, "options are:\n");
87 fprintf(stdout, " --account accountname\n");
88 fprintf(stdout, " --password password\n");
89 fprintf(stdout, " --proxy PROXY:PORT\n");
90 fprintf(stdout, " --host HOST\n");
91 fprintf(stdout, " --logfile logfile\n");
92 fprintf(stdout, " --bash\n");
93 fprintf(stdout, " --action action\n");
94 fprintf(stdout, " --user screenname\n");
95 fprintf(stdout, " --debug\n");
96 fprintf(stdout, " --version\n");
97 fprintf(stdout, " --help\n");
100 static void display_version(void)
102 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
105 static struct session *session_alloc(void)
107 struct session *session;
109 session = zalloc(sizeof(*session));
115 static void session_free(struct session *session)
119 free(session->password);
120 free(session->account);
121 free(session->tweet);
122 free(session->proxy);
124 free(session->homedir);
129 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
131 struct bti_curl_buffer *buffer;
133 buffer = zalloc(sizeof(*buffer));
137 /* start out with a data buffer of 1 byte to
138 * make the buffer fill logic simpler */
139 buffer->data = zalloc(1);
145 buffer->action = action;
149 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
157 static const char *twitter_update_url = "https://twitter.com/statuses/update.xml";
158 static const char *twitter_public_url = "http://twitter.com/statuses/public_timeline.xml";
159 static const char *twitter_friends_url = "https://twitter.com/statuses/friends_timeline.xml";
160 static const char *twitter_user_url = "http://twitter.com/statuses/user_timeline/";
162 static const char *identica_update_url = "http://identi.ca/api/statuses/update.xml";
163 static const char *identica_public_url = "http://identi.ca/api/statuses/public_timeline.xml";
164 static const char *identica_friends_url = "http://identi.ca/api/statuses/friends_timeline.xml";
165 static const char *identica_user_url = "http://identi.ca/api/statuses/user_timeline/";
167 static CURL *curl_init(void)
171 curl = curl_easy_init();
173 fprintf(stderr, "Can not init CURL!\n");
176 /* some ssl sanity checks on the connection we are making */
177 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
178 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
182 void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
184 xmlChar *text = NULL;
185 xmlChar *user = NULL;
188 current = current->xmlChildrenNode;
189 while (current != NULL) {
190 if (current->type == XML_ELEMENT_NODE) {
191 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
192 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
193 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
194 userinfo = current->xmlChildrenNode;
195 while (userinfo != NULL) {
196 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
199 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
201 userinfo = userinfo->next;
205 printf("[%s] %s\n", user, text);
212 current = current->next;
218 static void parse_timeline(char *document)
222 doc = xmlReadMemory(document, strlen(document), "timeline.xml", NULL, XML_PARSE_NOERROR);
227 current = xmlDocGetRootElement(doc);
228 if (current == NULL) {
229 fprintf(stderr, "empty document\n");
234 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
235 fprintf(stderr, "unexpected document type\n");
240 current = current->xmlChildrenNode;
241 while (current != NULL) {
242 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
243 parse_statuses(doc, current);
244 current = current->next;
251 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
253 struct bti_curl_buffer *curl_buf = userp;
254 size_t buffer_size = size * nmemb;
257 if ((!buffer) || (!buffer_size) || (!curl_buf))
260 /* add to the data we already have */
261 temp = zalloc(curl_buf->length + buffer_size + 1);
265 memcpy(temp, curl_buf->data, curl_buf->length);
266 free(curl_buf->data);
267 curl_buf->data = temp;
268 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
269 curl_buf->length += buffer_size;
270 if (curl_buf->action)
271 parse_timeline(curl_buf->data);
273 dbg("%s\n", curl_buf->data);
278 static int send_request(struct session *session)
280 char user_password[500];
282 /* is there usernames longer than 22 chars? */
284 struct bti_curl_buffer *curl_buf;
287 struct curl_httppost *formpost = NULL;
288 struct curl_httppost *lastptr = NULL;
289 struct curl_slist *slist = NULL;
294 curl_buf = bti_curl_buffer_alloc(session->action);
302 switch (session->action) {
304 snprintf(user_password, sizeof(user_password), "%s:%s",
305 session->account, session->password);
306 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
307 curl_formadd(&formpost, &lastptr,
308 CURLFORM_COPYNAME, "status",
309 CURLFORM_COPYCONTENTS, session->tweet,
312 curl_formadd(&formpost, &lastptr,
313 CURLFORM_COPYNAME, "source",
314 CURLFORM_COPYCONTENTS, "bti",
317 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
318 slist = curl_slist_append(slist, "Expect:");
319 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
320 switch (session->host) {
322 curl_easy_setopt(curl, CURLOPT_URL, twitter_update_url);
325 curl_easy_setopt(curl, CURLOPT_URL, identica_update_url);
328 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
332 snprintf(user_password, sizeof(user_password), "%s:%s",
333 session->account, session->password);
334 switch (session->host) {
336 curl_easy_setopt(curl, CURLOPT_URL, twitter_friends_url);
339 curl_easy_setopt(curl, CURLOPT_URL, identica_friends_url);
342 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
346 switch (session->host) {
348 sprintf(user_url, "%s%s.xml", twitter_user_url, session->user);
349 curl_easy_setopt(curl, CURLOPT_URL, user_url);
352 sprintf(user_url, "%s%s.xml", identica_user_url, session->user);
353 curl_easy_setopt(curl, CURLOPT_URL, user_url);
359 switch (session->host) {
361 curl_easy_setopt(curl, CURLOPT_URL, twitter_public_url);
364 curl_easy_setopt(curl, CURLOPT_URL, identica_public_url);
372 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
375 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
377 dbg("user_password = %s\n", user_password);
378 dbg("data = %s\n", data);
379 dbg("proxy = %s\n", session->proxy);
381 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
382 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
383 res = curl_easy_perform(curl);
384 if (res && !session->bash) {
385 fprintf(stderr, "error(%d) trying to perform operation\n", res);
389 curl_easy_cleanup(curl);
390 if (session->action == ACTION_UPDATE)
391 curl_formfree(formpost);
392 bti_curl_buffer_free(curl_buf);
396 static void parse_configfile(struct session *session)
401 char *account = NULL;
402 char *password = NULL;
405 char *logfile = NULL;
410 /* config file is ~/.bti */
411 file = alloca(strlen(session->homedir) + 7);
413 sprintf(file, "%s/.bti", session->homedir);
415 config_file = fopen(file, "r");
417 /* No error if file does not exist or is unreadable. */
418 if (config_file == NULL)
422 ssize_t n = getline(&line, &len, config_file);
425 if (line[n - 1] == '\n')
427 /* Parse file. Format is the usual value pairs:
430 # is a comment character
432 *strchrnul(line, '#') = '\0';
436 /* Ignore blank lines. */
440 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
444 } else if (!strncasecmp(c, "password", 8) &&
448 password = strdup(c);
449 } else if (!strncasecmp(c, "host", 4) &&
454 } else if (!strncasecmp(c, "proxy", 5) &&
459 } else if (!strncasecmp(c, "logfile", 7) &&
464 } else if (!strncasecmp(c, "action", 6) &&
469 } else if (!strncasecmp(c, "user", 4) &&
475 } while (!feof(config_file));
478 session->password = password;
480 session->account = account;
482 if (strcasecmp(host, "twitter") == 0)
483 session->host = HOST_TWITTER;
484 if (strcasecmp(host, "identica") == 0)
485 session->host = HOST_IDENTICA;
490 free(session->proxy);
491 session->proxy = proxy;
494 session->logfile = logfile;
496 if (strcasecmp(action, "update") == 0)
497 session->action = ACTION_UPDATE;
498 if (strcasecmp(action, "friends") == 0)
499 session->action = ACTION_FRIENDS;
500 if (strcasecmp(action, "user") == 0)
501 session->action = ACTION_USER;
502 if (strcasecmp(action, "public") == 0)
503 session->action = ACTION_PUBLIC;
507 session->user = user;
510 /* Free buffer and close file. */
515 static void log_session(struct session *session, int retval)
521 /* Only log something if we have a log file set */
522 if (!session->logfile)
525 filename = alloca(strlen(session->homedir) +
526 strlen(session->logfile) + 3);
528 sprintf(filename, "%s/%s", session->homedir, session->logfile);
530 log_file = fopen(filename, "a+");
531 if (log_file == NULL)
533 switch (session->host) {
545 if (session->action == ACTION_UPDATE) {
547 fprintf(log_file, "%s: host=%s tweet failed\n",
548 session->time, host);
550 fprintf(log_file, "%s: host=%s tweet=%s\n",
551 session->time, host, session->tweet);
552 } else if (session->action == ACTION_FRIENDS) {
553 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
554 session->time, host);
555 } else if (session->action == ACTION_PUBLIC) {
556 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
557 session->time, host);
563 int main(int argc, char *argv[], char *envp[])
565 static const struct option options[] = {
566 { "debug", 0, NULL, 'd' },
567 { "account", 1, NULL, 'a' },
568 { "password", 1, NULL, 'p' },
569 { "host", 1, NULL, 'H' },
570 { "proxy", 1, NULL, 'P' },
571 { "action", 1, NULL, 'A' },
572 { "user", 1, NULL, 'u' },
573 { "logfile", 1, NULL, 'L' },
574 { "help", 0, NULL, 'h' },
575 { "bash", 0, NULL, 'b' },
576 { "version", 0, NULL, 'v' },
579 struct session *session;
588 rl_bind_key('\t', rl_insert);
590 session = session_alloc();
592 fprintf(stderr, "no more memory...\n");
596 /* get the current time so that we can log it later */
598 session->time = strdup(ctime(&t));
599 session->time[strlen(session->time)-1] = 0x00;
601 session->homedir = strdup(getenv("HOME"));
603 curl_global_init(CURL_GLOBAL_ALL);
605 /* Set environment variables first, before reading command line options
606 * or config file values. */
607 http_proxy = getenv("http_proxy");
610 free(session->proxy);
611 session->proxy = strdup(http_proxy);
612 dbg("http_proxy = %s\n", session->proxy);
615 parse_configfile(session);
618 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:A:u:h",
627 if (session->account)
628 free(session->account);
629 session->account = strdup(optarg);
630 dbg("account = %s\n", session->account);
633 if (session->password)
634 free(session->password);
635 session->password = strdup(optarg);
636 dbg("password = %s\n", session->password);
640 free(session->proxy);
641 session->proxy = strdup(optarg);
642 dbg("proxy = %s\n", session->proxy);
645 if (strcasecmp(optarg, "update") == 0)
646 session->action = ACTION_UPDATE;
647 if (strcasecmp(optarg, "friends") == 0)
648 session->action = ACTION_FRIENDS;
649 if (strcasecmp(optarg, "user") == 0)
650 session->action = ACTION_USER;
651 if (strcasecmp(optarg, "public") == 0)
652 session->action = ACTION_PUBLIC;
653 dbg("action = %d\n", session->action);
658 session->user = strdup(optarg);
659 dbg("user = %s\n", session->user);
662 if (session->logfile)
663 free(session->logfile);
664 session->logfile = strdup(optarg);
665 dbg("logfile = %s\n", session->logfile);
668 if (strcasecmp(optarg, "twitter") == 0)
669 session->host = HOST_TWITTER;
670 if (strcasecmp(optarg, "identica") == 0)
671 session->host = HOST_IDENTICA;
672 dbg("host = %d\n", session->host);
689 if (!session->account) {
690 fprintf(stdout, "Enter twitter account: ");
691 session->account = readline(NULL);
694 if (!session->password) {
695 fprintf(stdout, "Enter twitter password: ");
696 session->password = readline(NULL);
699 if (session->action == ACTION_UPDATE) {
701 tweet = readline(NULL);
703 tweet = readline("tweet: ");
704 if (!tweet || strlen(tweet) == 0) {
709 session->tweet = zalloc(strlen(tweet) + 10);
711 sprintf(session->tweet, "$ %s", tweet);
713 sprintf(session->tweet, "%s", tweet);
716 dbg("tweet = %s\n", session->tweet);
720 session->user = session->account;
722 dbg("account = %s\n", session->account);
723 dbg("password = %s\n", session->password);
724 dbg("host = %d\n", session->host);
725 dbg("action = %d\n", session->action);
727 /* fork ourself so that the main shell can get on
728 * with it's life as we try to connect and handle everything
733 dbg("child is %d\n", child);
738 retval = send_request(session);
739 if (retval && !session->bash)
740 fprintf(stderr, "operation failed\n");
742 log_session(session, retval);
744 session_free(session);