2 * Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
4 * Copyright (C) 2009 Amir Mohammad Saied <amirsaied@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <sys/types.h>
35 #include <curl/curl.h>
36 #include <libxml/xmlmemory.h>
37 #include <libxml/parser.h>
38 #include <libxml/tree.h>
44 #define zalloc(size) calloc(size, 1)
46 #define dbg(format, arg...) \
49 fprintf(stdout, "bti: %s: " format , __func__ , \
92 void *readline_handle;
93 char *(*readline)(const char *);
96 struct bti_curl_buffer {
102 static void display_help(void)
104 fprintf(stdout, "bti - send tweet to twitter or identi.ca\n");
105 fprintf(stdout, "Version: " VERSION "\n");
106 fprintf(stdout, "Usage:\n");
107 fprintf(stdout, " bti [options]\n");
108 fprintf(stdout, "options are:\n");
109 fprintf(stdout, " --account accountname\n");
110 fprintf(stdout, " --password password\n");
111 fprintf(stdout, " --action action\n");
112 fprintf(stdout, " ('update', 'friends', 'public', 'replies', "
113 "'group' or 'user')\n");
114 fprintf(stdout, " --user screenname\n");
115 fprintf(stdout, " --group groupname\n");
116 fprintf(stdout, " --proxy PROXY:PORT\n");
117 fprintf(stdout, " --host HOST\n");
118 fprintf(stdout, " --logfile logfile\n");
119 fprintf(stdout, " --shrink-urls\n");
120 fprintf(stdout, " --page PAGENUMBER\n");
121 fprintf(stdout, " --bash\n");
122 fprintf(stdout, " --debug\n");
123 fprintf(stdout, " --verbose\n");
124 fprintf(stdout, " --dry-run\n");
125 fprintf(stdout, " --version\n");
126 fprintf(stdout, " --help\n");
129 static void display_version(void)
131 fprintf(stdout, "bti - version %s\n", VERSION);
134 static char *get_string(const char *name)
139 string = zalloc(1000);
143 fprintf(stdout, "%s", name);
144 if (!fgets(string, 999, stdin))
146 temp = strchr(string, '\n');
153 * Try to get a handle to a readline function from a variety of different
154 * libraries. If nothing is present on the system, then fall back to an
157 * Logic originally based off of code in the e2fsutils package in the
158 * lib/ss/get_readline.c file, which is licensed under the MIT license.
160 * This keeps us from having to relicense the bti codebase if readline
161 * ever changes its license, as there is no link-time dependancy.
162 * It is a run-time thing only, and we handle any readline-like library
163 * in the same manner, making bti not be a derivative work of any
166 static void session_readline_init(struct session *session)
168 /* Libraries we will try to use for readline/editline functionality */
169 const char *libpath = "libreadline.so.6:libreadline.so.5:"
170 "libreadline.so.4:libreadline.so:libedit.so.2:"
171 "libedit.so:libeditline.so.0:libeditline.so";
173 char *tmp, *cp, *next;
174 int (*bind_key)(int, void *);
175 void (*insert)(void);
177 /* default to internal function if we can't or won't find anything */
178 session->readline = get_string;
181 session->interactive = 1;
183 tmp = malloc(strlen(libpath)+1);
186 strcpy(tmp, libpath);
187 for (cp = tmp; cp; cp = next) {
188 next = strchr(cp, ':');
193 handle = dlopen(cp, RTLD_NOW);
195 dbg("Using %s for readline library\n", cp);
201 dbg("No readline library found.\n");
205 session->readline_handle = handle;
206 session->readline = (char *(*)(const char *))dlsym(handle, "readline");
207 if (session->readline == NULL) {
208 /* something odd happened, default back to internal stuff */
209 session->readline_handle = NULL;
210 session->readline = get_string;
215 * If we found a library, turn off filename expansion
216 * as that makes no sense from within bti.
218 bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
219 insert = (void (*)(void))dlsym(handle, "rl_insert");
220 if (bind_key && insert)
221 bind_key('\t', insert);
224 static void session_readline_cleanup(struct session *session)
226 if (session->readline_handle)
227 dlclose(session->readline_handle);
230 static struct session *session_alloc(void)
232 struct session *session;
234 session = zalloc(sizeof(*session));
237 session_readline_init(session);
241 static void session_free(struct session *session)
245 session_readline_cleanup(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);
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 is ~/.bti */
557 file = alloca(strlen(session->homedir) + 7);
559 sprintf(file, "%s/.bti", session->homedir);
561 config_file = fopen(file, "r");
563 /* No error if file does not exist or is unreadable. */
564 if (config_file == NULL)
568 ssize_t n = getline(&line, &len, config_file);
571 if (line[n - 1] == '\n')
573 /* Parse file. Format is the usual value pairs:
576 # is a comment character
578 *strchrnul(line, '#') = '\0';
582 /* Ignore blank lines. */
586 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
590 } else if (!strncasecmp(c, "password", 8) &&
594 password = strdup(c);
595 } else if (!strncasecmp(c, "host", 4) &&
600 } else if (!strncasecmp(c, "proxy", 5) &&
605 } else if (!strncasecmp(c, "logfile", 7) &&
610 } else if (!strncasecmp(c, "action", 6) &&
615 } else if (!strncasecmp(c, "user", 4) &&
620 } else if (!strncasecmp(c, "shrink-urls", 11) &&
623 if (!strncasecmp(c, "true", 4) ||
624 !strncasecmp(c, "yes", 3))
626 } else if (!strncasecmp(c, "verbose", 7) &&
629 if (!strncasecmp(c, "true", 4) ||
630 !strncasecmp(c, "yes", 3))
633 } while (!feof(config_file));
636 session->password = password;
638 session->account = account;
640 if (strcasecmp(host, "twitter") == 0) {
641 session->host = HOST_TWITTER;
642 session->hosturl = strdup(twitter_host);
643 session->hostname = strdup(twitter_name);
644 } else if (strcasecmp(host, "identica") == 0) {
645 session->host = HOST_IDENTICA;
646 session->hosturl = strdup(identica_host);
647 session->hostname = strdup(identica_name);
649 session->host = HOST_CUSTOM;
650 session->hosturl = strdup(host);
651 session->hostname = strdup(host);
657 free(session->proxy);
658 session->proxy = proxy;
661 session->logfile = logfile;
663 if (strcasecmp(action, "update") == 0)
664 session->action = ACTION_UPDATE;
665 else if (strcasecmp(action, "friends") == 0)
666 session->action = ACTION_FRIENDS;
667 else if (strcasecmp(action, "user") == 0)
668 session->action = ACTION_USER;
669 else if (strcasecmp(action, "replies") == 0)
670 session->action = ACTION_REPLIES;
671 else if (strcasecmp(action, "public") == 0)
672 session->action = ACTION_PUBLIC;
673 else if (strcasecmp(action, "group") == 0)
674 session->action = ACTION_GROUP;
676 session->action = ACTION_UNKNOWN;
680 session->user = user;
681 session->shrink_urls = shrink_urls;
683 /* Free buffer and close file. */
688 static void log_session(struct session *session, int retval)
693 /* Only log something if we have a log file set */
694 if (!session->logfile)
697 filename = alloca(strlen(session->homedir) +
698 strlen(session->logfile) + 3);
700 sprintf(filename, "%s/%s", session->homedir, session->logfile);
702 log_file = fopen(filename, "a+");
703 if (log_file == NULL)
706 switch (session->action) {
709 fprintf(log_file, "%s: host=%s tweet failed\n",
710 session->time, session->hostname);
712 fprintf(log_file, "%s: host=%s tweet=%s\n",
713 session->time, session->hostname,
717 fprintf(log_file, "%s: host=%s retrieving friends timeline\n",
718 session->time, session->hostname);
721 fprintf(log_file, "%s: host=%s retrieving %s's timeline\n",
722 session->time, session->hostname, session->user);
725 fprintf(log_file, "%s: host=%s retrieving replies\n",
726 session->time, session->hostname);
729 fprintf(log_file, "%s: host=%s retrieving public timeline\n",
730 session->time, session->hostname);
733 fprintf(log_file, "%s: host=%s retrieving group timeline\n",
734 session->time, session->hostname);
743 static char *get_string_from_stdin(void)
748 string = zalloc(1000);
752 if (!fgets(string, 999, stdin))
754 temp = strchr(string, '\n');
760 static void read_password(char *buf, size_t len, char *host)
770 tp.c_lflag &= (~ECHO);
771 tcsetattr(0, TCSANOW, &tp);
773 fprintf(stdout, "Enter password for %s: ", host);
776 retval = scanf("%79s", pwd);
778 fprintf(stdout, "\n");
780 tcsetattr(0, TCSANOW, &old);
782 strncpy(buf, pwd, len);
786 static int find_urls(const char *tweet, int **pranges)
789 * magic obtained from
790 * http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html
792 static const char *re_magic =
793 "(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}"
794 "[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)"
795 "(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?";
799 int ovector[10] = {0,};
800 const size_t ovsize = sizeof(ovector)/sizeof(*ovector);
801 int startoffset, tweetlen;
805 int *ranges = malloc(sizeof(int) * rbound);
807 re = pcre_compile(re_magic,
808 PCRE_NO_AUTO_CAPTURE,
809 &errptr, &erroffset, NULL);
811 fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr);
815 tweetlen = strlen(tweet);
816 for (startoffset = 0; startoffset < tweetlen; ) {
818 rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0,
820 if (rc == PCRE_ERROR_NOMATCH)
824 fprintf(stderr, "pcre_exec @%u: %s\n",
829 for (i = 0; i < rc; i += 2) {
830 if ((rcount+2) == rbound) {
832 ranges = realloc(ranges, sizeof(int) * rbound);
835 ranges[rcount++] = ovector[i];
836 ranges[rcount++] = ovector[i+1];
839 startoffset = ovector[1];
849 * bidirectional popen() call
851 * @param rwepipe - int array of size three
852 * @param exe - program to run
853 * @param argv - argument list
854 * @return pid or -1 on error
856 * The caller passes in an array of three integers (rwepipe), on successful
857 * execution it can then write to element 0 (stdin of exe), and read from
858 * element 1 (stdout) and 2 (stderr).
860 static int popenRWE(int *rwepipe, const char *exe, const char *const argv[])
890 } else if (pid == 0) {
902 execvp(exe, (char **)argv);
922 static int pcloseRWE(int pid, int *rwepipe)
928 rc = waitpid(pid, &status, 0);
932 static char *shrink_one_url(int *rwepipe, char *big)
934 int biglen = strlen(big);
939 rc = dprintf(rwepipe[0], "%s\n", big);
943 smalllen = biglen + 128;
944 small = malloc(smalllen);
948 rc = read(rwepipe[1], small, smalllen);
949 if (rc < 0 || rc > biglen)
950 goto error_free_small;
952 if (strncmp(small, "http://", 7))
953 goto error_free_small;
956 while (smalllen && isspace(small[smalllen-1]))
957 small[--smalllen] = 0;
967 static char *shrink_urls(char *text)
974 const char *const shrink_args[] = {
980 int inlen = strlen(text);
982 dbg("before len=%u\n", inlen);
984 shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args);
988 rcount = find_urls(text, &ranges);
992 for (i = 0; i < rcount; i += 2) {
993 int url_start = ranges[i];
994 int url_end = ranges[i+1];
995 int long_url_len = url_end - url_start;
996 char *url = strndup(text + url_start, long_url_len);
998 int not_url_len = url_start - inofs;
1000 dbg("long url[%u]: %s\n", long_url_len, url);
1001 url = shrink_one_url(shrink_pipe, url);
1002 short_url_len = url ? strlen(url) : 0;
1003 dbg("short url[%u]: %s\n", short_url_len, url);
1005 if (!url || short_url_len >= long_url_len) {
1006 /* The short url ended up being too long
1009 strncpy(text + outofs, text + inofs,
1010 not_url_len + long_url_len);
1012 inofs += not_url_len + long_url_len;
1013 outofs += not_url_len + long_url_len;
1016 /* copy the unmodified block */
1017 strncpy(text + outofs, text + inofs, not_url_len);
1018 inofs += not_url_len;
1019 outofs += not_url_len;
1021 /* copy the new url */
1022 strncpy(text + outofs, url, short_url_len);
1023 inofs += long_url_len;
1024 outofs += short_url_len;
1030 /* copy the last block after the last match */
1032 int tail = inlen - inofs;
1034 strncpy(text + outofs, text + inofs, tail);
1041 (void)pcloseRWE(shrink_pid, shrink_pipe);
1044 dbg("after len=%u\n", outofs);
1048 int main(int argc, char *argv[], char *envp[])
1050 static const struct option options[] = {
1051 { "debug", 0, NULL, 'd' },
1052 { "verbose", 0, NULL, 'V' },
1053 { "account", 1, NULL, 'a' },
1054 { "password", 1, NULL, 'p' },
1055 { "host", 1, NULL, 'H' },
1056 { "proxy", 1, NULL, 'P' },
1057 { "action", 1, NULL, 'A' },
1058 { "user", 1, NULL, 'u' },
1059 { "group", 1, NULL, 'G' },
1060 { "logfile", 1, NULL, 'L' },
1061 { "shrink-urls", 0, NULL, 's' },
1062 { "help", 0, NULL, 'h' },
1063 { "bash", 0, NULL, 'b' },
1064 { "dry-run", 0, NULL, 'n' },
1065 { "page", 1, NULL, 'g' },
1066 { "version", 0, NULL, 'v' },
1069 struct session *session;
1072 static char password[80];
1082 session = session_alloc();
1084 fprintf(stderr, "no more memory...\n");
1088 /* get the current time so that we can log it later */
1090 session->time = strdup(ctime(&t));
1091 session->time[strlen(session->time)-1] = 0x00;
1093 session->homedir = strdup(getenv("HOME"));
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;
1222 * Show the version to make it easier to determine what
1228 if (session->action == ACTION_UNKNOWN) {
1229 fprintf(stderr, "Unknown action, valid actions are:\n");
1230 fprintf(stderr, "'update', 'friends', 'public', "
1231 "'replies', 'group' or 'user'.\n");
1235 if (session->host == HOST_TWITTER && session->action == ACTION_GROUP) {
1236 fprintf(stderr, "Groups only work in Identi.ca.\n");
1240 if (session->action == ACTION_GROUP && !session->group) {
1241 fprintf(stdout, "Enter group name: ");
1242 session->group = session->readline(NULL);
1245 if (!session->account) {
1246 fprintf(stdout, "Enter account for %s: ", session->hostname);
1247 session->account = session->readline(NULL);
1250 if (!session->password) {
1251 read_password(password, sizeof(password), session->hostname);
1252 session->password = strdup(password);
1255 if (session->action == ACTION_UPDATE) {
1256 if (session->bash || !session->interactive)
1257 tweet = get_string_from_stdin();
1259 tweet = session->readline("tweet: ");
1260 if (!tweet || strlen(tweet) == 0) {
1265 if (session->shrink_urls)
1266 tweet = shrink_urls(tweet);
1268 session->tweet = zalloc(strlen(tweet) + 10);
1270 sprintf(session->tweet, "%c %s",
1271 getuid() ? '$' : '#', tweet);
1273 sprintf(session->tweet, "%s", tweet);
1276 dbg("tweet = %s\n", session->tweet);
1280 session->user = strdup(session->account);
1282 if (session->page == 0)
1284 dbg("account = %s\n", session->account);
1285 dbg("password = %s\n", session->password);
1286 dbg("host = %d\n", session->host);
1287 dbg("action = %d\n", session->action);
1289 /* fork ourself so that the main shell can get on
1290 * with it's life as we try to connect and handle everything
1292 if (session->bash) {
1295 dbg("child is %d\n", child);
1300 retval = send_request(session);
1301 if (retval && !session->bash)
1302 fprintf(stderr, "operation failed\n");
1304 log_session(session, retval);
1306 session_free(session);