1 |
/* |
2 |
* Copyright (C) 2008 Greg Kroah-Hartman <greg@kroah.com> |
3 |
* Copyright (C) 2009 Bart Trojanowski <bart@jukie.net> |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify it |
6 |
* under the terms of the GNU General Public License as published by the |
7 |
* Free Software Foundation version 2 of the License. |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, but |
10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
* General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License along |
15 |
* with this program; if not, write to the Free Software Foundation, Inc., |
16 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 |
*/ |
18 |
|
19 |
#define _GNU_SOURCE |
20 |
|
21 |
#include <stdio.h> |
22 |
#include <stdlib.h> |
23 |
#include <stddef.h> |
24 |
#include <string.h> |
25 |
#include <getopt.h> |
26 |
#include <errno.h> |
27 |
#include <ctype.h> |
28 |
#include <fcntl.h> |
29 |
#include <unistd.h> |
30 |
#include <time.h> |
31 |
#include <sys/stat.h> |
32 |
#include <sys/types.h> |
33 |
#include <sys/wait.h> |
34 |
#include <curl/curl.h> |
35 |
#include <readline/readline.h> |
36 |
#include <libxml/xmlmemory.h> |
37 |
#include <libxml/parser.h> |
38 |
#include <libxml/tree.h> |
39 |
#include <pcre.h> |
40 |
|
41 |
|
42 |
#define zalloc(size) calloc(size, 1) |
43 |
|
44 |
#define dbg(format, arg...) \ |
45 |
do { \ |
46 |
if (debug) \ |
47 |
fprintf(stdout, "bti: %s: " format , __func__ , \ |
48 |
## arg); \ |
49 |
} while (0) |
50 |
|
51 |
|
52 |
static int debug; |
53 |
|
54 |
enum host { |
55 |
HOST_TWITTER = 0, |
56 |
HOST_IDENTICA = 1, |
57 |
}; |
58 |
|
59 |
enum action { |
60 |
ACTION_UPDATE = 0, |
61 |
ACTION_FRIENDS = 1, |
62 |
ACTION_USER = 2, |
63 |
ACTION_REPLIES = 4, |
64 |
ACTION_PUBLIC = 8, |
65 |
ACTION_UNKNOWN = 16 |
66 |
}; |
67 |
|
68 |
struct session { |
69 |
char *password; |
70 |
char *account; |
71 |
char *tweet; |
72 |
char *proxy; |
73 |
char *time; |
74 |
char *homedir; |
75 |
char *logfile; |
76 |
char *user; |
77 |
int bash; |
78 |
int shrink_urls; |
79 |
int dry_run; |
80 |
int page; |
81 |
enum host host; |
82 |
enum action action; |
83 |
}; |
84 |
|
85 |
struct bti_curl_buffer { |
86 |
char *data; |
87 |
enum action action; |
88 |
int length; |
89 |
}; |
90 |
|
91 |
static void display_help(void) |
92 |
{ |
93 |
fprintf(stdout, "bti - send tweet to twitter or identi.ca\n"); |
94 |
fprintf(stdout, "Version: " VERSION "\n"); |
95 |
fprintf(stdout, "Usage:\n"); |
96 |
fprintf(stdout, " bti [options]\n"); |
97 |
fprintf(stdout, "options are:\n"); |
98 |
fprintf(stdout, " --account accountname\n"); |
99 |
fprintf(stdout, " --password password\n"); |
100 |
fprintf(stdout, " --action action\n"); |
101 |
fprintf(stdout, " ('update', 'friends', 'public', 'replies' " |
102 |
"or 'user')\n"); |
103 |
fprintf(stdout, " --user screenname\n"); |
104 |
fprintf(stdout, " --proxy PROXY:PORT\n"); |
105 |
fprintf(stdout, " --host HOST\n"); |
106 |
fprintf(stdout, " --logfile logfile\n"); |
107 |
fprintf(stdout, " --shrink-urls\n"); |
108 |
fprintf(stdout, " --page PAGENUMBER\n"); |
109 |
fprintf(stdout, " --bash\n"); |
110 |
fprintf(stdout, " --debug\n"); |
111 |
fprintf(stdout, " --dry-run\n"); |
112 |
fprintf(stdout, " --version\n"); |
113 |
fprintf(stdout, " --help\n"); |
114 |
} |
115 |
|
116 |
static void display_version(void) |
117 |
{ |
118 |
fprintf(stdout, "bti - version %s\n", VERSION); |
119 |
} |
120 |
|
121 |
static struct session *session_alloc(void) |
122 |
{ |
123 |
struct session *session; |
124 |
|
125 |
session = zalloc(sizeof(*session)); |
126 |
if (!session) |
127 |
return NULL; |
128 |
return session; |
129 |
} |
130 |
|
131 |
static void session_free(struct session *session) |
132 |
{ |
133 |
if (!session) |
134 |
return; |
135 |
free(session->password); |
136 |
free(session->account); |
137 |
free(session->tweet); |
138 |
free(session->proxy); |
139 |
free(session->time); |
140 |
free(session->homedir); |
141 |
free(session->user); |
142 |
free(session); |
143 |
} |
144 |
|
145 |
static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action) |
146 |
{ |
147 |
struct bti_curl_buffer *buffer; |
148 |
|
149 |
buffer = zalloc(sizeof(*buffer)); |
150 |
if (!buffer) |
151 |
return NULL; |
152 |
|
153 |
/* start out with a data buffer of 1 byte to |
154 |
* make the buffer fill logic simpler */ |
155 |
buffer->data = zalloc(1); |
156 |
if (!buffer->data) { |
157 |
free(buffer); |
158 |
return NULL; |
159 |
} |
160 |
buffer->length = 0; |
161 |
buffer->action = action; |
162 |
return buffer; |
163 |
} |
164 |
|
165 |
static void bti_curl_buffer_free(struct bti_curl_buffer *buffer) |
166 |
{ |
167 |
if (!buffer) |
168 |
return; |
169 |
free(buffer->data); |
170 |
free(buffer); |
171 |
} |
172 |
|
173 |
static const char *twitter_user_url = "http://twitter.com/statuses/user_timeline/"; |
174 |
static const char *twitter_update_url = "https://twitter.com/statuses/update.xml"; |
175 |
static const char *twitter_public_url = "http://twitter.com/statuses/public_timeline.xml"; |
176 |
static const char *twitter_friends_url = "https://twitter.com/statuses/friends_timeline.xml"; |
177 |
static const char *twitter_replies_url = "http://twitter.com/statuses/replies.xml"; |
178 |
|
179 |
static const char *identica_user_url = "http://identi.ca/api/statuses/user_timeline/"; |
180 |
static const char *identica_update_url = "http://identi.ca/api/statuses/update.xml"; |
181 |
static const char *identica_public_url = "http://identi.ca/api/statuses/public_timeline.xml"; |
182 |
static const char *identica_friends_url = "http://identi.ca/api/statuses/friends_timeline.xml"; |
183 |
static const char *identica_replies_url = "http://identi.ca/api/statuses/replies.xml"; |
184 |
|
185 |
static CURL *curl_init(void) |
186 |
{ |
187 |
CURL *curl; |
188 |
|
189 |
curl = curl_easy_init(); |
190 |
if (!curl) { |
191 |
fprintf(stderr, "Can not init CURL!\n"); |
192 |
return NULL; |
193 |
} |
194 |
/* some ssl sanity checks on the connection we are making */ |
195 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); |
196 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); |
197 |
return curl; |
198 |
} |
199 |
|
200 |
static void parse_statuses(xmlDocPtr doc, xmlNodePtr current) |
201 |
{ |
202 |
xmlChar *text = NULL; |
203 |
xmlChar *user = NULL; |
204 |
xmlNodePtr userinfo; |
205 |
|
206 |
current = current->xmlChildrenNode; |
207 |
while (current != NULL) { |
208 |
if (current->type == XML_ELEMENT_NODE) { |
209 |
if (!xmlStrcmp(current->name, (const xmlChar *)"text")) |
210 |
text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1); |
211 |
if (!xmlStrcmp(current->name, (const xmlChar *)"user")) { |
212 |
userinfo = current->xmlChildrenNode; |
213 |
while (userinfo != NULL) { |
214 |
if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) { |
215 |
if (user) |
216 |
xmlFree(user); |
217 |
user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1); |
218 |
} |
219 |
userinfo = userinfo->next; |
220 |
} |
221 |
} |
222 |
if (user && text) { |
223 |
printf("[%s] %s\n", user, text); |
224 |
xmlFree(user); |
225 |
xmlFree(text); |
226 |
user = NULL; |
227 |
text = NULL; |
228 |
} |
229 |
} |
230 |
current = current->next; |
231 |
} |
232 |
|
233 |
return; |
234 |
} |
235 |
|
236 |
static void parse_timeline(char *document) |
237 |
{ |
238 |
xmlDocPtr doc; |
239 |
xmlNodePtr current; |
240 |
|
241 |
doc = xmlReadMemory(document, strlen(document), "timeline.xml", |
242 |
NULL, XML_PARSE_NOERROR); |
243 |
if (doc == NULL) |
244 |
return; |
245 |
|
246 |
current = xmlDocGetRootElement(doc); |
247 |
if (current == NULL) { |
248 |
fprintf(stderr, "empty document\n"); |
249 |
xmlFreeDoc(doc); |
250 |
return; |
251 |
} |
252 |
|
253 |
if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) { |
254 |
fprintf(stderr, "unexpected document type\n"); |
255 |
xmlFreeDoc(doc); |
256 |
return; |
257 |
} |
258 |
|
259 |
current = current->xmlChildrenNode; |
260 |
while (current != NULL) { |
261 |
if ((!xmlStrcmp(current->name, (const xmlChar *)"status"))) |
262 |
parse_statuses(doc, current); |
263 |
current = current->next; |
264 |
} |
265 |
xmlFreeDoc(doc); |
266 |
|
267 |
return; |
268 |
} |
269 |
|
270 |
static size_t curl_callback(void *buffer, size_t size, size_t nmemb, |
271 |
void *userp) |
272 |
{ |
273 |
struct bti_curl_buffer *curl_buf = userp; |
274 |
size_t buffer_size = size * nmemb; |
275 |
char *temp; |
276 |
|
277 |
if ((!buffer) || (!buffer_size) || (!curl_buf)) |
278 |
return -EINVAL; |
279 |
|
280 |
/* add to the data we already have */ |
281 |
temp = zalloc(curl_buf->length + buffer_size + 1); |
282 |
if (!temp) |
283 |
return -ENOMEM; |
284 |
|
285 |
memcpy(temp, curl_buf->data, curl_buf->length); |
286 |
free(curl_buf->data); |
287 |
curl_buf->data = temp; |
288 |
memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size); |
289 |
curl_buf->length += buffer_size; |
290 |
if (curl_buf->action) |
291 |
parse_timeline(curl_buf->data); |
292 |
|
293 |
dbg("%s\n", curl_buf->data); |
294 |
|
295 |
return buffer_size; |
296 |
} |
297 |
|
298 |
static int send_request(struct session *session) |
299 |
{ |
300 |
char user_password[500]; |
301 |
char data[500]; |
302 |
/* is there usernames longer than 22 chars? */ |
303 |
char user_url[70]; |
304 |
struct bti_curl_buffer *curl_buf; |
305 |
CURL *curl = NULL; |
306 |
CURLcode res; |
307 |
struct curl_httppost *formpost = NULL; |
308 |
struct curl_httppost *lastptr = NULL; |
309 |
struct curl_slist *slist = NULL; |
310 |
|
311 |
if (!session) |
312 |
return -EINVAL; |
313 |
|
314 |
curl_buf = bti_curl_buffer_alloc(session->action); |
315 |
if (!curl_buf) |
316 |
return -ENOMEM; |
317 |
|
318 |
curl = curl_init(); |
319 |
if (!curl) |
320 |
return -EINVAL; |
321 |
|
322 |
switch (session->action) { |
323 |
case ACTION_UPDATE: |
324 |
snprintf(user_password, sizeof(user_password), "%s:%s", |
325 |
session->account, session->password); |
326 |
snprintf(data, sizeof(data), "status=\"%s\"", session->tweet); |
327 |
curl_formadd(&formpost, &lastptr, |
328 |
CURLFORM_COPYNAME, "status", |
329 |
CURLFORM_COPYCONTENTS, session->tweet, |
330 |
CURLFORM_END); |
331 |
|
332 |
curl_formadd(&formpost, &lastptr, |
333 |
CURLFORM_COPYNAME, "source", |
334 |
CURLFORM_COPYCONTENTS, "bti", |
335 |
CURLFORM_END); |
336 |
|
337 |
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); |
338 |
slist = curl_slist_append(slist, "Expect:"); |
339 |
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); |
340 |
switch (session->host) { |
341 |
case HOST_TWITTER: |
342 |
curl_easy_setopt(curl, CURLOPT_URL, |
343 |
twitter_update_url); |
344 |
break; |
345 |
case HOST_IDENTICA: |
346 |
curl_easy_setopt(curl, CURLOPT_URL, |
347 |
identica_update_url); |
348 |
break; |
349 |
} |
350 |
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); |
351 |
|
352 |
break; |
353 |
case ACTION_FRIENDS: |
354 |
snprintf(user_password, sizeof(user_password), "%s:%s", |
355 |
session->account, session->password); |
356 |
switch (session->host) { |
357 |
case HOST_TWITTER: |
358 |
sprintf(user_url, "%s?page=%d", twitter_friends_url, session->page); |
359 |
curl_easy_setopt(curl, CURLOPT_URL, user_url); |
360 |
break; |
361 |
case HOST_IDENTICA: |
362 |
sprintf(user_url, "%s?page=%d", identica_friends_url, session->page); |
363 |
curl_easy_setopt(curl, CURLOPT_URL, user_url); |
364 |
break; |
365 |
} |
366 |
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); |
367 |
|
368 |
break; |
369 |
case ACTION_USER: |
370 |
switch (session->host) { |
371 |
case HOST_TWITTER: |
372 |
sprintf(user_url, "%s%s.xml?page=%d", twitter_user_url, session->user, session->page); |
373 |
curl_easy_setopt(curl, CURLOPT_URL, user_url); |
374 |
break; |
375 |
case HOST_IDENTICA: |
376 |
sprintf(user_url, "%s%s.xml?page=%d", identica_user_url, session->user, session->page); |
377 |
curl_easy_setopt(curl, CURLOPT_URL, user_url); |
378 |
break; |
379 |
} |
380 |
|
381 |
break; |
382 |
case ACTION_REPLIES: |
383 |
snprintf(user_password, sizeof(user_password), "%s:%s", |
384 |
session->account, session->password); |
385 |
switch (session->host) { |
386 |
case HOST_TWITTER: |
387 |
sprintf(user_url, "%s?page=%d", twitter_replies_url, session->page); |
388 |
curl_easy_setopt(curl, CURLOPT_URL, user_url); |
389 |
break; |
390 |
case HOST_IDENTICA: |
391 |
sprintf(user_url, "%s?page=%d", identica_replies_url, session->page); |
392 |
curl_easy_setopt(curl, CURLOPT_URL, user_url); |
393 |
break; |
394 |
} |
395 |
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); |
396 |
|
397 |
break; |
398 |
case ACTION_PUBLIC: |
399 |
switch (session->host) { |
400 |
case HOST_TWITTER: |
401 |
sprintf(user_url, "%s?page=%d", twitter_public_url, session->page); |
402 |
curl_easy_setopt(curl, CURLOPT_URL, user_url); |
403 |
break; |
404 |
case HOST_IDENTICA: |
405 |
sprintf(user_url, "%s?page=%d", identica_public_url, session->page); |
406 |
curl_easy_setopt(curl, CURLOPT_URL, user_url); |
407 |
break; |
408 |
} |
409 |
|
410 |
break; |
411 |
default: |
412 |
break; |
413 |
} |
414 |
|
415 |
if (session->proxy) |
416 |
curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy); |
417 |
|
418 |
if (debug) |
419 |
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); |
420 |
|
421 |
dbg("user_password = %s\n", user_password); |
422 |
dbg("data = %s\n", data); |
423 |
dbg("proxy = %s\n", session->proxy); |
424 |
|
425 |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback); |
426 |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf); |
427 |
if (!session->dry_run) { |
428 |
res = curl_easy_perform(curl); |
429 |
if (res && !session->bash) { |
430 |
fprintf(stderr, "error(%d) trying to perform " |
431 |
"operation\n", res); |
432 |
return -EINVAL; |
433 |
} |
434 |
} |
435 |
|
436 |
curl_easy_cleanup(curl); |
437 |
if (session->action == ACTION_UPDATE) |
438 |
curl_formfree(formpost); |
439 |
bti_curl_buffer_free(curl_buf); |
440 |
return 0; |
441 |
} |
442 |
|
443 |
static void parse_configfile(struct session *session) |
444 |
{ |
445 |
FILE *config_file; |
446 |
char *line = NULL; |
447 |
size_t len = 0; |
448 |
char *account = NULL; |
449 |
char *password = NULL; |
450 |
char *host = NULL; |
451 |
char *proxy = NULL; |
452 |
char *logfile = NULL; |
453 |
char *action = NULL; |
454 |
char *user = NULL; |
455 |
char *file; |
456 |
int shrink_urls = 0; |
457 |
|
458 |
/* config file is ~/.bti */ |
459 |
file = alloca(strlen(session->homedir) + 7); |
460 |
|
461 |
sprintf(file, "%s/.bti", session->homedir); |
462 |
|
463 |
config_file = fopen(file, "r"); |
464 |
|
465 |
/* No error if file does not exist or is unreadable. */ |
466 |
if (config_file == NULL) |
467 |
return; |
468 |
|
469 |
do { |
470 |
ssize_t n = getline(&line, &len, config_file); |
471 |
if (n < 0) |
472 |
break; |
473 |
if (line[n - 1] == '\n') |
474 |
line[n - 1] = '\0'; |
475 |
/* Parse file. Format is the usual value pairs: |
476 |
account=name |
477 |
passwort=value |
478 |
# is a comment character |
479 |
*/ |
480 |
*strchrnul(line, '#') = '\0'; |
481 |
char *c = line; |
482 |
while (isspace(*c)) |
483 |
c++; |
484 |
/* Ignore blank lines. */ |
485 |
if (c[0] == '\0') |
486 |
continue; |
487 |
|
488 |
if (!strncasecmp(c, "account", 7) && (c[7] == '=')) { |
489 |
c += 8; |
490 |
if (c[0] != '\0') |
491 |
account = strdup(c); |
492 |
} else if (!strncasecmp(c, "password", 8) && |
493 |
(c[8] == '=')) { |
494 |
c += 9; |
495 |
if (c[0] != '\0') |
496 |
password = strdup(c); |
497 |
} else if (!strncasecmp(c, "host", 4) && |
498 |
(c[4] == '=')) { |
499 |
c += 5; |
500 |
if (c[0] != '\0') |
501 |
host = strdup(c); |
502 |
} else if (!strncasecmp(c, "proxy", 5) && |
503 |
(c[5] == '=')) { |
504 |
c += 6; |
505 |
if (c[0] != '\0') |
506 |
proxy = strdup(c); |
507 |
} else if (!strncasecmp(c, "logfile", 7) && |
508 |
(c[7] == '=')) { |
509 |
c += 8; |
510 |
if (c[0] != '\0') |
511 |
logfile = strdup(c); |
512 |
} else if (!strncasecmp(c, "action", 6) && |
513 |
(c[6] == '=')) { |
514 |
c += 7; |
515 |
if (c[0] != '\0') |
516 |
action = strdup(c); |
517 |
} else if (!strncasecmp(c, "user", 4) && |
518 |
(c[4] == '=')) { |
519 |
c += 5; |
520 |
if (c[0] != '\0') |
521 |
user = strdup(c); |
522 |
} else if (!strncasecmp(c, "shrink-urls", 11) && |
523 |
(c[11] == '=')) { |
524 |
c += 12; |
525 |
if (!strncasecmp(c, "true", 4) || |
526 |
!strncasecmp(c, "yes", 3)) |
527 |
shrink_urls = 1; |
528 |
} |
529 |
} while (!feof(config_file)); |
530 |
|
531 |
if (password) |
532 |
session->password = password; |
533 |
if (account) |
534 |
session->account = account; |
535 |
if (host) { |
536 |
if (strcasecmp(host, "twitter") == 0) |
537 |
session->host = HOST_TWITTER; |
538 |
if (strcasecmp(host, "identica") == 0) |
539 |
session->host = HOST_IDENTICA; |
540 |
free(host); |
541 |
} |
542 |
if (proxy) { |
543 |
if (session->proxy) |
544 |
free(session->proxy); |
545 |
session->proxy = proxy; |
546 |
} |
547 |
if (logfile) |
548 |
session->logfile = logfile; |
549 |
if (action) { |
550 |
if (strcasecmp(action, "update") == 0) |
551 |
session->action = ACTION_UPDATE; |
552 |
else if (strcasecmp(action, "friends") == 0) |
553 |
session->action = ACTION_FRIENDS; |
554 |
else if (strcasecmp(action, "user") == 0) |
555 |
session->action = ACTION_USER; |
556 |
else if (strcasecmp(action, "replies") == 0) |
557 |
session->action = ACTION_REPLIES; |
558 |
else if (strcasecmp(action, "public") == 0) |
559 |
session->action = ACTION_PUBLIC; |
560 |
else |
561 |
session->action = ACTION_UNKNOWN; |
562 |
free(action); |
563 |
} |
564 |
if (user) |
565 |
session->user = user; |
566 |
session->shrink_urls = shrink_urls; |
567 |
|
568 |
/* Free buffer and close file. */ |
569 |
free(line); |
570 |
fclose(config_file); |
571 |
} |
572 |
|
573 |
static void log_session(struct session *session, int retval) |
574 |
{ |
575 |
FILE *log_file; |
576 |
char *filename; |
577 |
char *host; |
578 |
|
579 |
/* Only log something if we have a log file set */ |
580 |
if (!session->logfile) |
581 |
return; |
582 |
|
583 |
filename = alloca(strlen(session->homedir) + |
584 |
strlen(session->logfile) + 3); |
585 |
|
586 |
sprintf(filename, "%s/%s", session->homedir, session->logfile); |
587 |
|
588 |
log_file = fopen(filename, "a+"); |
589 |
if (log_file == NULL) |
590 |
return; |
591 |
switch (session->host) { |
592 |
case HOST_TWITTER: |
593 |
host = "twitter"; |
594 |
break; |
595 |
case HOST_IDENTICA: |
596 |
host = "identi.ca"; |
597 |
break; |
598 |
default: |
599 |
host = "unknown"; |
600 |
break; |
601 |
} |
602 |
|
603 |
switch (session->action) { |
604 |
case ACTION_UPDATE: |
605 |
if (retval) |
606 |
fprintf(log_file, "%s: host=%s tweet failed\n", |
607 |
session->time, host); |
608 |
else |
609 |
fprintf(log_file, "%s: host=%s tweet=%s\n", |
610 |
session->time, host, session->tweet); |
611 |
break; |
612 |
case ACTION_FRIENDS: |
613 |
fprintf(log_file, "%s: host=%s retrieving friends timeline\n", |
614 |
session->time, host); |
615 |
break; |
616 |
case ACTION_USER: |
617 |
fprintf(log_file, "%s: host=%s retrieving %s's timeline\n", |
618 |
session->time, host, session->user); |
619 |
break; |
620 |
case ACTION_REPLIES: |
621 |
fprintf(log_file, "%s: host=%s retrieving replies\n", |
622 |
session->time, host); |
623 |
break; |
624 |
case ACTION_PUBLIC: |
625 |
fprintf(log_file, "%s: host=%s retrieving public timeline\n", |
626 |
session->time, host); |
627 |
break; |
628 |
default: |
629 |
break; |
630 |
} |
631 |
|
632 |
fclose(log_file); |
633 |
} |
634 |
|
635 |
static char *get_string_from_stdin(void) |
636 |
{ |
637 |
char *temp; |
638 |
char *string; |
639 |
|
640 |
string = zalloc(1000); |
641 |
if (!string) |
642 |
return NULL; |
643 |
|
644 |
if (!fgets(string, 999, stdin)) |
645 |
return NULL; |
646 |
temp = strchr(string, '\n'); |
647 |
*temp = '\0'; |
648 |
return string; |
649 |
} |
650 |
|
651 |
static int find_urls(const char *tweet, int **pranges) |
652 |
{ |
653 |
/* |
654 |
* magic obtained from |
655 |
* http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html |
656 |
*/ |
657 |
static const char *re_magic = |
658 |
"(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}" |
659 |
"[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)" |
660 |
"(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?"; |
661 |
pcre *re; |
662 |
const char *errptr; |
663 |
int erroffset; |
664 |
int ovector[10] = {0,}; |
665 |
const size_t ovsize = sizeof(ovector)/sizeof(*ovector); |
666 |
int startoffset, tweetlen; |
667 |
int i, rc; |
668 |
int rbound = 10; |
669 |
int rcount = 0; |
670 |
int *ranges = malloc(sizeof(int) * rbound); |
671 |
|
672 |
re = pcre_compile(re_magic, |
673 |
PCRE_NO_AUTO_CAPTURE, |
674 |
&errptr, &erroffset, NULL); |
675 |
if (!re) { |
676 |
fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr); |
677 |
exit(1); |
678 |
} |
679 |
|
680 |
tweetlen = strlen(tweet); |
681 |
for (startoffset = 0; startoffset < tweetlen; ) { |
682 |
|
683 |
rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0, |
684 |
ovector, ovsize); |
685 |
if (rc == PCRE_ERROR_NOMATCH) |
686 |
break; |
687 |
|
688 |
if (rc < 0) { |
689 |
fprintf(stderr, "pcre_exec @%u: %s\n", |
690 |
erroffset, errptr); |
691 |
exit(1); |
692 |
} |
693 |
|
694 |
for (i = 0; i < rc; i += 2) { |
695 |
if ((rcount+2) == rbound) { |
696 |
rbound *= 2; |
697 |
ranges = realloc(ranges, sizeof(int) * rbound); |
698 |
} |
699 |
|
700 |
ranges[rcount++] = ovector[i]; |
701 |
ranges[rcount++] = ovector[i+1]; |
702 |
} |
703 |
|
704 |
startoffset = ovector[1]; |
705 |
} |
706 |
|
707 |
pcre_free(re); |
708 |
|
709 |
*pranges = ranges; |
710 |
return rcount; |
711 |
} |
712 |
|
713 |
/** |
714 |
* bidirectional popen() call |
715 |
* |
716 |
* @param rwepipe - int array of size three |
717 |
* @param exe - program to run |
718 |
* @param argv - argument list |
719 |
* @return pid or -1 on error |
720 |
* |
721 |
* The caller passes in an array of three integers (rwepipe), on successful |
722 |
* execution it can then write to element 0 (stdin of exe), and read from |
723 |
* element 1 (stdout) and 2 (stderr). |
724 |
*/ |
725 |
static int popenRWE(int *rwepipe, const char *exe, const char *const argv[]) |
726 |
{ |
727 |
int in[2]; |
728 |
int out[2]; |
729 |
int err[2]; |
730 |
int pid; |
731 |
int rc; |
732 |
|
733 |
rc = pipe(in); |
734 |
if (rc < 0) |
735 |
goto error_in; |
736 |
|
737 |
rc = pipe(out); |
738 |
if (rc < 0) |
739 |
goto error_out; |
740 |
|
741 |
rc = pipe(err); |
742 |
if (rc < 0) |
743 |
goto error_err; |
744 |
|
745 |
pid = fork(); |
746 |
if (pid > 0) { |
747 |
/* parent */ |
748 |
close(in[0]); |
749 |
close(out[1]); |
750 |
close(err[1]); |
751 |
rwepipe[0] = in[1]; |
752 |
rwepipe[1] = out[0]; |
753 |
rwepipe[2] = err[0]; |
754 |
return pid; |
755 |
} else if (pid == 0) { |
756 |
/* child */ |
757 |
close(in[1]); |
758 |
close(out[0]); |
759 |
close(err[0]); |
760 |
close(0); |
761 |
rc = dup(in[0]); |
762 |
close(1); |
763 |
rc = dup(out[1]); |
764 |
close(2); |
765 |
rc = dup(err[1]); |
766 |
|
767 |
execvp(exe, (char **)argv); |
768 |
exit(1); |
769 |
} else |
770 |
goto error_fork; |
771 |
|
772 |
return pid; |
773 |
|
774 |
error_fork: |
775 |
close(err[0]); |
776 |
close(err[1]); |
777 |
error_err: |
778 |
close(out[0]); |
779 |
close(out[1]); |
780 |
error_out: |
781 |
close(in[0]); |
782 |
close(in[1]); |
783 |
error_in: |
784 |
return -1; |
785 |
} |
786 |
|
787 |
static int pcloseRWE(int pid, int *rwepipe) |
788 |
{ |
789 |
int rc, status; |
790 |
close(rwepipe[0]); |
791 |
close(rwepipe[1]); |
792 |
close(rwepipe[2]); |
793 |
rc = waitpid(pid, &status, 0); |
794 |
return status; |
795 |
} |
796 |
|
797 |
static char *shrink_one_url(int *rwepipe, char *big) |
798 |
{ |
799 |
int biglen = strlen(big); |
800 |
char *small; |
801 |
int smalllen; |
802 |
int rc; |
803 |
|
804 |
rc = dprintf(rwepipe[0], "%s\n", big); |
805 |
if (rc < 0) |
806 |
return big; |
807 |
|
808 |
smalllen = biglen + 128; |
809 |
small = malloc(smalllen); |
810 |
if (!small) |
811 |
return big; |
812 |
|
813 |
rc = read(rwepipe[1], small, smalllen); |
814 |
if (rc < 0 || rc > biglen) |
815 |
goto error_free_small; |
816 |
|
817 |
if (strncmp(small, "http://", 7)) |
818 |
goto error_free_small; |
819 |
|
820 |
smalllen = rc; |
821 |
while (smalllen && isspace(small[smalllen-1])) |
822 |
small[--smalllen] = 0; |
823 |
|
824 |
free(big); |
825 |
return small; |
826 |
|
827 |
error_free_small: |
828 |
free(small); |
829 |
return big; |
830 |
} |
831 |
|
832 |
static char *shrink_urls(char *text) |
833 |
{ |
834 |
int *ranges; |
835 |
int rcount; |
836 |
int i; |
837 |
int inofs = 0; |
838 |
int outofs = 0; |
839 |
const char *const shrink_args[] = { |
840 |
"bti-shrink-urls", |
841 |
NULL |
842 |
}; |
843 |
int shrink_pid; |
844 |
int shrink_pipe[3]; |
845 |
int inlen = strlen(text); |
846 |
|
847 |
dbg("before len=%u\n", inlen); |
848 |
|
849 |
shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args); |
850 |
if (shrink_pid < 0) |
851 |
return text; |
852 |
|
853 |
rcount = find_urls(text, &ranges); |
854 |
if (!rcount) |
855 |
return text; |
856 |
|
857 |
for (i = 0; i < rcount; i += 2) { |
858 |
int url_start = ranges[i]; |
859 |
int url_end = ranges[i+1]; |
860 |
int long_url_len = url_end - url_start; |
861 |
char *url = strndup(text + url_start, long_url_len); |
862 |
int short_url_len; |
863 |
int not_url_len = url_start - inofs; |
864 |
|
865 |
dbg("long url[%u]: %s\n", long_url_len, url); |
866 |
url = shrink_one_url(shrink_pipe, url); |
867 |
short_url_len = url ? strlen(url) : 0; |
868 |
dbg("short url[%u]: %s\n", short_url_len, url); |
869 |
|
870 |
if (!url || short_url_len >= long_url_len) { |
871 |
/* The short url ended up being too long |
872 |
* or unavailable */ |
873 |
if (inofs) { |
874 |
strncpy(text + outofs, text + inofs, |
875 |
not_url_len + long_url_len); |
876 |
} |
877 |
inofs += not_url_len + long_url_len; |
878 |
outofs += not_url_len + long_url_len; |
879 |
|
880 |
} else { |
881 |
/* copy the unmodified block */ |
882 |
strncpy(text + outofs, text + inofs, not_url_len); |
883 |
inofs += not_url_len; |
884 |
outofs += not_url_len; |
885 |
|
886 |
/* copy the new url */ |
887 |
strncpy(text + outofs, url, short_url_len); |
888 |
inofs += long_url_len; |
889 |
outofs += short_url_len; |
890 |
} |
891 |
|
892 |
free(url); |
893 |
} |
894 |
|
895 |
/* copy the last block after the last match */ |
896 |
if (inofs) { |
897 |
int tail = inlen - inofs; |
898 |
if (tail) { |
899 |
strncpy(text + outofs, text + inofs, tail); |
900 |
outofs += tail; |
901 |
} |
902 |
} |
903 |
|
904 |
free(ranges); |
905 |
|
906 |
(void)pcloseRWE(shrink_pid, shrink_pipe); |
907 |
|
908 |
text[outofs] = 0; |
909 |
dbg("after len=%u\n", outofs); |
910 |
return text; |
911 |
} |
912 |
|
913 |
int main(int argc, char *argv[], char *envp[]) |
914 |
{ |
915 |
static const struct option options[] = { |
916 |
{ "debug", 0, NULL, 'd' }, |
917 |
{ "account", 1, NULL, 'a' }, |
918 |
{ "password", 1, NULL, 'p' }, |
919 |
{ "host", 1, NULL, 'H' }, |
920 |
{ "proxy", 1, NULL, 'P' }, |
921 |
{ "action", 1, NULL, 'A' }, |
922 |
{ "user", 1, NULL, 'u' }, |
923 |
{ "logfile", 1, NULL, 'L' }, |
924 |
{ "shrink-urls", 0, NULL, 's' }, |
925 |
{ "help", 0, NULL, 'h' }, |
926 |
{ "bash", 0, NULL, 'b' }, |
927 |
{ "dry-run", 0, NULL, 'n' }, |
928 |
{ "page", 1, NULL, 'g' }, |
929 |
{ "version", 0, NULL, 'v' }, |
930 |
{ } |
931 |
}; |
932 |
struct session *session; |
933 |
pid_t child; |
934 |
char *tweet; |
935 |
int retval = 0; |
936 |
int option; |
937 |
char *http_proxy; |
938 |
time_t t; |
939 |
int page_nr; |
940 |
|
941 |
debug = 0; |
942 |
rl_bind_key('\t', rl_insert); |
943 |
|
944 |
session = session_alloc(); |
945 |
if (!session) { |
946 |
fprintf(stderr, "no more memory...\n"); |
947 |
return -1; |
948 |
} |
949 |
|
950 |
/* get the current time so that we can log it later */ |
951 |
time(&t); |
952 |
session->time = strdup(ctime(&t)); |
953 |
session->time[strlen(session->time)-1] = 0x00; |
954 |
|
955 |
session->homedir = strdup(getenv("HOME")); |
956 |
|
957 |
curl_global_init(CURL_GLOBAL_ALL); |
958 |
|
959 |
/* Set environment variables first, before reading command line options |
960 |
* or config file values. */ |
961 |
http_proxy = getenv("http_proxy"); |
962 |
if (http_proxy) { |
963 |
if (session->proxy) |
964 |
free(session->proxy); |
965 |
session->proxy = strdup(http_proxy); |
966 |
dbg("http_proxy = %s\n", session->proxy); |
967 |
} |
968 |
|
969 |
parse_configfile(session); |
970 |
|
971 |
while (1) { |
972 |
option = getopt_long_only(argc, argv, "dqe:p:P:H:a:A:u:hg:sn", |
973 |
options, NULL); |
974 |
if (option == -1) |
975 |
break; |
976 |
switch (option) { |
977 |
case 'd': |
978 |
debug = 1; |
979 |
break; |
980 |
case 'a': |
981 |
if (session->account) |
982 |
free(session->account); |
983 |
session->account = strdup(optarg); |
984 |
dbg("account = %s\n", session->account); |
985 |
break; |
986 |
case 'g': |
987 |
page_nr = atoi(optarg); |
988 |
dbg("page = %d\n", page_nr); |
989 |
session->page = page_nr; |
990 |
break; |
991 |
case 'p': |
992 |
if (session->password) |
993 |
free(session->password); |
994 |
session->password = strdup(optarg); |
995 |
dbg("password = %s\n", session->password); |
996 |
break; |
997 |
case 'P': |
998 |
if (session->proxy) |
999 |
free(session->proxy); |
1000 |
session->proxy = strdup(optarg); |
1001 |
dbg("proxy = %s\n", session->proxy); |
1002 |
break; |
1003 |
case 'A': |
1004 |
if (strcasecmp(optarg, "update") == 0) |
1005 |
session->action = ACTION_UPDATE; |
1006 |
else if (strcasecmp(optarg, "friends") == 0) |
1007 |
session->action = ACTION_FRIENDS; |
1008 |
else if (strcasecmp(optarg, "user") == 0) |
1009 |
session->action = ACTION_USER; |
1010 |
else if (strcasecmp(optarg, "replies") == 0) |
1011 |
session->action = ACTION_REPLIES; |
1012 |
else if (strcasecmp(optarg, "public") == 0) |
1013 |
session->action = ACTION_PUBLIC; |
1014 |
else |
1015 |
session->action = ACTION_UNKNOWN; |
1016 |
dbg("action = %d\n", session->action); |
1017 |
break; |
1018 |
case 'u': |
1019 |
if (session->user) |
1020 |
free(session->user); |
1021 |
session->user = strdup(optarg); |
1022 |
dbg("user = %s\n", session->user); |
1023 |
break; |
1024 |
case 'L': |
1025 |
if (session->logfile) |
1026 |
free(session->logfile); |
1027 |
session->logfile = strdup(optarg); |
1028 |
dbg("logfile = %s\n", session->logfile); |
1029 |
break; |
1030 |
case 's': |
1031 |
session->shrink_urls = 1; |
1032 |
break; |
1033 |
case 'H': |
1034 |
if (strcasecmp(optarg, "twitter") == 0) |
1035 |
session->host = HOST_TWITTER; |
1036 |
if (strcasecmp(optarg, "identica") == 0) |
1037 |
session->host = HOST_IDENTICA; |
1038 |
dbg("host = %d\n", session->host); |
1039 |
break; |
1040 |
case 'b': |
1041 |
session->bash = 1; |
1042 |
break; |
1043 |
case 'h': |
1044 |
display_help(); |
1045 |
goto exit; |
1046 |
case 'n': |
1047 |
session->dry_run = 1; |
1048 |
break; |
1049 |
case 'v': |
1050 |
display_version(); |
1051 |
goto exit; |
1052 |
default: |
1053 |
display_help(); |
1054 |
goto exit; |
1055 |
} |
1056 |
} |
1057 |
|
1058 |
/* |
1059 |
* Show the version to make it easier to determine what |
1060 |
* is going on here |
1061 |
*/ |
1062 |
if (debug) |
1063 |
display_version(); |
1064 |
|
1065 |
if (session->action == ACTION_UNKNOWN) { |
1066 |
fprintf(stderr, "Unknown action, valid actions are:\n"); |
1067 |
fprintf(stderr, "'update', 'friends', 'public', " |
1068 |
"'replies' or 'user'.\n"); |
1069 |
goto exit; |
1070 |
} |
1071 |
|
1072 |
if (!session->account) { |
1073 |
fprintf(stdout, "Enter twitter account: "); |
1074 |
session->account = readline(NULL); |
1075 |
} |
1076 |
|
1077 |
if (!session->password) { |
1078 |
fprintf(stdout, "Enter twitter password: "); |
1079 |
session->password = readline(NULL); |
1080 |
} |
1081 |
|
1082 |
if (session->action == ACTION_UPDATE) { |
1083 |
if (session->bash) |
1084 |
tweet = get_string_from_stdin(); |
1085 |
else |
1086 |
tweet = readline("tweet: "); |
1087 |
if (!tweet || strlen(tweet) == 0) { |
1088 |
dbg("no tweet?\n"); |
1089 |
return -1; |
1090 |
} |
1091 |
|
1092 |
if (session->shrink_urls) |
1093 |
tweet = shrink_urls(tweet); |
1094 |
|
1095 |
session->tweet = zalloc(strlen(tweet) + 10); |
1096 |
if (session->bash) |
1097 |
sprintf(session->tweet, "%c %s", getuid() ? '$' : '#', tweet); |
1098 |
else |
1099 |
sprintf(session->tweet, "%s", tweet); |
1100 |
|
1101 |
free(tweet); |
1102 |
dbg("tweet = %s\n", session->tweet); |
1103 |
} |
1104 |
|
1105 |
if (!session->user) |
1106 |
session->user = strdup(session->account); |
1107 |
|
1108 |
if (session->page == 0) |
1109 |
session->page = 1; |
1110 |
dbg("account = %s\n", session->account); |
1111 |
dbg("password = %s\n", session->password); |
1112 |
dbg("host = %d\n", session->host); |
1113 |
dbg("action = %d\n", session->action); |
1114 |
|
1115 |
/* fork ourself so that the main shell can get on |
1116 |
* with it's life as we try to connect and handle everything |
1117 |
*/ |
1118 |
if (session->bash) { |
1119 |
child = fork(); |
1120 |
if (child) { |
1121 |
dbg("child is %d\n", child); |
1122 |
exit(0); |
1123 |
} |
1124 |
} |
1125 |
|
1126 |
retval = send_request(session); |
1127 |
if (retval && !session->bash) |
1128 |
fprintf(stderr, "operation failed\n"); |
1129 |
|
1130 |
log_session(session, retval); |
1131 |
exit: |
1132 |
session_free(session); |
1133 |
return retval;; |
1134 |
} |