changeset 357:732de90812d8

- fixed memory leaks - some code cleanups
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Fri, 27 Aug 2010 01:13:44 +0900
parents e8e51c70793c
children b4e18baed486
files twitter_api.c twitter_api.h
diffstat 2 files changed, 25 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/twitter_api.c	Thu Aug 26 15:05:57 2010 +0900
+++ b/twitter_api.c	Fri Aug 27 01:13:44 2010 +0900
@@ -17,6 +17,10 @@
 #define TYPE_GET  0
 #define TYPE_POST 1
 
+char *c_key = "wrD3WGIh2P31d3fIjRkfcw";
+char *c_sec = "ZEhViGY8P5IPjwgV8EVOkdjHhShRAZ9yhlYw0ZDXU";
+char *SAMPLE_NONCE = "0123456789abcdefghijk";
+
 typedef struct oauth_request {
     char *url;
     char *c_key;
@@ -36,7 +40,6 @@
 char *make_oauth_post(oauth_request_t *auth_req);
 static void oauth_setup_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, size_t len, const gchar *error_message);
 
-
 #ifdef _WIN32
 extern gboolean blink_state;
 extern gboolean blink_modified;
@@ -45,14 +48,8 @@
 extern guint64 reply_to_msgid;
 extern PurpleAccount *account_for_twitter;
 
-char *request_token_url = "http://twitter.com/oauth/request_token";
-char *access_token_url  = "http://twitter.com/oauth/access_token";
-char *authorize_url     = "http://twitter.com/oauth/authorize";
 
-char *c_key = "wrD3WGIh2P31d3fIjRkfcw";
-char *c_sec = "ZEhViGY8P5IPjwgV8EVOkdjHhShRAZ9yhlYw0ZDXU";
-char *SAMPLE_NONCE = "0123456789abcdefghijk";
-
+/* oauth functions */
 void
 oauth_access_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
                         const gchar *url_text, size_t len,
@@ -104,13 +101,13 @@
     char *oauth = NULL;
     char *request = NULL;
     oauth_request_t *oauth_req = (oauth_request_t *)data;
+
 /*
 request URL:http://twitter.com/oauth/access_token?
     oauth_consumer_key=wrD3WGIh2P31d3fIjRkfcw&
     oauth_nonce=QUzhkt0AO3tjNrR&
     oauth_signature_method=HMAC-SHA1&
     oauth_timestamp=1282450223&
-
     oauth_token=VL126k8KRNXid7Q7ZHYHh05PuASunVaPzyzrozf14&
     oauth_verifier=9772286&
     oauth_version=1.0&
@@ -119,7 +116,7 @@
 
     /* access token*/
     g_free(oauth_req->url);
-    oauth_req->url = g_strdup(access_token_url);
+    oauth_req->url = g_strdup(ACCESS_TOKEN_URL);
     oauth_req->verifier = g_strdup(pin);
     oauth_req->type = TYPE_GET;
     oauth_req->notoken = FALSE;
@@ -128,7 +125,7 @@
 
     oauth = make_oauth_get(oauth_req);
 
-    request = g_strdup_printf("%s?%s", access_token_url, oauth);
+    request = g_strdup_printf("%s?%s", ACCESS_TOKEN_URL, oauth);
     twitter_debug("request=%s\n", request);
 
     purple_util_fetch_url_request(request, TRUE,
@@ -160,6 +157,7 @@
     oauth_token_secret=EMD3u1piAKPsQnq44Its9f8WmIReYnUFcJIgd3niu4&
     oauth_callback_confirmed=true
 */
+
     /* separate key and secret */
     f = strstr(url_text, "oauth_token=");
     if(!f)
@@ -179,12 +177,12 @@
     g_free(oauth_req->a_sec);
     oauth_req->a_sec = g_strndup(f+19, e-f-19);
 
-    /* redirect twitter's authorization url */
-    char *uri = g_strdup_printf("%s?oauth_token=%s", authorize_url, oauth_req->a_key);
+    /* redirect to twitter's authorization url */
+    char *uri = g_strdup_printf("%s?oauth_token=%s", AUTHORIZE_URL, oauth_req->a_key);
     twitter_debug("auth uri=%s\n", uri);
     purple_notify_uri(conn, uri);
 
-    /* show dialog to wait PIN number*/
+    /* show dialog to wait PIN */
     purple_request_input(conn,
                          "PIN",
                          "Enter PIN",
@@ -208,7 +206,8 @@
 {
     char *oauth = NULL;
     char *request = NULL;
-    oauth_request_t *oauth_req = g_new0(oauth_request_t, 1);
+    oauth_request_t *oauth_req = g_new0(oauth_request_t, 1); /* persistent */
+
 /*
 http://twitter.com/oauth/request_token?
     oauth_consumer_key=wrD3WGIh2P31d3fIjRkfcw&
@@ -218,7 +217,7 @@
     oauth_signature=A%2BZIiUVsQv5ZR8u%2F2oLmUFX1eHE%3D
 */
 
-    oauth_req->url = strdup(request_token_url);
+    oauth_req->url = strdup(REQUEST_TOKEN_URL);
     oauth_req->c_key = strdup(c_key);
     oauth_req->c_sec = strdup(c_sec);
     oauth_req->a_key = NULL;
@@ -229,7 +228,7 @@
 
     /* request token*/
     oauth = make_oauth_get(oauth_req);
-    request = g_strdup_printf("%s?%s", request_token_url, oauth);
+    request = g_strdup_printf("%s?%s", REQUEST_TOKEN_URL, oauth);
 
     twitter_debug("request=%s\n", request);
 
@@ -720,6 +719,7 @@
     oauth_req.notoken = FALSE;
 
     oauth = make_oauth_get(&oauth_req);
+    g_free(url0);
 
     /* header */
     header = g_strdup_printf(TWITTER_STATUS_GET, oauth);
@@ -735,7 +735,7 @@
     g_free(header);
     g_free(request);
     g_free(oauth);
-    g_free(url0);
+
     return TRUE;
 }
 
@@ -935,10 +935,10 @@
     oauth_req.notoken = FALSE;
 
     oauth = make_oauth_post(&oauth_req);
+    g_free(url0);
 
     reply_to_msgid = 0;
 
-
     header = g_strdup_printf(TWITTER_STATUS_POST,
                              (int)strlen(oauth));
 
@@ -1000,6 +1000,7 @@
     oauth_req.notoken = FALSE;
 
     oauth = make_oauth_post(&oauth_req);
+    g_free(url0);
 
     header = g_strdup_printf(TWITTER_FAV_POST,
                              (long long unsigned int)id,
@@ -1067,6 +1068,7 @@
     oauth_req.notoken = FALSE;
 
     oauth = make_oauth_post(&oauth_req);
+    g_free(url0);
 
     header = g_strdup_printf(TWITTER_RETWEET_POST,
                              (long long unsigned int)id,
--- a/twitter_api.h	Thu Aug 26 15:05:57 2010 +0900
+++ b/twitter_api.h	Fri Aug 27 01:13:44 2010 +0900
@@ -3,12 +3,14 @@
 
 #include <cipher.h> /* from libpurple */
 #include <request.h>
-//#include <conversation.h>
 #include <math.h>
 
 /* twitter API specific macros */
-#define TWITTER_BASE_URL "http://twitter.com"
+#define TWITTER_BASE_URL  "http://twitter.com"
 #define TWITTER_API_BASE_URL "http://api.twitter.com"
+#define REQUEST_TOKEN_URL "http://twitter.com/oauth/request_token"
+#define ACCESS_TOKEN_URL  "http://twitter.com/oauth/access_token"
+#define AUTHORIZE_URL     "http://twitter.com/oauth/authorize"
 
 #define TWITTER_STATUS_GET "GET /1/statuses/home_timeline.xml?%s HTTP/1.1\r\n" \
     "Host: api.twitter.com\r\n"                                          \
@@ -30,7 +32,6 @@
     "Content-Length: %d\r\n"
 
 #define TWITTER_STATUS_FORMAT "&source=pidgintwitter&status=%s"
-//#define TWITTER_REPLY_FORMAT  "&source=pidgintwitter&status=%s&in_reply_to_status_id=%llu"
 
 #define TWITTER_DEFAULT_INTERVAL (60)
 #define TWITTER_OLD_DEFAULT_ICON_URL "http://static.twitter.com/images/default_profile_bigger.png"