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 ); \
50 struct bti_curl_buffer {
55 static void display_help(void)
57 fprintf(stdout, "bti - send tweet to twitter\n");
58 fprintf(stdout, "Version: " BTI_VERSION "\n");
59 fprintf(stdout, "Usage:\n");
60 fprintf(stdout, " bti [options]\n");
61 fprintf(stdout, "options are:\n");
62 fprintf(stdout, " --account accountname\n");
63 fprintf(stdout, " --password password\n");
64 fprintf(stdout, " --bash\n");
65 fprintf(stdout, " --debug\n");
66 fprintf(stdout, " --version\n");
67 fprintf(stdout, " --help\n");
70 static void display_version(void)
72 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
75 static char *get_string_from_stdin(void)
84 if (!fgets(string, 99, stdin))
86 temp = strchr(string, '\n');
91 static struct session *session_alloc(void)
93 struct session *session;
95 session = zalloc(sizeof(*session));
101 static void session_free(struct session *session)
105 free(session->password);
106 free(session->account);
107 free(session->tweet);
111 static struct bti_curl_buffer *bti_curl_buffer_alloc(void)
113 struct bti_curl_buffer *buffer;
115 buffer = zalloc(sizeof(*buffer));
119 /* start out with a data buffer of 1 byte to
120 * make the buffer fill logic simpler */
121 buffer->data = zalloc(1);
130 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
138 static const char *twitter_url = "https://twitter.com/statuses/update.xml";
140 static CURL *curl_init(void)
144 curl = curl_easy_init();
146 fprintf(stderr, "Can not init CURL!\n");
149 /* some ssl sanity checks on the connection we are making */
150 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
151 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
155 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
157 struct bti_curl_buffer *curl_buf = userp;
158 size_t buffer_size = size * nmemb;
161 if ((!buffer) || (!buffer_size) || (!curl_buf))
164 /* add to the data we already have */
165 temp = zalloc(curl_buf->length + buffer_size + 1);
169 memcpy(temp, curl_buf->data, curl_buf->length);
170 free(curl_buf->data);
171 curl_buf->data = temp;
172 memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
173 curl_buf->length += buffer_size;
175 dbg("%s\n", curl_buf->data);
180 static int send_tweet(struct session *session)
182 char user_password[500];
184 struct bti_curl_buffer *curl_buf;
187 struct curl_httppost *formpost = NULL;
188 struct curl_httppost *lastptr = NULL;
193 curl_buf = bti_curl_buffer_alloc();
197 snprintf(user_password, sizeof(user_password), "%s:%s",
198 session->account, session->password);
199 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
205 curl_formadd(&formpost, &lastptr,
206 CURLFORM_COPYNAME, "status",
207 CURLFORM_COPYCONTENTS, session->tweet,
210 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
211 curl_easy_setopt(curl, CURLOPT_URL, twitter_url);
213 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
214 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
216 dbg("user_password = %s\n", user_password);
217 dbg("data = %s\n", data);
219 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
220 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
221 res = curl_easy_perform(curl);
222 if (res && !session->bash) {
223 fprintf(stderr, "error(%d) trying to send tweet\n", res);
227 curl_easy_cleanup(curl);
228 curl_formfree(formpost);
229 bti_curl_buffer_free(curl_buf);
233 static void parse_configfile(struct session *session)
238 char *account = NULL;
239 char *password = NULL;
241 char *home = getenv("HOME");
243 /* config file is ~/.bti */
244 file = alloca(strlen(home) + 7);
246 sprintf(file, "%s/.bti", home);
248 config_file = fopen(file, "r");
250 /* No error if file does not exist or is unreadable. */
251 if (config_file == NULL)
255 ssize_t n = getline(&line, &len, config_file);
258 if (line[n - 1] == '\n')
260 /* Parse file. Format is the usual value pairs:
263 # is a comment character
265 *strchrnul(line, '#') = '\0';
269 /* Ignore blank lines. */
273 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
277 } else if (!strncasecmp(c, "password", 8) &&
281 password = strdup(c);
283 } while (!feof(config_file));
286 session->password = password;
288 session->account= account;
290 /* Free buffer and close file. */
295 int main(int argc, char *argv[], char *envp[])
297 static const struct option options[] = {
298 { "debug", 0, NULL, 'd' },
299 { "account", 1, NULL, 'a' },
300 { "password", 1, NULL, 'p' },
301 { "help", 0, NULL, 'h' },
302 { "bash", 0, NULL, 'b' },
303 { "version", 0, NULL, 'v' },
306 struct session *session;
311 char *home = getenv("HOME");
312 char *pwd = getenv("PWD");
315 session = session_alloc();
317 fprintf(stderr, "no more memory...\n");
321 curl_global_init(CURL_GLOBAL_ALL);
322 parse_configfile(session);
325 option = getopt_long_only(argc, argv, "dqe:p:a:h",
334 if (session->account)
335 free(session->account);
336 session->account = strdup(optarg);
337 dbg("account = %s\n", session->account);
340 if (session->password)
341 free(session->password);
342 session->password = strdup(optarg);
343 dbg("password = %s\n", session->password);
360 if (!session->account) {
361 fprintf(stdout, "Enter twitter account: ");
362 session->account = get_string_from_stdin();
365 if (!session->password) {
366 fprintf(stdout, "Enter twitter password: ");
367 session->password = get_string_from_stdin();
370 /* get the current working directory basename */
371 if (strcmp(pwd, home) == 0)
374 dir = strrchr(pwd, '/');
381 tweet = get_string_from_stdin();
382 if (strlen(tweet) == 0) {
387 session->tweet = zalloc(strlen(tweet) + strlen(dir) + 10);
389 /* if --bash is specified, add the "PWD $ " to
390 * the start of the tweet. */
392 sprintf(session->tweet, "%s $ %s", dir, tweet);
394 sprintf(session->tweet, "%s", tweet);
397 dbg("account = %s\n", session->account);
398 dbg("password = %s\n", session->password);
399 dbg("tweet = %s\n", session->tweet);
401 /* fork ourself so that the main shell can get on
402 * with it's life as we try to connect and handle everything
406 dbg("child is %d\n", child);
410 retval = send_tweet(session);
411 if (retval && !session->bash) {
412 fprintf(stderr, "tweet failed\n");
416 session_free(session);