| 98 purple_buddy_icons_set_for_user(purple_connection_get_account(gc), |
99 purple_buddy_icons_set_for_user(purple_connection_get_account(gc), |
| 99 ggp_uin_to_str(uin), NULL, 0, NULL); |
100 ggp_uin_to_str(uin), NULL, 0, NULL); |
| 100 } |
101 } |
| 101 |
102 |
| 102 static void |
103 static void |
| 103 ggp_avatar_buddy_update_received(G_GNUC_UNUSED SoupSession *session, |
104 ggp_avatar_buddy_update_received(GObject *source, GAsyncResult *result, |
| 104 SoupMessage *msg, gpointer _pending_update) |
105 gpointer data) |
| 105 { |
106 { |
| 106 ggp_avatar_buddy_update_req *pending_update = _pending_update; |
107 ggp_avatar_buddy_update_req *pending_update = data; |
| |
108 GBytes *response_body = NULL; |
| |
109 GError *error = NULL; |
| |
110 const char *error_message = NULL; |
| 107 PurpleBuddy *buddy; |
111 PurpleBuddy *buddy; |
| 108 PurpleAccount *account; |
112 PurpleAccount *account; |
| 109 PurpleConnection *gc = pending_update->gc; |
113 PurpleConnection *gc = pending_update->gc; |
| 110 gchar timestamp_str[20]; |
114 gchar timestamp_str[20]; |
| 111 const gchar *got_data; |
115 char *got_data = NULL; |
| 112 size_t got_len; |
116 gsize got_len = 0; |
| 113 |
117 |
| 114 PURPLE_ASSERT_CONNECTION_IS_VALID(gc); |
118 PURPLE_ASSERT_CONNECTION_IS_VALID(gc); |
| 115 |
119 |
| 116 if (!SOUP_STATUS_IS_SUCCESSFUL(soup_message_get_status(msg))) { |
120 if(SOUP_STATUS_IS_SUCCESSFUL(soup_message_get_status(pending_update->msg))) { |
| |
121 response_body = soup_session_send_and_read_finish(SOUP_SESSION(source), |
| |
122 result, &error); |
| |
123 error_message = error != NULL ? error->message : "unknown"; |
| |
124 } else { |
| |
125 error_message = soup_message_get_reason_phrase(pending_update->msg); |
| |
126 } |
| |
127 if(response_body == NULL) { |
| 117 purple_debug_error("gg", |
128 purple_debug_error("gg", |
| 118 "ggp_avatar_buddy_update_received: bad response " |
129 "ggp_avatar_buddy_update_received: bad response " |
| 119 "while getting avatar for %u: %s", |
130 "while getting avatar for %u: %s", |
| 120 pending_update->uin, |
131 pending_update->uin, error_message); |
| 121 soup_message_get_reason_phrase(msg)); |
132 g_object_unref(pending_update->msg); |
| 122 g_free(pending_update); |
133 g_free(pending_update); |
| |
134 g_clear_error(&error); |
| 123 return; |
135 return; |
| 124 } |
136 } |
| 125 |
137 |
| 126 account = purple_connection_get_account(gc); |
138 account = purple_connection_get_account(gc); |
| 127 buddy = purple_blist_find_buddy(account, |
139 buddy = purple_blist_find_buddy(account, |
| 129 |
141 |
| 130 if (!buddy) { |
142 if (!buddy) { |
| 131 purple_debug_warning( |
143 purple_debug_warning( |
| 132 "gg", "ggp_avatar_buddy_update_received: buddy %u disappeared", |
144 "gg", "ggp_avatar_buddy_update_received: buddy %u disappeared", |
| 133 pending_update->uin); |
145 pending_update->uin); |
| |
146 g_object_unref(pending_update->msg); |
| 134 g_free(pending_update); |
147 g_free(pending_update); |
| 135 return; |
148 return; |
| 136 } |
149 } |
| 137 |
150 |
| 138 g_snprintf(timestamp_str, sizeof(timestamp_str), "%lu", |
151 g_snprintf(timestamp_str, sizeof(timestamp_str), "%lu", |
| 139 pending_update->timestamp); |
152 pending_update->timestamp); |
| 140 got_data = msg->response_body->data; |
153 got_data = g_bytes_unref_to_data(response_body, &got_len); |
| 141 got_len = msg->response_body->length; |
|
| 142 purple_buddy_icons_set_for_user(account, purple_buddy_get_name(buddy), |
154 purple_buddy_icons_set_for_user(account, purple_buddy_get_name(buddy), |
| 143 g_memdup2(got_data, got_len), got_len, |
155 got_data, got_len, timestamp_str); |
| 144 timestamp_str); |
|
| 145 |
156 |
| 146 purple_debug_info("gg", |
157 purple_debug_info("gg", |
| 147 "ggp_avatar_buddy_update_received: got avatar for buddy " |
158 "ggp_avatar_buddy_update_received: got avatar for buddy " |
| 148 "%u [ts=%lu]", |
159 "%u [ts=%lu]", |
| 149 pending_update->uin, pending_update->timestamp); |
160 pending_update->uin, pending_update->timestamp); |
| |
161 g_object_unref(pending_update->msg); |
| 150 g_free(pending_update); |
162 g_free(pending_update); |
| 151 } |
163 } |
| 152 |
164 |
| 153 void |
165 void |
| 154 ggp_avatar_buddy_update(PurpleConnection *gc, uin_t uin, time_t timestamp) |
166 ggp_avatar_buddy_update(PurpleConnection *gc, uin_t uin, time_t timestamp) |
| 212 pending_update->uin = uin; |
224 pending_update->uin = uin; |
| 213 pending_update->timestamp = timestamp; |
225 pending_update->timestamp = timestamp; |
| 214 pending_update->gc = gc; |
226 pending_update->gc = gc; |
| 215 |
227 |
| 216 url = g_strdup_printf(GGP_AVATAR_BUDDY_URL, pending_update->uin); |
228 url = g_strdup_printf(GGP_AVATAR_BUDDY_URL, pending_update->uin); |
| 217 req = soup_message_new("GET", url); |
229 pending_update->msg = req = soup_message_new("GET", url); |
| 218 g_free(url); |
230 g_free(url); |
| 219 soup_message_headers_replace(soup_message_get_request_headers(req), |
231 soup_message_headers_replace(soup_message_get_request_headers(req), |
| 220 "User-Agent", GGP_AVATAR_USERAGENT); |
232 "User-Agent", GGP_AVATAR_USERAGENT); |
| 221 // purple_http_request_set_max_len(req, GGP_AVATAR_SIZE_MAX); |
233 // purple_http_request_set_max_len(req, GGP_AVATAR_SIZE_MAX); |
| 222 soup_session_queue_message( |
234 soup_session_send_and_read_async(info->http, req, G_PRIORITY_DEFAULT, NULL, |
| 223 info->http, req, ggp_avatar_buddy_update_received, pending_update); |
235 ggp_avatar_buddy_update_received, |
| |
236 pending_update); |
| 224 } |
237 } |
| 225 |
238 |
| 226 /******************************************************************************* |
239 /******************************************************************************* |
| 227 * Own avatar setting. |
240 * Own avatar setting. |
| 228 ******************************************************************************/ |
241 ******************************************************************************/ |
| 234 * Authorization: IMToken 0123456789abcdef0123456789abcdef01234567 |
247 * Authorization: IMToken 0123456789abcdef0123456789abcdef01234567 |
| 235 * photo=<avatar content> |
248 * photo=<avatar content> |
| 236 */ |
249 */ |
| 237 |
250 |
| 238 static void |
251 static void |
| 239 ggp_avatar_own_sent(G_GNUC_UNUSED SoupSession *session, SoupMessage *msg, |
252 ggp_avatar_own_sent(GObject *source, GAsyncResult *result, gpointer data) { |
| 240 gpointer user_data) |
253 SoupMessage *msg = data; |
| 241 { |
254 GBytes *response_body = NULL; |
| 242 PurpleConnection *gc = user_data; |
255 GError *error = NULL; |
| 243 |
256 const char *buffer = NULL; |
| 244 PURPLE_ASSERT_CONNECTION_IS_VALID(gc); |
257 gsize size = 0; |
| 245 |
258 |
| 246 if (!SOUP_STATUS_IS_SUCCESSFUL(soup_message_get_status(msg))) { |
259 if (!SOUP_STATUS_IS_SUCCESSFUL(soup_message_get_status(msg))) { |
| 247 purple_debug_error("gg", "ggp_avatar_own_sent: avatar not sent. %s\n", |
260 purple_debug_error("gg", "ggp_avatar_own_sent: avatar not sent. %s", |
| 248 soup_message_get_reason_phrase(msg)); |
261 soup_message_get_reason_phrase(msg)); |
| 249 return; |
262 g_object_unref(msg); |
| 250 } |
263 return; |
| 251 purple_debug_info("gg", "ggp_avatar_own_sent: %s\n", |
264 } |
| 252 msg->response_body->data); |
265 g_clear_object(&msg); |
| |
266 |
| |
267 response_body = soup_session_send_and_read_finish(SOUP_SESSION(source), |
| |
268 result, &error); |
| |
269 if(response_body == NULL) { |
| |
270 purple_debug_error("gg", "ggp_avatar_own_sent: avatar not sent. %s", |
| |
271 error->message); |
| |
272 g_error_free(error); |
| |
273 return; |
| |
274 } |
| |
275 |
| |
276 buffer = g_bytes_get_data(response_body, &size); |
| |
277 purple_debug_info("gg", "ggp_avatar_own_sent: %*s", (int)size, buffer); |
| |
278 g_bytes_unref(response_body); |
| 253 } |
279 } |
| 254 |
280 |
| 255 static void |
281 static void |
| 256 ggp_avatar_own_got_token(PurpleConnection *gc, const gchar *token, |
282 ggp_avatar_own_got_token(PurpleConnection *gc, const gchar *token, |
| 257 gpointer _img) |
283 gpointer _img) |
| 284 soup_form_encode("uin", uin_str, "photo", img_data, NULL)); |
310 soup_form_encode("uin", uin_str, "photo", img_data, NULL)); |
| 285 // purple_http_request_set_max_len(req, GGP_AVATAR_RESPONSE_MAX); |
311 // purple_http_request_set_max_len(req, GGP_AVATAR_RESPONSE_MAX); |
| 286 headers = soup_message_get_request_headers(req); |
312 headers = soup_message_get_request_headers(req); |
| 287 soup_message_headers_replace(headers, "Authorization", token); |
313 soup_message_headers_replace(headers, "Authorization", token); |
| 288 soup_message_headers_replace(headers, "From", "avatars to avatars"); |
314 soup_message_headers_replace(headers, "From", "avatars to avatars"); |
| 289 soup_session_queue_message(info->http, req, ggp_avatar_own_sent, gc); |
315 soup_session_send_and_read_async(info->http, req, G_PRIORITY_DEFAULT, NULL, |
| |
316 ggp_avatar_own_sent, req); |
| 290 g_free(img_data); |
317 g_free(img_data); |
| 291 g_free(uin_str); |
318 g_free(uin_str); |
| 292 } |
319 } |
| 293 |
320 |
| 294 void |
321 void |