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); \
72 struct bti_curl_buffer {
78 static void display_help(void)
80 fprintf(stdout, "bti - send tweet to twitter\n");
81 fprintf(stdout, "Version: " BTI_VERSION "\n");
82 fprintf(stdout, "Usage:\n");
83 fprintf(stdout, " bti [options]\n");
84 fprintf(stdout, "options are:\n");
85 fprintf(stdout, " --account accountname\n");
86 fprintf(stdout, " --password password\n");
87 fprintf(stdout, " --proxy PROXY:PORT\n");
88 fprintf(stdout, " --host HOST\n");
89 fprintf(stdout, " --logfile logfile\n");
90 fprintf(stdout, " --bash\n");
91 fprintf(stdout, " --action action\n");
92 fprintf(stdout, " --debug\n");
93 fprintf(stdout, " --version\n");
94 fprintf(stdout, " --help\n");
97 static void display_version(void)
99 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
102 static struct session *session_alloc(void)
104 struct session *session;
106 session = zalloc(sizeof(*session));
112 static void session_free(struct session *session)
116 free(session->password);
117 free(session->account);
118 free(session->tweet);
119 free(session->proxy);
121 free(session->homedir);
125 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
127 struct bti_curl_buffer *buffer;
129 buffer = zalloc(sizeof(*buffer));
133 /* start out with a data buffer of 1 byte to
134 * make the buffer fill logic simpler */
135 buffer->data = zalloc(1);
141 buffer->action = action;
145 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
153 static const char *twitter_update_url = "https://twitter.com/statuses/update.xml";
154 static const char *twitter_public_url = "http://twitter.com/statuses/public_timeline.xml";
155 static const char *twitter_friends_url = "https://twitter.com/statuses/friends_timeline.xml";
157 static const char *identica_update_url = "http://identi.ca/api/statuses/update.xml";
158 static const char *identica_public_url = "http://identi.ca/api/statuses/public_timeline.xml";
159 static const char *identica_friends_url = "http://identi.ca/api/statuses/friends_timeline.xml";
161 static CURL *curl_init(void)
165 curl = curl_easy_init();
167 fprintf(stderr, "Can not init CURL!\n");
170 /* some ssl sanity checks on the connection we are making */
171 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
172 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
176 void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
181 current = current->xmlChildrenNode;
182 while (current != NULL) {
183 if (current->type == XML_ELEMENT_NODE) {
184 if (!xmlStrcmp(current->name, (const xmlChar *)"text")) {
185 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
189 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
190 userinfo = current->xmlChildrenNode;
191 while (userinfo != NULL) {
192 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
193 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
194 printf(" [%s]\n", user);
198 userinfo = userinfo->next;
203 current = current->next;
209 static void parse_timeline(char *document)
213 doc = xmlReadMemory(document, strlen(document), "timeline.xml", NULL, XML_PARSE_NOERROR);
218 current = xmlDocGetRootElement(doc);
219 if (current == NULL) {
220 fprintf(stderr, "empty document\n");
225 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
226 fprintf(stderr, "unexpected document type\n");
231 current = current->xmlChildrenNode;
232 while (current != NULL) {
233 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
234 parse_statuses(doc, current);
235 current = current->next;
242 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
244 struct bti_curl_buffer *curl_buf = userp;
245 size_t buffer_size = size * nmemb;
248 if ((!buffer) || (!buffer_size) || (!curl_buf))
251 /* add to the data we already have */
252 temp = zalloc(curl_buf->length + buffer_size + 1);
256 memcpy(temp, curl_buf->data, curl_buf->length);
257 free(curl_buf->data);
258 curl_buf->data = temp;
259 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
260 curl_buf->length += buffer_size;
261 if ((curl_buf->action == ACTION_FRIENDS) ||
262 (curl_buf->action == ACTION_PUBLIC))
263 parse_timeline(curl_buf->data);
265 dbg("%s\n", curl_buf->data);
270 static int send_request(struct session *session)
272 char user_password[500];
274 struct bti_curl_buffer *curl_buf;
277 struct curl_httppost *formpost = NULL;
278 struct curl_httppost *lastptr = NULL;
279 struct curl_slist *slist = NULL;
284 curl_buf = bti_curl_buffer_alloc(session->action);
292 switch (session->action) {
294 snprintf(user_password, sizeof(user_password), "%s:%s",
295 session->account, session->password);
296 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
297 curl_formadd(&formpost, &lastptr,
298 CURLFORM_COPYNAME, "status",
299 CURLFORM_COPYCONTENTS, session->tweet,
302 curl_formadd(&formpost, &lastptr,
303 CURLFORM_COPYNAME, "source",
304 CURLFORM_COPYCONTENTS, "bti",
307 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
308 slist = curl_slist_append(slist, "Expect:");
309 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
310 switch (session->host) {
312 curl_easy_setopt(curl, CURLOPT_URL, twitter_update_url);
315 curl_easy_setopt(curl, CURLOPT_URL, identica_update_url);
318 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
322 snprintf(user_password, sizeof(user_password), "%s:%s",
323 session->account, session->password);
324 switch (session->host) {
326 curl_easy_setopt(curl, CURLOPT_URL, twitter_friends_url);
329 curl_easy_setopt(curl, CURLOPT_URL, identica_friends_url);
332 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
336 switch (session->host) {
338 curl_easy_setopt(curl, CURLOPT_URL, twitter_public_url);
341 curl_easy_setopt(curl, CURLOPT_URL, identica_public_url);
349 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
352 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
354 dbg("user_password = %s\n", user_password);
355 dbg("data = %s\n", data);
356 dbg("proxy = %s\n", session->proxy);
358 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
359 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
360 res = curl_easy_perform(curl);
361 if (res && !session->bash) {
362 fprintf(stderr, "error(%d) trying to perform operation\n", res);
366 curl_easy_cleanup(curl);
367 if (session->action == ACTION_UPDATE)
368 curl_formfree(formpost);
369 bti_curl_buffer_free(curl_buf);
373 static void parse_configfile(struct session *session)
378 char *account = NULL;
379 char *password = NULL;
382 char *logfile = NULL;
386 /* config file is ~/.bti */
387 file = alloca(strlen(session->homedir) + 7);
389 sprintf(file, "%s/.bti", session->homedir);
391 config_file = fopen(file, "r");
393 /* No error if file does not exist or is unreadable. */
394 if (config_file == NULL)
398 ssize_t n = getline(&line, &len, config_file);
401 if (line[n - 1] == '\n')
403 /* Parse file. Format is the usual value pairs:
406 # is a comment character
408 *strchrnul(line, '#') = '\0';
412 /* Ignore blank lines. */
416 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
420 } else if (!strncasecmp(c, "password", 8) &&
424 password = strdup(c);
425 } else if (!strncasecmp(c, "host", 4) &&
430 } else if (!strncasecmp(c, "proxy", 5) &&
435 } else if (!strncasecmp(c, "logfile", 7) &&
440 } else if (!strncasecmp(c, "action", 6) &&
446 } while (!feof(config_file));
449 session->password = password;
451 session->account = account;
453 if (strcasecmp(host, "twitter") == 0)
454 session->host = HOST_TWITTER;
455 if (strcasecmp(host, "identica") == 0)
456 session->host = HOST_IDENTICA;
461 free(session->proxy);
462 session->proxy = proxy;
465 session->logfile = logfile;
467 if (strcasecmp(action, "update") == 0)
468 session->action = ACTION_UPDATE;
469 if (strcasecmp(action, "friends") == 0)
470 session->action = ACTION_FRIENDS;
471 if (strcasecmp(action, "public") == 0)
472 session->action = ACTION_PUBLIC;
476 /* Free buffer and close file. */
481 static void log_session(struct session *session, int retval)
487 /* Only log something if we have a log file set */
488 if (!session->logfile)
491 filename = alloca(strlen(session->homedir) +
492 strlen(session->logfile) + 3);
494 sprintf(filename, "%s/%s", session->homedir, session->logfile);
496 log_file = fopen(filename, "a+");
497 if (log_file == NULL)
499 switch (session->host) {
511 if (session->action == ACTION_UPDATE) {
513 fprintf(log_file, "%s: host=%s tweet failed\n",
514 session->time, host);
516 fprintf(log_file, "%s: host=%s tweet=%s\n",
517 session->time, host, session->tweet);
518 } else if (session->action == ACTION_FRIENDS) {
519 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
520 session->time, host);
521 } else if (session->action == ACTION_PUBLIC) {
522 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
523 session->time, host);
529 int main(int argc, char *argv[], char *envp[])
531 static const struct option options[] = {
532 { "debug", 0, NULL, 'd' },
533 { "account", 1, NULL, 'a' },
534 { "password", 1, NULL, 'p' },
535 { "host", 1, NULL, 'H' },
536 { "proxy", 1, NULL, 'P' },
537 { "action", 1, NULL, 'A' },
538 { "logfile", 1, NULL, 'L' },
539 { "help", 0, NULL, 'h' },
540 { "bash", 0, NULL, 'b' },
541 { "version", 0, NULL, 'v' },
544 struct session *session;
553 rl_bind_key('\t', rl_insert);
555 session = session_alloc();
557 fprintf(stderr, "no more memory...\n");
561 /* get the current time so that we can log it later */
563 session->time = strdup(ctime(&t));
564 session->time[strlen(session->time)-1] = 0x00;
566 session->homedir = strdup(getenv("HOME"));
568 curl_global_init(CURL_GLOBAL_ALL);
570 /* Set environment variables first, before reading command line options
571 * or config file values. */
572 http_proxy = getenv("http_proxy");
575 free(session->proxy);
576 session->proxy = strdup(http_proxy);
577 dbg("http_proxy = %s\n", session->proxy);
580 parse_configfile(session);
583 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:A:h",
592 if (session->account)
593 free(session->account);
594 session->account = strdup(optarg);
595 dbg("account = %s\n", session->account);
598 if (session->password)
599 free(session->password);
600 session->password = strdup(optarg);
601 dbg("password = %s\n", session->password);
605 free(session->proxy);
606 session->proxy = strdup(optarg);
607 dbg("proxy = %s\n", session->proxy);
610 if (strcasecmp(optarg, "update") == 0)
611 session->action = ACTION_UPDATE;
612 if (strcasecmp(optarg, "friends") == 0)
613 session->action = ACTION_FRIENDS;
614 if (strcasecmp(optarg, "public") == 0)
615 session->action = ACTION_PUBLIC;
616 dbg("action = %d\n", session->action);
619 if (session->logfile)
620 free(session->logfile);
621 session->logfile = strdup(optarg);
622 dbg("logfile = %s\n", session->logfile);
625 if (strcasecmp(optarg, "twitter") == 0)
626 session->host = HOST_TWITTER;
627 if (strcasecmp(optarg, "identica") == 0)
628 session->host = HOST_IDENTICA;
629 dbg("host = %d\n", session->host);
646 if (!session->account) {
647 fprintf(stdout, "Enter twitter account: ");
648 session->account = readline(NULL);
651 if (!session->password) {
652 fprintf(stdout, "Enter twitter password: ");
653 session->password = readline(NULL);
656 if (session->action == ACTION_UPDATE) {
658 tweet = readline(NULL);
660 tweet = readline("tweet: ");
661 if (!tweet || strlen(tweet) == 0) {
666 session->tweet = zalloc(strlen(tweet) + 10);
668 sprintf(session->tweet, "$ %s", tweet);
670 sprintf(session->tweet, "%s", tweet);
673 dbg("tweet = %s\n", session->tweet);
676 dbg("account = %s\n", session->account);
677 dbg("password = %s\n", session->password);
678 dbg("host = %d\n", session->host);
679 dbg("action = %d\n", session->action);
681 /* fork ourself so that the main shell can get on
682 * with it's life as we try to connect and handle everything
687 dbg("child is %d\n", child);
692 retval = send_request(session);
693 if (retval && !session->bash)
694 fprintf(stderr, "operation failed\n");
696 log_session(session, retval);
698 session_free(session);