summaryrefslogtreecommitdiff
path: root/libpurple/protocols/gg/avatar.c
blob: e7cd0bf7d68dc5537b80e9f80444db60b6f93905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * Rewritten from scratch during Google Summer of Code 2012
 * by Tomek Wasilczyk (http://www.wasilczyk.pl).
 *
 * Previously implemented by:
 *  - Arkadiusz Miskiewicz <misiek@pld.org.pl> - first implementation (2001);
 *  - Bartosz Oler <bartosz@bzimage.us> - reimplemented during GSoC 2005;
 *  - Krzysztof Klinikowski <grommasher@gmail.com> - some parts (2009-2011).
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include "avatar.h"

#include <debug.h>
#include <glibcompat.h>

#include "gg.h"
#include "utils.h"
#include "oauth/oauth-purple.h"

/* Common */

#define GGP_AVATAR_USERAGENT "GG Client build 11.0.0.7562"
#define GGP_AVATAR_SIZE_MAX 1048576

/* Buddy avatars updating */

typedef struct
{
	uin_t uin;
	time_t timestamp;
	PurpleConnection *gc;
} ggp_avatar_buddy_update_req;

#define GGP_AVATAR_BUDDY_URL "http://avatars.gg.pl/%u/s,big"

/* Own avatar setting */

struct _ggp_avatar_session_data {
	PurpleImage *own_img;
};

#define GGP_AVATAR_RESPONSE_MAX 10240

/*******************************************************************************
 * Common.
 ******************************************************************************/

static inline ggp_avatar_session_data *
ggp_avatar_get_avdata(PurpleConnection *gc)
{
	GGPInfo *accdata = purple_connection_get_protocol_data(gc);
	return accdata->avatar_data;
}

void ggp_avatar_setup(PurpleConnection *gc)
{
	GGPInfo *info = purple_connection_get_protocol_data(gc);

	info->avatar_data = g_new0(ggp_avatar_session_data, 1);
}

void ggp_avatar_cleanup(PurpleConnection *gc)
{
	GGPInfo *info = purple_connection_get_protocol_data(gc);

	g_free(info->avatar_data);
}

/*******************************************************************************
 * Buddy avatars updating.
 ******************************************************************************/

void ggp_avatar_buddy_remove(PurpleConnection *gc, uin_t uin)
{
	if (purple_debug_is_verbose()) {
		purple_debug_misc("gg", "ggp_avatar_buddy_remove(%p, %u)\n", gc, uin);
	}

	purple_buddy_icons_set_for_user(purple_connection_get_account(gc),
		ggp_uin_to_str(uin), NULL, 0, NULL);
}

static void
ggp_avatar_buddy_update_received(G_GNUC_UNUSED SoupSession *session,
                                 SoupMessage *msg, gpointer _pending_update)
{
	ggp_avatar_buddy_update_req *pending_update = _pending_update;
	PurpleBuddy *buddy;
	PurpleAccount *account;
	PurpleConnection *gc = pending_update->gc;
	gchar timestamp_str[20];
	const gchar *got_data;
	size_t got_len;

	PURPLE_ASSERT_CONNECTION_IS_VALID(gc);

	if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
		purple_debug_error("gg",
		                   "ggp_avatar_buddy_update_received: bad response "
		                   "while getting avatar for %u: %s",
		                   pending_update->uin, msg->reason_phrase);
		g_free(pending_update);
		return;
	}

	account = purple_connection_get_account(gc);
	buddy = purple_blist_find_buddy(account,
	                                ggp_uin_to_str(pending_update->uin));

	if (!buddy) {
		purple_debug_warning(
		        "gg", "ggp_avatar_buddy_update_received: buddy %u disappeared",
		        pending_update->uin);
		g_free(pending_update);
		return;
	}

	g_snprintf(timestamp_str, sizeof(timestamp_str), "%lu",
	           pending_update->timestamp);
	got_data = msg->response_body->data;
	got_len = msg->response_body->length;
	purple_buddy_icons_set_for_user(account, purple_buddy_get_name(buddy),
	                                g_memdup(got_data, got_len), got_len,
	                                timestamp_str);

	purple_debug_info("gg",
	                  "ggp_avatar_buddy_update_received: got avatar for buddy "
	                  "%u [ts=%lu]",
	                  pending_update->uin, pending_update->timestamp);
	g_free(pending_update);
}

void
ggp_avatar_buddy_update(PurpleConnection *gc, uin_t uin, time_t timestamp)
{
	GGPInfo *info = purple_connection_get_protocol_data(gc);
	gchar *url;
	SoupMessage *req;
	ggp_avatar_buddy_update_req *pending_update;
	PurpleBuddy *buddy;
	PurpleAccount *account = purple_connection_get_account(gc);
	time_t old_timestamp;
	const char *old_timestamp_str;

	if (purple_debug_is_verbose()) {
		purple_debug_misc("gg", "ggp_avatar_buddy_update(%p, %u, %lu)", gc, uin,
		                  timestamp);
	}

	buddy = purple_blist_find_buddy(account, ggp_uin_to_str(uin));

	if (!buddy) {
		if (ggp_str_to_uin(purple_account_get_username(account)) == uin) {
			purple_debug_misc(
			        "gg",
			        "ggp_avatar_buddy_update(%p): own avatar update requested, "
			        "but we don't have ourselves on buddy list",
			        gc);
		} else {
			purple_debug_warning("gg",
			                     "ggp_avatar_buddy_update(%p): %u update "
			                     "requested, but he's not on buddy list",
			                     gc, uin);
		}
		return;
	}

	old_timestamp_str = purple_buddy_icons_get_checksum_for_user(buddy);
	old_timestamp = old_timestamp_str ? g_ascii_strtoull(
		old_timestamp_str, NULL, 10) : 0;
	if (old_timestamp == timestamp) {
		if (purple_debug_is_verbose()) {
			purple_debug_misc("gg",
			                  "ggp_avatar_buddy_update(%p): %u have up to date "
			                  "avatar with ts=%lu",
			                  gc, uin, timestamp);
		}
		return;
	}
	if (old_timestamp > timestamp) {
		purple_debug_warning("gg",
		                     "ggp_avatar_buddy_update(%p): saved timestamp for "
		                     "%u is newer than received (%lu > %lu)",
		                     gc, uin, old_timestamp, timestamp);
	}

	purple_debug_info("gg",
	                  "ggp_avatar_buddy_update(%p): updating %u with ts=%lu...",
	                  gc, uin, timestamp);

	pending_update = g_new(ggp_avatar_buddy_update_req, 1);
	pending_update->uin = uin;
	pending_update->timestamp = timestamp;
	pending_update->gc = gc;

	url = g_strdup_printf(GGP_AVATAR_BUDDY_URL, pending_update->uin);
	req = soup_message_new("GET", url);
	g_free(url);
	soup_message_headers_replace(req->request_headers, "User-Agent",
	                             GGP_AVATAR_USERAGENT);
	// purple_http_request_set_max_len(req, GGP_AVATAR_SIZE_MAX);
	soup_session_queue_message(
	        info->http, req, ggp_avatar_buddy_update_received, pending_update);
}

/*******************************************************************************
 * Own avatar setting.
 ******************************************************************************/

/**
 * TODO: use new, GG11 method, when IMToken will be provided by libgadu.
 *
 * POST https://avatars.mpa.gg.pl/avatars/user,<uin>/0
 * Authorization: IMToken 0123456789abcdef0123456789abcdef01234567
 * photo=<avatar content>
 */

static void
ggp_avatar_own_sent(G_GNUC_UNUSED SoupSession *session, SoupMessage *msg,
                    gpointer user_data)
{
	PurpleConnection *gc = user_data;

	PURPLE_ASSERT_CONNECTION_IS_VALID(gc);

	if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
		purple_debug_error("gg", "ggp_avatar_own_sent: avatar not sent. %s\n",
		                   msg->reason_phrase);
		return;
	}
	purple_debug_info("gg", "ggp_avatar_own_sent: %s\n",
	                  msg->response_body->data);
}

static void
ggp_avatar_own_got_token(PurpleConnection *gc, const gchar *token,
	gpointer _img)
{
	GGPInfo *info = purple_connection_get_protocol_data(gc);
	ggp_avatar_session_data *avdata = ggp_avatar_get_avdata(gc);
	SoupMessage *req;
	PurpleImage *img = _img;
	gchar *img_data, *uin_str;
	PurpleAccount *account = purple_connection_get_account(gc);
	uin_t uin = ggp_str_to_uin(purple_account_get_username(account));

	if (img != avdata->own_img) {
		purple_debug_warning("gg", "ggp_avatar_own_got_token: "
			"avatar was changed in meantime\n");
		return;
	}
	avdata->own_img = NULL;

	img_data = g_base64_encode(purple_image_get_data(img),
		purple_image_get_data_size(img));
	uin_str = g_strdup_printf("%d", uin);

	purple_debug_misc("gg", "ggp_avatar_own_got_token: "
		"uploading new avatar...\n");

	req = soup_form_request_new("POST", "http://avatars.nowe.gg/upload", "uin",
	                            uin_str, "photo", img_data, NULL);
	// purple_http_request_set_max_len(req, GGP_AVATAR_RESPONSE_MAX);
	soup_message_headers_replace(req->request_headers, "Authorization", token);
	soup_message_headers_replace(req->request_headers, "From",
	                             "avatars to avatars");
	soup_session_queue_message(info->http, req, ggp_avatar_own_sent, gc);
	g_free(img_data);
	g_free(uin_str);
}

void
ggp_avatar_own_set(PurpleConnection *gc, PurpleImage *img)
{
	ggp_avatar_session_data *avdata;

	PURPLE_ASSERT_CONNECTION_IS_VALID(gc);

	purple_debug_info("gg", "ggp_avatar_own_set(%p, %p)", gc, img);

	avdata = ggp_avatar_get_avdata(gc);

	if (img == NULL) {
		purple_debug_warning("gg", "ggp_avatar_own_set: avatar removing is "
		                           "probably not possible within old protocol");
		return;
	}

	avdata->own_img = img;

	ggp_oauth_request(gc, ggp_avatar_own_got_token, img, NULL, NULL);
}