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__ , \
93 void *readline_handle;
94 char *(*readline)(const char *);
97 struct bti_curl_buffer {
103 static void display_help(void)
105 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n");
106 fprintf(stdout, "Version: " VERSION "\n");
107 fprintf(stdout, "Usage:\n");
108 fprintf(stdout, " bti [options]\n");
109 fprintf(stdout, "options are:\n");
110 fprintf(stdout, " --account accountname\n");
111 fprintf(stdout, " --password password\n");
112 fprintf(stdout, " --action action\n");
113 fprintf(stdout, " ('update', 'friends', 'public', 'replies', "
114 "'group' or 'user')\n");
115 fprintf(stdout, " --user screenname\n");
116 fprintf(stdout, " --group groupname\n");
117 fprintf(stdout, " --proxy PROXY:PORT\n");
118 fprintf(stdout, " --host HOST\n");
119 fprintf(stdout, " --logfile logfile\n");
120 fprintf(stdout, " --shrink-urls\n");
121 fprintf(stdout, " --page PAGENUMBER\n");
122 fprintf(stdout, " --bash\n");
123 fprintf(stdout, " --debug\n");
124 fprintf(stdout, " --verbose\n");
125 fprintf(stdout, " --dry-run\n");
126 fprintf(stdout, " --version\n");
127 fprintf(stdout, " --help\n");
130 static void display_version(void)
132 fprintf(stdout, "bti - version %s\n", VERSION);
135 static char *get_string(const char *name)
140 string = zalloc(1000);
144 fprintf(stdout, "%s", name);
145 if (!fgets(string, 999, stdin))
147 temp = strchr(string, '\n');
154 * Try to get a handle to a readline function from a variety of different
155 * libraries. If nothing is present on the system, then fall back to an
158 * Logic originally based off of code in the e2fsutils package in the
159 * lib/ss/get_readline.c file, which is licensed under the MIT license.
161 * This keeps us from having to relicense the bti codebase if readline
162 * ever changes its license, as there is no link-time dependancy.
163 * It is a run-time thing only, and we handle any readline-like library
164 * in the same manner, making bti not be a derivative work of any
167 static void session_readline_init(struct session *session)
169 /* Libraries we will try to use for readline/editline functionality */
170 const char *libpath = "libreadline.so.6:libreadline.so.5:"
171 "libreadline.so.4:libreadline.so:libedit.so.2:"
172 "libedit.so:libeditline.so.0:libeditline.so";
174 char *tmp, *cp, *next;
175 int (*bind_key)(int, void *);
176 void (*insert)(void);
178 /* default to internal function if we can't or won't find anything */
179 session->readline = get_string;
182 session->interactive = 1;
184 tmp = malloc(strlen(libpath)+1);
187 strcpy(tmp, libpath);
188 for (cp = tmp; cp; cp = next) {
189 next = strchr(cp, ':');
194 handle = dlopen(cp, RTLD_NOW);
196 dbg("Using %s for readline library\n", cp);
202 dbg("No readline library found.\n");
206 session->readline_handle = handle;
207 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
208 if (session->readline == NULL) {
209 /* something odd happened, default back to internal stuff */
210 session->readline_handle = NULL;
211 session->readline = get_string;
216 * If we found a library, turn off filename expansion
217 * as that makes no sense from within bti.
219 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
220 insert = (void (*)(void))dlsym(handle, "rl_insert");
221 if (bind_key && insert)
222 bind_key('\t', insert);
225 static void session_readline_cleanup(struct session *session)
227 if (session->readline_handle)
228 dlclose(session->readline_handle);
231 static struct session *session_alloc(void)
233 struct session *session;
235 session = zalloc(sizeof(*session));
241 static void session_free(struct session *session)
245 free(session->password);
246 free(session->account);
247 free(session->tweet);
248 free(session->proxy);
250 free(session->homedir);
252 free(session->group);
253 free(session->hosturl);
254 free(session->hostname);
255 free(session->configfile);
259 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
261 struct bti_curl_buffer *buffer;
263 buffer = zalloc(sizeof(*buffer));
267 /* start out with a data buffer of 1 byte to
268 * make the buffer fill logic simpler */
269 buffer->data = zalloc(1);
275 buffer->action = action;
279 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
287 static const char *twitter_host = "https://twitter.com/statuses";
288 static const char *identica_host = "https://identi.ca/api/statuses";
289 static const char *twitter_name = "twitter";
290 static const char *identica_name = "identi.ca";
292 static const char *user_uri = "/user_timeline/";
293 static const char *update_uri = "/update.xml";
294 static const char *public_uri = "/public_timeline.xml";
295 static const char *friends_uri = "/friends_timeline.xml";
296 static const char *replies_uri = "/replies.xml";
297 static const char *group_uri = "/../laconica/groups/timeline/";
299 static CURL *curl_init(void)
303 curl = curl_easy_init();
305 fprintf(stderr, "Can not init CURL!\n");
308 /* some ssl sanity checks on the connection we are making */
309 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
310 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
314 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
316 xmlChar *text = NULL;
317 xmlChar *user = NULL;
318 xmlChar *created = NULL;
321 current = current->xmlChildrenNode;
322 while (current != NULL) {
323 if (current->type == XML_ELEMENT_NODE) {
324 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
325 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
326 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
327 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
328 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
329 userinfo = current->xmlChildrenNode;
330 while (userinfo != NULL) {
331 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
334 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
336 userinfo = userinfo->next;
340 if (user && text && created) {
342 printf("[%s] (%.16s) %s\n",
343 user, created, text);
355 current = current->next;
361 static void parse_timeline(char *document)
366 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
367 NULL, XML_PARSE_NOERROR);
371 current = xmlDocGetRootElement(doc);
372 if (current == NULL) {
373 fprintf(stderr, "empty document\n");
378 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
379 fprintf(stderr, "unexpected document type\n");
384 current = current->xmlChildrenNode;
385 while (current != NULL) {
386 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
387 parse_statuses(doc, current);
388 current = current->next;
395 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
398 struct bti_curl_buffer *curl_buf = userp;
399 size_t buffer_size = size * nmemb;
402 if ((!buffer) || (!buffer_size) || (!curl_buf))
405 /* add to the data we already have */
406 temp = zalloc(curl_buf->length + buffer_size + 1);
410 memcpy(temp, curl_buf->data, curl_buf->length);
411 free(curl_buf->data);
412 curl_buf->data = temp;
413 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
414 curl_buf->length += buffer_size;
415 if (curl_buf->action)
416 parse_timeline(curl_buf->data);
418 dbg("%s\n", curl_buf->data);
423 static int send_request(struct session *session)
426 char user_password[500];
428 struct bti_curl_buffer *curl_buf;
431 struct curl_httppost *formpost = NULL;
432 struct curl_httppost *lastptr = NULL;
433 struct curl_slist *slist = NULL;
438 curl_buf = bti_curl_buffer_alloc(session->action);
446 if (!session->hosturl)
447 session->hosturl = strdup(twitter_host);
449 switch (session->action) {
451 snprintf(user_password, sizeof(user_password), "%s:%s",
452 session->account, session->password);
453 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
454 curl_formadd(&formpost, &lastptr,
455 CURLFORM_COPYNAME, "status",
456 CURLFORM_COPYCONTENTS, session->tweet,
459 curl_formadd(&formpost, &lastptr,
460 CURLFORM_COPYNAME, "source",
461 CURLFORM_COPYCONTENTS, "bti",
464 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
465 slist = curl_slist_append(slist, "Expect:");
466 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
468 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
469 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
470 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
474 snprintf(user_password, sizeof(user_password), "%s:%s",
475 session->account, session->password);
476 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
477 friends_uri, session->page);
478 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
479 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
483 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
484 user_uri, session->user, session->page);
485 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
489 snprintf(user_password, sizeof(user_password), "%s:%s",
490 session->account, session->password);
491 sprintf(endpoint, "%s%s?page=%d", session->hosturl, replies_uri,
493 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
494 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
498 sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri,
500 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
504 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
505 group_uri, session->group, session->page);
506 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
514 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
517 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
519 dbg("user_password = %s\n", user_password);
520 dbg("data = %s\n", data);
521 dbg("proxy = %s\n", session->proxy);
523 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
524 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
525 if (!session->dry_run) {
526 res = curl_easy_perform(curl);
527 if (res && !session->bash) {
528 fprintf(stderr, "error(%d) trying to perform "
534 curl_easy_cleanup(curl);
535 if (session->action == ACTION_UPDATE)
536 curl_formfree(formpost);
537 bti_curl_buffer_free(curl_buf);
541 static void parse_configfile(struct session *session)
546 char *account = NULL;
547 char *password = NULL;
550 char *logfile = NULL;
556 config_file = fopen(session->configfile, "r");
558 /* No error if file does not exist or is unreadable. */
559 if (config_file == NULL)
563 ssize_t n = getline(&line, &len, config_file);
566 if (line[n - 1] == '\n')
568 /* Parse file. Format is the usual value pairs:
571 # is a comment character
573 *strchrnul(line, '#') = '\0';
577 /* Ignore blank lines. */
581 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
585 } else if (!strncasecmp(c, "password", 8) &&
589 password = strdup(c);
590 } else if (!strncasecmp(c, "host", 4) &&
595 } else if (!strncasecmp(c, "proxy", 5) &&
600 } else if (!strncasecmp(c, "logfile", 7) &&
605 } else if (!strncasecmp(c, "action", 6) &&
610 } else if (!strncasecmp(c, "user", 4) &&
615 } else if (!strncasecmp(c, "shrink-urls", 11) &&
618 if (!strncasecmp(c, "true", 4) ||
619 !strncasecmp(c, "yes", 3))
621 } else if (!strncasecmp(c, "verbose", 7) &&
624 if (!strncasecmp(c, "true", 4) ||
625 !strncasecmp(c, "yes", 3))
628 } while (!feof(config_file));
631 session->password = password;
633 session->account = account;
635 if (strcasecmp(host, "twitter") == 0) {
636 session->host = HOST_TWITTER;
637 session->hosturl = strdup(twitter_host);
638 session->hostname = strdup(twitter_name);
639 } else if (strcasecmp(host, "identica") == 0) {
640 session->host = HOST_IDENTICA;
641 session->hosturl = strdup(identica_host);
642 session->hostname = strdup(identica_name);
644 session->host = HOST_CUSTOM;
645 session->hosturl = strdup(host);
646 session->hostname = strdup(host);
652 free(session->proxy);
653 session->proxy = proxy;
656 session->logfile = logfile;
658 if (strcasecmp(action, "update") == 0)
659 session->action = ACTION_UPDATE;
660 else if (strcasecmp(action, "friends") == 0)
661 session->action = ACTION_FRIENDS;
662 else if (strcasecmp(action, "user") == 0)
663 session->action = ACTION_USER;
664 else if (strcasecmp(action, "replies") == 0)
665 session->action = ACTION_REPLIES;
666 else if (strcasecmp(action, "public") == 0)
667 session->action = ACTION_PUBLIC;
668 else if (strcasecmp(action, "group") == 0)
669 session->action = ACTION_GROUP;
671 session->action = ACTION_UNKNOWN;
675 session->user = user;
676 session->shrink_urls = shrink_urls;
678 /* Free buffer and close file. */
683 static void log_session(struct session *session, int retval)
688 /* Only log something if we have a log file set */
689 if (!session->logfile)
692 filename = alloca(strlen(session->homedir) +
693 strlen(session->logfile) + 3);
695 sprintf(filename, "%s/%s", session->homedir, session->logfile);
697 log_file = fopen(filename, "a+");
698 if (log_file == NULL)
701 switch (session->action) {
704 fprintf(log_file, "%s: host=%s tweet failed\n",
705 session->time, session->hostname);
707 fprintf(log_file, "%s: host=%s tweet=%s\n",
708 session->time, session->hostname,
712 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
713 session->time, session->hostname);
716 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
717 session->time, session->hostname, session->user);
720 fprintf(log_file, "%s: host=%s retrieving replies\n",
721 session->time, session->hostname);
724 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
725 session->time, session->hostname);
728 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
729 session->time, session->hostname);
738 static char *get_string_from_stdin(void)
743 string = zalloc(1000);
747 if (!fgets(string, 999, stdin))
749 temp = strchr(string, '\n');
755 static void read_password(char *buf, size_t len, char *host)
765 tp.c_lflag &= (~ECHO);
766 tcsetattr(0, TCSANOW, &tp);
768 fprintf(stdout, "Enter password for %s: ", host);
771 retval = scanf("%79s", pwd);
773 fprintf(stdout, "\n");
775 tcsetattr(0, TCSANOW, &old);
777 strncpy(buf, pwd, len);
781 static int find_urls(const char *tweet, int **pranges)
784 * magic obtained from
785 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
787 static const char *re_magic =
788 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
789 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
790 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
794 int ovector[10] = {0,};
795 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
796 int startoffset, tweetlen;
800 int *ranges = malloc(sizeof(int) * rbound);
802 re = pcre_compile(re_magic,
803 PCRE_NO_AUTO_CAPTURE,
804 &errptr, &erroffset, NULL);
806 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
810 tweetlen = strlen(tweet);
811 for (startoffset = 0; startoffset < tweetlen; ) {
813 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
815 if (rc == PCRE_ERROR_NOMATCH)
819 fprintf(stderr, "pcre_exec @%u: %s\n",
824 for (i = 0; i < rc; i += 2) {
825 if ((rcount+2) == rbound) {
827 ranges = realloc(ranges, sizeof(int) * rbound);
830 ranges[rcount++] = ovector[i];
831 ranges[rcount++] = ovector[i+1];
834 startoffset = ovector[1];
844 * bidirectional popen() call
846 * @param rwepipe - int array of size three
847 * @param exe - program to run
848 * @param argv - argument list
849 * @return pid or -1 on error
851 * The caller passes in an array of three integers (rwepipe), on successful
852 * execution it can then write to element 0 (stdin of exe), and read from
853 * element 1 (stdout) and 2 (stderr).
855 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
885 } else if (pid == 0) {
897 execvp(exe, (char **)argv);
917 static int pcloseRWE(int pid, int *rwepipe)
923 rc = waitpid(pid, &status, 0);
927 static char *shrink_one_url(int *rwepipe, char *big)
929 int biglen = strlen(big);
934 rc = dprintf(rwepipe[0], "%s\n", big);
938 smalllen = biglen + 128;
939 small = malloc(smalllen);
943 rc = read(rwepipe[1], small, smalllen);
944 if (rc < 0 || rc > biglen)
945 goto error_free_small;
947 if (strncmp(small, "http://", 7))
948 goto error_free_small;
951 while (smalllen && isspace(small[smalllen-1]))
952 small[--smalllen] = 0;
962 static char *shrink_urls(char *text)
969 const char *const shrink_args[] = {
975 int inlen = strlen(text);
977 dbg("before len=%u\n", inlen);
979 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
983 rcount = find_urls(text, &ranges);
987 for (i = 0; i < rcount; i += 2) {
988 int url_start = ranges[i];
989 int url_end = ranges[i+1];
990 int long_url_len = url_end - url_start;
991 char *url = strndup(text + url_start, long_url_len);
993 int not_url_len = url_start - inofs;
995 dbg("long url[%u]: %s\n", long_url_len, url);
996 url = shrink_one_url(shrink_pipe, url);
997 short_url_len = url ? strlen(url) : 0;
998 dbg("short url[%u]: %s\n", short_url_len, url);
1000 if (!url || short_url_len >= long_url_len) {
1001 /* The short url ended up being too long
1004 strncpy(text + outofs, text + inofs,
1005 not_url_len + long_url_len);
1007 inofs += not_url_len + long_url_len;
1008 outofs += not_url_len + long_url_len;
1011 /* copy the unmodified block */
1012 strncpy(text + outofs, text + inofs, not_url_len);
1013 inofs += not_url_len;
1014 outofs += not_url_len;
1016 /* copy the new url */
1017 strncpy(text + outofs, url, short_url_len);
1018 inofs += long_url_len;
1019 outofs += short_url_len;
1025 /* copy the last block after the last match */
1027 int tail = inlen - inofs;
1029 strncpy(text + outofs, text + inofs, tail);
1036 (void)pcloseRWE(shrink_pid, shrink_pipe);
1039 dbg("after len=%u\n", outofs);
1043 int main(int argc, char *argv[], char *envp[])
1045 static const struct option options[] = {
1046 { "debug", 0, NULL, 'd' },
1047 { "verbose", 0, NULL, 'V' },
1048 { "account", 1, NULL, 'a' },
1049 { "password", 1, NULL, 'p' },
1050 { "host", 1, NULL, 'H' },
1051 { "proxy", 1, NULL, 'P' },
1052 { "action", 1, NULL, 'A' },
1053 { "user", 1, NULL, 'u' },
1054 { "group", 1, NULL, 'G' },
1055 { "logfile", 1, NULL, 'L' },
1056 { "shrink-urls", 0, NULL, 's' },
1057 { "help", 0, NULL, 'h' },
1058 { "bash", 0, NULL, 'b' },
1059 { "dry-run", 0, NULL, 'n' },
1060 { "page", 1, NULL, 'g' },
1061 { "version", 0, NULL, 'v' },
1064 struct session *session;
1067 static char password[80];
1077 session = session_alloc();
1079 fprintf(stderr, "no more memory...\n");
1083 /* get the current time so that we can log it later */
1085 session->time = strdup(ctime(&t));
1086 session->time[strlen(session->time)-1] = 0x00;
1088 /* Get the home directory so we can try to find a config file */
1089 session->homedir = strdup(getenv("HOME"));
1091 /* set up a default config file location (traditionally ~/.bti) */
1092 session->configfile = zalloc(strlen(session->homedir) + 7);
1093 sprintf(session->configfile, "%s/.bti", session->homedir);
1095 curl_global_init(CURL_GLOBAL_ALL);
1097 /* Set environment variables first, before reading command line options
1098 * or config file values. */
1099 http_proxy = getenv("http_proxy");
1102 free(session->proxy);
1103 session->proxy = strdup(http_proxy);
1104 dbg("http_proxy = %s\n", session->proxy);
1107 parse_configfile(session);
1110 option = getopt_long_only(argc, argv, "dp:P:H:a:A:u:hg:G:snVv",
1122 if (session->account)
1123 free(session->account);
1124 session->account = strdup(optarg);
1125 dbg("account = %s\n", session->account);
1128 page_nr = atoi(optarg);
1129 dbg("page = %d\n", page_nr);
1130 session->page = page_nr;
1133 if (session->password)
1134 free(session->password);
1135 session->password = strdup(optarg);
1136 dbg("password = %s\n", session->password);
1140 free(session->proxy);
1141 session->proxy = strdup(optarg);
1142 dbg("proxy = %s\n", session->proxy);
1145 if (strcasecmp(optarg, "update") == 0)
1146 session->action = ACTION_UPDATE;
1147 else if (strcasecmp(optarg, "friends") == 0)
1148 session->action = ACTION_FRIENDS;
1149 else if (strcasecmp(optarg, "user") == 0)
1150 session->action = ACTION_USER;
1151 else if (strcasecmp(optarg, "replies") == 0)
1152 session->action = ACTION_REPLIES;
1153 else if (strcasecmp(optarg, "public") == 0)
1154 session->action = ACTION_PUBLIC;
1155 else if (strcasecmp(optarg, "group") == 0)
1156 session->action = ACTION_GROUP;
1158 session->action = ACTION_UNKNOWN;
1159 dbg("action = %d\n", session->action);
1163 free(session->user);
1164 session->user = strdup(optarg);
1165 dbg("user = %s\n", session->user);
1170 free(session->group);
1171 session->group = strdup(optarg);
1172 dbg("group = %s\n", session->group);
1175 if (session->logfile)
1176 free(session->logfile);
1177 session->logfile = strdup(optarg);
1178 dbg("logfile = %s\n", session->logfile);
1181 session->shrink_urls = 1;
1184 if (session->hosturl)
1185 free(session->hosturl);
1186 if (session->hostname)
1187 free(session->hostname);
1188 if (strcasecmp(optarg, "twitter") == 0) {
1189 session->host = HOST_TWITTER;
1190 session->hosturl = strdup(twitter_host);
1191 session->hostname = strdup(twitter_name);
1192 } else if (strcasecmp(optarg, "identica") == 0) {
1193 session->host = HOST_IDENTICA;
1194 session->hosturl = strdup(identica_host);
1195 session->hostname = strdup(identica_name);
1197 session->host = HOST_CUSTOM;
1198 session->hosturl = strdup(optarg);
1199 session->hostname = strdup(optarg);
1201 dbg("host = %d\n", session->host);
1210 session->dry_run = 1;
1221 session_readline_init(session);
1223 * Show the version to make it easier to determine what
1229 if (session->action == ACTION_UNKNOWN) {
1230 fprintf(stderr, "Unknown action, valid actions are:\n");
1231 fprintf(stderr, "'update', 'friends', 'public', "
1232 "'replies', 'group' or 'user'.\n");
1236 if (session->host == HOST_TWITTER && session->action == ACTION_GROUP) {
1237 fprintf(stderr, "Groups only work in Identi.ca.\n");
1241 if (session->action == ACTION_GROUP && !session->group) {
1242 fprintf(stdout, "Enter group name: ");
1243 session->group = session->readline(NULL);
1246 if (!session->account) {
1247 fprintf(stdout, "Enter account for %s: ", session->hostname);
1248 session->account = session->readline(NULL);
1251 if (!session->password) {
1252 read_password(password, sizeof(password), session->hostname);
1253 session->password = strdup(password);
1256 if (session->action == ACTION_UPDATE) {
1257 if (session->bash || !session->interactive)
1258 tweet = get_string_from_stdin();
1260 tweet = session->readline("tweet: ");
1261 if (!tweet || strlen(tweet) == 0) {
1266 if (session->shrink_urls)
1267 tweet = shrink_urls(tweet);
1269 session->tweet = zalloc(strlen(tweet) + 10);
1271 sprintf(session->tweet, "%c %s",
1272 getuid() ? '$' : '#', tweet);
1274 sprintf(session->tweet, "%s", tweet);
1277 dbg("tweet = %s\n", session->tweet);
1281 session->user = strdup(session->account);
1283 if (session->page == 0)
1285 dbg("config file = %s\n", session->configfile);
1286 dbg("account = %s\n", session->account);
1287 dbg("password = %s\n", session->password);
1288 dbg("host = %d\n", session->host);
1289 dbg("action = %d\n", session->action);
1291 /* fork ourself so that the main shell can get on
1292 * with it's life as we try to connect and handle everything
1294 if (session->bash) {
1297 dbg("child is %d\n", child);
1302 retval = send_request(session);
1303 if (retval && !session->bash)
1304 fprintf(stderr, "operation failed\n");
1306 log_session(session, retval);
1308 session_readline_cleanup(session);
1309 session_free(session);