comparison twitter_api.c @ 357:732de90812d8

- fixed memory leaks - some code cleanups
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Fri, 27 Aug 2010 01:13:44 +0900
parents 0fe895195132
children c6392a86839b
comparison
equal deleted inserted replaced
356:e8e51c70793c 357:732de90812d8
14 static void post_status_with_api_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, size_t len, const gchar *error_message); 14 static void post_status_with_api_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, size_t len, const gchar *error_message);
15 15
16 /* oauth */ 16 /* oauth */
17 #define TYPE_GET 0 17 #define TYPE_GET 0
18 #define TYPE_POST 1 18 #define TYPE_POST 1
19
20 char *c_key = "wrD3WGIh2P31d3fIjRkfcw";
21 char *c_sec = "ZEhViGY8P5IPjwgV8EVOkdjHhShRAZ9yhlYw0ZDXU";
22 char *SAMPLE_NONCE = "0123456789abcdefghijk";
19 23
20 typedef struct oauth_request { 24 typedef struct oauth_request {
21 char *url; 25 char *url;
22 char *c_key; 26 char *c_key;
23 char *c_sec; 27 char *c_sec;
34 38
35 char *make_oauth_get(oauth_request_t *oauth_req); 39 char *make_oauth_get(oauth_request_t *oauth_req);
36 char *make_oauth_post(oauth_request_t *auth_req); 40 char *make_oauth_post(oauth_request_t *auth_req);
37 static void oauth_setup_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, size_t len, const gchar *error_message); 41 static void oauth_setup_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, size_t len, const gchar *error_message);
38 42
39
40 #ifdef _WIN32 43 #ifdef _WIN32
41 extern gboolean blink_state; 44 extern gboolean blink_state;
42 extern gboolean blink_modified; 45 extern gboolean blink_modified;
43 #endif 46 #endif
44 47
45 extern guint64 reply_to_msgid; 48 extern guint64 reply_to_msgid;
46 extern PurpleAccount *account_for_twitter; 49 extern PurpleAccount *account_for_twitter;
47 50
48 char *request_token_url = "http://twitter.com/oauth/request_token"; 51
49 char *access_token_url = "http://twitter.com/oauth/access_token"; 52 /* oauth functions */
50 char *authorize_url = "http://twitter.com/oauth/authorize";
51
52 char *c_key = "wrD3WGIh2P31d3fIjRkfcw";
53 char *c_sec = "ZEhViGY8P5IPjwgV8EVOkdjHhShRAZ9yhlYw0ZDXU";
54 char *SAMPLE_NONCE = "0123456789abcdefghijk";
55
56 void 53 void
57 oauth_access_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, 54 oauth_access_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
58 const gchar *url_text, size_t len, 55 const gchar *url_text, size_t len,
59 const gchar *error_message) 56 const gchar *error_message)
60 { 57 {
102 pin_dialog_ok_cb(gpointer data, char *pin) 99 pin_dialog_ok_cb(gpointer data, char *pin)
103 { 100 {
104 char *oauth = NULL; 101 char *oauth = NULL;
105 char *request = NULL; 102 char *request = NULL;
106 oauth_request_t *oauth_req = (oauth_request_t *)data; 103 oauth_request_t *oauth_req = (oauth_request_t *)data;
104
107 /* 105 /*
108 request URL:http://twitter.com/oauth/access_token? 106 request URL:http://twitter.com/oauth/access_token?
109 oauth_consumer_key=wrD3WGIh2P31d3fIjRkfcw& 107 oauth_consumer_key=wrD3WGIh2P31d3fIjRkfcw&
110 oauth_nonce=QUzhkt0AO3tjNrR& 108 oauth_nonce=QUzhkt0AO3tjNrR&
111 oauth_signature_method=HMAC-SHA1& 109 oauth_signature_method=HMAC-SHA1&
112 oauth_timestamp=1282450223& 110 oauth_timestamp=1282450223&
113
114 oauth_token=VL126k8KRNXid7Q7ZHYHh05PuASunVaPzyzrozf14& 111 oauth_token=VL126k8KRNXid7Q7ZHYHh05PuASunVaPzyzrozf14&
115 oauth_verifier=9772286& 112 oauth_verifier=9772286&
116 oauth_version=1.0& 113 oauth_version=1.0&
117 oauth_signature=OyBsxqWdsvLkV4LBnN8tMBnImVY%3D 114 oauth_signature=OyBsxqWdsvLkV4LBnN8tMBnImVY%3D
118 */ 115 */
119 116
120 /* access token*/ 117 /* access token*/
121 g_free(oauth_req->url); 118 g_free(oauth_req->url);
122 oauth_req->url = g_strdup(access_token_url); 119 oauth_req->url = g_strdup(ACCESS_TOKEN_URL);
123 oauth_req->verifier = g_strdup(pin); 120 oauth_req->verifier = g_strdup(pin);
124 oauth_req->type = TYPE_GET; 121 oauth_req->type = TYPE_GET;
125 oauth_req->notoken = FALSE; 122 oauth_req->notoken = FALSE;
126 123
127 twitter_debug("a_key=%s\n", oauth_req->a_key); 124 twitter_debug("a_key=%s\n", oauth_req->a_key);
128 125
129 oauth = make_oauth_get(oauth_req); 126 oauth = make_oauth_get(oauth_req);
130 127
131 request = g_strdup_printf("%s?%s", access_token_url, oauth); 128 request = g_strdup_printf("%s?%s", ACCESS_TOKEN_URL, oauth);
132 twitter_debug("request=%s\n", request); 129 twitter_debug("request=%s\n", request);
133 130
134 purple_util_fetch_url_request(request, TRUE, 131 purple_util_fetch_url_request(request, TRUE,
135 NULL, TRUE, NULL, TRUE, 132 NULL, TRUE, NULL, TRUE,
136 oauth_access_cb, data); 133 oauth_access_cb, data);
158 HTTP-reply: 155 HTTP-reply:
159 oauth_token=zKILrNcVPGRtUE6Rlh1KU6upYNJWW51mzt7btZx5Ac& 156 oauth_token=zKILrNcVPGRtUE6Rlh1KU6upYNJWW51mzt7btZx5Ac&
160 oauth_token_secret=EMD3u1piAKPsQnq44Its9f8WmIReYnUFcJIgd3niu4& 157 oauth_token_secret=EMD3u1piAKPsQnq44Its9f8WmIReYnUFcJIgd3niu4&
161 oauth_callback_confirmed=true 158 oauth_callback_confirmed=true
162 */ 159 */
160
163 /* separate key and secret */ 161 /* separate key and secret */
164 f = strstr(url_text, "oauth_token="); 162 f = strstr(url_text, "oauth_token=");
165 if(!f) 163 if(!f)
166 return; 164 return;
167 e = strstr(f, "&"); 165 e = strstr(f, "&");
177 if(!e) 175 if(!e)
178 return; 176 return;
179 g_free(oauth_req->a_sec); 177 g_free(oauth_req->a_sec);
180 oauth_req->a_sec = g_strndup(f+19, e-f-19); 178 oauth_req->a_sec = g_strndup(f+19, e-f-19);
181 179
182 /* redirect twitter's authorization url */ 180 /* redirect to twitter's authorization url */
183 char *uri = g_strdup_printf("%s?oauth_token=%s", authorize_url, oauth_req->a_key); 181 char *uri = g_strdup_printf("%s?oauth_token=%s", AUTHORIZE_URL, oauth_req->a_key);
184 twitter_debug("auth uri=%s\n", uri); 182 twitter_debug("auth uri=%s\n", uri);
185 purple_notify_uri(conn, uri); 183 purple_notify_uri(conn, uri);
186 184
187 /* show dialog to wait PIN number*/ 185 /* show dialog to wait PIN */
188 purple_request_input(conn, 186 purple_request_input(conn,
189 "PIN", 187 "PIN",
190 "Enter PIN", 188 "Enter PIN",
191 "Press allow button in the browser, then enter the PIN to complete process.", 189 "Press allow button in the browser, then enter the PIN to complete process.",
192 "", 190 "",
206 void 204 void
207 oauth_setup(gpointer data) 205 oauth_setup(gpointer data)
208 { 206 {
209 char *oauth = NULL; 207 char *oauth = NULL;
210 char *request = NULL; 208 char *request = NULL;
211 oauth_request_t *oauth_req = g_new0(oauth_request_t, 1); 209 oauth_request_t *oauth_req = g_new0(oauth_request_t, 1); /* persistent */
210
212 /* 211 /*
213 http://twitter.com/oauth/request_token? 212 http://twitter.com/oauth/request_token?
214 oauth_consumer_key=wrD3WGIh2P31d3fIjRkfcw& 213 oauth_consumer_key=wrD3WGIh2P31d3fIjRkfcw&
215 oauth_nonce=HCUxu1D3qN4Nklr9QVAymve40PtJyU& 214 oauth_nonce=HCUxu1D3qN4Nklr9QVAymve40PtJyU&
216 oauth_signature_method=HMAC-SHA1& 215 oauth_signature_method=HMAC-SHA1&
217 oauth_timestamp=1282446611&oauth_version=1.0& 216 oauth_timestamp=1282446611&oauth_version=1.0&
218 oauth_signature=A%2BZIiUVsQv5ZR8u%2F2oLmUFX1eHE%3D 217 oauth_signature=A%2BZIiUVsQv5ZR8u%2F2oLmUFX1eHE%3D
219 */ 218 */
220 219
221 oauth_req->url = strdup(request_token_url); 220 oauth_req->url = strdup(REQUEST_TOKEN_URL);
222 oauth_req->c_key = strdup(c_key); 221 oauth_req->c_key = strdup(c_key);
223 oauth_req->c_sec = strdup(c_sec); 222 oauth_req->c_sec = strdup(c_sec);
224 oauth_req->a_key = NULL; 223 oauth_req->a_key = NULL;
225 oauth_req->a_sec = NULL; 224 oauth_req->a_sec = NULL;
226 oauth_req->type = TYPE_GET; 225 oauth_req->type = TYPE_GET;
227 oauth_req->notoken = TRUE; 226 oauth_req->notoken = TRUE;
228 oauth_req->conv = (PurpleConversation *)data; 227 oauth_req->conv = (PurpleConversation *)data;
229 228
230 /* request token*/ 229 /* request token*/
231 oauth = make_oauth_get(oauth_req); 230 oauth = make_oauth_get(oauth_req);
232 request = g_strdup_printf("%s?%s", request_token_url, oauth); 231 request = g_strdup_printf("%s?%s", REQUEST_TOKEN_URL, oauth);
233 232
234 twitter_debug("request=%s\n", request); 233 twitter_debug("request=%s\n", request);
235 234
236 purple_util_fetch_url_request(request, TRUE, 235 purple_util_fetch_url_request(request, TRUE,
237 NULL, TRUE, NULL, TRUE, 236 NULL, TRUE, NULL, TRUE,
718 oauth_req.count = count; 717 oauth_req.count = count;
719 oauth_req.msgid = 0; 718 oauth_req.msgid = 0;
720 oauth_req.notoken = FALSE; 719 oauth_req.notoken = FALSE;
721 720
722 oauth = make_oauth_get(&oauth_req); 721 oauth = make_oauth_get(&oauth_req);
722 g_free(url0);
723 723
724 /* header */ 724 /* header */
725 header = g_strdup_printf(TWITTER_STATUS_GET, oauth); 725 header = g_strdup_printf(TWITTER_STATUS_GET, oauth);
726 request = g_strconcat(header, "\r\n", NULL); 726 request = g_strconcat(header, "\r\n", NULL);
727 727
733 get_status_with_api_cb, data); 733 get_status_with_api_cb, data);
734 734
735 g_free(header); 735 g_free(header);
736 g_free(request); 736 g_free(request);
737 g_free(oauth); 737 g_free(oauth);
738 g_free(url0); 738
739 return TRUE; 739 return TRUE;
740 } 740 }
741 741
742 /****************************/ 742 /****************************/
743 /* API based post functions */ 743 /* API based post functions */
933 oauth_req.count = 0; 933 oauth_req.count = 0;
934 oauth_req.msgid = reply_to_msgid; 934 oauth_req.msgid = reply_to_msgid;
935 oauth_req.notoken = FALSE; 935 oauth_req.notoken = FALSE;
936 936
937 oauth = make_oauth_post(&oauth_req); 937 oauth = make_oauth_post(&oauth_req);
938 g_free(url0);
938 939
939 reply_to_msgid = 0; 940 reply_to_msgid = 0;
940
941 941
942 header = g_strdup_printf(TWITTER_STATUS_POST, 942 header = g_strdup_printf(TWITTER_STATUS_POST,
943 (int)strlen(oauth)); 943 (int)strlen(oauth));
944 944
945 request = g_strconcat(header, "\r\n", oauth, "\r\n", NULL); 945 request = g_strconcat(header, "\r\n", oauth, "\r\n", NULL);
998 oauth_req.count = 0; 998 oauth_req.count = 0;
999 oauth_req.msgid = 0; 999 oauth_req.msgid = 0;
1000 oauth_req.notoken = FALSE; 1000 oauth_req.notoken = FALSE;
1001 1001
1002 oauth = make_oauth_post(&oauth_req); 1002 oauth = make_oauth_post(&oauth_req);
1003 g_free(url0);
1003 1004
1004 header = g_strdup_printf(TWITTER_FAV_POST, 1005 header = g_strdup_printf(TWITTER_FAV_POST,
1005 (long long unsigned int)id, 1006 (long long unsigned int)id,
1006 (int)strlen(oauth)); 1007 (int)strlen(oauth));
1007 1008
1065 oauth_req.count = 0; 1066 oauth_req.count = 0;
1066 oauth_req.msgid = 0; 1067 oauth_req.msgid = 0;
1067 oauth_req.notoken = FALSE; 1068 oauth_req.notoken = FALSE;
1068 1069
1069 oauth = make_oauth_post(&oauth_req); 1070 oauth = make_oauth_post(&oauth_req);
1071 g_free(url0);
1070 1072
1071 header = g_strdup_printf(TWITTER_RETWEET_POST, 1073 header = g_strdup_printf(TWITTER_RETWEET_POST,
1072 (long long unsigned int)id, 1074 (long long unsigned int)id,
1073 (int)strlen(oauth)); 1075 (int)strlen(oauth));
1074 1076