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 struct session *session_alloc(void)
92 struct session *session;
94 session = zalloc(sizeof(*session));
100 static void session_free(struct session *session)
104 free(session->password);
105 free(session->account);
106 free(session->tweet);
107 free(session->proxy);
109 free(session->homedir);
113 static struct bti_curl_buffer *bti_curl_buffer_alloc(void)
115 struct bti_curl_buffer *buffer;
117 buffer = zalloc(sizeof(*buffer));
121 /* start out with a data buffer of 1 byte to
122 * make the buffer fill logic simpler */
123 buffer->data = zalloc(1);
132 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
140 static const char *twitter_url = "https://twitter.com/statuses/update.xml";
141 static const char *identica_url = "http://identi.ca/api/statuses/update.xml";
143 static CURL *curl_init(void)
147 curl = curl_easy_init();
149 fprintf(stderr, "Can not init CURL!\n");
152 /* some ssl sanity checks on the connection we are making */
153 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
154 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
158 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
160 struct bti_curl_buffer *curl_buf = userp;
161 size_t buffer_size = size * nmemb;
164 if ((!buffer) || (!buffer_size) || (!curl_buf))
167 /* add to the data we already have */
168 temp = zalloc(curl_buf->length + buffer_size + 1);
172 memcpy(temp, curl_buf->data, curl_buf->length);
173 free(curl_buf->data);
174 curl_buf->data = temp;
175 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
176 curl_buf->length += buffer_size;
178 dbg("%s\n", curl_buf->data);
183 static int send_tweet(struct session *session)
185 char user_password[500];
187 struct bti_curl_buffer *curl_buf;
190 struct curl_httppost *formpost = NULL;
191 struct curl_httppost *lastptr = NULL;
192 struct curl_slist *slist = NULL;
197 curl_buf = bti_curl_buffer_alloc();
201 snprintf(user_password, sizeof(user_password), "%s:%s",
202 session->account, session->password);
203 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
209 curl_formadd(&formpost, &lastptr,
210 CURLFORM_COPYNAME, "status",
211 CURLFORM_COPYCONTENTS, session->tweet,
214 curl_formadd(&formpost, &lastptr,
215 CURLFORM_COPYNAME, "source",
216 CURLFORM_COPYCONTENTS, "bti",
219 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
221 switch (session->host) {
223 curl_easy_setopt(curl, CURLOPT_URL, twitter_url);
225 * twitter doesn't like the "Expect: 100-continue" header
226 * anymore, so turn it off.
228 slist = curl_slist_append(slist, "Expect:");
229 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
232 curl_easy_setopt(curl, CURLOPT_URL, identica_url);
237 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
240 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
241 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
243 dbg("user_password = %s\n", user_password);
244 dbg("data = %s\n", data);
245 dbg("proxy = %s\n", session->proxy);
247 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
248 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
249 res = curl_easy_perform(curl);
250 if (res && !session->bash) {
251 fprintf(stderr, "error(%d) trying to send tweet\n", res);
255 curl_easy_cleanup(curl);
256 curl_formfree(formpost);
257 bti_curl_buffer_free(curl_buf);
261 static void parse_configfile(struct session *session)
266 char *account = NULL;
267 char *password = NULL;
270 char *logfile = NULL;
273 /* config file is ~/.bti */
274 file = alloca(strlen(session->homedir) + 7);
276 sprintf(file, "%s/.bti", session->homedir);
278 config_file = fopen(file, "r");
280 /* No error if file does not exist or is unreadable. */
281 if (config_file == NULL)
285 ssize_t n = getline(&line, &len, config_file);
288 if (line[n - 1] == '\n')
290 /* Parse file. Format is the usual value pairs:
293 # is a comment character
295 *strchrnul(line, '#') = '\0';
299 /* Ignore blank lines. */
303 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
307 } else if (!strncasecmp(c, "password", 8) &&
311 password = strdup(c);
312 } else if (!strncasecmp(c, "host", 4) &&
317 } else if (!strncasecmp(c, "proxy", 5) &&
322 } else if (!strncasecmp(c, "logfile", 7) &&
328 } while (!feof(config_file));
331 session->password = password;
333 session->account = account;
335 if (strcasecmp(host, "twitter") == 0)
336 session->host = HOST_TWITTER;
337 if (strcasecmp(host, "identica") == 0)
338 session->host = HOST_IDENTICA;
343 free(session->proxy);
344 session->proxy = proxy;
347 session->logfile = logfile;
349 /* Free buffer and close file. */
354 static void log_session(struct session *session, int retval)
360 /* Only log something if we have a log file set */
361 if (!session->logfile)
364 filename = alloca(strlen(session->homedir) +
365 strlen(session->logfile) + 3);
367 sprintf(filename, "%s/%s", session->homedir, session->logfile);
369 log_file = fopen(filename, "a+");
370 if (log_file == NULL)
372 switch (session->host) {
385 fprintf(log_file, "%s: host=%s tweet failed\n",
386 session->time, host);
388 fprintf(log_file, "%s: host=%s tweet=%s\n",
389 session->time, host, session->tweet);
394 int main(int argc, char *argv[], char *envp[])
396 static const struct option options[] = {
397 { "debug", 0, NULL, 'd' },
398 { "account", 1, NULL, 'a' },
399 { "password", 1, NULL, 'p' },
400 { "host", 1, NULL, 'H' },
401 { "proxy", 1, NULL, 'P' },
402 { "logfile", 1, NULL, 'L' },
403 { "help", 0, NULL, 'h' },
404 { "bash", 0, NULL, 'b' },
405 { "version", 0, NULL, 'v' },
408 struct session *session;
417 rl_bind_key('\t', rl_insert);
419 session = session_alloc();
421 fprintf(stderr, "no more memory...\n");
425 /* get the current time so that we can log it later */
427 session->time = strdup(ctime(&t));
428 session->time[strlen(session->time)-1] = 0x00;
430 session->homedir = strdup(getenv("HOME"));
432 curl_global_init(CURL_GLOBAL_ALL);
434 /* Set environment variables first, before reading command line options
435 * or config file values. */
436 http_proxy = getenv("http_proxy");
439 free(session->proxy);
440 session->proxy = strdup(http_proxy);
441 dbg("http_proxy = %s\n", session->proxy);
444 parse_configfile(session);
447 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:h",
456 if (session->account)
457 free(session->account);
458 session->account = strdup(optarg);
459 dbg("account = %s\n", session->account);
462 if (session->password)
463 free(session->password);
464 session->password = strdup(optarg);
465 dbg("password = %s\n", session->password);
469 free(session->proxy);
470 session->proxy = strdup(optarg);
471 dbg("proxy = %s\n", session->proxy);
474 if (session->logfile)
475 free(session->logfile);
476 session->logfile = strdup(optarg);
477 dbg("logfile = %s\n", session->logfile);
480 if (strcasecmp(optarg, "twitter") == 0)
481 session->host = HOST_TWITTER;
482 if (strcasecmp(optarg, "identica") == 0)
483 session->host = HOST_IDENTICA;
484 dbg("host = %d\n", session->host);
501 if (!session->account) {
502 fprintf(stdout, "Enter twitter account: ");
503 session->account = readline(NULL);
506 if (!session->password) {
507 fprintf(stdout, "Enter twitter password: ");
508 session->password = readline(NULL);
512 tweet = readline(NULL);
514 tweet = readline("tweet: ");
515 if (!tweet || strlen(tweet) == 0) {
520 session->tweet = zalloc(strlen(tweet) + 10);
522 /* if --bash is specified, add the "PWD $ " to
523 * the start of the tweet. */
525 sprintf(session->tweet, "$ %s", tweet);
527 sprintf(session->tweet, "%s", tweet);
530 dbg("account = %s\n", session->account);
531 dbg("password = %s\n", session->password);
532 dbg("tweet = %s\n", session->tweet);
533 dbg("host = %d\n", session->host);
535 /* fork ourself so that the main shell can get on
536 * with it's life as we try to connect and handle everything
541 dbg("child is %d\n", child);
546 retval = send_tweet(session);
547 if (retval && !session->bash)
548 fprintf(stderr, "tweet failed\n");
550 log_session(session, retval);
552 session_free(session);