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[] = "https://identi.ca/api/oauth/request_token?oauth_callback=oob";
262 static const char identica_access_token_uri[] = "https://identi.ca/api/oauth/access_token";
263 static const char identica_authorize_uri[] = "https://identi.ca/api/oauth/authorize?oauth_token=";
264 static const char custom_request_token_uri[] = "/../oauth/request_token?oauth_callback=oob";
265 static const char custom_access_token_uri[] = "/../oauth/access_token";
266 static const char custom_authorize_uri[] = "/../oauth/authorize?oauth_token=";
268 static const char user_uri[] = "/user_timeline/";
269 static const char update_uri[] = "/update.xml";
270 static const char public_uri[] = "/public_timeline.xml";
271 static const char friends_uri[] = "/friends_timeline.xml";
272 static const char mentions_uri[] = "/mentions.xml";
273 static const char replies_uri[] = "/replies.xml";
274 static const char retweet_uri[] = "/retweet/";
275 static const char group_uri[] = "/../statusnet/groups/timeline/";
277 static const char config_default[] = "/etc/bti";
278 static const char config_user_default[] = ".bti";
280 static CURL *curl_init(void)
284 curl = curl_easy_init();
286 fprintf(stderr, "Can not init CURL!\n");
289 /* some ssl sanity checks on the connection we are making */
290 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
291 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
295 /* The final place data is sent to the screen/pty/tty */
296 static void bti_output_line(struct session *session, xmlChar *user,
297 xmlChar *id, xmlChar *created, xmlChar *text)
299 if (session->verbose)
300 printf("[%*s] {%s} (%.16s) %s\n", -session->column_output, user,
303 printf("[%*s] %s\n", -session->column_output, user, text);
306 static void parse_statuses(struct session *session,
307 xmlDocPtr doc, xmlNodePtr current)
309 xmlChar *text = NULL;
310 xmlChar *user = NULL;
311 xmlChar *created = NULL;
315 current = current->xmlChildrenNode;
316 while (current != NULL) {
317 if (current->type == XML_ELEMENT_NODE) {
318 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
319 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
320 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
321 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
322 if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
323 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
324 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
325 userinfo = current->xmlChildrenNode;
326 while (userinfo != NULL) {
327 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
330 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
332 userinfo = userinfo->next;
336 if (user && text && created && id) {
337 bti_output_line(session, user, id,
349 current = current->next;
355 static void parse_timeline(char *document, struct session *session)
360 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
361 NULL, XML_PARSE_NOERROR);
365 current = xmlDocGetRootElement(doc);
366 if (current == NULL) {
367 fprintf(stderr, "empty document\n");
372 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
373 fprintf(stderr, "unexpected document type\n");
378 current = current->xmlChildrenNode;
379 while (current != NULL) {
380 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
381 parse_statuses(session, doc, current);
382 current = current->next;
389 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
392 struct bti_curl_buffer *curl_buf = userp;
393 size_t buffer_size = size * nmemb;
396 if ((!buffer) || (!buffer_size) || (!curl_buf))
399 /* add to the data we already have */
400 temp = zalloc(curl_buf->length + buffer_size + 1);
404 memcpy(temp, curl_buf->data, curl_buf->length);
405 free(curl_buf->data);
406 curl_buf->data = temp;
407 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
408 curl_buf->length += buffer_size;
409 if (curl_buf->action)
410 parse_timeline(curl_buf->data, curl_buf->session);
412 dbg("%s\n", curl_buf->data);
417 static int parse_osp_reply(const char *reply, char **token, char **secret)
422 rc = oauth_split_url_parameters(reply, &rv);
423 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
424 if (rc == 2 || rc == 4) {
425 if (!strncmp(rv[0], "oauth_token=", 11) &&
426 !strncmp(rv[1], "oauth_token_secret=", 18)) {
428 *token = strdup(&(rv[0][12]));
430 *secret = strdup(&(rv[1][19]));
434 } else if (rc == 3) {
435 if (!strncmp(rv[1], "oauth_token=", 11) &&
436 !strncmp(rv[2], "oauth_token_secret=", 18)) {
438 *token = strdup(&(rv[1][12]));
440 *secret = strdup(&(rv[2][19]));
446 dbg("token: %s\n", *token);
447 dbg("secret: %s\n", *secret);
455 static int request_access_token(struct session *session)
457 char *post_params = NULL;
458 char *request_url = NULL;
461 char *at_secret = NULL;
462 char *verifier = NULL;
469 if (session->host == HOST_TWITTER)
470 request_url = oauth_sign_url2(
471 twitter_request_token_uri, NULL,
472 OA_HMAC, NULL, session->consumer_key,
473 session->consumer_secret, NULL, NULL);
474 else if (session->host == HOST_IDENTICA)
475 request_url = oauth_sign_url2(
476 identica_request_token_uri, NULL,
477 OA_HMAC, NULL, session->consumer_key,
478 session->consumer_secret, NULL, NULL);
480 sprintf(token_uri, "%s%s",
481 session->hosturl, custom_request_token_uri);
482 request_url = oauth_sign_url2(
484 OA_HMAC, NULL, session->consumer_key,
485 session->consumer_secret, NULL, NULL);
487 reply = oauth_http_get(request_url, post_params);
498 if (parse_osp_reply(reply, &at_key, &at_secret))
504 "Please open the following link in your browser, and "
505 "allow 'bti' to access your account. Then paste "
506 "back the provided PIN in here.\n");
507 if (session->host == HOST_TWITTER) {
508 fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
509 verifier = session->readline(NULL);
510 sprintf(at_uri, "%s?oauth_verifier=%s",
511 twitter_access_token_uri, verifier);
512 } else if (session->host == HOST_IDENTICA) {
513 fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key);
514 verifier = session->readline(NULL);
515 sprintf(at_uri, "%s?oauth_verifier=%s",
516 identica_access_token_uri, verifier);
518 fprintf(stdout, "%s%s%s\nPIN: ",
519 session->hosturl, custom_authorize_uri, at_key);
520 verifier = session->readline(NULL);
521 sprintf(at_uri, "%s%s?oauth_verifier=%s",
522 session->hosturl, custom_access_token_uri, verifier);
524 request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
525 session->consumer_key,
526 session->consumer_secret,
528 reply = oauth_http_get(request_url, post_params);
533 if (parse_osp_reply(reply, &at_key, &at_secret))
539 "Please put these two lines in your bti "
540 "configuration file (%s):\n"
541 "access_token_key=%s\n"
542 "access_token_secret=%s\n",
543 session->configfile, at_key, at_secret);
548 static int send_request(struct session *session)
551 char user_password[500];
553 struct bti_curl_buffer *curl_buf;
556 struct curl_httppost *formpost = NULL;
557 struct curl_httppost *lastptr = NULL;
558 struct curl_slist *slist = NULL;
559 char *req_url = NULL;
561 char *postarg = NULL;
562 char *escaped_tweet = NULL;
568 if (!session->hosturl)
569 session->hosturl = strdup(twitter_host);
571 if (session->no_oauth || session->guest) {
572 curl_buf = bti_curl_buffer_alloc(session->action);
575 curl_buf->session = session;
579 bti_curl_buffer_free(curl_buf);
583 if (!session->hosturl)
584 session->hosturl = strdup(twitter_host);
586 switch (session->action) {
588 snprintf(user_password, sizeof(user_password), "%s:%s",
589 session->account, session->password);
590 snprintf(data, sizeof(data), "status=\"%s\"",
592 curl_formadd(&formpost, &lastptr,
593 CURLFORM_COPYNAME, "status",
594 CURLFORM_COPYCONTENTS, session->tweet,
597 curl_formadd(&formpost, &lastptr,
598 CURLFORM_COPYNAME, "source",
599 CURLFORM_COPYCONTENTS, "bti",
602 if (session->replyto)
603 curl_formadd(&formpost, &lastptr,
605 "in_reply_to_status_id",
606 CURLFORM_COPYCONTENTS,
610 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
611 slist = curl_slist_append(slist, "Expect:");
612 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
614 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
615 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
616 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
620 snprintf(user_password, sizeof(user_password), "%s:%s",
621 session->account, session->password);
622 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
623 friends_uri, session->page);
624 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
625 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
629 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
630 user_uri, session->user, session->page);
631 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
635 snprintf(user_password, sizeof(user_password), "%s:%s",
636 session->account, session->password);
637 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
638 replies_uri, session->page);
639 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
640 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
644 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
645 public_uri, session->page);
646 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
650 sprintf(endpoint, "%s%s%s.xml?page=%d",
651 session->hosturl, group_uri, session->group,
653 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
661 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
664 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
666 dbg("user_password = %s\n", user_password);
667 dbg("data = %s\n", data);
668 dbg("proxy = %s\n", session->proxy);
670 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
671 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
672 if (!session->dry_run) {
673 res = curl_easy_perform(curl);
674 if (!session->background) {
679 fprintf(stderr, "error(%d) trying to "
680 "perform operation\n", res);
681 curl_easy_cleanup(curl);
682 if (session->action == ACTION_UPDATE)
683 curl_formfree(formpost);
684 bti_curl_buffer_free(curl_buf);
688 doc = xmlReadMemory(curl_buf->data,
690 "response.xml", NULL,
693 curl_easy_cleanup(curl);
694 if (session->action == ACTION_UPDATE)
695 curl_formfree(formpost);
696 bti_curl_buffer_free(curl_buf);
700 current = xmlDocGetRootElement(doc);
701 if (current == NULL) {
702 fprintf(stderr, "empty document\n");
704 curl_easy_cleanup(curl);
705 if (session->action == ACTION_UPDATE)
706 curl_formfree(formpost);
707 bti_curl_buffer_free(curl_buf);
711 if (xmlStrcmp(current->name, (const xmlChar *)"status")) {
712 fprintf(stderr, "unexpected document type\n");
714 curl_easy_cleanup(curl);
715 if (session->action == ACTION_UPDATE)
716 curl_formfree(formpost);
717 bti_curl_buffer_free(curl_buf);
725 curl_easy_cleanup(curl);
726 if (session->action == ACTION_UPDATE)
727 curl_formfree(formpost);
728 bti_curl_buffer_free(curl_buf);
730 switch (session->action) {
732 escaped_tweet = oauth_url_escape(session->tweet);
733 if (session->replyto) {
735 "%s%s?status=%s&in_reply_to_status_id=%s",
736 session->hosturl, update_uri,
737 escaped_tweet, session->replyto);
739 sprintf(endpoint, "%s%s?status=%s",
740 session->hosturl, update_uri,
747 sprintf(endpoint, "%s%s%s.xml?page=%d",
748 session->hosturl, user_uri, session->user,
752 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
753 mentions_uri, session->page);
756 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
757 public_uri, session->page);
760 sprintf(endpoint, "%s%s%s.xml?page=%d",
761 session->hosturl, group_uri, session->group,
765 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
766 friends_uri, session->page);
769 sprintf(endpoint, "%s%s%s.xml", session->hosturl,
770 retweet_uri, session->retweet);
777 dbg("%s\n", endpoint);
778 if (!session->dry_run) {
780 req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC,
781 NULL, session->consumer_key,
782 session->consumer_secret,
783 session->access_token_key,
784 session->access_token_secret);
785 reply = oauth_http_post(req_url, postarg);
787 req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
788 session->consumer_key,
789 session->consumer_secret,
790 session->access_token_key,
791 session->access_token_secret);
792 reply = oauth_http_get(req_url, postarg);
795 dbg("%s\n", req_url);
802 fprintf(stderr, "Error retrieving from URL (%s)\n", endpoint);
806 if ((session->action != ACTION_UPDATE) &&
807 (session->action != ACTION_RETWEET))
808 parse_timeline(reply, session);
813 static void log_session(struct session *session, int retval)
818 /* Only log something if we have a log file set */
819 if (!session->logfile)
822 filename = alloca(strlen(session->homedir) +
823 strlen(session->logfile) + 3);
825 sprintf(filename, "%s/%s", session->homedir, session->logfile);
827 log_file = fopen(filename, "a+");
828 if (log_file == NULL)
831 switch (session->action) {
834 fprintf(log_file, "%s: host=%s tweet failed\n",
835 session->time, session->hostname);
837 fprintf(log_file, "%s: host=%s tweet=%s\n",
838 session->time, session->hostname,
842 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
843 session->time, session->hostname);
846 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
847 session->time, session->hostname, session->user);
850 fprintf(log_file, "%s: host=%s retrieving replies\n",
851 session->time, session->hostname);
854 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
855 session->time, session->hostname);
858 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
859 session->time, session->hostname);
868 static char *get_string_from_stdin(void)
873 string = zalloc(1000);
877 if (!fgets(string, 999, stdin)) {
881 temp = strchr(string, '\n');
887 static void read_password(char *buf, size_t len, char *host)
896 tp.c_lflag &= (~ECHO);
897 tcsetattr(0, TCSANOW, &tp);
899 fprintf(stdout, "Enter password for %s: ", host);
904 * I'd like to do something with the return value here, but really,
907 (void)scanf("%79s", pwd);
910 fprintf(stdout, "\n");
912 tcsetattr(0, TCSANOW, &old);
914 strncpy(buf, pwd, len);
918 static int find_urls(const char *tweet, int **pranges)
921 * magic obtained from
922 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
924 static const char *re_magic =
925 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
926 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
927 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
931 int ovector[10] = {0,};
932 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
933 int startoffset, tweetlen;
937 int *ranges = malloc(sizeof(int) * rbound);
939 re = pcre_compile(re_magic,
940 PCRE_NO_AUTO_CAPTURE,
941 &errptr, &erroffset, NULL);
943 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
947 tweetlen = strlen(tweet);
948 for (startoffset = 0; startoffset < tweetlen; ) {
950 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
952 if (rc == PCRE_ERROR_NOMATCH)
956 fprintf(stderr, "pcre_exec @%u: %s\n",
961 for (i = 0; i < rc; i += 2) {
962 if ((rcount+2) == rbound) {
964 ranges = realloc(ranges, sizeof(int) * rbound);
967 ranges[rcount++] = ovector[i];
968 ranges[rcount++] = ovector[i+1];
971 startoffset = ovector[1];
981 * bidirectional popen() call
983 * @param rwepipe - int array of size three
984 * @param exe - program to run
985 * @param argv - argument list
986 * @return pid or -1 on error
988 * The caller passes in an array of three integers (rwepipe), on successful
989 * execution it can then write to element 0 (stdin of exe), and read from
990 * element 1 (stdout) and 2 (stderr).
992 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
1019 rwepipe[1] = out[0];
1020 rwepipe[2] = err[0];
1022 } else if (pid == 0) {
1034 execvp(exe, (char **)argv);
1054 static int pcloseRWE(int pid, int *rwepipe)
1060 (void)waitpid(pid, &status, 0);
1064 static char *shrink_one_url(int *rwepipe, char *big)
1066 int biglen = strlen(big);
1071 rc = dprintf(rwepipe[0], "%s\n", big);
1075 smalllen = biglen + 128;
1076 small = malloc(smalllen);
1080 rc = read(rwepipe[1], small, smalllen);
1081 if (rc < 0 || rc > biglen)
1082 goto error_free_small;
1084 if (strncmp(small, "http://", 7))
1085 goto error_free_small;
1088 while (smalllen && isspace(small[smalllen-1]))
1089 small[--smalllen] = 0;
1099 static char *shrink_urls(char *text)
1106 const char *const shrink_args[] = {
1112 int inlen = strlen(text);
1114 dbg("before len=%u\n", inlen);
1116 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1120 rcount = find_urls(text, &ranges);
1124 for (i = 0; i < rcount; i += 2) {
1125 int url_start = ranges[i];
1126 int url_end = ranges[i+1];
1127 int long_url_len = url_end - url_start;
1128 char *url = strndup(text + url_start, long_url_len);
1130 int not_url_len = url_start - inofs;
1132 dbg("long url[%u]: %s\n", long_url_len, url);
1133 url = shrink_one_url(shrink_pipe, url);
1134 short_url_len = url ? strlen(url) : 0;
1135 dbg("short url[%u]: %s\n", short_url_len, url);
1137 if (!url || short_url_len >= long_url_len) {
1138 /* The short url ended up being too long
1141 strncpy(text + outofs, text + inofs,
1142 not_url_len + long_url_len);
1144 inofs += not_url_len + long_url_len;
1145 outofs += not_url_len + long_url_len;
1148 /* copy the unmodified block */
1149 strncpy(text + outofs, text + inofs, not_url_len);
1150 inofs += not_url_len;
1151 outofs += not_url_len;
1153 /* copy the new url */
1154 strncpy(text + outofs, url, short_url_len);
1155 inofs += long_url_len;
1156 outofs += short_url_len;
1162 /* copy the last block after the last match */
1164 int tail = inlen - inofs;
1166 strncpy(text + outofs, text + inofs, tail);
1173 (void)pcloseRWE(shrink_pid, shrink_pipe);
1176 dbg("after len=%u\n", outofs);
1180 int main(int argc, char *argv[], char *envp[])
1182 static const struct option options[] = {
1183 { "debug", 0, NULL, 'd' },
1184 { "verbose", 0, NULL, 'V' },
1185 { "account", 1, NULL, 'a' },
1186 { "password", 1, NULL, 'p' },
1187 { "host", 1, NULL, 'H' },
1188 { "proxy", 1, NULL, 'P' },
1189 { "action", 1, NULL, 'A' },
1190 { "user", 1, NULL, 'u' },
1191 { "group", 1, NULL, 'G' },
1192 { "logfile", 1, NULL, 'L' },
1193 { "shrink-urls", 0, NULL, 's' },
1194 { "help", 0, NULL, 'h' },
1195 { "bash", 0, NULL, 'b' },
1196 { "background", 0, NULL, 'B' },
1197 { "dry-run", 0, NULL, 'n' },
1198 { "page", 1, NULL, 'g' },
1199 { "column", 1, NULL, 'o' },
1200 { "version", 0, NULL, 'v' },
1201 { "config", 1, NULL, 'c' },
1202 { "replyto", 1, NULL, 'r' },
1203 { "retweet", 1, NULL, 'w' },
1206 struct session *session;
1209 static char password[80];
1214 const char *config_file;
1220 session = session_alloc();
1222 fprintf(stderr, "no more memory...\n");
1226 /* get the current time so that we can log it later */
1228 session->time = strdup(ctime(&t));
1229 session->time[strlen(session->time)-1] = 0x00;
1232 * Get the home directory so we can try to find a config file.
1233 * If we have no home dir set up, look in /etc/bti
1235 home = getenv("HOME");
1237 /* We have a home dir, so this might be a user */
1238 session->homedir = strdup(home);
1239 config_file = config_user_default;
1241 session->homedir = strdup("");
1242 config_file = config_default;
1245 /* set up a default config file location (traditionally ~/.bti) */
1246 session->configfile = zalloc(strlen(session->homedir) + strlen(config_file) + 7);
1247 sprintf(session->configfile, "%s/%s", session->homedir, config_file);
1249 /* Set environment variables first, before reading command line options
1250 * or config file values. */
1251 http_proxy = getenv("http_proxy");
1254 free(session->proxy);
1255 session->proxy = strdup(http_proxy);
1256 dbg("http_proxy = %s\n", session->proxy);
1259 bti_parse_configfile(session);
1262 option = getopt_long_only(argc, argv,
1263 "dp:P:H:a:A:u:c:hg:o:G:sr:nVvw:",
1272 session->verbose = 1;
1275 if (session->account)
1276 free(session->account);
1277 session->account = strdup(optarg);
1278 dbg("account = %s\n", session->account);
1281 page_nr = atoi(optarg);
1282 dbg("page = %d\n", page_nr);
1283 session->page = page_nr;
1286 session->column_output = atoi(optarg);
1287 dbg("column_output = %d\n", session->column_output);
1290 session->replyto = strdup(optarg);
1291 dbg("in_reply_to_status_id = %s\n", session->replyto);
1294 session->retweet = strdup(optarg);
1295 dbg("Retweet ID = %s\n", session->retweet);
1298 if (session->password)
1299 free(session->password);
1300 session->password = strdup(optarg);
1301 dbg("password = %s\n", session->password);
1305 free(session->proxy);
1306 session->proxy = strdup(optarg);
1307 dbg("proxy = %s\n", session->proxy);
1310 if (strcasecmp(optarg, "update") == 0)
1311 session->action = ACTION_UPDATE;
1312 else if (strcasecmp(optarg, "friends") == 0)
1313 session->action = ACTION_FRIENDS;
1314 else if (strcasecmp(optarg, "user") == 0)
1315 session->action = ACTION_USER;
1316 else if (strcasecmp(optarg, "replies") == 0)
1317 session->action = ACTION_REPLIES;
1318 else if (strcasecmp(optarg, "public") == 0)
1319 session->action = ACTION_PUBLIC;
1320 else if (strcasecmp(optarg, "group") == 0)
1321 session->action = ACTION_GROUP;
1322 else if (strcasecmp(optarg, "retweet") == 0)
1323 session->action = ACTION_RETWEET;
1325 session->action = ACTION_UNKNOWN;
1326 dbg("action = %d\n", session->action);
1330 free(session->user);
1331 session->user = strdup(optarg);
1332 dbg("user = %s\n", session->user);
1337 free(session->group);
1338 session->group = strdup(optarg);
1339 dbg("group = %s\n", session->group);
1342 if (session->logfile)
1343 free(session->logfile);
1344 session->logfile = strdup(optarg);
1345 dbg("logfile = %s\n", session->logfile);
1348 session->shrink_urls = 1;
1351 if (session->hosturl)
1352 free(session->hosturl);
1353 if (session->hostname)
1354 free(session->hostname);
1355 if (strcasecmp(optarg, "twitter") == 0) {
1356 session->host = HOST_TWITTER;
1357 session->hosturl = strdup(twitter_host);
1358 session->hostname = strdup(twitter_name);
1359 } else if (strcasecmp(optarg, "identica") == 0) {
1360 session->host = HOST_IDENTICA;
1361 session->hosturl = strdup(identica_host);
1362 session->hostname = strdup(identica_name);
1364 session->host = HOST_CUSTOM;
1365 session->hosturl = strdup(optarg);
1366 session->hostname = strdup(optarg);
1368 dbg("host = %d\n", session->host);
1372 /* fall-through intended */
1374 session->background = 1;
1377 if (session->configfile)
1378 free(session->configfile);
1379 session->configfile = strdup(optarg);
1380 dbg("configfile = %s\n", session->configfile);
1383 * read the config file now. Yes, this could override
1384 * previously set options from the command line, but
1385 * the user asked for it...
1387 bti_parse_configfile(session);
1393 session->dry_run = 1;
1404 session_readline_init(session);
1406 * Show the version to make it easier to determine what
1412 if (session->host == HOST_TWITTER) {
1413 if (!session->consumer_key || !session->consumer_secret) {
1414 if (session->action == ACTION_USER ||
1415 session->action == ACTION_PUBLIC) {
1417 * Some actions may still work without
1423 "Twitter no longer supports HTTP basic authentication.\n"
1424 "Both consumer key, and consumer secret are required"
1425 " for bti in order to behave as an OAuth consumer.\n");
1429 if (session->action == ACTION_GROUP) {
1430 fprintf(stderr, "Groups only work in Identi.ca.\n");
1434 if (!session->consumer_key || !session->consumer_secret)
1435 session->no_oauth = 1;
1438 if (session->no_oauth) {
1439 if (!session->account) {
1440 fprintf(stdout, "Enter account for %s: ",
1442 session->account = session->readline(NULL);
1444 if (!session->password) {
1445 read_password(password, sizeof(password),
1447 session->password = strdup(password);
1449 } else if (!session->guest) {
1450 if (!session->access_token_key ||
1451 !session->access_token_secret) {
1452 request_access_token(session);
1457 if (session->action == ACTION_UNKNOWN) {
1458 fprintf(stderr, "Unknown action, valid actions are:\n"
1459 "'update', 'friends', 'public', 'replies', 'group' or 'user'.\n");
1463 if (session->action == ACTION_GROUP && !session->group) {
1464 fprintf(stdout, "Enter group name: ");
1465 session->group = session->readline(NULL);
1468 if (session->action == ACTION_RETWEET) {
1469 if (!session->retweet) {
1472 fprintf(stdout, "Status ID to retweet: ");
1473 rtid = get_string_from_stdin();
1474 session->retweet = zalloc(strlen(rtid) + 10);
1475 sprintf(session->retweet, "%s", rtid);
1479 if (!session->retweet || strlen(session->retweet) == 0) {
1480 dbg("no retweet?\n");
1484 dbg("retweet ID = %s\n", session->retweet);
1487 if (session->action == ACTION_UPDATE) {
1488 if (session->background || !session->interactive)
1489 tweet = get_string_from_stdin();
1491 tweet = session->readline("tweet: ");
1492 if (!tweet || strlen(tweet) == 0) {
1497 if (session->shrink_urls)
1498 tweet = shrink_urls(tweet);
1500 session->tweet = zalloc(strlen(tweet) + 10);
1502 sprintf(session->tweet, "%c %s",
1503 getuid() ? '$' : '#', tweet);
1505 sprintf(session->tweet, "%s", tweet);
1508 dbg("tweet = %s\n", session->tweet);
1511 if (session->page == 0)
1513 dbg("config file = %s\n", session->configfile);
1514 dbg("host = %d\n", session->host);
1515 dbg("action = %d\n", session->action);
1517 /* fork ourself so that the main shell can get on
1518 * with it's life as we try to connect and handle everything
1520 if (session->background) {
1523 dbg("child is %d\n", child);
1528 retval = send_request(session);
1529 if (retval && !session->background)
1530 fprintf(stderr, "operation failed\n");
1532 log_session(session, retval);
1534 session_readline_cleanup(session);
1535 session_free(session);