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, " --config configfile\n");
121 fprintf(stdout, " --shrink-urls\n");
122 fprintf(stdout, " --page PAGENUMBER\n");
123 fprintf(stdout, " --bash\n");
124 fprintf(stdout, " --debug\n");
125 fprintf(stdout, " --verbose\n");
126 fprintf(stdout, " --dry-run\n");
127 fprintf(stdout, " --version\n");
128 fprintf(stdout, " --help\n");
131 static void display_version(void)
133 fprintf(stdout, "bti - version %s\n", VERSION);
136 static char *get_string(const char *name)
141 string = zalloc(1000);
145 fprintf(stdout, "%s", name);
146 if (!fgets(string, 999, stdin))
148 temp = strchr(string, '\n');
155 * Try to get a handle to a readline function from a variety of different
156 * libraries. If nothing is present on the system, then fall back to an
159 * Logic originally based off of code in the e2fsutils package in the
160 * lib/ss/get_readline.c file, which is licensed under the MIT license.
162 * This keeps us from having to relicense the bti codebase if readline
163 * ever changes its license, as there is no link-time dependancy.
164 * It is a run-time thing only, and we handle any readline-like library
165 * in the same manner, making bti not be a derivative work of any
168 static void session_readline_init(struct session *session)
170 /* Libraries we will try to use for readline/editline functionality */
171 const char *libpath = "libreadline.so.6:libreadline.so.5:"
172 "libreadline.so.4:libreadline.so:libedit.so.2:"
173 "libedit.so:libeditline.so.0:libeditline.so";
175 char *tmp, *cp, *next;
176 int (*bind_key)(int, void *);
177 void (*insert)(void);
179 /* default to internal function if we can't or won't find anything */
180 session->readline = get_string;
183 session->interactive = 1;
185 tmp = malloc(strlen(libpath)+1);
188 strcpy(tmp, libpath);
189 for (cp = tmp; cp; cp = next) {
190 next = strchr(cp, ':');
195 handle = dlopen(cp, RTLD_NOW);
197 dbg("Using %s for readline library\n", cp);
203 dbg("No readline library found.\n");
207 session->readline_handle = handle;
208 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
209 if (session->readline == NULL) {
210 /* something odd happened, default back to internal stuff */
211 session->readline_handle = NULL;
212 session->readline = get_string;
217 * If we found a library, turn off filename expansion
218 * as that makes no sense from within bti.
220 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
221 insert = (void (*)(void))dlsym(handle, "rl_insert");
222 if (bind_key && insert)
223 bind_key('\t', insert);
226 static void session_readline_cleanup(struct session *session)
228 if (session->readline_handle)
229 dlclose(session->readline_handle);
232 static struct session *session_alloc(void)
234 struct session *session;
236 session = zalloc(sizeof(*session));
242 static void session_free(struct session *session)
246 free(session->password);
247 free(session->account);
248 free(session->tweet);
249 free(session->proxy);
251 free(session->homedir);
253 free(session->group);
254 free(session->hosturl);
255 free(session->hostname);
256 free(session->configfile);
260 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
262 struct bti_curl_buffer *buffer;
264 buffer = zalloc(sizeof(*buffer));
268 /* start out with a data buffer of 1 byte to
269 * make the buffer fill logic simpler */
270 buffer->data = zalloc(1);
276 buffer->action = action;
280 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
288 static const char *twitter_host = "https://twitter.com/statuses";
289 static const char *identica_host = "https://identi.ca/api/statuses";
290 static const char *twitter_name = "twitter";
291 static const char *identica_name = "identi.ca";
293 static const char *user_uri = "/user_timeline/";
294 static const char *update_uri = "/update.xml";
295 static const char *public_uri = "/public_timeline.xml";
296 static const char *friends_uri = "/friends_timeline.xml";
297 static const char *replies_uri = "/replies.xml";
298 static const char *group_uri = "/../laconica/groups/timeline/";
300 static CURL *curl_init(void)
304 curl = curl_easy_init();
306 fprintf(stderr, "Can not init CURL!\n");
309 /* some ssl sanity checks on the connection we are making */
310 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
311 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
315 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
317 xmlChar *text = NULL;
318 xmlChar *user = NULL;
319 xmlChar *created = NULL;
323 current = current->xmlChildrenNode;
324 while (current != NULL) {
325 if (current->type == XML_ELEMENT_NODE) {
326 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
327 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
328 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
329 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
330 if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
331 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
332 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
333 userinfo = current->xmlChildrenNode;
334 while (userinfo != NULL) {
335 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
338 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
340 userinfo = userinfo->next;
344 if (user && text && created && id) {
346 printf("[%s] {%s} (%.16s) %s\n",
347 user, id, created, text);
361 current = current->next;
367 static void parse_timeline(char *document)
372 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
373 NULL, XML_PARSE_NOERROR);
377 current = xmlDocGetRootElement(doc);
378 if (current == NULL) {
379 fprintf(stderr, "empty document\n");
384 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
385 fprintf(stderr, "unexpected document type\n");
390 current = current->xmlChildrenNode;
391 while (current != NULL) {
392 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
393 parse_statuses(doc, current);
394 current = current->next;
401 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
404 struct bti_curl_buffer *curl_buf = userp;
405 size_t buffer_size = size * nmemb;
408 if ((!buffer) || (!buffer_size) || (!curl_buf))
411 /* add to the data we already have */
412 temp = zalloc(curl_buf->length + buffer_size + 1);
416 memcpy(temp, curl_buf->data, curl_buf->length);
417 free(curl_buf->data);
418 curl_buf->data = temp;
419 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
420 curl_buf->length += buffer_size;
421 if (curl_buf->action)
422 parse_timeline(curl_buf->data);
424 dbg("%s\n", curl_buf->data);
429 static int send_request(struct session *session)
432 char user_password[500];
434 struct bti_curl_buffer *curl_buf;
437 struct curl_httppost *formpost = NULL;
438 struct curl_httppost *lastptr = NULL;
439 struct curl_slist *slist = NULL;
444 curl_buf = bti_curl_buffer_alloc(session->action);
452 if (!session->hosturl)
453 session->hosturl = strdup(twitter_host);
455 switch (session->action) {
457 snprintf(user_password, sizeof(user_password), "%s:%s",
458 session->account, session->password);
459 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
460 curl_formadd(&formpost, &lastptr,
461 CURLFORM_COPYNAME, "status",
462 CURLFORM_COPYCONTENTS, session->tweet,
465 curl_formadd(&formpost, &lastptr,
466 CURLFORM_COPYNAME, "source",
467 CURLFORM_COPYCONTENTS, "bti",
470 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
471 slist = curl_slist_append(slist, "Expect:");
472 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
474 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
475 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
476 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
480 snprintf(user_password, sizeof(user_password), "%s:%s",
481 session->account, session->password);
482 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
483 friends_uri, session->page);
484 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
485 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
489 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
490 user_uri, session->user, session->page);
491 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
495 snprintf(user_password, sizeof(user_password), "%s:%s",
496 session->account, session->password);
497 sprintf(endpoint, "%s%s?page=%d", session->hosturl, replies_uri,
499 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
500 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
504 sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri,
506 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
510 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
511 group_uri, session->group, session->page);
512 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
520 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
523 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
525 dbg("user_password = %s\n", user_password);
526 dbg("data = %s\n", data);
527 dbg("proxy = %s\n", session->proxy);
529 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
530 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
531 if (!session->dry_run) {
532 res = curl_easy_perform(curl);
533 if (res && !session->bash) {
534 fprintf(stderr, "error(%d) trying to perform "
540 curl_easy_cleanup(curl);
541 if (session->action == ACTION_UPDATE)
542 curl_formfree(formpost);
543 bti_curl_buffer_free(curl_buf);
547 static void parse_configfile(struct session *session)
552 char *account = NULL;
553 char *password = NULL;
556 char *logfile = NULL;
562 config_file = fopen(session->configfile, "r");
564 /* No error if file does not exist or is unreadable. */
565 if (config_file == NULL)
569 ssize_t n = getline(&line, &len, config_file);
572 if (line[n - 1] == '\n')
574 /* Parse file. Format is the usual value pairs:
577 # is a comment character
579 *strchrnul(line, '#') = '\0';
583 /* Ignore blank lines. */
587 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
591 } else if (!strncasecmp(c, "password", 8) &&
595 password = strdup(c);
596 } else if (!strncasecmp(c, "host", 4) &&
601 } else if (!strncasecmp(c, "proxy", 5) &&
606 } else if (!strncasecmp(c, "logfile", 7) &&
611 } else if (!strncasecmp(c, "action", 6) &&
616 } else if (!strncasecmp(c, "user", 4) &&
621 } else if (!strncasecmp(c, "shrink-urls", 11) &&
624 if (!strncasecmp(c, "true", 4) ||
625 !strncasecmp(c, "yes", 3))
627 } else if (!strncasecmp(c, "verbose", 7) &&
630 if (!strncasecmp(c, "true", 4) ||
631 !strncasecmp(c, "yes", 3))
634 } while (!feof(config_file));
637 session->password = password;
639 session->account = account;
641 if (strcasecmp(host, "twitter") == 0) {
642 session->host = HOST_TWITTER;
643 session->hosturl = strdup(twitter_host);
644 session->hostname = strdup(twitter_name);
645 } else if (strcasecmp(host, "identica") == 0) {
646 session->host = HOST_IDENTICA;
647 session->hosturl = strdup(identica_host);
648 session->hostname = strdup(identica_name);
650 session->host = HOST_CUSTOM;
651 session->hosturl = strdup(host);
652 session->hostname = strdup(host);
658 free(session->proxy);
659 session->proxy = proxy;
662 session->logfile = logfile;
664 if (strcasecmp(action, "update") == 0)
665 session->action = ACTION_UPDATE;
666 else if (strcasecmp(action, "friends") == 0)
667 session->action = ACTION_FRIENDS;
668 else if (strcasecmp(action, "user") == 0)
669 session->action = ACTION_USER;
670 else if (strcasecmp(action, "replies") == 0)
671 session->action = ACTION_REPLIES;
672 else if (strcasecmp(action, "public") == 0)
673 session->action = ACTION_PUBLIC;
674 else if (strcasecmp(action, "group") == 0)
675 session->action = ACTION_GROUP;
677 session->action = ACTION_UNKNOWN;
681 session->user = user;
682 session->shrink_urls = shrink_urls;
684 /* Free buffer and close file. */
689 static void log_session(struct session *session, int retval)
694 /* Only log something if we have a log file set */
695 if (!session->logfile)
698 filename = alloca(strlen(session->homedir) +
699 strlen(session->logfile) + 3);
701 sprintf(filename, "%s/%s", session->homedir, session->logfile);
703 log_file = fopen(filename, "a+");
704 if (log_file == NULL)
707 switch (session->action) {
710 fprintf(log_file, "%s: host=%s tweet failed\n",
711 session->time, session->hostname);
713 fprintf(log_file, "%s: host=%s tweet=%s\n",
714 session->time, session->hostname,
718 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
719 session->time, session->hostname);
722 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
723 session->time, session->hostname, session->user);
726 fprintf(log_file, "%s: host=%s retrieving replies\n",
727 session->time, session->hostname);
730 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
731 session->time, session->hostname);
734 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
735 session->time, session->hostname);
744 static char *get_string_from_stdin(void)
749 string = zalloc(1000);
753 if (!fgets(string, 999, stdin))
755 temp = strchr(string, '\n');
761 static void read_password(char *buf, size_t len, char *host)
771 tp.c_lflag &= (~ECHO);
772 tcsetattr(0, TCSANOW, &tp);
774 fprintf(stdout, "Enter password for %s: ", host);
777 retval = scanf("%79s", pwd);
779 fprintf(stdout, "\n");
781 tcsetattr(0, TCSANOW, &old);
783 strncpy(buf, pwd, len);
787 static int find_urls(const char *tweet, int **pranges)
790 * magic obtained from
791 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
793 static const char *re_magic =
794 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
795 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
796 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
800 int ovector[10] = {0,};
801 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
802 int startoffset, tweetlen;
806 int *ranges = malloc(sizeof(int) * rbound);
808 re = pcre_compile(re_magic,
809 PCRE_NO_AUTO_CAPTURE,
810 &errptr, &erroffset, NULL);
812 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
816 tweetlen = strlen(tweet);
817 for (startoffset = 0; startoffset < tweetlen; ) {
819 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
821 if (rc == PCRE_ERROR_NOMATCH)
825 fprintf(stderr, "pcre_exec @%u: %s\n",
830 for (i = 0; i < rc; i += 2) {
831 if ((rcount+2) == rbound) {
833 ranges = realloc(ranges, sizeof(int) * rbound);
836 ranges[rcount++] = ovector[i];
837 ranges[rcount++] = ovector[i+1];
840 startoffset = ovector[1];
850 * bidirectional popen() call
852 * @param rwepipe - int array of size three
853 * @param exe - program to run
854 * @param argv - argument list
855 * @return pid or -1 on error
857 * The caller passes in an array of three integers (rwepipe), on successful
858 * execution it can then write to element 0 (stdin of exe), and read from
859 * element 1 (stdout) and 2 (stderr).
861 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
891 } else if (pid == 0) {
903 execvp(exe, (char **)argv);
923 static int pcloseRWE(int pid, int *rwepipe)
929 rc = waitpid(pid, &status, 0);
933 static char *shrink_one_url(int *rwepipe, char *big)
935 int biglen = strlen(big);
940 rc = dprintf(rwepipe[0], "%s\n", big);
944 smalllen = biglen + 128;
945 small = malloc(smalllen);
949 rc = read(rwepipe[1], small, smalllen);
950 if (rc < 0 || rc > biglen)
951 goto error_free_small;
953 if (strncmp(small, "http://", 7))
954 goto error_free_small;
957 while (smalllen && isspace(small[smalllen-1]))
958 small[--smalllen] = 0;
968 static char *shrink_urls(char *text)
975 const char *const shrink_args[] = {
981 int inlen = strlen(text);
983 dbg("before len=%u\n", inlen);
985 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
989 rcount = find_urls(text, &ranges);
993 for (i = 0; i < rcount; i += 2) {
994 int url_start = ranges[i];
995 int url_end = ranges[i+1];
996 int long_url_len = url_end - url_start;
997 char *url = strndup(text + url_start, long_url_len);
999 int not_url_len = url_start - inofs;
1001 dbg("long url[%u]: %s\n", long_url_len, url);
1002 url = shrink_one_url(shrink_pipe, url);
1003 short_url_len = url ? strlen(url) : 0;
1004 dbg("short url[%u]: %s\n", short_url_len, url);
1006 if (!url || short_url_len >= long_url_len) {
1007 /* The short url ended up being too long
1010 strncpy(text + outofs, text + inofs,
1011 not_url_len + long_url_len);
1013 inofs += not_url_len + long_url_len;
1014 outofs += not_url_len + long_url_len;
1017 /* copy the unmodified block */
1018 strncpy(text + outofs, text + inofs, not_url_len);
1019 inofs += not_url_len;
1020 outofs += not_url_len;
1022 /* copy the new url */
1023 strncpy(text + outofs, url, short_url_len);
1024 inofs += long_url_len;
1025 outofs += short_url_len;
1031 /* copy the last block after the last match */
1033 int tail = inlen - inofs;
1035 strncpy(text + outofs, text + inofs, tail);
1042 (void)pcloseRWE(shrink_pid, shrink_pipe);
1045 dbg("after len=%u\n", outofs);
1049 int main(int argc, char *argv[], char *envp[])
1051 static const struct option options[] = {
1052 { "debug", 0, NULL, 'd' },
1053 { "verbose", 0, NULL, 'V' },
1054 { "account", 1, NULL, 'a' },
1055 { "password", 1, NULL, 'p' },
1056 { "host", 1, NULL, 'H' },
1057 { "proxy", 1, NULL, 'P' },
1058 { "action", 1, NULL, 'A' },
1059 { "user", 1, NULL, 'u' },
1060 { "group", 1, NULL, 'G' },
1061 { "logfile", 1, NULL, 'L' },
1062 { "shrink-urls", 0, NULL, 's' },
1063 { "help", 0, NULL, 'h' },
1064 { "bash", 0, NULL, 'b' },
1065 { "dry-run", 0, NULL, 'n' },
1066 { "page", 1, NULL, 'g' },
1067 { "version", 0, NULL, 'v' },
1068 { "config", 1, NULL, 'c' },
1071 struct session *session;
1074 static char password[80];
1084 session = session_alloc();
1086 fprintf(stderr, "no more memory...\n");
1090 /* get the current time so that we can log it later */
1092 session->time = strdup(ctime(&t));
1093 session->time[strlen(session->time)-1] = 0x00;
1095 /* Get the home directory so we can try to find a config file */
1096 session->homedir = strdup(getenv("HOME"));
1098 /* set up a default config file location (traditionally ~/.bti) */
1099 session->configfile = zalloc(strlen(session->homedir) + 7);
1100 sprintf(session->configfile, "%s/.bti", session->homedir);
1102 curl_global_init(CURL_GLOBAL_ALL);
1104 /* Set environment variables first, before reading command line options
1105 * or config file values. */
1106 http_proxy = getenv("http_proxy");
1109 free(session->proxy);
1110 session->proxy = strdup(http_proxy);
1111 dbg("http_proxy = %s\n", session->proxy);
1114 parse_configfile(session);
1117 option = getopt_long_only(argc, argv, "dp:P:H:a:A:u:c:hg:G:snVv",
1129 if (session->account)
1130 free(session->account);
1131 session->account = strdup(optarg);
1132 dbg("account = %s\n", session->account);
1135 page_nr = atoi(optarg);
1136 dbg("page = %d\n", page_nr);
1137 session->page = page_nr;
1140 if (session->password)
1141 free(session->password);
1142 session->password = strdup(optarg);
1143 dbg("password = %s\n", session->password);
1147 free(session->proxy);
1148 session->proxy = strdup(optarg);
1149 dbg("proxy = %s\n", session->proxy);
1152 if (strcasecmp(optarg, "update") == 0)
1153 session->action = ACTION_UPDATE;
1154 else if (strcasecmp(optarg, "friends") == 0)
1155 session->action = ACTION_FRIENDS;
1156 else if (strcasecmp(optarg, "user") == 0)
1157 session->action = ACTION_USER;
1158 else if (strcasecmp(optarg, "replies") == 0)
1159 session->action = ACTION_REPLIES;
1160 else if (strcasecmp(optarg, "public") == 0)
1161 session->action = ACTION_PUBLIC;
1162 else if (strcasecmp(optarg, "group") == 0)
1163 session->action = ACTION_GROUP;
1165 session->action = ACTION_UNKNOWN;
1166 dbg("action = %d\n", session->action);
1170 free(session->user);
1171 session->user = strdup(optarg);
1172 dbg("user = %s\n", session->user);
1177 free(session->group);
1178 session->group = strdup(optarg);
1179 dbg("group = %s\n", session->group);
1182 if (session->logfile)
1183 free(session->logfile);
1184 session->logfile = strdup(optarg);
1185 dbg("logfile = %s\n", session->logfile);
1188 session->shrink_urls = 1;
1191 if (session->hosturl)
1192 free(session->hosturl);
1193 if (session->hostname)
1194 free(session->hostname);
1195 if (strcasecmp(optarg, "twitter") == 0) {
1196 session->host = HOST_TWITTER;
1197 session->hosturl = strdup(twitter_host);
1198 session->hostname = strdup(twitter_name);
1199 } else if (strcasecmp(optarg, "identica") == 0) {
1200 session->host = HOST_IDENTICA;
1201 session->hosturl = strdup(identica_host);
1202 session->hostname = strdup(identica_name);
1204 session->host = HOST_CUSTOM;
1205 session->hosturl = strdup(optarg);
1206 session->hostname = strdup(optarg);
1208 dbg("host = %d\n", session->host);
1214 if (session->configfile)
1215 free(session->configfile);
1216 session->configfile = strdup(optarg);
1217 dbg("configfile = %s\n", session->configfile);
1220 * read the config file now. Yes, this could override previously
1221 * set options from the command line, but the user asked for it...
1223 parse_configfile(session);
1229 session->dry_run = 1;
1240 session_readline_init(session);
1242 * Show the version to make it easier to determine what
1248 if (session->action == ACTION_UNKNOWN) {
1249 fprintf(stderr, "Unknown action, valid actions are:\n");
1250 fprintf(stderr, "'update', 'friends', 'public', "
1251 "'replies', 'group' or 'user'.\n");
1255 if (session->host == HOST_TWITTER && session->action == ACTION_GROUP) {
1256 fprintf(stderr, "Groups only work in Identi.ca.\n");
1260 if (session->action == ACTION_GROUP && !session->group) {
1261 fprintf(stdout, "Enter group name: ");
1262 session->group = session->readline(NULL);
1265 if (!session->account) {
1266 fprintf(stdout, "Enter account for %s: ", session->hostname);
1267 session->account = session->readline(NULL);
1270 if (!session->password) {
1271 read_password(password, sizeof(password), session->hostname);
1272 session->password = strdup(password);
1275 if (session->action == ACTION_UPDATE) {
1276 if (session->bash || !session->interactive)
1277 tweet = get_string_from_stdin();
1279 tweet = session->readline("tweet: ");
1280 if (!tweet || strlen(tweet) == 0) {
1285 if (session->shrink_urls)
1286 tweet = shrink_urls(tweet);
1288 session->tweet = zalloc(strlen(tweet) + 10);
1290 sprintf(session->tweet, "%c %s",
1291 getuid() ? '$' : '#', tweet);
1293 sprintf(session->tweet, "%s", tweet);
1296 dbg("tweet = %s\n", session->tweet);
1300 session->user = strdup(session->account);
1302 if (session->page == 0)
1304 dbg("config file = %s\n", session->configfile);
1305 dbg("account = %s\n", session->account);
1306 dbg("password = %s\n", session->password);
1307 dbg("host = %d\n", session->host);
1308 dbg("action = %d\n", session->action);
1310 /* fork ourself so that the main shell can get on
1311 * with it's life as we try to connect and handle everything
1313 if (session->bash) {
1316 dbg("child is %d\n", child);
1321 retval = send_request(session);
1322 if (retval && !session->bash)
1323 fprintf(stderr, "operation failed\n");
1325 log_session(session, retval);
1327 session_readline_cleanup(session);
1328 session_free(session);