summaryrefslogtreecommitdiff
path: root/libpurple/protocols/gg/image-prpl.c
blob: 4d6c1ab32141ec89af1fc493573d315863d21521 (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
/* 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 "image-prpl.h"

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

#include "gg.h"
#include "utils.h"

#include <image-store.h>

struct _ggp_image_session_data
{
	GHashTable *recv_images;
	GHashTable *sent_images;
};

typedef struct
{
	PurpleImage *image;
	gchar *conv_name; /* TODO: callback */
} ggp_image_sent;

static void ggp_image_sent_free(gpointer _sent_image)
{
	ggp_image_sent *sent_image = _sent_image;
	g_object_unref(sent_image->image);
	g_free(sent_image->conv_name);
	g_free(sent_image);
}

static uint64_t ggp_image_params_to_id(uint32_t crc32, uint32_t size)
{
	return ((uint64_t)crc32 << 32) | size;
}

static inline ggp_image_session_data *
ggp_image_get_sdata(PurpleConnection *gc)
{
	GGPInfo *accdata = purple_connection_get_protocol_data(gc);
	return accdata->image_data;
}

void ggp_image_setup(PurpleConnection *gc)
{
	GGPInfo *accdata = purple_connection_get_protocol_data(gc);
	ggp_image_session_data *sdata = g_new0(ggp_image_session_data, 1);

	accdata->image_data = sdata;

	sdata->recv_images = g_hash_table_new_full(
		g_int64_hash, g_int64_equal, g_free, g_object_unref);
	sdata->sent_images = g_hash_table_new_full(
		g_int64_hash, g_int64_equal, g_free,
		ggp_image_sent_free);
}

void ggp_image_cleanup(PurpleConnection *gc)
{
	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);

	g_hash_table_destroy(sdata->recv_images);
	g_hash_table_destroy(sdata->sent_images);
	g_free(sdata);
}

ggp_image_prepare_result
ggp_image_prepare(PurpleConversation *conv, PurpleImage *image, uint64_t *id)
{
	PurpleConnection *gc = purple_conversation_get_connection(conv);
	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
	size_t image_size;
	gconstpointer image_data;
	uint32_t image_crc;
	ggp_image_sent *sent_image;

	g_return_val_if_fail(image, GGP_IMAGE_PREPARE_FAILURE);

	image_size = purple_image_get_data_size(image);

	if (image_size > GGP_IMAGE_SIZE_MAX) {
		purple_debug_warning("gg", "ggp_image_prepare: image "
			"is too big (max bytes: %d)\n", GGP_IMAGE_SIZE_MAX);
		return GGP_IMAGE_PREPARE_TOO_BIG;
	}

	g_object_ref(image);
	image_data = purple_image_get_data(image);
	image_crc = gg_crc32(0, image_data, image_size);

	purple_debug_info("gg", "ggp_image_prepare: image prepared "
		"[crc=%u, size=%" G_GSIZE_FORMAT "]",
		image_crc, image_size);

	*id = ggp_image_params_to_id(image_crc, image_size);

	g_object_ref(image);
	sent_image = g_new(ggp_image_sent, 1);
	sent_image->image = image;
	sent_image->conv_name = g_strdup(purple_conversation_get_name(conv));
	g_hash_table_insert(sdata->sent_images, ggp_uint64dup(*id),
		sent_image);

	return GGP_IMAGE_PREPARE_OK;
}

void ggp_image_recv(PurpleConnection *gc,
	const struct gg_event_image_reply *image_reply)
{
	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
	PurpleImage *img;
	uint64_t id;

	id = ggp_image_params_to_id(image_reply->crc32, image_reply->size);
	img = g_hash_table_lookup(sdata->recv_images, &id);
	if (!img) {
		purple_debug_warning("gg", "ggp_image_recv: "
			"image " GGP_IMAGE_ID_FORMAT " wasn't requested\n",
			id);
		return;
	}

	purple_debug_info("gg", "ggp_image_recv: got image "
		"[crc=%u, size=%u, filename=%s, id=" GGP_IMAGE_ID_FORMAT "]",
		image_reply->crc32, image_reply->size,
		image_reply->filename, id);

	img = purple_image_new_from_data(
		(const guint8 *)image_reply->image,
		image_reply->size
	);
	purple_image_set_friendly_filename(img, image_reply->filename);

	g_hash_table_insert(sdata->recv_images, &id, img);
}

void ggp_image_send(PurpleConnection *gc,
	const struct gg_event_image_request *image_request)
{
	GGPInfo *accdata = purple_connection_get_protocol_data(gc);
	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
	ggp_image_sent *sent_image;
	PurpleConversation *conv;
	uint64_t id;
	gchar *gg_filename;

	purple_debug_info("gg", "ggp_image_send: got image request "
		"[uin=%u, crc=%u, size=%u]\n",
		image_request->sender,
		image_request->crc32,
		image_request->size);

	id = ggp_image_params_to_id(image_request->crc32, image_request->size);

	sent_image = g_hash_table_lookup(sdata->sent_images, &id);

	if (sent_image == NULL && image_request->sender == ggp_str_to_uin(
		purple_account_get_username(purple_connection_get_account(gc))))
	{
		purple_debug_misc("gg", "ggp_image_send: requested image "
			"not found, but this may be another session request\n");
		return;
	}
	if (sent_image == NULL) {
		purple_debug_warning("gg", "ggp_image_send: requested image "
			"not found\n");
		return;
	}

	purple_debug_misc("gg", "ggp_image_send: requested image found "
		"[id=" GGP_IMAGE_ID_FORMAT ", conv=%s]\n",
		id, sent_image->conv_name);

	g_return_if_fail(sent_image->image);

	/* TODO: check allowed recipients */
	gg_filename = g_strdup_printf(GGP_IMAGE_ID_FORMAT, id);
	gg_image_reply(accdata->session, image_request->sender,
		gg_filename,
		purple_image_get_data(sent_image->image),
		purple_image_get_data_size(sent_image->image));
	g_free(gg_filename);

	conv = purple_conversations_find_with_account(
		sent_image->conv_name,
		purple_connection_get_account(gc));
	if (conv != NULL) {
		gchar *msg = g_strdup_printf(_("Image delivered to %u."),
			image_request->sender);
		purple_conversation_write_system_message(conv, msg,
			PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_NOTIFY);
		g_free(msg);
	}
}

PurpleImage *
ggp_image_request(PurpleConnection *gc, uin_t uin, uint64_t id)
{
	GGPInfo *accdata = purple_connection_get_protocol_data(gc);
	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
	PurpleImage *img;
	uint32_t crc = id >> 32;
	uint32_t size = id;

	if (size > GGP_IMAGE_SIZE_MAX && crc <= GGP_IMAGE_SIZE_MAX) {
		uint32_t tmp;
		purple_debug_warning("gg", "ggp_image_request: "
			"crc and size are swapped!\n");
		tmp = crc;
		crc = size;
		size = tmp;
		id = ggp_image_params_to_id(crc, size);
	}

	img = g_hash_table_lookup(sdata->recv_images, &id);
	if (img) {
		purple_debug_info("gg", "ggp_image_request: "
			"image " GGP_IMAGE_ID_FORMAT " got from cache", id);
		return img;
	}


	g_hash_table_insert(sdata->recv_images, ggp_uint64dup(id), NULL);

	purple_debug_info("gg", "ggp_image_request: requesting image "
		GGP_IMAGE_ID_FORMAT, id);
	if (gg_image_request(accdata->session, uin, size, crc) != 0)
		purple_debug_error("gg", "ggp_image_request: failed");

	return img;
}