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)
178 xmlChar *text = NULL;
179 xmlChar *user = NULL;
182 current = current->xmlChildrenNode;
183 while (current != NULL) {
184 if (current->type == XML_ELEMENT_NODE) {
185 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
186 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
187 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
188 userinfo = current->xmlChildrenNode;
189 while (userinfo != NULL) {
190 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
193 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
195 userinfo = userinfo->next;
199 printf("[%s] %s\n", user, text);
206 current = current->next;
212 static void parse_timeline(char *document)
216 doc = xmlReadMemory(document, strlen(document), "timeline.xml", NULL, XML_PARSE_NOERROR);
221 current = xmlDocGetRootElement(doc);
222 if (current == NULL) {
223 fprintf(stderr, "empty document\n");
228 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
229 fprintf(stderr, "unexpected document type\n");
234 current = current->xmlChildrenNode;
235 while (current != NULL) {
236 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
237 parse_statuses(doc, current);
238 current = current->next;
245 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
247 struct bti_curl_buffer *curl_buf = userp;
248 size_t buffer_size = size * nmemb;
251 if ((!buffer) || (!buffer_size) || (!curl_buf))
254 /* add to the data we already have */
255 temp = zalloc(curl_buf->length + buffer_size + 1);
259 memcpy(temp, curl_buf->data, curl_buf->length);
260 free(curl_buf->data);
261 curl_buf->data = temp;
262 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
263 curl_buf->length += buffer_size;
264 if ((curl_buf->action == ACTION_FRIENDS) ||
265 (curl_buf->action == ACTION_PUBLIC))
266 parse_timeline(curl_buf->data);
268 dbg("%s\n", curl_buf->data);
273 static int send_request(struct session *session)
275 char user_password[500];
277 struct bti_curl_buffer *curl_buf;
280 struct curl_httppost *formpost = NULL;
281 struct curl_httppost *lastptr = NULL;
282 struct curl_slist *slist = NULL;
287 curl_buf = bti_curl_buffer_alloc(session->action);
295 switch (session->action) {
297 snprintf(user_password, sizeof(user_password), "%s:%s",
298 session->account, session->password);
299 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
300 curl_formadd(&formpost, &lastptr,
301 CURLFORM_COPYNAME, "status",
302 CURLFORM_COPYCONTENTS, session->tweet,
305 curl_formadd(&formpost, &lastptr,
306 CURLFORM_COPYNAME, "source",
307 CURLFORM_COPYCONTENTS, "bti",
310 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
311 slist = curl_slist_append(slist, "Expect:");
312 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
313 switch (session->host) {
315 curl_easy_setopt(curl, CURLOPT_URL, twitter_update_url);
318 curl_easy_setopt(curl, CURLOPT_URL, identica_update_url);
321 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
325 snprintf(user_password, sizeof(user_password), "%s:%s",
326 session->account, session->password);
327 switch (session->host) {
329 curl_easy_setopt(curl, CURLOPT_URL, twitter_friends_url);
332 curl_easy_setopt(curl, CURLOPT_URL, identica_friends_url);
335 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
339 switch (session->host) {
341 curl_easy_setopt(curl, CURLOPT_URL, twitter_public_url);
344 curl_easy_setopt(curl, CURLOPT_URL, identica_public_url);
352 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
355 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
357 dbg("user_password = %s\n", user_password);
358 dbg("data = %s\n", data);
359 dbg("proxy = %s\n", session->proxy);
361 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
362 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
363 res = curl_easy_perform(curl);
364 if (res && !session->bash) {
365 fprintf(stderr, "error(%d) trying to perform operation\n", res);
369 curl_easy_cleanup(curl);
370 if (session->action == ACTION_UPDATE)
371 curl_formfree(formpost);
372 bti_curl_buffer_free(curl_buf);
376 static void parse_configfile(struct session *session)
381 char *account = NULL;
382 char *password = NULL;
385 char *logfile = NULL;
389 /* config file is ~/.bti */
390 file = alloca(strlen(session->homedir) + 7);
392 sprintf(file, "%s/.bti", session->homedir);
394 config_file = fopen(file, "r");
396 /* No error if file does not exist or is unreadable. */
397 if (config_file == NULL)
401 ssize_t n = getline(&line, &len, config_file);
404 if (line[n - 1] == '\n')
406 /* Parse file. Format is the usual value pairs:
409 # is a comment character
411 *strchrnul(line, '#') = '\0';
415 /* Ignore blank lines. */
419 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
423 } else if (!strncasecmp(c, "password", 8) &&
427 password = strdup(c);
428 } else if (!strncasecmp(c, "host", 4) &&
433 } else if (!strncasecmp(c, "proxy", 5) &&
438 } else if (!strncasecmp(c, "logfile", 7) &&
443 } else if (!strncasecmp(c, "action", 6) &&
449 } while (!feof(config_file));
452 session->password = password;
454 session->account = account;
456 if (strcasecmp(host, "twitter") == 0)
457 session->host = HOST_TWITTER;
458 if (strcasecmp(host, "identica") == 0)
459 session->host = HOST_IDENTICA;
464 free(session->proxy);
465 session->proxy = proxy;
468 session->logfile = logfile;
470 if (strcasecmp(action, "update") == 0)
471 session->action = ACTION_UPDATE;
472 if (strcasecmp(action, "friends") == 0)
473 session->action = ACTION_FRIENDS;
474 if (strcasecmp(action, "public") == 0)
475 session->action = ACTION_PUBLIC;
479 /* Free buffer and close file. */
484 static void log_session(struct session *session, int retval)
490 /* Only log something if we have a log file set */
491 if (!session->logfile)
494 filename = alloca(strlen(session->homedir) +
495 strlen(session->logfile) + 3);
497 sprintf(filename, "%s/%s", session->homedir, session->logfile);
499 log_file = fopen(filename, "a+");
500 if (log_file == NULL)
502 switch (session->host) {
514 if (session->action == ACTION_UPDATE) {
516 fprintf(log_file, "%s: host=%s tweet failed\n",
517 session->time, host);
519 fprintf(log_file, "%s: host=%s tweet=%s\n",
520 session->time, host, session->tweet);
521 } else if (session->action == ACTION_FRIENDS) {
522 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
523 session->time, host);
524 } else if (session->action == ACTION_PUBLIC) {
525 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
526 session->time, host);
532 int main(int argc, char *argv[], char *envp[])
534 static const struct option options[] = {
535 { "debug", 0, NULL, 'd' },
536 { "account", 1, NULL, 'a' },
537 { "password", 1, NULL, 'p' },
538 { "host", 1, NULL, 'H' },
539 { "proxy", 1, NULL, 'P' },
540 { "action", 1, NULL, 'A' },
541 { "logfile", 1, NULL, 'L' },
542 { "help", 0, NULL, 'h' },
543 { "bash", 0, NULL, 'b' },
544 { "version", 0, NULL, 'v' },
547 struct session *session;
556 rl_bind_key('\t', rl_insert);
558 session = session_alloc();
560 fprintf(stderr, "no more memory...\n");
564 /* get the current time so that we can log it later */
566 session->time = strdup(ctime(&t));
567 session->time[strlen(session->time)-1] = 0x00;
569 session->homedir = strdup(getenv("HOME"));
571 curl_global_init(CURL_GLOBAL_ALL);
573 /* Set environment variables first, before reading command line options
574 * or config file values. */
575 http_proxy = getenv("http_proxy");
578 free(session->proxy);
579 session->proxy = strdup(http_proxy);
580 dbg("http_proxy = %s\n", session->proxy);
583 parse_configfile(session);
586 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:A:h",
595 if (session->account)
596 free(session->account);
597 session->account = strdup(optarg);
598 dbg("account = %s\n", session->account);
601 if (session->password)
602 free(session->password);
603 session->password = strdup(optarg);
604 dbg("password = %s\n", session->password);
608 free(session->proxy);
609 session->proxy = strdup(optarg);
610 dbg("proxy = %s\n", session->proxy);
613 if (strcasecmp(optarg, "update") == 0)
614 session->action = ACTION_UPDATE;
615 if (strcasecmp(optarg, "friends") == 0)
616 session->action = ACTION_FRIENDS;
617 if (strcasecmp(optarg, "public") == 0)
618 session->action = ACTION_PUBLIC;
619 dbg("action = %d\n", session->action);
622 if (session->logfile)
623 free(session->logfile);
624 session->logfile = strdup(optarg);
625 dbg("logfile = %s\n", session->logfile);
628 if (strcasecmp(optarg, "twitter") == 0)
629 session->host = HOST_TWITTER;
630 if (strcasecmp(optarg, "identica") == 0)
631 session->host = HOST_IDENTICA;
632 dbg("host = %d\n", session->host);
649 if (!session->account) {
650 fprintf(stdout, "Enter twitter account: ");
651 session->account = readline(NULL);
654 if (!session->password) {
655 fprintf(stdout, "Enter twitter password: ");
656 session->password = readline(NULL);
659 if (session->action == ACTION_UPDATE) {
661 tweet = readline(NULL);
663 tweet = readline("tweet: ");
664 if (!tweet || strlen(tweet) == 0) {
669 session->tweet = zalloc(strlen(tweet) + 10);
671 sprintf(session->tweet, "$ %s", tweet);
673 sprintf(session->tweet, "%s", tweet);
676 dbg("tweet = %s\n", session->tweet);
679 dbg("account = %s\n", session->account);
680 dbg("password = %s\n", session->password);
681 dbg("host = %d\n", session->host);
682 dbg("action = %d\n", session->action);
684 /* fork ourself so that the main shell can get on
685 * with it's life as we try to connect and handle everything
690 dbg("child is %d\n", child);
695 retval = send_request(session);
696 if (retval && !session->bash)
697 fprintf(stderr, "operation failed\n");
699 log_session(session, retval);
701 session_free(session);