debian/watch: use github redirector, there are no released tarballs at the
[debian/bti.git] / bti.c
1 /*
2  * Copyright (C) 2008-2011 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
4  * Copyright (C) 2009-2010 Amir Mohammad Saied <amirsaied@gmail.com>
5  *
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.
9  *
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.
14  *
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.
18  */
19
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <curl/curl.h>
36 #include <libxml/xmlmemory.h>
37 #include <libxml/parser.h>
38 #include <libxml/tree.h>
39 #include <pcre.h>
40 #include <termios.h>
41 #include <dlfcn.h>
42 #include <oauth.h>
43 #include "bti.h"
44
45 #define zalloc(size)    calloc(size, 1)
46
47 #define dbg(format, arg...)                                             \
48         do {                                                            \
49                 if (debug)                                              \
50                         fprintf(stdout, "bti: %s: " format , __func__ , \
51                                 ## arg);                                \
52         } while (0)
53
54
55 int debug;
56
57 static void display_help(void)
58 {
59         fprintf(stdout, "bti - send tweet to twitter or identi.ca\n"
60                 "Version: %s\n"
61                 "Usage:\n"
62                 "  bti [options]\n"
63                 "options are:\n"
64                 "  --account accountname\n"
65                 "  --password password\n"
66                 "  --action action\n"
67                 "    ('update', 'friends', 'public', 'replies', or 'user')\n"
68                 "  --user screenname\n"
69                 "  --group groupname\n"
70                 "  --proxy PROXY:PORT\n"
71                 "  --host HOST\n"
72                 "  --logfile logfile\n"
73                 "  --config configfile\n"
74                 "  --replyto ID\n"
75                 "  --retweet ID\n"
76                 "  --shrink-urls\n"
77                 "  --page PAGENUMBER\n"
78                 "  --bash\n"
79                 "  --background\n"
80                 "  --debug\n"
81                 "  --verbose\n"
82                 "  --dry-run\n"
83                 "  --version\n"
84                 "  --help\n", VERSION);
85 }
86
87 static void display_version(void)
88 {
89         fprintf(stdout, "bti - version %s\n", VERSION);
90 }
91
92 static char *get_string(const char *name)
93 {
94         char *temp;
95         char *string;
96
97         string = zalloc(1000);
98         if (!string)
99                 exit(1);
100         if (name != NULL)
101                 fprintf(stdout, "%s", name);
102         if (!fgets(string, 999, stdin))
103                 return NULL;
104         temp = strchr(string, '\n');
105         if (temp)
106                 *temp = '\0';
107         return string;
108 }
109
110 /*
111  * Try to get a handle to a readline function from a variety of different
112  * libraries.  If nothing is present on the system, then fall back to an
113  * internal one.
114  *
115  * Logic originally based off of code in the e2fsutils package in the
116  * lib/ss/get_readline.c file, which is licensed under the MIT license.
117  *
118  * This keeps us from having to relicense the bti codebase if readline
119  * ever changes its license, as there is no link-time dependancy.
120  * It is a run-time thing only, and we handle any readline-like library
121  * in the same manner, making bti not be a derivative work of any
122  * other program.
123  */
124 static void session_readline_init(struct session *session)
125 {
126         /* Libraries we will try to use for readline/editline functionality */
127         const char *libpath = "libreadline.so.6:libreadline.so.5:"
128                                 "libreadline.so.4:libreadline.so:libedit.so.2:"
129                                 "libedit.so:libeditline.so.0:libeditline.so";
130         void *handle = NULL;
131         char *tmp, *cp, *next;
132         int (*bind_key)(int, void *);
133         void (*insert)(void);
134
135         /* default to internal function if we can't or won't find anything */
136         session->readline = get_string;
137         if (!isatty(0))
138                 return;
139         session->interactive = 1;
140
141         tmp = malloc(strlen(libpath)+1);
142         if (!tmp)
143                 return;
144         strcpy(tmp, libpath);
145         for (cp = tmp; cp; cp = next) {
146                 next = strchr(cp, ':');
147                 if (next)
148                         *next++ = 0;
149                 if (*cp == 0)
150                         continue;
151                 handle = dlopen(cp, RTLD_NOW);
152                 if (handle) {
153                         dbg("Using %s for readline library\n", cp);
154                         break;
155                 }
156         }
157         free(tmp);
158         if (!handle) {
159                 dbg("No readline library found.\n");
160                 return;
161         }
162
163         session->readline_handle = handle;
164         session->readline = (char *(*)(const char *))dlsym(handle, "readline");
165         if (session->readline == NULL) {
166                 /* something odd happened, default back to internal stuff */
167                 session->readline_handle = NULL;
168                 session->readline = get_string;
169                 return;
170         }
171
172         /*
173          * If we found a library, turn off filename expansion
174          * as that makes no sense from within bti.
175          */
176         bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
177         insert = (void (*)(void))dlsym(handle, "rl_insert");
178         if (bind_key && insert)
179                 bind_key('\t', insert);
180 }
181
182 static void session_readline_cleanup(struct session *session)
183 {
184         if (session->readline_handle)
185                 dlclose(session->readline_handle);
186 }
187
188 static struct session *session_alloc(void)
189 {
190         struct session *session;
191
192         session = zalloc(sizeof(*session));
193         if (!session)
194                 return NULL;
195         return session;
196 }
197
198 static void session_free(struct session *session)
199 {
200         if (!session)
201                 return;
202         free(session->retweet);
203         free(session->replyto);
204         free(session->password);
205         free(session->account);
206         free(session->consumer_key);
207         free(session->consumer_secret);
208         free(session->access_token_key);
209         free(session->access_token_secret);
210         free(session->tweet);
211         free(session->proxy);
212         free(session->time);
213         free(session->homedir);
214         free(session->user);
215         free(session->group);
216         free(session->hosturl);
217         free(session->hostname);
218         free(session->configfile);
219         free(session);
220 }
221
222 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
223 {
224         struct bti_curl_buffer *buffer;
225
226         buffer = zalloc(sizeof(*buffer));
227         if (!buffer)
228                 return NULL;
229
230         /* start out with a data buffer of 1 byte to
231          * make the buffer fill logic simpler */
232         buffer->data = zalloc(1);
233         if (!buffer->data) {
234                 free(buffer);
235                 return NULL;
236         }
237         buffer->length = 0;
238         buffer->action = action;
239         return buffer;
240 }
241
242 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
243 {
244         if (!buffer)
245                 return;
246         free(buffer->data);
247         free(buffer);
248 }
249
250 const char twitter_host[]  = "http://api.twitter.com/1/statuses";
251 const char identica_host[] = "https://identi.ca/api/statuses";
252 const char twitter_name[]  = "twitter";
253 const char identica_name[] = "identi.ca";
254
255 static const char twitter_request_token_uri[]  = "http://twitter.com/oauth/request_token";
256 static const char twitter_access_token_uri[]   = "http://twitter.com/oauth/access_token";
257 static const char twitter_authorize_uri[]      = "http://twitter.com/oauth/authorize?oauth_token=";
258 static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token?oauth_callback=oob";
259 static const char identica_access_token_uri[]  = "http://identi.ca/api/oauth/access_token";
260 static const char identica_authorize_uri[]     = "http://identi.ca/api/oauth/authorize?oauth_token=";
261
262 static const char user_uri[]     = "/user_timeline/";
263 static const char update_uri[]   = "/update.xml";
264 static const char public_uri[]   = "/public_timeline.xml";
265 static const char friends_uri[]  = "/friends_timeline.xml";
266 static const char mentions_uri[] = "/mentions.xml";
267 static const char replies_uri[]  = "/replies.xml";
268 static const char retweet_uri[]  = "/retweet/";
269 static const char group_uri[]    = "/../statusnet/groups/timeline/";
270
271 static CURL *curl_init(void)
272 {
273         CURL *curl;
274
275         curl = curl_easy_init();
276         if (!curl) {
277                 fprintf(stderr, "Can not init CURL!\n");
278                 return NULL;
279         }
280         /* some ssl sanity checks on the connection we are making */
281         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
282         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
283         return curl;
284 }
285
286 /* The final place data is sent to the screen/pty/tty */
287 static void bti_output_line(struct session *session, xmlChar *user,
288                             xmlChar *id, xmlChar *created, xmlChar *text)
289 {
290         if (session->verbose)
291                 printf("[%s] {%s} (%.16s) %s\n", user, id, created, text);
292         else
293                 printf("[%s] %s\n", user, text);
294 }
295
296 static void parse_statuses(struct session *session,
297                            xmlDocPtr doc, xmlNodePtr current)
298 {
299         xmlChar *text = NULL;
300         xmlChar *user = NULL;
301         xmlChar *created = NULL;
302         xmlChar *id = NULL;
303         xmlNodePtr userinfo;
304
305         current = current->xmlChildrenNode;
306         while (current != NULL) {
307                 if (current->type == XML_ELEMENT_NODE) {
308                         if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
309                                 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
310                         if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
311                                 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
312                         if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
313                                 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
314                         if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
315                                 userinfo = current->xmlChildrenNode;
316                                 while (userinfo != NULL) {
317                                         if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
318                                                 if (user)
319                                                         xmlFree(user);
320                                                 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
321                                         }
322                                         userinfo = userinfo->next;
323                                 }
324                         }
325
326                         if (user && text && created && id) {
327                                 bti_output_line(session, user, id,
328                                                 created, text);
329                                 xmlFree(user);
330                                 xmlFree(text);
331                                 xmlFree(created);
332                                 xmlFree(id);
333                                 user = NULL;
334                                 text = NULL;
335                                 created = NULL;
336                                 id = NULL;
337                         }
338                 }
339                 current = current->next;
340         }
341
342         return;
343 }
344
345 static void parse_timeline(char *document, struct session *session)
346 {
347         xmlDocPtr doc;
348         xmlNodePtr current;
349
350         doc = xmlReadMemory(document, strlen(document), "timeline.xml",
351                             NULL, XML_PARSE_NOERROR);
352         if (doc == NULL)
353                 return;
354
355         current = xmlDocGetRootElement(doc);
356         if (current == NULL) {
357                 fprintf(stderr, "empty document\n");
358                 xmlFreeDoc(doc);
359                 return;
360         }
361
362         if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
363                 fprintf(stderr, "unexpected document type\n");
364                 xmlFreeDoc(doc);
365                 return;
366         }
367
368         current = current->xmlChildrenNode;
369         while (current != NULL) {
370                 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
371                         parse_statuses(session, doc, current);
372                 current = current->next;
373         }
374         xmlFreeDoc(doc);
375
376         return;
377 }
378
379 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
380                             void *userp)
381 {
382         struct bti_curl_buffer *curl_buf = userp;
383         size_t buffer_size = size * nmemb;
384         char *temp;
385
386         if ((!buffer) || (!buffer_size) || (!curl_buf))
387                 return -EINVAL;
388
389         /* add to the data we already have */
390         temp = zalloc(curl_buf->length + buffer_size + 1);
391         if (!temp)
392                 return -ENOMEM;
393
394         memcpy(temp, curl_buf->data, curl_buf->length);
395         free(curl_buf->data);
396         curl_buf->data = temp;
397         memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
398         curl_buf->length += buffer_size;
399         if (curl_buf->action)
400                 parse_timeline(curl_buf->data, curl_buf->session);
401
402         dbg("%s\n", curl_buf->data);
403
404         return buffer_size;
405 }
406
407 static int parse_osp_reply(const char *reply, char **token, char **secret)
408 {
409         int rc;
410         int retval = 1;
411         char **rv = NULL;
412         rc = oauth_split_url_parameters(reply, &rv);
413         qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
414         if (rc == 2 || rc == 4) {
415                 if (!strncmp(rv[0], "oauth_token=", 11) &&
416                     !strncmp(rv[1], "oauth_token_secret=", 18)) {
417                         if (token)
418                                 *token = strdup(&(rv[0][12]));
419                         if (secret)
420                                 *secret = strdup(&(rv[1][19]));
421
422                         retval = 0;
423                 }
424         } else if (rc == 3) {
425                 if (!strncmp(rv[1], "oauth_token=", 11) &&
426                     !strncmp(rv[2], "oauth_token_secret=", 18)) {
427                         if (token)
428                                 *token = strdup(&(rv[1][12]));
429                         if (secret)
430                                 *secret = strdup(&(rv[2][19]));
431
432                         retval = 0;
433                 }
434         }
435
436         dbg("token: %s\n", *token);
437         dbg("secret: %s\n", *secret);
438
439         if (rv)
440                 free(rv);
441
442         return retval;
443 }
444
445 static int request_access_token(struct session *session)
446 {
447         char *post_params = NULL;
448         char *request_url = NULL;
449         char *reply       = NULL;
450         char *at_key      = NULL;
451         char *at_secret   = NULL;
452         char *verifier    = NULL;
453         char at_uri[90];
454
455         if (!session)
456                 return -EINVAL;
457
458         if (session->host == HOST_TWITTER)
459                 request_url = oauth_sign_url2(
460                                 twitter_request_token_uri, NULL,
461                                 OA_HMAC, NULL, session->consumer_key,
462                                 session->consumer_secret, NULL, NULL);
463         else if (session->host == HOST_IDENTICA)
464                 request_url = oauth_sign_url2(
465                                 identica_request_token_uri, NULL,
466                                 OA_HMAC, NULL, session->consumer_key,
467                                 session->consumer_secret, NULL, NULL);
468         reply = oauth_http_get(request_url, post_params);
469
470         if (request_url)
471                 free(request_url);
472
473         if (post_params)
474                 free(post_params);
475
476         if (!reply)
477                 return 1;
478
479         if (parse_osp_reply(reply, &at_key, &at_secret))
480                 return 1;
481
482         free(reply);
483
484         fprintf(stdout,
485                 "Please open the following link in your browser, and "
486                 "allow 'bti' to access your account. Then paste "
487                 "back the provided PIN in here.\n");
488         if (session->host == HOST_TWITTER) {
489                 fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
490                 verifier = session->readline(NULL);
491                 sprintf(at_uri, "%s?oauth_verifier=%s",
492                         twitter_access_token_uri, verifier);
493         } else if (session->host == HOST_IDENTICA) {
494                 fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key);
495                 verifier = session->readline(NULL);
496                 sprintf(at_uri, "%s?oauth_verifier=%s",
497                         identica_access_token_uri, verifier);
498         }
499         request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
500                                       session->consumer_key,
501                                       session->consumer_secret,
502                                       at_key, at_secret);
503         reply = oauth_http_get(request_url, post_params);
504
505         if (!reply)
506                 return 1;
507
508         if (parse_osp_reply(reply, &at_key, &at_secret))
509                 return 1;
510
511         free(reply);
512
513         fprintf(stdout,
514                 "Please put these two lines in your bti "
515                 "configuration file (~/.bti):\n"
516                 "access_token_key=%s\n"
517                 "access_token_secret=%s\n",
518                 at_key, at_secret);
519
520         return 0;
521 }
522
523 static int send_request(struct session *session)
524 {
525         char endpoint[500];
526         char user_password[500];
527         char data[500];
528         struct bti_curl_buffer *curl_buf;
529         CURL *curl = NULL;
530         CURLcode res;
531         struct curl_httppost *formpost = NULL;
532         struct curl_httppost *lastptr = NULL;
533         struct curl_slist *slist = NULL;
534         char *req_url = NULL;
535         char *reply = NULL;
536         char *postarg = NULL;
537         char *escaped_tweet = NULL;
538         int is_post = 0;
539
540         if (!session)
541                 return -EINVAL;
542
543         if (!session->hosturl)
544                 session->hosturl = strdup(twitter_host);
545
546         if (session->no_oauth || session->guest) {
547                 curl_buf = bti_curl_buffer_alloc(session->action);
548                 if (!curl_buf)
549                         return -ENOMEM;
550                 curl_buf->session = session;
551
552                 curl = curl_init();
553                 if (!curl)
554                         return -EINVAL;
555
556                 if (!session->hosturl)
557                         session->hosturl = strdup(twitter_host);
558
559                 switch (session->action) {
560                 case ACTION_UPDATE:
561                         snprintf(user_password, sizeof(user_password), "%s:%s",
562                                  session->account, session->password);
563                         snprintf(data, sizeof(data), "status=\"%s\"",
564                                  session->tweet);
565                         curl_formadd(&formpost, &lastptr,
566                                      CURLFORM_COPYNAME, "status",
567                                      CURLFORM_COPYCONTENTS, session->tweet,
568                                      CURLFORM_END);
569
570                         curl_formadd(&formpost, &lastptr,
571                                      CURLFORM_COPYNAME, "source",
572                                      CURLFORM_COPYCONTENTS, "bti",
573                                      CURLFORM_END);
574
575                         if (session->replyto)
576                                 curl_formadd(&formpost, &lastptr,
577                                              CURLFORM_COPYNAME,
578                                              "in_reply_to_status_id",
579                                              CURLFORM_COPYCONTENTS,
580                                              session->replyto,
581                                              CURLFORM_END);
582
583                         curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
584                         slist = curl_slist_append(slist, "Expect:");
585                         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
586
587                         sprintf(endpoint, "%s%s", session->hosturl, update_uri);
588                         curl_easy_setopt(curl, CURLOPT_URL, endpoint);
589                         curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
590                         break;
591
592                 case ACTION_FRIENDS:
593                         snprintf(user_password, sizeof(user_password), "%s:%s",
594                                  session->account, session->password);
595                         sprintf(endpoint, "%s%s?page=%d", session->hosturl,
596                                         friends_uri, session->page);
597                         curl_easy_setopt(curl, CURLOPT_URL, endpoint);
598                         curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
599                         break;
600
601                 case ACTION_USER:
602                         sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
603                                 user_uri, session->user, session->page);
604                         curl_easy_setopt(curl, CURLOPT_URL, endpoint);
605                         break;
606
607                 case ACTION_REPLIES:
608                         snprintf(user_password, sizeof(user_password), "%s:%s",
609                                  session->account, session->password);
610                         sprintf(endpoint, "%s%s?page=%d", session->hosturl,
611                                 replies_uri, session->page);
612                         curl_easy_setopt(curl, CURLOPT_URL, endpoint);
613                         curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
614                         break;
615
616                 case ACTION_PUBLIC:
617                         sprintf(endpoint, "%s%s?page=%d", session->hosturl,
618                                 public_uri, session->page);
619                         curl_easy_setopt(curl, CURLOPT_URL, endpoint);
620                         break;
621
622                 case ACTION_GROUP:
623                         sprintf(endpoint, "%s%s%s.xml?page=%d",
624                                 session->hosturl, group_uri, session->group,
625                                 session->page);
626                         curl_easy_setopt(curl, CURLOPT_URL, endpoint);
627                         break;
628
629                 default:
630                         break;
631                 }
632
633                 if (session->proxy)
634                         curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
635
636                 if (debug)
637                         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
638
639                 dbg("user_password = %s\n", user_password);
640                 dbg("data = %s\n", data);
641                 dbg("proxy = %s\n", session->proxy);
642
643                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
644                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
645                 if (!session->dry_run) {
646                         res = curl_easy_perform(curl);
647                         if (!session->background) {
648                                 xmlDocPtr doc;
649                                 xmlNodePtr current;
650
651                                 if (res) {
652                                         fprintf(stderr, "error(%d) trying to "
653                                                 "perform operation\n", res);
654                                         return -EINVAL;
655                                 }
656
657                                 doc = xmlReadMemory(curl_buf->data,
658                                                     curl_buf->length,
659                                                     "response.xml", NULL,
660                                                     XML_PARSE_NOERROR);
661                                 if (doc == NULL)
662                                         return -EINVAL;
663
664                                 current = xmlDocGetRootElement(doc);
665                                 if (current == NULL) {
666                                         fprintf(stderr, "empty document\n");
667                                         xmlFreeDoc(doc);
668                                         return -EINVAL;
669                                 }
670
671                                 if (xmlStrcmp(current->name, (const xmlChar *)"status")) {
672                                         fprintf(stderr, "unexpected document type\n");
673                                         xmlFreeDoc(doc);
674                                         return -EINVAL;
675                                 }
676
677                                 xmlFreeDoc(doc);
678                         }
679                 }
680
681                 curl_easy_cleanup(curl);
682                 if (session->action == ACTION_UPDATE)
683                         curl_formfree(formpost);
684                 bti_curl_buffer_free(curl_buf);
685         } else {
686                 switch (session->action) {
687                 case ACTION_UPDATE:
688                         escaped_tweet = oauth_url_escape(session->tweet);
689                         if (session->replyto) {
690                                 sprintf(endpoint,
691                                         "%s%s?status=%s&in_reply_to_status_id=%s",
692                                         session->hosturl, update_uri,
693                                         escaped_tweet, session->replyto);
694                         } else {
695                                 sprintf(endpoint, "%s%s?status=%s",
696                                         session->hosturl, update_uri,
697                                         escaped_tweet);
698                         }
699
700                         is_post = 1;
701                         break;
702                 case ACTION_USER:
703                         sprintf(endpoint, "%s%s%s.xml?page=%d",
704                                 session->hosturl, user_uri, session->user,
705                                 session->page);
706                         break;
707                 case ACTION_REPLIES:
708                         sprintf(endpoint, "%s%s?page=%d", session->hosturl,
709                                 mentions_uri, session->page);
710                         break;
711                 case ACTION_PUBLIC:
712                         sprintf(endpoint, "%s%s?page=%d", session->hosturl,
713                                 public_uri, session->page);
714                         break;
715                 case ACTION_GROUP:
716                         sprintf(endpoint, "%s%s%s.xml?page=%d",
717                                 session->hosturl, group_uri, session->group,
718                                 session->page);
719                         break;
720                 case ACTION_FRIENDS:
721                         sprintf(endpoint, "%s%s?page=%d", session->hosturl,
722                                 friends_uri, session->page);
723                         break;
724                 case ACTION_RETWEET:
725                         sprintf(endpoint, "%s%s%s.xml", session->hosturl,
726                                 retweet_uri, session->retweet);
727                         is_post = 1;
728                         break;
729                 default:
730                         break;
731                 }
732
733                 dbg("%s\n", endpoint);
734                 if (!session->dry_run) {
735                         if (is_post) {
736                                 req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC,
737                                                           NULL, session->consumer_key,
738                                                           session->consumer_secret,
739                                                           session->access_token_key,
740                                                           session->access_token_secret);
741                                 reply = oauth_http_post(req_url, postarg);
742                         } else {
743                                 req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
744                                                           session->consumer_key,
745                                                           session->consumer_secret,
746                                                           session->access_token_key,
747                                                           session->access_token_secret);
748                                 reply = oauth_http_get(req_url, postarg);
749                         }
750
751                         dbg("%s\n", req_url);
752                         dbg("%s\n", reply);
753                         if (req_url)
754                                 free(req_url);
755                 }
756
757                 if (!reply) {
758                         fprintf(stderr, "Error retrieving from URL (%s)\n", endpoint);
759                         return -EIO;
760                 }
761
762                 if ((session->action != ACTION_UPDATE) &&
763                                 (session->action != ACTION_RETWEET))
764                         parse_timeline(reply, session);
765         }
766         return 0;
767 }
768
769 static void log_session(struct session *session, int retval)
770 {
771         FILE *log_file;
772         char *filename;
773
774         /* Only log something if we have a log file set */
775         if (!session->logfile)
776                 return;
777
778         filename = alloca(strlen(session->homedir) +
779                           strlen(session->logfile) + 3);
780
781         sprintf(filename, "%s/%s", session->homedir, session->logfile);
782
783         log_file = fopen(filename, "a+");
784         if (log_file == NULL)
785                 return;
786
787         switch (session->action) {
788         case ACTION_UPDATE:
789                 if (retval)
790                         fprintf(log_file, "%s: host=%s tweet failed\n",
791                                 session->time, session->hostname);
792                 else
793                         fprintf(log_file, "%s: host=%s tweet=%s\n",
794                                 session->time, session->hostname,
795                                 session->tweet);
796                 break;
797         case ACTION_FRIENDS:
798                 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
799                         session->time, session->hostname);
800                 break;
801         case ACTION_USER:
802                 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
803                         session->time, session->hostname, session->user);
804                 break;
805         case ACTION_REPLIES:
806                 fprintf(log_file, "%s: host=%s retrieving replies\n",
807                         session->time, session->hostname);
808                 break;
809         case ACTION_PUBLIC:
810                 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
811                         session->time, session->hostname);
812                 break;
813         case ACTION_GROUP:
814                 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
815                         session->time, session->hostname);
816                 break;
817         default:
818                 break;
819         }
820
821         fclose(log_file);
822 }
823
824 static char *get_string_from_stdin(void)
825 {
826         char *temp;
827         char *string;
828
829         string = zalloc(1000);
830         if (!string)
831                 return NULL;
832
833         if (!fgets(string, 999, stdin))
834                 return NULL;
835         temp = strchr(string, '\n');
836         if (temp)
837                 *temp = '\0';
838         return string;
839 }
840
841 static void read_password(char *buf, size_t len, char *host)
842 {
843         char pwd[80];
844         int retval;
845         struct termios old;
846         struct termios tp;
847
848         tcgetattr(0, &tp);
849         old = tp;
850
851         tp.c_lflag &= (~ECHO);
852         tcsetattr(0, TCSANOW, &tp);
853
854         fprintf(stdout, "Enter password for %s: ", host);
855         fflush(stdout);
856         tcflow(0, TCOOFF);
857         retval = scanf("%79s", pwd);
858         tcflow(0, TCOON);
859         fprintf(stdout, "\n");
860
861         tcsetattr(0, TCSANOW, &old);
862
863         strncpy(buf, pwd, len);
864         buf[len-1] = '\0';
865 }
866
867 static int find_urls(const char *tweet, int **pranges)
868 {
869         /*
870          * magic obtained from
871          * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
872          */
873         static const char *re_magic =
874                 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
875                 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
876                 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
877         pcre *re;
878         const char *errptr;
879         int erroffset;
880         int ovector[10] = {0,};
881         const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
882         int startoffset, tweetlen;
883         int i, rc;
884         int rbound = 10;
885         int rcount = 0;
886         int *ranges = malloc(sizeof(int) * rbound);
887
888         re = pcre_compile(re_magic,
889                         PCRE_NO_AUTO_CAPTURE,
890                         &errptr, &erroffset, NULL);
891         if (!re) {
892                 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
893                 exit(1);
894         }
895
896         tweetlen = strlen(tweet);
897         for (startoffset = 0; startoffset < tweetlen; ) {
898
899                 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
900                                 ovector, ovsize);
901                 if (rc == PCRE_ERROR_NOMATCH)
902                         break;
903
904                 if (rc < 0) {
905                         fprintf(stderr, "pcre_exec @%u: %s\n",
906                                 erroffset, errptr);
907                         exit(1);
908                 }
909
910                 for (i = 0; i < rc; i += 2) {
911                         if ((rcount+2) == rbound) {
912                                 rbound *= 2;
913                                 ranges = realloc(ranges, sizeof(int) * rbound);
914                         }
915
916                         ranges[rcount++] = ovector[i];
917                         ranges[rcount++] = ovector[i+1];
918                 }
919
920                 startoffset = ovector[1];
921         }
922
923         pcre_free(re);
924
925         *pranges = ranges;
926         return rcount;
927 }
928
929 /**
930  * bidirectional popen() call
931  *
932  * @param rwepipe - int array of size three
933  * @param exe - program to run
934  * @param argv - argument list
935  * @return pid or -1 on error
936  *
937  * The caller passes in an array of three integers (rwepipe), on successful
938  * execution it can then write to element 0 (stdin of exe), and read from
939  * element 1 (stdout) and 2 (stderr).
940  */
941 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
942 {
943         int in[2];
944         int out[2];
945         int err[2];
946         int pid;
947         int rc;
948
949         rc = pipe(in);
950         if (rc < 0)
951                 goto error_in;
952
953         rc = pipe(out);
954         if (rc < 0)
955                 goto error_out;
956
957         rc = pipe(err);
958         if (rc < 0)
959                 goto error_err;
960
961         pid = fork();
962         if (pid > 0) {
963                 /* parent */
964                 close(in[0]);
965                 close(out[1]);
966                 close(err[1]);
967                 rwepipe[0] = in[1];
968                 rwepipe[1] = out[0];
969                 rwepipe[2] = err[0];
970                 return pid;
971         } else if (pid == 0) {
972                 /* child */
973                 close(in[1]);
974                 close(out[0]);
975                 close(err[0]);
976                 close(0);
977                 rc = dup(in[0]);
978                 close(1);
979                 rc = dup(out[1]);
980                 close(2);
981                 rc = dup(err[1]);
982
983                 execvp(exe, (char **)argv);
984                 exit(1);
985         } else
986                 goto error_fork;
987
988         return pid;
989
990 error_fork:
991         close(err[0]);
992         close(err[1]);
993 error_err:
994         close(out[0]);
995         close(out[1]);
996 error_out:
997         close(in[0]);
998         close(in[1]);
999 error_in:
1000         return -1;
1001 }
1002
1003 static int pcloseRWE(int pid, int *rwepipe)
1004 {
1005         int rc, status;
1006         close(rwepipe[0]);
1007         close(rwepipe[1]);
1008         close(rwepipe[2]);
1009         rc = waitpid(pid, &status, 0);
1010         return status;
1011 }
1012
1013 static char *shrink_one_url(int *rwepipe, char *big)
1014 {
1015         int biglen = strlen(big);
1016         char *small;
1017         int smalllen;
1018         int rc;
1019
1020         rc = dprintf(rwepipe[0], "%s\n", big);
1021         if (rc < 0)
1022                 return big;
1023
1024         smalllen = biglen + 128;
1025         small = malloc(smalllen);
1026         if (!small)
1027                 return big;
1028
1029         rc = read(rwepipe[1], small, smalllen);
1030         if (rc < 0 || rc > biglen)
1031                 goto error_free_small;
1032
1033         if (strncmp(small, "http://", 7))
1034                 goto error_free_small;
1035
1036         smalllen = rc;
1037         while (smalllen && isspace(small[smalllen-1]))
1038                         small[--smalllen] = 0;
1039
1040         free(big);
1041         return small;
1042
1043 error_free_small:
1044         free(small);
1045         return big;
1046 }
1047
1048 static char *shrink_urls(char *text)
1049 {
1050         int *ranges;
1051         int rcount;
1052         int i;
1053         int inofs = 0;
1054         int outofs = 0;
1055         const char *const shrink_args[] = {
1056                 "bti-shrink-urls",
1057                 NULL
1058         };
1059         int shrink_pid;
1060         int shrink_pipe[3];
1061         int inlen = strlen(text);
1062
1063         dbg("before len=%u\n", inlen);
1064
1065         shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1066         if (shrink_pid < 0)
1067                 return text;
1068
1069         rcount = find_urls(text, &ranges);
1070         if (!rcount)
1071                 return text;
1072
1073         for (i = 0; i < rcount; i += 2) {
1074                 int url_start = ranges[i];
1075                 int url_end = ranges[i+1];
1076                 int long_url_len = url_end - url_start;
1077                 char *url = strndup(text + url_start, long_url_len);
1078                 int short_url_len;
1079                 int not_url_len = url_start - inofs;
1080
1081                 dbg("long  url[%u]: %s\n", long_url_len, url);
1082                 url = shrink_one_url(shrink_pipe, url);
1083                 short_url_len = url ? strlen(url) : 0;
1084                 dbg("short url[%u]: %s\n", short_url_len, url);
1085
1086                 if (!url || short_url_len >= long_url_len) {
1087                         /* The short url ended up being too long
1088                          * or unavailable */
1089                         if (inofs) {
1090                                 strncpy(text + outofs, text + inofs,
1091                                                 not_url_len + long_url_len);
1092                         }
1093                         inofs += not_url_len + long_url_len;
1094                         outofs += not_url_len + long_url_len;
1095
1096                 } else {
1097                         /* copy the unmodified block */
1098                         strncpy(text + outofs, text + inofs, not_url_len);
1099                         inofs += not_url_len;
1100                         outofs += not_url_len;
1101
1102                         /* copy the new url */
1103                         strncpy(text + outofs, url, short_url_len);
1104                         inofs += long_url_len;
1105                         outofs += short_url_len;
1106                 }
1107
1108                 free(url);
1109         }
1110
1111         /* copy the last block after the last match */
1112         if (inofs) {
1113                 int tail = inlen - inofs;
1114                 if (tail) {
1115                         strncpy(text + outofs, text + inofs, tail);
1116                         outofs += tail;
1117                 }
1118         }
1119
1120         free(ranges);
1121
1122         (void)pcloseRWE(shrink_pid, shrink_pipe);
1123
1124         text[outofs] = 0;
1125         dbg("after len=%u\n", outofs);
1126         return text;
1127 }
1128
1129 int main(int argc, char *argv[], char *envp[])
1130 {
1131         static const struct option options[] = {
1132                 { "debug", 0, NULL, 'd' },
1133                 { "verbose", 0, NULL, 'V' },
1134                 { "account", 1, NULL, 'a' },
1135                 { "password", 1, NULL, 'p' },
1136                 { "host", 1, NULL, 'H' },
1137                 { "proxy", 1, NULL, 'P' },
1138                 { "action", 1, NULL, 'A' },
1139                 { "user", 1, NULL, 'u' },
1140                 { "group", 1, NULL, 'G' },
1141                 { "logfile", 1, NULL, 'L' },
1142                 { "shrink-urls", 0, NULL, 's' },
1143                 { "help", 0, NULL, 'h' },
1144                 { "bash", 0, NULL, 'b' },
1145                 { "background", 0, NULL, 'B' },
1146                 { "dry-run", 0, NULL, 'n' },
1147                 { "page", 1, NULL, 'g' },
1148                 { "version", 0, NULL, 'v' },
1149                 { "config", 1, NULL, 'c' },
1150                 { "replyto", 1, NULL, 'r' },
1151                 { "retweet", 1, NULL, 'w' },
1152                 { }
1153         };
1154         struct session *session;
1155         pid_t child;
1156         char *tweet;
1157         static char password[80];
1158         int retval = 0;
1159         int option;
1160         char *http_proxy;
1161         time_t t;
1162         int page_nr;
1163
1164         debug = 0;
1165
1166         session = session_alloc();
1167         if (!session) {
1168                 fprintf(stderr, "no more memory...\n");
1169                 return -1;
1170         }
1171
1172         /* get the current time so that we can log it later */
1173         time(&t);
1174         session->time = strdup(ctime(&t));
1175         session->time[strlen(session->time)-1] = 0x00;
1176
1177         /* Get the home directory so we can try to find a config file */
1178         session->homedir = strdup(getenv("HOME"));
1179
1180         /* set up a default config file location (traditionally ~/.bti) */
1181         session->configfile = zalloc(strlen(session->homedir) + 7);
1182         sprintf(session->configfile, "%s/.bti", session->homedir);
1183
1184         /* Set environment variables first, before reading command line options
1185          * or config file values. */
1186         http_proxy = getenv("http_proxy");
1187         if (http_proxy) {
1188                 if (session->proxy)
1189                         free(session->proxy);
1190                 session->proxy = strdup(http_proxy);
1191                 dbg("http_proxy = %s\n", session->proxy);
1192         }
1193
1194         bti_parse_configfile(session);
1195
1196         while (1) {
1197                 option = getopt_long_only(argc, argv,
1198                                           "dp:P:H:a:A:u:c:hg:G:sr:nVvw:",
1199                                           options, NULL);
1200                 if (option == -1)
1201                         break;
1202                 switch (option) {
1203                 case 'd':
1204                         debug = 1;
1205                         break;
1206                 case 'V':
1207                         session->verbose = 1;
1208                         break;
1209                 case 'a':
1210                         if (session->account)
1211                                 free(session->account);
1212                         session->account = strdup(optarg);
1213                         dbg("account = %s\n", session->account);
1214                         break;
1215                 case 'g':
1216                         page_nr = atoi(optarg);
1217                         dbg("page = %d\n", page_nr);
1218                         session->page = page_nr;
1219                         break;
1220                 case 'r':
1221                         session->replyto = strdup(optarg);
1222                         dbg("in_reply_to_status_id = %s\n", session->replyto);
1223                         break;
1224                 case 'w':
1225                         session->retweet = strdup(optarg);
1226                         dbg("Retweet ID = %s\n", session->retweet);
1227                         break;
1228                 case 'p':
1229                         if (session->password)
1230                                 free(session->password);
1231                         session->password = strdup(optarg);
1232                         dbg("password = %s\n", session->password);
1233                         break;
1234                 case 'P':
1235                         if (session->proxy)
1236                                 free(session->proxy);
1237                         session->proxy = strdup(optarg);
1238                         dbg("proxy = %s\n", session->proxy);
1239                         break;
1240                 case 'A':
1241                         if (strcasecmp(optarg, "update") == 0)
1242                                 session->action = ACTION_UPDATE;
1243                         else if (strcasecmp(optarg, "friends") == 0)
1244                                 session->action = ACTION_FRIENDS;
1245                         else if (strcasecmp(optarg, "user") == 0)
1246                                 session->action = ACTION_USER;
1247                         else if (strcasecmp(optarg, "replies") == 0)
1248                                 session->action = ACTION_REPLIES;
1249                         else if (strcasecmp(optarg, "public") == 0)
1250                                 session->action = ACTION_PUBLIC;
1251                         else if (strcasecmp(optarg, "group") == 0)
1252                                 session->action = ACTION_GROUP;
1253                         else if (strcasecmp(optarg, "retweet") == 0)
1254                                 session->action = ACTION_RETWEET;
1255                         else
1256                                 session->action = ACTION_UNKNOWN;
1257                         dbg("action = %d\n", session->action);
1258                         break;
1259                 case 'u':
1260                         if (session->user)
1261                                 free(session->user);
1262                         session->user = strdup(optarg);
1263                         dbg("user = %s\n", session->user);
1264                         break;
1265
1266                 case 'G':
1267                         if (session->group)
1268                                 free(session->group);
1269                         session->group = strdup(optarg);
1270                         dbg("group = %s\n", session->group);
1271                         break;
1272                 case 'L':
1273                         if (session->logfile)
1274                                 free(session->logfile);
1275                         session->logfile = strdup(optarg);
1276                         dbg("logfile = %s\n", session->logfile);
1277                         break;
1278                 case 's':
1279                         session->shrink_urls = 1;
1280                         break;
1281                 case 'H':
1282                         if (session->hosturl)
1283                                 free(session->hosturl);
1284                         if (session->hostname)
1285                                 free(session->hostname);
1286                         if (strcasecmp(optarg, "twitter") == 0) {
1287                                 session->host = HOST_TWITTER;
1288                                 session->hosturl = strdup(twitter_host);
1289                                 session->hostname = strdup(twitter_name);
1290                         } else if (strcasecmp(optarg, "identica") == 0) {
1291                                 session->host = HOST_IDENTICA;
1292                                 session->hosturl = strdup(identica_host);
1293                                 session->hostname = strdup(identica_name);
1294                         } else {
1295                                 session->host = HOST_CUSTOM;
1296                                 session->hosturl = strdup(optarg);
1297                                 session->hostname = strdup(optarg);
1298                         }
1299                         dbg("host = %d\n", session->host);
1300                         break;
1301                 case 'b':
1302                         session->bash = 1;
1303                         /* fall-through intended */
1304                 case 'B':
1305                         session->background = 1;
1306                         break;
1307                 case 'c':
1308                         if (session->configfile)
1309                                 free(session->configfile);
1310                         session->configfile = strdup(optarg);
1311                         dbg("configfile = %s\n", session->configfile);
1312
1313                         /*
1314                          * read the config file now.  Yes, this could override
1315                          * previously set options from the command line, but
1316                          * the user asked for it...
1317                          */
1318                         bti_parse_configfile(session);
1319                         break;
1320                 case 'h':
1321                         display_help();
1322                         goto exit;
1323                 case 'n':
1324                         session->dry_run = 1;
1325                         break;
1326                 case 'v':
1327                         display_version();
1328                         goto exit;
1329                 default:
1330                         display_help();
1331                         goto exit;
1332                 }
1333         }
1334
1335         session_readline_init(session);
1336         /*
1337          * Show the version to make it easier to determine what
1338          * is going on here
1339          */
1340         if (debug)
1341                 display_version();
1342
1343         if (session->host == HOST_TWITTER) {
1344                 if (!session->consumer_key || !session->consumer_secret) {
1345                         if (session->action == ACTION_USER ||
1346                                         session->action == ACTION_PUBLIC) {
1347                                 /*
1348                                  * Some actions may still work without
1349                                  * authentication
1350                                  */
1351                                 session->guest = 1;
1352                         } else {
1353                                 fprintf(stderr,
1354                                                 "Twitter no longer supports HTTP basic authentication.\n"
1355                                                 "Both consumer key, and consumer secret are required"
1356                                                 " for bti in order to behave as an OAuth consumer.\n");
1357                                 goto exit;
1358                         }
1359                 }
1360                 if (session->action == ACTION_GROUP) {
1361                         fprintf(stderr, "Groups only work in Identi.ca.\n");
1362                         goto exit;
1363                 }
1364         } else {
1365                 if (!session->consumer_key || !session->consumer_secret)
1366                         session->no_oauth = 1;
1367         }
1368
1369         if (session->no_oauth) {
1370                 if (!session->account) {
1371                         fprintf(stdout, "Enter account for %s: ",
1372                                 session->hostname);
1373                         session->account = session->readline(NULL);
1374                 }
1375                 if (!session->password) {
1376                         read_password(password, sizeof(password),
1377                                       session->hostname);
1378                         session->password = strdup(password);
1379                 }
1380         } else if (!session->guest) {
1381                 if (!session->access_token_key ||
1382                     !session->access_token_secret) {
1383                         request_access_token(session);
1384                         goto exit;
1385                 }
1386         }
1387
1388         if (session->action == ACTION_UNKNOWN) {
1389                 fprintf(stderr, "Unknown action, valid actions are:\n"
1390                         "'update', 'friends', 'public', 'replies', 'group' or 'user'.\n");
1391                 goto exit;
1392         }
1393
1394         if (session->action == ACTION_GROUP && !session->group) {
1395                 fprintf(stdout, "Enter group name: ");
1396                 session->group = session->readline(NULL);
1397         }
1398
1399         if (session->action == ACTION_RETWEET) {
1400                 if (!session->retweet) {
1401                         char *rtid;
1402
1403                         fprintf(stdout, "Status ID to retweet: ");
1404                         rtid = get_string_from_stdin();
1405                         session->retweet = zalloc(strlen(rtid) + 10);
1406                         sprintf(session->retweet, "%s", rtid);
1407                         free(rtid);
1408                 }
1409
1410                 if (!session->retweet || strlen(session->retweet) == 0) {
1411                         dbg("no retweet?\n");
1412                         return -1;
1413                 }
1414
1415                 dbg("retweet ID = %s\n", session->retweet);
1416         }
1417
1418         if (session->action == ACTION_UPDATE) {
1419                 if (session->background || !session->interactive)
1420                         tweet = get_string_from_stdin();
1421                 else
1422                         tweet = session->readline("tweet: ");
1423                 if (!tweet || strlen(tweet) == 0) {
1424                         dbg("no tweet?\n");
1425                         return -1;
1426                 }
1427
1428                 if (session->shrink_urls)
1429                         tweet = shrink_urls(tweet);
1430
1431                 session->tweet = zalloc(strlen(tweet) + 10);
1432                 if (session->bash)
1433                         sprintf(session->tweet, "%c %s",
1434                                 getuid() ? '$' : '#', tweet);
1435                 else
1436                         sprintf(session->tweet, "%s", tweet);
1437
1438                 free(tweet);
1439                 dbg("tweet = %s\n", session->tweet);
1440         }
1441
1442         if (session->page == 0)
1443                 session->page = 1;
1444         dbg("config file = %s\n", session->configfile);
1445         dbg("host = %d\n", session->host);
1446         dbg("action = %d\n", session->action);
1447
1448         /* fork ourself so that the main shell can get on
1449          * with it's life as we try to connect and handle everything
1450          */
1451         if (session->background) {
1452                 child = fork();
1453                 if (child) {
1454                         dbg("child is %d\n", child);
1455                         exit(0);
1456                 }
1457         }
1458
1459         retval = send_request(session);
1460         if (retval && !session->background)
1461                 fprintf(stderr, "operation failed\n");
1462
1463         log_session(session, retval);
1464 exit:
1465         session_readline_cleanup(session);
1466         session_free(session);
1467         return retval;;
1468 }