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__ , \
92 void *readline_handle;
93 char *(*readline)(const char *);
96 struct bti_curl_buffer {
102 static void display_help(void)
104 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n");
105 fprintf(stdout, "Version: " VERSION "\n");
106 fprintf(stdout, "Usage:\n");
107 fprintf(stdout, " bti [options]\n");
108 fprintf(stdout, "options are:\n");
109 fprintf(stdout, " --account accountname\n");
110 fprintf(stdout, " --password password\n");
111 fprintf(stdout, " --action action\n");
112 fprintf(stdout, " ('update', 'friends', 'public', 'replies', "
113 "'group' or 'user')\n");
114 fprintf(stdout, " --user screenname\n");
115 fprintf(stdout, " --group groupname\n");
116 fprintf(stdout, " --proxy PROXY:PORT\n");
117 fprintf(stdout, " --host HOST\n");
118 fprintf(stdout, " --logfile logfile\n");
119 fprintf(stdout, " --shrink-urls\n");
120 fprintf(stdout, " --page PAGENUMBER\n");
121 fprintf(stdout, " --bash\n");
122 fprintf(stdout, " --debug\n");
123 fprintf(stdout, " --verbose\n");
124 fprintf(stdout, " --dry-run\n");
125 fprintf(stdout, " --version\n");
126 fprintf(stdout, " --help\n");
129 static void display_version(void)
131 fprintf(stdout, "bti - version %s\n", VERSION);
134 static char *get_string(const char *name)
139 string = zalloc(1000);
143 fprintf(stdout, "%s", name);
144 if (!fgets(string, 999, stdin))
146 temp = strchr(string, '\n');
153 * Try to get a handle to a readline function from a variety of different
154 * libraries. If nothing is present on the system, then fall back to an
157 * Logic originally based off of code in the e2fsutils package in the
158 * lib/ss/get_readline.c file, which is licensed under the MIT license.
160 * This keeps us from having to relicense the bti codebase if readline
161 * ever changes its license, as there is no link-time dependancy.
162 * It is a run-time thing only, and we handle any readline-like library
163 * in the same manner, making bti not be a derivative work of any
166 static void session_readline_init(struct session *session)
168 /* Libraries we will try to use for readline/editline functionality */
169 const char *libpath = "libreadline.so.6:libreadline.so.5:"
170 "libreadline.so.4:libreadline.so:libedit.so.2:"
171 "libedit.so:libeditline.so.0:libeditline.so";
173 char *tmp, *cp, *next;
174 int (*bind_key)(int, void *);
175 void (*insert)(void);
177 /* default to internal function if we can't or won't find anything */
178 session->readline = get_string;
181 session->interactive = 1;
183 tmp = malloc(strlen(libpath)+1);
186 strcpy(tmp, libpath);
187 for (cp = tmp; cp; cp = next) {
188 next = strchr(cp, ':');
193 handle = dlopen(cp, RTLD_NOW);
195 dbg("Using %s for readline library\n", cp);
201 dbg("No readline library found.\n");
205 session->readline_handle = handle;
206 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
207 if (session->readline == NULL) {
208 /* something odd happened, default back to internal stuff */
209 session->readline_handle = NULL;
210 session->readline = get_string;
215 * If we found a library, turn off filename expansion
216 * as that makes no sense from within bti.
218 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
219 insert = (void (*)(void))dlsym(handle, "rl_insert");
220 if (bind_key && insert)
221 bind_key('\t', insert);
224 static void session_readline_cleanup(struct session *session)
226 if (session->readline_handle)
227 dlclose(session->readline_handle);
230 static struct session *session_alloc(void)
232 struct session *session;
234 session = zalloc(sizeof(*session));
240 static void session_free(struct session *session)
244 free(session->password);
245 free(session->account);
246 free(session->tweet);
247 free(session->proxy);
249 free(session->homedir);
251 free(session->group);
252 free(session->hosturl);
253 free(session->hostname);
257 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
259 struct bti_curl_buffer *buffer;
261 buffer = zalloc(sizeof(*buffer));
265 /* start out with a data buffer of 1 byte to
266 * make the buffer fill logic simpler */
267 buffer->data = zalloc(1);
273 buffer->action = action;
277 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
285 static const char *twitter_host = "https://twitter.com/statuses";
286 static const char *identica_host = "https://identi.ca/api/statuses";
287 static const char *twitter_name = "twitter";
288 static const char *identica_name = "identi.ca";
290 static const char *user_uri = "/user_timeline/";
291 static const char *update_uri = "/update.xml";
292 static const char *public_uri = "/public_timeline.xml";
293 static const char *friends_uri = "/friends_timeline.xml";
294 static const char *replies_uri = "/replies.xml";
295 static const char *group_uri = "/../laconica/groups/timeline/";
297 static CURL *curl_init(void)
301 curl = curl_easy_init();
303 fprintf(stderr, "Can not init CURL!\n");
306 /* some ssl sanity checks on the connection we are making */
307 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
308 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
312 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
314 xmlChar *text = NULL;
315 xmlChar *user = NULL;
316 xmlChar *created = NULL;
319 current = current->xmlChildrenNode;
320 while (current != NULL) {
321 if (current->type == XML_ELEMENT_NODE) {
322 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
323 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
324 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
325 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
326 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
327 userinfo = current->xmlChildrenNode;
328 while (userinfo != NULL) {
329 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
332 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
334 userinfo = userinfo->next;
338 if (user && text && created) {
340 printf("[%s] (%.16s) %s\n",
341 user, created, text);
353 current = current->next;
359 static void parse_timeline(char *document)
364 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
365 NULL, XML_PARSE_NOERROR);
369 current = xmlDocGetRootElement(doc);
370 if (current == NULL) {
371 fprintf(stderr, "empty document\n");
376 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
377 fprintf(stderr, "unexpected document type\n");
382 current = current->xmlChildrenNode;
383 while (current != NULL) {
384 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
385 parse_statuses(doc, current);
386 current = current->next;
393 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
396 struct bti_curl_buffer *curl_buf = userp;
397 size_t buffer_size = size * nmemb;
400 if ((!buffer) || (!buffer_size) || (!curl_buf))
403 /* add to the data we already have */
404 temp = zalloc(curl_buf->length + buffer_size + 1);
408 memcpy(temp, curl_buf->data, curl_buf->length);
409 free(curl_buf->data);
410 curl_buf->data = temp;
411 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
412 curl_buf->length += buffer_size;
413 if (curl_buf->action)
414 parse_timeline(curl_buf->data);
416 dbg("%s\n", curl_buf->data);
421 static int send_request(struct session *session)
424 char user_password[500];
426 struct bti_curl_buffer *curl_buf;
429 struct curl_httppost *formpost = NULL;
430 struct curl_httppost *lastptr = NULL;
431 struct curl_slist *slist = NULL;
436 curl_buf = bti_curl_buffer_alloc(session->action);
444 if (!session->hosturl)
445 session->hosturl = strdup(twitter_host);
447 switch (session->action) {
449 snprintf(user_password, sizeof(user_password), "%s:%s",
450 session->account, session->password);
451 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
452 curl_formadd(&formpost, &lastptr,
453 CURLFORM_COPYNAME, "status",
454 CURLFORM_COPYCONTENTS, session->tweet,
457 curl_formadd(&formpost, &lastptr,
458 CURLFORM_COPYNAME, "source",
459 CURLFORM_COPYCONTENTS, "bti",
462 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
463 slist = curl_slist_append(slist, "Expect:");
464 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
466 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
467 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
468 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
472 snprintf(user_password, sizeof(user_password), "%s:%s",
473 session->account, session->password);
474 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
475 friends_uri, session->page);
476 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
477 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
481 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
482 user_uri, session->user, session->page);
483 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
487 snprintf(user_password, sizeof(user_password), "%s:%s",
488 session->account, session->password);
489 sprintf(endpoint, "%s%s?page=%d", session->hosturl, replies_uri,
491 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
492 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
496 sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri,
498 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
502 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
503 group_uri, session->group, session->page);
504 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
512 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
515 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
517 dbg("user_password = %s\n", user_password);
518 dbg("data = %s\n", data);
519 dbg("proxy = %s\n", session->proxy);
521 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
522 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
523 if (!session->dry_run) {
524 res = curl_easy_perform(curl);
525 if (res && !session->bash) {
526 fprintf(stderr, "error(%d) trying to perform "
532 curl_easy_cleanup(curl);
533 if (session->action == ACTION_UPDATE)
534 curl_formfree(formpost);
535 bti_curl_buffer_free(curl_buf);
539 static void parse_configfile(struct session *session)
544 char *account = NULL;
545 char *password = NULL;
548 char *logfile = NULL;
554 /* config file is ~/.bti */
555 file = alloca(strlen(session->homedir) + 7);
557 sprintf(file, "%s/.bti", session->homedir);
559 config_file = fopen(file, "r");
561 /* No error if file does not exist or is unreadable. */
562 if (config_file == NULL)
566 ssize_t n = getline(&line, &len, config_file);
569 if (line[n - 1] == '\n')
571 /* Parse file. Format is the usual value pairs:
574 # is a comment character
576 *strchrnul(line, '#') = '\0';
580 /* Ignore blank lines. */
584 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
588 } else if (!strncasecmp(c, "password", 8) &&
592 password = strdup(c);
593 } else if (!strncasecmp(c, "host", 4) &&
598 } else if (!strncasecmp(c, "proxy", 5) &&
603 } else if (!strncasecmp(c, "logfile", 7) &&
608 } else if (!strncasecmp(c, "action", 6) &&
613 } else if (!strncasecmp(c, "user", 4) &&
618 } else if (!strncasecmp(c, "shrink-urls", 11) &&
621 if (!strncasecmp(c, "true", 4) ||
622 !strncasecmp(c, "yes", 3))
624 } else if (!strncasecmp(c, "verbose", 7) &&
627 if (!strncasecmp(c, "true", 4) ||
628 !strncasecmp(c, "yes", 3))
631 } while (!feof(config_file));
634 session->password = password;
636 session->account = account;
638 if (strcasecmp(host, "twitter") == 0) {
639 session->host = HOST_TWITTER;
640 session->hosturl = strdup(twitter_host);
641 session->hostname = strdup(twitter_name);
642 } else if (strcasecmp(host, "identica") == 0) {
643 session->host = HOST_IDENTICA;
644 session->hosturl = strdup(identica_host);
645 session->hostname = strdup(identica_name);
647 session->host = HOST_CUSTOM;
648 session->hosturl = strdup(host);
649 session->hostname = strdup(host);
655 free(session->proxy);
656 session->proxy = proxy;
659 session->logfile = logfile;
661 if (strcasecmp(action, "update") == 0)
662 session->action = ACTION_UPDATE;
663 else if (strcasecmp(action, "friends") == 0)
664 session->action = ACTION_FRIENDS;
665 else if (strcasecmp(action, "user") == 0)
666 session->action = ACTION_USER;
667 else if (strcasecmp(action, "replies") == 0)
668 session->action = ACTION_REPLIES;
669 else if (strcasecmp(action, "public") == 0)
670 session->action = ACTION_PUBLIC;
671 else if (strcasecmp(action, "group") == 0)
672 session->action = ACTION_GROUP;
674 session->action = ACTION_UNKNOWN;
678 session->user = user;
679 session->shrink_urls = shrink_urls;
681 /* Free buffer and close file. */
686 static void log_session(struct session *session, int retval)
691 /* Only log something if we have a log file set */
692 if (!session->logfile)
695 filename = alloca(strlen(session->homedir) +
696 strlen(session->logfile) + 3);
698 sprintf(filename, "%s/%s", session->homedir, session->logfile);
700 log_file = fopen(filename, "a+");
701 if (log_file == NULL)
704 switch (session->action) {
707 fprintf(log_file, "%s: host=%s tweet failed\n",
708 session->time, session->hostname);
710 fprintf(log_file, "%s: host=%s tweet=%s\n",
711 session->time, session->hostname,
715 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
716 session->time, session->hostname);
719 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
720 session->time, session->hostname, session->user);
723 fprintf(log_file, "%s: host=%s retrieving replies\n",
724 session->time, session->hostname);
727 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
728 session->time, session->hostname);
731 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
732 session->time, session->hostname);
741 static char *get_string_from_stdin(void)
746 string = zalloc(1000);
750 if (!fgets(string, 999, stdin))
752 temp = strchr(string, '\n');
758 static void read_password(char *buf, size_t len, char *host)
768 tp.c_lflag &= (~ECHO);
769 tcsetattr(0, TCSANOW, &tp);
771 fprintf(stdout, "Enter password for %s: ", host);
774 retval = scanf("%79s", pwd);
776 fprintf(stdout, "\n");
778 tcsetattr(0, TCSANOW, &old);
780 strncpy(buf, pwd, len);
784 static int find_urls(const char *tweet, int **pranges)
787 * magic obtained from
788 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
790 static const char *re_magic =
791 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
792 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
793 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
797 int ovector[10] = {0,};
798 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
799 int startoffset, tweetlen;
803 int *ranges = malloc(sizeof(int) * rbound);
805 re = pcre_compile(re_magic,
806 PCRE_NO_AUTO_CAPTURE,
807 &errptr, &erroffset, NULL);
809 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
813 tweetlen = strlen(tweet);
814 for (startoffset = 0; startoffset < tweetlen; ) {
816 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
818 if (rc == PCRE_ERROR_NOMATCH)
822 fprintf(stderr, "pcre_exec @%u: %s\n",
827 for (i = 0; i < rc; i += 2) {
828 if ((rcount+2) == rbound) {
830 ranges = realloc(ranges, sizeof(int) * rbound);
833 ranges[rcount++] = ovector[i];
834 ranges[rcount++] = ovector[i+1];
837 startoffset = ovector[1];
847 * bidirectional popen() call
849 * @param rwepipe - int array of size three
850 * @param exe - program to run
851 * @param argv - argument list
852 * @return pid or -1 on error
854 * The caller passes in an array of three integers (rwepipe), on successful
855 * execution it can then write to element 0 (stdin of exe), and read from
856 * element 1 (stdout) and 2 (stderr).
858 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
888 } else if (pid == 0) {
900 execvp(exe, (char **)argv);
920 static int pcloseRWE(int pid, int *rwepipe)
926 rc = waitpid(pid, &status, 0);
930 static char *shrink_one_url(int *rwepipe, char *big)
932 int biglen = strlen(big);
937 rc = dprintf(rwepipe[0], "%s\n", big);
941 smalllen = biglen + 128;
942 small = malloc(smalllen);
946 rc = read(rwepipe[1], small, smalllen);
947 if (rc < 0 || rc > biglen)
948 goto error_free_small;
950 if (strncmp(small, "http://", 7))
951 goto error_free_small;
954 while (smalllen && isspace(small[smalllen-1]))
955 small[--smalllen] = 0;
965 static char *shrink_urls(char *text)
972 const char *const shrink_args[] = {
978 int inlen = strlen(text);
980 dbg("before len=%u\n", inlen);
982 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
986 rcount = find_urls(text, &ranges);
990 for (i = 0; i < rcount; i += 2) {
991 int url_start = ranges[i];
992 int url_end = ranges[i+1];
993 int long_url_len = url_end - url_start;
994 char *url = strndup(text + url_start, long_url_len);
996 int not_url_len = url_start - inofs;
998 dbg("long url[%u]: %s\n", long_url_len, url);
999 url = shrink_one_url(shrink_pipe, url);
1000 short_url_len = url ? strlen(url) : 0;
1001 dbg("short url[%u]: %s\n", short_url_len, url);
1003 if (!url || short_url_len >= long_url_len) {
1004 /* The short url ended up being too long
1007 strncpy(text + outofs, text + inofs,
1008 not_url_len + long_url_len);
1010 inofs += not_url_len + long_url_len;
1011 outofs += not_url_len + long_url_len;
1014 /* copy the unmodified block */
1015 strncpy(text + outofs, text + inofs, not_url_len);
1016 inofs += not_url_len;
1017 outofs += not_url_len;
1019 /* copy the new url */
1020 strncpy(text + outofs, url, short_url_len);
1021 inofs += long_url_len;
1022 outofs += short_url_len;
1028 /* copy the last block after the last match */
1030 int tail = inlen - inofs;
1032 strncpy(text + outofs, text + inofs, tail);
1039 (void)pcloseRWE(shrink_pid, shrink_pipe);
1042 dbg("after len=%u\n", outofs);
1046 int main(int argc, char *argv[], char *envp[])
1048 static const struct option options[] = {
1049 { "debug", 0, NULL, 'd' },
1050 { "verbose", 0, NULL, 'V' },
1051 { "account", 1, NULL, 'a' },
1052 { "password", 1, NULL, 'p' },
1053 { "host", 1, NULL, 'H' },
1054 { "proxy", 1, NULL, 'P' },
1055 { "action", 1, NULL, 'A' },
1056 { "user", 1, NULL, 'u' },
1057 { "group", 1, NULL, 'G' },
1058 { "logfile", 1, NULL, 'L' },
1059 { "shrink-urls", 0, NULL, 's' },
1060 { "help", 0, NULL, 'h' },
1061 { "bash", 0, NULL, 'b' },
1062 { "dry-run", 0, NULL, 'n' },
1063 { "page", 1, NULL, 'g' },
1064 { "version", 0, NULL, 'v' },
1067 struct session *session;
1070 static char password[80];
1080 session = session_alloc();
1082 fprintf(stderr, "no more memory...\n");
1086 /* get the current time so that we can log it later */
1088 session->time = strdup(ctime(&t));
1089 session->time[strlen(session->time)-1] = 0x00;
1091 session->homedir = strdup(getenv("HOME"));
1093 curl_global_init(CURL_GLOBAL_ALL);
1095 /* Set environment variables first, before reading command line options
1096 * or config file values. */
1097 http_proxy = getenv("http_proxy");
1100 free(session->proxy);
1101 session->proxy = strdup(http_proxy);
1102 dbg("http_proxy = %s\n", session->proxy);
1105 parse_configfile(session);
1108 option = getopt_long_only(argc, argv, "dp:P:H:a:A:u:hg:G:snVv",
1120 if (session->account)
1121 free(session->account);
1122 session->account = strdup(optarg);
1123 dbg("account = %s\n", session->account);
1126 page_nr = atoi(optarg);
1127 dbg("page = %d\n", page_nr);
1128 session->page = page_nr;
1131 if (session->password)
1132 free(session->password);
1133 session->password = strdup(optarg);
1134 dbg("password = %s\n", session->password);
1138 free(session->proxy);
1139 session->proxy = strdup(optarg);
1140 dbg("proxy = %s\n", session->proxy);
1143 if (strcasecmp(optarg, "update") == 0)
1144 session->action = ACTION_UPDATE;
1145 else if (strcasecmp(optarg, "friends") == 0)
1146 session->action = ACTION_FRIENDS;
1147 else if (strcasecmp(optarg, "user") == 0)
1148 session->action = ACTION_USER;
1149 else if (strcasecmp(optarg, "replies") == 0)
1150 session->action = ACTION_REPLIES;
1151 else if (strcasecmp(optarg, "public") == 0)
1152 session->action = ACTION_PUBLIC;
1153 else if (strcasecmp(optarg, "group") == 0)
1154 session->action = ACTION_GROUP;
1156 session->action = ACTION_UNKNOWN;
1157 dbg("action = %d\n", session->action);
1161 free(session->user);
1162 session->user = strdup(optarg);
1163 dbg("user = %s\n", session->user);
1168 free(session->group);
1169 session->group = strdup(optarg);
1170 dbg("group = %s\n", session->group);
1173 if (session->logfile)
1174 free(session->logfile);
1175 session->logfile = strdup(optarg);
1176 dbg("logfile = %s\n", session->logfile);
1179 session->shrink_urls = 1;
1182 if (session->hosturl)
1183 free(session->hosturl);
1184 if (session->hostname)
1185 free(session->hostname);
1186 if (strcasecmp(optarg, "twitter") == 0) {
1187 session->host = HOST_TWITTER;
1188 session->hosturl = strdup(twitter_host);
1189 session->hostname = strdup(twitter_name);
1190 } else if (strcasecmp(optarg, "identica") == 0) {
1191 session->host = HOST_IDENTICA;
1192 session->hosturl = strdup(identica_host);
1193 session->hostname = strdup(identica_name);
1195 session->host = HOST_CUSTOM;
1196 session->hosturl = strdup(optarg);
1197 session->hostname = strdup(optarg);
1199 dbg("host = %d\n", session->host);
1208 session->dry_run = 1;
1219 session_readline_init(session);
1221 * Show the version to make it easier to determine what
1227 if (session->action == ACTION_UNKNOWN) {
1228 fprintf(stderr, "Unknown action, valid actions are:\n");
1229 fprintf(stderr, "'update', 'friends', 'public', "
1230 "'replies', 'group' or 'user'.\n");
1234 if (session->host == HOST_TWITTER && session->action == ACTION_GROUP) {
1235 fprintf(stderr, "Groups only work in Identi.ca.\n");
1239 if (session->action == ACTION_GROUP && !session->group) {
1240 fprintf(stdout, "Enter group name: ");
1241 session->group = session->readline(NULL);
1244 if (!session->account) {
1245 fprintf(stdout, "Enter account for %s: ", session->hostname);
1246 session->account = session->readline(NULL);
1249 if (!session->password) {
1250 read_password(password, sizeof(password), session->hostname);
1251 session->password = strdup(password);
1254 if (session->action == ACTION_UPDATE) {
1255 if (session->bash || !session->interactive)
1256 tweet = get_string_from_stdin();
1258 tweet = session->readline("tweet: ");
1259 if (!tweet || strlen(tweet) == 0) {
1264 if (session->shrink_urls)
1265 tweet = shrink_urls(tweet);
1267 session->tweet = zalloc(strlen(tweet) + 10);
1269 sprintf(session->tweet, "%c %s",
1270 getuid() ? '$' : '#', tweet);
1272 sprintf(session->tweet, "%s", tweet);
1275 dbg("tweet = %s\n", session->tweet);
1279 session->user = strdup(session->account);
1281 if (session->page == 0)
1283 dbg("account = %s\n", session->account);
1284 dbg("password = %s\n", session->password);
1285 dbg("host = %d\n", session->host);
1286 dbg("action = %d\n", session->action);
1288 /* fork ourself so that the main shell can get on
1289 * with it's life as we try to connect and handle everything
1291 if (session->bash) {
1294 dbg("child is %d\n", child);
1299 retval = send_request(session);
1300 if (retval && !session->bash)
1301 fprintf(stderr, "operation failed\n");
1303 log_session(session, retval);
1305 session_readline_cleanup(session);
1306 session_free(session);