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 or identi.ca\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, " --action action\n");
91 fprintf(stdout, " ('update', 'friends', 'public', 'replies' or 'user')\n");
92 fprintf(stdout, " --user screenname\n");
93 fprintf(stdout, " --proxy PROXY:PORT\n");
94 fprintf(stdout, " --host HOST\n");
95 fprintf(stdout, " --logfile logfile\n");
96 fprintf(stdout, " --bash\n");
97 fprintf(stdout, " --debug\n");
98 fprintf(stdout, " --version\n");
99 fprintf(stdout, " --help\n");
102 static void display_version(void)
104 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
107 static struct session *session_alloc(void)
109 struct session *session;
111 session = zalloc(sizeof(*session));
117 static void session_free(struct session *session)
121 free(session->password);
122 free(session->account);
123 free(session->tweet);
124 free(session->proxy);
126 free(session->homedir);
131 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
133 struct bti_curl_buffer *buffer;
135 buffer = zalloc(sizeof(*buffer));
139 /* start out with a data buffer of 1 byte to
140 * make the buffer fill logic simpler */
141 buffer->data = zalloc(1);
147 buffer->action = action;
151 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
159 static const char *twitter_user_url = "http://twitter.com/statuses/user_timeline/";
160 static const char *twitter_update_url = "https://twitter.com/statuses/update.xml";
161 static const char *twitter_public_url = "http://twitter.com/statuses/public_timeline.xml";
162 static const char *twitter_friends_url = "https://twitter.com/statuses/friends_timeline.xml";
163 static const char *twitter_replies_url = "http://twitter.com/statuses/replies.xml";
165 static const char *identica_user_url = "http://identi.ca/api/statuses/user_timeline/";
166 static const char *identica_update_url = "http://identi.ca/api/statuses/update.xml";
167 static const char *identica_public_url = "http://identi.ca/api/statuses/public_timeline.xml";
168 static const char *identica_friends_url = "http://identi.ca/api/statuses/friends_timeline.xml";
169 static const char *identica_replies_url = "http://identi.ca/api/statuses/replies.xml";
171 static CURL *curl_init(void)
175 curl = curl_easy_init();
177 fprintf(stderr, "Can not init CURL!\n");
180 /* some ssl sanity checks on the connection we are making */
181 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
182 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
186 void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
188 xmlChar *text = NULL;
189 xmlChar *user = NULL;
192 current = current->xmlChildrenNode;
193 while (current != NULL) {
194 if (current->type == XML_ELEMENT_NODE) {
195 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
196 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
197 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
198 userinfo = current->xmlChildrenNode;
199 while (userinfo != NULL) {
200 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
203 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
205 userinfo = userinfo->next;
209 printf("[%s] %s\n", user, text);
216 current = current->next;
222 static void parse_timeline(char *document)
226 doc = xmlReadMemory(document, strlen(document), "timeline.xml", NULL, XML_PARSE_NOERROR);
231 current = xmlDocGetRootElement(doc);
232 if (current == NULL) {
233 fprintf(stderr, "empty document\n");
238 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
239 fprintf(stderr, "unexpected document type\n");
244 current = current->xmlChildrenNode;
245 while (current != NULL) {
246 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
247 parse_statuses(doc, current);
248 current = current->next;
255 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
257 struct bti_curl_buffer *curl_buf = userp;
258 size_t buffer_size = size * nmemb;
261 if ((!buffer) || (!buffer_size) || (!curl_buf))
264 /* add to the data we already have */
265 temp = zalloc(curl_buf->length + buffer_size + 1);
269 memcpy(temp, curl_buf->data, curl_buf->length);
270 free(curl_buf->data);
271 curl_buf->data = temp;
272 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
273 curl_buf->length += buffer_size;
274 if (curl_buf->action)
275 parse_timeline(curl_buf->data);
277 dbg("%s\n", curl_buf->data);
282 static int send_request(struct session *session)
284 char user_password[500];
286 /* is there usernames longer than 22 chars? */
288 struct bti_curl_buffer *curl_buf;
291 struct curl_httppost *formpost = NULL;
292 struct curl_httppost *lastptr = NULL;
293 struct curl_slist *slist = NULL;
298 curl_buf = bti_curl_buffer_alloc(session->action);
306 switch (session->action) {
308 snprintf(user_password, sizeof(user_password), "%s:%s",
309 session->account, session->password);
310 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
311 curl_formadd(&formpost, &lastptr,
312 CURLFORM_COPYNAME, "status",
313 CURLFORM_COPYCONTENTS, session->tweet,
316 curl_formadd(&formpost, &lastptr,
317 CURLFORM_COPYNAME, "source",
318 CURLFORM_COPYCONTENTS, "bti",
321 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
322 slist = curl_slist_append(slist, "Expect:");
323 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
324 switch (session->host) {
326 curl_easy_setopt(curl, CURLOPT_URL, twitter_update_url);
329 curl_easy_setopt(curl, CURLOPT_URL, identica_update_url);
332 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
336 snprintf(user_password, sizeof(user_password), "%s:%s",
337 session->account, session->password);
338 switch (session->host) {
340 curl_easy_setopt(curl, CURLOPT_URL, twitter_friends_url);
343 curl_easy_setopt(curl, CURLOPT_URL, identica_friends_url);
346 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
350 switch (session->host) {
352 sprintf(user_url, "%s%s.xml", twitter_user_url, session->user);
353 curl_easy_setopt(curl, CURLOPT_URL, user_url);
356 sprintf(user_url, "%s%s.xml", identica_user_url, session->user);
357 curl_easy_setopt(curl, CURLOPT_URL, user_url);
363 snprintf(user_password, sizeof(user_password), "%s:%s",
364 session->account, session->password);
365 switch (session->host) {
367 curl_easy_setopt(curl, CURLOPT_URL, twitter_replies_url);
370 curl_easy_setopt(curl, CURLOPT_URL, identica_replies_url);
373 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
377 switch (session->host) {
379 curl_easy_setopt(curl, CURLOPT_URL, twitter_public_url);
382 curl_easy_setopt(curl, CURLOPT_URL, identica_public_url);
390 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
393 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
395 dbg("user_password = %s\n", user_password);
396 dbg("data = %s\n", data);
397 dbg("proxy = %s\n", session->proxy);
399 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
400 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
401 res = curl_easy_perform(curl);
402 if (res && !session->bash) {
403 fprintf(stderr, "error(%d) trying to perform operation\n", res);
407 curl_easy_cleanup(curl);
408 if (session->action == ACTION_UPDATE)
409 curl_formfree(formpost);
410 bti_curl_buffer_free(curl_buf);
414 static void parse_configfile(struct session *session)
419 char *account = NULL;
420 char *password = NULL;
423 char *logfile = NULL;
428 /* config file is ~/.bti */
429 file = alloca(strlen(session->homedir) + 7);
431 sprintf(file, "%s/.bti", session->homedir);
433 config_file = fopen(file, "r");
435 /* No error if file does not exist or is unreadable. */
436 if (config_file == NULL)
440 ssize_t n = getline(&line, &len, config_file);
443 if (line[n - 1] == '\n')
445 /* Parse file. Format is the usual value pairs:
448 # is a comment character
450 *strchrnul(line, '#') = '\0';
454 /* Ignore blank lines. */
458 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
462 } else if (!strncasecmp(c, "password", 8) &&
466 password = strdup(c);
467 } else if (!strncasecmp(c, "host", 4) &&
472 } else if (!strncasecmp(c, "proxy", 5) &&
477 } else if (!strncasecmp(c, "logfile", 7) &&
482 } else if (!strncasecmp(c, "action", 6) &&
487 } else if (!strncasecmp(c, "user", 4) &&
493 } while (!feof(config_file));
496 session->password = password;
498 session->account = account;
500 if (strcasecmp(host, "twitter") == 0)
501 session->host = HOST_TWITTER;
502 if (strcasecmp(host, "identica") == 0)
503 session->host = HOST_IDENTICA;
508 free(session->proxy);
509 session->proxy = proxy;
512 session->logfile = logfile;
514 if (strcasecmp(action, "update") == 0)
515 session->action = ACTION_UPDATE;
516 if (strcasecmp(action, "friends") == 0)
517 session->action = ACTION_FRIENDS;
518 if (strcasecmp(action, "user") == 0)
519 session->action = ACTION_USER;
520 if (strcasecmp(action, "replies") == 0)
521 session->action = ACTION_REPLIES;
522 if (strcasecmp(action, "public") == 0)
523 session->action = ACTION_PUBLIC;
527 session->user = user;
530 /* Free buffer and close file. */
535 static void log_session(struct session *session, int retval)
541 /* Only log something if we have a log file set */
542 if (!session->logfile)
545 filename = alloca(strlen(session->homedir) +
546 strlen(session->logfile) + 3);
548 sprintf(filename, "%s/%s", session->homedir, session->logfile);
550 log_file = fopen(filename, "a+");
551 if (log_file == NULL)
553 switch (session->host) {
565 switch (session->action) {
568 fprintf(log_file, "%s: host=%s tweet failed\n",
569 session->time, host);
571 fprintf(log_file, "%s: host=%s tweet=%s\n",
572 session->time, host, session->tweet);
575 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
576 session->time, host);
579 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
580 session->time, host, session->user);
583 fprintf(log_file, "%s: host=%s retrieving replies\n",
584 session->time, host);
587 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
588 session->time, host);
595 int main(int argc, char *argv[], char *envp[])
597 static const struct option options[] = {
598 { "debug", 0, NULL, 'd' },
599 { "account", 1, NULL, 'a' },
600 { "password", 1, NULL, 'p' },
601 { "host", 1, NULL, 'H' },
602 { "proxy", 1, NULL, 'P' },
603 { "action", 1, NULL, 'A' },
604 { "user", 1, NULL, 'u' },
605 { "logfile", 1, NULL, 'L' },
606 { "help", 0, NULL, 'h' },
607 { "bash", 0, NULL, 'b' },
608 { "version", 0, NULL, 'v' },
611 struct session *session;
620 rl_bind_key('\t', rl_insert);
622 session = session_alloc();
624 fprintf(stderr, "no more memory...\n");
628 /* get the current time so that we can log it later */
630 session->time = strdup(ctime(&t));
631 session->time[strlen(session->time)-1] = 0x00;
633 session->homedir = strdup(getenv("HOME"));
635 curl_global_init(CURL_GLOBAL_ALL);
637 /* Set environment variables first, before reading command line options
638 * or config file values. */
639 http_proxy = getenv("http_proxy");
642 free(session->proxy);
643 session->proxy = strdup(http_proxy);
644 dbg("http_proxy = %s\n", session->proxy);
647 parse_configfile(session);
650 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:A:u:h",
659 if (session->account)
660 free(session->account);
661 session->account = strdup(optarg);
662 dbg("account = %s\n", session->account);
665 if (session->password)
666 free(session->password);
667 session->password = strdup(optarg);
668 dbg("password = %s\n", session->password);
672 free(session->proxy);
673 session->proxy = strdup(optarg);
674 dbg("proxy = %s\n", session->proxy);
677 if (strcasecmp(optarg, "update") == 0)
678 session->action = ACTION_UPDATE;
679 if (strcasecmp(optarg, "friends") == 0)
680 session->action = ACTION_FRIENDS;
681 if (strcasecmp(optarg, "user") == 0)
682 session->action = ACTION_USER;
683 if (strcasecmp(optarg, "replies") == 0)
684 session->action = ACTION_REPLIES;
685 if (strcasecmp(optarg, "public") == 0)
686 session->action = ACTION_PUBLIC;
687 dbg("action = %d\n", session->action);
692 session->user = strdup(optarg);
693 dbg("user = %s\n", session->user);
696 if (session->logfile)
697 free(session->logfile);
698 session->logfile = strdup(optarg);
699 dbg("logfile = %s\n", session->logfile);
702 if (strcasecmp(optarg, "twitter") == 0)
703 session->host = HOST_TWITTER;
704 if (strcasecmp(optarg, "identica") == 0)
705 session->host = HOST_IDENTICA;
706 dbg("host = %d\n", session->host);
723 if (!session->account) {
724 fprintf(stdout, "Enter twitter account: ");
725 session->account = readline(NULL);
728 if (!session->password) {
729 fprintf(stdout, "Enter twitter password: ");
730 session->password = readline(NULL);
733 if (session->action == ACTION_UPDATE) {
735 tweet = readline(NULL);
737 tweet = readline("tweet: ");
738 if (!tweet || strlen(tweet) == 0) {
743 session->tweet = zalloc(strlen(tweet) + 10);
745 sprintf(session->tweet, "$ %s", tweet);
747 sprintf(session->tweet, "%s", tweet);
750 dbg("tweet = %s\n", session->tweet);
754 session->user = session->account;
756 dbg("account = %s\n", session->account);
757 dbg("password = %s\n", session->password);
758 dbg("host = %d\n", session->host);
759 dbg("action = %d\n", session->action);
761 /* fork ourself so that the main shell can get on
762 * with it's life as we try to connect and handle everything
767 dbg("child is %d\n", child);
772 retval = send_request(session);
773 if (retval && !session->bash)
774 fprintf(stderr, "operation failed\n");
776 log_session(session, retval);
778 session_free(session);