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 ); \
61 struct bti_curl_buffer {
66 static void display_help(void)
68 fprintf(stdout, "bti - send tweet to twitter\n");
69 fprintf(stdout, "Version: " BTI_VERSION "\n");
70 fprintf(stdout, "Usage:\n");
71 fprintf(stdout, " bti [options]\n");
72 fprintf(stdout, "options are:\n");
73 fprintf(stdout, " --account accountname\n");
74 fprintf(stdout, " --password password\n");
75 fprintf(stdout, " --proxy PROXY:PORT\n");
76 fprintf(stdout, " --host HOST\n");
77 fprintf(stdout, " --bash\n");
78 fprintf(stdout, " --debug\n");
79 fprintf(stdout, " --version\n");
80 fprintf(stdout, " --help\n");
83 static void display_version(void)
85 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
88 static char *get_string_from_stdin(void)
90 static char *string = (char *)NULL;
93 string = (char *)NULL;
96 string = readline("tweet: ");
101 static struct session *session_alloc(void)
103 struct session *session;
105 session = zalloc(sizeof(*session));
111 static void session_free(struct session *session)
115 free(session->password);
116 free(session->account);
117 free(session->tweet);
118 free(session->proxy);
120 free(session->homedir);
124 static struct bti_curl_buffer *bti_curl_buffer_alloc(void)
126 struct bti_curl_buffer *buffer;
128 buffer = zalloc(sizeof(*buffer));
132 /* start out with a data buffer of 1 byte to
133 * make the buffer fill logic simpler */
134 buffer->data = zalloc(1);
143 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
151 static const char *twitter_url = "https://twitter.com/statuses/update.xml";
152 static const char *identica_url = "http://identi.ca/api/statuses/update.xml";
154 static CURL *curl_init(void)
158 curl = curl_easy_init();
160 fprintf(stderr, "Can not init CURL!\n");
163 /* some ssl sanity checks on the connection we are making */
164 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
165 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
169 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
171 struct bti_curl_buffer *curl_buf = userp;
172 size_t buffer_size = size * nmemb;
175 if ((!buffer) || (!buffer_size) || (!curl_buf))
178 /* add to the data we already have */
179 temp = zalloc(curl_buf->length + buffer_size + 1);
183 memcpy(temp, curl_buf->data, curl_buf->length);
184 free(curl_buf->data);
185 curl_buf->data = temp;
186 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
187 curl_buf->length += buffer_size;
189 dbg("%s\n", curl_buf->data);
194 static int send_tweet(struct session *session)
196 char user_password[500];
198 struct bti_curl_buffer *curl_buf;
201 struct curl_httppost *formpost = NULL;
202 struct curl_httppost *lastptr = NULL;
203 struct curl_slist *slist = NULL;
208 curl_buf = bti_curl_buffer_alloc();
212 snprintf(user_password, sizeof(user_password), "%s:%s",
213 session->account, session->password);
214 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
220 curl_formadd(&formpost, &lastptr,
221 CURLFORM_COPYNAME, "status",
222 CURLFORM_COPYCONTENTS, session->tweet,
225 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
227 switch (session->host) {
229 curl_easy_setopt(curl, CURLOPT_URL, twitter_url);
231 * twitter doesn't like the "Expect: 100-continue" header
232 * anymore, so turn it off.
234 slist = curl_slist_append(slist, "Expect:");
235 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
238 curl_easy_setopt(curl, CURLOPT_URL, identica_url);
243 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
246 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
247 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
249 dbg("user_password = %s\n", user_password);
250 dbg("data = %s\n", data);
251 dbg("proxy = %s\n", session->proxy);
253 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
254 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
255 res = curl_easy_perform(curl);
256 if (res && !session->bash) {
257 fprintf(stderr, "error(%d) trying to send tweet\n", res);
261 curl_easy_cleanup(curl);
262 curl_formfree(formpost);
263 bti_curl_buffer_free(curl_buf);
267 static void parse_configfile(struct session *session)
272 char *account = NULL;
273 char *password = NULL;
278 /* config file is ~/.bti */
279 file = alloca(strlen(session->homedir) + 7);
281 sprintf(file, "%s/.bti", session->homedir);
283 config_file = fopen(file, "r");
285 /* No error if file does not exist or is unreadable. */
286 if (config_file == NULL)
290 ssize_t n = getline(&line, &len, config_file);
293 if (line[n - 1] == '\n')
295 /* Parse file. Format is the usual value pairs:
298 # is a comment character
300 *strchrnul(line, '#') = '\0';
304 /* Ignore blank lines. */
308 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
312 } else if (!strncasecmp(c, "password", 8) &&
316 password = strdup(c);
317 } else if (!strncasecmp(c, "host", 4) &&
322 } else if (!strncasecmp(c, "proxy", 5) &&
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 /* Free buffer and close file. */
352 static void log_session(struct session *session, int retval)
358 /* logfile is ~/.bti.log */
359 filename = alloca(strlen(session->homedir) + 10);
361 sprintf(filename, "%s/.bti.log", session->homedir);
363 log_file = fopen(filename, "a+");
364 if (log_file == NULL)
366 switch (session->host) {
379 fprintf(log_file, "%s: host=%s tweet failed\n", session->time, host);
381 fprintf(log_file, "%s: host=%s tweet=%s\n", session->time, host, session->tweet);
386 int main(int argc, char *argv[], char *envp[])
388 static const struct option options[] = {
389 { "debug", 0, NULL, 'd' },
390 { "account", 1, NULL, 'a' },
391 { "password", 1, NULL, 'p' },
392 { "host", 1, NULL, 'H' },
393 { "proxy", 1, NULL, 'P' },
394 { "help", 0, NULL, 'h' },
395 { "bash", 0, NULL, 'b' },
396 { "version", 0, NULL, 'v' },
399 struct session *session;
407 char *pwd = getenv("PWD");
411 rl_bind_key('\t', rl_insert);
412 session = session_alloc();
414 fprintf(stderr, "no more memory...\n");
418 /* get the current time so that we can log it later */
420 session->time = strdup(ctime(&t));
421 session->time[strlen(session->time)-1] = 0x00;
423 session->homedir = strdup(getenv("HOME"));
425 curl_global_init(CURL_GLOBAL_ALL);
427 /* Set environment variables first, before reading command line options
428 * or config file values. */
429 http_proxy = getenv("http_proxy");
432 free(session->proxy);
433 session->proxy = strdup(http_proxy);
434 dbg("http_proxy = %s\n", session->proxy);
437 parse_configfile(session);
440 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:h",
449 if (session->account)
450 free(session->account);
451 session->account = strdup(optarg);
452 dbg("account = %s\n", session->account);
455 if (session->password)
456 free(session->password);
457 session->password = strdup(optarg);
458 dbg("password = %s\n", session->password);
462 free(session->proxy);
463 session->proxy = strdup(optarg);
464 dbg("proxy = %s\n", session->proxy);
467 if (strcasecmp(optarg, "twitter") == 0)
468 session->host = HOST_TWITTER;
469 if (strcasecmp(optarg, "identica") == 0)
470 session->host = HOST_IDENTICA;
471 dbg("host = %d\n", session->host);
488 if (!session->account) {
489 fprintf(stdout, "Enter twitter account: ");
490 session->account = get_string_from_stdin();
493 if (!session->password) {
494 fprintf(stdout, "Enter twitter password: ");
495 session->password = get_string_from_stdin();
498 /* get the current working directory basename */
499 if (strcmp(pwd, home) == 0)
502 dir = strrchr(pwd, '/');
509 tweet = get_string_from_stdin();
510 if (!tweet || strlen(tweet) == 0) {
515 // session->tweet = zalloc(strlen(tweet) + strlen(dir) + 10);
516 session->tweet = zalloc(strlen(tweet) + 10);
518 /* if --bash is specified, add the "PWD $ " to
519 * the start of the tweet. */
521 // sprintf(session->tweet, "%s $ %s", dir, tweet);
522 sprintf(session->tweet, "$ %s", tweet);
524 sprintf(session->tweet, "%s", tweet);
527 dbg("account = %s\n", session->account);
528 dbg("password = %s\n", session->password);
529 dbg("tweet = %s\n", session->tweet);
530 dbg("host = %d\n", session->host);
532 /* fork ourself so that the main shell can get on
533 * with it's life as we try to connect and handle everything
538 dbg("child is %d\n", child);
543 retval = send_tweet(session);
544 if (retval && !session->bash)
545 fprintf(stderr, "tweet failed\n");
547 log_session(session, retval);
549 session_free(session);