2 * Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
4 * Copyright (C) 2009 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");
114 fprintf(stdout, "Version: " VERSION "\n");
115 fprintf(stdout, "Usage:\n");
116 fprintf(stdout, " bti [options]\n");
117 fprintf(stdout, "options are:\n");
118 fprintf(stdout, " --account accountname\n");
119 fprintf(stdout, " --password password\n");
120 fprintf(stdout, " --action action\n");
121 fprintf(stdout, " ('update', 'friends', 'public', 'replies', "
122 "'group' or 'user')\n");
123 fprintf(stdout, " --user screenname\n");
124 fprintf(stdout, " --group groupname\n");
125 fprintf(stdout, " --proxy PROXY:PORT\n");
126 fprintf(stdout, " --host HOST\n");
127 fprintf(stdout, " --logfile logfile\n");
128 fprintf(stdout, " --config configfile\n");
129 fprintf(stdout, " --replyto ID\n");
130 fprintf(stdout, " --shrink-urls\n");
131 fprintf(stdout, " --page PAGENUMBER\n");
132 fprintf(stdout, " --bash\n");
133 fprintf(stdout, " --background\n");
134 fprintf(stdout, " --debug\n");
135 fprintf(stdout, " --verbose\n");
136 fprintf(stdout, " --dry-run\n");
137 fprintf(stdout, " --version\n");
138 fprintf(stdout, " --help\n");
141 static void display_version(void)
143 fprintf(stdout, "bti - version %s\n", VERSION);
146 static char *get_string(const char *name)
151 string = zalloc(1000);
155 fprintf(stdout, "%s", name);
156 if (!fgets(string, 999, stdin))
158 temp = strchr(string, '\n');
165 * Try to get a handle to a readline function from a variety of different
166 * libraries. If nothing is present on the system, then fall back to an
169 * Logic originally based off of code in the e2fsutils package in the
170 * lib/ss/get_readline.c file, which is licensed under the MIT license.
172 * This keeps us from having to relicense the bti codebase if readline
173 * ever changes its license, as there is no link-time dependancy.
174 * It is a run-time thing only, and we handle any readline-like library
175 * in the same manner, making bti not be a derivative work of any
178 static void session_readline_init(struct session *session)
180 /* Libraries we will try to use for readline/editline functionality */
181 const char *libpath = "libreadline.so.6:libreadline.so.5:"
182 "libreadline.so.4:libreadline.so:libedit.so.2:"
183 "libedit.so:libeditline.so.0:libeditline.so";
185 char *tmp, *cp, *next;
186 int (*bind_key)(int, void *);
187 void (*insert)(void);
189 /* default to internal function if we can't or won't find anything */
190 session->readline = get_string;
193 session->interactive = 1;
195 tmp = malloc(strlen(libpath)+1);
198 strcpy(tmp, libpath);
199 for (cp = tmp; cp; cp = next) {
200 next = strchr(cp, ':');
205 handle = dlopen(cp, RTLD_NOW);
207 dbg("Using %s for readline library\n", cp);
213 dbg("No readline library found.\n");
217 session->readline_handle = handle;
218 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
219 if (session->readline == NULL) {
220 /* something odd happened, default back to internal stuff */
221 session->readline_handle = NULL;
222 session->readline = get_string;
227 * If we found a library, turn off filename expansion
228 * as that makes no sense from within bti.
230 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
231 insert = (void (*)(void))dlsym(handle, "rl_insert");
232 if (bind_key && insert)
233 bind_key('\t', insert);
236 static void session_readline_cleanup(struct session *session)
238 if (session->readline_handle)
239 dlclose(session->readline_handle);
242 static struct session *session_alloc(void)
244 struct session *session;
246 session = zalloc(sizeof(*session));
252 static void session_free(struct session *session)
256 free(session->replyto);
257 free(session->password);
258 free(session->account);
259 free(session->consumer_key);
260 free(session->consumer_secret);
261 free(session->access_token_key);
262 free(session->access_token_secret);
263 free(session->tweet);
264 free(session->proxy);
266 free(session->homedir);
268 free(session->group);
269 free(session->hosturl);
270 free(session->hostname);
271 free(session->configfile);
275 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
277 struct bti_curl_buffer *buffer;
279 buffer = zalloc(sizeof(*buffer));
283 /* start out with a data buffer of 1 byte to
284 * make the buffer fill logic simpler */
285 buffer->data = zalloc(1);
291 buffer->action = action;
295 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
303 static const char twitter_host[] = "http://api.twitter.com/1/statuses";
304 static const char identica_host[] = "https://identi.ca/api/statuses";
305 static const char twitter_name[] = "twitter";
306 static const char identica_name[] = "identi.ca";
308 static const char twitter_request_token_uri[] = "http://twitter.com/oauth/request_token";
309 static const char twitter_access_token_uri[] = "http://twitter.com/oauth/access_token";
310 static const char twitter_authorize_uri[] = "http://twitter.com/oauth/authorize?oauth_token=";
311 static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token";
312 static const char identica_access_token_uri[] = "http://identi.ca/api/oauth/access_token";
313 static const char identica_authorize_uri[] = "http://identi.ca/api/oauth/authorize?oauth_token=";
315 static const char user_uri[] = "/user_timeline/";
316 static const char update_uri[] = "/update.xml";
317 static const char public_uri[] = "/public_timeline.xml";
318 static const char friends_uri[] = "/friends_timeline.xml";
319 static const char mentions_uri[] = "/mentions.xml";
320 static const char replies_uri[] = "/replies.xml";
321 static const char group_uri[] = "/../statusnet/groups/timeline/";
323 static CURL *curl_init(void)
327 curl = curl_easy_init();
329 fprintf(stderr, "Can not init CURL!\n");
332 /* some ssl sanity checks on the connection we are making */
333 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
334 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
338 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
340 xmlChar *text = NULL;
341 xmlChar *user = NULL;
342 xmlChar *created = NULL;
346 current = current->xmlChildrenNode;
347 while (current != NULL) {
348 if (current->type == XML_ELEMENT_NODE) {
349 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
350 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
351 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
352 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
353 if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
354 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
355 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
356 userinfo = current->xmlChildrenNode;
357 while (userinfo != NULL) {
358 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
361 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
363 userinfo = userinfo->next;
367 if (user && text && created && id) {
369 printf("[%s] {%s} (%.16s) %s\n",
370 user, id, created, text);
384 current = current->next;
390 static void parse_timeline(char *document)
395 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
396 NULL, XML_PARSE_NOERROR);
400 current = xmlDocGetRootElement(doc);
401 if (current == NULL) {
402 fprintf(stderr, "empty document\n");
407 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
408 fprintf(stderr, "unexpected document type\n");
413 current = current->xmlChildrenNode;
414 while (current != NULL) {
415 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
416 parse_statuses(doc, current);
417 current = current->next;
424 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
427 struct bti_curl_buffer *curl_buf = userp;
428 size_t buffer_size = size * nmemb;
431 if ((!buffer) || (!buffer_size) || (!curl_buf))
434 /* add to the data we already have */
435 temp = zalloc(curl_buf->length + buffer_size + 1);
439 memcpy(temp, curl_buf->data, curl_buf->length);
440 free(curl_buf->data);
441 curl_buf->data = temp;
442 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
443 curl_buf->length += buffer_size;
444 if (curl_buf->action)
445 parse_timeline(curl_buf->data);
447 dbg("%s\n", curl_buf->data);
452 static int parse_osp_reply(const char *reply, char **token, char **secret)
457 rc = oauth_split_url_parameters(reply, &rv);
458 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
459 if (rc == 2 || rc == 4) {
460 if (!strncmp(rv[0], "oauth_token=", 11) &&
461 !strncmp(rv[1], "oauth_token_secret=", 18)) {
463 *token = strdup(&(rv[0][12]));
465 *secret = strdup(&(rv[1][19]));
469 } else if (rc == 3) {
470 if (!strncmp(rv[1], "oauth_token=", 11) &&
471 !strncmp(rv[2], "oauth_token_secret=", 18)) {
473 *token = strdup(&(rv[1][12]));
475 *secret = strdup(&(rv[2][19]));
481 dbg("token: %s\n", *token);
482 dbg("secret: %s\n", *secret);
490 static int request_access_token(struct session *session)
492 char *post_params = NULL;
493 char *request_url = NULL;
496 char *at_secret = NULL;
497 char *verifier = NULL;
503 if (session->host == HOST_TWITTER)
504 request_url = oauth_sign_url2(
505 twitter_request_token_uri, NULL,
506 OA_HMAC, NULL, session->consumer_key,
507 session->consumer_secret, NULL, NULL);
508 else if (session->host == HOST_IDENTICA)
509 request_url = oauth_sign_url2(
510 identica_request_token_uri, NULL,
511 OA_HMAC, NULL, session->consumer_key,
512 session->consumer_secret, NULL, NULL);
513 reply = oauth_http_get(request_url, post_params);
524 if (parse_osp_reply(reply, &at_key, &at_secret))
530 "Please open the following link in your browser, and "
531 "allow 'bti' to access your account. Then paste "
532 "back the provided PIN in here.\n");
533 if (session->host == HOST_TWITTER) {
534 fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
535 verifier = session->readline(NULL);
536 sprintf(at_uri, "%s?oauth_verifier=%s",
537 twitter_access_token_uri, verifier);
538 } else if (session->host == HOST_IDENTICA) {
539 fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key);
540 verifier = session->readline(NULL);
541 sprintf(at_uri, "%s?oauth_verifier=%s",
542 identica_access_token_uri, verifier);
544 request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
545 session->consumer_key,
546 session->consumer_secret,
548 reply = oauth_http_get(request_url, post_params);
553 if (parse_osp_reply(reply, &at_key, &at_secret))
559 "Please put these two lines in your bti "
560 "configuration file (~/.bti):\n"
561 "access_token_key=%s\n"
562 "access_token_secret=%s\n",
568 static int send_request(struct session *session)
571 char user_password[500];
573 struct bti_curl_buffer *curl_buf;
576 struct curl_httppost *formpost = NULL;
577 struct curl_httppost *lastptr = NULL;
578 struct curl_slist *slist = NULL;
579 char *req_url = NULL;
581 char *postarg = NULL;
582 char *escaped_tweet = NULL;
588 if (!session->hosturl)
589 session->hosturl = strdup(twitter_host);
591 if (session->no_oauth) {
592 curl_buf = bti_curl_buffer_alloc(session->action);
600 if (!session->hosturl)
601 session->hosturl = strdup(twitter_host);
603 switch (session->action) {
605 snprintf(user_password, sizeof(user_password), "%s:%s",
606 session->account, session->password);
607 snprintf(data, sizeof(data), "status=\"%s\"",
609 curl_formadd(&formpost, &lastptr,
610 CURLFORM_COPYNAME, "status",
611 CURLFORM_COPYCONTENTS, session->tweet,
614 curl_formadd(&formpost, &lastptr,
615 CURLFORM_COPYNAME, "source",
616 CURLFORM_COPYCONTENTS, "bti",
619 if (session->replyto)
620 curl_formadd(&formpost, &lastptr,
621 CURLFORM_COPYNAME, "in_reply_to_status_id",
622 CURLFORM_COPYCONTENTS, session->replyto,
625 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
626 slist = curl_slist_append(slist, "Expect:");
627 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
629 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
630 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
631 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
635 snprintf(user_password, sizeof(user_password), "%s:%s",
636 session->account, session->password);
637 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
638 friends_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%s.xml?page=%d", session->hosturl,
645 user_uri, session->user, session->page);
646 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
650 snprintf(user_password, sizeof(user_password), "%s:%s",
651 session->account, session->password);
652 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
653 replies_uri, session->page);
654 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
655 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
659 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
660 public_uri, session->page);
661 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
665 sprintf(endpoint, "%s%s%s.xml?page=%d",
666 session->hosturl, group_uri, session->group,
668 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
676 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
679 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
681 dbg("user_password = %s\n", user_password);
682 dbg("data = %s\n", data);
683 dbg("proxy = %s\n", session->proxy);
685 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
686 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
687 if (!session->dry_run) {
688 res = curl_easy_perform(curl);
689 if (res && !session->background) {
690 fprintf(stderr, "error(%d) trying to perform "
696 curl_easy_cleanup(curl);
697 if (session->action == ACTION_UPDATE)
698 curl_formfree(formpost);
699 bti_curl_buffer_free(curl_buf);
701 switch (session->action) {
703 escaped_tweet = oauth_url_escape(session->tweet);
704 sprintf(endpoint, "%s%s?status=%s", session->hosturl,
705 update_uri, escaped_tweet);
709 sprintf(endpoint, "%s%s%s.xml?page=%d",
710 session->hosturl, user_uri, session->user,
714 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
715 mentions_uri, session->page);
718 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
719 public_uri, session->page);
722 sprintf(endpoint, "%s%s%s.xml?page=%d",
723 session->hosturl, group_uri, session->group,
727 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
728 friends_uri, session->page);
735 req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC,
736 NULL, session->consumer_key,
737 session->consumer_secret,
738 session->access_token_key,
739 session->access_token_secret);
740 reply = oauth_http_post(req_url, postarg);
742 req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
743 session->consumer_key,
744 session->consumer_secret,
745 session->access_token_key,
746 session->access_token_secret);
747 reply = oauth_http_get(req_url, postarg);
750 dbg("%s\n", req_url);
755 if (session->action != ACTION_UPDATE)
756 parse_timeline(reply);
761 static void parse_configfile(struct session *session)
766 char *account = NULL;
767 char *password = NULL;
768 char *consumer_key = NULL;
769 char *consumer_secret = NULL;
770 char *access_token_key = NULL;
771 char *access_token_secret = NULL;
774 char *logfile = NULL;
777 char *replyto = NULL;
780 config_file = fopen(session->configfile, "r");
782 /* No error if file does not exist or is unreadable. */
783 if (config_file == NULL)
787 ssize_t n = getline(&line, &len, config_file);
790 if (line[n - 1] == '\n')
792 /* Parse file. Format is the usual value pairs:
795 # is a comment character
797 *strchrnul(line, '#') = '\0';
801 /* Ignore blank lines. */
805 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
809 } else if (!strncasecmp(c, "password", 8) &&
813 password = strdup(c);
814 } else if (!strncasecmp(c, "consumer_key", 12) &&
818 consumer_key = strdup(c);
819 } else if (!strncasecmp(c, "consumer_secret", 15) &&
823 consumer_secret = strdup(c);
824 } else if (!strncasecmp(c, "access_token_key", 16) &&
828 access_token_key = strdup(c);
829 } else if (!strncasecmp(c, "access_token_secret", 19) &&
833 access_token_secret = strdup(c);
834 } else if (!strncasecmp(c, "host", 4) &&
839 } else if (!strncasecmp(c, "proxy", 5) &&
844 } else if (!strncasecmp(c, "logfile", 7) &&
849 } else if (!strncasecmp(c, "replyto", 7) &&
854 } else if (!strncasecmp(c, "action", 6) &&
859 } else if (!strncasecmp(c, "user", 4) &&
864 } else if (!strncasecmp(c, "shrink-urls", 11) &&
867 if (!strncasecmp(c, "true", 4) ||
868 !strncasecmp(c, "yes", 3))
870 } else if (!strncasecmp(c, "verbose", 7) &&
873 if (!strncasecmp(c, "true", 4) ||
874 !strncasecmp(c, "yes", 3))
877 } while (!feof(config_file));
880 session->password = password;
882 session->account = account;
884 session->consumer_key = consumer_key;
886 session->consumer_secret = consumer_secret;
887 if (access_token_key)
888 session->access_token_key = access_token_key;
889 if (access_token_secret)
890 session->access_token_secret = access_token_secret;
892 if (strcasecmp(host, "twitter") == 0) {
893 session->host = HOST_TWITTER;
894 session->hosturl = strdup(twitter_host);
895 session->hostname = strdup(twitter_name);
896 } else if (strcasecmp(host, "identica") == 0) {
897 session->host = HOST_IDENTICA;
898 session->hosturl = strdup(identica_host);
899 session->hostname = strdup(identica_name);
901 session->host = HOST_CUSTOM;
902 session->hosturl = strdup(host);
903 session->hostname = strdup(host);
909 free(session->proxy);
910 session->proxy = proxy;
913 session->logfile = logfile;
915 session->replyto = replyto;
917 if (strcasecmp(action, "update") == 0)
918 session->action = ACTION_UPDATE;
919 else if (strcasecmp(action, "friends") == 0)
920 session->action = ACTION_FRIENDS;
921 else if (strcasecmp(action, "user") == 0)
922 session->action = ACTION_USER;
923 else if (strcasecmp(action, "replies") == 0)
924 session->action = ACTION_REPLIES;
925 else if (strcasecmp(action, "public") == 0)
926 session->action = ACTION_PUBLIC;
927 else if (strcasecmp(action, "group") == 0)
928 session->action = ACTION_GROUP;
930 session->action = ACTION_UNKNOWN;
934 session->user = user;
935 session->shrink_urls = shrink_urls;
937 /* Free buffer and close file. */
942 static void log_session(struct session *session, int retval)
947 /* Only log something if we have a log file set */
948 if (!session->logfile)
951 filename = alloca(strlen(session->homedir) +
952 strlen(session->logfile) + 3);
954 sprintf(filename, "%s/%s", session->homedir, session->logfile);
956 log_file = fopen(filename, "a+");
957 if (log_file == NULL)
960 switch (session->action) {
963 fprintf(log_file, "%s: host=%s tweet failed\n",
964 session->time, session->hostname);
966 fprintf(log_file, "%s: host=%s tweet=%s\n",
967 session->time, session->hostname,
971 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
972 session->time, session->hostname);
975 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
976 session->time, session->hostname, session->user);
979 fprintf(log_file, "%s: host=%s retrieving replies\n",
980 session->time, session->hostname);
983 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
984 session->time, session->hostname);
987 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
988 session->time, session->hostname);
997 static char *get_string_from_stdin(void)
1002 string = zalloc(1000);
1006 if (!fgets(string, 999, stdin))
1008 temp = strchr(string, '\n');
1014 static void read_password(char *buf, size_t len, char *host)
1024 tp.c_lflag &= (~ECHO);
1025 tcsetattr(0, TCSANOW, &tp);
1027 fprintf(stdout, "Enter password for %s: ", host);
1030 retval = scanf("%79s", pwd);
1032 fprintf(stdout, "\n");
1034 tcsetattr(0, TCSANOW, &old);
1036 strncpy(buf, pwd, len);
1040 static int find_urls(const char *tweet, int **pranges)
1043 * magic obtained from
1044 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
1046 static const char *re_magic =
1047 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
1048 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
1049 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
1053 int ovector[10] = {0,};
1054 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
1055 int startoffset, tweetlen;
1059 int *ranges = malloc(sizeof(int) * rbound);
1061 re = pcre_compile(re_magic,
1062 PCRE_NO_AUTO_CAPTURE,
1063 &errptr, &erroffset, NULL);
1065 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
1069 tweetlen = strlen(tweet);
1070 for (startoffset = 0; startoffset < tweetlen; ) {
1072 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
1074 if (rc == PCRE_ERROR_NOMATCH)
1078 fprintf(stderr, "pcre_exec @%u: %s\n",
1083 for (i = 0; i < rc; i += 2) {
1084 if ((rcount+2) == rbound) {
1086 ranges = realloc(ranges, sizeof(int) * rbound);
1089 ranges[rcount++] = ovector[i];
1090 ranges[rcount++] = ovector[i+1];
1093 startoffset = ovector[1];
1103 * bidirectional popen() call
1105 * @param rwepipe - int array of size three
1106 * @param exe - program to run
1107 * @param argv - argument list
1108 * @return pid or -1 on error
1110 * The caller passes in an array of three integers (rwepipe), on successful
1111 * execution it can then write to element 0 (stdin of exe), and read from
1112 * element 1 (stdout) and 2 (stderr).
1114 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
1141 rwepipe[1] = out[0];
1142 rwepipe[2] = err[0];
1144 } else if (pid == 0) {
1156 execvp(exe, (char **)argv);
1176 static int pcloseRWE(int pid, int *rwepipe)
1182 rc = waitpid(pid, &status, 0);
1186 static char *shrink_one_url(int *rwepipe, char *big)
1188 int biglen = strlen(big);
1193 rc = dprintf(rwepipe[0], "%s\n", big);
1197 smalllen = biglen + 128;
1198 small = malloc(smalllen);
1202 rc = read(rwepipe[1], small, smalllen);
1203 if (rc < 0 || rc > biglen)
1204 goto error_free_small;
1206 if (strncmp(small, "http://", 7))
1207 goto error_free_small;
1210 while (smalllen && isspace(small[smalllen-1]))
1211 small[--smalllen] = 0;
1221 static char *shrink_urls(char *text)
1228 const char *const shrink_args[] = {
1234 int inlen = strlen(text);
1236 dbg("before len=%u\n", inlen);
1238 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1242 rcount = find_urls(text, &ranges);
1246 for (i = 0; i < rcount; i += 2) {
1247 int url_start = ranges[i];
1248 int url_end = ranges[i+1];
1249 int long_url_len = url_end - url_start;
1250 char *url = strndup(text + url_start, long_url_len);
1252 int not_url_len = url_start - inofs;
1254 dbg("long url[%u]: %s\n", long_url_len, url);
1255 url = shrink_one_url(shrink_pipe, url);
1256 short_url_len = url ? strlen(url) : 0;
1257 dbg("short url[%u]: %s\n", short_url_len, url);
1259 if (!url || short_url_len >= long_url_len) {
1260 /* The short url ended up being too long
1263 strncpy(text + outofs, text + inofs,
1264 not_url_len + long_url_len);
1266 inofs += not_url_len + long_url_len;
1267 outofs += not_url_len + long_url_len;
1270 /* copy the unmodified block */
1271 strncpy(text + outofs, text + inofs, not_url_len);
1272 inofs += not_url_len;
1273 outofs += not_url_len;
1275 /* copy the new url */
1276 strncpy(text + outofs, url, short_url_len);
1277 inofs += long_url_len;
1278 outofs += short_url_len;
1284 /* copy the last block after the last match */
1286 int tail = inlen - inofs;
1288 strncpy(text + outofs, text + inofs, tail);
1295 (void)pcloseRWE(shrink_pid, shrink_pipe);
1298 dbg("after len=%u\n", outofs);
1302 int main(int argc, char *argv[], char *envp[])
1304 static const struct option options[] = {
1305 { "debug", 0, NULL, 'd' },
1306 { "verbose", 0, NULL, 'V' },
1307 { "account", 1, NULL, 'a' },
1308 { "password", 1, NULL, 'p' },
1309 { "host", 1, NULL, 'H' },
1310 { "proxy", 1, NULL, 'P' },
1311 { "action", 1, NULL, 'A' },
1312 { "user", 1, NULL, 'u' },
1313 { "group", 1, NULL, 'G' },
1314 { "logfile", 1, NULL, 'L' },
1315 { "shrink-urls", 0, NULL, 's' },
1316 { "help", 0, NULL, 'h' },
1317 { "bash", 0, NULL, 'b' },
1318 { "background", 0, NULL, 'B' },
1319 { "dry-run", 0, NULL, 'n' },
1320 { "page", 1, NULL, 'g' },
1321 { "version", 0, NULL, 'v' },
1322 { "config", 1, NULL, 'c' },
1323 { "replyto", 1, NULL, 'r' },
1326 struct session *session;
1329 static char password[80];
1339 session = session_alloc();
1341 fprintf(stderr, "no more memory...\n");
1345 /* get the current time so that we can log it later */
1347 session->time = strdup(ctime(&t));
1348 session->time[strlen(session->time)-1] = 0x00;
1350 /* Get the home directory so we can try to find a config file */
1351 session->homedir = strdup(getenv("HOME"));
1353 /* set up a default config file location (traditionally ~/.bti) */
1354 session->configfile = zalloc(strlen(session->homedir) + 7);
1355 sprintf(session->configfile, "%s/.bti", session->homedir);
1357 /* Set environment variables first, before reading command line options
1358 * or config file values. */
1359 http_proxy = getenv("http_proxy");
1362 free(session->proxy);
1363 session->proxy = strdup(http_proxy);
1364 dbg("http_proxy = %s\n", session->proxy);
1367 parse_configfile(session);
1370 option = getopt_long_only(argc, argv,
1371 "dp:P:H:a:A:u:c:hg:G:sr:nVv",
1383 if (session->account)
1384 free(session->account);
1385 session->account = strdup(optarg);
1386 dbg("account = %s\n", session->account);
1389 page_nr = atoi(optarg);
1390 dbg("page = %d\n", page_nr);
1391 session->page = page_nr;
1394 session->replyto = strdup(optarg);
1395 dbg("in_reply_to_status_id = %s\n", session->replyto);
1398 if (session->password)
1399 free(session->password);
1400 session->password = strdup(optarg);
1401 dbg("password = %s\n", session->password);
1405 free(session->proxy);
1406 session->proxy = strdup(optarg);
1407 dbg("proxy = %s\n", session->proxy);
1410 if (strcasecmp(optarg, "update") == 0)
1411 session->action = ACTION_UPDATE;
1412 else if (strcasecmp(optarg, "friends") == 0)
1413 session->action = ACTION_FRIENDS;
1414 else if (strcasecmp(optarg, "user") == 0)
1415 session->action = ACTION_USER;
1416 else if (strcasecmp(optarg, "replies") == 0)
1417 session->action = ACTION_REPLIES;
1418 else if (strcasecmp(optarg, "public") == 0)
1419 session->action = ACTION_PUBLIC;
1420 else if (strcasecmp(optarg, "group") == 0)
1421 session->action = ACTION_GROUP;
1423 session->action = ACTION_UNKNOWN;
1424 dbg("action = %d\n", session->action);
1428 free(session->user);
1429 session->user = strdup(optarg);
1430 dbg("user = %s\n", session->user);
1435 free(session->group);
1436 session->group = strdup(optarg);
1437 dbg("group = %s\n", session->group);
1440 if (session->logfile)
1441 free(session->logfile);
1442 session->logfile = strdup(optarg);
1443 dbg("logfile = %s\n", session->logfile);
1446 session->shrink_urls = 1;
1449 if (session->hosturl)
1450 free(session->hosturl);
1451 if (session->hostname)
1452 free(session->hostname);
1453 if (strcasecmp(optarg, "twitter") == 0) {
1454 session->host = HOST_TWITTER;
1455 session->hosturl = strdup(twitter_host);
1456 session->hostname = strdup(twitter_name);
1457 } else if (strcasecmp(optarg, "identica") == 0) {
1458 session->host = HOST_IDENTICA;
1459 session->hosturl = strdup(identica_host);
1460 session->hostname = strdup(identica_name);
1462 session->host = HOST_CUSTOM;
1463 session->hosturl = strdup(optarg);
1464 session->hostname = strdup(optarg);
1466 dbg("host = %d\n", session->host);
1470 /* fall-through intended */
1472 session->background = 1;
1475 if (session->configfile)
1476 free(session->configfile);
1477 session->configfile = strdup(optarg);
1478 dbg("configfile = %s\n", session->configfile);
1481 * read the config file now. Yes, this could override
1482 * previously set options from the command line, but
1483 * the user asked for it...
1485 parse_configfile(session);
1491 session->dry_run = 1;
1502 session_readline_init(session);
1504 * Show the version to make it easier to determine what
1510 if (session->host == HOST_TWITTER) {
1511 if (!session->consumer_key || !session->consumer_secret) {
1513 "Twitter no longer supports HTTP basic authentication.\n"
1514 "Both consumer key, and consumer secret are required"
1515 " for bti in order to behave as an OAuth consumer.\n");
1518 if (session->action == ACTION_GROUP) {
1519 fprintf(stderr, "Groups only work in Identi.ca.\n");
1523 if (!session->consumer_key || !session->consumer_secret)
1524 session->no_oauth = 1;
1527 if (session->no_oauth) {
1528 if (!session->account) {
1529 fprintf(stdout, "Enter account for %s: ",
1531 session->account = session->readline(NULL);
1533 if (!session->password) {
1534 read_password(password, sizeof(password),
1536 session->password = strdup(password);
1539 if (!session->access_token_key ||
1540 !session->access_token_secret) {
1541 request_access_token(session);
1546 if (session->action == ACTION_UNKNOWN) {
1547 fprintf(stderr, "Unknown action, valid actions are:\n"
1548 "'update', 'friends', 'public', 'replies', 'group' or 'user'.\n");
1552 if (session->action == ACTION_GROUP && !session->group) {
1553 fprintf(stdout, "Enter group name: ");
1554 session->group = session->readline(NULL);
1557 if (session->action == ACTION_UPDATE) {
1558 if (session->background || !session->interactive)
1559 tweet = get_string_from_stdin();
1561 tweet = session->readline("tweet: ");
1562 if (!tweet || strlen(tweet) == 0) {
1567 if (session->shrink_urls)
1568 tweet = shrink_urls(tweet);
1570 session->tweet = zalloc(strlen(tweet) + 10);
1572 sprintf(session->tweet, "%c %s",
1573 getuid() ? '$' : '#', tweet);
1575 sprintf(session->tweet, "%s", tweet);
1578 dbg("tweet = %s\n", session->tweet);
1581 if (session->page == 0)
1583 dbg("config file = %s\n", session->configfile);
1584 dbg("host = %d\n", session->host);
1585 dbg("action = %d\n", session->action);
1587 /* fork ourself so that the main shell can get on
1588 * with it's life as we try to connect and handle everything
1590 if (session->background) {
1593 dbg("child is %d\n", child);
1598 retval = send_request(session);
1599 if (retval && !session->background)
1600 fprintf(stderr, "operation failed\n");
1602 log_session(session, retval);
1604 session_readline_cleanup(session);
1605 session_free(session);