2 * Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <curl/curl.h>
30 #include <readline/readline.h>
31 #include "bti_version.h"
34 #define zalloc(size) calloc(size, 1)
36 #define dbg(format, arg...) \
39 printf("%s: " format , __func__ , ## arg); \
62 struct bti_curl_buffer {
67 static void display_help(void)
69 fprintf(stdout, "bti - send tweet to twitter\n");
70 fprintf(stdout, "Version: " BTI_VERSION "\n");
71 fprintf(stdout, "Usage:\n");
72 fprintf(stdout, " bti [options]\n");
73 fprintf(stdout, "options are:\n");
74 fprintf(stdout, " --account accountname\n");
75 fprintf(stdout, " --password password\n");
76 fprintf(stdout, " --proxy PROXY:PORT\n");
77 fprintf(stdout, " --host HOST\n");
78 fprintf(stdout, " --logfile logfile\n");
79 fprintf(stdout, " --bash\n");
80 fprintf(stdout, " --debug\n");
81 fprintf(stdout, " --version\n");
82 fprintf(stdout, " --help\n");
85 static void display_version(void)
87 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
90 static char *get_string_from_stdin(void)
92 static char *string = (char *)NULL;
95 string = (char *)NULL;
98 string = readline("tweet: ");
103 static struct session *session_alloc(void)
105 struct session *session;
107 session = zalloc(sizeof(*session));
113 static void session_free(struct session *session)
117 free(session->password);
118 free(session->account);
119 free(session->tweet);
120 free(session->proxy);
122 free(session->homedir);
126 static struct bti_curl_buffer *bti_curl_buffer_alloc(void)
128 struct bti_curl_buffer *buffer;
130 buffer = zalloc(sizeof(*buffer));
134 /* start out with a data buffer of 1 byte to
135 * make the buffer fill logic simpler */
136 buffer->data = zalloc(1);
145 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
153 static const char *twitter_url = "https://twitter.com/statuses/update.xml";
154 static const char *identica_url = "http://identi.ca/api/statuses/update.xml";
156 static CURL *curl_init(void)
160 curl = curl_easy_init();
162 fprintf(stderr, "Can not init CURL!\n");
165 /* some ssl sanity checks on the connection we are making */
166 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
167 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
171 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
173 struct bti_curl_buffer *curl_buf = userp;
174 size_t buffer_size = size * nmemb;
177 if ((!buffer) || (!buffer_size) || (!curl_buf))
180 /* add to the data we already have */
181 temp = zalloc(curl_buf->length + buffer_size + 1);
185 memcpy(temp, curl_buf->data, curl_buf->length);
186 free(curl_buf->data);
187 curl_buf->data = temp;
188 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
189 curl_buf->length += buffer_size;
191 dbg("%s\n", curl_buf->data);
196 static int send_tweet(struct session *session)
198 char user_password[500];
200 struct bti_curl_buffer *curl_buf;
203 struct curl_httppost *formpost = NULL;
204 struct curl_httppost *lastptr = NULL;
205 struct curl_slist *slist = NULL;
210 curl_buf = bti_curl_buffer_alloc();
214 snprintf(user_password, sizeof(user_password), "%s:%s",
215 session->account, session->password);
216 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
222 curl_formadd(&formpost, &lastptr,
223 CURLFORM_COPYNAME, "status",
224 CURLFORM_COPYCONTENTS, session->tweet,
227 curl_formadd(&formpost, &lastptr,
228 CURLFORM_COPYNAME, "source",
229 CURLFORM_COPYCONTENTS, "bti",
232 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
234 switch (session->host) {
236 curl_easy_setopt(curl, CURLOPT_URL, twitter_url);
238 * twitter doesn't like the "Expect: 100-continue" header
239 * anymore, so turn it off.
241 slist = curl_slist_append(slist, "Expect:");
242 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
245 curl_easy_setopt(curl, CURLOPT_URL, identica_url);
250 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
253 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
254 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
256 dbg("user_password = %s\n", user_password);
257 dbg("data = %s\n", data);
258 dbg("proxy = %s\n", session->proxy);
260 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
261 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
262 res = curl_easy_perform(curl);
263 if (res && !session->bash) {
264 fprintf(stderr, "error(%d) trying to send tweet\n", res);
268 curl_easy_cleanup(curl);
269 curl_formfree(formpost);
270 bti_curl_buffer_free(curl_buf);
274 static void parse_configfile(struct session *session)
279 char *account = NULL;
280 char *password = NULL;
283 char *logfile = NULL;
286 /* config file is ~/.bti */
287 file = alloca(strlen(session->homedir) + 7);
289 sprintf(file, "%s/.bti", session->homedir);
291 config_file = fopen(file, "r");
293 /* No error if file does not exist or is unreadable. */
294 if (config_file == NULL)
298 ssize_t n = getline(&line, &len, config_file);
301 if (line[n - 1] == '\n')
303 /* Parse file. Format is the usual value pairs:
306 # is a comment character
308 *strchrnul(line, '#') = '\0';
312 /* Ignore blank lines. */
316 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
320 } else if (!strncasecmp(c, "password", 8) &&
324 password = strdup(c);
325 } else if (!strncasecmp(c, "host", 4) &&
330 } else if (!strncasecmp(c, "proxy", 5) &&
335 } else if (!strncasecmp(c, "logfile", 7) &&
341 } while (!feof(config_file));
344 session->password = password;
346 session->account = account;
348 if (strcasecmp(host, "twitter") == 0)
349 session->host = HOST_TWITTER;
350 if (strcasecmp(host, "identica") == 0)
351 session->host = HOST_IDENTICA;
356 free(session->proxy);
357 session->proxy = proxy;
360 session->logfile = logfile;
362 /* Free buffer and close file. */
367 static void log_session(struct session *session, int retval)
373 /* Only log something if we have a log file set */
374 if (!session->logfile)
377 filename = alloca(strlen(session->homedir) +
378 strlen(session->logfile) + 3);
380 sprintf(filename, "%s/%s", session->homedir, session->logfile);
382 log_file = fopen(filename, "a+");
383 if (log_file == NULL)
385 switch (session->host) {
398 fprintf(log_file, "%s: host=%s tweet failed\n",
399 session->time, host);
401 fprintf(log_file, "%s: host=%s tweet=%s\n",
402 session->time, host, session->tweet);
407 int main(int argc, char *argv[], char *envp[])
409 static const struct option options[] = {
410 { "debug", 0, NULL, 'd' },
411 { "account", 1, NULL, 'a' },
412 { "password", 1, NULL, 'p' },
413 { "host", 1, NULL, 'H' },
414 { "proxy", 1, NULL, 'P' },
415 { "logfile", 1, NULL, 'L' },
416 { "help", 0, NULL, 'h' },
417 { "bash", 0, NULL, 'b' },
418 { "version", 0, NULL, 'v' },
421 struct session *session;
430 rl_bind_key('\t', rl_insert);
432 session = session_alloc();
434 fprintf(stderr, "no more memory...\n");
438 /* get the current time so that we can log it later */
440 session->time = strdup(ctime(&t));
441 session->time[strlen(session->time)-1] = 0x00;
443 session->homedir = strdup(getenv("HOME"));
445 curl_global_init(CURL_GLOBAL_ALL);
447 /* Set environment variables first, before reading command line options
448 * or config file values. */
449 http_proxy = getenv("http_proxy");
452 free(session->proxy);
453 session->proxy = strdup(http_proxy);
454 dbg("http_proxy = %s\n", session->proxy);
457 parse_configfile(session);
460 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:h",
469 if (session->account)
470 free(session->account);
471 session->account = strdup(optarg);
472 dbg("account = %s\n", session->account);
475 if (session->password)
476 free(session->password);
477 session->password = strdup(optarg);
478 dbg("password = %s\n", session->password);
482 free(session->proxy);
483 session->proxy = strdup(optarg);
484 dbg("proxy = %s\n", session->proxy);
487 if (session->logfile)
488 free(session->logfile);
489 session->logfile = strdup(optarg);
490 dbg("logfile = %s\n", session->logfile);
493 if (strcasecmp(optarg, "twitter") == 0)
494 session->host = HOST_TWITTER;
495 if (strcasecmp(optarg, "identica") == 0)
496 session->host = HOST_IDENTICA;
497 dbg("host = %d\n", session->host);
514 if (!session->account) {
515 fprintf(stdout, "Enter twitter account: ");
516 session->account = get_string_from_stdin();
519 if (!session->password) {
520 fprintf(stdout, "Enter twitter password: ");
521 session->password = get_string_from_stdin();
524 tweet = get_string_from_stdin();
525 if (!tweet || strlen(tweet) == 0) {
530 session->tweet = zalloc(strlen(tweet) + 10);
532 /* if --bash is specified, add the "PWD $ " to
533 * the start of the tweet. */
535 sprintf(session->tweet, "$ %s", tweet);
537 sprintf(session->tweet, "%s", tweet);
540 dbg("account = %s\n", session->account);
541 dbg("password = %s\n", session->password);
542 dbg("tweet = %s\n", session->tweet);
543 dbg("host = %d\n", session->host);
545 /* fork ourself so that the main shell can get on
546 * with it's life as we try to connect and handle everything
551 dbg("child is %d\n", child);
556 retval = send_tweet(session);
557 if (retval && !session->bash)
558 fprintf(stderr, "tweet failed\n");
560 log_session(session, retval);
562 session_free(session);