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__ , \
94 void *readline_handle;
95 char *(*readline)(const char *);
98 struct bti_curl_buffer {
104 static void display_help(void)
106 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n");
107 fprintf(stdout, "Version: " VERSION "\n");
108 fprintf(stdout, "Usage:\n");
109 fprintf(stdout, " bti [options]\n");
110 fprintf(stdout, "options are:\n");
111 fprintf(stdout, " --account accountname\n");
112 fprintf(stdout, " --password password\n");
113 fprintf(stdout, " --action action\n");
114 fprintf(stdout, " ('update', 'friends', 'public', 'replies', "
115 "'group' or 'user')\n");
116 fprintf(stdout, " --user screenname\n");
117 fprintf(stdout, " --group groupname\n");
118 fprintf(stdout, " --proxy PROXY:PORT\n");
119 fprintf(stdout, " --host HOST\n");
120 fprintf(stdout, " --logfile logfile\n");
121 fprintf(stdout, " --config configfile\n");
122 fprintf(stdout, " --replyto ID\n");
123 fprintf(stdout, " --shrink-urls\n");
124 fprintf(stdout, " --page PAGENUMBER\n");
125 fprintf(stdout, " --bash\n");
126 fprintf(stdout, " --debug\n");
127 fprintf(stdout, " --verbose\n");
128 fprintf(stdout, " --dry-run\n");
129 fprintf(stdout, " --version\n");
130 fprintf(stdout, " --help\n");
133 static void display_version(void)
135 fprintf(stdout, "bti - version %s\n", VERSION);
138 static char *get_string(const char *name)
143 string = zalloc(1000);
147 fprintf(stdout, "%s", name);
148 if (!fgets(string, 999, stdin))
150 temp = strchr(string, '\n');
157 * Try to get a handle to a readline function from a variety of different
158 * libraries. If nothing is present on the system, then fall back to an
161 * Logic originally based off of code in the e2fsutils package in the
162 * lib/ss/get_readline.c file, which is licensed under the MIT license.
164 * This keeps us from having to relicense the bti codebase if readline
165 * ever changes its license, as there is no link-time dependancy.
166 * It is a run-time thing only, and we handle any readline-like library
167 * in the same manner, making bti not be a derivative work of any
170 static void session_readline_init(struct session *session)
172 /* Libraries we will try to use for readline/editline functionality */
173 const char *libpath = "libreadline.so.6:libreadline.so.5:"
174 "libreadline.so.4:libreadline.so:libedit.so.2:"
175 "libedit.so:libeditline.so.0:libeditline.so";
177 char *tmp, *cp, *next;
178 int (*bind_key)(int, void *);
179 void (*insert)(void);
181 /* default to internal function if we can't or won't find anything */
182 session->readline = get_string;
185 session->interactive = 1;
187 tmp = malloc(strlen(libpath)+1);
190 strcpy(tmp, libpath);
191 for (cp = tmp; cp; cp = next) {
192 next = strchr(cp, ':');
197 handle = dlopen(cp, RTLD_NOW);
199 dbg("Using %s for readline library\n", cp);
205 dbg("No readline library found.\n");
209 session->readline_handle = handle;
210 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
211 if (session->readline == NULL) {
212 /* something odd happened, default back to internal stuff */
213 session->readline_handle = NULL;
214 session->readline = get_string;
219 * If we found a library, turn off filename expansion
220 * as that makes no sense from within bti.
222 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
223 insert = (void (*)(void))dlsym(handle, "rl_insert");
224 if (bind_key && insert)
225 bind_key('\t', insert);
228 static void session_readline_cleanup(struct session *session)
230 if (session->readline_handle)
231 dlclose(session->readline_handle);
234 static struct session *session_alloc(void)
236 struct session *session;
238 session = zalloc(sizeof(*session));
244 static void session_free(struct session *session)
248 free(session->replyto);
249 free(session->password);
250 free(session->account);
251 free(session->tweet);
252 free(session->proxy);
254 free(session->homedir);
256 free(session->group);
257 free(session->hosturl);
258 free(session->hostname);
259 free(session->configfile);
263 static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
265 struct bti_curl_buffer *buffer;
267 buffer = zalloc(sizeof(*buffer));
271 /* start out with a data buffer of 1 byte to
272 * make the buffer fill logic simpler */
273 buffer->data = zalloc(1);
279 buffer->action = action;
283 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
291 static const char *twitter_host = "https://twitter.com/statuses";
292 static const char *identica_host = "https://identi.ca/api/statuses";
293 static const char *twitter_name = "twitter";
294 static const char *identica_name = "identi.ca";
296 static const char *user_uri = "/user_timeline/";
297 static const char *update_uri = "/update.xml";
298 static const char *public_uri = "/public_timeline.xml";
299 static const char *friends_uri = "/friends_timeline.xml";
300 static const char *replies_uri = "/replies.xml";
301 static const char *group_uri = "/../laconica/groups/timeline/";
303 static CURL *curl_init(void)
307 curl = curl_easy_init();
309 fprintf(stderr, "Can not init CURL!\n");
312 /* some ssl sanity checks on the connection we are making */
313 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
314 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
318 static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
320 xmlChar *text = NULL;
321 xmlChar *user = NULL;
322 xmlChar *created = NULL;
326 current = current->xmlChildrenNode;
327 while (current != NULL) {
328 if (current->type == XML_ELEMENT_NODE) {
329 if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
330 created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
331 if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
332 text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
333 if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
334 id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
335 if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
336 userinfo = current->xmlChildrenNode;
337 while (userinfo != NULL) {
338 if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
341 user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
343 userinfo = userinfo->next;
347 if (user && text && created && id) {
349 printf("[%s] {%s} (%.16s) %s\n",
350 user, id, created, text);
364 current = current->next;
370 static void parse_timeline(char *document)
375 doc = xmlReadMemory(document, strlen(document), "timeline.xml",
376 NULL, XML_PARSE_NOERROR);
380 current = xmlDocGetRootElement(doc);
381 if (current == NULL) {
382 fprintf(stderr, "empty document\n");
387 if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
388 fprintf(stderr, "unexpected document type\n");
393 current = current->xmlChildrenNode;
394 while (current != NULL) {
395 if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
396 parse_statuses(doc, current);
397 current = current->next;
404 static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
407 struct bti_curl_buffer *curl_buf = userp;
408 size_t buffer_size = size * nmemb;
411 if ((!buffer) || (!buffer_size) || (!curl_buf))
414 /* add to the data we already have */
415 temp = zalloc(curl_buf->length + buffer_size + 1);
419 memcpy(temp, curl_buf->data, curl_buf->length);
420 free(curl_buf->data);
421 curl_buf->data = temp;
422 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
423 curl_buf->length += buffer_size;
424 if (curl_buf->action)
425 parse_timeline(curl_buf->data);
427 dbg("%s\n", curl_buf->data);
432 static int send_request(struct session *session)
435 char user_password[500];
437 struct bti_curl_buffer *curl_buf;
440 struct curl_httppost *formpost = NULL;
441 struct curl_httppost *lastptr = NULL;
442 struct curl_slist *slist = NULL;
447 curl_buf = bti_curl_buffer_alloc(session->action);
455 if (!session->hosturl)
456 session->hosturl = strdup(twitter_host);
458 switch (session->action) {
460 snprintf(user_password, sizeof(user_password), "%s:%s",
461 session->account, session->password);
462 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
463 curl_formadd(&formpost, &lastptr,
464 CURLFORM_COPYNAME, "status",
465 CURLFORM_COPYCONTENTS, session->tweet,
468 curl_formadd(&formpost, &lastptr,
469 CURLFORM_COPYNAME, "source",
470 CURLFORM_COPYCONTENTS, "bti",
473 if (session->replyto)
474 curl_formadd(&formpost, &lastptr,
475 CURLFORM_COPYNAME, "in_reply_to_status_id",
476 CURLFORM_COPYCONTENTS, session->replyto,
479 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
480 slist = curl_slist_append(slist, "Expect:");
481 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
483 sprintf(endpoint, "%s%s", session->hosturl, update_uri);
484 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
485 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
489 snprintf(user_password, sizeof(user_password), "%s:%s",
490 session->account, session->password);
491 sprintf(endpoint, "%s%s?page=%d", session->hosturl,
492 friends_uri, session->page);
493 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
494 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
498 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
499 user_uri, session->user, session->page);
500 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
504 snprintf(user_password, sizeof(user_password), "%s:%s",
505 session->account, session->password);
506 sprintf(endpoint, "%s%s?page=%d", session->hosturl, replies_uri,
508 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
509 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
513 sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri,
515 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
519 sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl,
520 group_uri, session->group, session->page);
521 curl_easy_setopt(curl, CURLOPT_URL, endpoint);
529 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
532 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
534 dbg("user_password = %s\n", user_password);
535 dbg("data = %s\n", data);
536 dbg("proxy = %s\n", session->proxy);
538 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
539 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
540 if (!session->dry_run) {
541 res = curl_easy_perform(curl);
542 if (res && !session->bash) {
543 fprintf(stderr, "error(%d) trying to perform "
549 curl_easy_cleanup(curl);
550 if (session->action == ACTION_UPDATE)
551 curl_formfree(formpost);
552 bti_curl_buffer_free(curl_buf);
556 static void parse_configfile(struct session *session)
561 char *account = NULL;
562 char *password = NULL;
565 char *logfile = NULL;
568 char *replyto = NULL;
572 config_file = fopen(session->configfile, "r");
574 /* No error if file does not exist or is unreadable. */
575 if (config_file == NULL)
579 ssize_t n = getline(&line, &len, config_file);
582 if (line[n - 1] == '\n')
584 /* Parse file. Format is the usual value pairs:
587 # is a comment character
589 *strchrnul(line, '#') = '\0';
593 /* Ignore blank lines. */
597 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
601 } else if (!strncasecmp(c, "password", 8) &&
605 password = strdup(c);
606 } else if (!strncasecmp(c, "host", 4) &&
611 } else if (!strncasecmp(c, "proxy", 5) &&
616 } else if (!strncasecmp(c, "logfile", 7) &&
621 } else if (!strncasecmp(c, "replyto", 7) &&
626 } else if (!strncasecmp(c, "action", 6) &&
631 } else if (!strncasecmp(c, "user", 4) &&
636 } else if (!strncasecmp(c, "shrink-urls", 11) &&
639 if (!strncasecmp(c, "true", 4) ||
640 !strncasecmp(c, "yes", 3))
642 } else if (!strncasecmp(c, "verbose", 7) &&
645 if (!strncasecmp(c, "true", 4) ||
646 !strncasecmp(c, "yes", 3))
649 } while (!feof(config_file));
652 session->password = password;
654 session->account = account;
656 if (strcasecmp(host, "twitter") == 0) {
657 session->host = HOST_TWITTER;
658 session->hosturl = strdup(twitter_host);
659 session->hostname = strdup(twitter_name);
660 } else if (strcasecmp(host, "identica") == 0) {
661 session->host = HOST_IDENTICA;
662 session->hosturl = strdup(identica_host);
663 session->hostname = strdup(identica_name);
665 session->host = HOST_CUSTOM;
666 session->hosturl = strdup(host);
667 session->hostname = strdup(host);
673 free(session->proxy);
674 session->proxy = proxy;
677 session->logfile = logfile;
679 session->replyto = replyto;
681 if (strcasecmp(action, "update") == 0)
682 session->action = ACTION_UPDATE;
683 else if (strcasecmp(action, "friends") == 0)
684 session->action = ACTION_FRIENDS;
685 else if (strcasecmp(action, "user") == 0)
686 session->action = ACTION_USER;
687 else if (strcasecmp(action, "replies") == 0)
688 session->action = ACTION_REPLIES;
689 else if (strcasecmp(action, "public") == 0)
690 session->action = ACTION_PUBLIC;
691 else if (strcasecmp(action, "group") == 0)
692 session->action = ACTION_GROUP;
694 session->action = ACTION_UNKNOWN;
698 session->user = user;
699 session->shrink_urls = shrink_urls;
701 /* Free buffer and close file. */
706 static void log_session(struct session *session, int retval)
711 /* Only log something if we have a log file set */
712 if (!session->logfile)
715 filename = alloca(strlen(session->homedir) +
716 strlen(session->logfile) + 3);
718 sprintf(filename, "%s/%s", session->homedir, session->logfile);
720 log_file = fopen(filename, "a+");
721 if (log_file == NULL)
724 switch (session->action) {
727 fprintf(log_file, "%s: host=%s tweet failed\n",
728 session->time, session->hostname);
730 fprintf(log_file, "%s: host=%s tweet=%s\n",
731 session->time, session->hostname,
735 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
736 session->time, session->hostname);
739 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
740 session->time, session->hostname, session->user);
743 fprintf(log_file, "%s: host=%s retrieving replies\n",
744 session->time, session->hostname);
747 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
748 session->time, session->hostname);
751 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
752 session->time, session->hostname);
761 static char *get_string_from_stdin(void)
766 string = zalloc(1000);
770 if (!fgets(string, 999, stdin))
772 temp = strchr(string, '\n');
778 static void read_password(char *buf, size_t len, char *host)
788 tp.c_lflag &= (~ECHO);
789 tcsetattr(0, TCSANOW, &tp);
791 fprintf(stdout, "Enter password for %s: ", host);
794 retval = scanf("%79s", pwd);
796 fprintf(stdout, "\n");
798 tcsetattr(0, TCSANOW, &old);
800 strncpy(buf, pwd, len);
804 static int find_urls(const char *tweet, int **pranges)
807 * magic obtained from
808 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
810 static const char *re_magic =
811 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
812 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
813 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
817 int ovector[10] = {0,};
818 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
819 int startoffset, tweetlen;
823 int *ranges = malloc(sizeof(int) * rbound);
825 re = pcre_compile(re_magic,
826 PCRE_NO_AUTO_CAPTURE,
827 &errptr, &erroffset, NULL);
829 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
833 tweetlen = strlen(tweet);
834 for (startoffset = 0; startoffset < tweetlen; ) {
836 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
838 if (rc == PCRE_ERROR_NOMATCH)
842 fprintf(stderr, "pcre_exec @%u: %s\n",
847 for (i = 0; i < rc; i += 2) {
848 if ((rcount+2) == rbound) {
850 ranges = realloc(ranges, sizeof(int) * rbound);
853 ranges[rcount++] = ovector[i];
854 ranges[rcount++] = ovector[i+1];
857 startoffset = ovector[1];
867 * bidirectional popen() call
869 * @param rwepipe - int array of size three
870 * @param exe - program to run
871 * @param argv - argument list
872 * @return pid or -1 on error
874 * The caller passes in an array of three integers (rwepipe), on successful
875 * execution it can then write to element 0 (stdin of exe), and read from
876 * element 1 (stdout) and 2 (stderr).
878 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
908 } else if (pid == 0) {
920 execvp(exe, (char **)argv);
940 static int pcloseRWE(int pid, int *rwepipe)
946 rc = waitpid(pid, &status, 0);
950 static char *shrink_one_url(int *rwepipe, char *big)
952 int biglen = strlen(big);
957 rc = dprintf(rwepipe[0], "%s\n", big);
961 smalllen = biglen + 128;
962 small = malloc(smalllen);
966 rc = read(rwepipe[1], small, smalllen);
967 if (rc < 0 || rc > biglen)
968 goto error_free_small;
970 if (strncmp(small, "http://", 7))
971 goto error_free_small;
974 while (smalllen && isspace(small[smalllen-1]))
975 small[--smalllen] = 0;
985 static char *shrink_urls(char *text)
992 const char *const shrink_args[] = {
998 int inlen = strlen(text);
1000 dbg("before len=%u\n", inlen);
1002 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
1006 rcount = find_urls(text, &ranges);
1010 for (i = 0; i < rcount; i += 2) {
1011 int url_start = ranges[i];
1012 int url_end = ranges[i+1];
1013 int long_url_len = url_end - url_start;
1014 char *url = strndup(text + url_start, long_url_len);
1016 int not_url_len = url_start - inofs;
1018 dbg("long url[%u]: %s\n", long_url_len, url);
1019 url = shrink_one_url(shrink_pipe, url);
1020 short_url_len = url ? strlen(url) : 0;
1021 dbg("short url[%u]: %s\n", short_url_len, url);
1023 if (!url || short_url_len >= long_url_len) {
1024 /* The short url ended up being too long
1027 strncpy(text + outofs, text + inofs,
1028 not_url_len + long_url_len);
1030 inofs += not_url_len + long_url_len;
1031 outofs += not_url_len + long_url_len;
1034 /* copy the unmodified block */
1035 strncpy(text + outofs, text + inofs, not_url_len);
1036 inofs += not_url_len;
1037 outofs += not_url_len;
1039 /* copy the new url */
1040 strncpy(text + outofs, url, short_url_len);
1041 inofs += long_url_len;
1042 outofs += short_url_len;
1048 /* copy the last block after the last match */
1050 int tail = inlen - inofs;
1052 strncpy(text + outofs, text + inofs, tail);
1059 (void)pcloseRWE(shrink_pid, shrink_pipe);
1062 dbg("after len=%u\n", outofs);
1066 int main(int argc, char *argv[], char *envp[])
1068 static const struct option options[] = {
1069 { "debug", 0, NULL, 'd' },
1070 { "verbose", 0, NULL, 'V' },
1071 { "account", 1, NULL, 'a' },
1072 { "password", 1, NULL, 'p' },
1073 { "host", 1, NULL, 'H' },
1074 { "proxy", 1, NULL, 'P' },
1075 { "action", 1, NULL, 'A' },
1076 { "user", 1, NULL, 'u' },
1077 { "group", 1, NULL, 'G' },
1078 { "logfile", 1, NULL, 'L' },
1079 { "shrink-urls", 0, NULL, 's' },
1080 { "help", 0, NULL, 'h' },
1081 { "bash", 0, NULL, 'b' },
1082 { "dry-run", 0, NULL, 'n' },
1083 { "page", 1, NULL, 'g' },
1084 { "version", 0, NULL, 'v' },
1085 { "config", 1, NULL, 'c' },
1086 { "replyto", 1, NULL, 'r' },
1089 struct session *session;
1092 static char password[80];
1102 session = session_alloc();
1104 fprintf(stderr, "no more memory...\n");
1108 /* get the current time so that we can log it later */
1110 session->time = strdup(ctime(&t));
1111 session->time[strlen(session->time)-1] = 0x00;
1113 /* Get the home directory so we can try to find a config file */
1114 session->homedir = strdup(getenv("HOME"));
1116 /* set up a default config file location (traditionally ~/.bti) */
1117 session->configfile = zalloc(strlen(session->homedir) + 7);
1118 sprintf(session->configfile, "%s/.bti", session->homedir);
1120 curl_global_init(CURL_GLOBAL_ALL);
1122 /* Set environment variables first, before reading command line options
1123 * or config file values. */
1124 http_proxy = getenv("http_proxy");
1127 free(session->proxy);
1128 session->proxy = strdup(http_proxy);
1129 dbg("http_proxy = %s\n", session->proxy);
1132 parse_configfile(session);
1135 option = getopt_long_only(argc, argv, "dp:P:H:a:A:u:c:hg:G:sr:nVv",
1147 if (session->account)
1148 free(session->account);
1149 session->account = strdup(optarg);
1150 dbg("account = %s\n", session->account);
1153 page_nr = atoi(optarg);
1154 dbg("page = %d\n", page_nr);
1155 session->page = page_nr;
1158 session->replyto = strdup(optarg);
1159 dbg("in_reply_to_status_id = %s\n", session->replyto);
1162 if (session->password)
1163 free(session->password);
1164 session->password = strdup(optarg);
1165 dbg("password = %s\n", session->password);
1169 free(session->proxy);
1170 session->proxy = strdup(optarg);
1171 dbg("proxy = %s\n", session->proxy);
1174 if (strcasecmp(optarg, "update") == 0)
1175 session->action = ACTION_UPDATE;
1176 else if (strcasecmp(optarg, "friends") == 0)
1177 session->action = ACTION_FRIENDS;
1178 else if (strcasecmp(optarg, "user") == 0)
1179 session->action = ACTION_USER;
1180 else if (strcasecmp(optarg, "replies") == 0)
1181 session->action = ACTION_REPLIES;
1182 else if (strcasecmp(optarg, "public") == 0)
1183 session->action = ACTION_PUBLIC;
1184 else if (strcasecmp(optarg, "group") == 0)
1185 session->action = ACTION_GROUP;
1187 session->action = ACTION_UNKNOWN;
1188 dbg("action = %d\n", session->action);
1192 free(session->user);
1193 session->user = strdup(optarg);
1194 dbg("user = %s\n", session->user);
1199 free(session->group);
1200 session->group = strdup(optarg);
1201 dbg("group = %s\n", session->group);
1204 if (session->logfile)
1205 free(session->logfile);
1206 session->logfile = strdup(optarg);
1207 dbg("logfile = %s\n", session->logfile);
1210 session->shrink_urls = 1;
1213 if (session->hosturl)
1214 free(session->hosturl);
1215 if (session->hostname)
1216 free(session->hostname);
1217 if (strcasecmp(optarg, "twitter") == 0) {
1218 session->host = HOST_TWITTER;
1219 session->hosturl = strdup(twitter_host);
1220 session->hostname = strdup(twitter_name);
1221 } else if (strcasecmp(optarg, "identica") == 0) {
1222 session->host = HOST_IDENTICA;
1223 session->hosturl = strdup(identica_host);
1224 session->hostname = strdup(identica_name);
1226 session->host = HOST_CUSTOM;
1227 session->hosturl = strdup(optarg);
1228 session->hostname = strdup(optarg);
1230 dbg("host = %d\n", session->host);
1236 if (session->configfile)
1237 free(session->configfile);
1238 session->configfile = strdup(optarg);
1239 dbg("configfile = %s\n", session->configfile);
1242 * read the config file now. Yes, this could override previously
1243 * set options from the command line, but the user asked for it...
1245 parse_configfile(session);
1251 session->dry_run = 1;
1262 session_readline_init(session);
1264 * Show the version to make it easier to determine what
1270 if (session->action == ACTION_UNKNOWN) {
1271 fprintf(stderr, "Unknown action, valid actions are:\n");
1272 fprintf(stderr, "'update', 'friends', 'public', "
1273 "'replies', 'group' or 'user'.\n");
1277 if (session->host == HOST_TWITTER && session->action == ACTION_GROUP) {
1278 fprintf(stderr, "Groups only work in Identi.ca.\n");
1282 if (session->action == ACTION_GROUP && !session->group) {
1283 fprintf(stdout, "Enter group name: ");
1284 session->group = session->readline(NULL);
1287 if (!session->account) {
1288 fprintf(stdout, "Enter account for %s: ", session->hostname);
1289 session->account = session->readline(NULL);
1292 if (!session->password) {
1293 read_password(password, sizeof(password), session->hostname);
1294 session->password = strdup(password);
1297 if (session->action == ACTION_UPDATE) {
1298 if (session->bash || !session->interactive)
1299 tweet = get_string_from_stdin();
1301 tweet = session->readline("tweet: ");
1302 if (!tweet || strlen(tweet) == 0) {
1307 if (session->shrink_urls)
1308 tweet = shrink_urls(tweet);
1310 session->tweet = zalloc(strlen(tweet) + 10);
1312 sprintf(session->tweet, "%c %s",
1313 getuid() ? '$' : '#', tweet);
1315 sprintf(session->tweet, "%s", tweet);
1318 dbg("tweet = %s\n", session->tweet);
1322 session->user = strdup(session->account);
1324 if (session->page == 0)
1326 dbg("config file = %s\n", session->configfile);
1327 dbg("account = %s\n", session->account);
1328 dbg("password = %s\n", session->password);
1329 dbg("host = %d\n", session->host);
1330 dbg("action = %d\n", session->action);
1332 /* fork ourself so that the main shell can get on
1333 * with it's life as we try to connect and handle everything
1335 if (session->bash) {
1338 dbg("child is %d\n", child);
1343 retval = send_request(session);
1344 if (retval && !session->bash)
1345 fprintf(stderr, "operation failed\n");
1347 log_session(session, retval);
1349 session_readline_cleanup(session);
1350 session_free(session);