source: protocols/jabber/conference.c @ 52744f8

Last change on this file since 52744f8 was 13857c6, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-01-13T17:13:45Z

Fixed handing of failed groupchat joins.

  • Property mode set to 100644
File size: 9.8 KB
Line 
1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - Conference rooms                                         *
5*                                                                           *
6*  Copyright 2007 Wilmer van der Gaast <wilmer@gaast.net>                   *
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
26static xt_status jabber_chat_join_failed( struct im_connection *ic, struct xt_node *node, struct xt_node *orig );
27
28struct groupchat *jabber_chat_join( struct im_connection *ic, char *room, char *nick, char *password )
29{
30        struct jabber_chat *jc;
31        struct xt_node *node;
32        struct groupchat *c;
33        char *roomjid;
34       
35        roomjid = g_strdup_printf( "%s/%s", room, nick );
36        node = xt_new_node( "x", NULL, NULL );
37        xt_add_attr( node, "xmlns", XMLNS_MUC );
38        node = jabber_make_packet( "presence", NULL, roomjid, node );
39        jabber_cache_add( ic, node, jabber_chat_join_failed );
40       
41        if( !jabber_write_packet( ic, node ) )
42        {
43                g_free( roomjid );
44                return NULL;
45        }
46       
47        jc = g_new0( struct jabber_chat, 1 );
48        jc->name = jabber_normalize( room );
49       
50        if( ( jc->me = jabber_buddy_add( ic, roomjid ) ) == NULL )
51        {
52                g_free( roomjid );
53                g_free( jc->name );
54                g_free( jc );
55                return NULL;
56        }
57       
58        /* roomjid isn't normalized yet, and we need an original version
59           of the nick to send a proper presence update. */
60        jc->my_full_jid = roomjid;
61       
62        c = imcb_chat_new( ic, room );
63        c->data = jc;
64       
65        return c;
66}
67
68static xt_status jabber_chat_join_failed( struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
69{
70        struct jabber_error *err;
71        struct jabber_buddy *bud;
72        char *room;
73       
74        room = xt_find_attr( orig, "to" );
75        bud = jabber_buddy_by_jid( ic, room, 0 );
76        err = jabber_error_parse( xt_find_node( node->children, "error" ), XMLNS_STANZA_ERROR );
77        if( err )
78        {
79                imcb_error( ic, "Error joining groupchat %s: %s%s%s", room, err->code,
80                            err->text ? ": " : "", err->text ? err->text : "" );
81                jabber_error_free( err );
82        }
83        if( bud )
84                jabber_chat_free( jabber_chat_by_jid( ic, bud->bare_jid ) );
85       
86        return XT_HANDLED;
87}
88
89struct groupchat *jabber_chat_by_jid( struct im_connection *ic, const char *name )
90{
91        char *normalized = jabber_normalize( name );
92        struct groupchat *ret;
93        struct jabber_chat *jc;
94       
95        for( ret = ic->groupchats; ret; ret = ret->next )
96        {
97                jc = ret->data;
98                if( strcmp( normalized, jc->name ) == 0 )
99                        break;
100        }
101        g_free( normalized );
102       
103        return ret;
104}
105
106void jabber_chat_free( struct groupchat *c )
107{
108        struct jabber_chat *jc = c->data;
109       
110        jabber_buddy_remove_bare( c->ic, jc->name );
111       
112        g_free( jc->my_full_jid );
113        g_free( jc->name );
114        g_free( jc );
115       
116        imcb_chat_free( c );
117}
118
119int jabber_chat_msg( struct groupchat *c, char *message, int flags )
120{
121        struct im_connection *ic = c->ic;
122        struct jabber_chat *jc = c->data;
123        struct xt_node *node;
124       
125        node = xt_new_node( "body", message, NULL );
126        node = jabber_make_packet( "message", "groupchat", jc->name, node );
127       
128        if( !jabber_write_packet( ic, node ) )
129        {
130                xt_free_node( node );
131                return 0;
132        }
133        xt_free_node( node );
134       
135        return 1;
136}
137
138int jabber_chat_topic( struct groupchat *c, char *topic )
139{
140        struct im_connection *ic = c->ic;
141        struct jabber_chat *jc = c->data;
142        struct xt_node *node;
143       
144        node = xt_new_node( "subject", topic, NULL );
145        node = jabber_make_packet( "message", "groupchat", jc->name, node );
146       
147        if( !jabber_write_packet( ic, node ) )
148        {
149                xt_free_node( node );
150                return 0;
151        }
152        xt_free_node( node );
153       
154        return 1;
155}
156
157int jabber_chat_leave( struct groupchat *c, const char *reason )
158{
159        struct im_connection *ic = c->ic;
160        struct jabber_chat *jc = c->data;
161        struct xt_node *node;
162       
163        node = xt_new_node( "x", NULL, NULL );
164        xt_add_attr( node, "xmlns", XMLNS_MUC );
165        node = jabber_make_packet( "presence", "unavailable", jc->my_full_jid, node );
166       
167        if( !jabber_write_packet( ic, node ) )
168        {
169                xt_free_node( node );
170                return 0;
171        }
172        xt_free_node( node );
173       
174        return 1;
175}
176
177void jabber_chat_invite( struct groupchat *c, char *who, char *message )
178{
179        struct xt_node *node;
180        struct im_connection *ic = c->ic;
181        struct jabber_chat *jc = c->data;
182
183        node = xt_new_node( "reason", message, NULL ); 
184
185        node = xt_new_node( "invite", NULL, node );
186        xt_add_attr( node, "to", who ); 
187
188        node = xt_new_node( "x", NULL, node ); 
189        xt_add_attr( node, "xmlns", XMLNS_MUC_USER ); 
190       
191        node = jabber_make_packet( "message", NULL, jc->name, node ); 
192
193        jabber_write_packet( ic, node ); 
194
195        xt_free_node( node );
196}
197
198/* Not really the same syntax as the normal pkt_ functions, but this isn't
199   called by the xmltree parser directly and this way I can add some extra
200   parameters so we won't have to repeat too many things done by the caller
201   already. */
202void jabber_chat_pkt_presence( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node )
203{
204        struct groupchat *chat;
205        struct xt_node *c;
206        char *type = xt_find_attr( node, "type" );
207        struct jabber_chat *jc;
208        char *s;
209       
210        if( ( chat = jabber_chat_by_jid( ic, bud->bare_jid ) ) == NULL )
211        {
212                /* How could this happen?? We could do kill( self, 11 )
213                   now or just wait for the OS to do it. :-) */
214                return;
215        }
216       
217        jc = chat->data;
218       
219        if( type == NULL && !( bud->flags & JBFLAG_IS_CHATROOM ) )
220        {
221                bud->flags |= JBFLAG_IS_CHATROOM;
222                /* If this one wasn't set yet, this buddy just joined the chat.
223                   Slightly hackish way of finding out eh? ;-) */
224               
225                /* This is pretty messy... Here it sets ext_jid to the real
226                   JID of the participant. Works for non-anonymized channels.
227                   Might break if someone joins a chat twice, though. */
228                for( c = node->children; ( c = xt_find_node( c, "x" ) ); c = c->next )
229                        if( ( s = xt_find_attr( c, "xmlns" ) ) &&
230                            ( strcmp( s, XMLNS_MUC_USER ) == 0 ) )
231                        {
232                                c = xt_find_node( c->children, "item" );
233                                if( ( s = xt_find_attr( c, "jid" ) ) )
234                                {
235                                        /* Yay, found what we need. :-) */
236                                        bud->ext_jid = jabber_normalize( s );
237                                        break;
238                                }
239                        }
240               
241                /* Make up some other handle, if necessary. */
242                if( bud->ext_jid == NULL )
243                {
244                        if( bud == jc->me )
245                        {
246                                bud->ext_jid = jabber_normalize( ic->acc->user );
247                        }
248                        else
249                        {
250                                int i;
251                               
252                                /* Don't want the nick to be at the end, so let's
253                                   think of some slightly different notation to use
254                                   for anonymous groupchat participants in BitlBee. */
255                                bud->ext_jid = g_strdup_printf( "%s=%s", bud->resource, bud->bare_jid );
256                               
257                                /* And strip any unwanted characters. */
258                                for( i = 0; bud->resource[i]; i ++ )
259                                        if( bud->ext_jid[i] == '=' || bud->ext_jid[i] == '@' )
260                                                bud->ext_jid[i] = '_';
261                               
262                                /* Some program-specific restrictions. */
263                                imcb_clean_handle( ic, bud->ext_jid );
264                        }
265                        bud->flags |= JBFLAG_IS_ANONYMOUS;
266                }
267               
268                if( bud != jc->me )
269                {
270                        imcb_add_buddy( ic, bud->ext_jid, NULL );
271                        imcb_buddy_nick_hint( ic, bud->ext_jid, bud->resource );
272                }
273               
274                s = strchr( bud->ext_jid, '/' );
275                if( s ) *s = 0; /* Should NEVER be NULL, but who knows... */
276                imcb_chat_add_buddy( chat, bud->ext_jid );
277                if( s ) *s = '/';
278        }
279        else if( type ) /* type can only be NULL or "unavailable" in this function */
280        {
281                s = strchr( bud->ext_jid, '/' );
282                if( s ) *s = 0;
283                imcb_chat_remove_buddy( chat, bud->ext_jid, NULL );
284                if( bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS )
285                        imcb_remove_buddy( ic, bud->ext_jid, NULL );
286                if( s ) *s = '/';
287               
288                if( bud == jc->me )
289                        jabber_chat_free( chat );
290        }
291}
292
293void jabber_chat_pkt_message( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node )
294{
295        struct xt_node *subject = xt_find_node( node->children, "subject" );
296        struct xt_node *body = xt_find_node( node->children, "body" );
297        struct groupchat *chat;
298        char *s;
299       
300        if( bud == NULL )
301        {
302                s = xt_find_attr( node, "from" ); /* pkt_message() already NULL-checked this one. */
303                if( strchr( s, '/' ) == NULL )
304                        /* This is fine, the groupchat itself isn't in jd->buddies. */
305                        imcb_log( ic, "System message from groupchat %s: %s", s, body? body->text : "NULL" );
306                else
307                        /* This, however, isn't fine! */
308                        imcb_log( ic, "Groupchat message from unknown participant %s: %s", s, body ? body->text : "NULL" );
309               
310                return;
311        }
312        else if( ( chat = jabber_chat_by_jid( ic, bud->bare_jid ) ) == NULL )
313        {
314                /* How could this happen?? We could do kill( self, 11 )
315                   now or just wait for the OS to do it. :-) */
316                return;
317        }
318       
319        if( subject )
320        {
321                s = strchr( bud->ext_jid, '/' );
322                if( s ) *s = 0;
323                imcb_chat_topic( chat, bud->ext_jid, subject->text_len > 0 ?
324                                 subject->text : NULL, jabber_get_timestamp( node ) );
325                if( s ) *s = '/';
326        }
327        if( body && body->text_len > 0 )
328        {
329                s = strchr( bud->ext_jid, '/' );
330                if( s ) *s = 0;
331                imcb_chat_msg( chat, bud->ext_jid, body->text, 0, jabber_get_timestamp( node ) );
332                if( s ) *s = '/';
333        }
334}
Note: See TracBrowser for help on using the repository browser.