Mercurial > pidgin
annotate src/protocols/jabber/oob.c @ 13152:4bb701a8736f
[gaim-migrate @ 15515]
gaim_utf8_strftime() will now provide %z itself if your C library doesn't have it. I'm going to use this shortly.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Tue, 07 Feb 2006 07:25:45 +0000 |
parents | 90f488e08216 |
children | 33bef17125c2 |
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 gboolean newline; | |
37 | |
38 char *iq_id; | |
39 | |
40 JabberStream *js; | |
41 | |
42 } JabberOOBXfer; | |
43 | |
44 static void jabber_oob_xfer_init(GaimXfer *xfer) | |
45 { | |
46 JabberOOBXfer *jox = xfer->data; | |
47 gaim_xfer_start(xfer, -1, jox->address, jox->port); | |
48 } | |
49 | |
50 static void jabber_oob_xfer_free(GaimXfer *xfer) | |
51 { | |
52 JabberOOBXfer *jox = xfer->data; | |
7395 | 53 jox->js->oob_file_transfers = g_list_remove(jox->js->oob_file_transfers, |
54 xfer); | |
7170 | 55 |
56 g_string_free(jox->headers, TRUE); | |
57 g_free(jox->address); | |
58 g_free(jox->page); | |
59 g_free(jox->iq_id); | |
60 g_free(jox); | |
61 | |
62 xfer->data = NULL; | |
63 } | |
64 | |
65 static void jabber_oob_xfer_end(GaimXfer *xfer) | |
66 { | |
67 JabberOOBXfer *jox = xfer->data; | |
68 JabberIq *iq; | |
69 | |
70 iq = jabber_iq_new(jox->js, JABBER_IQ_RESULT); | |
71 xmlnode_set_attrib(iq->node, "to", xfer->who); | |
72 jabber_iq_set_id(iq, jox->iq_id); | |
73 | |
74 jabber_iq_send(iq); | |
75 | |
76 jabber_oob_xfer_free(xfer); | |
77 } | |
78 | |
79 static void jabber_oob_xfer_start(GaimXfer *xfer) | |
80 { | |
81 JabberOOBXfer *jox = xfer->data; | |
82 | |
83 char *buf = g_strdup_printf("GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", | |
84 jox->page, jox->address); | |
85 write(xfer->fd, buf, strlen(buf)); | |
86 g_free(buf); | |
87 } | |
88 | |
12151
de798f2f4bf1
[gaim-migrate @ 14452]
Richard Laager <rlaager@wiktel.com>
parents:
11159
diff
changeset
|
89 static gssize jabber_oob_xfer_read(guchar **buffer, GaimXfer *xfer) { |
7170 | 90 JabberOOBXfer *jox = xfer->data; |
91 char test; | |
92 int size; | |
93 | |
94 if(read(xfer->fd, &test, sizeof(test)) > 0) { | |
95 jox->headers = g_string_append_c(jox->headers, test); | |
96 if(test == '\r') | |
97 return 0; | |
98 if(test == '\n') { | |
99 if(jox->newline) { | |
100 gchar *lenstr = strstr(jox->headers->str, "Content-Length: "); | |
101 if(lenstr) { | |
102 sscanf(lenstr, "Content-Length: %d", &size); | |
103 gaim_xfer_set_size(xfer, size); | |
104 } | |
105 gaim_xfer_set_read_fnc(xfer, NULL); | |
106 return 0; | |
107 } else | |
108 jox->newline = TRUE; | |
109 return 0; | |
110 } | |
111 jox->newline = FALSE; | |
112 return 0; | |
7234 | 113 } else { |
114 gaim_debug(GAIM_DEBUG_ERROR, "jabber", "Read error on oob xfer!\n"); | |
115 gaim_xfer_cancel_local(xfer); | |
7170 | 116 } |
7234 | 117 |
7170 | 118 return 0; |
119 } | |
120 | |
8399 | 121 static void jabber_oob_xfer_recv_error(GaimXfer *xfer, const char *code) { |
7170 | 122 JabberOOBXfer *jox = xfer->data; |
123 JabberIq *iq; | |
8399 | 124 xmlnode *y, *z; |
7170 | 125 |
126 iq = jabber_iq_new(jox->js, JABBER_IQ_ERROR); | |
127 xmlnode_set_attrib(iq->node, "to", xfer->who); | |
128 jabber_iq_set_id(iq, jox->iq_id); | |
129 y = xmlnode_new_child(iq->node, "error"); | |
8399 | 130 xmlnode_set_attrib(y, "code", code); |
131 if(!strcmp(code, "406")) { | |
132 z = xmlnode_new_child(y, "not-acceptable"); | |
133 xmlnode_set_attrib(y, "type", "modify"); | |
134 xmlnode_set_attrib(z, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas"); | |
135 } else if(!strcmp(code, "404")) { | |
136 z = xmlnode_new_child(y, "not-found"); | |
137 xmlnode_set_attrib(y, "type", "cancel"); | |
138 xmlnode_set_attrib(z, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas"); | |
139 } | |
7170 | 140 jabber_iq_send(iq); |
141 | |
142 jabber_oob_xfer_free(xfer); | |
143 } | |
144 | |
8399 | 145 static void jabber_oob_xfer_recv_denied(GaimXfer *xfer) { |
146 jabber_oob_xfer_recv_error(xfer, "406"); | |
147 } | |
148 | |
149 static void jabber_oob_xfer_recv_canceled(GaimXfer *xfer) { | |
150 jabber_oob_xfer_recv_error(xfer, "404"); | |
151 } | |
152 | |
12324
90f488e08216
[gaim-migrate @ 14628]
Richard Laager <rlaager@wiktel.com>
parents:
12323
diff
changeset
|
153 void jabber_oob_parse(JabberStream *js, xmlnode *packet) { |
7170 | 154 JabberOOBXfer *jox; |
155 GaimXfer *xfer; | |
156 char *filename; | |
157 char *url; | |
158 xmlnode *querynode, *urlnode; | |
159 | |
160 if(!(querynode = xmlnode_get_child(packet, "query"))) | |
161 return; | |
162 | |
163 if(!(urlnode = xmlnode_get_child(querynode, "url"))) | |
164 return; | |
165 | |
166 url = xmlnode_get_data(urlnode); | |
167 | |
168 jox = g_new0(JabberOOBXfer, 1); | |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
8399
diff
changeset
|
169 gaim_url_parse(url, &jox->address, &jox->port, &jox->page, NULL, NULL); |
7170 | 170 g_free(url); |
171 jox->js = js; | |
172 jox->headers = g_string_new(""); | |
173 jox->iq_id = g_strdup(xmlnode_get_attrib(packet, "id")); | |
174 | |
175 xfer = gaim_xfer_new(js->gc->account, GAIM_XFER_RECEIVE, | |
176 xmlnode_get_attrib(packet, "from")); | |
177 xfer->data = jox; | |
178 | |
179 if(!(filename = g_strdup(g_strrstr(jox->page, "/")))) | |
180 filename = g_strdup(jox->page); | |
181 | |
182 gaim_xfer_set_filename(xfer, filename); | |
183 | |
184 g_free(filename); | |
185 | |
186 gaim_xfer_set_init_fnc(xfer, jabber_oob_xfer_init); | |
187 gaim_xfer_set_end_fnc(xfer, jabber_oob_xfer_end); | |
8399 | 188 gaim_xfer_set_request_denied_fnc(xfer, jabber_oob_xfer_recv_denied); |
189 gaim_xfer_set_cancel_recv_fnc(xfer, jabber_oob_xfer_recv_canceled); | |
7170 | 190 gaim_xfer_set_read_fnc(xfer, jabber_oob_xfer_read); |
191 gaim_xfer_set_start_fnc(xfer, jabber_oob_xfer_start); | |
192 | |
7395 | 193 js->oob_file_transfers = g_list_append(js->oob_file_transfers, xfer); |
7170 | 194 |
195 gaim_xfer_request(xfer); | |
196 } | |
197 | |
198 |