-
Notifications
You must be signed in to change notification settings - Fork 31
/
bti.c
1841 lines (1622 loc) · 44.5 KB
/
bti.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008-2011 Greg Kroah-Hartman <[email protected]>
* Copyright (C) 2009 Bart Trojanowski <[email protected]>
* Copyright (C) 2009-2010 Amir Mohammad Saied <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <curl/curl.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <json-c/json.h>
#include <pcre.h>
#include <termios.h>
#include <dlfcn.h>
#include <oauth.h>
#include "bti.h"
#define zalloc(size) calloc(size, 1)
#define dbg(format, arg...) \
do { \
if (debug) \
fprintf(stdout, "bti: %s: " format , __func__ , \
## arg); \
} while (0)
int debug;
static void display_help(void)
{
fprintf(stdout, "bti - send tweet to twitter\n"
"Version: %s\n"
"Usage:\n"
" bti [options]\n"
"options are:\n"
" --account accountname\n"
" --password password\n"
" --action action\n"
" ('update', 'friends', 'public', 'replies', 'user', or 'direct')\n"
" --user screenname\n"
" --proxy PROXY:PORT\n"
" --host HOST\n"
" --logfile logfile\n"
" --config configfile\n"
" --replyto ID\n"
" --retweet ID\n"
" --shrink-urls\n"
" --page PAGENUMBER\n"
" --column COLUMNWIDTH\n"
" --bash\n"
" --background\n"
" --debug\n"
" --verbose\n"
" --dry-run\n"
" --version\n"
" --help\n", VERSION);
}
static int strlen_utf8(char *s)
{
int i = 0, j = 0;
while (s[i]) {
if ((s[i] & 0xc0) != 0x80)
j++;
i++;
}
return j;
}
static void display_version(void)
{
fprintf(stdout, "bti - version %s\n", VERSION);
}
static char *get_string(const char *name)
{
char *temp;
char *string;
string = zalloc(1000);
if (!string)
exit(1);
if (name != NULL)
fprintf(stdout, "%s", name);
if (!fgets(string, 999, stdin)) {
free(string);
return NULL;
}
temp = strchr(string, '\n');
if (temp)
*temp = '\0';
return string;
}
/*
* Try to get a handle to a readline function from a variety of different
* libraries. If nothing is present on the system, then fall back to an
* internal one.
*
* Logic originally based off of code in the e2fsutils package in the
* lib/ss/get_readline.c file, which is licensed under the MIT license.
*
* This keeps us from having to relicense the bti codebase if readline
* ever changes its license, as there is no link-time dependency.
* It is a run-time thing only, and we handle any readline-like library
* in the same manner, making bti not be a derivative work of any
* other program.
*/
static void session_readline_init(struct session *session)
{
/* Libraries we will try to use for readline/editline functionality */
const char *libpath = "libreadline.so.6:libreadline.so.5:"
"libreadline.so.4:libreadline.so:libedit.so.2:"
"libedit.so:libeditline.so.0:libeditline.so";
void *handle = NULL;
char *tmp, *cp, *next;
int (*bind_key)(int, void *);
void (*insert)(void);
/* default to internal function if we can't or won't find anything */
session->readline = get_string;
if (!isatty(0))
return;
session->interactive = 1;
tmp = malloc(strlen(libpath)+1);
if (!tmp)
return;
strcpy(tmp, libpath);
for (cp = tmp; cp; cp = next) {
next = strchr(cp, ':');
if (next)
*next++ = 0;
if (*cp == 0)
continue;
handle = dlopen(cp, RTLD_NOW);
if (handle) {
dbg("Using %s for readline library\n", cp);
break;
}
}
free(tmp);
if (!handle) {
dbg("No readline library found.\n");
return;
}
session->readline_handle = handle;
session->readline = (char *(*)(const char *))dlsym(handle, "readline");
if (session->readline == NULL) {
/* something odd happened, default back to internal stuff */
session->readline_handle = NULL;
session->readline = get_string;
return;
}
/*
* If we found a library, turn off filename expansion
* as that makes no sense from within bti.
*/
bind_key = (int (*)(int, void *))dlsym(handle, "rl_bind_key");
insert = (void (*)(void))dlsym(handle, "rl_insert");
if (bind_key && insert)
bind_key('\t', insert);
}
static void session_readline_cleanup(struct session *session)
{
if (session->readline_handle)
dlclose(session->readline_handle);
}
static struct session *session_alloc(void)
{
struct session *session;
session = zalloc(sizeof(*session));
if (!session)
return NULL;
return session;
}
static void session_free(struct session *session)
{
if (!session)
return;
free(session->retweet);
free(session->replyto);
free(session->password);
free(session->account);
free(session->consumer_key);
free(session->consumer_secret);
free(session->access_token_key);
free(session->access_token_secret);
free(session->tweet);
free(session->proxy);
free(session->time);
free(session->homedir);
free(session->user);
free(session->hosturl);
free(session->hostname);
free(session->configfile);
free(session);
}
static struct bti_curl_buffer *bti_curl_buffer_alloc(enum action action)
{
struct bti_curl_buffer *buffer;
buffer = zalloc(sizeof(*buffer));
if (!buffer)
return NULL;
/* start out with a data buffer of 1 byte to
* make the buffer fill logic simpler */
buffer->data = zalloc(1);
if (!buffer->data) {
free(buffer);
return NULL;
}
buffer->length = 0;
buffer->action = action;
return buffer;
}
static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
{
if (!buffer)
return;
free(buffer->data);
free(buffer);
}
const char twitter_host[] = "https://api.twitter.com/1.1/statuses";
const char twitter_host_stream[] = "https://stream.twitter.com/1.1/statuses"; /*this is not reset, and doesnt work */
const char twitter_host_simple[] = "https://api.twitter.com/1.1";
const char twitter_name[] = "twitter";
static const char twitter_request_token_uri[] = "https://twitter.com/oauth/request_token?oauth_callback=oob";
static const char twitter_access_token_uri[] = "https://twitter.com/oauth/access_token";
static const char twitter_authorize_uri[] = "https://twitter.com/oauth/authorize?oauth_token=";
static const char custom_request_token_uri[] = "/../oauth/request_token?oauth_callback=oob";
static const char custom_access_token_uri[] = "/../oauth/access_token";
static const char custom_authorize_uri[] = "/../oauth/authorize?oauth_token=";
static const char user_uri[] = "/user_timeline.json";
static const char update_uri[] = "/update.json";
static const char public_uri[] = "/sample.json";
static const char friends_uri[] = "/home_timeline.json";
static const char mentions_uri[] = "/mentions_timeline.json";
static const char replies_uri[] = "/replies.xml";
static const char retweet_uri[] = "/retweet/";
/*static const char direct_uri[] = "/direct_messages/new.xml";*/
static const char direct_uri[] = "/direct_messages/new.json";
static const char config_default[] = "/etc/bti";
static const char config_xdg_default[] = ".config/bti";
static const char config_user_default[] = ".bti";
static CURL *curl_init(void)
{
CURL *curl;
curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "Can not init CURL!\n");
return NULL;
}
/* some ssl sanity checks on the connection we are making */
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
return curl;
}
static void find_config_file(struct session *session)
{
struct stat s;
char *home;
char *file;
int homedir_size;
/*
* Get the home directory so we can try to find a config file.
* If we have no home dir set up, look in /etc/bti
*/
home = getenv("HOME");
if (!home) {
/* No home dir, so take the defaults and get out of here */
session->homedir = strdup("");
session->configfile = strdup(config_default);
return;
}
/* We have a home dir, so this might be a user */
session->homedir = strdup(home);
homedir_size = strlen(session->homedir);
/*
* Try to find a config file, we do so in this order:
* ~/.bti old-school config file
* ~/.config/bti new-school config file
*/
file = zalloc(homedir_size + strlen(config_user_default) + 7);
sprintf(file, "%s/%s", home, config_user_default);
if (stat(file, &s) == 0) {
/* Found the config file at ~/.bti */
session->configfile = strdup(file);
free(file);
return;
}
free(file);
file = zalloc(homedir_size + strlen(config_xdg_default) + 7);
sprintf(file, "%s/%s", home, config_xdg_default);
if (stat(file, &s) == 0) {
/* config file is at ~/.config/bti */
session->configfile = strdup(file);
free(file);
return;
}
/* No idea where the config file is, so punt */
free(file);
session->configfile = strdup("");
}
/* The final place data is sent to the screen/pty/tty */
static void bti_output_line(struct session *session, xmlChar *user,
xmlChar *id, xmlChar *created, xmlChar *text)
{
if (session->verbose)
printf("[%*s] {%s} (%.16s) %s\n", -session->column_output, user,
id, created, text);
else
printf("[%*s] %s\n", -session->column_output, user, text);
}
static void parse_statuses(struct session *session,
xmlDocPtr doc, xmlNodePtr current)
{
xmlChar *text = NULL;
xmlChar *user = NULL;
xmlChar *created = NULL;
xmlChar *id = NULL;
xmlNodePtr userinfo;
current = current->xmlChildrenNode;
while (current != NULL) {
if (current->type == XML_ELEMENT_NODE) {
if (!xmlStrcmp(current->name, (const xmlChar *)"created_at"))
created = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
if (!xmlStrcmp(current->name, (const xmlChar *)"text"))
text = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
if (!xmlStrcmp(current->name, (const xmlChar *)"id"))
id = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
if (!xmlStrcmp(current->name, (const xmlChar *)"user")) {
userinfo = current->xmlChildrenNode;
while (userinfo != NULL) {
if ((!xmlStrcmp(userinfo->name, (const xmlChar *)"screen_name"))) {
if (user)
xmlFree(user);
user = xmlNodeListGetString(doc, userinfo->xmlChildrenNode, 1);
}
userinfo = userinfo->next;
}
}
if (user && text && created && id) {
bti_output_line(session, user, id,
created, text);
xmlFree(user);
xmlFree(text);
xmlFree(created);
xmlFree(id);
user = NULL;
text = NULL;
created = NULL;
id = NULL;
}
}
current = current->next;
}
return;
}
static void parse_timeline(char *document, struct session *session)
{
xmlDocPtr doc;
xmlNodePtr current;
doc = xmlReadMemory(document, strlen(document), "timeline.xml",
NULL, XML_PARSE_NOERROR);
if (doc == NULL)
return;
current = xmlDocGetRootElement(doc);
if (current == NULL) {
fprintf(stderr, "empty document\n");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(current->name, (const xmlChar *) "statuses")) {
fprintf(stderr, "unexpected document type\n");
xmlFreeDoc(doc);
return;
}
current = current->xmlChildrenNode;
while (current != NULL) {
if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
parse_statuses(session, doc, current);
current = current->next;
}
xmlFreeDoc(doc);
return;
}
/* avoids the c99 option */
#define json_object_object_foreach_alt(obj,key,val) \
char *key; \
struct json_object *val; \
struct lh_entry *entry; \
for (entry = json_object_get_object(obj)->head; \
({ if(entry && !is_error(entry)) { \
key = (char*)entry->k; \
val = (struct json_object*)entry->v; \
} ; entry; }); \
entry = entry->next )
/* Forward Declaration */
static void json_parse(json_object * jobj, int nestlevel);
static void print_json_value(json_object *jobj, int nestlevel)
{
enum json_type type;
type = json_object_get_type(jobj);
switch (type) {
case json_type_boolean:
printf("boolean ");
printf("value: %s\n", json_object_get_boolean(jobj)? "true": "false");
break;
case json_type_double:
printf("double ");
printf("value: %lf\n", json_object_get_double(jobj));
break;
case json_type_int:
printf("int ");
printf("value: %d\n", json_object_get_int(jobj));
break;
case json_type_string:
printf("string ");
printf("value: %s\n", json_object_get_string(jobj));
break;
default:
break;
}
}
#define MAXKEYSTACK 20
char *keystack[MAXKEYSTACK];
static void json_parse_array(json_object *jobj, char *key, int nestlevel)
{
enum json_type type;
nestlevel++;
/* Simply get the array */
json_object *jarray = jobj;
if (key) {
/* Get the array if it is a key value pair */
jarray = json_object_object_get(jobj, key);
}
/* Get the length of the array */
int arraylen = json_object_array_length(jarray);
if (debug)
printf("Array Length: %d\n",arraylen);
int i;
json_object *jvalue;
for (i = 0; i < arraylen; i++) {
if (debug) {
int j;
for (j=0; j < nestlevel; ++j)
printf(" ");
printf("element[%d]\n",i);
}
/* Get the array element at position i */
jvalue = json_object_array_get_idx(jarray, i);
type = json_object_get_type(jvalue);
if (type == json_type_array) {
json_parse_array(jvalue, NULL, nestlevel);
} else if (type != json_type_object) {
if (debug) {
printf("value[%d]: ", i);
print_json_value(jvalue,nestlevel);
}
} else {
/* printf("obj: "); */
keystack[nestlevel%MAXKEYSTACK]="[]";
json_parse(jvalue,nestlevel);
}
}
}
struct results {
int code;
char *message;
} results;
struct session *store_session;
struct tweetdetail {
char *id;
char *text;
char *screen_name;
char *created_at;
} tweetdetail;
static void json_interpret(json_object *jobj, int nestlevel)
{
if (nestlevel == 3 &&
strcmp(keystack[1], "errors") == 0 &&
strcmp(keystack[2], "[]") == 0) {
if (strcmp(keystack[3], "message") == 0) {
if (json_object_get_type(jobj) == json_type_string)
results.message = (char *)json_object_get_string(jobj);
}
if (strcmp(keystack[3], "code") == 0) {
if (json_object_get_type(jobj) == json_type_int)
results.code = json_object_get_int(jobj);
}
}
if (nestlevel >= 2 &&
strcmp(keystack[1],"[]") == 0) {
if (strcmp(keystack[2], "created_at") == 0) {
if (debug)
printf("%s : %s\n", keystack[2], json_object_get_string(jobj));
tweetdetail.created_at = (char *)json_object_get_string(jobj);
}
if (strcmp(keystack[2], "text") == 0) {
if (debug)
printf("%s : %s\n", keystack[2], json_object_get_string(jobj));
tweetdetail.text = (char *)json_object_get_string(jobj);
}
if (strcmp(keystack[2], "id") == 0) {
if (debug)
printf("%s : %s\n", keystack[2], json_object_get_string(jobj));
tweetdetail.id = (char *)json_object_get_string(jobj);
}
if (nestlevel >= 3 &&
strcmp(keystack[2], "user") == 0) {
if (strcmp(keystack[3], "screen_name") == 0) {
if (debug)
printf("%s->%s : %s\n", keystack[2], keystack[3], json_object_get_string(jobj));
tweetdetail.screen_name=(char *)json_object_get_string(jobj);
bti_output_line(store_session,
(xmlChar *)tweetdetail.screen_name,
(xmlChar *)tweetdetail.id,
(xmlChar *)tweetdetail.created_at,
(xmlChar *)tweetdetail.text);
}
}
}
}
/* Parsing the json object */
static void json_parse(json_object * jobj, int nestlevel)
{
int i;
if (jobj==NULL) {
fprintf(stderr,"jobj null\n");
return;
}
nestlevel++;
enum json_type type;
json_object_object_foreach_alt(jobj, key, val) {
/* work around pre10 */
if (val)
type = json_object_get_type(val);
else
type=json_type_null;
if (debug)
for (i = 0; i < nestlevel; ++i)
printf(" ");
if (debug)
printf("key %-34s ", key);
if (debug)
for (i = 0; i < 8 - nestlevel; ++i)
printf(" ");
switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string:
if (debug) print_json_value(val,nestlevel);
if (debug) for (i=0; i<nestlevel+1; ++i) printf(" ");
if (debug) printf("(");
if (debug) for (i=1; i<nestlevel; ++i) { printf("%s->",keystack[i]); }
if (debug) printf("%s)\n",key);
keystack[nestlevel%MAXKEYSTACK] = key;
json_interpret(val,nestlevel);
break;
case json_type_object:
if (debug) printf("json_type_object\n");
keystack[nestlevel%MAXKEYSTACK] = key;
json_parse(json_object_object_get(jobj, key), nestlevel);
break;
case json_type_array:
if (debug) printf("json_type_array, ");
keystack[nestlevel%MAXKEYSTACK] = key;
json_parse_array(jobj, key, nestlevel);
break;
case json_type_null:
if (debug) printf("null\n");
break;
default:
if (debug) printf("\n");
break;
}
}
}
static int parse_response_json(char *document, struct session *session)
{
dbg("Got this json response:\n");
dbg("%s\n",document);
results.code=0;
results.message=NULL;
json_object *jobj = json_tokener_parse(document);
/* make global for now */
store_session = session;
if (!is_error(jobj)) {
/* guards against a json pre 0.10 bug */
json_parse(jobj,0);
}
if (results.code && results.message != NULL) {
if (debug)
printf("Got an error code:\n code=%d\n message=%s\n",
results.code, results.message);
fprintf(stderr, "error condition detected: %d = %s\n",
results.code, results.message);
return -EIO;
}
return 0;
}
static void parse_timeline_json(char *document, struct session *session)
{
dbg("Got this json response:\n");
dbg("%s\n",document);
results.code = 0;
results.message = NULL;
json_object *jobj = json_tokener_parse(document);
/* make global for now */
store_session = session;
if (!is_error(jobj)) {
/* guards against a json pre 0.10 bug */
if (json_object_get_type(jobj)==json_type_array) {
json_parse_array(jobj, NULL, 0);
} else {
json_parse(jobj,0);
}
}
if (results.code && results.message != NULL) {
if (debug)
printf("Got an error code:\n code=%d\n message=%s\n",
results.code, results.message);
fprintf(stderr, "error condition detected: %d = %s\n",
results.code, results.message);
}
}
static size_t curl_callback(void *buffer, size_t size, size_t nmemb,
void *userp)
{
struct bti_curl_buffer *curl_buf = userp;
size_t buffer_size = size * nmemb;
char *temp;
if ((!buffer) || (!buffer_size) || (!curl_buf))
return -EINVAL;
/* add to the data we already have */
temp = zalloc(curl_buf->length + buffer_size + 1);
if (!temp)
return -ENOMEM;
memcpy(temp, curl_buf->data, curl_buf->length);
free(curl_buf->data);
curl_buf->data = temp;
memcpy(&curl_buf->data[curl_buf->length], (char *)buffer, buffer_size);
curl_buf->length += buffer_size;
if (curl_buf->action)
parse_timeline(curl_buf->data, curl_buf->session);
dbg("%s\n", curl_buf->data);
return buffer_size;
}
static int parse_osp_reply(const char *reply, char **token, char **secret)
{
int rc;
int retval = 1;
char **rv = NULL;
rc = oauth_split_url_parameters(reply, &rv);
qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
if (rc == 2 || rc == 4 || rc == 5) {
if (!strncmp(rv[0], "oauth_token=", 11) &&
!strncmp(rv[1], "oauth_token_secret=", 18)) {
if (token)
*token = strdup(&(rv[0][12]));
if (secret)
*secret = strdup(&(rv[1][19]));
retval = 0;
}
} else if (rc == 3) {
if (!strncmp(rv[1], "oauth_token=", 11) &&
!strncmp(rv[2], "oauth_token_secret=", 18)) {
if (token)
*token = strdup(&(rv[1][12]));
if (secret)
*secret = strdup(&(rv[2][19]));
retval = 0;
}
}
dbg("token: %s\n", *token);
dbg("secret: %s\n", *secret);
if (rv)
free(rv);
return retval;
}
static int request_access_token(struct session *session)
{
char *post_params = NULL;
char *request_url = NULL;
char *reply = NULL;
char *at_key = NULL;
char *at_secret = NULL;
char *verifier = NULL;
char at_uri[90];
char token_uri[90];
if (!session)
return -EINVAL;
if (session->host == HOST_TWITTER)
request_url = oauth_sign_url2(
twitter_request_token_uri, NULL,
OA_HMAC, NULL, session->consumer_key,
session->consumer_secret, NULL, NULL);
else {
sprintf(token_uri, "%s%s",
session->hosturl, custom_request_token_uri);
request_url = oauth_sign_url2(
token_uri, NULL,
OA_HMAC, NULL, session->consumer_key,
session->consumer_secret, NULL, NULL);
}
reply = oauth_http_get(request_url, post_params);
if (request_url)
free(request_url);
if (post_params)
free(post_params);
if (!reply)
return 1;
if (parse_osp_reply(reply, &at_key, &at_secret))
return 1;
free(reply);
fprintf(stdout,
"Please open the following link in your browser, and "
"allow 'bti' to access your account. Then paste "
"back the provided PIN in here.\n");
if (session->host == HOST_TWITTER) {
fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, at_key);
verifier = session->readline(NULL);
sprintf(at_uri, "%s?oauth_verifier=%s",
twitter_access_token_uri, verifier);
} else {
fprintf(stdout, "%s%s%s\nPIN: ",
session->hosturl, custom_authorize_uri, at_key);
verifier = session->readline(NULL);
sprintf(at_uri, "%s%s?oauth_verifier=%s",
session->hosturl, custom_access_token_uri, verifier);
}
request_url = oauth_sign_url2(at_uri, NULL, OA_HMAC, NULL,
session->consumer_key,
session->consumer_secret,
at_key, at_secret);
reply = oauth_http_get(request_url, post_params);
if (!reply)
return 1;
if (parse_osp_reply(reply, &at_key, &at_secret))
return 1;
free(reply);
fprintf(stdout,
"Please put these two lines in your bti "
"configuration file (%s):\n"
"access_token_key=%s\n"
"access_token_secret=%s\n",
session->configfile, at_key, at_secret);
return 0;
}
static int send_request(struct session *session)
{
const int endpoint_size = 2000;
char endpoint[endpoint_size];
char user_password[500];
char data[500];
struct bti_curl_buffer *curl_buf;
CURL *curl = NULL;
CURLcode res;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *slist = NULL;
char *req_url = NULL;
char *reply = NULL;
char *postarg = NULL;
char *escaped_tweet = NULL;
int is_post = 0;
if (!session)
return -EINVAL;
if (!session->hosturl)
session->hosturl = strdup(twitter_host);
if (session->no_oauth || session->guest) {
curl_buf = bti_curl_buffer_alloc(session->action);
if (!curl_buf)
return -ENOMEM;
curl_buf->session = session;
curl = curl_init();
if (!curl) {
bti_curl_buffer_free(curl_buf);
return -EINVAL;
}
if (!session->hosturl)
session->hosturl = strdup(twitter_host);
switch (session->action) {
case ACTION_UPDATE:
snprintf(user_password, sizeof(user_password), "%s:%s",
session->account, session->password);
snprintf(data, sizeof(data), "status=\"%s\"",
session->tweet);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "status",
CURLFORM_COPYCONTENTS, session->tweet,
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "source",
CURLFORM_COPYCONTENTS, "bti",
CURLFORM_END);
if (session->replyto)
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME,
"in_reply_to_status_id",
CURLFORM_COPYCONTENTS,
session->replyto,
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
slist = curl_slist_append(slist, "Expect:");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
snprintf(endpoint, endpoint_size, "%s%s", session->hosturl, update_uri);
curl_easy_setopt(curl, CURLOPT_URL, endpoint);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
break;
case ACTION_FRIENDS:
snprintf(user_password, sizeof(user_password), "%s:%s",
session->account, session->password);
snprintf(endpoint, endpoint_size, "%s%s?page=%d", session->hosturl,
friends_uri, session->page);
curl_easy_setopt(curl, CURLOPT_URL, endpoint);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
break;
case ACTION_USER:
snprintf(endpoint, endpoint_size, "%s%s%s.xml?page=%d", session->hosturl,
user_uri, session->user, session->page);
curl_easy_setopt(curl, CURLOPT_URL, endpoint);
break;
case ACTION_REPLIES:
snprintf(user_password, sizeof(user_password), "%s:%s",
session->account, session->password);
snprintf(endpoint, endpoint_size, "%s%s?page=%d", session->hosturl,
replies_uri, session->page);
curl_easy_setopt(curl, CURLOPT_URL, endpoint);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_password);
break;
case ACTION_PUBLIC:
/*snprintf(endpoint, endpoint_size, "%s%s?page=%d", session->hosturl,*/
snprintf(endpoint, endpoint_size, "%s%s", twitter_host_stream,
public_uri);
curl_easy_setopt(curl, CURLOPT_URL, endpoint);
break;
case ACTION_DIRECT:
/* NOT IMPLEMENTED - twitter requires authentication anyway */
break;
default:
break;
}
if (session->proxy)
curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy);
if (debug)
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
dbg("user_password = %s\n", user_password);
dbg("data = %s\n", data);
dbg("proxy = %s\n", session->proxy);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf);
if (!session->dry_run) {
res = curl_easy_perform(curl);
if (!session->background) {
xmlDocPtr doc;
xmlNodePtr current;
if (res) {
fprintf(stderr,
"error(%d) trying to perform operation\n",
res);
curl_easy_cleanup(curl);
if (session->action == ACTION_UPDATE)
curl_formfree(formpost);
bti_curl_buffer_free(curl_buf);
return -EINVAL;
}