2 * Copyright (C) 2008-2011 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
4 * Copyright (C) 2009-2010 Amir Mohammad Saied <amirsaied@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <sys/types.h>
35 #include <curl/curl.h>
36 #include <libxml/xmlmemory.h>
37 #include <libxml/parser.h>
38 #include <libxml/tree.h>
45 #define zalloc(size) calloc(size, 1)
47 #define dbg(format, arg...) \
50 fprintf(stdout, "bti: %s: " format , __func__ , \
57 static void display_help(void)
59 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n"
64 " --account accountname\n"
65 " --password password\n"
67 " ('update', 'friends', 'public', 'replies', or 'user')\n"
68 " --user screenname\n"
69 " --group groupname\n"
70 " --proxy PROXY:PORT\n"
72 " --logfile logfile\n"
73 " --config configfile\n"
77 " --page PAGENUMBER\n"
84 " --help\n", VERSION);
87 static void display_version(void)
89 fprintf(stdout, "bti - version %s\n", VERSION);
92 static char *get_string(const char *name)
97 string = zalloc(1000);
101 fprintf(stdout, "%s", name);
102 if (!fgets(string, 999, stdin))
104 temp = strchr(string, '\n');
111 * Try to get a handle to a readline function from a variety of different
112 * libraries. If nothing is present on the system, then fall back to an
115 * Logic originally based off of code in the e2fsutils package in the
116 * lib/ss/get_readline.c file, which is licensed under the MIT license.
118 * This keeps us from having to relicense the bti codebase if readline
119 * ever changes its license, as there is no link-time dependency.
120 * It is a run-time thing only, and we handle any readline-like library
121 * in the same manner, making bti not be a derivative work of any
124 static void session_readline_init(struct session *session)
126 /* Libraries we will try to use for readline/editline functionality */
127 const char *libpath = "libreadline.so.6:libreadline.so.5:"
128 "libreadline.so.4:libreadline.so:libedit.so.2:"
129 "libedit.so:libeditline.so.0:libeditline.so";
131 char *tmp, *cp, *next;
132 int (*bind_key)(int, void *);
133 void (*insert)(void);
135 /* default to internal function if we can't or won't find anything */
136 session->readline = get_string;
139 session->interactive = 1;
141 tmp = malloc(strlen(libpath)+1);
144 strcpy(tmp, libpath);
145 for (cp = tmp; cp; cp = next) {
146 next = strchr(cp, ':');
151 handle = dlopen(cp, RTLD_NOW);
153 dbg("Using %s for readline library\n", cp);
159 dbg("No readline library found.\n");
163 session->readline_handle = handle;
164 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
165 if (session->readline == NULL) {
166 /* something odd happened, default back to internal stuff */
167 session->readline_handle = NULL;
168 session->readline = get_string;
173 * If we found a library, turn off filename expansion
174 * as that makes no sense from within bti.
176 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
177 insert = (void (*)(void))dlsym(handle, "rl_insert");
178 if (bind_key && insert)
179 bind_key('\t', insert);
182 static void session_readline_cleanup(struct session *session)
184 if (session->readline_handle)
185 dlclose(session->readline_handle);
188 static struct session *session_alloc(void)
190 struct session *session;
192 session = zalloc(sizeof(*session));
198 static void session_free(struct session *session)
202 free(session->retweet);
203 free(session->replyto);
204 free(session->password);
205 free(session->account);
206 free(session->consumer_key);
207 free(session->consumer_secret);
208 free(session->access_token_key);
209 free(session->access_token_secret);
210 free(session->tweet);
211 free(session->proxy);
213 free(session->homedir);
215 free(session->group);
216 free(session->hosturl);
217 free(session->hostname);
218 free(session->configfile);
222 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
224 struct bti_curl_buffer *buffer;
226 buffer = zalloc(sizeof(*buffer));
230 /* start out with a data buffer of 1 byte to
231 * make the buffer fill logic simpler */
232 buffer->data = zalloc(1);
238 buffer->action = action;
242 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
250 const char twitter_host[] = "http://api.twitter.com/1/statuses";
251 const char identica_host[] = "https://identi.ca/api/statuses";
252 const char twitter_name[] = "twitter";
253 const char identica_name[] = "identi.ca";
255 static const char twitter_request_token_uri[] = "http://twitter.com/oauth/request_token";
256 static const char twitter_access_token_uri[] = "http://twitter.com/oauth/access_token";
257 static const char twitter_authorize_uri[] = "http://twitter.com/oauth/authorize?oauth_token=";
258 static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token?oauth_callback=oob";
259 static const char identica_access_token_uri[] = "http://identi.ca/api/oauth/access_token";
260 static const char identica_authorize_uri[] = "http://identi.ca/api/oauth/authorize?oauth_token=";
262 static const char user_uri[] = "/user_timeline/";
263 static const char update_uri[] = "/update.xml";
264 static const char public_uri[] = "/public_timeline.xml";
265 static const char friends_uri[] = "/friends_timeline.xml";
266 static const char mentions_uri[] = "/mentions.xml";
267 static const char replies_uri[] = "/replies.xml";
268 static const char retweet_uri[] = "/retweet/";
269 static const char group_uri[] = "/../statusnet/groups/timeline/";
271 static const char config_default[] = "/etc/bti";
272 static const char config_user_default[] = ".bti";
274 static CURL *curl_init(void)
278 curl = curl_easy_init();
280 fprintf(stderr, "Can not init CURL!\n");
283 /* some ssl sanity checks on the connection we are making */
284 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
285 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
289 /* The final place data is sent to the screen/pty/tty */
290 static void bti_output_line(struct session *session, xmlChar *user,
291 xmlChar *id, xmlChar *created, xmlChar *text)
293 if (session->verbose)
294 printf("[%s] {%s} (%.16s) %s\n", user, id, created, text);
296 printf("[%s] %s\n", user, text);
299 static void parse_statuses(struct session *session,
300 xmlDocPtr doc, xmlNodePtr current)
302 xmlChar *text = NULL;
303 xmlChar *user = NULL;
304 xmlChar *created = NULL;
308 current = current->xmlChildrenNode;
309 while (current != NULL) {
310 if (current->type == XML_ELEMENT_NODE) {
311 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
312 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
313 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
314 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
315 if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
316 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
317 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
318 userinfo = current->xmlChildrenNode;
319 while (userinfo != NULL) {
320 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
323 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
325 userinfo = userinfo->next;
329 if (user && text && created && id) {
330 bti_output_line(session, user, id,
342 current = current->next;
348 static void parse_timeline(char *document, struct session *session)
353 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
354 NULL, XML_PARSE_NOERROR);
358 current = xmlDocGetRootElement(doc);
359 if (current == NULL) {
360 fprintf(stderr, "empty document\n");
365 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
366 fprintf(stderr, "unexpected document type\n");
371 current = current->xmlChildrenNode;
372 while (current != NULL) {
373 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
374 parse_statuses(session, doc, current);
375 current = current->next;
382 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
385 struct bti_curl_buffer *curl_buf = userp;
386 size_t buffer_size = size * nmemb;
389 if ((!buffer) || (!buffer_size) || (!curl_buf))
392 /* add to the data we already have */
393 temp = zalloc(curl_buf->length + buffer_size + 1);
397 memcpy(temp, curl_buf->data, curl_buf->length);
398 free(curl_buf->data);
399 curl_buf->data = temp;
400 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
401 curl_buf->length += buffer_size;
402 if (curl_buf->action)
403 parse_timeline(curl_buf->data, curl_buf->session);
405 dbg("%s\n", curl_buf->data);
410 static int parse_osp_reply(const char *reply, char **token, char **secret)
415 rc = oauth_split_url_parameters(reply, &rv);
416 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
417 if (rc == 2 || rc == 4) {
418 if (!strncmp(rv[0], "oauth_token=", 11) &&
419 !strncmp(rv[1], "oauth_token_secret=", 18)) {
421 *token = strdup(&(rv[0][12]));
423 *secret = strdup(&(rv[1][19]));
427 } else if (rc == 3) {
428 if (!strncmp(rv[1], "oauth_token=", 11) &&
429 !strncmp(rv[2], "oauth_token_secret=", 18)) {
431 *token = strdup(&(rv[1][12]));
433 *secret = strdup(&(rv[2][19]));
439 dbg("token: %s\n", *token);
440 dbg("secret: %s\n", *secret);
448 static int request_access_token(struct session *session)
450 char *post_params = NULL;
451 char *request_url = NULL;
454 char *at_secret = NULL;
455 char *verifier = NULL;
461 if (session->host == HOST_TWITTER)
462 request_url = oauth_sign_url2(
463 twitter_request_token_uri, NULL,
464 OA_HMAC, NULL, session->consumer_key,
465 session->consumer_secret, NULL, NULL);
466 else if (session->host == HOST_IDENTICA)
467 request_url = oauth_sign_url2(
468 identica_request_token_uri, NULL,
469 OA_HMAC, NULL, session->consumer_key,
470 session->consumer_secret, NULL, NULL);
471 reply = oauth_http_get(request_url, post_params);
482 if (parse_osp_reply(reply, &at_key, &at_secret))
488 "Please open the following link in your browser, and "
489 "allow 'bti' to access your account. Then paste "
490 "back the provided PIN in here.\n");
491 if (session->host == HOST_TWITTER) {
492 fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
493 verifier = session->readline(NULL);
494 sprintf(at_uri, "%s?oauth_verifier=%s",
495 twitter_access_token_uri, verifier);
496 } else if (session->host == HOST_IDENTICA) {
497 fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key);
498 verifier = session->readline(NULL);
499 sprintf(at_uri, "%s?oauth_verifier=%s",
500 identica_access_token_uri, verifier);
502 request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
503 session->consumer_key,
504 session->consumer_secret,
506 reply = oauth_http_get(request_url, post_params);
511 if (parse_osp_reply(reply, &at_key, &at_secret))
517 "Please put these two lines in your bti "
518 "configuration file (~/.bti):\n"
519 "access_token_key=%s\n"
520 "access_token_secret=%s\n",
526 static int send_request(struct session *session)
529 char user_password[500];
531 struct bti_curl_buffer *curl_buf;
534 struct curl_httppost *formpost = NULL;
535 struct curl_httppost *lastptr = NULL;
536 struct curl_slist *slist = NULL;
537 char *req_url = NULL;
539 char *postarg = NULL;
540 char *escaped_tweet = NULL;
546 if (!session->hosturl)
547 session->hosturl = strdup(twitter_host);
549 if (session->no_oauth || session->guest) {
550 curl_buf = bti_curl_buffer_alloc(session->action);
553 curl_buf->session = session;
559 if (!session->hosturl)
560 session->hosturl = strdup(twitter_host);
562 switch (session->action) {
564 snprintf(user_password, sizeof(user_password), "%s:%s",
565 session->account, session->password);
566 snprintf(data, sizeof(data), "status=\"%s\"",
568 curl_formadd(&formpost, &lastptr,
569 CURLFORM_COPYNAME, "status",
570 CURLFORM_COPYCONTENTS, session->tweet,
573 curl_formadd(&formpost, &lastptr,
574 CURLFORM_COPYNAME, "source",
575 CURLFORM_COPYCONTENTS, "bti",
578 if (session->replyto)
579 curl_formadd(&formpost, &lastptr,
581 "in_reply_to_status_id",
582 CURLFORM_COPYCONTENTS,
586 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
587 slist = curl_slist_append(slist, "Expect:");
588 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
590 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
591 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
592 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
596 snprintf(user_password, sizeof(user_password), "%s:%s",
597 session->account, session->password);
598 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
599 friends_uri, session->page);
600 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
601 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
605 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
606 user_uri, session->user, session->page);
607 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
611 snprintf(user_password, sizeof(user_password), "%s:%s",
612 session->account, session->password);
613 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
614 replies_uri, session->page);
615 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
616 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
620 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
621 public_uri, session->page);
622 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
626 sprintf(endpoint, "%s%s%s.xml?page=%d",
627 session->hosturl, group_uri, session->group,
629 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
637 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
640 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
642 dbg("user_password = %s\n", user_password);
643 dbg("data = %s\n", data);
644 dbg("proxy = %s\n", session->proxy);
646 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
647 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
648 if (!session->dry_run) {
649 res = curl_easy_perform(curl);
650 if (!session->background) {
655 fprintf(stderr, "error(%d) trying to "
656 "perform operation\n", res);
660 doc = xmlReadMemory(curl_buf->data,
662 "response.xml", NULL,
667 current = xmlDocGetRootElement(doc);
668 if (current == NULL) {
669 fprintf(stderr, "empty document\n");
674 if (xmlStrcmp(current->name, (const xmlChar *)"status")) {
675 fprintf(stderr, "unexpected document type\n");
684 curl_easy_cleanup(curl);
685 if (session->action == ACTION_UPDATE)
686 curl_formfree(formpost);
687 bti_curl_buffer_free(curl_buf);
689 switch (session->action) {
691 escaped_tweet = oauth_url_escape(session->tweet);
692 if (session->replyto) {
694 "%s%s?status=%s&in_reply_to_status_id=%s",
695 session->hosturl, update_uri,
696 escaped_tweet, session->replyto);
698 sprintf(endpoint, "%s%s?status=%s",
699 session->hosturl, update_uri,
706 sprintf(endpoint, "%s%s%s.xml?page=%d",
707 session->hosturl, user_uri, session->user,
711 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
712 mentions_uri, session->page);
715 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
716 public_uri, session->page);
719 sprintf(endpoint, "%s%s%s.xml?page=%d",
720 session->hosturl, group_uri, session->group,
724 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
725 friends_uri, session->page);
728 sprintf(endpoint, "%s%s%s.xml", session->hosturl,
729 retweet_uri, session->retweet);
736 dbg("%s\n", endpoint);
737 if (!session->dry_run) {
739 req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC,
740 NULL, session->consumer_key,
741 session->consumer_secret,
742 session->access_token_key,
743 session->access_token_secret);
744 reply = oauth_http_post(req_url, postarg);
746 req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
747 session->consumer_key,
748 session->consumer_secret,
749 session->access_token_key,
750 session->access_token_secret);
751 reply = oauth_http_get(req_url, postarg);
754 dbg("%s\n", req_url);
761 fprintf(stderr, "Error retrieving from URL (%s)\n", endpoint);
765 if ((session->action != ACTION_UPDATE) &&
766 (session->action != ACTION_RETWEET))
767 parse_timeline(reply, session);
772 static void log_session(struct session *session, int retval)
777 /* Only log something if we have a log file set */
778 if (!session->logfile)
781 filename = alloca(strlen(session->homedir) +
782 strlen(session->logfile) + 3);
784 sprintf(filename, "%s/%s", session->homedir, session->logfile);
786 log_file = fopen(filename, "a+");
787 if (log_file == NULL)
790 switch (session->action) {
793 fprintf(log_file, "%s: host=%s tweet failed\n",
794 session->time, session->hostname);
796 fprintf(log_file, "%s: host=%s tweet=%s\n",
797 session->time, session->hostname,
801 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
802 session->time, session->hostname);
805 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
806 session->time, session->hostname, session->user);
809 fprintf(log_file, "%s: host=%s retrieving replies\n",
810 session->time, session->hostname);
813 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
814 session->time, session->hostname);
817 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
818 session->time, session->hostname);
827 static char *get_string_from_stdin(void)
832 string = zalloc(1000);
836 if (!fgets(string, 999, stdin))
838 temp = strchr(string, '\n');
844 static void read_password(char *buf, size_t len, char *host)
854 tp.c_lflag &= (~ECHO);
855 tcsetattr(0, TCSANOW, &tp);
857 fprintf(stdout, "Enter password for %s: ", host);
860 retval = scanf("%79s", pwd);
862 fprintf(stdout, "\n");
864 tcsetattr(0, TCSANOW, &old);
866 strncpy(buf, pwd, len);
870 static int find_urls(const char *tweet, int **pranges)
873 * magic obtained from
874 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
876 static const char *re_magic =
877 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
878 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
879 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
883 int ovector[10] = {0,};
884 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
885 int startoffset, tweetlen;
889 int *ranges = malloc(sizeof(int) * rbound);
891 re = pcre_compile(re_magic,
892 PCRE_NO_AUTO_CAPTURE,
893 &errptr, &erroffset, NULL);
895 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
899 tweetlen = strlen(tweet);
900 for (startoffset = 0; startoffset < tweetlen; ) {
902 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
904 if (rc == PCRE_ERROR_NOMATCH)
908 fprintf(stderr, "pcre_exec @%u: %s\n",
913 for (i = 0; i < rc; i += 2) {
914 if ((rcount+2) == rbound) {
916 ranges = realloc(ranges, sizeof(int) * rbound);
919 ranges[rcount++] = ovector[i];
920 ranges[rcount++] = ovector[i+1];
923 startoffset = ovector[1];
933 * bidirectional popen() call
935 * @param rwepipe - int array of size three
936 * @param exe - program to run
937 * @param argv - argument list
938 * @return pid or -1 on error
940 * The caller passes in an array of three integers (rwepipe), on successful
941 * execution it can then write to element 0 (stdin of exe), and read from
942 * element 1 (stdout) and 2 (stderr).
944 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
974 } else if (pid == 0) {
986 execvp(exe, (char **)argv);
1006 static int pcloseRWE(int pid, int *rwepipe)
1012 rc = waitpid(pid, &status, 0);
1016 static char *shrink_one_url(int *rwepipe, char *big)
1018 int biglen = strlen(big);
1023 rc = dprintf(rwepipe[0], "%s\n", big);
1027 smalllen = biglen + 128;
1028 small = malloc(smalllen);
1032 rc = read(rwepipe[1], small, smalllen);
1033 if (rc < 0 || rc > biglen)
1034 goto error_free_small;
1036 if (strncmp(small, "http://", 7))
1037 goto error_free_small;
1040 while (smalllen && isspace(small[smalllen-1]))
1041 small[--smalllen] = 0;
1051 static char *shrink_urls(char *text)
1058 const char *const shrink_args[] = {
1064 int inlen = strlen(text);
1066 dbg("before len=%u\n", inlen);
1068 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1072 rcount = find_urls(text, &ranges);
1076 for (i = 0; i < rcount; i += 2) {
1077 int url_start = ranges[i];
1078 int url_end = ranges[i+1];
1079 int long_url_len = url_end - url_start;
1080 char *url = strndup(text + url_start, long_url_len);
1082 int not_url_len = url_start - inofs;
1084 dbg("long url[%u]: %s\n", long_url_len, url);
1085 url = shrink_one_url(shrink_pipe, url);
1086 short_url_len = url ? strlen(url) : 0;
1087 dbg("short url[%u]: %s\n", short_url_len, url);
1089 if (!url || short_url_len >= long_url_len) {
1090 /* The short url ended up being too long
1093 strncpy(text + outofs, text + inofs,
1094 not_url_len + long_url_len);
1096 inofs += not_url_len + long_url_len;
1097 outofs += not_url_len + long_url_len;
1100 /* copy the unmodified block */
1101 strncpy(text + outofs, text + inofs, not_url_len);
1102 inofs += not_url_len;
1103 outofs += not_url_len;
1105 /* copy the new url */
1106 strncpy(text + outofs, url, short_url_len);
1107 inofs += long_url_len;
1108 outofs += short_url_len;
1114 /* copy the last block after the last match */
1116 int tail = inlen - inofs;
1118 strncpy(text + outofs, text + inofs, tail);
1125 (void)pcloseRWE(shrink_pid, shrink_pipe);
1128 dbg("after len=%u\n", outofs);
1132 int main(int argc, char *argv[], char *envp[])
1134 static const struct option options[] = {
1135 { "debug", 0, NULL, 'd' },
1136 { "verbose", 0, NULL, 'V' },
1137 { "account", 1, NULL, 'a' },
1138 { "password", 1, NULL, 'p' },
1139 { "host", 1, NULL, 'H' },
1140 { "proxy", 1, NULL, 'P' },
1141 { "action", 1, NULL, 'A' },
1142 { "user", 1, NULL, 'u' },
1143 { "group", 1, NULL, 'G' },
1144 { "logfile", 1, NULL, 'L' },
1145 { "shrink-urls", 0, NULL, 's' },
1146 { "help", 0, NULL, 'h' },
1147 { "bash", 0, NULL, 'b' },
1148 { "background", 0, NULL, 'B' },
1149 { "dry-run", 0, NULL, 'n' },
1150 { "page", 1, NULL, 'g' },
1151 { "version", 0, NULL, 'v' },
1152 { "config", 1, NULL, 'c' },
1153 { "replyto", 1, NULL, 'r' },
1154 { "retweet", 1, NULL, 'w' },
1157 struct session *session;
1160 static char password[80];
1165 const char *config_file;
1171 session = session_alloc();
1173 fprintf(stderr, "no more memory...\n");
1177 /* get the current time so that we can log it later */
1179 session->time = strdup(ctime(&t));
1180 session->time[strlen(session->time)-1] = 0x00;
1183 * Get the home directory so we can try to find a config file.
1184 * If we have no home dir set up, look in /etc/bti
1186 home = getenv("HOME");
1188 /* We have a home dir, so this might be a user */
1189 session->homedir = strdup(home);
1190 config_file = config_user_default;
1192 session->homedir = strdup("");
1193 config_file = config_default;
1196 /* set up a default config file location (traditionally ~/.bti) */
1197 session->configfile = zalloc(strlen(session->homedir) + strlen(config_file) + 7);
1198 sprintf(session->configfile, "%s/%s", session->homedir, config_file);
1200 /* Set environment variables first, before reading command line options
1201 * or config file values. */
1202 http_proxy = getenv("http_proxy");
1205 free(session->proxy);
1206 session->proxy = strdup(http_proxy);
1207 dbg("http_proxy = %s\n", session->proxy);
1210 bti_parse_configfile(session);
1213 option = getopt_long_only(argc, argv,
1214 "dp:P:H:a:A:u:c:hg:G:sr:nVvw:",
1223 session->verbose = 1;
1226 if (session->account)
1227 free(session->account);
1228 session->account = strdup(optarg);
1229 dbg("account = %s\n", session->account);
1232 page_nr = atoi(optarg);
1233 dbg("page = %d\n", page_nr);
1234 session->page = page_nr;
1237 session->replyto = strdup(optarg);
1238 dbg("in_reply_to_status_id = %s\n", session->replyto);
1241 session->retweet = strdup(optarg);
1242 dbg("Retweet ID = %s\n", session->retweet);
1245 if (session->password)
1246 free(session->password);
1247 session->password = strdup(optarg);
1248 dbg("password = %s\n", session->password);
1252 free(session->proxy);
1253 session->proxy = strdup(optarg);
1254 dbg("proxy = %s\n", session->proxy);
1257 if (strcasecmp(optarg, "update") == 0)
1258 session->action = ACTION_UPDATE;
1259 else if (strcasecmp(optarg, "friends") == 0)
1260 session->action = ACTION_FRIENDS;
1261 else if (strcasecmp(optarg, "user") == 0)
1262 session->action = ACTION_USER;
1263 else if (strcasecmp(optarg, "replies") == 0)
1264 session->action = ACTION_REPLIES;
1265 else if (strcasecmp(optarg, "public") == 0)
1266 session->action = ACTION_PUBLIC;
1267 else if (strcasecmp(optarg, "group") == 0)
1268 session->action = ACTION_GROUP;
1269 else if (strcasecmp(optarg, "retweet") == 0)
1270 session->action = ACTION_RETWEET;
1272 session->action = ACTION_UNKNOWN;
1273 dbg("action = %d\n", session->action);
1277 free(session->user);
1278 session->user = strdup(optarg);
1279 dbg("user = %s\n", session->user);
1284 free(session->group);
1285 session->group = strdup(optarg);
1286 dbg("group = %s\n", session->group);
1289 if (session->logfile)
1290 free(session->logfile);
1291 session->logfile = strdup(optarg);
1292 dbg("logfile = %s\n", session->logfile);
1295 session->shrink_urls = 1;
1298 if (session->hosturl)
1299 free(session->hosturl);
1300 if (session->hostname)
1301 free(session->hostname);
1302 if (strcasecmp(optarg, "twitter") == 0) {
1303 session->host = HOST_TWITTER;
1304 session->hosturl = strdup(twitter_host);
1305 session->hostname = strdup(twitter_name);
1306 } else if (strcasecmp(optarg, "identica") == 0) {
1307 session->host = HOST_IDENTICA;
1308 session->hosturl = strdup(identica_host);
1309 session->hostname = strdup(identica_name);
1311 session->host = HOST_CUSTOM;
1312 session->hosturl = strdup(optarg);
1313 session->hostname = strdup(optarg);
1315 dbg("host = %d\n", session->host);
1319 /* fall-through intended */
1321 session->background = 1;
1324 if (session->configfile)
1325 free(session->configfile);
1326 session->configfile = strdup(optarg);
1327 dbg("configfile = %s\n", session->configfile);
1330 * read the config file now. Yes, this could override
1331 * previously set options from the command line, but
1332 * the user asked for it...
1334 bti_parse_configfile(session);
1340 session->dry_run = 1;
1351 session_readline_init(session);
1353 * Show the version to make it easier to determine what
1359 if (session->host == HOST_TWITTER) {
1360 if (!session->consumer_key || !session->consumer_secret) {
1361 if (session->action == ACTION_USER ||
1362 session->action == ACTION_PUBLIC) {
1364 * Some actions may still work without
1370 "Twitter no longer supports HTTP basic authentication.\n"
1371 "Both consumer key, and consumer secret are required"
1372 " for bti in order to behave as an OAuth consumer.\n");
1376 if (session->action == ACTION_GROUP) {
1377 fprintf(stderr, "Groups only work in Identi.ca.\n");
1381 if (!session->consumer_key || !session->consumer_secret)
1382 session->no_oauth = 1;
1385 if (session->no_oauth) {
1386 if (!session->account) {
1387 fprintf(stdout, "Enter account for %s: ",
1389 session->account = session->readline(NULL);
1391 if (!session->password) {
1392 read_password(password, sizeof(password),
1394 session->password = strdup(password);
1396 } else if (!session->guest) {
1397 if (!session->access_token_key ||
1398 !session->access_token_secret) {
1399 request_access_token(session);
1404 if (session->action == ACTION_UNKNOWN) {
1405 fprintf(stderr, "Unknown action, valid actions are:\n"
1406 "'update', 'friends', 'public', 'replies', 'group' or 'user'.\n");
1410 if (session->action == ACTION_GROUP && !session->group) {
1411 fprintf(stdout, "Enter group name: ");
1412 session->group = session->readline(NULL);
1415 if (session->action == ACTION_RETWEET) {
1416 if (!session->retweet) {
1419 fprintf(stdout, "Status ID to retweet: ");
1420 rtid = get_string_from_stdin();
1421 session->retweet = zalloc(strlen(rtid) + 10);
1422 sprintf(session->retweet, "%s", rtid);
1426 if (!session->retweet || strlen(session->retweet) == 0) {
1427 dbg("no retweet?\n");
1431 dbg("retweet ID = %s\n", session->retweet);
1434 if (session->action == ACTION_UPDATE) {
1435 if (session->background || !session->interactive)
1436 tweet = get_string_from_stdin();
1438 tweet = session->readline("tweet: ");
1439 if (!tweet || strlen(tweet) == 0) {
1444 if (session->shrink_urls)
1445 tweet = shrink_urls(tweet);
1447 session->tweet = zalloc(strlen(tweet) + 10);
1449 sprintf(session->tweet, "%c %s",
1450 getuid() ? '$' : '#', tweet);
1452 sprintf(session->tweet, "%s", tweet);
1455 dbg("tweet = %s\n", session->tweet);
1458 if (session->page == 0)
1460 dbg("config file = %s\n", session->configfile);
1461 dbg("host = %d\n", session->host);
1462 dbg("action = %d\n", session->action);
1464 /* fork ourself so that the main shell can get on
1465 * with it's life as we try to connect and handle everything
1467 if (session->background) {
1470 dbg("child is %d\n", child);
1475 retval = send_request(session);
1476 if (retval && !session->background)
1477 fprintf(stderr, "operation failed\n");
1479 log_session(session, retval);
1481 session_readline_cleanup(session);
1482 session_free(session);