1 |
/* |
2 |
* Copyright (C) 2008-2010 Greg Kroah-Hartman <greg@kroah.com> |
3 |
* Copyright (C) 2009 Bart Trojanowski <bart@jukie.net> |
4 |
* Copyright (C) 2009 Amir Mohammad Saied <amirsaied@gmail.com> |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify it |
7 |
* under the terms of the GNU General Public License as published by the |
8 |
* Free Software Foundation version 2 of the License. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, but |
11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
* General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License along |
16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 |
*/ |
19 |
|
20 |
#define _GNU_SOURCE |
21 |
|
22 |
#include <stdio.h> |
23 |
#include <stdlib.h> |
24 |
#include <stddef.h> |
25 |
#include <string.h> |
26 |
#include <getopt.h> |
27 |
#include <errno.h> |
28 |
#include <ctype.h> |
29 |
#include <fcntl.h> |
30 |
#include <unistd.h> |
31 |
#include <time.h> |
32 |
#include <sys/stat.h> |
33 |
#include <sys/types.h> |
34 |
#include <sys/wait.h> |
35 |
#include <curl/curl.h> |
36 |
#include <libxml/xmlmemory.h> |
37 |
#include <libxml/parser.h> |
38 |
#include <libxml/tree.h> |
39 |
#include <pcre.h> |
40 |
#include <termios.h> |
41 |
#include <dlfcn.h> |
42 |
#include <oauth.h> |
43 |
|
44 |
|
45 |
#define zalloc(size) calloc(size, 1) |
46 |
|
47 |
#define dbg(format, arg...) \ |
48 |
do { \ |
49 |
if (debug) \ |
50 |
fprintf(stdout, "bti: %s: " format , __func__ , \ |
51 |
## arg); \ |
52 |
} while (0) |
53 |
|
54 |
|
55 |
static int debug; |
56 |
static int verbose; |
57 |
|
58 |
enum host { |
59 |
HOST_TWITTER = 0, |
60 |
HOST_IDENTICA = 1, |
61 |
HOST_CUSTOM = 2 |
62 |
}; |
63 |
|
64 |
enum action { |
65 |
ACTION_UPDATE = 0, |
66 |
ACTION_FRIENDS = 1, |
67 |
ACTION_USER = 2, |
68 |
ACTION_REPLIES = 4, |
69 |
ACTION_PUBLIC = 8, |
70 |
ACTION_GROUP = 16, |
71 |
ACTION_UNKNOWN = 32 |
72 |
}; |
73 |
|
74 |
struct session { |
75 |
char *password; |
76 |
char *account; |
77 |
char *consumer_key; |
78 |
char *consumer_secret; |
79 |
char *access_token_key; |
80 |
char *access_token_secret; |
81 |
char *tweet; |
82 |
char *proxy; |
83 |
char *time; |
84 |
char *homedir; |
85 |
char *logfile; |
86 |
char *user; |
87 |
char *group; |
88 |
char *hosturl; |
89 |
char *hostname; |
90 |
char *configfile; |
91 |
char *replyto; |
92 |
int bash; |
93 |
int background; |
94 |
int interactive; |
95 |
int shrink_urls; |
96 |
int dry_run; |
97 |
int page; |
98 |
int no_oauth; |
99 |
enum host host; |
100 |
enum action action; |
101 |
void *readline_handle; |
102 |
char *(*readline)(const char *); |
103 |
}; |
104 |
|
105 |
struct bti_curl_buffer { |
106 |
char *data; |
107 |
enum action action; |
108 |
int length; |
109 |
}; |
110 |
|
111 |
static void display_help(void) |
112 |
{ |
113 |
fprintf(stdout, "bti - send tweet to twitter or identi.ca\n" |
114 |
"Version: %s\n" |
115 |
"Usage:\n" |
116 |
" bti [options]\n" |
117 |
"options are:\n" |
118 |
" --account accountname\n" |
119 |
" --password password\n" |
120 |
" --action action\n" |
121 |
" ('update', 'friends', 'public', 'replies', or 'user')\n" |
122 |
" --user screenname\n" |
123 |
" --group groupname\n" |
124 |
" --proxy PROXY:PORT\n" |
125 |
" --host HOST\n" |
126 |
" --logfile logfile\n" |
127 |
" --config configfile\n" |
128 |
" --replyto ID\n" |
129 |
" --shrink-urls\n" |
130 |
" --page PAGENUMBER\n" |
131 |
" --bash\n" |
132 |
" --background\n" |
133 |
" --debug\n" |
134 |
" --verbose\n" |
135 |
" --dry-run\n" |
136 |
" --version\n" |
137 |
" --help\n", VERSION); |
138 |
} |
139 |
|
140 |
static void display_version(void) |
141 |
{ |
142 |
fprintf(stdout, "bti - version %s\n", VERSION); |
143 |
} |
144 |
|
145 |
static char *get_string(const char *name) |
146 |
{ |
147 |
char *temp; |
148 |
char *string; |
149 |
|
150 |
string = zalloc(1000); |
151 |
if (!string) |
152 |
exit(1); |
153 |
if (name != NULL) |
154 |
fprintf(stdout, "%s", name); |
155 |
if (!fgets(string, 999, stdin)) |
156 |
return NULL; |
157 |
temp = strchr(string, '\n'); |
158 |
if (temp) |
159 |
*temp = '\0'; |
160 |
return string; |
161 |
} |
162 |
|
163 |
/* |
164 |
* Try to get a handle to a readline function from a variety of different |
165 |
* libraries. If nothing is present on the system, then fall back to an |
166 |
* internal one. |
167 |
* |
168 |
* Logic originally based off of code in the e2fsutils package in the |
169 |
* lib/ss/get_readline.c file, which is licensed under the MIT license. |
170 |
* |
171 |
* This keeps us from having to relicense the bti codebase if readline |
172 |
* ever changes its license, as there is no link-time dependancy. |
173 |
* It is a run-time thing only, and we handle any readline-like library |
174 |
* in the same manner, making bti not be a derivative work of any |
175 |
* other program. |
176 |
*/ |
177 |
static void session_readline_init(struct session *session) |
178 |
{ |
179 |
/* Libraries we will try to use for readline/editline functionality */ |
180 |
const char *libpath = "libreadline.so.6:libreadline.so.5:" |
181 |
"libreadline.so.4:libreadline.so:libedit.so.2:" |
182 |
"libedit.so:libeditline.so.0:libeditline.so"; |
183 |
void *handle = NULL; |
184 |
char *tmp, *cp, *next; |
185 |
int (*bind_key)(int, void *); |
186 |
void (*insert)(void); |
187 |
|
188 |
/* default to internal function if we can't or won't find anything */ |
189 |
session->readline = get_string; |
190 |
if (!isatty(0)) |
191 |
return; |
192 |
session->interactive = 1; |
193 |
|
194 |
tmp = malloc(strlen(libpath)+1); |
195 |
if (!tmp) |
196 |
return; |
197 |
strcpy(tmp, libpath); |
198 |
for (cp = tmp; cp; cp = next) { |
199 |
next = strchr(cp, ':'); |
200 |
if (next) |
201 |
*next++ = 0; |
202 |
if (*cp == 0) |
203 |
continue; |
204 |
handle = dlopen(cp, RTLD_NOW); |
205 |
if (handle) { |
206 |
dbg("Using %s for readline library\n", cp); |
207 |
break; |
208 |
} |
209 |
} |
210 |
free(tmp); |
211 |
if (!handle) { |
212 |
dbg("No readline library found.\n"); |
213 |
return; |
214 |
} |
215 |
|
216 |
session->readline_handle = handle; |
217 |
session->readline = (char *(*)(const char *))dlsym(handle, "readline"); |
218 |
if (session->readline == NULL) { |
219 |
/* something odd happened, default back to internal stuff */ |
220 |
session->readline_handle = NULL; |
221 |
session->readline = get_string; |
222 |
return; |
223 |
} |
224 |
|
225 |
/* |
226 |
* If we found a library, turn off filename expansion |
227 |
* as that makes no sense from within bti. |
228 |
*/ |
229 |
bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key"); |
230 |
insert = (void (*)(void))dlsym(handle, "rl_insert"); |
231 |
if (bind_key && insert) |
232 |
bind_key('\t', insert); |
233 |
} |
234 |
|
235 |
static void session_readline_cleanup(struct session *session) |
236 |
{ |
237 |
if (session->readline_handle) |
238 |
dlclose(session->readline_handle); |
239 |
} |
240 |
|
241 |
static struct session *session_alloc(void) |
242 |
{ |
243 |
struct session *session; |
244 |
|
245 |
session = zalloc(sizeof(*session)); |
246 |
if (!session) |
247 |
return NULL; |
248 |
return session; |
249 |
} |
250 |
|
251 |
static void session_free(struct session *session) |
252 |
{ |
253 |
if (!session) |
254 |
return; |
255 |
free(session->replyto); |
256 |
free(session->password); |
257 |
free(session->account); |
258 |
free(session->consumer_key); |
259 |
free(session->consumer_secret); |
260 |
free(session->access_token_key); |
261 |
free(session->access_token_secret); |
262 |
free(session->tweet); |
263 |
free(session->proxy); |
264 |
free(session->time); |
265 |
free(session->homedir); |
266 |
free(session->user); |
267 |
free(session->group); |
268 |
free(session->hosturl); |
269 |
free(session->hostname); |
270 |
free(session->configfile); |
271 |
free(session); |
272 |
} |
273 |
|
274 |
static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action) |
275 |
{ |
276 |
struct bti_curl_buffer *buffer; |
277 |
|
278 |
buffer = zalloc(sizeof(*buffer)); |
279 |
if (!buffer) |
280 |
return NULL; |
281 |
|
282 |
/* start out with a data buffer of 1 byte to |
283 |
* make the buffer fill logic simpler */ |
284 |
buffer->data = zalloc(1); |
285 |
if (!buffer->data) { |
286 |
free(buffer); |
287 |
return NULL; |
288 |
} |
289 |
buffer->length = 0; |
290 |
buffer->action = action; |
291 |
return buffer; |
292 |
} |
293 |
|
294 |
static void bti_curl_buffer_free(struct bti_curl_buffer *buffer) |
295 |
{ |
296 |
if (!buffer) |
297 |
return; |
298 |
free(buffer->data); |
299 |
free(buffer); |
300 |
} |
301 |
|
302 |
static const char twitter_host[] = "http://api.twitter.com/1/statuses"; |
303 |
static const char identica_host[] = "https://identi.ca/api/statuses"; |
304 |
static const char twitter_name[] = "twitter"; |
305 |
static const char identica_name[] = "identi.ca"; |
306 |
|
307 |
static const char twitter_request_token_uri[] = "http://twitter.com/oauth/request_token"; |
308 |
static const char twitter_access_token_uri[] = "http://twitter.com/oauth/access_token"; |
309 |
static const char twitter_authorize_uri[] = "http://twitter.com/oauth/authorize?oauth_token="; |
310 |
static const char identica_request_token_uri[] = "http://identi.ca/api/oauth/request_token"; |
311 |
static const char identica_access_token_uri[] = "http://identi.ca/api/oauth/access_token"; |
312 |
static const char identica_authorize_uri[] = "http://identi.ca/api/oauth/authorize?oauth_token="; |
313 |
|
314 |
static const char user_uri[] = "/user_timeline/"; |
315 |
static const char update_uri[] = "/update.xml"; |
316 |
static const char public_uri[] = "/public_timeline.xml"; |
317 |
static const char friends_uri[] = "/friends_timeline.xml"; |
318 |
static const char mentions_uri[] = "/mentions.xml"; |
319 |
static const char replies_uri[] = "/replies.xml"; |
320 |
static const char group_uri[] = "/../statusnet/groups/timeline/"; |
321 |
|
322 |
static CURL *curl_init(void) |
323 |
{ |
324 |
CURL *curl; |
325 |
|
326 |
curl = curl_easy_init(); |
327 |
if (!curl) { |
328 |
fprintf(stderr, "Can not init CURL!\n"); |
329 |
return NULL; |
330 |
} |
331 |
/* some ssl sanity checks on the connection we are making */ |
332 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); |
333 |
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); |
334 |
return curl; |
335 |
} |
336 |
|
337 |
static void parse_statuses(xmlDocPtr doc, xmlNodePtr current) |
338 |
{ |
339 |
xmlChar *text = NULL; |
340 |
xmlChar *user = NULL; |
341 |
xmlChar *created = NULL; |
342 |
xmlChar *id = NULL; |
343 |
xmlNodePtr userinfo; |
344 |
|
345 |
current = current->xmlChildrenNode; |
346 |
while (current != NULL) { |
347 |
if (current->type == XML_ELEMENT_NODE) { |
348 |
if (!xmlStrcmp(current->name, (const xmlChar *)"created_at")) |
349 |
created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1); |
350 |
if (!xmlStrcmp(current->name, (const xmlChar *)"text")) |
351 |
text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1); |
352 |
if (!xmlStrcmp(current->name, (const xmlChar *)"id")) |
353 |
id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1); |
354 |
if (!xmlStrcmp(current->name, (const xmlChar *)"user")) { |
355 |
userinfo = current->xmlChildrenNode; |
356 |
while (userinfo != NULL) { |
357 |
if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) { |
358 |
if (user) |
359 |
xmlFree(user); |
360 |
user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1); |
361 |
} |
362 |
userinfo = userinfo->next; |
363 |
} |
364 |
} |
365 |
|
366 |
if (user && text && created && id) { |
367 |
if (verbose) |
368 |
printf("[%s] {%s} (%.16s) %s\n", |
369 |
user, id, created, text); |
370 |
else |
371 |
printf("[%s] %s\n", |
372 |
user, text); |
373 |
xmlFree(user); |
374 |
xmlFree(text); |
375 |
xmlFree(created); |
376 |
xmlFree(id); |
377 |
user = NULL; |
378 |
text = NULL; |
379 |
created = NULL; |
380 |
id = NULL; |
381 |
} |
382 |
} |
383 |
current = current->next; |
384 |
} |
385 |
|
386 |
return; |
387 |
} |
388 |
|
389 |
static void parse_timeline(char *document) |
390 |
{ |
391 |
xmlDocPtr doc; |
392 |
xmlNodePtr current; |
393 |
|
394 |
doc = xmlReadMemory(document, strlen(document), "timeline.xml", |
395 |
NULL, XML_PARSE_NOERROR); |
396 |
if (doc == NULL) |
397 |
return; |
398 |
|
399 |
current = xmlDocGetRootElement(doc); |
400 |
if (current == NULL) { |
401 |
fprintf(stderr, "empty document\n"); |
402 |
xmlFreeDoc(doc); |
403 |
return; |
404 |
} |
405 |
|
406 |
if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) { |
407 |
fprintf(stderr, "unexpected document type\n"); |
408 |
xmlFreeDoc(doc); |
409 |
return; |
410 |
} |
411 |
|
412 |
current = current->xmlChildrenNode; |
413 |
while (current != NULL) { |
414 |
if ((!xmlStrcmp(current->name, (const xmlChar *)"status"))) |
415 |
parse_statuses(doc, current); |
416 |
current = current->next; |
417 |
} |
418 |
xmlFreeDoc(doc); |
419 |
|
420 |
return; |
421 |
} |
422 |
|
423 |
static size_t curl_callback(void *buffer, size_t size, size_t nmemb, |
424 |
void *userp) |
425 |
{ |
426 |
struct bti_curl_buffer *curl_buf = userp; |
427 |
size_t buffer_size = size * nmemb; |
428 |
char *temp; |
429 |
|
430 |
if ((!buffer) || (!buffer_size) || (!curl_buf)) |
431 |
return -EINVAL; |
432 |
|
433 |
/* add to the data we already have */ |
434 |
temp = zalloc(curl_buf->length + buffer_size + 1); |
435 |
if (!temp) |
436 |
return -ENOMEM; |
437 |
|
438 |
memcpy(temp, curl_buf->data, curl_buf->length); |
439 |
free(curl_buf->data); |
440 |
curl_buf->data = temp; |
441 |
memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size); |
442 |
curl_buf->length += buffer_size; |
443 |
if (curl_buf->action) |
444 |
parse_timeline(curl_buf->data); |
445 |
|
446 |
dbg("%s\n", curl_buf->data); |
447 |
|
448 |
return buffer_size; |
449 |
} |
450 |
|
451 |
static int parse_osp_reply(const char *reply, char **token, char **secret) |
452 |
{ |
453 |
int rc; |
454 |
int retval = 1; |
455 |
char **rv = NULL; |
456 |
rc = oauth_split_url_parameters(reply, &rv); |
457 |
qsort(rv, rc, sizeof(char *), oauth_cmpstringp); |
458 |
if (rc == 2 || rc == 4) { |
459 |
if (!strncmp(rv[0], "oauth_token=", 11) && |
460 |
!strncmp(rv[1], "oauth_token_secret=", 18)) { |
461 |
if (token) |
462 |
*token = strdup(&(rv[0][12])); |
463 |
if (secret) |
464 |
*secret = strdup(&(rv[1][19])); |
465 |
|
466 |
retval = 0; |
467 |
} |
468 |
} else if (rc == 3) { |
469 |
if (!strncmp(rv[1], "oauth_token=", 11) && |
470 |
!strncmp(rv[2], "oauth_token_secret=", 18)) { |
471 |
if (token) |
472 |
*token = strdup(&(rv[1][12])); |
473 |
if (secret) |
474 |
*secret = strdup(&(rv[2][19])); |
475 |
|
476 |
retval = 0; |
477 |
} |
478 |
} |
479 |
|
480 |
dbg("token: %s\n", *token); |
481 |
dbg("secret: %s\n", *secret); |
482 |
|
483 |
if (rv) |
484 |
free(rv); |
485 |
|
486 |
return retval; |
487 |
} |
488 |
|
489 |
static int request_access_token(struct session *session) |
490 |
{ |
491 |
char *post_params = NULL; |
492 |
char *request_url = NULL; |
493 |
char *reply = NULL; |
494 |
char *at_key = NULL; |
495 |
char *at_secret = NULL; |
496 |
char *verifier = NULL; |
497 |
char at_uri[90]; |
498 |
|
499 |
if (!session) |
500 |
return -EINVAL; |
501 |
|
502 |
if (session->host == HOST_TWITTER) |
503 |
request_url = oauth_sign_url2( |
504 |
twitter_request_token_uri, NULL, |
505 |
OA_HMAC, NULL, session->consumer_key, |
506 |
session->consumer_secret, NULL, NULL); |
507 |
else if (session->host == HOST_IDENTICA) |
508 |
request_url = oauth_sign_url2( |
509 |
identica_request_token_uri, NULL, |
510 |
OA_HMAC, NULL, session->consumer_key, |
511 |
session->consumer_secret, NULL, NULL); |
512 |
reply = oauth_http_get(request_url, post_params); |
513 |
|
514 |
if (request_url) |
515 |
free(request_url); |
516 |
|
517 |
if (post_params) |
518 |
free(post_params); |
519 |
|
520 |
if (!reply) |
521 |
return 1; |
522 |
|
523 |
if (parse_osp_reply(reply, &at_key, &at_secret)) |
524 |
return 1; |
525 |
|
526 |
free(reply); |
527 |
|
528 |
fprintf(stdout, |
529 |
"Please open the following link in your browser, and " |
530 |
"allow 'bti' to access your account. Then paste " |
531 |
"back the provided PIN in here.\n"); |
532 |
if (session->host == HOST_TWITTER) { |
533 |
fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key); |
534 |
verifier = session->readline(NULL); |
535 |
sprintf(at_uri, "%s?oauth_verifier=%s", |
536 |
twitter_access_token_uri, verifier); |
537 |
} else if (session->host == HOST_IDENTICA) { |
538 |
fprintf(stdout, "%s%s\nPIN: ", identica_authorize_uri, at_key); |
539 |
verifier = session->readline(NULL); |
540 |
sprintf(at_uri, "%s?oauth_verifier=%s", |
541 |
identica_access_token_uri, verifier); |
542 |
} |
543 |
request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL, |
544 |
session->consumer_key, |
545 |
session->consumer_secret, |
546 |
at_key, at_secret); |
547 |
reply = oauth_http_get(request_url, post_params); |
548 |
|
549 |
if (!reply) |
550 |
return 1; |
551 |
|
552 |
if (parse_osp_reply(reply, &at_key, &at_secret)) |
553 |
return 1; |
554 |
|
555 |
free(reply); |
556 |
|
557 |
fprintf(stdout, |
558 |
"Please put these two lines in your bti " |
559 |
"configuration file (~/.bti):\n" |
560 |
"access_token_key=%s\n" |
561 |
"access_token_secret=%s\n", |
562 |
at_key, at_secret); |
563 |
|
564 |
return 0; |
565 |
} |
566 |
|
567 |
static int send_request(struct session *session) |
568 |
{ |
569 |
char endpoint[500]; |
570 |
char user_password[500]; |
571 |
char data[500]; |
572 |
struct bti_curl_buffer *curl_buf; |
573 |
CURL *curl = NULL; |
574 |
CURLcode res; |
575 |
struct curl_httppost *formpost = NULL; |
576 |
struct curl_httppost *lastptr = NULL; |
577 |
struct curl_slist *slist = NULL; |
578 |
char *req_url = NULL; |
579 |
char *reply = NULL; |
580 |
char *postarg = NULL; |
581 |
char *escaped_tweet = NULL; |
582 |
int is_post = 0; |
583 |
|
584 |
if (!session) |
585 |
return -EINVAL; |
586 |
|
587 |
if (!session->hosturl) |
588 |
session->hosturl = strdup(twitter_host); |
589 |
|
590 |
if (session->no_oauth) { |
591 |
curl_buf = bti_curl_buffer_alloc(session->action); |
592 |
if (!curl_buf) |
593 |
return -ENOMEM; |
594 |
|
595 |
curl = curl_init(); |
596 |
if (!curl) |
597 |
return -EINVAL; |
598 |
|
599 |
if (!session->hosturl) |
600 |
session->hosturl = strdup(twitter_host); |
601 |
|
602 |
switch (session->action) { |
603 |
case ACTION_UPDATE: |
604 |
snprintf(user_password, sizeof(user_password), "%s:%s", |
605 |
session->account, session->password); |
606 |
snprintf(data, sizeof(data), "status=\"%s\"", |
607 |
session->tweet); |
608 |
curl_formadd(&formpost, &lastptr, |
609 |
CURLFORM_COPYNAME, "status", |
610 |
CURLFORM_COPYCONTENTS, session->tweet, |
611 |
CURLFORM_END); |
612 |
|
613 |
curl_formadd(&formpost, &lastptr, |
614 |
CURLFORM_COPYNAME, "source", |
615 |
CURLFORM_COPYCONTENTS, "bti", |
616 |
CURLFORM_END); |
617 |
|
618 |
if (session->replyto) |
619 |
curl_formadd(&formpost, &lastptr, |
620 |
CURLFORM_COPYNAME, "in_reply_to_status_id", |
621 |
CURLFORM_COPYCONTENTS, session->replyto, |
622 |
CURLFORM_END); |
623 |
|
624 |
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); |
625 |
slist = curl_slist_append(slist, "Expect:"); |
626 |
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); |
627 |
|
628 |
sprintf(endpoint, "%s%s", session->hosturl, update_uri); |
629 |
curl_easy_setopt(curl, CURLOPT_URL, endpoint); |
630 |
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); |
631 |
break; |
632 |
|
633 |
case ACTION_FRIENDS: |
634 |
snprintf(user_password, sizeof(user_password), "%s:%s", |
635 |
session->account, session->password); |
636 |
sprintf(endpoint, "%s%s?page=%d", session->hosturl, |
637 |
friends_uri, session->page); |
638 |
curl_easy_setopt(curl, CURLOPT_URL, endpoint); |
639 |
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); |
640 |
break; |
641 |
|
642 |
case ACTION_USER: |
643 |
sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl, |
644 |
user_uri, session->user, session->page); |
645 |
curl_easy_setopt(curl, CURLOPT_URL, endpoint); |
646 |
break; |
647 |
|
648 |
case ACTION_REPLIES: |
649 |
snprintf(user_password, sizeof(user_password), "%s:%s", |
650 |
session->account, session->password); |
651 |
sprintf(endpoint, "%s%s?page=%d", session->hosturl, |
652 |
replies_uri, session->page); |
653 |
curl_easy_setopt(curl, CURLOPT_URL, endpoint); |
654 |
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); |
655 |
break; |
656 |
|
657 |
case ACTION_PUBLIC: |
658 |
sprintf(endpoint, "%s%s?page=%d", session->hosturl, |
659 |
public_uri, session->page); |
660 |
curl_easy_setopt(curl, CURLOPT_URL, endpoint); |
661 |
break; |
662 |
|
663 |
case ACTION_GROUP: |
664 |
sprintf(endpoint, "%s%s%s.xml?page=%d", |
665 |
session->hosturl, group_uri, session->group, |
666 |
session->page); |
667 |
curl_easy_setopt(curl, CURLOPT_URL, endpoint); |
668 |
break; |
669 |
|
670 |
default: |
671 |
break; |
672 |
} |
673 |
|
674 |
if (session->proxy) |
675 |
curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy); |
676 |
|
677 |
if (debug) |
678 |
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); |
679 |
|
680 |
dbg("user_password = %s\n", user_password); |
681 |
dbg("data = %s\n", data); |
682 |
dbg("proxy = %s\n", session->proxy); |
683 |
|
684 |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback); |
685 |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf); |
686 |
if (!session->dry_run) { |
687 |
res = curl_easy_perform(curl); |
688 |
if (res && !session->background) { |
689 |
fprintf(stderr, "error(%d) trying to perform " |
690 |
"operation\n", res); |
691 |
return -EINVAL; |
692 |
} |
693 |
} |
694 |
|
695 |
curl_easy_cleanup(curl); |
696 |
if (session->action == ACTION_UPDATE) |
697 |
curl_formfree(formpost); |
698 |
bti_curl_buffer_free(curl_buf); |
699 |
} else { |
700 |
switch (session->action) { |
701 |
case ACTION_UPDATE: |
702 |
escaped_tweet = oauth_url_escape(session->tweet); |
703 |
sprintf(endpoint, "%s%s?status=%s", session->hosturl, |
704 |
update_uri, escaped_tweet); |
705 |
is_post = 1; |
706 |
break; |
707 |
case ACTION_USER: |
708 |
sprintf(endpoint, "%s%s%s.xml?page=%d", |
709 |
session->hosturl, user_uri, session->user, |
710 |
session->page); |
711 |
break; |
712 |
case ACTION_REPLIES: |
713 |
sprintf(endpoint, "%s%s?page=%d", session->hosturl, |
714 |
mentions_uri, session->page); |
715 |
break; |
716 |
case ACTION_PUBLIC: |
717 |
sprintf(endpoint, "%s%s?page=%d", session->hosturl, |
718 |
public_uri, session->page); |
719 |
break; |
720 |
case ACTION_GROUP: |
721 |
sprintf(endpoint, "%s%s%s.xml?page=%d", |
722 |
session->hosturl, group_uri, session->group, |
723 |
session->page); |
724 |
break; |
725 |
case ACTION_FRIENDS: |
726 |
sprintf(endpoint, "%s%s?page=%d", session->hosturl, |
727 |
friends_uri, session->page); |
728 |
break; |
729 |
default: |
730 |
break; |
731 |
} |
732 |
|
733 |
if (is_post) { |
734 |
req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC, |
735 |
NULL, session->consumer_key, |
736 |
session->consumer_secret, |
737 |
session->access_token_key, |
738 |
session->access_token_secret); |
739 |
reply = oauth_http_post(req_url, postarg); |
740 |
} else { |
741 |
req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL, |
742 |
session->consumer_key, |
743 |
session->consumer_secret, |
744 |
session->access_token_key, |
745 |
session->access_token_secret); |
746 |
reply = oauth_http_get(req_url, postarg); |
747 |
} |
748 |
|
749 |
dbg("%s\n", req_url); |
750 |
dbg("%s\n", reply); |
751 |
if (req_url) |
752 |
free(req_url); |
753 |
|
754 |
if (session->action != ACTION_UPDATE) |
755 |
parse_timeline(reply); |
756 |
} |
757 |
return 0; |
758 |
} |
759 |
|
760 |
static void parse_configfile(struct session *session) |
761 |
{ |
762 |
FILE *config_file; |
763 |
char *line = NULL; |
764 |
size_t len = 0; |
765 |
char *account = NULL; |
766 |
char *password = NULL; |
767 |
char *consumer_key = NULL; |
768 |
char *consumer_secret = NULL; |
769 |
char *access_token_key = NULL; |
770 |
char *access_token_secret = NULL; |
771 |
char *host = NULL; |
772 |
char *proxy = NULL; |
773 |
char *logfile = NULL; |
774 |
char *action = NULL; |
775 |
char *user = NULL; |
776 |
char *replyto = NULL; |
777 |
int shrink_urls = 0; |
778 |
|
779 |
config_file = fopen(session->configfile, "r"); |
780 |
|
781 |
/* No error if file does not exist or is unreadable. */ |
782 |
if (config_file == NULL) |
783 |
return; |
784 |
|
785 |
do { |
786 |
ssize_t n = getline(&line, &len, config_file); |
787 |
if (n < 0) |
788 |
break; |
789 |
if (line[n - 1] == '\n') |
790 |
line[n - 1] = '\0'; |
791 |
/* Parse file. Format is the usual value pairs: |
792 |
account=name |
793 |
passwort=value |
794 |
# is a comment character |
795 |
*/ |
796 |
*strchrnul(line, '#') = '\0'; |
797 |
char *c = line; |
798 |
while (isspace(*c)) |
799 |
c++; |
800 |
/* Ignore blank lines. */ |
801 |
if (c[0] == '\0') |
802 |
continue; |
803 |
|
804 |
if (!strncasecmp(c, "account", 7) && (c[7] == '=')) { |
805 |
c += 8; |
806 |
if (c[0] != '\0') |
807 |
account = strdup(c); |
808 |
} else if (!strncasecmp(c, "password", 8) && |
809 |
(c[8] == '=')) { |
810 |
c += 9; |
811 |
if (c[0] != '\0') |
812 |
password = strdup(c); |
813 |
} else if (!strncasecmp(c, "consumer_key", 12) && |
814 |
(c[12] == '=')) { |
815 |
c += 13; |
816 |
if (c[0] != '\0') |
817 |
consumer_key = strdup(c); |
818 |
} else if (!strncasecmp(c, "consumer_secret", 15) && |
819 |
(c[15] == '=')) { |
820 |
c += 16; |
821 |
if (c[0] != '\0') |
822 |
consumer_secret = strdup(c); |
823 |
} else if (!strncasecmp(c, "access_token_key", 16) && |
824 |
(c[16] == '=')) { |
825 |
c += 17; |
826 |
if (c[0] != '\0') |
827 |
access_token_key = strdup(c); |
828 |
} else if (!strncasecmp(c, "access_token_secret", 19) && |
829 |
(c[19] == '=')) { |
830 |
c += 20; |
831 |
if (c[0] != '\0') |
832 |
access_token_secret = strdup(c); |
833 |
} else if (!strncasecmp(c, "host", 4) && |
834 |
(c[4] == '=')) { |
835 |
c += 5; |
836 |
if (c[0] != '\0') |
837 |
host = strdup(c); |
838 |
} else if (!strncasecmp(c, "proxy", 5) && |
839 |
(c[5] == '=')) { |
840 |
c += 6; |
841 |
if (c[0] != '\0') |
842 |
proxy = strdup(c); |
843 |
} else if (!strncasecmp(c, "logfile", 7) && |
844 |
(c[7] == '=')) { |
845 |
c += 8; |
846 |
if (c[0] != '\0') |
847 |
logfile = strdup(c); |
848 |
} else if (!strncasecmp(c, "replyto", 7) && |
849 |
(c[7] == '=')) { |
850 |
c += 8; |
851 |
if (c[0] != '\0') |
852 |
replyto = strdup(c); |
853 |
} else if (!strncasecmp(c, "action", 6) && |
854 |
(c[6] == '=')) { |
855 |
c += 7; |
856 |
if (c[0] != '\0') |
857 |
action = strdup(c); |
858 |
} else if (!strncasecmp(c, "user", 4) && |
859 |
(c[4] == '=')) { |
860 |
c += 5; |
861 |
if (c[0] != '\0') |
862 |
user = strdup(c); |
863 |
} else if (!strncasecmp(c, "shrink-urls", 11) && |
864 |
(c[11] == '=')) { |
865 |
c += 12; |
866 |
if (!strncasecmp(c, "true", 4) || |
867 |
!strncasecmp(c, "yes", 3)) |
868 |
shrink_urls = 1; |
869 |
} else if (!strncasecmp(c, "verbose", 7) && |
870 |
(c[7] == '=')) { |
871 |
c += 8; |
872 |
if (!strncasecmp(c, "true", 4) || |
873 |
!strncasecmp(c, "yes", 3)) |
874 |
verbose = 1; |
875 |
} |
876 |
} while (!feof(config_file)); |
877 |
|
878 |
if (password) |
879 |
session->password = password; |
880 |
if (account) |
881 |
session->account = account; |
882 |
if (consumer_key) |
883 |
session->consumer_key = consumer_key; |
884 |
if (consumer_secret) |
885 |
session->consumer_secret = consumer_secret; |
886 |
if (access_token_key) |
887 |
session->access_token_key = access_token_key; |
888 |
if (access_token_secret) |
889 |
session->access_token_secret = access_token_secret; |
890 |
if (host) { |
891 |
if (strcasecmp(host, "twitter") == 0) { |
892 |
session->host = HOST_TWITTER; |
893 |
session->hosturl = strdup(twitter_host); |
894 |
session->hostname = strdup(twitter_name); |
895 |
} else if (strcasecmp(host, "identica") == 0) { |
896 |
session->host = HOST_IDENTICA; |
897 |
session->hosturl = strdup(identica_host); |
898 |
session->hostname = strdup(identica_name); |
899 |
} else { |
900 |
session->host = HOST_CUSTOM; |
901 |
session->hosturl = strdup(host); |
902 |
session->hostname = strdup(host); |
903 |
} |
904 |
free(host); |
905 |
} |
906 |
if (proxy) { |
907 |
if (session->proxy) |
908 |
free(session->proxy); |
909 |
session->proxy = proxy; |
910 |
} |
911 |
if (logfile) |
912 |
session->logfile = logfile; |
913 |
if (replyto) |
914 |
session->replyto = replyto; |
915 |
if (action) { |
916 |
if (strcasecmp(action, "update") == 0) |
917 |
session->action = ACTION_UPDATE; |
918 |
else if (strcasecmp(action, "friends") == 0) |
919 |
session->action = ACTION_FRIENDS; |
920 |
else if (strcasecmp(action, "user") == 0) |
921 |
session->action = ACTION_USER; |
922 |
else if (strcasecmp(action, "replies") == 0) |
923 |
session->action = ACTION_REPLIES; |
924 |
else if (strcasecmp(action, "public") == 0) |
925 |
session->action = ACTION_PUBLIC; |
926 |
else if (strcasecmp(action, "group") == 0) |
927 |
session->action = ACTION_GROUP; |
928 |
else |
929 |
session->action = ACTION_UNKNOWN; |
930 |
free(action); |
931 |
} |
932 |
if (user) |
933 |
session->user = user; |
934 |
session->shrink_urls = shrink_urls; |
935 |
|
936 |
/* Free buffer and close file. */ |
937 |
free(line); |
938 |
fclose(config_file); |
939 |
} |
940 |
|
941 |
static void log_session(struct session *session, int retval) |
942 |
{ |
943 |
FILE *log_file; |
944 |
char *filename; |
945 |
|
946 |
/* Only log something if we have a log file set */ |
947 |
if (!session->logfile) |
948 |
return; |
949 |
|
950 |
filename = alloca(strlen(session->homedir) + |
951 |
strlen(session->logfile) + 3); |
952 |
|
953 |
sprintf(filename, "%s/%s", session->homedir, session->logfile); |
954 |
|
955 |
log_file = fopen(filename, "a+"); |
956 |
if (log_file == NULL) |
957 |
return; |
958 |
|
959 |
switch (session->action) { |
960 |
case ACTION_UPDATE: |
961 |
if (retval) |
962 |
fprintf(log_file, "%s: host=%s tweet failed\n", |
963 |
session->time, session->hostname); |
964 |
else |
965 |
fprintf(log_file, "%s: host=%s tweet=%s\n", |
966 |
session->time, session->hostname, |
967 |
session->tweet); |
968 |
break; |
969 |
case ACTION_FRIENDS: |
970 |
fprintf(log_file, "%s: host=%s retrieving friends timeline\n", |
971 |
session->time, session->hostname); |
972 |
break; |
973 |
case ACTION_USER: |
974 |
fprintf(log_file, "%s: host=%s retrieving %s's timeline\n", |
975 |
session->time, session->hostname, session->user); |
976 |
break; |
977 |
case ACTION_REPLIES: |
978 |
fprintf(log_file, "%s: host=%s retrieving replies\n", |
979 |
session->time, session->hostname); |
980 |
break; |
981 |
case ACTION_PUBLIC: |
982 |
fprintf(log_file, "%s: host=%s retrieving public timeline\n", |
983 |
session->time, session->hostname); |
984 |
break; |
985 |
case ACTION_GROUP: |
986 |
fprintf(log_file, "%s: host=%s retrieving group timeline\n", |
987 |
session->time, session->hostname); |
988 |
break; |
989 |
default: |
990 |
break; |
991 |
} |
992 |
|
993 |
fclose(log_file); |
994 |
} |
995 |
|
996 |
static char *get_string_from_stdin(void) |
997 |
{ |
998 |
char *temp; |
999 |
char *string; |
1000 |
|
1001 |
string = zalloc(1000); |
1002 |
if (!string) |
1003 |
return NULL; |
1004 |
|
1005 |
if (!fgets(string, 999, stdin)) |
1006 |
return NULL; |
1007 |
temp = strchr(string, '\n'); |
1008 |
if (temp) |
1009 |
*temp = '\0'; |
1010 |
return string; |
1011 |
} |
1012 |
|
1013 |
static void read_password(char *buf, size_t len, char *host) |
1014 |
{ |
1015 |
char pwd[80]; |
1016 |
int retval; |
1017 |
struct termios old; |
1018 |
struct termios tp; |
1019 |
|
1020 |
tcgetattr(0, &tp); |
1021 |
old = tp; |
1022 |
|
1023 |
tp.c_lflag &= (~ECHO); |
1024 |
tcsetattr(0, TCSANOW, &tp); |
1025 |
|
1026 |
fprintf(stdout, "Enter password for %s: ", host); |
1027 |
fflush(stdout); |
1028 |
tcflow(0, TCOOFF); |
1029 |
retval = scanf("%79s", pwd); |
1030 |
tcflow(0, TCOON); |
1031 |
fprintf(stdout, "\n"); |
1032 |
|
1033 |
tcsetattr(0, TCSANOW, &old); |
1034 |
|
1035 |
strncpy(buf, pwd, len); |
1036 |
buf[len-1] = '\0'; |
1037 |
} |
1038 |
|
1039 |
static int find_urls(const char *tweet, int **pranges) |
1040 |
{ |
1041 |
/* |
1042 |
* magic obtained from |
1043 |
* http://www.geekpedia.com/KB65_How-to-validate-an-URL-using-RegEx-in-Csharp.html |
1044 |
*/ |
1045 |
static const char *re_magic = |
1046 |
"(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)/{1,3}" |
1047 |
"[0-9a-zA-Z;/~?:@&=+$\\.\\-_'()%]+)" |
1048 |
"(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?"; |
1049 |
pcre *re; |
1050 |
const char *errptr; |
1051 |
int erroffset; |
1052 |
int ovector[10] = {0,}; |
1053 |
const size_t ovsize = sizeof(ovector)/sizeof(*ovector); |
1054 |
int startoffset, tweetlen; |
1055 |
int i, rc; |
1056 |
int rbound = 10; |
1057 |
int rcount = 0; |
1058 |
int *ranges = malloc(sizeof(int) * rbound); |
1059 |
|
1060 |
re = pcre_compile(re_magic, |
1061 |
PCRE_NO_AUTO_CAPTURE, |
1062 |
&errptr, &erroffset, NULL); |
1063 |
if (!re) { |
1064 |
fprintf(stderr, "pcre_compile @%u: %s\n", erroffset, errptr); |
1065 |
exit(1); |
1066 |
} |
1067 |
|
1068 |
tweetlen = strlen(tweet); |
1069 |
for (startoffset = 0; startoffset < tweetlen; ) { |
1070 |
|
1071 |
rc = pcre_exec(re, NULL, tweet, strlen(tweet), startoffset, 0, |
1072 |
ovector, ovsize); |
1073 |
if (rc == PCRE_ERROR_NOMATCH) |
1074 |
break; |
1075 |
|
1076 |
if (rc < 0) { |
1077 |
fprintf(stderr, "pcre_exec @%u: %s\n", |
1078 |
erroffset, errptr); |
1079 |
exit(1); |
1080 |
} |
1081 |
|
1082 |
for (i = 0; i < rc; i += 2) { |
1083 |
if ((rcount+2) == rbound) { |
1084 |
rbound *= 2; |
1085 |
ranges = realloc(ranges, sizeof(int) * rbound); |
1086 |
} |
1087 |
|
1088 |
ranges[rcount++] = ovector[i]; |
1089 |
ranges[rcount++] = ovector[i+1]; |
1090 |
} |
1091 |
|
1092 |
startoffset = ovector[1]; |
1093 |
} |
1094 |
|
1095 |
pcre_free(re); |
1096 |
|
1097 |
*pranges = ranges; |
1098 |
return rcount; |
1099 |
} |
1100 |
|
1101 |
/** |
1102 |
* bidirectional popen() call |
1103 |
* |
1104 |
* @param rwepipe - int array of size three |
1105 |
* @param exe - program to run |
1106 |
* @param argv - argument list |
1107 |
* @return pid or -1 on error |
1108 |
* |
1109 |
* The caller passes in an array of three integers (rwepipe), on successful |
1110 |
* execution it can then write to element 0 (stdin of exe), and read from |
1111 |
* element 1 (stdout) and 2 (stderr). |
1112 |
*/ |
1113 |
static int popenRWE(int *rwepipe, const char *exe, const char *const argv[]) |
1114 |
{ |
1115 |
int in[2]; |
1116 |
int out[2]; |
1117 |
int err[2]; |
1118 |
int pid; |
1119 |
int rc; |
1120 |
|
1121 |
rc = pipe(in); |
1122 |
if (rc < 0) |
1123 |
goto error_in; |
1124 |
|
1125 |
rc = pipe(out); |
1126 |
if (rc < 0) |
1127 |
goto error_out; |
1128 |
|
1129 |
rc = pipe(err); |
1130 |
if (rc < 0) |
1131 |
goto error_err; |
1132 |
|
1133 |
pid = fork(); |
1134 |
if (pid > 0) { |
1135 |
/* parent */ |
1136 |
close(in[0]); |
1137 |
close(out[1]); |
1138 |
close(err[1]); |
1139 |
rwepipe[0] = in[1]; |
1140 |
rwepipe[1] = out[0]; |
1141 |
rwepipe[2] = err[0]; |
1142 |
return pid; |
1143 |
} else if (pid == 0) { |
1144 |
/* child */ |
1145 |
close(in[1]); |
1146 |
close(out[0]); |
1147 |
close(err[0]); |
1148 |
close(0); |
1149 |
rc = dup(in[0]); |
1150 |
close(1); |
1151 |
rc = dup(out[1]); |
1152 |
close(2); |
1153 |
rc = dup(err[1]); |
1154 |
|
1155 |
execvp(exe, (char **)argv); |
1156 |
exit(1); |
1157 |
} else |
1158 |
goto error_fork; |
1159 |
|
1160 |
return pid; |
1161 |
|
1162 |
error_fork: |
1163 |
close(err[0]); |
1164 |
close(err[1]); |
1165 |
error_err: |
1166 |
close(out[0]); |
1167 |
close(out[1]); |
1168 |
error_out: |
1169 |
close(in[0]); |
1170 |
close(in[1]); |
1171 |
error_in: |
1172 |
return -1; |
1173 |
} |
1174 |
|
1175 |
static int pcloseRWE(int pid, int *rwepipe) |
1176 |
{ |
1177 |
int rc, status; |
1178 |
close(rwepipe[0]); |
1179 |
close(rwepipe[1]); |
1180 |
close(rwepipe[2]); |
1181 |
rc = waitpid(pid, &status, 0); |
1182 |
return status; |
1183 |
} |
1184 |
|
1185 |
static char *shrink_one_url(int *rwepipe, char *big) |
1186 |
{ |
1187 |
int biglen = strlen(big); |
1188 |
char *small; |
1189 |
int smalllen; |
1190 |
int rc; |
1191 |
|
1192 |
rc = dprintf(rwepipe[0], "%s\n", big); |
1193 |
if (rc < 0) |
1194 |
return big; |
1195 |
|
1196 |
smalllen = biglen + 128; |
1197 |
small = malloc(smalllen); |
1198 |
if (!small) |
1199 |
return big; |
1200 |
|
1201 |
rc = read(rwepipe[1], small, smalllen); |
1202 |
if (rc < 0 || rc > biglen) |
1203 |
goto error_free_small; |
1204 |
|
1205 |
if (strncmp(small, "http://", 7)) |
1206 |
goto error_free_small; |
1207 |
|
1208 |
smalllen = rc; |
1209 |
while (smalllen && isspace(small[smalllen-1])) |
1210 |
small[--smalllen] = 0; |
1211 |
|
1212 |
free(big); |
1213 |
return small; |
1214 |
|
1215 |
error_free_small: |
1216 |
free(small); |
1217 |
return big; |
1218 |
} |
1219 |
|
1220 |
static char *shrink_urls(char *text) |
1221 |
{ |
1222 |
int *ranges; |
1223 |
int rcount; |
1224 |
int i; |
1225 |
int inofs = 0; |
1226 |
int outofs = 0; |
1227 |
const char *const shrink_args[] = { |
1228 |
"bti-shrink-urls", |
1229 |
NULL |
1230 |
}; |
1231 |
int shrink_pid; |
1232 |
int shrink_pipe[3]; |
1233 |
int inlen = strlen(text); |
1234 |
|
1235 |
dbg("before len=%u\n", inlen); |
1236 |
|
1237 |
shrink_pid = popenRWE(shrink_pipe, shrink_args[0], shrink_args); |
1238 |
if (shrink_pid < 0) |
1239 |
return text; |
1240 |
|
1241 |
rcount = find_urls(text, &ranges); |
1242 |
if (!rcount) |
1243 |
return text; |
1244 |
|
1245 |
for (i = 0; i < rcount; i += 2) { |
1246 |
int url_start = ranges[i]; |
1247 |
int url_end = ranges[i+1]; |
1248 |
int long_url_len = url_end - url_start; |
1249 |
char *url = strndup(text + url_start, long_url_len); |
1250 |
int short_url_len; |
1251 |
int not_url_len = url_start - inofs; |
1252 |
|
1253 |
dbg("long url[%u]: %s\n", long_url_len, url); |
1254 |
url = shrink_one_url(shrink_pipe, url); |
1255 |
short_url_len = url ? strlen(url) : 0; |
1256 |
dbg("short url[%u]: %s\n", short_url_len, url); |
1257 |
|
1258 |
if (!url || short_url_len >= long_url_len) { |
1259 |
/* The short url ended up being too long |
1260 |
* or unavailable */ |
1261 |
if (inofs) { |
1262 |
strncpy(text + outofs, text + inofs, |
1263 |
not_url_len + long_url_len); |
1264 |
} |
1265 |
inofs += not_url_len + long_url_len; |
1266 |
outofs += not_url_len + long_url_len; |
1267 |
|
1268 |
} else { |
1269 |
/* copy the unmodified block */ |
1270 |
strncpy(text + outofs, text + inofs, not_url_len); |
1271 |
inofs += not_url_len; |
1272 |
outofs += not_url_len; |
1273 |
|
1274 |
/* copy the new url */ |
1275 |
strncpy(text + outofs, url, short_url_len); |
1276 |
inofs += long_url_len; |
1277 |
outofs += short_url_len; |
1278 |
} |
1279 |
|
1280 |
free(url); |
1281 |
} |
1282 |
|
1283 |
/* copy the last block after the last match */ |
1284 |
if (inofs) { |
1285 |
int tail = inlen - inofs; |
1286 |
if (tail) { |
1287 |
strncpy(text + outofs, text + inofs, tail); |
1288 |
outofs += tail; |
1289 |
} |
1290 |
} |
1291 |
|
1292 |
free(ranges); |
1293 |
|
1294 |
(void)pcloseRWE(shrink_pid, shrink_pipe); |
1295 |
|
1296 |
text[outofs] = 0; |
1297 |
dbg("after len=%u\n", outofs); |
1298 |
return text; |
1299 |
} |
1300 |
|
1301 |
int main(int argc, char *argv[], char *envp[]) |
1302 |
{ |
1303 |
static const struct option options[] = { |
1304 |
{ "debug", 0, NULL, 'd' }, |
1305 |
{ "verbose", 0, NULL, 'V' }, |
1306 |
{ "account", 1, NULL, 'a' }, |
1307 |
{ "password", 1, NULL, 'p' }, |
1308 |
{ "host", 1, NULL, 'H' }, |
1309 |
{ "proxy", 1, NULL, 'P' }, |
1310 |
{ "action", 1, NULL, 'A' }, |
1311 |
{ "user", 1, NULL, 'u' }, |
1312 |
{ "group", 1, NULL, 'G' }, |
1313 |
{ "logfile", 1, NULL, 'L' }, |
1314 |
{ "shrink-urls", 0, NULL, 's' }, |
1315 |
{ "help", 0, NULL, 'h' }, |
1316 |
{ "bash", 0, NULL, 'b' }, |
1317 |
{ "background", 0, NULL, 'B' }, |
1318 |
{ "dry-run", 0, NULL, 'n' }, |
1319 |
{ "page", 1, NULL, 'g' }, |
1320 |
{ "version", 0, NULL, 'v' }, |
1321 |
{ "config", 1, NULL, 'c' }, |
1322 |
{ "replyto", 1, NULL, 'r' }, |
1323 |
{ } |
1324 |
}; |
1325 |
struct session *session; |
1326 |
pid_t child; |
1327 |
char *tweet; |
1328 |
static char password[80]; |
1329 |
int retval = 0; |
1330 |
int option; |
1331 |
char *http_proxy; |
1332 |
time_t t; |
1333 |
int page_nr; |
1334 |
|
1335 |
debug = 0; |
1336 |
verbose = 0; |
1337 |
|
1338 |
session = session_alloc(); |
1339 |
if (!session) { |
1340 |
fprintf(stderr, "no more memory...\n"); |
1341 |
return -1; |
1342 |
} |
1343 |
|
1344 |
/* get the current time so that we can log it later */ |
1345 |
time(&t); |
1346 |
session->time = strdup(ctime(&t)); |
1347 |
session->time[strlen(session->time)-1] = 0x00; |
1348 |
|
1349 |
/* Get the home directory so we can try to find a config file */ |
1350 |
session->homedir = strdup(getenv("HOME")); |
1351 |
|
1352 |
/* set up a default config file location (traditionally ~/.bti) */ |
1353 |
session->configfile = zalloc(strlen(session->homedir) + 7); |
1354 |
sprintf(session->configfile, "%s/.bti", session->homedir); |
1355 |
|
1356 |
/* Set environment variables first, before reading command line options |
1357 |
* or config file values. */ |
1358 |
http_proxy = getenv("http_proxy"); |
1359 |
if (http_proxy) { |
1360 |
if (session->proxy) |
1361 |
free(session->proxy); |
1362 |
session->proxy = strdup(http_proxy); |
1363 |
dbg("http_proxy = %s\n", session->proxy); |
1364 |
} |
1365 |
|
1366 |
parse_configfile(session); |
1367 |
|
1368 |
while (1) { |
1369 |
option = getopt_long_only(argc, argv, |
1370 |
"dp:P:H:a:A:u:c:hg:G:sr:nVv", |
1371 |
options, NULL); |
1372 |
if (option == -1) |
1373 |
break; |
1374 |
switch (option) { |
1375 |
case 'd': |
1376 |
debug = 1; |
1377 |
break; |
1378 |
case 'V': |
1379 |
verbose = 1; |
1380 |
break; |
1381 |
case 'a': |
1382 |
if (session->account) |
1383 |
free(session->account); |
1384 |
session->account = strdup(optarg); |
1385 |
dbg("account = %s\n", session->account); |
1386 |
break; |
1387 |
case 'g': |
1388 |
page_nr = atoi(optarg); |
1389 |
dbg("page = %d\n", page_nr); |
1390 |
session->page = page_nr; |
1391 |
break; |
1392 |
case 'r': |
1393 |
session->replyto = strdup(optarg); |
1394 |
dbg("in_reply_to_status_id = %s\n", session->replyto); |
1395 |
break; |
1396 |
case 'p': |
1397 |
if (session->password) |
1398 |
free(session->password); |
1399 |
session->password = strdup(optarg); |
1400 |
dbg("password = %s\n", session->password); |
1401 |
break; |
1402 |
case 'P': |
1403 |
if (session->proxy) |
1404 |
free(session->proxy); |
1405 |
session->proxy = strdup(optarg); |
1406 |
dbg("proxy = %s\n", session->proxy); |
1407 |
break; |
1408 |
case 'A': |
1409 |
if (strcasecmp(optarg, "update") == 0) |
1410 |
session->action = ACTION_UPDATE; |
1411 |
else if (strcasecmp(optarg, "friends") == 0) |
1412 |
session->action = ACTION_FRIENDS; |
1413 |
else if (strcasecmp(optarg, "user") == 0) |
1414 |
session->action = ACTION_USER; |
1415 |
else if (strcasecmp(optarg, "replies") == 0) |
1416 |
session->action = ACTION_REPLIES; |
1417 |
else if (strcasecmp(optarg, "public") == 0) |
1418 |
session->action = ACTION_PUBLIC; |
1419 |
else if (strcasecmp(optarg, "group") == 0) |
1420 |
session->action = ACTION_GROUP; |
1421 |
else |
1422 |
session->action = ACTION_UNKNOWN; |
1423 |
dbg("action = %d\n", session->action); |
1424 |
break; |
1425 |
case 'u': |
1426 |
if (session->user) |
1427 |
free(session->user); |
1428 |
session->user = strdup(optarg); |
1429 |
dbg("user = %s\n", session->user); |
1430 |
break; |
1431 |
|
1432 |
case 'G': |
1433 |
if (session->group) |
1434 |
free(session->group); |
1435 |
session->group = strdup(optarg); |
1436 |
dbg("group = %s\n", session->group); |
1437 |
break; |
1438 |
case 'L': |
1439 |
if (session->logfile) |
1440 |
free(session->logfile); |
1441 |
session->logfile = strdup(optarg); |
1442 |
dbg("logfile = %s\n", session->logfile); |
1443 |
break; |
1444 |
case 's': |
1445 |
session->shrink_urls = 1; |
1446 |
break; |
1447 |
case 'H': |
1448 |
if (session->hosturl) |
1449 |
free(session->hosturl); |
1450 |
if (session->hostname) |
1451 |
free(session->hostname); |
1452 |
if (strcasecmp(optarg, "twitter") == 0) { |
1453 |
session->host = HOST_TWITTER; |
1454 |
session->hosturl = strdup(twitter_host); |
1455 |
session->hostname = strdup(twitter_name); |
1456 |
} else if (strcasecmp(optarg, "identica") == 0) { |
1457 |
session->host = HOST_IDENTICA; |
1458 |
session->hosturl = strdup(identica_host); |
1459 |
session->hostname = strdup(identica_name); |
1460 |
} else { |
1461 |
session->host = HOST_CUSTOM; |
1462 |
session->hosturl = strdup(optarg); |
1463 |
session->hostname = strdup(optarg); |
1464 |
} |
1465 |
dbg("host = %d\n", session->host); |
1466 |
break; |
1467 |
case 'b': |
1468 |
session->bash = 1; |
1469 |
/* fall-through intended */ |
1470 |
case 'B': |
1471 |
session->background = 1; |
1472 |
break; |
1473 |
case 'c': |
1474 |
if (session->configfile) |
1475 |
free(session->configfile); |
1476 |
session->configfile = strdup(optarg); |
1477 |
dbg("configfile = %s\n", session->configfile); |
1478 |
|
1479 |
/* |
1480 |
* read the config file now. Yes, this could override |
1481 |
* previously set options from the command line, but |
1482 |
* the user asked for it... |
1483 |
*/ |
1484 |
parse_configfile(session); |
1485 |
break; |
1486 |
case 'h': |
1487 |
display_help(); |
1488 |
goto exit; |
1489 |
case 'n': |
1490 |
session->dry_run = 1; |
1491 |
break; |
1492 |
case 'v': |
1493 |
display_version(); |
1494 |
goto exit; |
1495 |
default: |
1496 |
display_help(); |
1497 |
goto exit; |
1498 |
} |
1499 |
} |
1500 |
|
1501 |
session_readline_init(session); |
1502 |
/* |
1503 |
* Show the version to make it easier to determine what |
1504 |
* is going on here |
1505 |
*/ |
1506 |
if (debug) |
1507 |
display_version(); |
1508 |
|
1509 |
if (session->host == HOST_TWITTER) { |
1510 |
if (!session->consumer_key || !session->consumer_secret) { |
1511 |
fprintf(stderr, |
1512 |
"Twitter no longer supports HTTP basic authentication.\n" |
1513 |
"Both consumer key, and consumer secret are required" |
1514 |
" for bti in order to behave as an OAuth consumer.\n"); |
1515 |
goto exit; |
1516 |
} |
1517 |
if (session->action == ACTION_GROUP) { |
1518 |
fprintf(stderr, "Groups only work in Identi.ca.\n"); |
1519 |
goto exit; |
1520 |
} |
1521 |
} else { |
1522 |
if (!session->consumer_key || !session->consumer_secret) |
1523 |
session->no_oauth = 1; |
1524 |
} |
1525 |
|
1526 |
if (session->no_oauth) { |
1527 |
if (!session->account) { |
1528 |
fprintf(stdout, "Enter account for %s: ", |
1529 |
session->hostname); |
1530 |
session->account = session->readline(NULL); |
1531 |
} |
1532 |
if (!session->password) { |
1533 |
read_password(password, sizeof(password), |
1534 |
session->hostname); |
1535 |
session->password = strdup(password); |
1536 |
} |
1537 |
} else { |
1538 |
if (!session->access_token_key || |
1539 |
!session->access_token_secret) { |
1540 |
request_access_token(session); |
1541 |
goto exit; |
1542 |
} |
1543 |
} |
1544 |
|
1545 |
if (session->action == ACTION_UNKNOWN) { |
1546 |
fprintf(stderr, "Unknown action, valid actions are:\n" |
1547 |
"'update', 'friends', 'public', 'replies', 'group' or 'user'.\n"); |
1548 |
goto exit; |
1549 |
} |
1550 |
|
1551 |
if (session->action == ACTION_GROUP && !session->group) { |
1552 |
fprintf(stdout, "Enter group name: "); |
1553 |
session->group = session->readline(NULL); |
1554 |
} |
1555 |
|
1556 |
if (session->action == ACTION_UPDATE) { |
1557 |
if (session->background || !session->interactive) |
1558 |
tweet = get_string_from_stdin(); |
1559 |
else |
1560 |
tweet = session->readline("tweet: "); |
1561 |
if (!tweet || strlen(tweet) == 0) { |
1562 |
dbg("no tweet?\n"); |
1563 |
return -1; |
1564 |
} |
1565 |
|
1566 |
if (session->shrink_urls) |
1567 |
tweet = shrink_urls(tweet); |
1568 |
|
1569 |
session->tweet = zalloc(strlen(tweet) + 10); |
1570 |
if (session->bash) |
1571 |
sprintf(session->tweet, "%c %s", |
1572 |
getuid() ? '$' : '#', tweet); |
1573 |
else |
1574 |
sprintf(session->tweet, "%s", tweet); |
1575 |
|
1576 |
free(tweet); |
1577 |
dbg("tweet = %s\n", session->tweet); |
1578 |
} |
1579 |
|
1580 |
if (session->page == 0) |
1581 |
session->page = 1; |
1582 |
dbg("config file = %s\n", session->configfile); |
1583 |
dbg("host = %d\n", session->host); |
1584 |
dbg("action = %d\n", session->action); |
1585 |
|
1586 |
/* fork ourself so that the main shell can get on |
1587 |
* with it's life as we try to connect and handle everything |
1588 |
*/ |
1589 |
if (session->background) { |
1590 |
child = fork(); |
1591 |
if (child) { |
1592 |
dbg("child is %d\n", child); |
1593 |
exit(0); |
1594 |
} |
1595 |
} |
1596 |
|
1597 |
retval = send_request(session); |
1598 |
if (retval && !session->background) |
1599 |
fprintf(stderr, "operation failed\n"); |
1600 |
|
1601 |
log_session(session, retval); |
1602 |
exit: |
1603 |
session_readline_cleanup(session); |
1604 |
session_free(session); |
1605 |
return retval;; |
1606 |
} |