/[debian]/bti/trunk/bti.c
ViewVC logotype

Contents of /bti/trunk/bti.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1476 - (show annotations)
Thu Oct 23 22:26:37 2008 UTC (12 years, 1 month ago) by gregoa
File MIME type: text/plain
File size: 10221 byte(s)
[svn-inject] Forking bti source to Trunk
1 /*
2 * Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stddef.h>
21 #include <string.h>
22 #include <getopt.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <curl/curl.h>
29 #include "bti_version.h"
30
31
32 #define zalloc(size) calloc(size, 1)
33
34 #define dbg(format, arg...) \
35 do { \
36 if (debug) \
37 printf("%s: " format , __func__ , ## arg ); \
38 } while (0)
39
40
41 static int debug = 0;
42
43 enum host {
44 HOST_TWITTER = 0,
45 HOST_IDENTICA = 1,
46 };
47
48 struct session {
49 char *password;
50 char *account;
51 char *tweet;
52 int bash;
53 enum host host;
54 };
55
56 struct bti_curl_buffer {
57 char *data;
58 int length;
59 };
60
61 static void display_help(void)
62 {
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");
75 }
76
77 static void display_version(void)
78 {
79 fprintf(stdout, "bti - version %s\n", BTI_VERSION);
80 }
81
82 static char *get_string_from_stdin(void)
83 {
84 char *temp;
85 char *string;
86
87 string = zalloc(1000);
88 if (!string)
89 return NULL;
90
91 if (!fgets(string, 999, stdin))
92 return NULL;
93 temp = strchr(string, '\n');
94 *temp = '\0';
95 return string;
96 }
97
98 static struct session *session_alloc(void)
99 {
100 struct session *session;
101
102 session = zalloc(sizeof(*session));
103 if (!session)
104 return NULL;
105 return session;
106 }
107
108 static void session_free(struct session *session)
109 {
110 if (!session)
111 return;
112 free(session->password);
113 free(session->account);
114 free(session->tweet);
115 free(session);
116 }
117
118 static struct bti_curl_buffer *bti_curl_buffer_alloc(void)
119 {
120 struct bti_curl_buffer *buffer;
121
122 buffer = zalloc(sizeof(*buffer));
123 if (!buffer)
124 return NULL;
125
126 /* start out with a data buffer of 1 byte to
127 * make the buffer fill logic simpler */
128 buffer->data = zalloc(1);
129 if (!buffer->data) {
130 free(buffer);
131 return NULL;
132 }
133 buffer->length = 0;
134 return buffer;
135 }
136
137 static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
138 {
139 if (!buffer)
140 return;
141 free(buffer->data);
142 free(buffer);
143 }
144
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";
147
148 static CURL *curl_init(void)
149 {
150 CURL *curl;
151
152 curl = curl_easy_init();
153 if (!curl) {
154 fprintf(stderr, "Can not init CURL!\n");
155 return NULL;
156 }
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);
160 return curl;
161 }
162
163 size_t curl_callback(void *buffer, size_t size, size_t nmemb, void *userp)
164 {
165 struct bti_curl_buffer *curl_buf = userp;
166 size_t buffer_size = size * nmemb;
167 char *temp;
168
169 if ((!buffer) || (!buffer_size) || (!curl_buf))
170 return -EINVAL;
171
172 /* add to the data we already have */
173 temp = zalloc(curl_buf->length + buffer_size + 1);
174 if (!temp)
175 return -ENOMEM;
176
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;
182
183 dbg("%s\n", curl_buf->data);
184
185 return buffer_size;
186 }
187
188 static int send_tweet(struct session *session)
189 {
190 char user_password[500];
191 char data[500];
192 struct bti_curl_buffer *curl_buf;
193 CURL *curl = NULL;
194 CURLcode res;
195 struct curl_httppost *formpost = NULL;
196 struct curl_httppost *lastptr = NULL;
197
198 if (!session)
199 return -EINVAL;
200
201 curl_buf = bti_curl_buffer_alloc();
202 if (!curl_buf)
203 return -ENOMEM;
204
205 snprintf(user_password, sizeof(user_password), "%s:%s",
206 session->account, session->password);
207 snprintf(data, sizeof(data), "status=\"%s\"", session->tweet);
208
209 curl = curl_init();
210 if (!curl)
211 return -EINVAL;
212
213 curl_formadd(&formpost, &lastptr,
214 CURLFORM_COPYNAME, "status",
215 CURLFORM_COPYCONTENTS, session->tweet,
216 CURLFORM_END);
217
218 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
219
220 switch (session->host) {
221 case HOST_TWITTER:
222 curl_easy_setopt(curl, CURLOPT_URL, twitter_url);
223 break;
224 case HOST_IDENTICA:
225 curl_easy_setopt(curl, CURLOPT_URL, identica_url);
226 break;
227 }
228
229 if (debug)
230 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
231 curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
232
233 dbg("user_password = %s\n", user_password);
234 dbg("data = %s\n", data);
235
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);
241 return -EINVAL;
242 }
243
244 curl_easy_cleanup(curl);
245 curl_formfree(formpost);
246 bti_curl_buffer_free(curl_buf);
247 return 0;
248 }
249
250 static void parse_configfile(struct session *session)
251 {
252 FILE *config_file;
253 char *line = NULL;
254 size_t len = 0;
255 char *account = NULL;
256 char *password = NULL;
257 char *host = NULL;
258 char *file;
259 char *home = getenv("HOME");
260
261 /* config file is ~/.bti */
262 file = alloca(strlen(home) + 7);
263
264 sprintf(file, "%s/.bti", home);
265
266 config_file = fopen(file, "r");
267
268 /* No error if file does not exist or is unreadable. */
269 if (config_file == NULL)
270 return;
271
272 do {
273 ssize_t n = getline(&line, &len, config_file);
274 if (n < 0)
275 break;
276 if (line[n - 1] == '\n')
277 line[n - 1] = '\0';
278 /* Parse file. Format is the usual value pairs:
279 account=name
280 passwort=value
281 # is a comment character
282 */
283 *strchrnul(line, '#') = '\0';
284 char *c = line;
285 while (isspace(*c))
286 c++;
287 /* Ignore blank lines. */
288 if (c[0] == '\0')
289 continue;
290
291 if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
292 c += 8;
293 if (c[0] != '\0')
294 account = strdup(c);
295 } else if (!strncasecmp(c, "password", 8) &&
296 (c[8] == '=')) {
297 c += 9;
298 if (c[0] != '\0')
299 password = strdup(c);
300 } else if (!strncasecmp(c, "host", 4) &&
301 (c[4] == '=')) {
302 c += 5;
303 if (c[0] != '\0')
304 host = strdup(c);
305 }
306 } while (!feof(config_file));
307
308 if (password)
309 session->password = password;
310 if (account)
311 session->account = account;
312 if (host) {
313 if (strcasecmp(host, "twitter") == 0)
314 session->host = HOST_TWITTER;
315 if (strcasecmp(host, "identica") == 0)
316 session->host = HOST_IDENTICA;
317 free(host);
318 }
319
320 /* Free buffer and close file. */
321 free(line);
322 fclose(config_file);
323 }
324
325 int main(int argc, char *argv[], char *envp[])
326 {
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' },
335 { }
336 };
337 struct session *session;
338 pid_t child;
339 char *tweet;
340 int retval;
341 int option;
342 #if 0
343 char *home = getenv("HOME");
344 char *pwd = getenv("PWD");
345 char *dir;
346 #endif
347 session = session_alloc();
348 if (!session) {
349 fprintf(stderr, "no more memory...\n");
350 return -1;
351 }
352
353 curl_global_init(CURL_GLOBAL_ALL);
354 parse_configfile(session);
355
356 while (1) {
357 option = getopt_long_only(argc, argv, "dqe:p:H:a:h",
358 options, NULL);
359 if (option == -1)
360 break;
361 switch (option) {
362 case 'd':
363 debug = 1;
364 break;
365 case 'a':
366 if (session->account)
367 free(session->account);
368 session->account = strdup(optarg);
369 dbg("account = %s\n", session->account);
370 break;
371 case 'p':
372 if (session->password)
373 free(session->password);
374 session->password = strdup(optarg);
375 dbg("password = %s\n", session->password);
376 break;
377 case 'H':
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);
383 break;
384 case 'b':
385 session->bash= 1;
386 break;
387 case 'h':
388 display_help();
389 goto exit;
390 case 'v':
391 display_version();
392 goto exit;
393 default:
394 display_help();
395 goto exit;
396 }
397 }
398
399 if (!session->account) {
400 fprintf(stdout, "Enter twitter account: ");
401 session->account = get_string_from_stdin();
402 }
403
404 if (!session->password) {
405 fprintf(stdout, "Enter twitter password: ");
406 session->password = get_string_from_stdin();
407 }
408 #if 0
409 /* get the current working directory basename */
410 if (strcmp(pwd, home) == 0)
411 dir = "~";
412 else {
413 dir = strrchr(pwd, '/');
414 if (dir)
415 dir++;
416 else
417 dir = "?";
418 }
419 #endif
420 tweet = get_string_from_stdin();
421 if (strlen(tweet) == 0) {
422 dbg("no tweet?\n");
423 return -1;
424 }
425
426 // session->tweet = zalloc(strlen(tweet) + strlen(dir) + 10);
427 session->tweet = zalloc(strlen(tweet) + 10);
428
429 /* if --bash is specified, add the "PWD $ " to
430 * the start of the tweet. */
431 if (session->bash)
432 // sprintf(session->tweet, "%s $ %s", dir, tweet);
433 sprintf(session->tweet, "$ %s", tweet);
434 else
435 sprintf(session->tweet, "%s", tweet);
436 free(tweet);
437
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);
442
443 /* fork ourself so that the main shell can get on
444 * with it's life as we try to connect and handle everything
445 */
446 if (session->bash) {
447 child = fork();
448 if (child) {
449 dbg("child is %d\n", child);
450 exit(0);
451 }
452 }
453
454 retval = send_tweet(session);
455 if (retval && !session->bash) {
456 fprintf(stderr, "tweet failed\n");
457 return -1;
458 }
459
460 session_free(session);
461 exit:
462 return 0;
463 }

  ViewVC Help
Powered by ViewVC 1.1.26