source: protocols/jabber/conference.c @ 80e9db9

Last change on this file since 80e9db9 was 80e9db9, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-12-02T23:14:49Z

Forgot to return something in jabber_chat_join_failed().

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