summaryrefslogtreecommitdiff
path: root/libpurple/protocols/jabber/oob.c
blob: 459c08d23b5f740cccc633dc3360f5e8403c8edc (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
/*
 * purple - Jabber Protocol Plugin
 *
 * 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.
 *
 * 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 <purple.h>

#include "jabber.h"
#include "iq.h"
#include "oob.h"

/* Number of bytes to read asynchronously per chunk. */
#define JABBER_OOB_READ_SIZE (1024*1024)

struct _JabberOOBXfer {
	JabberStream *js;
	gchar *iq_id;
	gchar *url;
	GCancellable *cancellable;
	SoupMessage *msg;
};

G_DEFINE_DYNAMIC_TYPE(JabberOOBXfer, jabber_oob_xfer, PURPLE_TYPE_XFER);

static void jabber_oob_xfer_xfer_init(PurpleXfer *xfer)
{
	purple_xfer_start(xfer, -1, NULL, 0);
}

static void jabber_oob_xfer_end(PurpleXfer *xfer)
{
	JabberOOBXfer *jox = JABBER_OOB_XFER(xfer);
	JabberIq *iq;

	iq = jabber_iq_new(jox->js, JABBER_IQ_RESULT);
	purple_xmlnode_set_attrib(iq->node, "to", purple_xfer_get_remote_user(xfer));
	jabber_iq_set_id(iq, jox->iq_id);

	jabber_iq_send(iq);
}

static void
jabber_oob_xfer_got_content_length(SoupMessage *msg, gpointer user_data)
{
	PurpleXfer *xfer = user_data;
	SoupMessageHeaders *headers;
	goffset total;

	headers = soup_message_get_response_headers(msg);
	total = soup_message_headers_get_content_length(headers);

	purple_xfer_set_size(xfer, total);
}

static void
jabber_oob_xfer_writer(GObject *source, GAsyncResult *result, gpointer data) {
	GInputStream *input = G_INPUT_STREAM(source);
	PurpleXfer *xfer = data;
	JabberOOBXfer *jox = JABBER_OOB_XFER(xfer);
	GBytes *bytes = NULL;
	GError *error = NULL;
	gpointer buffer = NULL;
	gsize size = 0;

	bytes = g_input_stream_read_bytes_finish(input, result, &error);
	if(bytes == NULL || error != NULL) {
		purple_debug_error("jabber", "Error reading OOB data: %s",
		                   error ? error->message : "unknown error");
		purple_xfer_set_status(xfer, PURPLE_XFER_STATUS_CANCEL_REMOTE);
		purple_xfer_end(xfer);
		g_clear_pointer(&bytes, g_bytes_unref);
		g_clear_object(&input);
		jox->msg = NULL;
		return;
	}

	buffer = g_bytes_unref_to_data(bytes, &size);

	/* We've reached the end of the file. */
	if(size == 0) {
		purple_xfer_set_completed(xfer, TRUE);
		purple_xfer_end(xfer);
		g_clear_object(&input);
		jox->msg = NULL;
		return;
	}

	if (!purple_xfer_write_file(xfer, buffer, size)) {
		purple_xfer_set_status(xfer, PURPLE_XFER_STATUS_CANCEL_LOCAL);
		purple_xfer_end(xfer);
		g_clear_object(&input);
		jox->msg = NULL;
		return;
	}

	/* Asynchronously read the next chunk of data. */
	g_input_stream_read_bytes_async(input, JABBER_OOB_READ_SIZE,
	                                G_PRIORITY_DEFAULT, jox->cancellable,
	                                jabber_oob_xfer_writer, xfer);
}

static void
jabber_oob_xfer_send_cb(GObject *source, GAsyncResult *result, gpointer data) {
	PurpleXfer *xfer = data;
	JabberOOBXfer *jox = JABBER_OOB_XFER(xfer);
	GInputStream *input = NULL;
	GError *error = NULL;

	if(!SOUP_STATUS_IS_SUCCESSFUL(soup_message_get_status(jox->msg))) {
		purple_xfer_set_status(xfer, PURPLE_XFER_STATUS_CANCEL_REMOTE);
		purple_xfer_end(xfer);
		jox->msg = NULL;
		return;
	}

	input = soup_session_send_finish(SOUP_SESSION(source), result, &error);
	if(input == NULL || error != NULL) {
		purple_debug_error("jabber", "Error sending OOB request: %s",
		                   error ? error->message : "unknown error");
		purple_xfer_set_status(xfer, PURPLE_XFER_STATUS_CANCEL_REMOTE);
		purple_xfer_end(xfer);
		g_clear_object(&input);
		jox->msg = NULL;
		return;
	}

	/* Asynchronously read an initial chunk of data. */
	g_input_stream_read_bytes_async(input, JABBER_OOB_READ_SIZE,
	                                G_PRIORITY_DEFAULT, jox->cancellable,
	                                jabber_oob_xfer_writer, xfer);
}

static void jabber_oob_xfer_start(PurpleXfer *xfer)
{
	SoupMessage *msg;
	JabberOOBXfer *jox = JABBER_OOB_XFER(xfer);

	msg = soup_message_new("GET", jox->url);
	soup_message_add_header_handler(
	        msg, "got-headers", "Content-Length",
	        G_CALLBACK(jabber_oob_xfer_got_content_length), xfer);
	soup_session_send_async(jox->js->http_conns, msg, G_PRIORITY_DEFAULT,
	                        jox->cancellable, jabber_oob_xfer_send_cb, xfer);
}

static void jabber_oob_xfer_recv_error(PurpleXfer *xfer, const char *code) {
	JabberOOBXfer *jox = JABBER_OOB_XFER(xfer);
	JabberIq *iq;
	PurpleXmlNode *y, *z;

	iq = jabber_iq_new(jox->js, JABBER_IQ_ERROR);
	purple_xmlnode_set_attrib(iq->node, "to", purple_xfer_get_remote_user(xfer));
	jabber_iq_set_id(iq, jox->iq_id);
	y = purple_xmlnode_new_child(iq->node, "error");
	purple_xmlnode_set_attrib(y, "code", code);
	if(purple_strequal(code, "406")) {
		z = purple_xmlnode_new_child(y, "not-acceptable");
		purple_xmlnode_set_attrib(y, "type", "modify");
		purple_xmlnode_set_namespace(z, NS_XMPP_STANZAS);
	} else if(purple_strequal(code, "404")) {
		z = purple_xmlnode_new_child(y, "not-found");
		purple_xmlnode_set_attrib(y, "type", "cancel");
		purple_xmlnode_set_namespace(z, NS_XMPP_STANZAS);
	}
	jabber_iq_send(iq);
}

static void jabber_oob_xfer_recv_denied(PurpleXfer *xfer) {
	jabber_oob_xfer_recv_error(xfer, "406");
}

static void jabber_oob_xfer_recv_cancelled(PurpleXfer *xfer) {
	JabberOOBXfer *jox = JABBER_OOB_XFER(xfer);

	g_cancellable_cancel(jox->cancellable);

	jabber_oob_xfer_recv_error(xfer, "404");
}

void jabber_oob_parse(JabberStream *js, const char *from, JabberIqType type,
	const char *id, PurpleXmlNode *querynode) {
	JabberOOBXfer *jox;
	const gchar *filename, *slash;
	gchar *url;
	PurpleXmlNode *urlnode;

	if(type != JABBER_IQ_SET)
		return;

	if(!from)
		return;

	if(!(urlnode = purple_xmlnode_get_child(querynode, "url")))
		return;

	url = purple_xmlnode_get_data(urlnode);
	if (!url)
		return;

	jox = g_object_new(
		JABBER_TYPE_OOB_XFER,
		"account", purple_connection_get_account(js->gc),
		"type", PURPLE_XFER_TYPE_RECEIVE,
		"remote-user", from,
		NULL
	);

	jox->iq_id = g_strdup(id);
	jox->js = js;
	jox->url = url;

	slash = strrchr(url, '/');
	if (slash == NULL) {
		filename = url;
	} else {
		filename = slash + 1;
	}

	purple_xfer_set_filename(PURPLE_XFER(jox), filename);

	js->oob_file_transfers = g_list_append(js->oob_file_transfers, jox);

	purple_xfer_request(PURPLE_XFER(jox));
}

static void
jabber_oob_xfer_init(JabberOOBXfer *xfer) {
	xfer->cancellable = g_cancellable_new();
}

static void
jabber_oob_xfer_finalize(GObject *obj) {
	JabberOOBXfer *jox = JABBER_OOB_XFER(obj);

	jox->js->oob_file_transfers = g_list_remove(jox->js->oob_file_transfers,
			jox);

	g_free(jox->iq_id);
	g_free(jox->url);
	if(jox->cancellable != NULL) {
		g_cancellable_cancel(jox->cancellable);
	}
	g_clear_object(&jox->cancellable);

	G_OBJECT_CLASS(jabber_oob_xfer_parent_class)->finalize(obj);
}

static void
jabber_oob_xfer_class_finalize(G_GNUC_UNUSED JabberOOBXferClass *klass) {
}

static void
jabber_oob_xfer_class_init(JabberOOBXferClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	PurpleXferClass *xfer_class = PURPLE_XFER_CLASS(klass);

	obj_class->finalize = jabber_oob_xfer_finalize;

	xfer_class->init = jabber_oob_xfer_xfer_init;
	xfer_class->end = jabber_oob_xfer_end;
	xfer_class->request_denied = jabber_oob_xfer_recv_denied;
	xfer_class->cancel_recv = jabber_oob_xfer_recv_cancelled;
	xfer_class->start = jabber_oob_xfer_start;
}

void
jabber_oob_xfer_register(GTypeModule *module) {
	jabber_oob_xfer_register_type(module);
}