2 * Copyright (C) 2008-2010 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__ , \
78 char *consumer_secret;
79 char *access_token_key;
80 char *access_token_secret;
101 void *readline_handle;
102 char *(*readline)(const char *);
105 struct bti_curl_buffer {
111 static void display_help(void)
113 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n"
118 " --account accountname\n"
119 " --password password\n"
121 " ('update', 'friends', 'public', 'replies', or 'user')\n"
122 " --user screenname\n"
123 " --group groupname\n"
124 " --proxy PROXY:PORT\n"
126 " --logfile logfile\n"
127 " --config configfile\n"
130 " --page PAGENUMBER\n"
137 " --help\n", VERSION);
140 static void display_version(void)
142 fprintf(stdout, "bti - version %s\n", VERSION);
145 static char *get_string(const char *name)
150 string = zalloc(1000);
154 fprintf(stdout, "%s", name);
155 if (!fgets(string, 999, stdin))
157 temp = strchr(string, '\n');
164 * Try to get a handle to a readline function from a variety of different
165 * libraries. If nothing is present on the system, then fall back to an
168 * Logic originally based off of code in the e2fsutils package in the
169 * lib/ss/get_readline.c file, which is licensed under the MIT license.
171 * This keeps us from having to relicense the bti codebase if readline
172 * ever changes its license, as there is no link-time dependancy.
173 * It is a run-time thing only, and we handle any readline-like library
174 * in the same manner, making bti not be a derivative work of any
177 static void session_readline_init(struct session *session)
179 /* Libraries we will try to use for readline/editline functionality */
180 const char *libpath = "libreadline.so.6:libreadline.so.5:"
181 "libreadline.so.4:libreadline.so:libedit.so.2:"
182 "libedit.so:libeditline.so.0:libeditline.so";
184 char *tmp, *cp, *next;
185 int (*bind_key)(int, void *);
186 void (*insert)(void);
188 /* default to internal function if we can't or won't find anything */
189 session->readline = get_string;
192 session->interactive = 1;
194 tmp = malloc(strlen(libpath)+1);
197 strcpy(tmp, libpath);
198 for (cp = tmp; cp; cp = next) {
199 next = strchr(cp, ':');
204 handle = dlopen(cp, RTLD_NOW);
206 dbg("Using %s for readline library\n", cp);
212 dbg("No readline library found.\n");
216 session->readline_handle = handle;
217 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
218 if (session->readline == NULL) {
219 /* something odd happened, default back to internal stuff */
220 session->readline_handle = NULL;
221 session->readline = get_string;
226 * If we found a library, turn off filename expansion
227 * as that makes no sense from within bti.
229 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
230 insert = (void (*)(void))dlsym(handle, "rl_insert");
231 if (bind_key && insert)
232 bind_key('\t', insert);
235 static void session_readline_cleanup(struct session *session)
237 if (session->readline_handle)
238 dlclose(session->readline_handle);
241 static struct session *session_alloc(void)
243 struct session *session;
245 session = zalloc(sizeof(*session));
251 static void session_free(struct session *session)
255 free(session->replyto);
256 free(session->password);
257 free(session->account);
258 free(session->consumer_key);
259 free(session->consumer_secret);
260 free(session->access_token_key);
261 free(session->access_token_secret);
262 free(session->tweet);
263 free(session->proxy);
265 free(session->homedir);
267 free(session->group);
268 free(session->hosturl);
269 free(session->hostname);
270 free(session->configfile);
274 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
276 struct bti_curl_buffer *buffer;
278 buffer = zalloc(sizeof(*buffer));
282 /* start out with a data buffer of 1 byte to
283 * make the buffer fill logic simpler */
284 buffer->data = zalloc(1);
290 buffer->action = action;
294 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
302 static const char twitter_host[] = "http://api.twitter.com/1/statuses";
303 static const char identica_host[] = "https://identi.ca/api/statuses";
304 static const char twitter_name[] = "twitter";
305 static const char identica_name[] = "identi.ca";
307 static const char twitter_request_token_uri[] = "http://twitter.com/oauth/request_token";
308 static const char twitter_access_token_uri[] = "http://twitter.com/oauth/access_token";
309 static const char twitter_authorize_uri[] = "http://twitter.com/oauth/authorize?oauth_token=";
310 static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token?oauth_callback=oob";
311 static const char identica_access_token_uri[] = "http://identi.ca/api/oauth/access_token";
312 static const char identica_authorize_uri[] = "http://identi.ca/api/oauth/authorize?oauth_token=";
314 static const char user_uri[] = "/user_timeline/";
315 static const char update_uri[] = "/update.xml";
316 static const char public_uri[] = "/public_timeline.xml";
317 static const char friends_uri[] = "/friends_timeline.xml";
318 static const char mentions_uri[] = "/mentions.xml";
319 static const char replies_uri[] = "/replies.xml";
320 static const char group_uri[] = "/../statusnet/groups/timeline/";
322 static CURL *curl_init(void)
326 curl = curl_easy_init();
328 fprintf(stderr, "Can not init CURL!\n");
331 /* some ssl sanity checks on the connection we are making */
332 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
333 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
337 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
339 xmlChar *text = NULL;
340 xmlChar *user = NULL;
341 xmlChar *created = NULL;
345 current = current->xmlChildrenNode;
346 while (current != NULL) {
347 if (current->type == XML_ELEMENT_NODE) {
348 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
349 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
350 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
351 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
352 if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
353 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
354 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
355 userinfo = current->xmlChildrenNode;
356 while (userinfo != NULL) {
357 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
360 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
362 userinfo = userinfo->next;
366 if (user && text && created && id) {
368 printf("[%s] {%s} (%.16s) %s\n",
369 user, id, created, text);
383 current = current->next;
389 static void parse_timeline(char *document)
394 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
395 NULL, XML_PARSE_NOERROR);
399 current = xmlDocGetRootElement(doc);
400 if (current == NULL) {
401 fprintf(stderr, "empty document\n");
406 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
407 fprintf(stderr, "unexpected document type\n");
412 current = current->xmlChildrenNode;
413 while (current != NULL) {
414 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
415 parse_statuses(doc, current);
416 current = current->next;
423 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
426 struct bti_curl_buffer *curl_buf = userp;
427 size_t buffer_size = size * nmemb;
430 if ((!buffer) || (!buffer_size) || (!curl_buf))
433 /* add to the data we already have */
434 temp = zalloc(curl_buf->length + buffer_size + 1);
438 memcpy(temp, curl_buf->data, curl_buf->length);
439 free(curl_buf->data);
440 curl_buf->data = temp;
441 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
442 curl_buf->length += buffer_size;
443 if (curl_buf->action)
444 parse_timeline(curl_buf->data);
446 dbg("%s\n", curl_buf->data);
451 static int parse_osp_reply(const char *reply, char **token, char **secret)
456 rc = oauth_split_url_parameters(reply, &rv);
457 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
458 if (rc == 2 || rc == 4) {
459 if (!strncmp(rv[0], "oauth_token=", 11) &&
460 !strncmp(rv[1], "oauth_token_secret=", 18)) {
462 *token = strdup(&(rv[0][12]));
464 *secret = strdup(&(rv[1][19]));
468 } else if (rc == 3) {
469 if (!strncmp(rv[1], "oauth_token=", 11) &&
470 !strncmp(rv[2], "oauth_token_secret=", 18)) {
472 *token = strdup(&(rv[1][12]));
474 *secret = strdup(&(rv[2][19]));
480 dbg("token: %s\n", *token);
481 dbg("secret: %s\n", *secret);
489 static int request_access_token(struct session *session)
491 char *post_params = NULL;
492 char *request_url = NULL;
495 char *at_secret = NULL;
496 char *verifier = NULL;
502 if (session->host == HOST_TWITTER)
503 request_url = oauth_sign_url2(
504 twitter_request_token_uri, NULL,
505 OA_HMAC, NULL, session->consumer_key,
506 session->consumer_secret, NULL, NULL);
507 else if (session->host == HOST_IDENTICA)
508 request_url = oauth_sign_url2(
509 identica_request_token_uri, NULL,
510 OA_HMAC, NULL, session->consumer_key,
511 session->consumer_secret, NULL, NULL);
512 reply = oauth_http_get(request_url, post_params);
523 if (parse_osp_reply(reply, &at_key, &at_secret))
529 "Please open the following link in your browser, and "
530 "allow 'bti' to access your account. Then paste "
531 "back the provided PIN in here.\n");
532 if (session->host == HOST_TWITTER) {
533 fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
534 verifier = session->readline(NULL);
535 sprintf(at_uri, "%s?oauth_verifier=%s",
536 twitter_access_token_uri, verifier);
537 } else if (session->host == HOST_IDENTICA) {
538 fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key);
539 verifier = session->readline(NULL);
540 sprintf(at_uri, "%s?oauth_verifier=%s",
541 identica_access_token_uri, verifier);
543 request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
544 session->consumer_key,
545 session->consumer_secret,
547 reply = oauth_http_get(request_url, post_params);
552 if (parse_osp_reply(reply, &at_key, &at_secret))
558 "Please put these two lines in your bti "
559 "configuration file (~/.bti):\n"
560 "access_token_key=%s\n"
561 "access_token_secret=%s\n",
567 static int send_request(struct session *session)
570 char user_password[500];
572 struct bti_curl_buffer *curl_buf;
575 struct curl_httppost *formpost = NULL;
576 struct curl_httppost *lastptr = NULL;
577 struct curl_slist *slist = NULL;
578 char *req_url = NULL;
580 char *postarg = NULL;
581 char *escaped_tweet = NULL;
587 if (!session->hosturl)
588 session->hosturl = strdup(twitter_host);
590 if (session->no_oauth) {
591 curl_buf = bti_curl_buffer_alloc(session->action);
599 if (!session->hosturl)
600 session->hosturl = strdup(twitter_host);
602 switch (session->action) {
604 snprintf(user_password, sizeof(user_password), "%s:%s",
605 session->account, session->password);
606 snprintf(data, sizeof(data), "status=\"%s\"",
608 curl_formadd(&formpost, &lastptr,
609 CURLFORM_COPYNAME, "status",
610 CURLFORM_COPYCONTENTS, session->tweet,
613 curl_formadd(&formpost, &lastptr,
614 CURLFORM_COPYNAME, "source",
615 CURLFORM_COPYCONTENTS, "bti",
618 if (session->replyto)
619 curl_formadd(&formpost, &lastptr,
620 CURLFORM_COPYNAME, "in_reply_to_status_id",
621 CURLFORM_COPYCONTENTS, session->replyto,
624 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
625 slist = curl_slist_append(slist, "Expect:");
626 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
628 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
629 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
630 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
634 snprintf(user_password, sizeof(user_password), "%s:%s",
635 session->account, session->password);
636 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
637 friends_uri, session->page);
638 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
639 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
643 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
644 user_uri, session->user, session->page);
645 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
649 snprintf(user_password, sizeof(user_password), "%s:%s",
650 session->account, session->password);
651 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
652 replies_uri, session->page);
653 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
654 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
658 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
659 public_uri, session->page);
660 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
664 sprintf(endpoint, "%s%s%s.xml?page=%d",
665 session->hosturl, group_uri, session->group,
667 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
675 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
678 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
680 dbg("user_password = %s\n", user_password);
681 dbg("data = %s\n", data);
682 dbg("proxy = %s\n", session->proxy);
684 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
685 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
686 if (!session->dry_run) {
687 res = curl_easy_perform(curl);
688 if (res && !session->background) {
689 fprintf(stderr, "error(%d) trying to perform "
695 curl_easy_cleanup(curl);
696 if (session->action == ACTION_UPDATE)
697 curl_formfree(formpost);
698 bti_curl_buffer_free(curl_buf);
700 switch (session->action) {
702 escaped_tweet = oauth_url_escape(session->tweet);
703 sprintf(endpoint, "%s%s?status=%s", session->hosturl,
704 update_uri, escaped_tweet);
708 sprintf(endpoint, "%s%s%s.xml?page=%d",
709 session->hosturl, user_uri, session->user,
713 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
714 mentions_uri, session->page);
717 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
718 public_uri, session->page);
721 sprintf(endpoint, "%s%s%s.xml?page=%d",
722 session->hosturl, group_uri, session->group,
726 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
727 friends_uri, session->page);
734 req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC,
735 NULL, session->consumer_key,
736 session->consumer_secret,
737 session->access_token_key,
738 session->access_token_secret);
739 reply = oauth_http_post(req_url, postarg);
741 req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
742 session->consumer_key,
743 session->consumer_secret,
744 session->access_token_key,
745 session->access_token_secret);
746 reply = oauth_http_get(req_url, postarg);
749 dbg("%s\n", req_url);
754 if (session->action != ACTION_UPDATE)
755 parse_timeline(reply);
760 static void parse_configfile(struct session *session)
765 char *account = NULL;
766 char *password = NULL;
767 char *consumer_key = NULL;
768 char *consumer_secret = NULL;
769 char *access_token_key = NULL;
770 char *access_token_secret = NULL;
773 char *logfile = NULL;
776 char *replyto = NULL;
779 config_file = fopen(session->configfile, "r");
781 /* No error if file does not exist or is unreadable. */
782 if (config_file == NULL)
786 ssize_t n = getline(&line, &len, config_file);
789 if (line[n - 1] == '\n')
791 /* Parse file. Format is the usual value pairs:
794 # is a comment character
796 *strchrnul(line, '#') = '\0';
800 /* Ignore blank lines. */
804 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
808 } else if (!strncasecmp(c, "password", 8) &&
812 password = strdup(c);
813 } else if (!strncasecmp(c, "consumer_key", 12) &&
817 consumer_key = strdup(c);
818 } else if (!strncasecmp(c, "consumer_secret", 15) &&
822 consumer_secret = strdup(c);
823 } else if (!strncasecmp(c, "access_token_key", 16) &&
827 access_token_key = strdup(c);
828 } else if (!strncasecmp(c, "access_token_secret", 19) &&
832 access_token_secret = strdup(c);
833 } else if (!strncasecmp(c, "host", 4) &&
838 } else if (!strncasecmp(c, "proxy", 5) &&
843 } else if (!strncasecmp(c, "logfile", 7) &&
848 } else if (!strncasecmp(c, "replyto", 7) &&
853 } else if (!strncasecmp(c, "action", 6) &&
858 } else if (!strncasecmp(c, "user", 4) &&
863 } else if (!strncasecmp(c, "shrink-urls", 11) &&
866 if (!strncasecmp(c, "true", 4) ||
867 !strncasecmp(c, "yes", 3))
869 } else if (!strncasecmp(c, "verbose", 7) &&
872 if (!strncasecmp(c, "true", 4) ||
873 !strncasecmp(c, "yes", 3))
876 } while (!feof(config_file));
879 session->password = password;
881 session->account = account;
883 session->consumer_key = consumer_key;
885 session->consumer_secret = consumer_secret;
886 if (access_token_key)
887 session->access_token_key = access_token_key;
888 if (access_token_secret)
889 session->access_token_secret = access_token_secret;
891 if (strcasecmp(host, "twitter") == 0) {
892 session->host = HOST_TWITTER;
893 session->hosturl = strdup(twitter_host);
894 session->hostname = strdup(twitter_name);
895 } else if (strcasecmp(host, "identica") == 0) {
896 session->host = HOST_IDENTICA;
897 session->hosturl = strdup(identica_host);
898 session->hostname = strdup(identica_name);
900 session->host = HOST_CUSTOM;
901 session->hosturl = strdup(host);
902 session->hostname = strdup(host);
908 free(session->proxy);
909 session->proxy = proxy;
912 session->logfile = logfile;
914 session->replyto = replyto;
916 if (strcasecmp(action, "update") == 0)
917 session->action = ACTION_UPDATE;
918 else if (strcasecmp(action, "friends") == 0)
919 session->action = ACTION_FRIENDS;
920 else if (strcasecmp(action, "user") == 0)
921 session->action = ACTION_USER;
922 else if (strcasecmp(action, "replies") == 0)
923 session->action = ACTION_REPLIES;
924 else if (strcasecmp(action, "public") == 0)
925 session->action = ACTION_PUBLIC;
926 else if (strcasecmp(action, "group") == 0)
927 session->action = ACTION_GROUP;
929 session->action = ACTION_UNKNOWN;
933 session->user = user;
934 session->shrink_urls = shrink_urls;
936 /* Free buffer and close file. */
941 static void log_session(struct session *session, int retval)
946 /* Only log something if we have a log file set */
947 if (!session->logfile)
950 filename = alloca(strlen(session->homedir) +
951 strlen(session->logfile) + 3);
953 sprintf(filename, "%s/%s", session->homedir, session->logfile);
955 log_file = fopen(filename, "a+");
956 if (log_file == NULL)
959 switch (session->action) {
962 fprintf(log_file, "%s: host=%s tweet failed\n",
963 session->time, session->hostname);
965 fprintf(log_file, "%s: host=%s tweet=%s\n",
966 session->time, session->hostname,
970 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
971 session->time, session->hostname);
974 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
975 session->time, session->hostname, session->user);
978 fprintf(log_file, "%s: host=%s retrieving replies\n",
979 session->time, session->hostname);
982 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
983 session->time, session->hostname);
986 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
987 session->time, session->hostname);
996 static char *get_string_from_stdin(void)
1001 string = zalloc(1000);
1005 if (!fgets(string, 999, stdin))
1007 temp = strchr(string, '\n');
1013 static void read_password(char *buf, size_t len, char *host)
1023 tp.c_lflag &= (~ECHO);
1024 tcsetattr(0, TCSANOW, &tp);
1026 fprintf(stdout, "Enter password for %s: ", host);
1029 retval = scanf("%79s", pwd);
1031 fprintf(stdout, "\n");
1033 tcsetattr(0, TCSANOW, &old);
1035 strncpy(buf, pwd, len);
1039 static int find_urls(const char *tweet, int **pranges)
1042 * magic obtained from
1043 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
1045 static const char *re_magic =
1046 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
1047 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
1048 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
1052 int ovector[10] = {0,};
1053 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
1054 int startoffset, tweetlen;
1058 int *ranges = malloc(sizeof(int) * rbound);
1060 re = pcre_compile(re_magic,
1061 PCRE_NO_AUTO_CAPTURE,
1062 &errptr, &erroffset, NULL);
1064 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
1068 tweetlen = strlen(tweet);
1069 for (startoffset = 0; startoffset < tweetlen; ) {
1071 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
1073 if (rc == PCRE_ERROR_NOMATCH)
1077 fprintf(stderr, "pcre_exec @%u: %s\n",
1082 for (i = 0; i < rc; i += 2) {
1083 if ((rcount+2) == rbound) {
1085 ranges = realloc(ranges, sizeof(int) * rbound);
1088 ranges[rcount++] = ovector[i];
1089 ranges[rcount++] = ovector[i+1];
1092 startoffset = ovector[1];
1102 * bidirectional popen() call
1104 * @param rwepipe - int array of size three
1105 * @param exe - program to run
1106 * @param argv - argument list
1107 * @return pid or -1 on error
1109 * The caller passes in an array of three integers (rwepipe), on successful
1110 * execution it can then write to element 0 (stdin of exe), and read from
1111 * element 1 (stdout) and 2 (stderr).
1113 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
1140 rwepipe[1] = out[0];
1141 rwepipe[2] = err[0];
1143 } else if (pid == 0) {
1155 execvp(exe, (char **)argv);
1175 static int pcloseRWE(int pid, int *rwepipe)
1181 rc = waitpid(pid, &status, 0);
1185 static char *shrink_one_url(int *rwepipe, char *big)
1187 int biglen = strlen(big);
1192 rc = dprintf(rwepipe[0], "%s\n", big);
1196 smalllen = biglen + 128;
1197 small = malloc(smalllen);
1201 rc = read(rwepipe[1], small, smalllen);
1202 if (rc < 0 || rc > biglen)
1203 goto error_free_small;
1205 if (strncmp(small, "http://", 7))
1206 goto error_free_small;
1209 while (smalllen && isspace(small[smalllen-1]))
1210 small[--smalllen] = 0;
1220 static char *shrink_urls(char *text)
1227 const char *const shrink_args[] = {
1233 int inlen = strlen(text);
1235 dbg("before len=%u\n", inlen);
1237 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1241 rcount = find_urls(text, &ranges);
1245 for (i = 0; i < rcount; i += 2) {
1246 int url_start = ranges[i];
1247 int url_end = ranges[i+1];
1248 int long_url_len = url_end - url_start;
1249 char *url = strndup(text + url_start, long_url_len);
1251 int not_url_len = url_start - inofs;
1253 dbg("long url[%u]: %s\n", long_url_len, url);
1254 url = shrink_one_url(shrink_pipe, url);
1255 short_url_len = url ? strlen(url) : 0;
1256 dbg("short url[%u]: %s\n", short_url_len, url);
1258 if (!url || short_url_len >= long_url_len) {
1259 /* The short url ended up being too long
1262 strncpy(text + outofs, text + inofs,
1263 not_url_len + long_url_len);
1265 inofs += not_url_len + long_url_len;
1266 outofs += not_url_len + long_url_len;
1269 /* copy the unmodified block */
1270 strncpy(text + outofs, text + inofs, not_url_len);
1271 inofs += not_url_len;
1272 outofs += not_url_len;
1274 /* copy the new url */
1275 strncpy(text + outofs, url, short_url_len);
1276 inofs += long_url_len;
1277 outofs += short_url_len;
1283 /* copy the last block after the last match */
1285 int tail = inlen - inofs;
1287 strncpy(text + outofs, text + inofs, tail);
1294 (void)pcloseRWE(shrink_pid, shrink_pipe);
1297 dbg("after len=%u\n", outofs);
1301 int main(int argc, char *argv[], char *envp[])
1303 static const struct option options[] = {
1304 { "debug", 0, NULL, 'd' },
1305 { "verbose", 0, NULL, 'V' },
1306 { "account", 1, NULL, 'a' },
1307 { "password", 1, NULL, 'p' },
1308 { "host", 1, NULL, 'H' },
1309 { "proxy", 1, NULL, 'P' },
1310 { "action", 1, NULL, 'A' },
1311 { "user", 1, NULL, 'u' },
1312 { "group", 1, NULL, 'G' },
1313 { "logfile", 1, NULL, 'L' },
1314 { "shrink-urls", 0, NULL, 's' },
1315 { "help", 0, NULL, 'h' },
1316 { "bash", 0, NULL, 'b' },
1317 { "background", 0, NULL, 'B' },
1318 { "dry-run", 0, NULL, 'n' },
1319 { "page", 1, NULL, 'g' },
1320 { "version", 0, NULL, 'v' },
1321 { "config", 1, NULL, 'c' },
1322 { "replyto", 1, NULL, 'r' },
1325 struct session *session;
1328 static char password[80];
1338 session = session_alloc();
1340 fprintf(stderr, "no more memory...\n");
1344 /* get the current time so that we can log it later */
1346 session->time = strdup(ctime(&t));
1347 session->time[strlen(session->time)-1] = 0x00;
1349 /* Get the home directory so we can try to find a config file */
1350 session->homedir = strdup(getenv("HOME"));
1352 /* set up a default config file location (traditionally ~/.bti) */
1353 session->configfile = zalloc(strlen(session->homedir) + 7);
1354 sprintf(session->configfile, "%s/.bti", session->homedir);
1356 /* Set environment variables first, before reading command line options
1357 * or config file values. */
1358 http_proxy = getenv("http_proxy");
1361 free(session->proxy);
1362 session->proxy = strdup(http_proxy);
1363 dbg("http_proxy = %s\n", session->proxy);
1366 parse_configfile(session);
1369 option = getopt_long_only(argc, argv,
1370 "dp:P:H:a:A:u:c:hg:G:sr:nVv",
1382 if (session->account)
1383 free(session->account);
1384 session->account = strdup(optarg);
1385 dbg("account = %s\n", session->account);
1388 page_nr = atoi(optarg);
1389 dbg("page = %d\n", page_nr);
1390 session->page = page_nr;
1393 session->replyto = strdup(optarg);
1394 dbg("in_reply_to_status_id = %s\n", session->replyto);
1397 if (session->password)
1398 free(session->password);
1399 session->password = strdup(optarg);
1400 dbg("password = %s\n", session->password);
1404 free(session->proxy);
1405 session->proxy = strdup(optarg);
1406 dbg("proxy = %s\n", session->proxy);
1409 if (strcasecmp(optarg, "update") == 0)
1410 session->action = ACTION_UPDATE;
1411 else if (strcasecmp(optarg, "friends") == 0)
1412 session->action = ACTION_FRIENDS;
1413 else if (strcasecmp(optarg, "user") == 0)
1414 session->action = ACTION_USER;
1415 else if (strcasecmp(optarg, "replies") == 0)
1416 session->action = ACTION_REPLIES;
1417 else if (strcasecmp(optarg, "public") == 0)
1418 session->action = ACTION_PUBLIC;
1419 else if (strcasecmp(optarg, "group") == 0)
1420 session->action = ACTION_GROUP;
1422 session->action = ACTION_UNKNOWN;
1423 dbg("action = %d\n", session->action);
1427 free(session->user);
1428 session->user = strdup(optarg);
1429 dbg("user = %s\n", session->user);
1434 free(session->group);
1435 session->group = strdup(optarg);
1436 dbg("group = %s\n", session->group);
1439 if (session->logfile)
1440 free(session->logfile);
1441 session->logfile = strdup(optarg);
1442 dbg("logfile = %s\n", session->logfile);
1445 session->shrink_urls = 1;
1448 if (session->hosturl)
1449 free(session->hosturl);
1450 if (session->hostname)
1451 free(session->hostname);
1452 if (strcasecmp(optarg, "twitter") == 0) {
1453 session->host = HOST_TWITTER;
1454 session->hosturl = strdup(twitter_host);
1455 session->hostname = strdup(twitter_name);
1456 } else if (strcasecmp(optarg, "identica") == 0) {
1457 session->host = HOST_IDENTICA;
1458 session->hosturl = strdup(identica_host);
1459 session->hostname = strdup(identica_name);
1461 session->host = HOST_CUSTOM;
1462 session->hosturl = strdup(optarg);
1463 session->hostname = strdup(optarg);
1465 dbg("host = %d\n", session->host);
1469 /* fall-through intended */
1471 session->background = 1;
1474 if (session->configfile)
1475 free(session->configfile);
1476 session->configfile = strdup(optarg);
1477 dbg("configfile = %s\n", session->configfile);
1480 * read the config file now. Yes, this could override
1481 * previously set options from the command line, but
1482 * the user asked for it...
1484 parse_configfile(session);
1490 session->dry_run = 1;
1501 session_readline_init(session);
1503 * Show the version to make it easier to determine what
1509 if (session->host == HOST_TWITTER) {
1510 if (!session->consumer_key || !session->consumer_secret) {
1512 "Twitter no longer supports HTTP basic authentication.\n"
1513 "Both consumer key, and consumer secret are required"
1514 " for bti in order to behave as an OAuth consumer.\n");
1517 if (session->action == ACTION_GROUP) {
1518 fprintf(stderr, "Groups only work in Identi.ca.\n");
1522 if (!session->consumer_key || !session->consumer_secret)
1523 session->no_oauth = 1;
1526 if (session->no_oauth) {
1527 if (!session->account) {
1528 fprintf(stdout, "Enter account for %s: ",
1530 session->account = session->readline(NULL);
1532 if (!session->password) {
1533 read_password(password, sizeof(password),
1535 session->password = strdup(password);
1538 if (!session->access_token_key ||
1539 !session->access_token_secret) {
1540 request_access_token(session);
1545 if (session->action == ACTION_UNKNOWN) {
1546 fprintf(stderr, "Unknown action, valid actions are:\n"
1547 "'update', 'friends', 'public', 'replies', 'group' or 'user'.\n");
1551 if (session->action == ACTION_GROUP && !session->group) {
1552 fprintf(stdout, "Enter group name: ");
1553 session->group = session->readline(NULL);
1556 if (session->action == ACTION_UPDATE) {
1557 if (session->background || !session->interactive)
1558 tweet = get_string_from_stdin();
1560 tweet = session->readline("tweet: ");
1561 if (!tweet || strlen(tweet) == 0) {
1566 if (session->shrink_urls)
1567 tweet = shrink_urls(tweet);
1569 session->tweet = zalloc(strlen(tweet) + 10);
1571 sprintf(session->tweet, "%c %s",
1572 getuid() ? '$' : '#', tweet);
1574 sprintf(session->tweet, "%s", tweet);
1577 dbg("tweet = %s\n", session->tweet);
1580 if (session->page == 0)
1582 dbg("config file = %s\n", session->configfile);
1583 dbg("host = %d\n", session->host);
1584 dbg("action = %d\n", session->action);
1586 /* fork ourself so that the main shell can get on
1587 * with it's life as we try to connect and handle everything
1589 if (session->background) {
1592 dbg("child is %d\n", child);
1597 retval = send_request(session);
1598 if (retval && !session->background)
1599 fprintf(stderr, "operation failed\n");
1601 log_session(session, retval);
1603 session_readline_cleanup(session);
1604 session_free(session);