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;
571 config_file = fopen(session->configfile, "r");
573 /* No error if file does not exist or is unreadable. */
574 if (config_file == NULL)
578 ssize_t n = getline(&line, &len, config_file);
581 if (line[n - 1] == '\n')
583 /* Parse file. Format is the usual value pairs:
586 # is a comment character
588 *strchrnul(line, '#') = '\0';
592 /* Ignore blank lines. */
596 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
600 } else if (!strncasecmp(c, "password", 8) &&
604 password = strdup(c);
605 } else if (!strncasecmp(c, "host", 4) &&
610 } else if (!strncasecmp(c, "proxy", 5) &&
615 } else if (!strncasecmp(c, "logfile", 7) &&
620 } else if (!strncasecmp(c, "action", 6) &&
625 } else if (!strncasecmp(c, "user", 4) &&
630 } else if (!strncasecmp(c, "shrink-urls", 11) &&
633 if (!strncasecmp(c, "true", 4) ||
634 !strncasecmp(c, "yes", 3))
636 } else if (!strncasecmp(c, "verbose", 7) &&
639 if (!strncasecmp(c, "true", 4) ||
640 !strncasecmp(c, "yes", 3))
643 } while (!feof(config_file));
646 session->password = password;
648 session->account = account;
650 if (strcasecmp(host, "twitter") == 0) {
651 session->host = HOST_TWITTER;
652 session->hosturl = strdup(twitter_host);
653 session->hostname = strdup(twitter_name);
654 } else if (strcasecmp(host, "identica") == 0) {
655 session->host = HOST_IDENTICA;
656 session->hosturl = strdup(identica_host);
657 session->hostname = strdup(identica_name);
659 session->host = HOST_CUSTOM;
660 session->hosturl = strdup(host);
661 session->hostname = strdup(host);
667 free(session->proxy);
668 session->proxy = proxy;
671 session->logfile = logfile;
673 if (strcasecmp(action, "update") == 0)
674 session->action = ACTION_UPDATE;
675 else if (strcasecmp(action, "friends") == 0)
676 session->action = ACTION_FRIENDS;
677 else if (strcasecmp(action, "user") == 0)
678 session->action = ACTION_USER;
679 else if (strcasecmp(action, "replies") == 0)
680 session->action = ACTION_REPLIES;
681 else if (strcasecmp(action, "public") == 0)
682 session->action = ACTION_PUBLIC;
683 else if (strcasecmp(action, "group") == 0)
684 session->action = ACTION_GROUP;
686 session->action = ACTION_UNKNOWN;
690 session->user = user;
691 session->shrink_urls = shrink_urls;
693 /* Free buffer and close file. */
698 static void log_session(struct session *session, int retval)
703 /* Only log something if we have a log file set */
704 if (!session->logfile)
707 filename = alloca(strlen(session->homedir) +
708 strlen(session->logfile) + 3);
710 sprintf(filename, "%s/%s", session->homedir, session->logfile);
712 log_file = fopen(filename, "a+");
713 if (log_file == NULL)
716 switch (session->action) {
719 fprintf(log_file, "%s: host=%s tweet failed\n",
720 session->time, session->hostname);
722 fprintf(log_file, "%s: host=%s tweet=%s\n",
723 session->time, session->hostname,
727 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
728 session->time, session->hostname);
731 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
732 session->time, session->hostname, session->user);
735 fprintf(log_file, "%s: host=%s retrieving replies\n",
736 session->time, session->hostname);
739 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
740 session->time, session->hostname);
743 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
744 session->time, session->hostname);
753 static char *get_string_from_stdin(void)
758 string = zalloc(1000);
762 if (!fgets(string, 999, stdin))
764 temp = strchr(string, '\n');
770 static void read_password(char *buf, size_t len, char *host)
780 tp.c_lflag &= (~ECHO);
781 tcsetattr(0, TCSANOW, &tp);
783 fprintf(stdout, "Enter password for %s: ", host);
786 retval = scanf("%79s", pwd);
788 fprintf(stdout, "\n");
790 tcsetattr(0, TCSANOW, &old);
792 strncpy(buf, pwd, len);
796 static int find_urls(const char *tweet, int **pranges)
799 * magic obtained from
800 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
802 static const char *re_magic =
803 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
804 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
805 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
809 int ovector[10] = {0,};
810 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
811 int startoffset, tweetlen;
815 int *ranges = malloc(sizeof(int) * rbound);
817 re = pcre_compile(re_magic,
818 PCRE_NO_AUTO_CAPTURE,
819 &errptr, &erroffset, NULL);
821 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
825 tweetlen = strlen(tweet);
826 for (startoffset = 0; startoffset < tweetlen; ) {
828 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
830 if (rc == PCRE_ERROR_NOMATCH)
834 fprintf(stderr, "pcre_exec @%u: %s\n",
839 for (i = 0; i < rc; i += 2) {
840 if ((rcount+2) == rbound) {
842 ranges = realloc(ranges, sizeof(int) * rbound);
845 ranges[rcount++] = ovector[i];
846 ranges[rcount++] = ovector[i+1];
849 startoffset = ovector[1];
859 * bidirectional popen() call
861 * @param rwepipe - int array of size three
862 * @param exe - program to run
863 * @param argv - argument list
864 * @return pid or -1 on error
866 * The caller passes in an array of three integers (rwepipe), on successful
867 * execution it can then write to element 0 (stdin of exe), and read from
868 * element 1 (stdout) and 2 (stderr).
870 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
900 } else if (pid == 0) {
912 execvp(exe, (char **)argv);
932 static int pcloseRWE(int pid, int *rwepipe)
938 rc = waitpid(pid, &status, 0);
942 static char *shrink_one_url(int *rwepipe, char *big)
944 int biglen = strlen(big);
949 rc = dprintf(rwepipe[0], "%s\n", big);
953 smalllen = biglen + 128;
954 small = malloc(smalllen);
958 rc = read(rwepipe[1], small, smalllen);
959 if (rc < 0 || rc > biglen)
960 goto error_free_small;
962 if (strncmp(small, "http://", 7))
963 goto error_free_small;
966 while (smalllen && isspace(small[smalllen-1]))
967 small[--smalllen] = 0;
977 static char *shrink_urls(char *text)
984 const char *const shrink_args[] = {
990 int inlen = strlen(text);
992 dbg("before len=%u\n", inlen);
994 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
998 rcount = find_urls(text, &ranges);
1002 for (i = 0; i < rcount; i += 2) {
1003 int url_start = ranges[i];
1004 int url_end = ranges[i+1];
1005 int long_url_len = url_end - url_start;
1006 char *url = strndup(text + url_start, long_url_len);
1008 int not_url_len = url_start - inofs;
1010 dbg("long url[%u]: %s\n", long_url_len, url);
1011 url = shrink_one_url(shrink_pipe, url);
1012 short_url_len = url ? strlen(url) : 0;
1013 dbg("short url[%u]: %s\n", short_url_len, url);
1015 if (!url || short_url_len >= long_url_len) {
1016 /* The short url ended up being too long
1019 strncpy(text + outofs, text + inofs,
1020 not_url_len + long_url_len);
1022 inofs += not_url_len + long_url_len;
1023 outofs += not_url_len + long_url_len;
1026 /* copy the unmodified block */
1027 strncpy(text + outofs, text + inofs, not_url_len);
1028 inofs += not_url_len;
1029 outofs += not_url_len;
1031 /* copy the new url */
1032 strncpy(text + outofs, url, short_url_len);
1033 inofs += long_url_len;
1034 outofs += short_url_len;
1040 /* copy the last block after the last match */
1042 int tail = inlen - inofs;
1044 strncpy(text + outofs, text + inofs, tail);
1051 (void)pcloseRWE(shrink_pid, shrink_pipe);
1054 dbg("after len=%u\n", outofs);
1058 int main(int argc, char *argv[], char *envp[])
1060 static const struct option options[] = {
1061 { "debug", 0, NULL, 'd' },
1062 { "verbose", 0, NULL, 'V' },
1063 { "account", 1, NULL, 'a' },
1064 { "password", 1, NULL, 'p' },
1065 { "host", 1, NULL, 'H' },
1066 { "proxy", 1, NULL, 'P' },
1067 { "action", 1, NULL, 'A' },
1068 { "user", 1, NULL, 'u' },
1069 { "group", 1, NULL, 'G' },
1070 { "logfile", 1, NULL, 'L' },
1071 { "shrink-urls", 0, NULL, 's' },
1072 { "help", 0, NULL, 'h' },
1073 { "bash", 0, NULL, 'b' },
1074 { "dry-run", 0, NULL, 'n' },
1075 { "page", 1, NULL, 'g' },
1076 { "version", 0, NULL, 'v' },
1077 { "config", 1, NULL, 'c' },
1078 { "replyto", 1, NULL, 'r' },
1081 struct session *session;
1084 static char password[80];
1094 session = session_alloc();
1096 fprintf(stderr, "no more memory...\n");
1100 /* get the current time so that we can log it later */
1102 session->time = strdup(ctime(&t));
1103 session->time[strlen(session->time)-1] = 0x00;
1105 /* Get the home directory so we can try to find a config file */
1106 session->homedir = strdup(getenv("HOME"));
1108 /* set up a default config file location (traditionally ~/.bti) */
1109 session->configfile = zalloc(strlen(session->homedir) + 7);
1110 sprintf(session->configfile, "%s/.bti", session->homedir);
1112 curl_global_init(CURL_GLOBAL_ALL);
1114 /* Set environment variables first, before reading command line options
1115 * or config file values. */
1116 http_proxy = getenv("http_proxy");
1119 free(session->proxy);
1120 session->proxy = strdup(http_proxy);
1121 dbg("http_proxy = %s\n", session->proxy);
1124 parse_configfile(session);
1127 option = getopt_long_only(argc, argv, "dp:P:H:a:A:u:c:hg:G:sr:nVv",
1139 if (session->account)
1140 free(session->account);
1141 session->account = strdup(optarg);
1142 dbg("account = %s\n", session->account);
1145 page_nr = atoi(optarg);
1146 dbg("page = %d\n", page_nr);
1147 session->page = page_nr;
1150 session->replyto = strdup(optarg);
1151 dbg("in_reply_to_status_id = %s\n", session->replyto);
1154 if (session->password)
1155 free(session->password);
1156 session->password = strdup(optarg);
1157 dbg("password = %s\n", session->password);
1161 free(session->proxy);
1162 session->proxy = strdup(optarg);
1163 dbg("proxy = %s\n", session->proxy);
1166 if (strcasecmp(optarg, "update") == 0)
1167 session->action = ACTION_UPDATE;
1168 else if (strcasecmp(optarg, "friends") == 0)
1169 session->action = ACTION_FRIENDS;
1170 else if (strcasecmp(optarg, "user") == 0)
1171 session->action = ACTION_USER;
1172 else if (strcasecmp(optarg, "replies") == 0)
1173 session->action = ACTION_REPLIES;
1174 else if (strcasecmp(optarg, "public") == 0)
1175 session->action = ACTION_PUBLIC;
1176 else if (strcasecmp(optarg, "group") == 0)
1177 session->action = ACTION_GROUP;
1179 session->action = ACTION_UNKNOWN;
1180 dbg("action = %d\n", session->action);
1184 free(session->user);
1185 session->user = strdup(optarg);
1186 dbg("user = %s\n", session->user);
1191 free(session->group);
1192 session->group = strdup(optarg);
1193 dbg("group = %s\n", session->group);
1196 if (session->logfile)
1197 free(session->logfile);
1198 session->logfile = strdup(optarg);
1199 dbg("logfile = %s\n", session->logfile);
1202 session->shrink_urls = 1;
1205 if (session->hosturl)
1206 free(session->hosturl);
1207 if (session->hostname)
1208 free(session->hostname);
1209 if (strcasecmp(optarg, "twitter") == 0) {
1210 session->host = HOST_TWITTER;
1211 session->hosturl = strdup(twitter_host);
1212 session->hostname = strdup(twitter_name);
1213 } else if (strcasecmp(optarg, "identica") == 0) {
1214 session->host = HOST_IDENTICA;
1215 session->hosturl = strdup(identica_host);
1216 session->hostname = strdup(identica_name);
1218 session->host = HOST_CUSTOM;
1219 session->hosturl = strdup(optarg);
1220 session->hostname = strdup(optarg);
1222 dbg("host = %d\n", session->host);
1228 if (session->configfile)
1229 free(session->configfile);
1230 session->configfile = strdup(optarg);
1231 dbg("configfile = %s\n", session->configfile);
1234 * read the config file now. Yes, this could override previously
1235 * set options from the command line, but the user asked for it...
1237 parse_configfile(session);
1243 session->dry_run = 1;
1254 session_readline_init(session);
1256 * Show the version to make it easier to determine what
1262 if (session->action == ACTION_UNKNOWN) {
1263 fprintf(stderr, "Unknown action, valid actions are:\n");
1264 fprintf(stderr, "'update', 'friends', 'public', "
1265 "'replies', 'group' or 'user'.\n");
1269 if (session->host == HOST_TWITTER && session->action == ACTION_GROUP) {
1270 fprintf(stderr, "Groups only work in Identi.ca.\n");
1274 if (session->action == ACTION_GROUP && !session->group) {
1275 fprintf(stdout, "Enter group name: ");
1276 session->group = session->readline(NULL);
1279 if (!session->account) {
1280 fprintf(stdout, "Enter account for %s: ", session->hostname);
1281 session->account = session->readline(NULL);
1284 if (!session->password) {
1285 read_password(password, sizeof(password), session->hostname);
1286 session->password = strdup(password);
1289 if (session->action == ACTION_UPDATE) {
1290 if (session->bash || !session->interactive)
1291 tweet = get_string_from_stdin();
1293 tweet = session->readline("tweet: ");
1294 if (!tweet || strlen(tweet) == 0) {
1299 if (session->shrink_urls)
1300 tweet = shrink_urls(tweet);
1302 session->tweet = zalloc(strlen(tweet) + 10);
1304 sprintf(session->tweet, "%c %s",
1305 getuid() ? '$' : '#', tweet);
1307 sprintf(session->tweet, "%s", tweet);
1310 dbg("tweet = %s\n", session->tweet);
1314 session->user = strdup(session->account);
1316 if (session->page == 0)
1318 dbg("config file = %s\n", session->configfile);
1319 dbg("account = %s\n", session->account);
1320 dbg("password = %s\n", session->password);
1321 dbg("host = %d\n", session->host);
1322 dbg("action = %d\n", session->action);
1324 /* fork ourself so that the main shell can get on
1325 * with it's life as we try to connect and handle everything
1327 if (session->bash) {
1330 dbg("child is %d\n", child);
1335 retval = send_request(session);
1336 if (retval && !session->bash)
1337 fprintf(stderr, "operation failed\n");
1339 log_session(session, retval);
1341 session_readline_cleanup(session);
1342 session_free(session);