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 ); \
56 struct bti_curl_buffer {
61 static void display_help(void)
63 fprintf(stdout, "bti - send tweet to twitter\n");
64 fprintf(stdout, "Version: " BTI_VERSION "\n");
65 fprintf(stdout, "Usage:\n");
66 fprintf(stdout, " bti [options]\n");
67 fprintf(stdout, "options are:\n");
68 fprintf(stdout, " --account accountname\n");
69 fprintf(stdout, " --password password\n");
70 fprintf(stdout, " --host HOST\n");
71 fprintf(stdout, " --bash\n");
72 fprintf(stdout, " --debug\n");
73 fprintf(stdout, " --version\n");
74 fprintf(stdout, " --help\n");
77 static void display_version(void)
79 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
82 static char *get_string_from_stdin(void)
87 string = zalloc(1000);
91 if (!fgets(string, 999, stdin))
93 temp = strchr(string, '\n');
98 static struct session *session_alloc(void)
100 struct session *session;
102 session = zalloc(sizeof(*session));
108 static void session_free(struct session *session)
112 free(session->password);
113 free(session->account);
114 free(session->tweet);
118 static struct bti_curl_buffer *bti_curl_buffer_alloc(void)
120 struct bti_curl_buffer *buffer;
122 buffer = zalloc(sizeof(*buffer));
126 /* start out with a data buffer of 1 byte to
127 * make the buffer fill logic simpler */
128 buffer->data = zalloc(1);
137 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
145 static const char *twitter_url = "https://twitter.com/statuses/update.xml";
146 static const char *identica_url = "http://identi.ca/api/statuses/update.xml";
148 static CURL *curl_init(void)
152 curl = curl_easy_init();
154 fprintf(stderr, "Can not init CURL!\n");
157 /* some ssl sanity checks on the connection we are making */
158 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
159 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
163 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
165 struct bti_curl_buffer *curl_buf = userp;
166 size_t buffer_size = size * nmemb;
169 if ((!buffer) || (!buffer_size) || (!curl_buf))
172 /* add to the data we already have */
173 temp = zalloc(curl_buf->length + buffer_size + 1);
177 memcpy(temp, curl_buf->data, curl_buf->length);
178 free(curl_buf->data);
179 curl_buf->data = temp;
180 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
181 curl_buf->length += buffer_size;
183 dbg("%s\n", curl_buf->data);
188 static int send_tweet(struct session *session)
190 char user_password[500];
192 struct bti_curl_buffer *curl_buf;
195 struct curl_httppost *formpost = NULL;
196 struct curl_httppost *lastptr = NULL;
201 curl_buf = bti_curl_buffer_alloc();
205 snprintf(user_password, sizeof(user_password), "%s:%s",
206 session->account, session->password);
207 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
213 curl_formadd(&formpost, &lastptr,
214 CURLFORM_COPYNAME, "status",
215 CURLFORM_COPYCONTENTS, session->tweet,
218 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
220 switch (session->host) {
222 curl_easy_setopt(curl, CURLOPT_URL, twitter_url);
225 curl_easy_setopt(curl, CURLOPT_URL, identica_url);
230 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
231 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
233 dbg("user_password = %s\n", user_password);
234 dbg("data = %s\n", data);
236 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
237 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
238 res = curl_easy_perform(curl);
239 if (res && !session->bash) {
240 fprintf(stderr, "error(%d) trying to send tweet\n", res);
244 curl_easy_cleanup(curl);
245 curl_formfree(formpost);
246 bti_curl_buffer_free(curl_buf);
250 static void parse_configfile(struct session *session)
255 char *account = NULL;
256 char *password = NULL;
259 char *home = getenv("HOME");
261 /* config file is ~/.bti */
262 file = alloca(strlen(home) + 7);
264 sprintf(file, "%s/.bti", home);
266 config_file = fopen(file, "r");
268 /* No error if file does not exist or is unreadable. */
269 if (config_file == NULL)
273 ssize_t n = getline(&line, &len, config_file);
276 if (line[n - 1] == '\n')
278 /* Parse file. Format is the usual value pairs:
281 # is a comment character
283 *strchrnul(line, '#') = '\0';
287 /* Ignore blank lines. */
291 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
295 } else if (!strncasecmp(c, "password", 8) &&
299 password = strdup(c);
300 } else if (!strncasecmp(c, "host", 4) &&
306 } while (!feof(config_file));
309 session->password = password;
311 session->account = account;
313 if (strcasecmp(host, "twitter") == 0)
314 session->host = HOST_TWITTER;
315 if (strcasecmp(host, "identica") == 0)
316 session->host = HOST_IDENTICA;
320 /* Free buffer and close file. */
325 int main(int argc, char *argv[], char *envp[])
327 static const struct option options[] = {
328 { "debug", 0, NULL, 'd' },
329 { "account", 1, NULL, 'a' },
330 { "password", 1, NULL, 'p' },
331 { "host", 1, NULL, 'H' },
332 { "help", 0, NULL, 'h' },
333 { "bash", 0, NULL, 'b' },
334 { "version", 0, NULL, 'v' },
337 struct session *session;
343 char *home = getenv("HOME");
344 char *pwd = getenv("PWD");
347 session = session_alloc();
349 fprintf(stderr, "no more memory...\n");
353 curl_global_init(CURL_GLOBAL_ALL);
354 parse_configfile(session);
357 option = getopt_long_only(argc, argv, "dqe:p:H:a:h",
366 if (session->account)
367 free(session->account);
368 session->account = strdup(optarg);
369 dbg("account = %s\n", session->account);
372 if (session->password)
373 free(session->password);
374 session->password = strdup(optarg);
375 dbg("password = %s\n", session->password);
378 if (strcasecmp(optarg, "twitter") == 0)
379 session->host = HOST_TWITTER;
380 if (strcasecmp(optarg, "identica") == 0)
381 session->host = HOST_IDENTICA;
382 dbg("host = %d\n", session->host);
399 if (!session->account) {
400 fprintf(stdout, "Enter twitter account: ");
401 session->account = get_string_from_stdin();
404 if (!session->password) {
405 fprintf(stdout, "Enter twitter password: ");
406 session->password = get_string_from_stdin();
409 /* get the current working directory basename */
410 if (strcmp(pwd, home) == 0)
413 dir = strrchr(pwd, '/');
420 tweet = get_string_from_stdin();
421 if (!tweet || strlen(tweet) == 0) {
426 // session->tweet = zalloc(strlen(tweet) + strlen(dir) + 10);
427 session->tweet = zalloc(strlen(tweet) + 10);
429 /* if --bash is specified, add the "PWD $ " to
430 * the start of the tweet. */
432 // sprintf(session->tweet, "%s $ %s", dir, tweet);
433 sprintf(session->tweet, "$ %s", tweet);
435 sprintf(session->tweet, "%s", tweet);
438 dbg("account = %s\n", session->account);
439 dbg("password = %s\n", session->password);
440 dbg("tweet = %s\n", session->tweet);
441 dbg("host = %d\n", session->host);
443 /* fork ourself so that the main shell can get on
444 * with it's life as we try to connect and handle everything
449 dbg("child is %d\n", child);
454 retval = send_tweet(session);
455 if (retval && !session->bash) {
456 fprintf(stderr, "tweet failed\n");
460 session_free(session);