]> ToastFreeware Gitweb - gregoa/bti.git/blobdiff - bti.c
create bti.h
[gregoa/bti.git] / bti.c
diff --git a/bti.c b/bti.c
index 0229a9a75fdf7a09fa0bc86a5fb177aa72909ae2..b5a01d3921cbeeed81233fd1f318a5330bb3a158 100644 (file)
--- a/bti.c
+++ b/bti.c
@@ -40,7 +40,7 @@
 #include <termios.h>
 #include <dlfcn.h>
 #include <oauth.h>
 #include <termios.h>
 #include <dlfcn.h>
 #include <oauth.h>
-
+#include "bti.h"
 
 #define zalloc(size)   calloc(size, 1)
 
 
 #define zalloc(size)   calloc(size, 1)
 
 
 
 static int debug;
 
 
 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;
-       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)
 {
 
 static void display_help(void)
 {
@@ -339,7 +283,8 @@ static CURL *curl_init(void)
        return curl;
 }
 
        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;
 {
        xmlChar *text = NULL;
        xmlChar *user = NULL;
@@ -369,7 +314,7 @@ static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
                        }
 
                        if (user && text && created && id) {
                        }
 
                        if (user && text && created && id) {
-                               if (verbose)
+                               if (session->verbose)
                                        printf("[%s] {%s} (%.16s) %s\n",
                                                user, id, created, text);
                                else
                                        printf("[%s] {%s} (%.16s) %s\n",
                                                user, id, created, text);
                                else
@@ -391,7 +336,7 @@ static void parse_statuses(xmlDocPtr doc, xmlNodePtr current)
        return;
 }
 
        return;
 }
 
-static void parse_timeline(char *document)
+static void parse_timeline(char *document, struct session *session)
 {
        xmlDocPtr doc;
        xmlNodePtr current;
 {
        xmlDocPtr doc;
        xmlNodePtr current;
@@ -417,7 +362,7 @@ static void parse_timeline(char *document)
        current = current->xmlChildrenNode;
        while (current != NULL) {
                if ((!xmlStrcmp(current->name, (const xmlChar *)"status")))
        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);
                current = current->next;
        }
        xmlFreeDoc(doc);
@@ -446,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)
        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);
 
 
        dbg("%s\n", curl_buf->data);
 
@@ -592,10 +537,11 @@ static int send_request(struct session *session)
        if (!session->hosturl)
                session->hosturl = strdup(twitter_host);
 
        if (!session->hosturl)
                session->hosturl = strdup(twitter_host);
 
-       if (session->no_oauth) {
+       if (session->no_oauth || session->guest) {
                curl_buf = bti_curl_buffer_alloc(session->action);
                if (!curl_buf)
                        return -ENOMEM;
                curl_buf = bti_curl_buffer_alloc(session->action);
                if (!curl_buf)
                        return -ENOMEM;
+               curl_buf->session = session;
 
                curl = curl_init();
                if (!curl)
 
                curl = curl_init();
                if (!curl)
@@ -749,30 +695,33 @@ static int send_request(struct session *session)
                        break;
                }
 
                        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))
 
                if ((session->action != ACTION_UPDATE) &&
                                (session->action != ACTION_RETWEET))
-                       parse_timeline(reply);
+                       parse_timeline(reply, session);
        }
        return 0;
 }
        }
        return 0;
 }
@@ -892,7 +841,7 @@ static void parse_configfile(struct session *session)
                        c += 8;
                        if (!strncasecmp(c, "true", 4) ||
                                        !strncasecmp(c, "yes", 3))
                        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;
                } else if (!strncasecmp(c,"retweet", 7) &&
                                (c[7] == '=')) {
                        c += 8;
@@ -1363,7 +1312,6 @@ int main(int argc, char *argv[], char *envp[])
        int page_nr;
 
        debug = 0;
        int page_nr;
 
        debug = 0;
-       verbose = 0;
 
        session = session_alloc();
        if (!session) {
 
        session = session_alloc();
        if (!session) {
@@ -1406,7 +1354,7 @@ int main(int argc, char *argv[], char *envp[])
                        debug = 1;
                        break;
                case 'V':
                        debug = 1;
                        break;
                case 'V':
-                       verbose = 1;
+                       session->verbose = 1;
                        break;
                case 'a':
                        if (session->account)
                        break;
                case 'a':
                        if (session->account)
@@ -1544,11 +1492,17 @@ int main(int argc, char *argv[], char *envp[])
 
        if (session->host == HOST_TWITTER) {
                if (!session->consumer_key || !session->consumer_secret) {
 
        if (session->host == HOST_TWITTER) {
                if (!session->consumer_key || !session->consumer_secret) {
-                       fprintf(stderr,
-                               "Twitter no longer supports HTTP basic authentication.\n"
-                               "Both consumer key, and consumer secret are required"
-                               " for bti in order to behave as an OAuth consumer.\n");
-                       goto exit;
+                       if (session->action == ACTION_USER ||
+                                       session->action == ACTION_PUBLIC) {
+                               /* Some actions may still work without authentication */
+                               session->guest = 1;
+                       } else {
+                               fprintf(stderr,
+                                               "Twitter no longer supports HTTP basic authentication.\n"
+                                               "Both consumer key, and consumer secret are required"
+                                               " for bti in order to behave as an OAuth consumer.\n");
+                               goto exit;
+                       }
                }
                if (session->action == ACTION_GROUP) {
                        fprintf(stderr, "Groups only work in Identi.ca.\n");
                }
                if (session->action == ACTION_GROUP) {
                        fprintf(stderr, "Groups only work in Identi.ca.\n");
@@ -1570,7 +1524,7 @@ int main(int argc, char *argv[], char *envp[])
                                      session->hostname);
                        session->password = strdup(password);
                }
                                      session->hostname);
                        session->password = strdup(password);
                }
-       } else {
+       } else if (!session->guest) {
                if (!session->access_token_key ||
                    !session->access_token_secret) {
                        request_access_token(session);
                if (!session->access_token_key ||
                    !session->access_token_secret) {
                        request_access_token(session);