source: protocols/jabber/si.c @ dce3903

Last change on this file since dce3903 was dce3903, checked in by ulim <a.sporto+bee@…>, at 2007-12-04T00:48:57Z

Send and receive seems to work now! Also adopted the new buffering strategy,
only one buffer of 2k per transfer now.

  • Property mode set to 100644
File size: 13.9 KB
RevLine 
[2c2df7d]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
27void jabber_si_answer_request( file_transfer_t *ft );
[2ff2076]28int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf );
[2c2df7d]29
[2ff2076]30/* file_transfer free() callback */
[2c2df7d]31void 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        g_free( tf->ini_jid );
48        g_free( tf->tgt_jid );
49        g_free( tf->iq_id );
50        g_free( tf->sid );
51}
52
[2ff2076]53/* file_transfer finished() callback */
[2c2df7d]54void jabber_si_finished( file_transfer_t *ft )
55{
56        struct jabber_transfer *tf = ft->data;
57
58        imcb_log( tf->ic, "File %s transferred successfully!" , ft->file_name );
59}
60
[2ff2076]61/* file_transfer canceled() callback */
[2c2df7d]62void jabber_si_canceled( file_transfer_t *ft, char *reason )
63{
64        struct jabber_transfer *tf = ft->data;
65        struct xt_node *reply, *iqnode;
66
67        if( tf->accepted )
68                return;
69       
70        iqnode = jabber_make_packet( "iq", "error", tf->ini_jid, NULL );
71        xt_add_attr( iqnode, "id", tf->iq_id );
72        reply = jabber_make_error_packet( iqnode, "forbidden", "cancel", "403" );
73        xt_free_node( iqnode );
74       
75        if( !jabber_write_packet( tf->ic, reply ) )
76                imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
77        xt_free_node( reply );
78
79}
80
[2ff2076]81void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who ) 
82{
83        struct jabber_transfer *tf;
84        struct jabber_data *jd = ic->proto_data;
85
[dce3903]86        imcb_log( ic, "Trying to send %s(%zd bytes) to %s", ft->file_name, ft->file_size, who );
[2ff2076]87
88        tf = g_new0( struct jabber_transfer, 1 );
89
90        tf->ic = ic;
91        tf->ft = ft;
92        tf->ft->data = tf;
93        tf->ft->free = jabber_si_free_transfer;
94        tf->ft->finished = jabber_si_finished;
95        ft->write = jabber_bs_send_write;
96
97        jd->filetransfers = g_slist_prepend( jd->filetransfers, tf );
98
99        jabber_si_send_request( ic, who, tf );
100
101        imcb_file_recv_start( ft );
102}
103
[2c2df7d]104/*
105 * First function that gets called when a file transfer request comes in.
106 * A lot to parse.
107 *
108 * We choose a stream type from the options given by the initiator.
109 * Then we wait for imcb to call the accept or cancel callbacks.
110 */
111int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode)
112{
113        struct xt_node *c, *d, *reply;
114        char *sid, *ini_jid, *tgt_jid, *iq_id, *s, *ext_jid;
115        struct jabber_buddy *bud;
116        int requestok = FALSE;
117        char *name;
118        size_t size;
119        struct jabber_transfer *tf;
120        struct jabber_data *jd = ic->proto_data;
121        file_transfer_t *ft;
122       
123        /* All this means we expect something like this: ( I think )
124         * <iq from=... to=... id=...>
125         *      <si id=id xmlns=si profile=ft>
126         *              <file xmlns=ft/>
127         *              <feature xmlns=feature>
128         *                      <x xmlns=xdata type=submit>
129         *                              <field var=stream-method>
130         *
131         */
132        if( !( ini_jid          = xt_find_attr(   node, "from" )                        ) ||
133            !( tgt_jid          = xt_find_attr(   node, "to" )                          ) ||
134            !( iq_id            = xt_find_attr(   node, "id" )                          ) ||
135            !( sid              = xt_find_attr( sinode, "id" )                          ) ||
136            !( strcmp( xt_find_attr( sinode, "profile" ), XMLNS_FILETRANSFER ) == 0     ) ||
137            !( d                = xt_find_node( sinode->children, "file" )              ) ||
138            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0            ) ||
139            !( name             = xt_find_attr( d, "name" )                             ) ||
140            !( size             = (size_t) atoll( xt_find_attr( d, "size" ) )           ) ||
141            !( d                = xt_find_node( sinode->children, "feature" )           ) ||
142            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0                 ) ||
143            !( d                = xt_find_node( d->children, "x" )                      ) ||
144            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0                   ) ||
145            !( strcmp( xt_find_attr( d, "type" ), "form" ) == 0                         ) ||
146            !( d                = xt_find_node( d->children, "field" )                  ) ||
147            !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0                 ) )
148        {
149                imcb_log( ic, "WARNING: Received incomplete Stream Initiation request" );
150        } else
151        {
152                /* Check if we support one of the options */
153
154                c = d->children;
155                while( ( c = xt_find_node( c, "option" ) ) )
156                        if(     ( d = xt_find_node( c->children, "value" ) ) &&
157                                ( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) )
158                        {
159                                requestok = TRUE;
160                                break;
161                        }
[2ff2076]162
163                if ( !requestok )
164                        imcb_log( ic, "WARNING: Unsupported file transfer request from %s", ini_jid);
[2c2df7d]165        }
166       
167        if ( requestok )
168        {
169                /* Figure out who the transfer should come frome... */
170
171                if( ( s = strchr( ini_jid, '/' ) ) )
172                {
173                        if( ( bud = jabber_buddy_by_jid( ic, ini_jid, GET_BUDDY_EXACT ) ) )
174                        {
175                                bud->last_act = time( NULL );
176                                ext_jid = bud->ext_jid ? : bud->bare_jid;
177                        }
178                        else
179                                *s = 0; /* We need to generate a bare JID now. */
180                }
181
182                if( !( ft = imcb_file_send_start( ic, ext_jid, name, size ) ) )
183                { 
184                        imcb_log( ic, "WARNING: Error handling transfer request from %s", ini_jid);
185                        requestok = FALSE;
186                }
187
188                *s = '/';
[2ff2076]189        }
[2c2df7d]190
191        if ( !requestok )
192        { 
193                reply = jabber_make_error_packet( node, "item-not-found", "cancel", NULL );
194                if (!jabber_write_packet( ic, reply ))
195                        imcb_log( ic, "WARNING: Error generating reply to file transfer request" );
196                xt_free_node( reply );
197                return XT_HANDLED;
198        }
199
200        /* Request is fine. */
201
202        imcb_log( ic, "File transfer request from %s for %s (%zd kb). ", xt_find_attr( node, "from" ), name, size/1024 );
203
204        imcb_log( ic, "Accept the DCC transfer if you'd like the file. If you don't, issue the 'transfers reject' command.");
205
206        tf = g_new0( struct jabber_transfer, 1 );
207
208        tf->ini_jid = g_strdup( ini_jid );
209        tf->tgt_jid = g_strdup( tgt_jid );
210        tf->iq_id = g_strdup( iq_id );
211        tf->sid = g_strdup( sid );
212        tf->ic = ic;
213        tf->ft = ft;
214        tf->ft->data = tf;
215        tf->ft->accept = jabber_si_answer_request;
216        tf->ft->free = jabber_si_free_transfer;
217        tf->ft->finished = jabber_si_finished;
218        tf->ft->canceled = jabber_si_canceled;
219
220        jd->filetransfers = g_slist_prepend( jd->filetransfers, tf );
221
222        return XT_HANDLED;
223}
224
225/*
[dce3903]226 * imc called the accept callback which probably means that the user accepted this file transfer.
[2c2df7d]227 * We send our response to the initiator.
228 * In the next step, the initiator will send us a request for the given stream type.
229 * (currently that can only be a SOCKS5 bytestream)
230 */
231void jabber_si_answer_request( file_transfer_t *ft ) {
232        struct jabber_transfer *tf = ft->data;
233        struct xt_node *node, *sinode, *reply;
234
235        /* generate response, start with the SI tag */
236        sinode = xt_new_node( "si", NULL, NULL );
237        xt_add_attr( sinode, "xmlns", XMLNS_SI );
238        xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
239        xt_add_attr( sinode, "id", tf->sid );
240
241        /* now the file tag */
242        node = xt_new_node( "file", NULL, NULL );
243        xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
244
245        xt_add_child( sinode, node );
246
247        /* and finally the feature tag */
248        node = xt_new_node( "field", NULL, NULL );
249        xt_add_attr( node, "var", "stream-method" );
250        xt_add_attr( node, "type", "list-single" );
251
252        /* Currently all we can do. One could also implement in-band (IBB) */
253        xt_add_child( node, xt_new_node( "value", XMLNS_BYTESTREAMS, NULL ) );
254
255        node = xt_new_node( "x", NULL, node );
256        xt_add_attr( node, "xmlns", XMLNS_XDATA );
257        xt_add_attr( node, "type", "submit" );
258
259        node = xt_new_node( "feature", NULL, node );
260        xt_add_attr( node, "xmlns", XMLNS_FEATURE );
261
262        xt_add_child( sinode, node );
263
264        reply = jabber_make_packet( "iq", "result", tf->ini_jid, sinode );
265        xt_add_attr( reply, "id", tf->iq_id );
266       
267        if( !jabber_write_packet( tf->ic, reply ) )
268                imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
269        else
270                tf->accepted = TRUE;
271        xt_free_node( reply );
272}
[2ff2076]273
274static xt_status jabber_si_handle_response(struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
275{
276        struct xt_node *c, *d;
[dce3903]277        char *ini_jid, *tgt_jid, *iq_id;
[2ff2076]278        GSList *tflist;
279        struct jabber_transfer *tf=NULL;
280        struct jabber_data *jd = ic->proto_data;
281
282        if( !( tgt_jid = xt_find_attr( node, "from" ) ) ||
283            !( ini_jid = xt_find_attr( node, "to" ) ) )
284        {
285                imcb_log( ic, "Invalid SI response from=%s to=%s", tgt_jid, ini_jid );
286                return XT_HANDLED;
287        }
288       
289        /* All this means we expect something like this: ( I think )
[dce3903]290         * <iq from=... to=... id=...>
[2ff2076]291         *      <si xmlns=si>
[dce3903]292         *      [       <file xmlns=ft/>    ] <-- not neccessary
[2ff2076]293         *              <feature xmlns=feature>
294         *                      <x xmlns=xdata type=submit>
295         *                              <field var=stream-method>
296         *                                      <value>
297         */
298        if( !( tgt_jid = xt_find_attr( node, "from" ) ) ||
299            !( ini_jid = xt_find_attr( node, "to" ) ) ||
[dce3903]300            !( iq_id   = xt_find_attr( node, "id" ) ) ||
[2ff2076]301            !( c = xt_find_node( node->children, "si" ) ) ||
302            !( strcmp( xt_find_attr( c, "xmlns" ), XMLNS_SI ) == 0 ) ||
[dce3903]303/*          !( d = xt_find_node( c->children, "file" ) ) ||
304            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0 ) || */
[2ff2076]305            !( d = xt_find_node( c->children, "feature" ) ) ||
306            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0 ) ||
307            !( d = xt_find_node( d->children, "x" ) ) ||
308            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0 ) ||
309            !( strcmp( xt_find_attr( d, "type" ), "submit" ) == 0 ) ||
310            !( d = xt_find_node( d->children, "field" ) ) ||
311            !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0 ) ||
312            !( d = xt_find_node( d->children, "value" ) ) )
313        {
314                imcb_log( ic, "WARNING: Received incomplete Stream Initiation response" );
315                return XT_HANDLED;
316        }
317
318        if( !( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) { 
319                /* since we should only have advertised what we can do and the peer should
320                 * only have chosen what we offered, this should never happen */
321                imcb_log( ic, "WARNING: Received invalid Stream Initiation response, method %s", d->text );
322                       
323                return XT_HANDLED;
324        }
325       
326        /* Let's see if we can find out what this bytestream should be for... */
327
328        for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) )
329        {
330                struct jabber_transfer *tft = tflist->data;
[dce3903]331                if( ( strcmp( tft->iq_id, iq_id ) == 0 ) )
[2ff2076]332                {
333                        tf = tft;
334                        break;
335                }
336        }
337
338        if (!tf) 
339        {
340                imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid );
341                return XT_HANDLED;
342        }
343
344        tf->ini_jid = g_strdup( ini_jid );
345        tf->tgt_jid = g_strdup( tgt_jid );
346
[dce3903]347        imcb_log( ic, "File %s: %s accepted the transfer!", tf->ft->file_name, tgt_jid );
348
[2ff2076]349        jabber_bs_send_start( tf );
350
351        return XT_HANDLED;
352}
353
354int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf )
355{
356        struct xt_node *node, *sinode;
357        struct jabber_buddy *bud;
358
359        /* who knows how many bits the future holds :) */
360        char filesizestr[ 1 + ( int ) ( 0.301029995663981198f * sizeof( size_t ) * 8 ) ];
361
362        const char *methods[] = 
363        {       
364                XMLNS_BYTESTREAMS,
365                //XMLNS_IBB,
366                NULL 
367        };
368        const char **m;
369        char *s;
370
371        /* Maybe we should hash this? */
372        tf->sid = g_strdup_printf( "BitlBeeJabberSID%d", tf->ft->local_id );
373       
374        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_name( ic, s + 1 ) )
375                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
376        else
377                bud = jabber_buddy_by_jid( ic, who, 0 );
378
379        /* start with the SI tag */
380        sinode = xt_new_node( "si", NULL, NULL );
381        xt_add_attr( sinode, "xmlns", XMLNS_SI );
382        xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
383        xt_add_attr( sinode, "id", tf->sid );
384
385/*      if( mimetype )
386                xt_add_attr( node, "mime-type", mimetype ); */
387
388        /* now the file tag */
389/*      if( desc )
390                node = xt_new_node( "desc", descr, NULL ); */
391        node = xt_new_node( "range", NULL, NULL );
392
393        sprintf( filesizestr, "%zd", tf->ft->file_size );
394        node = xt_new_node( "file", NULL, node );
395        xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
396        xt_add_attr( node, "name", tf->ft->file_name );
397        xt_add_attr( node, "size", filesizestr );
398/*      if (hash)
399                xt_add_attr( node, "hash", hash );
400        if (date)
401                xt_add_attr( node, "date", date ); */
402
403        xt_add_child( sinode, node );
404
405        /* and finally the feature tag */
406        node = xt_new_node( "field", NULL, NULL );
407        xt_add_attr( node, "var", "stream-method" );
408        xt_add_attr( node, "type", "list-single" );
409
410        for ( m = methods ; *m ; m ++ )
411                xt_add_child( node, xt_new_node( "option", NULL, xt_new_node( "value", (char *)*m, NULL ) ) );
412
413        node = xt_new_node( "x", NULL, node );
414        xt_add_attr( node, "xmlns", XMLNS_XDATA );
415        xt_add_attr( node, "type", "form" );
416
417        node = xt_new_node( "feature", NULL, node );
418        xt_add_attr( node, "xmlns", XMLNS_FEATURE );
419
420        xt_add_child( sinode, node );
421
422        /* and we are there... */
423        node = jabber_make_packet( "iq", "set", bud ? bud->full_jid : who, sinode );
424        jabber_cache_add( ic, node, jabber_si_handle_response );
[dce3903]425        tf->iq_id = g_strdup( xt_find_attr( node, "id" ) );
[2ff2076]426       
427        return jabber_write_packet( ic, node );
428}
Note: See TracBrowser for help on using the repository browser.