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;
197 struct curl_slist *slist = NULL;
202 curl_buf = bti_curl_buffer_alloc();
206 snprintf(user_password, sizeof(user_password), "%s:%s",
207 session->account, session->password);
208 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
214 curl_formadd(&formpost, &lastptr,
215 CURLFORM_COPYNAME, "status",
216 CURLFORM_COPYCONTENTS, session->tweet,
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_VERBOSE, 1);
238 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
240 dbg("user_password = %s\n", user_password);
241 dbg("data = %s\n", data);
243 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
244 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
245 res = curl_easy_perform(curl);
246 if (res && !session->bash) {
247 fprintf(stderr, "error(%d) trying to send tweet\n", res);
251 curl_easy_cleanup(curl);
252 curl_formfree(formpost);
253 bti_curl_buffer_free(curl_buf);
257 static void parse_configfile(struct session *session)
262 char *account = NULL;
263 char *password = NULL;
266 char *home = getenv("HOME");
268 /* config file is ~/.bti */
269 file = alloca(strlen(home) + 7);
271 sprintf(file, "%s/.bti", home);
273 config_file = fopen(file, "r");
275 /* No error if file does not exist or is unreadable. */
276 if (config_file == NULL)
280 ssize_t n = getline(&line, &len, config_file);
283 if (line[n - 1] == '\n')
285 /* Parse file. Format is the usual value pairs:
288 # is a comment character
290 *strchrnul(line, '#') = '\0';
294 /* Ignore blank lines. */
298 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
302 } else if (!strncasecmp(c, "password", 8) &&
306 password = strdup(c);
307 } else if (!strncasecmp(c, "host", 4) &&
313 } while (!feof(config_file));
316 session->password = password;
318 session->account = account;
320 if (strcasecmp(host, "twitter") == 0)
321 session->host = HOST_TWITTER;
322 if (strcasecmp(host, "identica") == 0)
323 session->host = HOST_IDENTICA;
327 /* Free buffer and close file. */
332 int main(int argc, char *argv[], char *envp[])
334 static const struct option options[] = {
335 { "debug", 0, NULL, 'd' },
336 { "account", 1, NULL, 'a' },
337 { "password", 1, NULL, 'p' },
338 { "host", 1, NULL, 'H' },
339 { "help", 0, NULL, 'h' },
340 { "bash", 0, NULL, 'b' },
341 { "version", 0, NULL, 'v' },
344 struct session *session;
350 char *home = getenv("HOME");
351 char *pwd = getenv("PWD");
354 session = session_alloc();
356 fprintf(stderr, "no more memory...\n");
360 curl_global_init(CURL_GLOBAL_ALL);
361 parse_configfile(session);
364 option = getopt_long_only(argc, argv, "dqe:p:H:a:h",
373 if (session->account)
374 free(session->account);
375 session->account = strdup(optarg);
376 dbg("account = %s\n", session->account);
379 if (session->password)
380 free(session->password);
381 session->password = strdup(optarg);
382 dbg("password = %s\n", session->password);
385 if (strcasecmp(optarg, "twitter") == 0)
386 session->host = HOST_TWITTER;
387 if (strcasecmp(optarg, "identica") == 0)
388 session->host = HOST_IDENTICA;
389 dbg("host = %d\n", session->host);
406 if (!session->account) {
407 fprintf(stdout, "Enter twitter account: ");
408 session->account = get_string_from_stdin();
411 if (!session->password) {
412 fprintf(stdout, "Enter twitter password: ");
413 session->password = get_string_from_stdin();
416 /* get the current working directory basename */
417 if (strcmp(pwd, home) == 0)
420 dir = strrchr(pwd, '/');
427 tweet = get_string_from_stdin();
428 if (!tweet || strlen(tweet) == 0) {
433 // session->tweet = zalloc(strlen(tweet) + strlen(dir) + 10);
434 session->tweet = zalloc(strlen(tweet) + 10);
436 /* if --bash is specified, add the "PWD $ " to
437 * the start of the tweet. */
439 // sprintf(session->tweet, "%s $ %s", dir, tweet);
440 sprintf(session->tweet, "$ %s", tweet);
442 sprintf(session->tweet, "%s", tweet);
445 dbg("account = %s\n", session->account);
446 dbg("password = %s\n", session->password);
447 dbg("tweet = %s\n", session->tweet);
448 dbg("host = %d\n", session->host);
450 /* fork ourself so that the main shell can get on
451 * with it's life as we try to connect and handle everything
456 dbg("child is %d\n", child);
461 retval = send_tweet(session);
462 if (retval && !session->bash) {
463 fprintf(stderr, "tweet failed\n");
467 session_free(session);