14192
|
1 /*
|
|
2 * gaim - Jabber Protocol Plugin
|
|
3 *
|
|
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 */
|
|
21 #include "internal.h"
|
|
22 #include "debug.h"
|
|
23 #include "ft.h"
|
|
24 #include "util.h"
|
|
25
|
|
26 #include "jabber.h"
|
|
27 #include "iq.h"
|
|
28 #include "oob.h"
|
|
29
|
|
30 typedef struct _JabberOOBXfer {
|
|
31 char *address;
|
|
32 int port;
|
|
33 char *page;
|
|
34
|
|
35 GString *headers;
|
|
36
|
|
37 char *iq_id;
|
|
38
|
|
39 JabberStream *js;
|
|
40
|
|
41 gchar *write_buffer;
|
|
42 gsize written_len;
|
|
43 guint writeh;
|
|
44
|
|
45 } JabberOOBXfer;
|
|
46
|
|
47 static void jabber_oob_xfer_init(GaimXfer *xfer)
|
|
48 {
|
|
49 JabberOOBXfer *jox = xfer->data;
|
|
50 gaim_xfer_start(xfer, -1, jox->address, jox->port);
|
|
51 }
|
|
52
|
|
53 static void jabber_oob_xfer_free(GaimXfer *xfer)
|
|
54 {
|
|
55 JabberOOBXfer *jox = xfer->data;
|
|
56 jox->js->oob_file_transfers = g_list_remove(jox->js->oob_file_transfers,
|
|
57 xfer);
|
|
58
|
|
59 g_string_free(jox->headers, TRUE);
|
|
60 g_free(jox->address);
|
|
61 g_free(jox->page);
|
|
62 g_free(jox->iq_id);
|
|
63 g_free(jox->write_buffer);
|
|
64 if(jox->writeh)
|
|
65 gaim_input_remove(jox->writeh);
|
|
66 g_free(jox);
|
|
67
|
|
68 xfer->data = NULL;
|
|
69 }
|
|
70
|
|
71 static void jabber_oob_xfer_end(GaimXfer *xfer)
|
|
72 {
|
|
73 JabberOOBXfer *jox = xfer->data;
|
|
74 JabberIq *iq;
|
|
75
|
|
76 iq = jabber_iq_new(jox->js, JABBER_IQ_RESULT);
|
|
77 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
78 jabber_iq_set_id(iq, jox->iq_id);
|
|
79
|
|
80 jabber_iq_send(iq);
|
|
81
|
|
82 jabber_oob_xfer_free(xfer);
|
|
83 }
|
|
84
|
|
85 static void jabber_oob_xfer_request_send(gpointer data, gint source, GaimInputCondition cond) {
|
|
86 GaimXfer *xfer = data;
|
|
87 JabberOOBXfer *jox = xfer->data;
|
|
88 int len, total_len = strlen(jox->write_buffer);
|
|
89
|
|
90 len = write(xfer->fd, jox->write_buffer + jox->written_len,
|
|
91 total_len - jox->written_len);
|
|
92
|
|
93 if(len < 0 && errno == EAGAIN)
|
|
94 return;
|
|
95 else if(len < 0) {
|
|
96 gaim_debug(GAIM_DEBUG_ERROR, "jabber", "Write error on oob xfer!\n");
|
|
97 gaim_input_remove(jox->writeh);
|
|
98 gaim_xfer_cancel_local(xfer);
|
|
99 }
|
|
100 jox->written_len += len;
|
|
101
|
|
102 if(jox->written_len == total_len) {
|
|
103 gaim_input_remove(jox->writeh);
|
|
104 g_free(jox->write_buffer);
|
|
105 jox->write_buffer = NULL;
|
|
106 }
|
|
107 }
|
|
108
|
|
109 static void jabber_oob_xfer_start(GaimXfer *xfer)
|
|
110 {
|
|
111 JabberOOBXfer *jox = xfer->data;
|
|
112
|
|
113 if(jox->write_buffer == NULL) {
|
|
114 jox->write_buffer = g_strdup_printf(
|
|
115 "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n",
|
|
116 jox->page, jox->address);
|
|
117 jox->written_len = 0;
|
|
118 }
|
|
119
|
|
120 jox->writeh = gaim_input_add(xfer->fd, GAIM_INPUT_WRITE,
|
|
121 jabber_oob_xfer_request_send, xfer);
|
|
122
|
|
123 jabber_oob_xfer_request_send(xfer, xfer->fd, GAIM_INPUT_WRITE);
|
|
124 }
|
|
125
|
|
126 static gssize jabber_oob_xfer_read(guchar **buffer, GaimXfer *xfer) {
|
|
127 JabberOOBXfer *jox = xfer->data;
|
|
128 char test[2048];
|
|
129 char *tmp, *lenstr;
|
|
130 int len;
|
|
131
|
|
132 if((len = read(xfer->fd, test, sizeof(test))) > 0) {
|
|
133 jox->headers = g_string_append_len(jox->headers, test, len);
|
|
134 if((tmp = strstr(jox->headers->str, "\r\n\r\n"))) {
|
|
135 *tmp = '\0';
|
|
136 lenstr = strstr(jox->headers->str, "Content-Length: ");
|
|
137 if(lenstr) {
|
|
138 int size;
|
|
139 sscanf(lenstr, "Content-Length: %d", &size);
|
|
140 gaim_xfer_set_size(xfer, size);
|
|
141 }
|
|
142 gaim_xfer_set_read_fnc(xfer, NULL);
|
|
143
|
|
144 tmp += 4;
|
|
145
|
|
146 *buffer = g_strdup(tmp);
|
|
147 return strlen(tmp);
|
|
148 }
|
|
149 return 0;
|
|
150 } else if (errno != EAGAIN) {
|
|
151 gaim_debug(GAIM_DEBUG_ERROR, "jabber", "Read error on oob xfer!\n");
|
|
152 gaim_xfer_cancel_local(xfer);
|
|
153 }
|
|
154
|
|
155 return 0;
|
|
156 }
|
|
157
|
|
158 static void jabber_oob_xfer_recv_error(GaimXfer *xfer, const char *code) {
|
|
159 JabberOOBXfer *jox = xfer->data;
|
|
160 JabberIq *iq;
|
|
161 xmlnode *y, *z;
|
|
162
|
|
163 iq = jabber_iq_new(jox->js, JABBER_IQ_ERROR);
|
|
164 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
165 jabber_iq_set_id(iq, jox->iq_id);
|
|
166 y = xmlnode_new_child(iq->node, "error");
|
|
167 xmlnode_set_attrib(y, "code", code);
|
|
168 if(!strcmp(code, "406")) {
|
|
169 z = xmlnode_new_child(y, "not-acceptable");
|
|
170 xmlnode_set_attrib(y, "type", "modify");
|
|
171 xmlnode_set_namespace(z, "urn:ietf:params:xml:ns:xmpp-stanzas");
|
|
172 } else if(!strcmp(code, "404")) {
|
|
173 z = xmlnode_new_child(y, "not-found");
|
|
174 xmlnode_set_attrib(y, "type", "cancel");
|
|
175 xmlnode_set_namespace(z, "urn:ietf:params:xml:ns:xmpp-stanzas");
|
|
176 }
|
|
177 jabber_iq_send(iq);
|
|
178
|
|
179 jabber_oob_xfer_free(xfer);
|
|
180 }
|
|
181
|
|
182 static void jabber_oob_xfer_recv_denied(GaimXfer *xfer) {
|
|
183 jabber_oob_xfer_recv_error(xfer, "406");
|
|
184 }
|
|
185
|
|
186 static void jabber_oob_xfer_recv_canceled(GaimXfer *xfer) {
|
|
187 jabber_oob_xfer_recv_error(xfer, "404");
|
|
188 }
|
|
189
|
|
190 void jabber_oob_parse(JabberStream *js, xmlnode *packet) {
|
|
191 JabberOOBXfer *jox;
|
|
192 GaimXfer *xfer;
|
|
193 char *filename;
|
|
194 char *url;
|
14294
|
195 const char *type;
|
14192
|
196 xmlnode *querynode, *urlnode;
|
|
197
|
14294
|
198 if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "set"))
|
|
199 return;
|
|
200
|
14192
|
201 if(!(querynode = xmlnode_get_child(packet, "query")))
|
|
202 return;
|
|
203
|
|
204 if(!(urlnode = xmlnode_get_child(querynode, "url")))
|
|
205 return;
|
|
206
|
|
207 url = xmlnode_get_data(urlnode);
|
|
208
|
|
209 jox = g_new0(JabberOOBXfer, 1);
|
|
210 gaim_url_parse(url, &jox->address, &jox->port, &jox->page, NULL, NULL);
|
|
211 g_free(url);
|
|
212 jox->js = js;
|
|
213 jox->headers = g_string_new("");
|
|
214 jox->iq_id = g_strdup(xmlnode_get_attrib(packet, "id"));
|
|
215
|
|
216 xfer = gaim_xfer_new(js->gc->account, GAIM_XFER_RECEIVE,
|
|
217 xmlnode_get_attrib(packet, "from"));
|
|
218 xfer->data = jox;
|
|
219
|
|
220 if(!(filename = g_strdup(g_strrstr(jox->page, "/"))))
|
|
221 filename = g_strdup(jox->page);
|
|
222
|
|
223 gaim_xfer_set_filename(xfer, filename);
|
|
224
|
|
225 g_free(filename);
|
|
226
|
|
227 gaim_xfer_set_init_fnc(xfer, jabber_oob_xfer_init);
|
|
228 gaim_xfer_set_end_fnc(xfer, jabber_oob_xfer_end);
|
|
229 gaim_xfer_set_request_denied_fnc(xfer, jabber_oob_xfer_recv_denied);
|
|
230 gaim_xfer_set_cancel_recv_fnc(xfer, jabber_oob_xfer_recv_canceled);
|
|
231 gaim_xfer_set_read_fnc(xfer, jabber_oob_xfer_read);
|
|
232 gaim_xfer_set_start_fnc(xfer, jabber_oob_xfer_start);
|
|
233
|
|
234 js->oob_file_transfers = g_list_append(js->oob_file_transfers, xfer);
|
|
235
|
|
236 gaim_xfer_request(xfer);
|
|
237 }
|
|
238
|
|
239
|