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;
99 void *readline_handle;
100 char *(*readline)(const char *);
103 struct bti_curl_buffer {
109 static void display_help(void)
111 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n");
112 fprintf(stdout, "Version: " VERSION "\n");
113 fprintf(stdout, "Usage:\n");
114 fprintf(stdout, " bti [options]\n");
115 fprintf(stdout, "options are:\n");
116 fprintf(stdout, " --account accountname\n");
117 fprintf(stdout, " --password password\n");
118 fprintf(stdout, " --action action\n");
119 fprintf(stdout, " ('update', 'friends', 'public', 'replies', "
120 "'group' or 'user')\n");
121 fprintf(stdout, " --user screenname\n");
122 fprintf(stdout, " --group groupname\n");
123 fprintf(stdout, " --proxy PROXY:PORT\n");
124 fprintf(stdout, " --host HOST\n");
125 fprintf(stdout, " --logfile logfile\n");
126 fprintf(stdout, " --config configfile\n");
127 fprintf(stdout, " --shrink-urls\n");
128 fprintf(stdout, " --page PAGENUMBER\n");
129 fprintf(stdout, " --bash\n");
130 fprintf(stdout, " --debug\n");
131 fprintf(stdout, " --verbose\n");
132 fprintf(stdout, " --dry-run\n");
133 fprintf(stdout, " --version\n");
134 fprintf(stdout, " --help\n");
137 static void display_version(void)
139 fprintf(stdout, "bti - version %s\n", VERSION);
142 static char *get_string(const char *name)
147 string = zalloc(1000);
151 fprintf(stdout, "%s", name);
152 if (!fgets(string, 999, stdin))
154 temp = strchr(string, '\n');
161 * Try to get a handle to a readline function from a variety of different
162 * libraries. If nothing is present on the system, then fall back to an
165 * Logic originally based off of code in the e2fsutils package in the
166 * lib/ss/get_readline.c file, which is licensed under the MIT license.
168 * This keeps us from having to relicense the bti codebase if readline
169 * ever changes its license, as there is no link-time dependancy.
170 * It is a run-time thing only, and we handle any readline-like library
171 * in the same manner, making bti not be a derivative work of any
174 static void session_readline_init(struct session *session)
176 /* Libraries we will try to use for readline/editline functionality */
177 const char *libpath = "libreadline.so.6:libreadline.so.5:"
178 "libreadline.so.4:libreadline.so:libedit.so.2:"
179 "libedit.so:libeditline.so.0:libeditline.so";
181 char *tmp, *cp, *next;
182 int (*bind_key)(int, void *);
183 void (*insert)(void);
185 /* default to internal function if we can't or won't find anything */
186 session->readline = get_string;
189 session->interactive = 1;
191 tmp = malloc(strlen(libpath)+1);
194 strcpy(tmp, libpath);
195 for (cp = tmp; cp; cp = next) {
196 next = strchr(cp, ':');
201 handle = dlopen(cp, RTLD_NOW);
203 dbg("Using %s for readline library\n", cp);
209 dbg("No readline library found.\n");
213 session->readline_handle = handle;
214 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
215 if (session->readline == NULL) {
216 /* something odd happened, default back to internal stuff */
217 session->readline_handle = NULL;
218 session->readline = get_string;
223 * If we found a library, turn off filename expansion
224 * as that makes no sense from within bti.
226 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
227 insert = (void (*)(void))dlsym(handle, "rl_insert");
228 if (bind_key && insert)
229 bind_key('\t', insert);
232 static void session_readline_cleanup(struct session *session)
234 if (session->readline_handle)
235 dlclose(session->readline_handle);
238 static struct session *session_alloc(void)
240 struct session *session;
242 session = zalloc(sizeof(*session));
248 static void session_free(struct session *session)
252 free(session->password);
253 free(session->account);
254 free(session->consumer_key);
255 free(session->consumer_secret);
256 free(session->access_token_key);
257 free(session->access_token_secret);
258 free(session->tweet);
259 free(session->proxy);
261 free(session->homedir);
263 free(session->group);
264 free(session->hosturl);
265 free(session->hostname);
266 free(session->configfile);
270 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
272 struct bti_curl_buffer *buffer;
274 buffer = zalloc(sizeof(*buffer));
278 /* start out with a data buffer of 1 byte to
279 * make the buffer fill logic simpler */
280 buffer->data = zalloc(1);
286 buffer->action = action;
290 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
298 static const char *twitter_host = "http://api.twitter.com/1/statuses";
299 static const char *identica_host = "https://identi.ca/api/statuses";
300 static const char *twitter_name = "twitter";
301 static const char *identica_name = "identi.ca";
303 static const char *twitter_request_token_uri = "http://twitter.com/oauth/request_token";
304 static const char *twitter_access_token_uri = "http://twitter.com/oauth/access_token";
305 static const char *twitter_authorize_uri = "http://twitter.com/oauth/authorize?oauth_token=";
306 static const char *identica_request_token_uri = "http://identi.ca/api/oauth/request_token";
307 static const char *identica_access_token_uri = "http://identi.ca/api/oauth/access_token";
308 static const char *identica_authorize_uri = "http://identi.ca/api/oauth/authorize?oauth_token=";
310 static const char *user_uri = "/user_timeline/";
311 static const char *update_uri = "/update.xml";
312 static const char *public_uri = "/public_timeline.xml";
313 static const char *friends_uri = "/friends_timeline.xml";
314 static const char *mentions_uri = "/mentions.xml";
315 static const char *replies_uri = "/replies.xml";
316 static const char *group_uri = "/../statusnet/groups/timeline/";
318 static CURL *curl_init(void)
322 curl = curl_easy_init();
324 fprintf(stderr, "Can not init CURL!\n");
327 /* some ssl sanity checks on the connection we are making */
328 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
329 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
333 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
335 xmlChar *text = NULL;
336 xmlChar *user = NULL;
337 xmlChar *created = NULL;
340 current = current->xmlChildrenNode;
341 while (current != NULL) {
342 if (current->type == XML_ELEMENT_NODE) {
343 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
344 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
345 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
346 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
347 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
348 userinfo = current->xmlChildrenNode;
349 while (userinfo != NULL) {
350 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
353 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
355 userinfo = userinfo->next;
359 if (user && text && created) {
361 printf("[%s] (%.16s) %s\n",
362 user, created, text);
374 current = current->next;
380 static void parse_timeline(char *document)
385 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
386 NULL, XML_PARSE_NOERROR);
390 current = xmlDocGetRootElement(doc);
391 if (current == NULL) {
392 fprintf(stderr, "empty document\n");
397 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
398 fprintf(stderr, "unexpected document type\n");
403 current = current->xmlChildrenNode;
404 while (current != NULL) {
405 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
406 parse_statuses(doc, current);
407 current = current->next;
414 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
417 struct bti_curl_buffer *curl_buf = userp;
418 size_t buffer_size = size * nmemb;
421 if ((!buffer) || (!buffer_size) || (!curl_buf))
424 /* add to the data we already have */
425 temp = zalloc(curl_buf->length + buffer_size + 1);
429 memcpy(temp, curl_buf->data, curl_buf->length);
430 free(curl_buf->data);
431 curl_buf->data = temp;
432 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
433 curl_buf->length += buffer_size;
434 if (curl_buf->action)
435 parse_timeline(curl_buf->data);
437 dbg("%s\n", curl_buf->data);
442 static int parse_osp_reply(const char *reply, char **token, char **secret)
447 rc = oauth_split_url_parameters(reply, &rv);
448 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
449 if (rc == 2 || rc == 4) {
450 if (!strncmp(rv[0],"oauth_token=",11) && !strncmp(rv[1],"oauth_token_secret=",18)) {
452 *token =strdup(&(rv[0][12]));
454 *secret=strdup(&(rv[1][19]));
458 } else if (rc == 3) {
459 if (!strncmp(rv[1],"oauth_token=",11) && !strncmp(rv[2],"oauth_token_secret=",18)) {
461 *token =strdup(&(rv[1][12]));
463 *secret=strdup(&(rv[2][19]));
469 dbg("token: %s\n", *token);
470 dbg("secret: %s\n", *secret);
479 static int request_access_token(struct session *session)
481 char *post_params = NULL;
482 char *request_url = NULL;
485 char *at_secret = NULL;
486 char *verifier = NULL;
492 if (session->host == HOST_TWITTER)
493 request_url = oauth_sign_url2(
494 twitter_request_token_uri, NULL,
495 OA_HMAC, NULL, session->consumer_key,
496 session->consumer_secret, NULL, NULL);
497 else if (session->host == HOST_IDENTICA)
498 request_url = oauth_sign_url2(
499 identica_request_token_uri, NULL,
500 OA_HMAC, NULL, session->consumer_key,
501 session->consumer_secret, NULL, NULL);
502 reply = oauth_http_get(request_url, post_params);
513 if (parse_osp_reply(reply, &at_key, &at_secret))
518 fprintf(stdout, "Please open the following link in your browser, and ");
519 fprintf(stdout, "allow 'bti' to access your account. Then paste ");
520 fprintf(stdout, "back the provided PIN in here.\n");
521 if (session->host == HOST_TWITTER) {
522 fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
523 verifier = session->readline(NULL);
524 sprintf(at_uri, "%s?oauth_verifier=%s", twitter_access_token_uri, verifier);
525 } else if (session->host == HOST_IDENTICA) {
526 fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key);
527 verifier = session->readline(NULL);
528 sprintf(at_uri, "%s?oauth_verifier=%s", identica_access_token_uri, verifier);
530 request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
531 session->consumer_key, session->consumer_secret, at_key,
533 reply = oauth_http_get(request_url, post_params);
538 if (parse_osp_reply(reply, &at_key, &at_secret))
543 fprintf(stdout, "Please put these two lines in your bti configuration ");
544 fprintf(stdout, "file (~/.bti):\n");
545 fprintf(stdout, "access_token_key=%s\n", at_key);
546 fprintf(stdout, "access_token_secret=%s\n", at_secret);
551 static int send_request(struct session *session)
554 char user_password[500];
556 struct bti_curl_buffer *curl_buf;
559 struct curl_httppost *formpost = NULL;
560 struct curl_httppost *lastptr = NULL;
561 struct curl_slist *slist = NULL;
562 char *req_url = NULL;
564 char *postarg = NULL;
570 if (!session->hosturl)
571 session->hosturl = strdup(twitter_host);
573 if (session->no_oauth) {
574 curl_buf = bti_curl_buffer_alloc(session->action);
582 if (!session->hosturl)
583 session->hosturl = strdup(twitter_host);
585 switch (session->action) {
587 snprintf(user_password, sizeof(user_password), "%s:%s",
588 session->account, session->password);
589 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
590 curl_formadd(&formpost, &lastptr,
591 CURLFORM_COPYNAME, "status",
592 CURLFORM_COPYCONTENTS, session->tweet,
595 curl_formadd(&formpost, &lastptr,
596 CURLFORM_COPYNAME, "source",
597 CURLFORM_COPYCONTENTS, "bti",
600 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
601 slist = curl_slist_append(slist, "Expect:");
602 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
604 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
605 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
606 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
610 snprintf(user_password, sizeof(user_password), "%s:%s",
611 session->account, session->password);
612 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
613 friends_uri, session->page);
614 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
615 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
619 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
620 user_uri, session->user, session->page);
621 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
625 snprintf(user_password, sizeof(user_password), "%s:%s",
626 session->account, session->password);
627 sprintf(endpoint, "%s%s?page=%d", session->hosturl, replies_uri,
629 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
630 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
634 sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri,
636 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
640 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
641 group_uri, session->group, session->page);
642 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
650 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
653 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
655 dbg("user_password = %s\n", user_password);
656 dbg("data = %s\n", data);
657 dbg("proxy = %s\n", session->proxy);
659 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
660 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
661 if (!session->dry_run) {
662 res = curl_easy_perform(curl);
663 if (res && !session->bash) {
664 fprintf(stderr, "error(%d) trying to perform "
670 curl_easy_cleanup(curl);
671 if (session->action == ACTION_UPDATE)
672 curl_formfree(formpost);
673 bti_curl_buffer_free(curl_buf);
675 switch (session->action) {
679 session->hosturl, update_uri, session->tweet);
683 sprintf(endpoint, "%s%s%s.xml?page=%d",
684 session->hosturl, user_uri,
685 session->user, session->page);
688 sprintf(endpoint, "%s%s?page=%d",
689 session->hosturl, mentions_uri, session->page);
692 sprintf(endpoint, "%s%s?page=%d",
693 session->hosturl, public_uri, session->page);
696 sprintf(endpoint, "%s%s%s.xml?page=%d",
697 session->hosturl, group_uri,
698 session->group, session->page);
701 sprintf(endpoint, "%s%s?page=%d",
702 session->hosturl, friends_uri, session->page);
709 req_url = oauth_sign_url2(
710 endpoint, &postarg, OA_HMAC, NULL,
711 session->consumer_key, session->consumer_secret,
712 session->access_token_key, session->access_token_secret
714 reply = oauth_http_post(req_url, postarg);
716 req_url = oauth_sign_url2(
717 endpoint, NULL, OA_HMAC, NULL,
718 session->consumer_key, session->consumer_secret,
719 session->access_token_key, session->access_token_secret
721 reply = oauth_http_get(req_url, postarg);
724 dbg("%s\n", req_url);
729 if (session->action != ACTION_UPDATE)
730 parse_timeline(reply);
735 static void parse_configfile(struct session *session)
740 char *account = NULL;
741 char *password = NULL;
742 char *consumer_key = NULL;
743 char *consumer_secret = NULL;
744 char *access_token_key = NULL;
745 char *access_token_secret = NULL;
748 char *logfile = NULL;
753 config_file = fopen(session->configfile, "r");
755 /* No error if file does not exist or is unreadable. */
756 if (config_file == NULL)
760 ssize_t n = getline(&line, &len, config_file);
763 if (line[n - 1] == '\n')
765 /* Parse file. Format is the usual value pairs:
768 # is a comment character
770 *strchrnul(line, '#') = '\0';
774 /* Ignore blank lines. */
778 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
782 } else if (!strncasecmp(c, "password", 8) &&
786 password = strdup(c);
787 } else if (!strncasecmp(c, "consumer_key", 12) &&
791 consumer_key = strdup(c);
792 } else if (!strncasecmp(c, "consumer_secret", 15) &&
796 consumer_secret = strdup(c);
797 } else if (!strncasecmp(c, "access_token_key", 16) &&
801 access_token_key = strdup(c);
802 } else if (!strncasecmp(c, "access_token_secret", 19) &&
806 access_token_secret = strdup(c);
807 } else if (!strncasecmp(c, "host", 4) &&
812 } else if (!strncasecmp(c, "proxy", 5) &&
817 } else if (!strncasecmp(c, "logfile", 7) &&
822 } else if (!strncasecmp(c, "action", 6) &&
827 } else if (!strncasecmp(c, "user", 4) &&
832 } else if (!strncasecmp(c, "shrink-urls", 11) &&
835 if (!strncasecmp(c, "true", 4) ||
836 !strncasecmp(c, "yes", 3))
838 } else if (!strncasecmp(c, "verbose", 7) &&
841 if (!strncasecmp(c, "true", 4) ||
842 !strncasecmp(c, "yes", 3))
845 } while (!feof(config_file));
848 session->password = password;
850 session->account = account;
852 session->consumer_key = consumer_key;
854 session->consumer_secret = consumer_secret;
855 if (access_token_key)
856 session->access_token_key = access_token_key;
857 if (access_token_secret)
858 session->access_token_secret = access_token_secret;
860 if (strcasecmp(host, "twitter") == 0) {
861 session->host = HOST_TWITTER;
862 session->hosturl = strdup(twitter_host);
863 session->hostname = strdup(twitter_name);
864 } else if (strcasecmp(host, "identica") == 0) {
865 session->host = HOST_IDENTICA;
866 session->hosturl = strdup(identica_host);
867 session->hostname = strdup(identica_name);
869 session->host = HOST_CUSTOM;
870 session->hosturl = strdup(host);
871 session->hostname = strdup(host);
877 free(session->proxy);
878 session->proxy = proxy;
881 session->logfile = logfile;
883 if (strcasecmp(action, "update") == 0)
884 session->action = ACTION_UPDATE;
885 else if (strcasecmp(action, "friends") == 0)
886 session->action = ACTION_FRIENDS;
887 else if (strcasecmp(action, "user") == 0)
888 session->action = ACTION_USER;
889 else if (strcasecmp(action, "replies") == 0)
890 session->action = ACTION_REPLIES;
891 else if (strcasecmp(action, "public") == 0)
892 session->action = ACTION_PUBLIC;
893 else if (strcasecmp(action, "group") == 0)
894 session->action = ACTION_GROUP;
896 session->action = ACTION_UNKNOWN;
900 session->user = user;
901 session->shrink_urls = shrink_urls;
903 /* Free buffer and close file. */
908 static void log_session(struct session *session, int retval)
913 /* Only log something if we have a log file set */
914 if (!session->logfile)
917 filename = alloca(strlen(session->homedir) +
918 strlen(session->logfile) + 3);
920 sprintf(filename, "%s/%s", session->homedir, session->logfile);
922 log_file = fopen(filename, "a+");
923 if (log_file == NULL)
926 switch (session->action) {
929 fprintf(log_file, "%s: host=%s tweet failed\n",
930 session->time, session->hostname);
932 fprintf(log_file, "%s: host=%s tweet=%s\n",
933 session->time, session->hostname,
937 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
938 session->time, session->hostname);
941 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
942 session->time, session->hostname, session->user);
945 fprintf(log_file, "%s: host=%s retrieving replies\n",
946 session->time, session->hostname);
949 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
950 session->time, session->hostname);
953 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
954 session->time, session->hostname);
963 static char *get_string_from_stdin(void)
968 string = zalloc(1000);
972 if (!fgets(string, 999, stdin))
974 temp = strchr(string, '\n');
980 static void read_password(char *buf, size_t len, char *host)
990 tp.c_lflag &= (~ECHO);
991 tcsetattr(0, TCSANOW, &tp);
993 fprintf(stdout, "Enter password for %s: ", host);
996 retval = scanf("%79s", pwd);
998 fprintf(stdout, "\n");
1000 tcsetattr(0, TCSANOW, &old);
1002 strncpy(buf, pwd, len);
1006 static int find_urls(const char *tweet, int **pranges)
1009 * magic obtained from
1010 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
1012 static const char *re_magic =
1013 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
1014 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
1015 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
1019 int ovector[10] = {0,};
1020 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
1021 int startoffset, tweetlen;
1025 int *ranges = malloc(sizeof(int) * rbound);
1027 re = pcre_compile(re_magic,
1028 PCRE_NO_AUTO_CAPTURE,
1029 &errptr, &erroffset, NULL);
1031 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
1035 tweetlen = strlen(tweet);
1036 for (startoffset = 0; startoffset < tweetlen; ) {
1038 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
1040 if (rc == PCRE_ERROR_NOMATCH)
1044 fprintf(stderr, "pcre_exec @%u: %s\n",
1049 for (i = 0; i < rc; i += 2) {
1050 if ((rcount+2) == rbound) {
1052 ranges = realloc(ranges, sizeof(int) * rbound);
1055 ranges[rcount++] = ovector[i];
1056 ranges[rcount++] = ovector[i+1];
1059 startoffset = ovector[1];
1069 * bidirectional popen() call
1071 * @param rwepipe - int array of size three
1072 * @param exe - program to run
1073 * @param argv - argument list
1074 * @return pid or -1 on error
1076 * The caller passes in an array of three integers (rwepipe), on successful
1077 * execution it can then write to element 0 (stdin of exe), and read from
1078 * element 1 (stdout) and 2 (stderr).
1080 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
1107 rwepipe[1] = out[0];
1108 rwepipe[2] = err[0];
1110 } else if (pid == 0) {
1122 execvp(exe, (char **)argv);
1142 static int pcloseRWE(int pid, int *rwepipe)
1148 rc = waitpid(pid, &status, 0);
1152 static char *shrink_one_url(int *rwepipe, char *big)
1154 int biglen = strlen(big);
1159 rc = dprintf(rwepipe[0], "%s\n", big);
1163 smalllen = biglen + 128;
1164 small = malloc(smalllen);
1168 rc = read(rwepipe[1], small, smalllen);
1169 if (rc < 0 || rc > biglen)
1170 goto error_free_small;
1172 if (strncmp(small, "http://", 7))
1173 goto error_free_small;
1176 while (smalllen && isspace(small[smalllen-1]))
1177 small[--smalllen] = 0;
1187 static char *shrink_urls(char *text)
1194 const char *const shrink_args[] = {
1200 int inlen = strlen(text);
1202 dbg("before len=%u\n", inlen);
1204 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1208 rcount = find_urls(text, &ranges);
1212 for (i = 0; i < rcount; i += 2) {
1213 int url_start = ranges[i];
1214 int url_end = ranges[i+1];
1215 int long_url_len = url_end - url_start;
1216 char *url = strndup(text + url_start, long_url_len);
1218 int not_url_len = url_start - inofs;
1220 dbg("long url[%u]: %s\n", long_url_len, url);
1221 url = shrink_one_url(shrink_pipe, url);
1222 short_url_len = url ? strlen(url) : 0;
1223 dbg("short url[%u]: %s\n", short_url_len, url);
1225 if (!url || short_url_len >= long_url_len) {
1226 /* The short url ended up being too long
1229 strncpy(text + outofs, text + inofs,
1230 not_url_len + long_url_len);
1232 inofs += not_url_len + long_url_len;
1233 outofs += not_url_len + long_url_len;
1236 /* copy the unmodified block */
1237 strncpy(text + outofs, text + inofs, not_url_len);
1238 inofs += not_url_len;
1239 outofs += not_url_len;
1241 /* copy the new url */
1242 strncpy(text + outofs, url, short_url_len);
1243 inofs += long_url_len;
1244 outofs += short_url_len;
1250 /* copy the last block after the last match */
1252 int tail = inlen - inofs;
1254 strncpy(text + outofs, text + inofs, tail);
1261 (void)pcloseRWE(shrink_pid, shrink_pipe);
1264 dbg("after len=%u\n", outofs);
1268 int main(int argc, char *argv[], char *envp[])
1270 static const struct option options[] = {
1271 { "debug", 0, NULL, 'd' },
1272 { "verbose", 0, NULL, 'V' },
1273 { "host", 1, NULL, 'H' },
1274 { "proxy", 1, NULL, 'P' },
1275 { "action", 1, NULL, 'A' },
1276 { "user", 1, NULL, 'u' },
1277 { "group", 1, NULL, 'G' },
1278 { "logfile", 1, NULL, 'L' },
1279 { "shrink-urls", 0, NULL, 's' },
1280 { "help", 0, NULL, 'h' },
1281 { "bash", 0, NULL, 'b' },
1282 { "dry-run", 0, NULL, 'n' },
1283 { "page", 1, NULL, 'g' },
1284 { "version", 0, NULL, 'v' },
1285 { "config", 1, NULL, 'c' },
1288 struct session *session;
1291 static char password[80];
1301 session = session_alloc();
1303 fprintf(stderr, "no more memory...\n");
1307 /* get the current time so that we can log it later */
1309 session->time = strdup(ctime(&t));
1310 session->time[strlen(session->time)-1] = 0x00;
1312 /* Get the home directory so we can try to find a config file */
1313 session->homedir = strdup(getenv("HOME"));
1315 /* set up a default config file location (traditionally ~/.bti) */
1316 session->configfile = zalloc(strlen(session->homedir) + 7);
1317 sprintf(session->configfile, "%s/.bti", session->homedir);
1319 /* Set environment variables first, before reading command line options
1320 * or config file values. */
1321 http_proxy = getenv("http_proxy");
1324 free(session->proxy);
1325 session->proxy = strdup(http_proxy);
1326 dbg("http_proxy = %s\n", session->proxy);
1329 parse_configfile(session);
1332 option = getopt_long_only(argc, argv, "dp:P:H:a:A:u:c:hg:G:snVv",
1344 page_nr = atoi(optarg);
1345 dbg("page = %d\n", page_nr);
1346 session->page = page_nr;
1350 free(session->proxy);
1351 session->proxy = strdup(optarg);
1352 dbg("proxy = %s\n", session->proxy);
1355 if (strcasecmp(optarg, "update") == 0)
1356 session->action = ACTION_UPDATE;
1357 else if (strcasecmp(optarg, "friends") == 0)
1358 session->action = ACTION_FRIENDS;
1359 else if (strcasecmp(optarg, "user") == 0)
1360 session->action = ACTION_USER;
1361 else if (strcasecmp(optarg, "replies") == 0)
1362 session->action = ACTION_REPLIES;
1363 else if (strcasecmp(optarg, "public") == 0)
1364 session->action = ACTION_PUBLIC;
1365 else if (strcasecmp(optarg, "group") == 0)
1366 session->action = ACTION_GROUP;
1368 session->action = ACTION_UNKNOWN;
1369 dbg("action = %d\n", session->action);
1373 free(session->user);
1374 session->user = strdup(optarg);
1375 dbg("user = %s\n", session->user);
1380 free(session->group);
1381 session->group = strdup(optarg);
1382 dbg("group = %s\n", session->group);
1385 if (session->logfile)
1386 free(session->logfile);
1387 session->logfile = strdup(optarg);
1388 dbg("logfile = %s\n", session->logfile);
1391 session->shrink_urls = 1;
1394 if (session->hosturl)
1395 free(session->hosturl);
1396 if (session->hostname)
1397 free(session->hostname);
1398 if (strcasecmp(optarg, "twitter") == 0) {
1399 session->host = HOST_TWITTER;
1400 session->hosturl = strdup(twitter_host);
1401 session->hostname = strdup(twitter_name);
1402 } else if (strcasecmp(optarg, "identica") == 0) {
1403 session->host = HOST_IDENTICA;
1404 session->hosturl = strdup(identica_host);
1405 session->hostname = strdup(identica_name);
1407 session->host = HOST_CUSTOM;
1408 session->hosturl = strdup(optarg);
1409 session->hostname = strdup(optarg);
1411 dbg("host = %d\n", session->host);
1417 if (session->configfile)
1418 free(session->configfile);
1419 session->configfile = strdup(optarg);
1420 dbg("configfile = %s\n", session->configfile);
1423 * read the config file now. Yes, this could override previously
1424 * set options from the command line, but the user asked for it...
1426 parse_configfile(session);
1432 session->dry_run = 1;
1443 session_readline_init(session);
1445 * Show the version to make it easier to determine what
1451 if (session->host == HOST_TWITTER) {
1452 if (!session->consumer_key || !session->consumer_secret) {
1453 fprintf(stderr, "Twitter no longer supuports HTTP basic authentication.\n");
1454 fprintf(stderr, "Both consumer key, and consumer secret are required");
1455 fprintf(stderr, " for bti in order to behave as an OAuth consumer.\n");
1458 if (session->action == ACTION_GROUP) {
1459 fprintf(stderr, "Groups only work in Identi.ca.\n");
1463 if (!session->consumer_key || !session->consumer_secret) {
1464 session->no_oauth = 1;
1468 if (session->no_oauth) {
1469 if (!session->account) {
1470 fprintf(stdout, "Enter account for %s: ", session->hostname);
1471 session->account = session->readline(NULL);
1473 if (!session->password) {
1474 read_password(password, sizeof(password), session->hostname);
1475 session->password = strdup(password);
1478 if (!session->access_token_key || !session->access_token_secret) {
1479 request_access_token(session);
1484 if (session->action == ACTION_UNKNOWN) {
1485 fprintf(stderr, "Unknown action, valid actions are:\n");
1486 fprintf(stderr, "'update', 'friends', 'public', "
1487 "'replies', 'group' or 'user'.\n");
1491 if (session->action == ACTION_GROUP && !session->group) {
1492 fprintf(stdout, "Enter group name: ");
1493 session->group = session->readline(NULL);
1496 if (session->action == ACTION_UPDATE) {
1497 if (session->bash || !session->interactive)
1498 tweet = get_string_from_stdin();
1500 tweet = session->readline("tweet: ");
1501 if (!tweet || strlen(tweet) == 0) {
1506 if (session->shrink_urls)
1507 tweet = shrink_urls(tweet);
1509 session->tweet = zalloc(strlen(tweet) + 10);
1511 sprintf(session->tweet, "%c %s",
1512 getuid() ? '$' : '#', tweet);
1514 sprintf(session->tweet, "%s", tweet);
1517 dbg("tweet = %s\n", session->tweet);
1520 if (session->page == 0)
1522 dbg("config file = %s\n", session->configfile);
1523 dbg("host = %d\n", session->host);
1524 dbg("action = %d\n", session->action);
1526 /* fork ourself so that the main shell can get on
1527 * with it's life as we try to connect and handle everything
1529 if (session->bash) {
1532 dbg("child is %d\n", child);
1537 retval = send_request(session);
1538 if (retval && !session->bash)
1539 fprintf(stderr, "operation failed\n");
1541 log_session(session, retval);
1543 session_readline_cleanup(session);
1544 session_free(session);