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>
44 #define zalloc(size) calloc(size, 1)
46 #define dbg(format, arg...) \
49 fprintf(stdout, "bti: %s: " format , __func__ , \
91 void *readline_handle;
92 char *(*readline)(const char *);
95 struct bti_curl_buffer {
101 static void display_help(void)
103 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n");
104 fprintf(stdout, "Version: " VERSION "\n");
105 fprintf(stdout, "Usage:\n");
106 fprintf(stdout, " bti [options]\n");
107 fprintf(stdout, "options are:\n");
108 fprintf(stdout, " --account accountname\n");
109 fprintf(stdout, " --password password\n");
110 fprintf(stdout, " --action action\n");
111 fprintf(stdout, " ('update', 'friends', 'public', 'replies', "
112 "'group' or 'user')\n");
113 fprintf(stdout, " --user screenname\n");
114 fprintf(stdout, " --group groupname\n");
115 fprintf(stdout, " --proxy PROXY:PORT\n");
116 fprintf(stdout, " --host HOST\n");
117 fprintf(stdout, " --logfile logfile\n");
118 fprintf(stdout, " --shrink-urls\n");
119 fprintf(stdout, " --page PAGENUMBER\n");
120 fprintf(stdout, " --bash\n");
121 fprintf(stdout, " --debug\n");
122 fprintf(stdout, " --verbose\n");
123 fprintf(stdout, " --dry-run\n");
124 fprintf(stdout, " --version\n");
125 fprintf(stdout, " --help\n");
128 static void display_version(void)
130 fprintf(stdout, "bti - version %s\n", VERSION);
133 static char *get_string(const char *name)
138 string = zalloc(1000);
142 fprintf(stdout, "%s", name);
143 if (!fgets(string, 999, stdin))
145 temp = strchr(string, '\n');
151 * Try to get a handle to a readline function from a variety of different
152 * libraries. If nothing is present on the system, then fall back to an
155 * Logic originally based off of code in the e2fsutils package in the
156 * lib/ss/get_readline.c file, which is licensed under the MIT license.
158 * This keeps us from having to relicense the bti codebase if readline
159 * ever changes its license, as there is no link-time dependancy.
160 * It is a run-time thing only, and we handle any readline-like library
161 * in the same manner, making bti not be a derivative work of any
164 static void session_readline_init(struct session *session)
166 /* Libraries we will try to use for readline/editline functionality */
167 const char *libpath = "libreadline.so.6:libreadline.so.5:"
168 "libreadline.so.4:libreadline.so:libedit.so.2:"
169 "libedit.so:libeditline.so.0:libeditline.so";
171 char *tmp, *cp, *next;
172 int (*bind_key)(int, void *);
173 void (*insert)(void);
175 /* default to internal function if we can't find anything */
176 session->readline = get_string;
178 tmp = malloc(strlen(libpath)+1);
181 strcpy(tmp, libpath);
182 for (cp = tmp; cp; cp = next) {
183 next = strchr(cp, ':');
188 if ((handle = dlopen(cp, RTLD_NOW))) {
189 dbg("Using %s for readline library\n", cp);
195 dbg("No readline library found.\n");
199 session->readline_handle = handle;
200 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
201 if (session->readline == NULL) {
202 /* something odd happened, default back to internal stuff */
203 session->readline_handle = NULL;
204 session->readline = get_string;
209 * If we found a library, turn off filename expansion
210 * as that makes no sense from within bti.
212 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
213 insert = (void (*)(void))dlsym(handle, "rl_insert");
214 if (bind_key && insert)
215 bind_key('\t', insert);
218 static void session_readline_cleanup(struct session *session)
220 if (session->readline_handle)
221 dlclose(session->readline_handle);
224 static struct session *session_alloc(void)
226 struct session *session;
228 session = zalloc(sizeof(*session));
231 session_readline_init(session);
235 static void session_free(struct session *session)
239 session_readline_cleanup(session);
240 free(session->password);
241 free(session->account);
242 free(session->tweet);
243 free(session->proxy);
245 free(session->homedir);
247 free(session->group);
248 free(session->hosturl);
249 free(session->hostname);
253 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
255 struct bti_curl_buffer *buffer;
257 buffer = zalloc(sizeof(*buffer));
261 /* start out with a data buffer of 1 byte to
262 * make the buffer fill logic simpler */
263 buffer->data = zalloc(1);
269 buffer->action = action;
273 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
281 static const char *twitter_host = "https://twitter.com/statuses";
282 static const char *identica_host = "https://identi.ca/api/statuses";
283 static const char *twitter_name = "twitter";
284 static const char *identica_name = "identi.ca";
286 static const char *user_uri = "/user_timeline/";
287 static const char *update_uri = "/update.xml";
288 static const char *public_uri = "/public_timeline.xml";
289 static const char *friends_uri = "/friends_timeline.xml";
290 static const char *replies_uri = "/replies.xml";
291 static const char *group_uri = "/../laconica/groups/timeline/";
293 static CURL *curl_init(void)
297 curl = curl_easy_init();
299 fprintf(stderr, "Can not init CURL!\n");
302 /* some ssl sanity checks on the connection we are making */
303 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
304 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
308 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
310 xmlChar *text = NULL;
311 xmlChar *user = NULL;
312 xmlChar *created = NULL;
315 current = current->xmlChildrenNode;
316 while (current != NULL) {
317 if (current->type == XML_ELEMENT_NODE) {
318 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
319 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
320 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
321 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
322 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
323 userinfo = current->xmlChildrenNode;
324 while (userinfo != NULL) {
325 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
328 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
330 userinfo = userinfo->next;
334 if (user && text && created) {
336 printf("[%s] (%.16s) %s\n",
337 user, created, text);
349 current = current->next;
355 static void parse_timeline(char *document)
360 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
361 NULL, XML_PARSE_NOERROR);
365 current = xmlDocGetRootElement(doc);
366 if (current == NULL) {
367 fprintf(stderr, "empty document\n");
372 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
373 fprintf(stderr, "unexpected document type\n");
378 current = current->xmlChildrenNode;
379 while (current != NULL) {
380 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
381 parse_statuses(doc, current);
382 current = current->next;
389 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
392 struct bti_curl_buffer *curl_buf = userp;
393 size_t buffer_size = size * nmemb;
396 if ((!buffer) || (!buffer_size) || (!curl_buf))
399 /* add to the data we already have */
400 temp = zalloc(curl_buf->length + buffer_size + 1);
404 memcpy(temp, curl_buf->data, curl_buf->length);
405 free(curl_buf->data);
406 curl_buf->data = temp;
407 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
408 curl_buf->length += buffer_size;
409 if (curl_buf->action)
410 parse_timeline(curl_buf->data);
412 dbg("%s\n", curl_buf->data);
417 static int send_request(struct session *session)
420 char user_password[500];
422 struct bti_curl_buffer *curl_buf;
425 struct curl_httppost *formpost = NULL;
426 struct curl_httppost *lastptr = NULL;
427 struct curl_slist *slist = NULL;
432 curl_buf = bti_curl_buffer_alloc(session->action);
440 if (!session->hosturl)
441 session->hosturl = strdup(twitter_host);
443 switch (session->action) {
445 snprintf(user_password, sizeof(user_password), "%s:%s",
446 session->account, session->password);
447 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
448 curl_formadd(&formpost, &lastptr,
449 CURLFORM_COPYNAME, "status",
450 CURLFORM_COPYCONTENTS, session->tweet,
453 curl_formadd(&formpost, &lastptr,
454 CURLFORM_COPYNAME, "source",
455 CURLFORM_COPYCONTENTS, "bti",
458 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
459 slist = curl_slist_append(slist, "Expect:");
460 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
462 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
463 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
464 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
468 snprintf(user_password, sizeof(user_password), "%s:%s",
469 session->account, session->password);
470 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
471 friends_uri, session->page);
472 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
473 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
477 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
478 user_uri, session->user, session->page);
479 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
483 snprintf(user_password, sizeof(user_password), "%s:%s",
484 session->account, session->password);
485 sprintf(endpoint, "%s%s?page=%d", session->hosturl, replies_uri,
487 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
488 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
492 sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri,
494 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
498 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
499 group_uri, session->group, session->page);
500 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
508 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
511 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
513 dbg("user_password = %s\n", user_password);
514 dbg("data = %s\n", data);
515 dbg("proxy = %s\n", session->proxy);
517 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
518 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
519 if (!session->dry_run) {
520 res = curl_easy_perform(curl);
521 if (res && !session->bash) {
522 fprintf(stderr, "error(%d) trying to perform "
528 curl_easy_cleanup(curl);
529 if (session->action == ACTION_UPDATE)
530 curl_formfree(formpost);
531 bti_curl_buffer_free(curl_buf);
535 static void parse_configfile(struct session *session)
540 char *account = NULL;
541 char *password = NULL;
544 char *logfile = NULL;
550 /* config file is ~/.bti */
551 file = alloca(strlen(session->homedir) + 7);
553 sprintf(file, "%s/.bti", session->homedir);
555 config_file = fopen(file, "r");
557 /* No error if file does not exist or is unreadable. */
558 if (config_file == NULL)
562 ssize_t n = getline(&line, &len, config_file);
565 if (line[n - 1] == '\n')
567 /* Parse file. Format is the usual value pairs:
570 # is a comment character
572 *strchrnul(line, '#') = '\0';
576 /* Ignore blank lines. */
580 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
584 } else if (!strncasecmp(c, "password", 8) &&
588 password = strdup(c);
589 } else if (!strncasecmp(c, "host", 4) &&
594 } else if (!strncasecmp(c, "proxy", 5) &&
599 } else if (!strncasecmp(c, "logfile", 7) &&
604 } else if (!strncasecmp(c, "action", 6) &&
609 } else if (!strncasecmp(c, "user", 4) &&
614 } else if (!strncasecmp(c, "shrink-urls", 11) &&
617 if (!strncasecmp(c, "true", 4) ||
618 !strncasecmp(c, "yes", 3))
620 } else if (!strncasecmp(c, "verbose", 7) &&
623 if (!strncasecmp(c, "true", 4) ||
624 !strncasecmp(c, "yes", 3))
627 } while (!feof(config_file));
630 session->password = password;
632 session->account = account;
634 if (strcasecmp(host, "twitter") == 0) {
635 session->host = HOST_TWITTER;
636 session->hosturl = strdup(twitter_host);
637 session->hostname = strdup(twitter_name);
638 } else if (strcasecmp(host, "identica") == 0) {
639 session->host = HOST_IDENTICA;
640 session->hosturl = strdup(identica_host);
641 session->hostname = strdup(identica_name);
643 session->host = HOST_CUSTOM;
644 session->hosturl = strdup(host);
645 session->hostname = strdup(host);
651 free(session->proxy);
652 session->proxy = proxy;
655 session->logfile = logfile;
657 if (strcasecmp(action, "update") == 0)
658 session->action = ACTION_UPDATE;
659 else if (strcasecmp(action, "friends") == 0)
660 session->action = ACTION_FRIENDS;
661 else if (strcasecmp(action, "user") == 0)
662 session->action = ACTION_USER;
663 else if (strcasecmp(action, "replies") == 0)
664 session->action = ACTION_REPLIES;
665 else if (strcasecmp(action, "public") == 0)
666 session->action = ACTION_PUBLIC;
667 else if (strcasecmp(action, "group") == 0)
668 session->action = ACTION_GROUP;
670 session->action = ACTION_UNKNOWN;
674 session->user = user;
675 session->shrink_urls = shrink_urls;
677 /* Free buffer and close file. */
682 static void log_session(struct session *session, int retval)
687 /* Only log something if we have a log file set */
688 if (!session->logfile)
691 filename = alloca(strlen(session->homedir) +
692 strlen(session->logfile) + 3);
694 sprintf(filename, "%s/%s", session->homedir, session->logfile);
696 log_file = fopen(filename, "a+");
697 if (log_file == NULL)
700 switch (session->action) {
703 fprintf(log_file, "%s: host=%s tweet failed\n",
704 session->time, session->hostname);
706 fprintf(log_file, "%s: host=%s tweet=%s\n",
707 session->time, session->hostname, session->tweet);
710 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
711 session->time, session->hostname);
714 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
715 session->time, session->hostname, session->user);
718 fprintf(log_file, "%s: host=%s retrieving replies\n",
719 session->time, session->hostname);
722 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
723 session->time, session->hostname);
726 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
727 session->time, session->hostname);
736 static char *get_string_from_stdin(void)
741 string = zalloc(1000);
745 if (!fgets(string, 999, stdin))
747 temp = strchr(string, '\n');
752 static void read_password(char *buf, size_t len, char *host)
762 tp.c_lflag &= (~ECHO);
763 tcsetattr(0, TCSANOW, &tp);
765 fprintf(stdout, "Enter password for %s: ", host);
768 retval = scanf("%79s", pwd);
770 fprintf(stdout, "\n");
772 tcsetattr(0, TCSANOW, &old);
774 strncpy(buf, pwd, len);
778 static int find_urls(const char *tweet, int **pranges)
781 * magic obtained from
782 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
784 static const char *re_magic =
785 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
786 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
787 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
791 int ovector[10] = {0,};
792 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
793 int startoffset, tweetlen;
797 int *ranges = malloc(sizeof(int) * rbound);
799 re = pcre_compile(re_magic,
800 PCRE_NO_AUTO_CAPTURE,
801 &errptr, &erroffset, NULL);
803 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
807 tweetlen = strlen(tweet);
808 for (startoffset = 0; startoffset < tweetlen; ) {
810 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
812 if (rc == PCRE_ERROR_NOMATCH)
816 fprintf(stderr, "pcre_exec @%u: %s\n",
821 for (i = 0; i < rc; i += 2) {
822 if ((rcount+2) == rbound) {
824 ranges = realloc(ranges, sizeof(int) * rbound);
827 ranges[rcount++] = ovector[i];
828 ranges[rcount++] = ovector[i+1];
831 startoffset = ovector[1];
841 * bidirectional popen() call
843 * @param rwepipe - int array of size three
844 * @param exe - program to run
845 * @param argv - argument list
846 * @return pid or -1 on error
848 * The caller passes in an array of three integers (rwepipe), on successful
849 * execution it can then write to element 0 (stdin of exe), and read from
850 * element 1 (stdout) and 2 (stderr).
852 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
882 } else if (pid == 0) {
894 execvp(exe, (char **)argv);
914 static int pcloseRWE(int pid, int *rwepipe)
920 rc = waitpid(pid, &status, 0);
924 static char *shrink_one_url(int *rwepipe, char *big)
926 int biglen = strlen(big);
931 rc = dprintf(rwepipe[0], "%s\n", big);
935 smalllen = biglen + 128;
936 small = malloc(smalllen);
940 rc = read(rwepipe[1], small, smalllen);
941 if (rc < 0 || rc > biglen)
942 goto error_free_small;
944 if (strncmp(small, "http://", 7))
945 goto error_free_small;
948 while (smalllen && isspace(small[smalllen-1]))
949 small[--smalllen] = 0;
959 static char *shrink_urls(char *text)
966 const char *const shrink_args[] = {
972 int inlen = strlen(text);
974 dbg("before len=%u\n", inlen);
976 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
980 rcount = find_urls(text, &ranges);
984 for (i = 0; i < rcount; i += 2) {
985 int url_start = ranges[i];
986 int url_end = ranges[i+1];
987 int long_url_len = url_end - url_start;
988 char *url = strndup(text + url_start, long_url_len);
990 int not_url_len = url_start - inofs;
992 dbg("long url[%u]: %s\n", long_url_len, url);
993 url = shrink_one_url(shrink_pipe, url);
994 short_url_len = url ? strlen(url) : 0;
995 dbg("short url[%u]: %s\n", short_url_len, url);
997 if (!url || short_url_len >= long_url_len) {
998 /* The short url ended up being too long
1001 strncpy(text + outofs, text + inofs,
1002 not_url_len + long_url_len);
1004 inofs += not_url_len + long_url_len;
1005 outofs += not_url_len + long_url_len;
1008 /* copy the unmodified block */
1009 strncpy(text + outofs, text + inofs, not_url_len);
1010 inofs += not_url_len;
1011 outofs += not_url_len;
1013 /* copy the new url */
1014 strncpy(text + outofs, url, short_url_len);
1015 inofs += long_url_len;
1016 outofs += short_url_len;
1022 /* copy the last block after the last match */
1024 int tail = inlen - inofs;
1026 strncpy(text + outofs, text + inofs, tail);
1033 (void)pcloseRWE(shrink_pid, shrink_pipe);
1036 dbg("after len=%u\n", outofs);
1040 int main(int argc, char *argv[], char *envp[])
1042 static const struct option options[] = {
1043 { "debug", 0, NULL, 'd' },
1044 { "verbose", 0, NULL, 'V' },
1045 { "account", 1, NULL, 'a' },
1046 { "password", 1, NULL, 'p' },
1047 { "host", 1, NULL, 'H' },
1048 { "proxy", 1, NULL, 'P' },
1049 { "action", 1, NULL, 'A' },
1050 { "user", 1, NULL, 'u' },
1051 { "group", 1, NULL, 'G' },
1052 { "logfile", 1, NULL, 'L' },
1053 { "shrink-urls", 0, NULL, 's' },
1054 { "help", 0, NULL, 'h' },
1055 { "bash", 0, NULL, 'b' },
1056 { "dry-run", 0, NULL, 'n' },
1057 { "page", 1, NULL, 'g' },
1058 { "version", 0, NULL, 'v' },
1061 struct session *session;
1064 static char password[80];
1074 session = session_alloc();
1076 fprintf(stderr, "no more memory...\n");
1080 /* get the current time so that we can log it later */
1082 session->time = strdup(ctime(&t));
1083 session->time[strlen(session->time)-1] = 0x00;
1085 session->homedir = strdup(getenv("HOME"));
1087 curl_global_init(CURL_GLOBAL_ALL);
1089 /* Set environment variables first, before reading command line options
1090 * or config file values. */
1091 http_proxy = getenv("http_proxy");
1094 free(session->proxy);
1095 session->proxy = strdup(http_proxy);
1096 dbg("http_proxy = %s\n", session->proxy);
1099 parse_configfile(session);
1102 option = getopt_long_only(argc, argv, "dp:P:H:a:A:u:hg:G:snVv",
1114 if (session->account)
1115 free(session->account);
1116 session->account = strdup(optarg);
1117 dbg("account = %s\n", session->account);
1120 page_nr = atoi(optarg);
1121 dbg("page = %d\n", page_nr);
1122 session->page = page_nr;
1125 if (session->password)
1126 free(session->password);
1127 session->password = strdup(optarg);
1128 dbg("password = %s\n", session->password);
1132 free(session->proxy);
1133 session->proxy = strdup(optarg);
1134 dbg("proxy = %s\n", session->proxy);
1137 if (strcasecmp(optarg, "update") == 0)
1138 session->action = ACTION_UPDATE;
1139 else if (strcasecmp(optarg, "friends") == 0)
1140 session->action = ACTION_FRIENDS;
1141 else if (strcasecmp(optarg, "user") == 0)
1142 session->action = ACTION_USER;
1143 else if (strcasecmp(optarg, "replies") == 0)
1144 session->action = ACTION_REPLIES;
1145 else if (strcasecmp(optarg, "public") == 0)
1146 session->action = ACTION_PUBLIC;
1147 else if (strcasecmp(optarg, "group") == 0)
1148 session->action = ACTION_GROUP;
1150 session->action = ACTION_UNKNOWN;
1151 dbg("action = %d\n", session->action);
1155 free(session->user);
1156 session->user = strdup(optarg);
1157 dbg("user = %s\n", session->user);
1162 free(session->group);
1163 session->group = strdup(optarg);
1164 dbg("group = %s\n", session->group);
1167 if (session->logfile)
1168 free(session->logfile);
1169 session->logfile = strdup(optarg);
1170 dbg("logfile = %s\n", session->logfile);
1173 session->shrink_urls = 1;
1176 if (session->hosturl)
1177 free(session->hosturl);
1178 if (session->hostname)
1179 free(session->hostname);
1180 if (strcasecmp(optarg, "twitter") == 0) {
1181 session->host = HOST_TWITTER;
1182 session->hosturl = strdup(twitter_host);
1183 session->hostname = strdup(twitter_name);
1184 } else if (strcasecmp(optarg, "identica") == 0) {
1185 session->host = HOST_IDENTICA;
1186 session->hosturl = strdup(identica_host);
1187 session->hostname = strdup(identica_name);
1189 session->host = HOST_CUSTOM;
1190 session->hosturl = strdup(optarg);
1191 session->hostname = strdup(optarg);
1193 dbg("host = %d\n", session->host);
1202 session->dry_run = 1;
1214 * Show the version to make it easier to determine what
1220 if (session->action == ACTION_UNKNOWN) {
1221 fprintf(stderr, "Unknown action, valid actions are:\n");
1222 fprintf(stderr, "'update', 'friends', 'public', "
1223 "'replies', 'group' or 'user'.\n");
1227 if (session->host == HOST_TWITTER && session->action == ACTION_GROUP) {
1228 fprintf(stderr, "Groups only work in Identi.ca.\n");
1232 if (session->action == ACTION_GROUP && !session->group) {
1233 fprintf(stdout, "Enter group name: ");
1234 session->group = session->readline(NULL);
1237 if (!session->account) {
1238 fprintf(stdout, "Enter account for %s: ", session->hostname);
1239 session->account = session->readline(NULL);
1242 if (!session->password) {
1243 read_password(password, sizeof(password), session->hostname);
1244 session->password = strdup(password);
1247 if (session->action == ACTION_UPDATE) {
1249 tweet = get_string_from_stdin();
1251 tweet = session->readline("tweet: ");
1252 if (!tweet || strlen(tweet) == 0) {
1257 if (session->shrink_urls)
1258 tweet = shrink_urls(tweet);
1260 session->tweet = zalloc(strlen(tweet) + 10);
1262 sprintf(session->tweet, "%c %s",
1263 getuid() ? '$' : '#', tweet);
1265 sprintf(session->tweet, "%s", tweet);
1268 dbg("tweet = %s\n", session->tweet);
1272 session->user = strdup(session->account);
1274 if (session->page == 0)
1276 dbg("account = %s\n", session->account);
1277 dbg("password = %s\n", session->password);
1278 dbg("host = %d\n", session->host);
1279 dbg("action = %d\n", session->action);
1281 /* fork ourself so that the main shell can get on
1282 * with it's life as we try to connect and handle everything
1284 if (session->bash) {
1287 dbg("child is %d\n", child);
1292 retval = send_request(session);
1293 if (retval && !session->bash)
1294 fprintf(stderr, "operation failed\n");
1296 log_session(session, retval);
1298 session_free(session);