1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - SI packets * |
---|
5 | * * |
---|
6 | * Copyright 2007 Uli Meis <a.sporto+bee@gmail.com> * |
---|
7 | * * |
---|
8 | * This program is free software; you can redistribute it and/or modify * |
---|
9 | * it under the terms of the GNU General Public License as published by * |
---|
10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
11 | * (at your option) any later version. * |
---|
12 | * * |
---|
13 | * This program is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
16 | * GNU General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU General Public License along * |
---|
19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
21 | * * |
---|
22 | \***************************************************************************/ |
---|
23 | |
---|
24 | #include "jabber.h" |
---|
25 | #include "sha1.h" |
---|
26 | |
---|
27 | void jabber_si_answer_request( file_transfer_t *ft ); |
---|
28 | int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf ); |
---|
29 | |
---|
30 | /* file_transfer free() callback */ |
---|
31 | void jabber_si_free_transfer( file_transfer_t *ft) |
---|
32 | { |
---|
33 | struct jabber_transfer *tf = ft->data; |
---|
34 | struct jabber_data *jd = tf->ic->proto_data; |
---|
35 | |
---|
36 | if ( tf->watch_in ) |
---|
37 | b_event_remove( tf->watch_in ); |
---|
38 | |
---|
39 | jd->filetransfers = g_slist_remove( jd->filetransfers, tf ); |
---|
40 | |
---|
41 | if( tf->fd ) |
---|
42 | { |
---|
43 | close( tf->fd ); |
---|
44 | tf->fd = 0; |
---|
45 | } |
---|
46 | |
---|
47 | if( tf->disco_timeout ) |
---|
48 | b_event_remove( tf->disco_timeout ); |
---|
49 | |
---|
50 | g_free( tf->ini_jid ); |
---|
51 | g_free( tf->tgt_jid ); |
---|
52 | g_free( tf->iq_id ); |
---|
53 | g_free( tf->sid ); |
---|
54 | } |
---|
55 | |
---|
56 | /* file_transfer finished() callback */ |
---|
57 | void jabber_si_finished( file_transfer_t *ft ) |
---|
58 | { |
---|
59 | struct jabber_transfer *tf = ft->data; |
---|
60 | time_t diff = time( NULL ) - ft->started ? : 1; |
---|
61 | |
---|
62 | imcb_log( tf->ic, "File %s transferred successfully at %d kb/s!" , ft->file_name, (int) ( ft->bytes_transferred / 1024 / diff ) ); |
---|
63 | } |
---|
64 | |
---|
65 | /* file_transfer canceled() callback */ |
---|
66 | void jabber_si_canceled( file_transfer_t *ft, char *reason ) |
---|
67 | { |
---|
68 | struct jabber_transfer *tf = ft->data; |
---|
69 | struct xt_node *reply, *iqnode; |
---|
70 | |
---|
71 | if( tf->accepted ) |
---|
72 | return; |
---|
73 | |
---|
74 | iqnode = jabber_make_packet( "iq", "error", tf->ini_jid, NULL ); |
---|
75 | xt_add_attr( iqnode, "id", tf->iq_id ); |
---|
76 | reply = jabber_make_error_packet( iqnode, "forbidden", "cancel", "403" ); |
---|
77 | xt_free_node( iqnode ); |
---|
78 | |
---|
79 | if( !jabber_write_packet( tf->ic, reply ) ) |
---|
80 | imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" ); |
---|
81 | xt_free_node( reply ); |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | int jabber_si_check_features( struct jabber_transfer *tf, GSList *features ) { |
---|
86 | int foundft = FALSE, foundbt = FALSE, foundsi = FALSE; |
---|
87 | |
---|
88 | while ( features ) |
---|
89 | { |
---|
90 | if( !strcmp( features->data, XMLNS_FILETRANSFER ) ) |
---|
91 | foundft = TRUE; |
---|
92 | if( !strcmp( features->data, XMLNS_BYTESTREAMS ) ) |
---|
93 | foundbt = TRUE; |
---|
94 | if( !strcmp( features->data, XMLNS_SI ) ) |
---|
95 | foundsi = TRUE; |
---|
96 | |
---|
97 | features = g_slist_next(features); |
---|
98 | } |
---|
99 | |
---|
100 | if( !foundft ) |
---|
101 | imcb_file_canceled( tf->ft, "Buddy's client doesn't feature file transfers" ); |
---|
102 | else if( !foundbt ) |
---|
103 | imcb_file_canceled( tf->ft, "Buddy's client doesn't feature byte streams (required)" ); |
---|
104 | else if( !foundsi ) |
---|
105 | imcb_file_canceled( tf->ft, "Buddy's client doesn't feature stream initiation (required)" ); |
---|
106 | |
---|
107 | return foundft && foundbt && foundsi; |
---|
108 | } |
---|
109 | |
---|
110 | void jabber_si_transfer_start( struct jabber_transfer *tf ) { |
---|
111 | |
---|
112 | if( !jabber_si_check_features( tf, tf->bud->features ) ) |
---|
113 | return; |
---|
114 | |
---|
115 | /* send the request to our buddy */ |
---|
116 | jabber_si_send_request( tf->ic, tf->bud->full_jid, tf ); |
---|
117 | |
---|
118 | /* and start the receive logic */ |
---|
119 | imcb_file_recv_start( tf->ft ); |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | gboolean jabber_si_waitfor_disco( gpointer data, gint fd, b_input_condition cond ) |
---|
124 | { |
---|
125 | struct jabber_transfer *tf = data; |
---|
126 | struct jabber_data *jd = tf->ic->proto_data; |
---|
127 | |
---|
128 | tf->disco_timeout_fired++; |
---|
129 | |
---|
130 | if( tf->bud->features && jd->have_streamhosts==1 ) { |
---|
131 | jabber_si_transfer_start( tf ); |
---|
132 | tf->disco_timeout = 0; |
---|
133 | return FALSE; |
---|
134 | } |
---|
135 | |
---|
136 | /* 8 seconds should be enough for server and buddy to respond */ |
---|
137 | if ( tf->disco_timeout_fired < 16 ) |
---|
138 | return TRUE; |
---|
139 | |
---|
140 | if( !tf->bud->features && jd->have_streamhosts!=1 ) |
---|
141 | imcb_file_canceled( tf->ft, "Couldn't get buddy's features or the server's" ); |
---|
142 | else if( !tf->bud->features ) |
---|
143 | imcb_file_canceled( tf->ft, "Couldn't get buddy's features" ); |
---|
144 | else |
---|
145 | imcb_file_canceled( tf->ft, "Couldn't get server's features" ); |
---|
146 | |
---|
147 | tf->disco_timeout = 0; |
---|
148 | return FALSE; |
---|
149 | } |
---|
150 | |
---|
151 | void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who ) |
---|
152 | { |
---|
153 | struct jabber_transfer *tf; |
---|
154 | struct jabber_data *jd = ic->proto_data; |
---|
155 | struct jabber_buddy *bud; |
---|
156 | char *server = jd->server, *s; |
---|
157 | |
---|
158 | if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) ) |
---|
159 | bud = jabber_buddy_by_ext_jid( ic, who, 0 ); |
---|
160 | else |
---|
161 | bud = jabber_buddy_by_jid( ic, who, 0 ); |
---|
162 | |
---|
163 | if( bud == NULL ) |
---|
164 | { |
---|
165 | imcb_file_canceled( tf->ft, "Couldn't find buddy (BUG?)" ); |
---|
166 | return; |
---|
167 | } |
---|
168 | |
---|
169 | imcb_log( ic, "Trying to send %s(%zd bytes) to %s", ft->file_name, ft->file_size, who ); |
---|
170 | |
---|
171 | tf = g_new0( struct jabber_transfer, 1 ); |
---|
172 | |
---|
173 | tf->ic = ic; |
---|
174 | tf->ft = ft; |
---|
175 | tf->ft->data = tf; |
---|
176 | tf->ft->free = jabber_si_free_transfer; |
---|
177 | tf->ft->finished = jabber_si_finished; |
---|
178 | tf->bud = bud; |
---|
179 | ft->write = jabber_bs_send_write; |
---|
180 | |
---|
181 | jd->filetransfers = g_slist_prepend( jd->filetransfers, tf ); |
---|
182 | |
---|
183 | /* query buddy's features and server's streaming proxies if neccessary */ |
---|
184 | |
---|
185 | if( !tf->bud->features ) |
---|
186 | jabber_iq_query_features( ic, bud->full_jid ); |
---|
187 | |
---|
188 | if( jd->have_streamhosts!=1 ) { |
---|
189 | jd->have_streamhosts = 0; |
---|
190 | jabber_iq_query_server( ic, server, XMLNS_DISCO_ITEMS ); |
---|
191 | } |
---|
192 | |
---|
193 | /* if we had to do a query, wait for the result. |
---|
194 | * Otherwise fire away. */ |
---|
195 | if( !tf->bud->features || jd->have_streamhosts!=1 ) |
---|
196 | tf->disco_timeout = b_timeout_add( 500, jabber_si_waitfor_disco, tf ); |
---|
197 | else |
---|
198 | jabber_si_transfer_start( tf ); |
---|
199 | } |
---|
200 | |
---|
201 | /* |
---|
202 | * First function that gets called when a file transfer request comes in. |
---|
203 | * A lot to parse. |
---|
204 | * |
---|
205 | * We choose a stream type from the options given by the initiator. |
---|
206 | * Then we wait for imcb to call the accept or cancel callbacks. |
---|
207 | */ |
---|
208 | int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode) |
---|
209 | { |
---|
210 | struct xt_node *c, *d, *reply; |
---|
211 | char *sid, *ini_jid, *tgt_jid, *iq_id, *s, *ext_jid; |
---|
212 | struct jabber_buddy *bud; |
---|
213 | int requestok = FALSE; |
---|
214 | char *name; |
---|
215 | size_t size; |
---|
216 | struct jabber_transfer *tf; |
---|
217 | struct jabber_data *jd = ic->proto_data; |
---|
218 | file_transfer_t *ft; |
---|
219 | |
---|
220 | /* All this means we expect something like this: ( I think ) |
---|
221 | * <iq from=... to=... id=...> |
---|
222 | * <si id=id xmlns=si profile=ft> |
---|
223 | * <file xmlns=ft/> |
---|
224 | * <feature xmlns=feature> |
---|
225 | * <x xmlns=xdata type=submit> |
---|
226 | * <field var=stream-method> |
---|
227 | * |
---|
228 | */ |
---|
229 | if( !( ini_jid = xt_find_attr( node, "from" ) ) || |
---|
230 | !( tgt_jid = xt_find_attr( node, "to" ) ) || |
---|
231 | !( iq_id = xt_find_attr( node, "id" ) ) || |
---|
232 | !( sid = xt_find_attr( sinode, "id" ) ) || |
---|
233 | !( strcmp( xt_find_attr( sinode, "profile" ), XMLNS_FILETRANSFER ) == 0 ) || |
---|
234 | !( d = xt_find_node( sinode->children, "file" ) ) || |
---|
235 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0 ) || |
---|
236 | !( name = xt_find_attr( d, "name" ) ) || |
---|
237 | !( size = (size_t) atoll( xt_find_attr( d, "size" ) ) ) || |
---|
238 | !( d = xt_find_node( sinode->children, "feature" ) ) || |
---|
239 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0 ) || |
---|
240 | !( d = xt_find_node( d->children, "x" ) ) || |
---|
241 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0 ) || |
---|
242 | !( strcmp( xt_find_attr( d, "type" ), "form" ) == 0 ) || |
---|
243 | !( d = xt_find_node( d->children, "field" ) ) || |
---|
244 | !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0 ) ) |
---|
245 | { |
---|
246 | imcb_log( ic, "WARNING: Received incomplete Stream Initiation request" ); |
---|
247 | } else |
---|
248 | { |
---|
249 | /* Check if we support one of the options */ |
---|
250 | |
---|
251 | c = d->children; |
---|
252 | while( ( c = xt_find_node( c, "option" ) ) ) |
---|
253 | if( ( d = xt_find_node( c->children, "value" ) ) && |
---|
254 | ( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) |
---|
255 | { |
---|
256 | requestok = TRUE; |
---|
257 | break; |
---|
258 | } |
---|
259 | |
---|
260 | if ( !requestok ) |
---|
261 | imcb_log( ic, "WARNING: Unsupported file transfer request from %s", ini_jid); |
---|
262 | } |
---|
263 | |
---|
264 | if ( requestok ) |
---|
265 | { |
---|
266 | /* Figure out who the transfer should come frome... */ |
---|
267 | |
---|
268 | if( ( s = strchr( ini_jid, '/' ) ) ) |
---|
269 | { |
---|
270 | if( ( bud = jabber_buddy_by_jid( ic, ini_jid, GET_BUDDY_EXACT ) ) ) |
---|
271 | { |
---|
272 | bud->last_act = time( NULL ); |
---|
273 | ext_jid = bud->ext_jid ? : bud->bare_jid; |
---|
274 | } |
---|
275 | else |
---|
276 | *s = 0; /* We need to generate a bare JID now. */ |
---|
277 | } |
---|
278 | |
---|
279 | if( !( ft = imcb_file_send_start( ic, ext_jid, name, size ) ) ) |
---|
280 | { |
---|
281 | imcb_log( ic, "WARNING: Error handling transfer request from %s", ini_jid); |
---|
282 | requestok = FALSE; |
---|
283 | } |
---|
284 | |
---|
285 | *s = '/'; |
---|
286 | } |
---|
287 | |
---|
288 | if ( !requestok ) |
---|
289 | { |
---|
290 | reply = jabber_make_error_packet( node, "item-not-found", "cancel", NULL ); |
---|
291 | if (!jabber_write_packet( ic, reply )) |
---|
292 | imcb_log( ic, "WARNING: Error generating reply to file transfer request" ); |
---|
293 | xt_free_node( reply ); |
---|
294 | return XT_HANDLED; |
---|
295 | } |
---|
296 | |
---|
297 | /* Request is fine. */ |
---|
298 | |
---|
299 | imcb_log( ic, "File transfer request from %s for %s (%zd kb). ", xt_find_attr( node, "from" ), name, size/1024 ); |
---|
300 | |
---|
301 | imcb_log( ic, "Accept the file transfer if you'd like the file. If you don't, issue the 'transfers reject' command."); |
---|
302 | |
---|
303 | tf = g_new0( struct jabber_transfer, 1 ); |
---|
304 | |
---|
305 | tf->ini_jid = g_strdup( ini_jid ); |
---|
306 | tf->tgt_jid = g_strdup( tgt_jid ); |
---|
307 | tf->iq_id = g_strdup( iq_id ); |
---|
308 | tf->sid = g_strdup( sid ); |
---|
309 | tf->ic = ic; |
---|
310 | tf->ft = ft; |
---|
311 | tf->ft->data = tf; |
---|
312 | tf->ft->accept = jabber_si_answer_request; |
---|
313 | tf->ft->free = jabber_si_free_transfer; |
---|
314 | tf->ft->finished = jabber_si_finished; |
---|
315 | tf->ft->canceled = jabber_si_canceled; |
---|
316 | |
---|
317 | jd->filetransfers = g_slist_prepend( jd->filetransfers, tf ); |
---|
318 | |
---|
319 | return XT_HANDLED; |
---|
320 | } |
---|
321 | |
---|
322 | /* |
---|
323 | * imc called the accept callback which probably means that the user accepted this file transfer. |
---|
324 | * We send our response to the initiator. |
---|
325 | * In the next step, the initiator will send us a request for the given stream type. |
---|
326 | * (currently that can only be a SOCKS5 bytestream) |
---|
327 | */ |
---|
328 | void jabber_si_answer_request( file_transfer_t *ft ) { |
---|
329 | struct jabber_transfer *tf = ft->data; |
---|
330 | struct xt_node *node, *sinode, *reply; |
---|
331 | |
---|
332 | /* generate response, start with the SI tag */ |
---|
333 | sinode = xt_new_node( "si", NULL, NULL ); |
---|
334 | xt_add_attr( sinode, "xmlns", XMLNS_SI ); |
---|
335 | xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER ); |
---|
336 | xt_add_attr( sinode, "id", tf->sid ); |
---|
337 | |
---|
338 | /* now the file tag */ |
---|
339 | node = xt_new_node( "file", NULL, NULL ); |
---|
340 | xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER ); |
---|
341 | |
---|
342 | xt_add_child( sinode, node ); |
---|
343 | |
---|
344 | /* and finally the feature tag */ |
---|
345 | node = xt_new_node( "field", NULL, NULL ); |
---|
346 | xt_add_attr( node, "var", "stream-method" ); |
---|
347 | xt_add_attr( node, "type", "list-single" ); |
---|
348 | |
---|
349 | /* Currently all we can do. One could also implement in-band (IBB) */ |
---|
350 | xt_add_child( node, xt_new_node( "value", XMLNS_BYTESTREAMS, NULL ) ); |
---|
351 | |
---|
352 | node = xt_new_node( "x", NULL, node ); |
---|
353 | xt_add_attr( node, "xmlns", XMLNS_XDATA ); |
---|
354 | xt_add_attr( node, "type", "submit" ); |
---|
355 | |
---|
356 | node = xt_new_node( "feature", NULL, node ); |
---|
357 | xt_add_attr( node, "xmlns", XMLNS_FEATURE ); |
---|
358 | |
---|
359 | xt_add_child( sinode, node ); |
---|
360 | |
---|
361 | reply = jabber_make_packet( "iq", "result", tf->ini_jid, sinode ); |
---|
362 | xt_add_attr( reply, "id", tf->iq_id ); |
---|
363 | |
---|
364 | if( !jabber_write_packet( tf->ic, reply ) ) |
---|
365 | imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" ); |
---|
366 | else |
---|
367 | tf->accepted = TRUE; |
---|
368 | xt_free_node( reply ); |
---|
369 | } |
---|
370 | |
---|
371 | static xt_status jabber_si_handle_response(struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
372 | { |
---|
373 | struct xt_node *c, *d; |
---|
374 | char *ini_jid, *tgt_jid, *iq_id; |
---|
375 | GSList *tflist; |
---|
376 | struct jabber_transfer *tf=NULL; |
---|
377 | struct jabber_data *jd = ic->proto_data; |
---|
378 | |
---|
379 | if( !( tgt_jid = xt_find_attr( node, "from" ) ) || |
---|
380 | !( ini_jid = xt_find_attr( node, "to" ) ) ) |
---|
381 | { |
---|
382 | imcb_log( ic, "Invalid SI response from=%s to=%s", tgt_jid, ini_jid ); |
---|
383 | return XT_HANDLED; |
---|
384 | } |
---|
385 | |
---|
386 | /* All this means we expect something like this: ( I think ) |
---|
387 | * <iq from=... to=... id=...> |
---|
388 | * <si xmlns=si> |
---|
389 | * [ <file xmlns=ft/> ] <-- not neccessary |
---|
390 | * <feature xmlns=feature> |
---|
391 | * <x xmlns=xdata type=submit> |
---|
392 | * <field var=stream-method> |
---|
393 | * <value> |
---|
394 | */ |
---|
395 | if( !( tgt_jid = xt_find_attr( node, "from" ) ) || |
---|
396 | !( ini_jid = xt_find_attr( node, "to" ) ) || |
---|
397 | !( iq_id = xt_find_attr( node, "id" ) ) || |
---|
398 | !( c = xt_find_node( node->children, "si" ) ) || |
---|
399 | !( strcmp( xt_find_attr( c, "xmlns" ), XMLNS_SI ) == 0 ) || |
---|
400 | /* !( d = xt_find_node( c->children, "file" ) ) || |
---|
401 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0 ) || */ |
---|
402 | !( d = xt_find_node( c->children, "feature" ) ) || |
---|
403 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0 ) || |
---|
404 | !( d = xt_find_node( d->children, "x" ) ) || |
---|
405 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0 ) || |
---|
406 | !( strcmp( xt_find_attr( d, "type" ), "submit" ) == 0 ) || |
---|
407 | !( d = xt_find_node( d->children, "field" ) ) || |
---|
408 | !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0 ) || |
---|
409 | !( d = xt_find_node( d->children, "value" ) ) ) |
---|
410 | { |
---|
411 | imcb_log( ic, "WARNING: Received incomplete Stream Initiation response" ); |
---|
412 | return XT_HANDLED; |
---|
413 | } |
---|
414 | |
---|
415 | if( !( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) { |
---|
416 | /* since we should only have advertised what we can do and the peer should |
---|
417 | * only have chosen what we offered, this should never happen */ |
---|
418 | imcb_log( ic, "WARNING: Received invalid Stream Initiation response, method %s", d->text ); |
---|
419 | |
---|
420 | return XT_HANDLED; |
---|
421 | } |
---|
422 | |
---|
423 | /* Let's see if we can find out what this bytestream should be for... */ |
---|
424 | |
---|
425 | for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) ) |
---|
426 | { |
---|
427 | struct jabber_transfer *tft = tflist->data; |
---|
428 | if( ( strcmp( tft->iq_id, iq_id ) == 0 ) ) |
---|
429 | { |
---|
430 | tf = tft; |
---|
431 | break; |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | if (!tf) |
---|
436 | { |
---|
437 | imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid ); |
---|
438 | return XT_HANDLED; |
---|
439 | } |
---|
440 | |
---|
441 | tf->ini_jid = g_strdup( ini_jid ); |
---|
442 | tf->tgt_jid = g_strdup( tgt_jid ); |
---|
443 | |
---|
444 | imcb_log( ic, "File %s: %s accepted the transfer!", tf->ft->file_name, tgt_jid ); |
---|
445 | |
---|
446 | jabber_bs_send_start( tf ); |
---|
447 | |
---|
448 | return XT_HANDLED; |
---|
449 | } |
---|
450 | |
---|
451 | int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf ) |
---|
452 | { |
---|
453 | struct xt_node *node, *sinode; |
---|
454 | struct jabber_buddy *bud; |
---|
455 | |
---|
456 | /* who knows how many bits the future holds :) */ |
---|
457 | char filesizestr[ 1 + ( int ) ( 0.301029995663981198f * sizeof( size_t ) * 8 ) ]; |
---|
458 | |
---|
459 | const char *methods[] = |
---|
460 | { |
---|
461 | XMLNS_BYTESTREAMS, |
---|
462 | //XMLNS_IBB, |
---|
463 | NULL |
---|
464 | }; |
---|
465 | const char **m; |
---|
466 | char *s; |
---|
467 | |
---|
468 | /* Maybe we should hash this? */ |
---|
469 | tf->sid = g_strdup_printf( "BitlBeeJabberSID%d", tf->ft->local_id ); |
---|
470 | |
---|
471 | if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) ) |
---|
472 | bud = jabber_buddy_by_ext_jid( ic, who, 0 ); |
---|
473 | else |
---|
474 | bud = jabber_buddy_by_jid( ic, who, 0 ); |
---|
475 | |
---|
476 | /* start with the SI tag */ |
---|
477 | sinode = xt_new_node( "si", NULL, NULL ); |
---|
478 | xt_add_attr( sinode, "xmlns", XMLNS_SI ); |
---|
479 | xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER ); |
---|
480 | xt_add_attr( sinode, "id", tf->sid ); |
---|
481 | |
---|
482 | /* if( mimetype ) |
---|
483 | xt_add_attr( node, "mime-type", mimetype ); */ |
---|
484 | |
---|
485 | /* now the file tag */ |
---|
486 | /* if( desc ) |
---|
487 | node = xt_new_node( "desc", descr, NULL ); */ |
---|
488 | node = xt_new_node( "range", NULL, NULL ); |
---|
489 | |
---|
490 | sprintf( filesizestr, "%zd", tf->ft->file_size ); |
---|
491 | node = xt_new_node( "file", NULL, node ); |
---|
492 | xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER ); |
---|
493 | xt_add_attr( node, "name", tf->ft->file_name ); |
---|
494 | xt_add_attr( node, "size", filesizestr ); |
---|
495 | /* if (hash) |
---|
496 | xt_add_attr( node, "hash", hash ); |
---|
497 | if (date) |
---|
498 | xt_add_attr( node, "date", date ); */ |
---|
499 | |
---|
500 | xt_add_child( sinode, node ); |
---|
501 | |
---|
502 | /* and finally the feature tag */ |
---|
503 | node = xt_new_node( "field", NULL, NULL ); |
---|
504 | xt_add_attr( node, "var", "stream-method" ); |
---|
505 | xt_add_attr( node, "type", "list-single" ); |
---|
506 | |
---|
507 | for ( m = methods ; *m ; m ++ ) |
---|
508 | xt_add_child( node, xt_new_node( "option", NULL, xt_new_node( "value", (char *)*m, NULL ) ) ); |
---|
509 | |
---|
510 | node = xt_new_node( "x", NULL, node ); |
---|
511 | xt_add_attr( node, "xmlns", XMLNS_XDATA ); |
---|
512 | xt_add_attr( node, "type", "form" ); |
---|
513 | |
---|
514 | node = xt_new_node( "feature", NULL, node ); |
---|
515 | xt_add_attr( node, "xmlns", XMLNS_FEATURE ); |
---|
516 | |
---|
517 | xt_add_child( sinode, node ); |
---|
518 | |
---|
519 | /* and we are there... */ |
---|
520 | node = jabber_make_packet( "iq", "set", bud ? bud->full_jid : who, sinode ); |
---|
521 | jabber_cache_add( ic, node, jabber_si_handle_response ); |
---|
522 | tf->iq_id = g_strdup( xt_find_attr( node, "id" ) ); |
---|
523 | |
---|
524 | return jabber_write_packet( ic, node ); |
---|
525 | } |
---|