X-Git-Url: https://git.toastfreeware.priv.at/gregoa/bti.git/blobdiff_plain/a2ba3bbc5e121d61af36921f8477a9f5d81dcfe0..9185ab0c0a2ea1db12bcb31f81aba7f54c36bdbc:/bti.c diff --git a/bti.c b/bti.c index a7fa43c..b5a01d3 100644 --- a/bti.c +++ b/bti.c @@ -40,7 +40,7 @@ #include #include #include - +#include "bti.h" #define zalloc(size) calloc(size, 1) @@ -53,63 +53,6 @@ static int debug; -static int verbose; - -enum host { - HOST_TWITTER = 0, - HOST_IDENTICA = 1, - HOST_CUSTOM = 2 -}; - -enum action { - ACTION_UPDATE = 0, - ACTION_FRIENDS = 1, - ACTION_USER = 2, - ACTION_REPLIES = 4, - ACTION_PUBLIC = 8, - ACTION_GROUP = 16, - ACTION_RETWEET = 32, - ACTION_UNKNOWN = 64 -}; - -struct session { - char *password; - char *account; - char *consumer_key; - char *consumer_secret; - char *access_token_key; - char *access_token_secret; - char *tweet; - char *proxy; - char *time; - char *homedir; - char *logfile; - char *user; - char *group; - char *hosturl; - char *hostname; - char *configfile; - char *replyto; - char *retweet; - int bash; - int background; - int interactive; - int shrink_urls; - int dry_run; - int page; - int no_oauth; - int guest; - enum host host; - enum action action; - void *readline_handle; - char *(*readline)(const char *); -}; - -struct bti_curl_buffer { - char *data; - enum action action; - int length; -}; static void display_help(void) { @@ -340,7 +283,8 @@ static CURL *curl_init(void) return curl; } -static void parse_statuses(xmlDocPtr doc, xmlNodePtr current) +static void parse_statuses(struct session *session, + xmlDocPtr doc, xmlNodePtr current) { xmlChar *text = NULL; xmlChar *user = NULL; @@ -370,7 +314,7 @@ static void parse_statuses(xmlDocPtr doc, xmlNodePtr current) } if (user && text && created && id) { - if (verbose) + if (session->verbose) printf("[%s] {%s} (%.16s) %s\n", user, id, created, text); else @@ -392,7 +336,7 @@ static void parse_statuses(xmlDocPtr doc, xmlNodePtr current) return; } -static void parse_timeline(char *document) +static void parse_timeline(char *document, struct session *session) { xmlDocPtr doc; xmlNodePtr current; @@ -418,7 +362,7 @@ static void parse_timeline(char *document) current = current->xmlChildrenNode; while (current != NULL) { if ((!xmlStrcmp(current->name, (const xmlChar *)"status"))) - parse_statuses(doc, current); + parse_statuses(session, doc, current); current = current->next; } xmlFreeDoc(doc); @@ -447,7 +391,7 @@ static size_t curl_callback(void *buffer, size_t size, size_t nmemb, 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); + parse_timeline(curl_buf->data, curl_buf->session); dbg("%s\n", curl_buf->data); @@ -597,6 +541,7 @@ static int send_request(struct session *session) curl_buf = bti_curl_buffer_alloc(session->action); if (!curl_buf) return -ENOMEM; + curl_buf->session = session; curl = curl_init(); if (!curl) @@ -750,30 +695,33 @@ static int send_request(struct session *session) break; } - if (is_post) { - req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC, - NULL, session->consumer_key, - session->consumer_secret, - session->access_token_key, - session->access_token_secret); - reply = oauth_http_post(req_url, postarg); - } else { - req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL, - session->consumer_key, - session->consumer_secret, - session->access_token_key, - session->access_token_secret); - reply = oauth_http_get(req_url, postarg); - } + dbg("%s\n", endpoint); + if (!session->dry_run) { + if (is_post) { + req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC, + NULL, session->consumer_key, + session->consumer_secret, + session->access_token_key, + session->access_token_secret); + reply = oauth_http_post(req_url, postarg); + } else { + req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL, + session->consumer_key, + session->consumer_secret, + session->access_token_key, + session->access_token_secret); + reply = oauth_http_get(req_url, postarg); + } - dbg("%s\n", req_url); - dbg("%s\n", reply); - if (req_url) - free(req_url); + dbg("%s\n", req_url); + dbg("%s\n", reply); + if (req_url) + free(req_url); + } if ((session->action != ACTION_UPDATE) && (session->action != ACTION_RETWEET)) - parse_timeline(reply); + parse_timeline(reply, session); } return 0; } @@ -893,7 +841,7 @@ static void parse_configfile(struct session *session) c += 8; if (!strncasecmp(c, "true", 4) || !strncasecmp(c, "yes", 3)) - verbose = 1; + session->verbose = 1; } else if (!strncasecmp(c,"retweet", 7) && (c[7] == '=')) { c += 8; @@ -1364,7 +1312,6 @@ int main(int argc, char *argv[], char *envp[]) int page_nr; debug = 0; - verbose = 0; session = session_alloc(); if (!session) { @@ -1407,7 +1354,7 @@ int main(int argc, char *argv[], char *envp[]) debug = 1; break; case 'V': - verbose = 1; + session->verbose = 1; break; case 'a': if (session->account)