source: protocols/jabber/si.c @ 2c2df7d

Last change on this file since 2c2df7d was 2c2df7d, checked in by ulim <a.sporto+bee@…>, at 2007-11-28T21:07:30Z

Initial import of jabber file receive and DCC send support. This introduces
only a few changes to bitlbees code, mainly the addition of the "transfers"
command.

This is known to work with Kopete, Psi, and Pidgin (formerly known as gaim).
At least with Pidgin also over a proxy. DCC has only been tested with irssi.
IPV6 is untested but should work.

Currently, only receiving via SOCKS5BYTESREAMS is implemented. I'm not sure if
the alternative(in-band bytestreams IBB) is worth implementing since I didn't
see a client yet that can do it. Additionally, it is probably very slow and
needs support by the server as well.

  • Property mode set to 100644
File size: 8.3 KB
Line 
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 );
28
29/* imcb callback */
30void jabber_si_free_transfer( file_transfer_t *ft)
31{
32        struct jabber_transfer *tf = ft->data;
33        struct jabber_data *jd = tf->ic->proto_data;
34
35        if ( tf->watch_in )
36                b_event_remove( tf->watch_in );
37
38        jd->filetransfers = g_slist_remove( jd->filetransfers, tf );
39
40        if( tf->fd )
41        {
42                close( tf->fd );
43                tf->fd = 0;
44        }
45
46        g_free( tf->ini_jid );
47        g_free( tf->tgt_jid );
48        g_free( tf->iq_id );
49        g_free( tf->sid );
50}
51
52/* imcb callback */
53void jabber_si_finished( file_transfer_t *ft )
54{
55        struct jabber_transfer *tf = ft->data;
56
57        imcb_log( tf->ic, "File %s transferred successfully!" , ft->file_name );
58}
59
60/* imcb callback */
61void jabber_si_canceled( file_transfer_t *ft, char *reason )
62{
63        struct jabber_transfer *tf = ft->data;
64        struct xt_node *reply, *iqnode;
65
66        if( tf->accepted )
67                return;
68       
69        iqnode = jabber_make_packet( "iq", "error", tf->ini_jid, NULL );
70        xt_add_attr( iqnode, "id", tf->iq_id );
71        reply = jabber_make_error_packet( iqnode, "forbidden", "cancel", "403" );
72        xt_free_node( iqnode );
73       
74        if( !jabber_write_packet( tf->ic, reply ) )
75                imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
76        xt_free_node( reply );
77
78}
79
80/*
81 * First function that gets called when a file transfer request comes in.
82 * A lot to parse.
83 *
84 * We choose a stream type from the options given by the initiator.
85 * Then we wait for imcb to call the accept or cancel callbacks.
86 */
87int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode)
88{
89        struct xt_node *c, *d, *reply;
90        char *sid, *ini_jid, *tgt_jid, *iq_id, *s, *ext_jid;
91        struct jabber_buddy *bud;
92        int requestok = FALSE;
93        char *name;
94        size_t size;
95        struct jabber_transfer *tf;
96        struct jabber_data *jd = ic->proto_data;
97        file_transfer_t *ft;
98       
99        /* All this means we expect something like this: ( I think )
100         * <iq from=... to=... id=...>
101         *      <si id=id xmlns=si profile=ft>
102         *              <file xmlns=ft/>
103         *              <feature xmlns=feature>
104         *                      <x xmlns=xdata type=submit>
105         *                              <field var=stream-method>
106         *
107         */
108        if( !( ini_jid          = xt_find_attr(   node, "from" )                        ) ||
109            !( tgt_jid          = xt_find_attr(   node, "to" )                          ) ||
110            !( iq_id            = xt_find_attr(   node, "id" )                          ) ||
111            !( sid              = xt_find_attr( sinode, "id" )                          ) ||
112            !( strcmp( xt_find_attr( sinode, "profile" ), XMLNS_FILETRANSFER ) == 0     ) ||
113            !( d                = xt_find_node( sinode->children, "file" )              ) ||
114            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0            ) ||
115            !( name             = xt_find_attr( d, "name" )                             ) ||
116            !( size             = (size_t) atoll( xt_find_attr( d, "size" ) )           ) ||
117            !( d                = xt_find_node( sinode->children, "feature" )           ) ||
118            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0                 ) ||
119            !( d                = xt_find_node( d->children, "x" )                      ) ||
120            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0                   ) ||
121            !( strcmp( xt_find_attr( d, "type" ), "form" ) == 0                         ) ||
122            !( d                = xt_find_node( d->children, "field" )                  ) ||
123            !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0                 ) )
124        {
125                imcb_log( ic, "WARNING: Received incomplete Stream Initiation request" );
126        } else
127        {
128                /* Check if we support one of the options */
129
130                c = d->children;
131                while( ( c = xt_find_node( c, "option" ) ) )
132                        if(     ( d = xt_find_node( c->children, "value" ) ) &&
133                                ( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) )
134                        {
135                                requestok = TRUE;
136                                break;
137                        }
138        }
139       
140        if ( requestok )
141        {
142                /* Figure out who the transfer should come frome... */
143
144                if( ( s = strchr( ini_jid, '/' ) ) )
145                {
146                        if( ( bud = jabber_buddy_by_jid( ic, ini_jid, GET_BUDDY_EXACT ) ) )
147                        {
148                                bud->last_act = time( NULL );
149                                ext_jid = bud->ext_jid ? : bud->bare_jid;
150                        }
151                        else
152                                *s = 0; /* We need to generate a bare JID now. */
153                }
154
155                if( !( ft = imcb_file_send_start( ic, ext_jid, name, size ) ) )
156                { 
157                        imcb_log( ic, "WARNING: Error handling transfer request from %s", ini_jid);
158                        requestok = FALSE;
159                }
160
161                *s = '/';
162        } else 
163                imcb_log( ic, "WARNING: Unsupported file transfer request from %s", ini_jid);
164
165        if ( !requestok )
166        { 
167                reply = jabber_make_error_packet( node, "item-not-found", "cancel", NULL );
168                if (!jabber_write_packet( ic, reply ))
169                        imcb_log( ic, "WARNING: Error generating reply to file transfer request" );
170                xt_free_node( reply );
171                return XT_HANDLED;
172        }
173
174        /* Request is fine. */
175
176        imcb_log( ic, "File transfer request from %s for %s (%zd kb). ", xt_find_attr( node, "from" ), name, size/1024 );
177
178        imcb_log( ic, "Accept the DCC transfer if you'd like the file. If you don't, issue the 'transfers reject' command.");
179
180        tf = g_new0( struct jabber_transfer, 1 );
181
182        tf->ini_jid = g_strdup( ini_jid );
183        tf->tgt_jid = g_strdup( tgt_jid );
184        tf->iq_id = g_strdup( iq_id );
185        tf->sid = g_strdup( sid );
186        tf->ic = ic;
187        tf->ft = ft;
188        tf->ft->data = tf;
189        tf->ft->accept = jabber_si_answer_request;
190        tf->ft->free = jabber_si_free_transfer;
191        tf->ft->finished = jabber_si_finished;
192        tf->ft->canceled = jabber_si_canceled;
193
194        jd->filetransfers = g_slist_prepend( jd->filetransfers, tf );
195
196        return XT_HANDLED;
197}
198
199/*
200 * imcb called the accept callback which probably means that the user accepted this file transfer.
201 * We send our response to the initiator.
202 * In the next step, the initiator will send us a request for the given stream type.
203 * (currently that can only be a SOCKS5 bytestream)
204 */
205void jabber_si_answer_request( file_transfer_t *ft ) {
206        struct jabber_transfer *tf = ft->data;
207        struct xt_node *node, *sinode, *reply;
208
209        /* generate response, start with the SI tag */
210        sinode = xt_new_node( "si", NULL, NULL );
211        xt_add_attr( sinode, "xmlns", XMLNS_SI );
212        xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
213        xt_add_attr( sinode, "id", tf->sid );
214
215        /* now the file tag */
216        node = xt_new_node( "file", NULL, NULL );
217        xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
218
219        xt_add_child( sinode, node );
220
221        /* and finally the feature tag */
222        node = xt_new_node( "field", NULL, NULL );
223        xt_add_attr( node, "var", "stream-method" );
224        xt_add_attr( node, "type", "list-single" );
225
226        /* Currently all we can do. One could also implement in-band (IBB) */
227        xt_add_child( node, xt_new_node( "value", XMLNS_BYTESTREAMS, NULL ) );
228
229        node = xt_new_node( "x", NULL, node );
230        xt_add_attr( node, "xmlns", XMLNS_XDATA );
231        xt_add_attr( node, "type", "submit" );
232
233        node = xt_new_node( "feature", NULL, node );
234        xt_add_attr( node, "xmlns", XMLNS_FEATURE );
235
236        xt_add_child( sinode, node );
237
238        reply = jabber_make_packet( "iq", "result", tf->ini_jid, sinode );
239        xt_add_attr( reply, "id", tf->iq_id );
240       
241        if( !jabber_write_packet( tf->ic, reply ) )
242                imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
243        else
244                tf->accepted = TRUE;
245        xt_free_node( reply );
246}
Note: See TracBrowser for help on using the repository browser.