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__ , \
79 char *consumer_secret;
80 char *access_token_key;
81 char *access_token_secret;
104 void *readline_handle;
105 char *(*readline)(const char *);
108 struct bti_curl_buffer {
114 static void display_help(void)
116 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n"
121 " --account accountname\n"
122 " --password password\n"
124 " ('update', 'friends', 'public', 'replies', or 'user')\n"
125 " --user screenname\n"
126 " --group groupname\n"
127 " --proxy PROXY:PORT\n"
129 " --logfile logfile\n"
130 " --config configfile\n"
134 " --page PAGENUMBER\n"
141 " --help\n", VERSION);
144 static void display_version(void)
146 fprintf(stdout, "bti - version %s\n", VERSION);
149 static char *get_string(const char *name)
154 string = zalloc(1000);
158 fprintf(stdout, "%s", name);
159 if (!fgets(string, 999, stdin))
161 temp = strchr(string, '\n');
168 * Try to get a handle to a readline function from a variety of different
169 * libraries. If nothing is present on the system, then fall back to an
172 * Logic originally based off of code in the e2fsutils package in the
173 * lib/ss/get_readline.c file, which is licensed under the MIT license.
175 * This keeps us from having to relicense the bti codebase if readline
176 * ever changes its license, as there is no link-time dependancy.
177 * It is a run-time thing only, and we handle any readline-like library
178 * in the same manner, making bti not be a derivative work of any
181 static void session_readline_init(struct session *session)
183 /* Libraries we will try to use for readline/editline functionality */
184 const char *libpath = "libreadline.so.6:libreadline.so.5:"
185 "libreadline.so.4:libreadline.so:libedit.so.2:"
186 "libedit.so:libeditline.so.0:libeditline.so";
188 char *tmp, *cp, *next;
189 int (*bind_key)(int, void *);
190 void (*insert)(void);
192 /* default to internal function if we can't or won't find anything */
193 session->readline = get_string;
196 session->interactive = 1;
198 tmp = malloc(strlen(libpath)+1);
201 strcpy(tmp, libpath);
202 for (cp = tmp; cp; cp = next) {
203 next = strchr(cp, ':');
208 handle = dlopen(cp, RTLD_NOW);
210 dbg("Using %s for readline library\n", cp);
216 dbg("No readline library found.\n");
220 session->readline_handle = handle;
221 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
222 if (session->readline == NULL) {
223 /* something odd happened, default back to internal stuff */
224 session->readline_handle = NULL;
225 session->readline = get_string;
230 * If we found a library, turn off filename expansion
231 * as that makes no sense from within bti.
233 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
234 insert = (void (*)(void))dlsym(handle, "rl_insert");
235 if (bind_key && insert)
236 bind_key('\t', insert);
239 static void session_readline_cleanup(struct session *session)
241 if (session->readline_handle)
242 dlclose(session->readline_handle);
245 static struct session *session_alloc(void)
247 struct session *session;
249 session = zalloc(sizeof(*session));
255 static void session_free(struct session *session)
259 free(session->retweet);
260 free(session->replyto);
261 free(session->password);
262 free(session->account);
263 free(session->consumer_key);
264 free(session->consumer_secret);
265 free(session->access_token_key);
266 free(session->access_token_secret);
267 free(session->tweet);
268 free(session->proxy);
270 free(session->homedir);
272 free(session->group);
273 free(session->hosturl);
274 free(session->hostname);
275 free(session->configfile);
279 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
281 struct bti_curl_buffer *buffer;
283 buffer = zalloc(sizeof(*buffer));
287 /* start out with a data buffer of 1 byte to
288 * make the buffer fill logic simpler */
289 buffer->data = zalloc(1);
295 buffer->action = action;
299 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
307 static const char twitter_host[] = "http://api.twitter.com/1/statuses";
308 static const char identica_host[] = "https://identi.ca/api/statuses";
309 static const char twitter_name[] = "twitter";
310 static const char identica_name[] = "identi.ca";
312 static const char twitter_request_token_uri[] = "http://twitter.com/oauth/request_token";
313 static const char twitter_access_token_uri[] = "http://twitter.com/oauth/access_token";
314 static const char twitter_authorize_uri[] = "http://twitter.com/oauth/authorize?oauth_token=";
315 static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token?oauth_callback=oob";
316 static const char identica_access_token_uri[] = "http://identi.ca/api/oauth/access_token";
317 static const char identica_authorize_uri[] = "http://identi.ca/api/oauth/authorize?oauth_token=";
319 static const char user_uri[] = "/user_timeline/";
320 static const char update_uri[] = "/update.xml";
321 static const char public_uri[] = "/public_timeline.xml";
322 static const char friends_uri[] = "/friends_timeline.xml";
323 static const char mentions_uri[] = "/mentions.xml";
324 static const char replies_uri[] = "/replies.xml";
325 static const char retweet_uri[] = "/retweet/";
326 static const char group_uri[] = "/../statusnet/groups/timeline/";
328 static CURL *curl_init(void)
332 curl = curl_easy_init();
334 fprintf(stderr, "Can not init CURL!\n");
337 /* some ssl sanity checks on the connection we are making */
338 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
339 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
343 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
345 xmlChar *text = NULL;
346 xmlChar *user = NULL;
347 xmlChar *created = NULL;
351 current = current->xmlChildrenNode;
352 while (current != NULL) {
353 if (current->type == XML_ELEMENT_NODE) {
354 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
355 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
356 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
357 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
358 if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
359 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
360 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
361 userinfo = current->xmlChildrenNode;
362 while (userinfo != NULL) {
363 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
366 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
368 userinfo = userinfo->next;
372 if (user && text && created && id) {
374 printf("[%s] {%s} (%.16s) %s\n",
375 user, id, created, text);
389 current = current->next;
395 static void parse_timeline(char *document)
400 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
401 NULL, XML_PARSE_NOERROR);
405 current = xmlDocGetRootElement(doc);
406 if (current == NULL) {
407 fprintf(stderr, "empty document\n");
412 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
413 fprintf(stderr, "unexpected document type\n");
418 current = current->xmlChildrenNode;
419 while (current != NULL) {
420 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
421 parse_statuses(doc, current);
422 current = current->next;
429 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
432 struct bti_curl_buffer *curl_buf = userp;
433 size_t buffer_size = size * nmemb;
436 if ((!buffer) || (!buffer_size) || (!curl_buf))
439 /* add to the data we already have */
440 temp = zalloc(curl_buf->length + buffer_size + 1);
444 memcpy(temp, curl_buf->data, curl_buf->length);
445 free(curl_buf->data);
446 curl_buf->data = temp;
447 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
448 curl_buf->length += buffer_size;
449 if (curl_buf->action)
450 parse_timeline(curl_buf->data);
452 dbg("%s\n", curl_buf->data);
457 static int parse_osp_reply(const char *reply, char **token, char **secret)
462 rc = oauth_split_url_parameters(reply, &rv);
463 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
464 if (rc == 2 || rc == 4) {
465 if (!strncmp(rv[0], "oauth_token=", 11) &&
466 !strncmp(rv[1], "oauth_token_secret=", 18)) {
468 *token = strdup(&(rv[0][12]));
470 *secret = strdup(&(rv[1][19]));
474 } else if (rc == 3) {
475 if (!strncmp(rv[1], "oauth_token=", 11) &&
476 !strncmp(rv[2], "oauth_token_secret=", 18)) {
478 *token = strdup(&(rv[1][12]));
480 *secret = strdup(&(rv[2][19]));
486 dbg("token: %s\n", *token);
487 dbg("secret: %s\n", *secret);
495 static int request_access_token(struct session *session)
497 char *post_params = NULL;
498 char *request_url = NULL;
501 char *at_secret = NULL;
502 char *verifier = NULL;
508 if (session->host == HOST_TWITTER)
509 request_url = oauth_sign_url2(
510 twitter_request_token_uri, NULL,
511 OA_HMAC, NULL, session->consumer_key,
512 session->consumer_secret, NULL, NULL);
513 else if (session->host == HOST_IDENTICA)
514 request_url = oauth_sign_url2(
515 identica_request_token_uri, NULL,
516 OA_HMAC, NULL, session->consumer_key,
517 session->consumer_secret, NULL, NULL);
518 reply = oauth_http_get(request_url, post_params);
529 if (parse_osp_reply(reply, &at_key, &at_secret))
535 "Please open the following link in your browser, and "
536 "allow 'bti' to access your account. Then paste "
537 "back the provided PIN in here.\n");
538 if (session->host == HOST_TWITTER) {
539 fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
540 verifier = session->readline(NULL);
541 sprintf(at_uri, "%s?oauth_verifier=%s",
542 twitter_access_token_uri, verifier);
543 } else if (session->host == HOST_IDENTICA) {
544 fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key);
545 verifier = session->readline(NULL);
546 sprintf(at_uri, "%s?oauth_verifier=%s",
547 identica_access_token_uri, verifier);
549 request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
550 session->consumer_key,
551 session->consumer_secret,
553 reply = oauth_http_get(request_url, post_params);
558 if (parse_osp_reply(reply, &at_key, &at_secret))
564 "Please put these two lines in your bti "
565 "configuration file (~/.bti):\n"
566 "access_token_key=%s\n"
567 "access_token_secret=%s\n",
573 static int send_request(struct session *session)
576 char user_password[500];
578 struct bti_curl_buffer *curl_buf;
581 struct curl_httppost *formpost = NULL;
582 struct curl_httppost *lastptr = NULL;
583 struct curl_slist *slist = NULL;
584 char *req_url = NULL;
586 char *postarg = NULL;
587 char *escaped_tweet = NULL;
593 if (!session->hosturl)
594 session->hosturl = strdup(twitter_host);
596 if (session->no_oauth || session->guest) {
597 curl_buf = bti_curl_buffer_alloc(session->action);
605 if (!session->hosturl)
606 session->hosturl = strdup(twitter_host);
608 switch (session->action) {
610 snprintf(user_password, sizeof(user_password), "%s:%s",
611 session->account, session->password);
612 snprintf(data, sizeof(data), "status=\"%s\"",
614 curl_formadd(&formpost, &lastptr,
615 CURLFORM_COPYNAME, "status",
616 CURLFORM_COPYCONTENTS, session->tweet,
619 curl_formadd(&formpost, &lastptr,
620 CURLFORM_COPYNAME, "source",
621 CURLFORM_COPYCONTENTS, "bti",
624 if (session->replyto)
625 curl_formadd(&formpost, &lastptr,
626 CURLFORM_COPYNAME, "in_reply_to_status_id",
627 CURLFORM_COPYCONTENTS, session->replyto,
630 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
631 slist = curl_slist_append(slist, "Expect:");
632 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
634 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
635 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
636 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
640 snprintf(user_password, sizeof(user_password), "%s:%s",
641 session->account, session->password);
642 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
643 friends_uri, session->page);
644 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
645 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
649 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
650 user_uri, session->user, session->page);
651 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
655 snprintf(user_password, sizeof(user_password), "%s:%s",
656 session->account, session->password);
657 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
658 replies_uri, session->page);
659 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
660 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
664 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
665 public_uri, session->page);
666 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
670 sprintf(endpoint, "%s%s%s.xml?page=%d",
671 session->hosturl, group_uri, session->group,
673 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
681 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
684 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
686 dbg("user_password = %s\n", user_password);
687 dbg("data = %s\n", data);
688 dbg("proxy = %s\n", session->proxy);
690 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
691 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
692 if (!session->dry_run) {
693 res = curl_easy_perform(curl);
694 if (res && !session->background) {
695 fprintf(stderr, "error(%d) trying to perform "
701 curl_easy_cleanup(curl);
702 if (session->action == ACTION_UPDATE)
703 curl_formfree(formpost);
704 bti_curl_buffer_free(curl_buf);
706 switch (session->action) {
708 escaped_tweet = oauth_url_escape(session->tweet);
709 if (session->replyto) {
711 "%s%s?status=%s&in_reply_to_status_id=%s",
712 session->hosturl, update_uri,
713 escaped_tweet, session->replyto);
715 sprintf(endpoint, "%s%s?status=%s",
716 session->hosturl, update_uri,
723 sprintf(endpoint, "%s%s%s.xml?page=%d",
724 session->hosturl, user_uri, session->user,
728 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
729 mentions_uri, session->page);
732 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
733 public_uri, session->page);
736 sprintf(endpoint, "%s%s%s.xml?page=%d",
737 session->hosturl, group_uri, session->group,
741 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
742 friends_uri, session->page);
745 sprintf(endpoint, "%s%s%s.xml", session->hosturl,
746 retweet_uri, session->retweet);
753 dbg("%s\n", endpoint);
754 if (!session->dry_run) {
756 req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC,
757 NULL, session->consumer_key,
758 session->consumer_secret,
759 session->access_token_key,
760 session->access_token_secret);
761 reply = oauth_http_post(req_url, postarg);
763 req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
764 session->consumer_key,
765 session->consumer_secret,
766 session->access_token_key,
767 session->access_token_secret);
768 reply = oauth_http_get(req_url, postarg);
771 dbg("%s\n", req_url);
777 if ((session->action != ACTION_UPDATE) &&
778 (session->action != ACTION_RETWEET))
779 parse_timeline(reply);
784 static void parse_configfile(struct session *session)
789 char *account = NULL;
790 char *password = NULL;
791 char *consumer_key = NULL;
792 char *consumer_secret = NULL;
793 char *access_token_key = NULL;
794 char *access_token_secret = NULL;
797 char *logfile = NULL;
800 char *replyto = NULL;
801 char *retweet = NULL;
804 config_file = fopen(session->configfile, "r");
806 /* No error if file does not exist or is unreadable. */
807 if (config_file == NULL)
811 ssize_t n = getline(&line, &len, config_file);
814 if (line[n - 1] == '\n')
816 /* Parse file. Format is the usual value pairs:
819 # is a comment character
821 *strchrnul(line, '#') = '\0';
825 /* Ignore blank lines. */
829 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
833 } else if (!strncasecmp(c, "password", 8) &&
837 password = strdup(c);
838 } else if (!strncasecmp(c, "consumer_key", 12) &&
842 consumer_key = strdup(c);
843 } else if (!strncasecmp(c, "consumer_secret", 15) &&
847 consumer_secret = strdup(c);
848 } else if (!strncasecmp(c, "access_token_key", 16) &&
852 access_token_key = strdup(c);
853 } else if (!strncasecmp(c, "access_token_secret", 19) &&
857 access_token_secret = strdup(c);
858 } else if (!strncasecmp(c, "host", 4) &&
863 } else if (!strncasecmp(c, "proxy", 5) &&
868 } else if (!strncasecmp(c, "logfile", 7) &&
873 } else if (!strncasecmp(c, "replyto", 7) &&
878 } else if (!strncasecmp(c, "action", 6) &&
883 } else if (!strncasecmp(c, "user", 4) &&
888 } else if (!strncasecmp(c, "shrink-urls", 11) &&
891 if (!strncasecmp(c, "true", 4) ||
892 !strncasecmp(c, "yes", 3))
894 } else if (!strncasecmp(c, "verbose", 7) &&
897 if (!strncasecmp(c, "true", 4) ||
898 !strncasecmp(c, "yes", 3))
900 } else if (!strncasecmp(c,"retweet", 7) &&
906 } while (!feof(config_file));
909 session->password = password;
911 session->account = account;
913 session->consumer_key = consumer_key;
915 session->consumer_secret = consumer_secret;
916 if (access_token_key)
917 session->access_token_key = access_token_key;
918 if (access_token_secret)
919 session->access_token_secret = access_token_secret;
921 if (strcasecmp(host, "twitter") == 0) {
922 session->host = HOST_TWITTER;
923 session->hosturl = strdup(twitter_host);
924 session->hostname = strdup(twitter_name);
925 } else if (strcasecmp(host, "identica") == 0) {
926 session->host = HOST_IDENTICA;
927 session->hosturl = strdup(identica_host);
928 session->hostname = strdup(identica_name);
930 session->host = HOST_CUSTOM;
931 session->hosturl = strdup(host);
932 session->hostname = strdup(host);
938 free(session->proxy);
939 session->proxy = proxy;
942 session->logfile = logfile;
944 session->replyto = replyto;
946 session->retweet = retweet;
948 if (strcasecmp(action, "update") == 0)
949 session->action = ACTION_UPDATE;
950 else if (strcasecmp(action, "friends") == 0)
951 session->action = ACTION_FRIENDS;
952 else if (strcasecmp(action, "user") == 0)
953 session->action = ACTION_USER;
954 else if (strcasecmp(action, "replies") == 0)
955 session->action = ACTION_REPLIES;
956 else if (strcasecmp(action, "public") == 0)
957 session->action = ACTION_PUBLIC;
958 else if (strcasecmp(action, "group") == 0)
959 session->action = ACTION_GROUP;
961 session->action = ACTION_UNKNOWN;
965 session->user = user;
966 session->shrink_urls = shrink_urls;
968 /* Free buffer and close file. */
973 static void log_session(struct session *session, int retval)
978 /* Only log something if we have a log file set */
979 if (!session->logfile)
982 filename = alloca(strlen(session->homedir) +
983 strlen(session->logfile) + 3);
985 sprintf(filename, "%s/%s", session->homedir, session->logfile);
987 log_file = fopen(filename, "a+");
988 if (log_file == NULL)
991 switch (session->action) {
994 fprintf(log_file, "%s: host=%s tweet failed\n",
995 session->time, session->hostname);
997 fprintf(log_file, "%s: host=%s tweet=%s\n",
998 session->time, session->hostname,
1001 case ACTION_FRIENDS:
1002 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
1003 session->time, session->hostname);
1006 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
1007 session->time, session->hostname, session->user);
1009 case ACTION_REPLIES:
1010 fprintf(log_file, "%s: host=%s retrieving replies\n",
1011 session->time, session->hostname);
1014 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
1015 session->time, session->hostname);
1018 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
1019 session->time, session->hostname);
1028 static char *get_string_from_stdin(void)
1033 string = zalloc(1000);
1037 if (!fgets(string, 999, stdin))
1039 temp = strchr(string, '\n');
1045 static void read_password(char *buf, size_t len, char *host)
1055 tp.c_lflag &= (~ECHO);
1056 tcsetattr(0, TCSANOW, &tp);
1058 fprintf(stdout, "Enter password for %s: ", host);
1061 retval = scanf("%79s", pwd);
1063 fprintf(stdout, "\n");
1065 tcsetattr(0, TCSANOW, &old);
1067 strncpy(buf, pwd, len);
1071 static int find_urls(const char *tweet, int **pranges)
1074 * magic obtained from
1075 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
1077 static const char *re_magic =
1078 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
1079 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
1080 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
1084 int ovector[10] = {0,};
1085 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
1086 int startoffset, tweetlen;
1090 int *ranges = malloc(sizeof(int) * rbound);
1092 re = pcre_compile(re_magic,
1093 PCRE_NO_AUTO_CAPTURE,
1094 &errptr, &erroffset, NULL);
1096 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
1100 tweetlen = strlen(tweet);
1101 for (startoffset = 0; startoffset < tweetlen; ) {
1103 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
1105 if (rc == PCRE_ERROR_NOMATCH)
1109 fprintf(stderr, "pcre_exec @%u: %s\n",
1114 for (i = 0; i < rc; i += 2) {
1115 if ((rcount+2) == rbound) {
1117 ranges = realloc(ranges, sizeof(int) * rbound);
1120 ranges[rcount++] = ovector[i];
1121 ranges[rcount++] = ovector[i+1];
1124 startoffset = ovector[1];
1134 * bidirectional popen() call
1136 * @param rwepipe - int array of size three
1137 * @param exe - program to run
1138 * @param argv - argument list
1139 * @return pid or -1 on error
1141 * The caller passes in an array of three integers (rwepipe), on successful
1142 * execution it can then write to element 0 (stdin of exe), and read from
1143 * element 1 (stdout) and 2 (stderr).
1145 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
1172 rwepipe[1] = out[0];
1173 rwepipe[2] = err[0];
1175 } else if (pid == 0) {
1187 execvp(exe, (char **)argv);
1207 static int pcloseRWE(int pid, int *rwepipe)
1213 rc = waitpid(pid, &status, 0);
1217 static char *shrink_one_url(int *rwepipe, char *big)
1219 int biglen = strlen(big);
1224 rc = dprintf(rwepipe[0], "%s\n", big);
1228 smalllen = biglen + 128;
1229 small = malloc(smalllen);
1233 rc = read(rwepipe[1], small, smalllen);
1234 if (rc < 0 || rc > biglen)
1235 goto error_free_small;
1237 if (strncmp(small, "http://", 7))
1238 goto error_free_small;
1241 while (smalllen && isspace(small[smalllen-1]))
1242 small[--smalllen] = 0;
1252 static char *shrink_urls(char *text)
1259 const char *const shrink_args[] = {
1265 int inlen = strlen(text);
1267 dbg("before len=%u\n", inlen);
1269 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1273 rcount = find_urls(text, &ranges);
1277 for (i = 0; i < rcount; i += 2) {
1278 int url_start = ranges[i];
1279 int url_end = ranges[i+1];
1280 int long_url_len = url_end - url_start;
1281 char *url = strndup(text + url_start, long_url_len);
1283 int not_url_len = url_start - inofs;
1285 dbg("long url[%u]: %s\n", long_url_len, url);
1286 url = shrink_one_url(shrink_pipe, url);
1287 short_url_len = url ? strlen(url) : 0;
1288 dbg("short url[%u]: %s\n", short_url_len, url);
1290 if (!url || short_url_len >= long_url_len) {
1291 /* The short url ended up being too long
1294 strncpy(text + outofs, text + inofs,
1295 not_url_len + long_url_len);
1297 inofs += not_url_len + long_url_len;
1298 outofs += not_url_len + long_url_len;
1301 /* copy the unmodified block */
1302 strncpy(text + outofs, text + inofs, not_url_len);
1303 inofs += not_url_len;
1304 outofs += not_url_len;
1306 /* copy the new url */
1307 strncpy(text + outofs, url, short_url_len);
1308 inofs += long_url_len;
1309 outofs += short_url_len;
1315 /* copy the last block after the last match */
1317 int tail = inlen - inofs;
1319 strncpy(text + outofs, text + inofs, tail);
1326 (void)pcloseRWE(shrink_pid, shrink_pipe);
1329 dbg("after len=%u\n", outofs);
1333 int main(int argc, char *argv[], char *envp[])
1335 static const struct option options[] = {
1336 { "debug", 0, NULL, 'd' },
1337 { "verbose", 0, NULL, 'V' },
1338 { "account", 1, NULL, 'a' },
1339 { "password", 1, NULL, 'p' },
1340 { "host", 1, NULL, 'H' },
1341 { "proxy", 1, NULL, 'P' },
1342 { "action", 1, NULL, 'A' },
1343 { "user", 1, NULL, 'u' },
1344 { "group", 1, NULL, 'G' },
1345 { "logfile", 1, NULL, 'L' },
1346 { "shrink-urls", 0, NULL, 's' },
1347 { "help", 0, NULL, 'h' },
1348 { "bash", 0, NULL, 'b' },
1349 { "background", 0, NULL, 'B' },
1350 { "dry-run", 0, NULL, 'n' },
1351 { "page", 1, NULL, 'g' },
1352 { "version", 0, NULL, 'v' },
1353 { "config", 1, NULL, 'c' },
1354 { "replyto", 1, NULL, 'r' },
1355 { "retweet", 1, NULL, 'w' },
1358 struct session *session;
1362 static char password[80];
1372 session = session_alloc();
1374 fprintf(stderr, "no more memory...\n");
1378 /* get the current time so that we can log it later */
1380 session->time = strdup(ctime(&t));
1381 session->time[strlen(session->time)-1] = 0x00;
1383 /* Get the home directory so we can try to find a config file */
1384 session->homedir = strdup(getenv("HOME"));
1386 /* set up a default config file location (traditionally ~/.bti) */
1387 session->configfile = zalloc(strlen(session->homedir) + 7);
1388 sprintf(session->configfile, "%s/.bti", session->homedir);
1390 /* Set environment variables first, before reading command line options
1391 * or config file values. */
1392 http_proxy = getenv("http_proxy");
1395 free(session->proxy);
1396 session->proxy = strdup(http_proxy);
1397 dbg("http_proxy = %s\n", session->proxy);
1400 parse_configfile(session);
1403 option = getopt_long_only(argc, argv,
1404 "dp:P:H:a:A:u:c:hg:G:sr:nVv",
1416 if (session->account)
1417 free(session->account);
1418 session->account = strdup(optarg);
1419 dbg("account = %s\n", session->account);
1422 page_nr = atoi(optarg);
1423 dbg("page = %d\n", page_nr);
1424 session->page = page_nr;
1427 session->replyto = strdup(optarg);
1428 dbg("in_reply_to_status_id = %s\n", session->replyto);
1431 session->retweet = strdup(optarg);
1432 dbg("Retweet ID = %s\n", session->retweet);
1435 if (session->password)
1436 free(session->password);
1437 session->password = strdup(optarg);
1438 dbg("password = %s\n", session->password);
1442 free(session->proxy);
1443 session->proxy = strdup(optarg);
1444 dbg("proxy = %s\n", session->proxy);
1447 if (strcasecmp(optarg, "update") == 0)
1448 session->action = ACTION_UPDATE;
1449 else if (strcasecmp(optarg, "friends") == 0)
1450 session->action = ACTION_FRIENDS;
1451 else if (strcasecmp(optarg, "user") == 0)
1452 session->action = ACTION_USER;
1453 else if (strcasecmp(optarg, "replies") == 0)
1454 session->action = ACTION_REPLIES;
1455 else if (strcasecmp(optarg, "public") == 0)
1456 session->action = ACTION_PUBLIC;
1457 else if (strcasecmp(optarg, "group") == 0)
1458 session->action = ACTION_GROUP;
1459 else if (strcasecmp(optarg,"retweet") == 0)
1460 session->action = ACTION_RETWEET;
1462 session->action = ACTION_UNKNOWN;
1463 dbg("action = %d\n", session->action);
1467 free(session->user);
1468 session->user = strdup(optarg);
1469 dbg("user = %s\n", session->user);
1474 free(session->group);
1475 session->group = strdup(optarg);
1476 dbg("group = %s\n", session->group);
1479 if (session->logfile)
1480 free(session->logfile);
1481 session->logfile = strdup(optarg);
1482 dbg("logfile = %s\n", session->logfile);
1485 session->shrink_urls = 1;
1488 if (session->hosturl)
1489 free(session->hosturl);
1490 if (session->hostname)
1491 free(session->hostname);
1492 if (strcasecmp(optarg, "twitter") == 0) {
1493 session->host = HOST_TWITTER;
1494 session->hosturl = strdup(twitter_host);
1495 session->hostname = strdup(twitter_name);
1496 } else if (strcasecmp(optarg, "identica") == 0) {
1497 session->host = HOST_IDENTICA;
1498 session->hosturl = strdup(identica_host);
1499 session->hostname = strdup(identica_name);
1501 session->host = HOST_CUSTOM;
1502 session->hosturl = strdup(optarg);
1503 session->hostname = strdup(optarg);
1505 dbg("host = %d\n", session->host);
1509 /* fall-through intended */
1511 session->background = 1;
1514 if (session->configfile)
1515 free(session->configfile);
1516 session->configfile = strdup(optarg);
1517 dbg("configfile = %s\n", session->configfile);
1520 * read the config file now. Yes, this could override
1521 * previously set options from the command line, but
1522 * the user asked for it...
1524 parse_configfile(session);
1530 session->dry_run = 1;
1541 session_readline_init(session);
1543 * Show the version to make it easier to determine what
1549 if (session->host == HOST_TWITTER) {
1550 if (!session->consumer_key || !session->consumer_secret) {
1551 if (session->action == ACTION_USER ||
1552 session->action == ACTION_PUBLIC) {
1553 /* Some actions may still work without authentication */
1557 "Twitter no longer supports HTTP basic authentication.\n"
1558 "Both consumer key, and consumer secret are required"
1559 " for bti in order to behave as an OAuth consumer.\n");
1563 if (session->action == ACTION_GROUP) {
1564 fprintf(stderr, "Groups only work in Identi.ca.\n");
1568 if (!session->consumer_key || !session->consumer_secret)
1569 session->no_oauth = 1;
1572 if (session->no_oauth) {
1573 if (!session->account) {
1574 fprintf(stdout, "Enter account for %s: ",
1576 session->account = session->readline(NULL);
1578 if (!session->password) {
1579 read_password(password, sizeof(password),
1581 session->password = strdup(password);
1583 } else if (!session->guest) {
1584 if (!session->access_token_key ||
1585 !session->access_token_secret) {
1586 request_access_token(session);
1591 if (session->action == ACTION_UNKNOWN) {
1592 fprintf(stderr, "Unknown action, valid actions are:\n"
1593 "'update', 'friends', 'public', 'replies', 'group' or 'user'.\n");
1597 if (session->action == ACTION_GROUP && !session->group) {
1598 fprintf(stdout, "Enter group name: ");
1599 session->group = session->readline(NULL);
1602 if (session->action == ACTION_RETWEET) {
1603 fprintf(stdout, "Status ID to retweet: ");
1604 retweet = get_string_from_stdin();
1606 if (!retweet || strlen(retweet) == 0) {
1607 dbg("no retweet?\n");
1611 session->retweet = zalloc(strlen(retweet) + 10);
1612 sprintf(session->retweet,"%s", retweet);
1614 dbg("retweet ID = %s\n", session->retweet);
1617 if (session->action == ACTION_UPDATE) {
1618 if (session->background || !session->interactive)
1619 tweet = get_string_from_stdin();
1621 tweet = session->readline("tweet: ");
1622 if (!tweet || strlen(tweet) == 0) {
1627 if (session->shrink_urls)
1628 tweet = shrink_urls(tweet);
1630 session->tweet = zalloc(strlen(tweet) + 10);
1632 sprintf(session->tweet, "%c %s",
1633 getuid() ? '$' : '#', tweet);
1635 sprintf(session->tweet, "%s", tweet);
1638 dbg("tweet = %s\n", session->tweet);
1641 if (session->page == 0)
1643 dbg("config file = %s\n", session->configfile);
1644 dbg("host = %d\n", session->host);
1645 dbg("action = %d\n", session->action);
1647 /* fork ourself so that the main shell can get on
1648 * with it's life as we try to connect and handle everything
1650 if (session->background) {
1653 dbg("child is %d\n", child);
1658 retval = send_request(session);
1659 if (retval && !session->background)
1660 fprintf(stderr, "operation failed\n");
1662 log_session(session, retval);
1664 session_readline_cleanup(session);
1665 session_free(session);