7170
|
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"
|
7234
|
22 #include "debug.h"
|
7170
|
23 #include "ft.h"
|
|
24 #include "util.h"
|
|
25
|
|
26 #include "jabber.h"
|
|
27 #include "iq.h"
|
|
28
|
|
29 typedef struct _JabberOOBXfer {
|
|
30 char *address;
|
|
31 int port;
|
|
32 char *page;
|
|
33
|
|
34 GString *headers;
|
|
35 gboolean newline;
|
|
36
|
|
37 char *iq_id;
|
|
38
|
|
39 JabberStream *js;
|
|
40
|
|
41 } JabberOOBXfer;
|
|
42
|
|
43 static void jabber_oob_xfer_init(GaimXfer *xfer)
|
|
44 {
|
|
45 JabberOOBXfer *jox = xfer->data;
|
|
46 gaim_xfer_start(xfer, -1, jox->address, jox->port);
|
|
47 }
|
|
48
|
|
49 static void jabber_oob_xfer_free(GaimXfer *xfer)
|
|
50 {
|
|
51 JabberOOBXfer *jox = xfer->data;
|
|
52 jox->js->file_transfers = g_list_remove(jox->js->file_transfers, xfer);
|
|
53
|
|
54 g_string_free(jox->headers, TRUE);
|
|
55 g_free(jox->address);
|
|
56 g_free(jox->page);
|
|
57 g_free(jox->iq_id);
|
|
58 g_free(jox);
|
|
59
|
|
60 xfer->data = NULL;
|
|
61 }
|
|
62
|
|
63 static void jabber_oob_xfer_end(GaimXfer *xfer)
|
|
64 {
|
|
65 JabberOOBXfer *jox = xfer->data;
|
|
66 JabberIq *iq;
|
|
67
|
|
68 iq = jabber_iq_new(jox->js, JABBER_IQ_RESULT);
|
|
69 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
70 jabber_iq_set_id(iq, jox->iq_id);
|
|
71
|
|
72 jabber_iq_send(iq);
|
|
73
|
|
74 jabber_oob_xfer_free(xfer);
|
|
75 }
|
|
76
|
|
77 static void jabber_oob_xfer_start(GaimXfer *xfer)
|
|
78 {
|
|
79 JabberOOBXfer *jox = xfer->data;
|
|
80
|
|
81 char *buf = g_strdup_printf("GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n",
|
|
82 jox->page, jox->address);
|
|
83 write(xfer->fd, buf, strlen(buf));
|
|
84 g_free(buf);
|
|
85 }
|
|
86
|
|
87 static size_t jabber_oob_xfer_read(char **buffer, GaimXfer *xfer) {
|
|
88 JabberOOBXfer *jox = xfer->data;
|
|
89 char test;
|
|
90 int size;
|
|
91
|
|
92 if(read(xfer->fd, &test, sizeof(test)) > 0) {
|
|
93 jox->headers = g_string_append_c(jox->headers, test);
|
|
94 if(test == '\r')
|
|
95 return 0;
|
|
96 if(test == '\n') {
|
|
97 if(jox->newline) {
|
|
98 gchar *lenstr = strstr(jox->headers->str, "Content-Length: ");
|
|
99 if(lenstr) {
|
|
100 sscanf(lenstr, "Content-Length: %d", &size);
|
|
101 gaim_xfer_set_size(xfer, size);
|
|
102 }
|
|
103 gaim_xfer_set_read_fnc(xfer, NULL);
|
|
104 return 0;
|
|
105 } else
|
|
106 jox->newline = TRUE;
|
|
107 return 0;
|
|
108 }
|
|
109 jox->newline = FALSE;
|
|
110 return 0;
|
7234
|
111 } else {
|
|
112 gaim_debug(GAIM_DEBUG_ERROR, "jabber", "Read error on oob xfer!\n");
|
|
113 gaim_xfer_cancel_local(xfer);
|
7170
|
114 }
|
7234
|
115
|
7170
|
116 return 0;
|
|
117 }
|
|
118
|
|
119 static void jabber_oob_xfer_cancel_send(GaimXfer *xfer) {
|
|
120 }
|
|
121
|
|
122 static void jabber_oob_xfer_cancel_recv(GaimXfer *xfer) {
|
|
123 JabberOOBXfer *jox = xfer->data;
|
|
124 JabberIq *iq;
|
|
125 xmlnode *y;
|
|
126
|
|
127 iq = jabber_iq_new(jox->js, JABBER_IQ_ERROR);
|
|
128 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
129 jabber_iq_set_id(iq, jox->iq_id);
|
|
130 y = xmlnode_new_child(iq->node, "error");
|
|
131 /* FIXME: need to handle other kinds of errors here */
|
|
132 xmlnode_set_attrib(y, "code", "406");
|
|
133 xmlnode_insert_data(y, "File Transfer Refused", -1);
|
|
134
|
|
135 jabber_iq_send(iq);
|
|
136
|
|
137 jabber_oob_xfer_free(xfer);
|
|
138 }
|
|
139
|
|
140 void jabber_oob_parse(JabberStream *js, xmlnode *packet) {
|
|
141 JabberOOBXfer *jox;
|
|
142 GaimXfer *xfer;
|
|
143 char *filename;
|
|
144 char *url;
|
|
145 xmlnode *querynode, *urlnode;
|
|
146
|
|
147 if(!(querynode = xmlnode_get_child(packet, "query")))
|
|
148 return;
|
|
149
|
|
150 if(!(urlnode = xmlnode_get_child(querynode, "url")))
|
|
151 return;
|
|
152
|
|
153 url = xmlnode_get_data(urlnode);
|
|
154
|
|
155 jox = g_new0(JabberOOBXfer, 1);
|
|
156 gaim_url_parse(url, &jox->address, &jox->port, &jox->page);
|
|
157 g_free(url);
|
|
158 jox->js = js;
|
|
159 jox->headers = g_string_new("");
|
|
160 jox->iq_id = g_strdup(xmlnode_get_attrib(packet, "id"));
|
|
161
|
|
162 xfer = gaim_xfer_new(js->gc->account, GAIM_XFER_RECEIVE,
|
|
163 xmlnode_get_attrib(packet, "from"));
|
|
164 xfer->data = jox;
|
|
165
|
|
166 if(!(filename = g_strdup(g_strrstr(jox->page, "/"))))
|
|
167 filename = g_strdup(jox->page);
|
|
168
|
|
169 gaim_xfer_set_filename(xfer, filename);
|
|
170
|
|
171 g_free(filename);
|
|
172
|
|
173 gaim_xfer_set_init_fnc(xfer, jabber_oob_xfer_init);
|
|
174 gaim_xfer_set_end_fnc(xfer, jabber_oob_xfer_end);
|
|
175 gaim_xfer_set_cancel_send_fnc(xfer, jabber_oob_xfer_cancel_send);
|
|
176 gaim_xfer_set_cancel_recv_fnc(xfer, jabber_oob_xfer_cancel_recv);
|
|
177 gaim_xfer_set_read_fnc(xfer, jabber_oob_xfer_read);
|
|
178 gaim_xfer_set_start_fnc(xfer, jabber_oob_xfer_start);
|
|
179
|
|
180 js->file_transfers = g_list_append(js->file_transfers, xfer);
|
|
181
|
|
182 gaim_xfer_request(xfer);
|
|
183 }
|
|
184
|
|
185
|