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"
78 " --column COLUMNWIDTH\n"
85 " --help\n", VERSION);
88 static void display_version(void)
90 fprintf(stdout, "bti - version %s\n", VERSION);
93 static char *get_string(const char *name)
98 string = zalloc(1000);
102 fprintf(stdout, "%s", name);
103 if (!fgets(string, 999, stdin)) {
107 temp = strchr(string, '\n');
114 * Try to get a handle to a readline function from a variety of different
115 * libraries. If nothing is present on the system, then fall back to an
118 * Logic originally based off of code in the e2fsutils package in the
119 * lib/ss/get_readline.c file, which is licensed under the MIT license.
121 * This keeps us from having to relicense the bti codebase if readline
122 * ever changes its license, as there is no link-time dependency.
123 * It is a run-time thing only, and we handle any readline-like library
124 * in the same manner, making bti not be a derivative work of any
127 static void session_readline_init(struct session *session)
129 /* Libraries we will try to use for readline/editline functionality */
130 const char *libpath = "libreadline.so.6:libreadline.so.5:"
131 "libreadline.so.4:libreadline.so:libedit.so.2:"
132 "libedit.so:libeditline.so.0:libeditline.so";
134 char *tmp, *cp, *next;
135 int (*bind_key)(int, void *);
136 void (*insert)(void);
138 /* default to internal function if we can't or won't find anything */
139 session->readline = get_string;
142 session->interactive = 1;
144 tmp = malloc(strlen(libpath)+1);
147 strcpy(tmp, libpath);
148 for (cp = tmp; cp; cp = next) {
149 next = strchr(cp, ':');
154 handle = dlopen(cp, RTLD_NOW);
156 dbg("Using %s for readline library\n", cp);
162 dbg("No readline library found.\n");
166 session->readline_handle = handle;
167 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
168 if (session->readline == NULL) {
169 /* something odd happened, default back to internal stuff */
170 session->readline_handle = NULL;
171 session->readline = get_string;
176 * If we found a library, turn off filename expansion
177 * as that makes no sense from within bti.
179 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
180 insert = (void (*)(void))dlsym(handle, "rl_insert");
181 if (bind_key && insert)
182 bind_key('\t', insert);
185 static void session_readline_cleanup(struct session *session)
187 if (session->readline_handle)
188 dlclose(session->readline_handle);
191 static struct session *session_alloc(void)
193 struct session *session;
195 session = zalloc(sizeof(*session));
201 static void session_free(struct session *session)
205 free(session->retweet);
206 free(session->replyto);
207 free(session->password);
208 free(session->account);
209 free(session->consumer_key);
210 free(session->consumer_secret);
211 free(session->access_token_key);
212 free(session->access_token_secret);
213 free(session->tweet);
214 free(session->proxy);
216 free(session->homedir);
218 free(session->group);
219 free(session->hosturl);
220 free(session->hostname);
221 free(session->configfile);
225 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
227 struct bti_curl_buffer *buffer;
229 buffer = zalloc(sizeof(*buffer));
233 /* start out with a data buffer of 1 byte to
234 * make the buffer fill logic simpler */
235 buffer->data = zalloc(1);
241 buffer->action = action;
245 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
253 const char twitter_host[] = "http://api.twitter.com/1/statuses";
254 const char identica_host[] = "https://identi.ca/api/statuses";
255 const char twitter_name[] = "twitter";
256 const char identica_name[] = "identi.ca";
258 static const char twitter_request_token_uri[] = "http://twitter.com/oauth/request_token";
259 static const char twitter_access_token_uri[] = "http://twitter.com/oauth/access_token";
260 static const char twitter_authorize_uri[] = "http://twitter.com/oauth/authorize?oauth_token=";
261 static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token?oauth_callback=oob";
262 static const char identica_access_token_uri[] = "http://identi.ca/api/oauth/access_token";
263 static const char identica_authorize_uri[] = "http://identi.ca/api/oauth/authorize?oauth_token=";
265 static const char user_uri[] = "/user_timeline/";
266 static const char update_uri[] = "/update.xml";
267 static const char public_uri[] = "/public_timeline.xml";
268 static const char friends_uri[] = "/friends_timeline.xml";
269 static const char mentions_uri[] = "/mentions.xml";
270 static const char replies_uri[] = "/replies.xml";
271 static const char retweet_uri[] = "/retweet/";
272 static const char group_uri[] = "/../statusnet/groups/timeline/";
274 static const char config_default[] = "/etc/bti";
275 static const char config_user_default[] = ".bti";
277 static CURL *curl_init(void)
281 curl = curl_easy_init();
283 fprintf(stderr, "Can not init CURL!\n");
286 /* some ssl sanity checks on the connection we are making */
287 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
288 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
292 /* The final place data is sent to the screen/pty/tty */
293 static void bti_output_line(struct session *session, xmlChar *user,
294 xmlChar *id, xmlChar *created, xmlChar *text)
296 if (session->verbose)
297 printf("[%*s] {%s} (%.16s) %s\n", -session->column_output, user,
300 printf("[%*s] %s\n", -session->column_output, user, text);
303 static void parse_statuses(struct session *session,
304 xmlDocPtr doc, xmlNodePtr current)
306 xmlChar *text = NULL;
307 xmlChar *user = NULL;
308 xmlChar *created = NULL;
312 current = current->xmlChildrenNode;
313 while (current != NULL) {
314 if (current->type == XML_ELEMENT_NODE) {
315 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
316 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
317 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
318 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
319 if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
320 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
321 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
322 userinfo = current->xmlChildrenNode;
323 while (userinfo != NULL) {
324 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
327 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
329 userinfo = userinfo->next;
333 if (user && text && created && id) {
334 bti_output_line(session, user, id,
346 current = current->next;
352 static void parse_timeline(char *document, struct session *session)
357 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
358 NULL, XML_PARSE_NOERROR);
362 current = xmlDocGetRootElement(doc);
363 if (current == NULL) {
364 fprintf(stderr, "empty document\n");
369 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
370 fprintf(stderr, "unexpected document type\n");
375 current = current->xmlChildrenNode;
376 while (current != NULL) {
377 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
378 parse_statuses(session, doc, current);
379 current = current->next;
386 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
389 struct bti_curl_buffer *curl_buf = userp;
390 size_t buffer_size = size * nmemb;
393 if ((!buffer) || (!buffer_size) || (!curl_buf))
396 /* add to the data we already have */
397 temp = zalloc(curl_buf->length + buffer_size + 1);
401 memcpy(temp, curl_buf->data, curl_buf->length);
402 free(curl_buf->data);
403 curl_buf->data = temp;
404 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
405 curl_buf->length += buffer_size;
406 if (curl_buf->action)
407 parse_timeline(curl_buf->data, curl_buf->session);
409 dbg("%s\n", curl_buf->data);
414 static int parse_osp_reply(const char *reply, char **token, char **secret)
419 rc = oauth_split_url_parameters(reply, &rv);
420 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
421 if (rc == 2 || rc == 4) {
422 if (!strncmp(rv[0], "oauth_token=", 11) &&
423 !strncmp(rv[1], "oauth_token_secret=", 18)) {
425 *token = strdup(&(rv[0][12]));
427 *secret = strdup(&(rv[1][19]));
431 } else if (rc == 3) {
432 if (!strncmp(rv[1], "oauth_token=", 11) &&
433 !strncmp(rv[2], "oauth_token_secret=", 18)) {
435 *token = strdup(&(rv[1][12]));
437 *secret = strdup(&(rv[2][19]));
443 dbg("token: %s\n", *token);
444 dbg("secret: %s\n", *secret);
452 static int request_access_token(struct session *session)
454 char *post_params = NULL;
455 char *request_url = NULL;
458 char *at_secret = NULL;
459 char *verifier = NULL;
465 if (session->host == HOST_TWITTER)
466 request_url = oauth_sign_url2(
467 twitter_request_token_uri, NULL,
468 OA_HMAC, NULL, session->consumer_key,
469 session->consumer_secret, NULL, NULL);
470 else if (session->host == HOST_IDENTICA)
471 request_url = oauth_sign_url2(
472 identica_request_token_uri, NULL,
473 OA_HMAC, NULL, session->consumer_key,
474 session->consumer_secret, NULL, NULL);
475 reply = oauth_http_get(request_url, post_params);
486 if (parse_osp_reply(reply, &at_key, &at_secret))
492 "Please open the following link in your browser, and "
493 "allow 'bti' to access your account. Then paste "
494 "back the provided PIN in here.\n");
495 if (session->host == HOST_TWITTER) {
496 fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
497 verifier = session->readline(NULL);
498 sprintf(at_uri, "%s?oauth_verifier=%s",
499 twitter_access_token_uri, verifier);
500 } else if (session->host == HOST_IDENTICA) {
501 fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key);
502 verifier = session->readline(NULL);
503 sprintf(at_uri, "%s?oauth_verifier=%s",
504 identica_access_token_uri, verifier);
506 request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
507 session->consumer_key,
508 session->consumer_secret,
510 reply = oauth_http_get(request_url, post_params);
515 if (parse_osp_reply(reply, &at_key, &at_secret))
521 "Please put these two lines in your bti "
522 "configuration file (~/.bti):\n"
523 "access_token_key=%s\n"
524 "access_token_secret=%s\n",
530 static int send_request(struct session *session)
533 char user_password[500];
535 struct bti_curl_buffer *curl_buf;
538 struct curl_httppost *formpost = NULL;
539 struct curl_httppost *lastptr = NULL;
540 struct curl_slist *slist = NULL;
541 char *req_url = NULL;
543 char *postarg = NULL;
544 char *escaped_tweet = NULL;
550 if (!session->hosturl)
551 session->hosturl = strdup(twitter_host);
553 if (session->no_oauth || session->guest) {
554 curl_buf = bti_curl_buffer_alloc(session->action);
557 curl_buf->session = session;
563 if (!session->hosturl)
564 session->hosturl = strdup(twitter_host);
566 switch (session->action) {
568 snprintf(user_password, sizeof(user_password), "%s:%s",
569 session->account, session->password);
570 snprintf(data, sizeof(data), "status=\"%s\"",
572 curl_formadd(&formpost, &lastptr,
573 CURLFORM_COPYNAME, "status",
574 CURLFORM_COPYCONTENTS, session->tweet,
577 curl_formadd(&formpost, &lastptr,
578 CURLFORM_COPYNAME, "source",
579 CURLFORM_COPYCONTENTS, "bti",
582 if (session->replyto)
583 curl_formadd(&formpost, &lastptr,
585 "in_reply_to_status_id",
586 CURLFORM_COPYCONTENTS,
590 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
591 slist = curl_slist_append(slist, "Expect:");
592 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
594 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
595 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
596 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
600 snprintf(user_password, sizeof(user_password), "%s:%s",
601 session->account, session->password);
602 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
603 friends_uri, session->page);
604 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
605 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
609 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
610 user_uri, session->user, session->page);
611 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
615 snprintf(user_password, sizeof(user_password), "%s:%s",
616 session->account, session->password);
617 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
618 replies_uri, session->page);
619 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
620 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
624 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
625 public_uri, session->page);
626 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
630 sprintf(endpoint, "%s%s%s.xml?page=%d",
631 session->hosturl, group_uri, session->group,
633 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
641 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
644 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
646 dbg("user_password = %s\n", user_password);
647 dbg("data = %s\n", data);
648 dbg("proxy = %s\n", session->proxy);
650 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
651 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
652 if (!session->dry_run) {
653 res = curl_easy_perform(curl);
654 if (!session->background) {
659 fprintf(stderr, "error(%d) trying to "
660 "perform operation\n", res);
664 doc = xmlReadMemory(curl_buf->data,
666 "response.xml", NULL,
671 current = xmlDocGetRootElement(doc);
672 if (current == NULL) {
673 fprintf(stderr, "empty document\n");
678 if (xmlStrcmp(current->name, (const xmlChar *)"status")) {
679 fprintf(stderr, "unexpected document type\n");
688 curl_easy_cleanup(curl);
689 if (session->action == ACTION_UPDATE)
690 curl_formfree(formpost);
691 bti_curl_buffer_free(curl_buf);
693 switch (session->action) {
695 escaped_tweet = oauth_url_escape(session->tweet);
696 if (session->replyto) {
698 "%s%s?status=%s&in_reply_to_status_id=%s",
699 session->hosturl, update_uri,
700 escaped_tweet, session->replyto);
702 sprintf(endpoint, "%s%s?status=%s",
703 session->hosturl, update_uri,
710 sprintf(endpoint, "%s%s%s.xml?page=%d",
711 session->hosturl, user_uri, session->user,
715 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
716 mentions_uri, session->page);
719 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
720 public_uri, session->page);
723 sprintf(endpoint, "%s%s%s.xml?page=%d",
724 session->hosturl, group_uri, session->group,
728 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
729 friends_uri, session->page);
732 sprintf(endpoint, "%s%s%s.xml", session->hosturl,
733 retweet_uri, session->retweet);
740 dbg("%s\n", endpoint);
741 if (!session->dry_run) {
743 req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC,
744 NULL, session->consumer_key,
745 session->consumer_secret,
746 session->access_token_key,
747 session->access_token_secret);
748 reply = oauth_http_post(req_url, postarg);
750 req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
751 session->consumer_key,
752 session->consumer_secret,
753 session->access_token_key,
754 session->access_token_secret);
755 reply = oauth_http_get(req_url, postarg);
758 dbg("%s\n", req_url);
765 fprintf(stderr, "Error retrieving from URL (%s)\n", endpoint);
769 if ((session->action != ACTION_UPDATE) &&
770 (session->action != ACTION_RETWEET))
771 parse_timeline(reply, session);
776 static void log_session(struct session *session, int retval)
781 /* Only log something if we have a log file set */
782 if (!session->logfile)
785 filename = alloca(strlen(session->homedir) +
786 strlen(session->logfile) + 3);
788 sprintf(filename, "%s/%s", session->homedir, session->logfile);
790 log_file = fopen(filename, "a+");
791 if (log_file == NULL)
794 switch (session->action) {
797 fprintf(log_file, "%s: host=%s tweet failed\n",
798 session->time, session->hostname);
800 fprintf(log_file, "%s: host=%s tweet=%s\n",
801 session->time, session->hostname,
805 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
806 session->time, session->hostname);
809 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
810 session->time, session->hostname, session->user);
813 fprintf(log_file, "%s: host=%s retrieving replies\n",
814 session->time, session->hostname);
817 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
818 session->time, session->hostname);
821 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
822 session->time, session->hostname);
831 static char *get_string_from_stdin(void)
836 string = zalloc(1000);
840 if (!fgets(string, 999, stdin)) {
844 temp = strchr(string, '\n');
850 static void read_password(char *buf, size_t len, char *host)
860 tp.c_lflag &= (~ECHO);
861 tcsetattr(0, TCSANOW, &tp);
863 fprintf(stdout, "Enter password for %s: ", host);
866 retval = scanf("%79s", pwd);
868 fprintf(stdout, "\n");
870 tcsetattr(0, TCSANOW, &old);
872 strncpy(buf, pwd, len);
876 static int find_urls(const char *tweet, int **pranges)
879 * magic obtained from
880 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
882 static const char *re_magic =
883 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
884 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
885 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
889 int ovector[10] = {0,};
890 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
891 int startoffset, tweetlen;
895 int *ranges = malloc(sizeof(int) * rbound);
897 re = pcre_compile(re_magic,
898 PCRE_NO_AUTO_CAPTURE,
899 &errptr, &erroffset, NULL);
901 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
905 tweetlen = strlen(tweet);
906 for (startoffset = 0; startoffset < tweetlen; ) {
908 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
910 if (rc == PCRE_ERROR_NOMATCH)
914 fprintf(stderr, "pcre_exec @%u: %s\n",
919 for (i = 0; i < rc; i += 2) {
920 if ((rcount+2) == rbound) {
922 ranges = realloc(ranges, sizeof(int) * rbound);
925 ranges[rcount++] = ovector[i];
926 ranges[rcount++] = ovector[i+1];
929 startoffset = ovector[1];
939 * bidirectional popen() call
941 * @param rwepipe - int array of size three
942 * @param exe - program to run
943 * @param argv - argument list
944 * @return pid or -1 on error
946 * The caller passes in an array of three integers (rwepipe), on successful
947 * execution it can then write to element 0 (stdin of exe), and read from
948 * element 1 (stdout) and 2 (stderr).
950 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
980 } else if (pid == 0) {
992 execvp(exe, (char **)argv);
1012 static int pcloseRWE(int pid, int *rwepipe)
1018 rc = waitpid(pid, &status, 0);
1022 static char *shrink_one_url(int *rwepipe, char *big)
1024 int biglen = strlen(big);
1029 rc = dprintf(rwepipe[0], "%s\n", big);
1033 smalllen = biglen + 128;
1034 small = malloc(smalllen);
1038 rc = read(rwepipe[1], small, smalllen);
1039 if (rc < 0 || rc > biglen)
1040 goto error_free_small;
1042 if (strncmp(small, "http://", 7))
1043 goto error_free_small;
1046 while (smalllen && isspace(small[smalllen-1]))
1047 small[--smalllen] = 0;
1057 static char *shrink_urls(char *text)
1064 const char *const shrink_args[] = {
1070 int inlen = strlen(text);
1072 dbg("before len=%u\n", inlen);
1074 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1078 rcount = find_urls(text, &ranges);
1082 for (i = 0; i < rcount; i += 2) {
1083 int url_start = ranges[i];
1084 int url_end = ranges[i+1];
1085 int long_url_len = url_end - url_start;
1086 char *url = strndup(text + url_start, long_url_len);
1088 int not_url_len = url_start - inofs;
1090 dbg("long url[%u]: %s\n", long_url_len, url);
1091 url = shrink_one_url(shrink_pipe, url);
1092 short_url_len = url ? strlen(url) : 0;
1093 dbg("short url[%u]: %s\n", short_url_len, url);
1095 if (!url || short_url_len >= long_url_len) {
1096 /* The short url ended up being too long
1099 strncpy(text + outofs, text + inofs,
1100 not_url_len + long_url_len);
1102 inofs += not_url_len + long_url_len;
1103 outofs += not_url_len + long_url_len;
1106 /* copy the unmodified block */
1107 strncpy(text + outofs, text + inofs, not_url_len);
1108 inofs += not_url_len;
1109 outofs += not_url_len;
1111 /* copy the new url */
1112 strncpy(text + outofs, url, short_url_len);
1113 inofs += long_url_len;
1114 outofs += short_url_len;
1120 /* copy the last block after the last match */
1122 int tail = inlen - inofs;
1124 strncpy(text + outofs, text + inofs, tail);
1131 (void)pcloseRWE(shrink_pid, shrink_pipe);
1134 dbg("after len=%u\n", outofs);
1138 int main(int argc, char *argv[], char *envp[])
1140 static const struct option options[] = {
1141 { "debug", 0, NULL, 'd' },
1142 { "verbose", 0, NULL, 'V' },
1143 { "account", 1, NULL, 'a' },
1144 { "password", 1, NULL, 'p' },
1145 { "host", 1, NULL, 'H' },
1146 { "proxy", 1, NULL, 'P' },
1147 { "action", 1, NULL, 'A' },
1148 { "user", 1, NULL, 'u' },
1149 { "group", 1, NULL, 'G' },
1150 { "logfile", 1, NULL, 'L' },
1151 { "shrink-urls", 0, NULL, 's' },
1152 { "help", 0, NULL, 'h' },
1153 { "bash", 0, NULL, 'b' },
1154 { "background", 0, NULL, 'B' },
1155 { "dry-run", 0, NULL, 'n' },
1156 { "page", 1, NULL, 'g' },
1157 { "column", 1, NULL, 'o' },
1158 { "version", 0, NULL, 'v' },
1159 { "config", 1, NULL, 'c' },
1160 { "replyto", 1, NULL, 'r' },
1161 { "retweet", 1, NULL, 'w' },
1164 struct session *session;
1167 static char password[80];
1172 const char *config_file;
1178 session = session_alloc();
1180 fprintf(stderr, "no more memory...\n");
1184 /* get the current time so that we can log it later */
1186 session->time = strdup(ctime(&t));
1187 session->time[strlen(session->time)-1] = 0x00;
1190 * Get the home directory so we can try to find a config file.
1191 * If we have no home dir set up, look in /etc/bti
1193 home = getenv("HOME");
1195 /* We have a home dir, so this might be a user */
1196 session->homedir = strdup(home);
1197 config_file = config_user_default;
1199 session->homedir = strdup("");
1200 config_file = config_default;
1203 /* set up a default config file location (traditionally ~/.bti) */
1204 session->configfile = zalloc(strlen(session->homedir) + strlen(config_file) + 7);
1205 sprintf(session->configfile, "%s/%s", session->homedir, config_file);
1207 /* Set environment variables first, before reading command line options
1208 * or config file values. */
1209 http_proxy = getenv("http_proxy");
1212 free(session->proxy);
1213 session->proxy = strdup(http_proxy);
1214 dbg("http_proxy = %s\n", session->proxy);
1217 bti_parse_configfile(session);
1220 option = getopt_long_only(argc, argv,
1221 "dp:P:H:a:A:u:c:hg:o:G:sr:nVvw:",
1230 session->verbose = 1;
1233 if (session->account)
1234 free(session->account);
1235 session->account = strdup(optarg);
1236 dbg("account = %s\n", session->account);
1239 page_nr = atoi(optarg);
1240 dbg("page = %d\n", page_nr);
1241 session->page = page_nr;
1244 session->column_output = atoi(optarg);
1245 dbg("column_output = %d\n", session->column_output);
1248 session->replyto = strdup(optarg);
1249 dbg("in_reply_to_status_id = %s\n", session->replyto);
1252 session->retweet = strdup(optarg);
1253 dbg("Retweet ID = %s\n", session->retweet);
1256 if (session->password)
1257 free(session->password);
1258 session->password = strdup(optarg);
1259 dbg("password = %s\n", session->password);
1263 free(session->proxy);
1264 session->proxy = strdup(optarg);
1265 dbg("proxy = %s\n", session->proxy);
1268 if (strcasecmp(optarg, "update") == 0)
1269 session->action = ACTION_UPDATE;
1270 else if (strcasecmp(optarg, "friends") == 0)
1271 session->action = ACTION_FRIENDS;
1272 else if (strcasecmp(optarg, "user") == 0)
1273 session->action = ACTION_USER;
1274 else if (strcasecmp(optarg, "replies") == 0)
1275 session->action = ACTION_REPLIES;
1276 else if (strcasecmp(optarg, "public") == 0)
1277 session->action = ACTION_PUBLIC;
1278 else if (strcasecmp(optarg, "group") == 0)
1279 session->action = ACTION_GROUP;
1280 else if (strcasecmp(optarg, "retweet") == 0)
1281 session->action = ACTION_RETWEET;
1283 session->action = ACTION_UNKNOWN;
1284 dbg("action = %d\n", session->action);
1288 free(session->user);
1289 session->user = strdup(optarg);
1290 dbg("user = %s\n", session->user);
1295 free(session->group);
1296 session->group = strdup(optarg);
1297 dbg("group = %s\n", session->group);
1300 if (session->logfile)
1301 free(session->logfile);
1302 session->logfile = strdup(optarg);
1303 dbg("logfile = %s\n", session->logfile);
1306 session->shrink_urls = 1;
1309 if (session->hosturl)
1310 free(session->hosturl);
1311 if (session->hostname)
1312 free(session->hostname);
1313 if (strcasecmp(optarg, "twitter") == 0) {
1314 session->host = HOST_TWITTER;
1315 session->hosturl = strdup(twitter_host);
1316 session->hostname = strdup(twitter_name);
1317 } else if (strcasecmp(optarg, "identica") == 0) {
1318 session->host = HOST_IDENTICA;
1319 session->hosturl = strdup(identica_host);
1320 session->hostname = strdup(identica_name);
1322 session->host = HOST_CUSTOM;
1323 session->hosturl = strdup(optarg);
1324 session->hostname = strdup(optarg);
1326 dbg("host = %d\n", session->host);
1330 /* fall-through intended */
1332 session->background = 1;
1335 if (session->configfile)
1336 free(session->configfile);
1337 session->configfile = strdup(optarg);
1338 dbg("configfile = %s\n", session->configfile);
1341 * read the config file now. Yes, this could override
1342 * previously set options from the command line, but
1343 * the user asked for it...
1345 bti_parse_configfile(session);
1351 session->dry_run = 1;
1362 session_readline_init(session);
1364 * Show the version to make it easier to determine what
1370 if (session->host == HOST_TWITTER) {
1371 if (!session->consumer_key || !session->consumer_secret) {
1372 if (session->action == ACTION_USER ||
1373 session->action == ACTION_PUBLIC) {
1375 * Some actions may still work without
1381 "Twitter no longer supports HTTP basic authentication.\n"
1382 "Both consumer key, and consumer secret are required"
1383 " for bti in order to behave as an OAuth consumer.\n");
1387 if (session->action == ACTION_GROUP) {
1388 fprintf(stderr, "Groups only work in Identi.ca.\n");
1392 if (!session->consumer_key || !session->consumer_secret)
1393 session->no_oauth = 1;
1396 if (session->no_oauth) {
1397 if (!session->account) {
1398 fprintf(stdout, "Enter account for %s: ",
1400 session->account = session->readline(NULL);
1402 if (!session->password) {
1403 read_password(password, sizeof(password),
1405 session->password = strdup(password);
1407 } else if (!session->guest) {
1408 if (!session->access_token_key ||
1409 !session->access_token_secret) {
1410 request_access_token(session);
1415 if (session->action == ACTION_UNKNOWN) {
1416 fprintf(stderr, "Unknown action, valid actions are:\n"
1417 "'update', 'friends', 'public', 'replies', 'group' or 'user'.\n");
1421 if (session->action == ACTION_GROUP && !session->group) {
1422 fprintf(stdout, "Enter group name: ");
1423 session->group = session->readline(NULL);
1426 if (session->action == ACTION_RETWEET) {
1427 if (!session->retweet) {
1430 fprintf(stdout, "Status ID to retweet: ");
1431 rtid = get_string_from_stdin();
1432 session->retweet = zalloc(strlen(rtid) + 10);
1433 sprintf(session->retweet, "%s", rtid);
1437 if (!session->retweet || strlen(session->retweet) == 0) {
1438 dbg("no retweet?\n");
1442 dbg("retweet ID = %s\n", session->retweet);
1445 if (session->action == ACTION_UPDATE) {
1446 if (session->background || !session->interactive)
1447 tweet = get_string_from_stdin();
1449 tweet = session->readline("tweet: ");
1450 if (!tweet || strlen(tweet) == 0) {
1455 if (session->shrink_urls)
1456 tweet = shrink_urls(tweet);
1458 session->tweet = zalloc(strlen(tweet) + 10);
1460 sprintf(session->tweet, "%c %s",
1461 getuid() ? '$' : '#', tweet);
1463 sprintf(session->tweet, "%s", tweet);
1466 dbg("tweet = %s\n", session->tweet);
1469 if (session->page == 0)
1471 dbg("config file = %s\n", session->configfile);
1472 dbg("host = %d\n", session->host);
1473 dbg("action = %d\n", session->action);
1475 /* fork ourself so that the main shell can get on
1476 * with it's life as we try to connect and handle everything
1478 if (session->background) {
1481 dbg("child is %d\n", child);
1486 retval = send_request(session);
1487 if (retval && !session->background)
1488 fprintf(stderr, "operation failed\n");
1490 log_session(session, retval);
1492 session_readline_cleanup(session);
1493 session_free(session);