Mercurial > pidgin
annotate src/protocols/jabber/oob.c @ 13714:4cf1db3ffada
[gaim-migrate @ 16122]
SF Patch #1479292 from resiak
This cleans up the Ctrl-Tab and Ctrl-Shift-Tab handling so it's easier to understand.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Tue, 02 May 2006 01:44:14 +0000 |
parents | 33bef17125c2 |
children | 25e63008d3bb |
rev | line source |
---|---|
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" | |
12324
90f488e08216
[gaim-migrate @ 14628]
Richard Laager <rlaager@wiktel.com>
parents:
12323
diff
changeset
|
28 #include "oob.h" |
7170 | 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 | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
41 gchar *write_buffer; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
42 gsize written_len; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
43 guint writeh; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
44 |
7170 | 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; | |
7395 | 56 jox->js->oob_file_transfers = g_list_remove(jox->js->oob_file_transfers, |
57 xfer); | |
7170 | 58 |
59 g_string_free(jox->headers, TRUE); | |
60 g_free(jox->address); | |
61 g_free(jox->page); | |
62 g_free(jox->iq_id); | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
63 g_free(jox->write_buffer); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
64 if(jox->writeh) |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
65 gaim_input_remove(jox->writeh); |
7170 | 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 | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
85 static void jabber_oob_xfer_request_send(gpointer data, gint source, GaimInputCondition cond) { |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
86 GaimXfer *xfer = data; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
87 JabberOOBXfer *jox = xfer->data; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
88 int len, total_len = strlen(jox->write_buffer); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
89 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
90 len = write(xfer->fd, jox->write_buffer + jox->written_len, |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
91 total_len - jox->written_len); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
92 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
93 if(len < 0 && errno == EAGAIN) |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
94 return; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
95 else if(len < 0) { |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
96 gaim_debug(GAIM_DEBUG_ERROR, "jabber", "Write error on oob xfer!\n"); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
97 gaim_input_remove(jox->writeh); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
98 gaim_xfer_cancel_local(xfer); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
99 } |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
100 jox->written_len += len; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
101 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
102 if(jox->written_len == total_len) { |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
103 gaim_input_remove(jox->writeh); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
104 g_free(jox->write_buffer); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
105 jox->write_buffer = NULL; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
106 } |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
107 } |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
108 |
7170 | 109 static void jabber_oob_xfer_start(GaimXfer *xfer) |
110 { | |
111 JabberOOBXfer *jox = xfer->data; | |
112 | |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
113 if(jox->write_buffer == NULL) { |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
114 jox->write_buffer = g_strdup_printf( |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
115 "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", |
7170 | 116 jox->page, jox->address); |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
117 jox->written_len = 0; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
118 } |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
119 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
120 jox->writeh = gaim_input_add(xfer->fd, GAIM_INPUT_WRITE, |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
121 jabber_oob_xfer_request_send, xfer); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
122 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
123 jabber_oob_xfer_request_send(xfer, xfer->fd, GAIM_INPUT_WRITE); |
7170 | 124 } |
125 | |
12151
de798f2f4bf1
[gaim-migrate @ 14452]
Richard Laager <rlaager@wiktel.com>
parents:
11159
diff
changeset
|
126 static gssize jabber_oob_xfer_read(guchar **buffer, GaimXfer *xfer) { |
7170 | 127 JabberOOBXfer *jox = xfer->data; |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
128 char test[2048]; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
129 char *tmp, *lenstr; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
130 int len; |
7170 | 131 |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
132 if((len = read(xfer->fd, test, sizeof(test))) > 0) { |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
133 jox->headers = g_string_append_len(jox->headers, test, len); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
134 if((tmp = strstr(jox->headers->str, "\r\n\r\n"))) { |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
135 *tmp = '\0'; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
136 lenstr = strstr(jox->headers->str, "Content-Length: "); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
137 if(lenstr) { |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
138 int size; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
139 sscanf(lenstr, "Content-Length: %d", &size); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
140 gaim_xfer_set_size(xfer, size); |
7170 | 141 } |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
142 gaim_xfer_set_read_fnc(xfer, NULL); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
143 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
144 tmp += 4; |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
145 |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
146 *buffer = g_strdup(tmp); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
147 return strlen(tmp); |
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
148 } |
7170 | 149 return 0; |
13200
33bef17125c2
[gaim-migrate @ 15563]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12324
diff
changeset
|
150 } else if (errno != EAGAIN) { |
7234 | 151 gaim_debug(GAIM_DEBUG_ERROR, "jabber", "Read error on oob xfer!\n"); |
152 gaim_xfer_cancel_local(xfer); | |
7170 | 153 } |
7234 | 154 |
7170 | 155 return 0; |
156 } | |
157 | |
8399 | 158 static void jabber_oob_xfer_recv_error(GaimXfer *xfer, const char *code) { |
7170 | 159 JabberOOBXfer *jox = xfer->data; |
160 JabberIq *iq; | |
8399 | 161 xmlnode *y, *z; |
7170 | 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"); | |
8399 | 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_attrib(z, "xmlns", "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_attrib(z, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas"); | |
176 } | |
7170 | 177 jabber_iq_send(iq); |
178 | |
179 jabber_oob_xfer_free(xfer); | |
180 } | |
181 | |
8399 | 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 | |
12324
90f488e08216
[gaim-migrate @ 14628]
Richard Laager <rlaager@wiktel.com>
parents:
12323
diff
changeset
|
190 void jabber_oob_parse(JabberStream *js, xmlnode *packet) { |
7170 | 191 JabberOOBXfer *jox; |
192 GaimXfer *xfer; | |
193 char *filename; | |
194 char *url; | |
195 xmlnode *querynode, *urlnode; | |
196 | |
197 if(!(querynode = xmlnode_get_child(packet, "query"))) | |
198 return; | |
199 | |
200 if(!(urlnode = xmlnode_get_child(querynode, "url"))) | |
201 return; | |
202 | |
203 url = xmlnode_get_data(urlnode); | |
204 | |
205 jox = g_new0(JabberOOBXfer, 1); | |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
8399
diff
changeset
|
206 gaim_url_parse(url, &jox->address, &jox->port, &jox->page, NULL, NULL); |
7170 | 207 g_free(url); |
208 jox->js = js; | |
209 jox->headers = g_string_new(""); | |
210 jox->iq_id = g_strdup(xmlnode_get_attrib(packet, "id")); | |
211 | |
212 xfer = gaim_xfer_new(js->gc->account, GAIM_XFER_RECEIVE, | |
213 xmlnode_get_attrib(packet, "from")); | |
214 xfer->data = jox; | |
215 | |
216 if(!(filename = g_strdup(g_strrstr(jox->page, "/")))) | |
217 filename = g_strdup(jox->page); | |
218 | |
219 gaim_xfer_set_filename(xfer, filename); | |
220 | |
221 g_free(filename); | |
222 | |
223 gaim_xfer_set_init_fnc(xfer, jabber_oob_xfer_init); | |
224 gaim_xfer_set_end_fnc(xfer, jabber_oob_xfer_end); | |
8399 | 225 gaim_xfer_set_request_denied_fnc(xfer, jabber_oob_xfer_recv_denied); |
226 gaim_xfer_set_cancel_recv_fnc(xfer, jabber_oob_xfer_recv_canceled); | |
7170 | 227 gaim_xfer_set_read_fnc(xfer, jabber_oob_xfer_read); |
228 gaim_xfer_set_start_fnc(xfer, jabber_oob_xfer_start); | |
229 | |
7395 | 230 js->oob_file_transfers = g_list_append(js->oob_file_transfers, xfer); |
7170 | 231 |
232 gaim_xfer_request(xfer); | |
233 } | |
234 | |
235 |