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.
28 #include <curl/curl.h>
29 #include "bti_version.h"
32 #define zalloc(size) calloc(size, 1)
34 #define dbg(format, arg...) \
37 printf("%s: " format , __func__ , ## arg ); \
57 struct bti_curl_buffer {
62 static void display_help(void)
64 fprintf(stdout, "bti - send tweet to twitter\n");
65 fprintf(stdout, "Version: " BTI_VERSION "\n");
66 fprintf(stdout, "Usage:\n");
67 fprintf(stdout, " bti [options]\n");
68 fprintf(stdout, "options are:\n");
69 fprintf(stdout, " --account accountname\n");
70 fprintf(stdout, " --password password\n");
71 fprintf(stdout, " --proxy PROXY:PORT\n");
72 fprintf(stdout, " --host HOST\n");
73 fprintf(stdout, " --bash\n");
74 fprintf(stdout, " --debug\n");
75 fprintf(stdout, " --version\n");
76 fprintf(stdout, " --help\n");
79 static void display_version(void)
81 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
84 static char *get_string_from_stdin(void)
89 string = zalloc(1000);
93 if (!fgets(string, 999, stdin))
95 temp = strchr(string, '\n');
100 static struct session *session_alloc(void)
102 struct session *session;
104 session = zalloc(sizeof(*session));
110 static void session_free(struct session *session)
114 free(session->password);
115 free(session->account);
116 free(session->tweet);
117 free(session->proxy);
121 static struct bti_curl_buffer *bti_curl_buffer_alloc(void)
123 struct bti_curl_buffer *buffer;
125 buffer = zalloc(sizeof(*buffer));
129 /* start out with a data buffer of 1 byte to
130 * make the buffer fill logic simpler */
131 buffer->data = zalloc(1);
140 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
148 static const char *twitter_url = "https://twitter.com/statuses/update.xml";
149 static const char *identica_url = "http://identi.ca/api/statuses/update.xml";
151 static CURL *curl_init(void)
155 curl = curl_easy_init();
157 fprintf(stderr, "Can not init CURL!\n");
160 /* some ssl sanity checks on the connection we are making */
161 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
162 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
166 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
168 struct bti_curl_buffer *curl_buf = userp;
169 size_t buffer_size = size * nmemb;
172 if ((!buffer) || (!buffer_size) || (!curl_buf))
175 /* add to the data we already have */
176 temp = zalloc(curl_buf->length + buffer_size + 1);
180 memcpy(temp, curl_buf->data, curl_buf->length);
181 free(curl_buf->data);
182 curl_buf->data = temp;
183 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
184 curl_buf->length += buffer_size;
186 dbg("%s\n", curl_buf->data);
191 static int send_tweet(struct session *session)
193 char user_password[500];
195 struct bti_curl_buffer *curl_buf;
198 struct curl_httppost *formpost = NULL;
199 struct curl_httppost *lastptr = NULL;
200 struct curl_slist *slist = NULL;
205 curl_buf = bti_curl_buffer_alloc();
209 snprintf(user_password, sizeof(user_password), "%s:%s",
210 session->account, session->password);
211 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
217 curl_formadd(&formpost, &lastptr,
218 CURLFORM_COPYNAME, "status",
219 CURLFORM_COPYCONTENTS, session->tweet,
222 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
224 switch (session->host) {
226 curl_easy_setopt(curl, CURLOPT_URL, twitter_url);
228 * twitter doesn't like the "Expect: 100-continue" header
229 * anymore, so turn it off.
231 slist = curl_slist_append(slist, "Expect:");
232 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
235 curl_easy_setopt(curl, CURLOPT_URL, identica_url);
240 curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
243 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
244 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
246 dbg("user_password = %s\n", user_password);
247 dbg("data = %s\n", data);
248 dbg("proxy = %s\n", session->proxy);
250 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
251 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
252 res = curl_easy_perform(curl);
253 if (res && !session->bash) {
254 fprintf(stderr, "error(%d) trying to send tweet\n", res);
258 curl_easy_cleanup(curl);
259 curl_formfree(formpost);
260 bti_curl_buffer_free(curl_buf);
264 static void parse_configfile(struct session *session)
269 char *account = NULL;
270 char *password = NULL;
274 char *home = getenv("HOME");
276 /* config file is ~/.bti */
277 file = alloca(strlen(home) + 7);
279 sprintf(file, "%s/.bti", home);
281 config_file = fopen(file, "r");
283 /* No error if file does not exist or is unreadable. */
284 if (config_file == NULL)
288 ssize_t n = getline(&line, &len, config_file);
291 if (line[n - 1] == '\n')
293 /* Parse file. Format is the usual value pairs:
296 # is a comment character
298 *strchrnul(line, '#') = '\0';
302 /* Ignore blank lines. */
306 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
310 } else if (!strncasecmp(c, "password", 8) &&
314 password = strdup(c);
315 } else if (!strncasecmp(c, "host", 4) &&
320 } else if (!strncasecmp(c, "proxy", 5) &&
326 } while (!feof(config_file));
329 session->password = password;
331 session->account = account;
333 if (strcasecmp(host, "twitter") == 0)
334 session->host = HOST_TWITTER;
335 if (strcasecmp(host, "identica") == 0)
336 session->host = HOST_IDENTICA;
341 free(session->proxy);
342 session->proxy = proxy;
345 /* Free buffer and close file. */
350 int main(int argc, char *argv[], char *envp[])
352 static const struct option options[] = {
353 { "debug", 0, NULL, 'd' },
354 { "account", 1, NULL, 'a' },
355 { "password", 1, NULL, 'p' },
356 { "host", 1, NULL, 'H' },
357 { "proxy", 1, NULL, 'P' },
358 { "help", 0, NULL, 'h' },
359 { "bash", 0, NULL, 'b' },
360 { "version", 0, NULL, 'v' },
363 struct session *session;
370 char *home = getenv("HOME");
371 char *pwd = getenv("PWD");
374 session = session_alloc();
376 fprintf(stderr, "no more memory...\n");
380 curl_global_init(CURL_GLOBAL_ALL);
382 /* Set environment variables first, before reading command line options
383 * or config file values. */
384 http_proxy = getenv("http_proxy");
387 free(session->proxy);
388 session->proxy = strdup(http_proxy);
389 dbg("http_proxy = %s\n", session->proxy);
392 parse_configfile(session);
395 option = getopt_long_only(argc, argv, "dqe:p:P:H:a:h",
404 if (session->account)
405 free(session->account);
406 session->account = strdup(optarg);
407 dbg("account = %s\n", session->account);
410 if (session->password)
411 free(session->password);
412 session->password = strdup(optarg);
413 dbg("password = %s\n", session->password);
417 free(session->proxy);
418 session->proxy = strdup(optarg);
419 dbg("proxy = %s\n", session->proxy);
422 if (strcasecmp(optarg, "twitter") == 0)
423 session->host = HOST_TWITTER;
424 if (strcasecmp(optarg, "identica") == 0)
425 session->host = HOST_IDENTICA;
426 dbg("host = %d\n", session->host);
443 if (!session->account) {
444 fprintf(stdout, "Enter twitter account: ");
445 session->account = get_string_from_stdin();
448 if (!session->password) {
449 fprintf(stdout, "Enter twitter password: ");
450 session->password = get_string_from_stdin();
453 /* get the current working directory basename */
454 if (strcmp(pwd, home) == 0)
457 dir = strrchr(pwd, '/');
464 tweet = get_string_from_stdin();
465 if (!tweet || strlen(tweet) == 0) {
470 // session->tweet = zalloc(strlen(tweet) + strlen(dir) + 10);
471 session->tweet = zalloc(strlen(tweet) + 10);
473 /* if --bash is specified, add the "PWD $ " to
474 * the start of the tweet. */
476 // sprintf(session->tweet, "%s $ %s", dir, tweet);
477 sprintf(session->tweet, "$ %s", tweet);
479 sprintf(session->tweet, "%s", tweet);
482 dbg("account = %s\n", session->account);
483 dbg("password = %s\n", session->password);
484 dbg("tweet = %s\n", session->tweet);
485 dbg("host = %d\n", session->host);
487 /* fork ourself so that the main shell can get on
488 * with it's life as we try to connect and handle everything
493 dbg("child is %d\n", child);
498 retval = send_tweet(session);
499 if (retval && !session->bash) {
500 fprintf(stderr, "tweet failed\n");
504 session_free(session);