source: protocols/jabber/conference.c @ 5d7dc00

Last change on this file since 5d7dc00 was 5d7dc00, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-07-02T21:24:04Z

Stripping of unwanted characters (@ and =) in channel nicknames.

  • Property mode set to 100644
File size: 7.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
26struct groupchat *jabber_chat_join( struct im_connection *ic, char *room, char *nick, char *password )
27{
28        struct jabber_chat *jc;
29        struct xt_node *node;
30        struct groupchat *c;
31        char *roomjid;
32       
33        roomjid = g_strdup_printf( "%s/%s", room, nick );
34        node = xt_new_node( "x", NULL, NULL );
35        xt_add_attr( node, "xmlns", XMLNS_MUC );
36        node = jabber_make_packet( "presence", NULL, roomjid, node );
37       
38        if( !jabber_write_packet( ic, node ) )
39        {
40                g_free( roomjid );
41                xt_free_node( node );
42                return NULL;
43        }
44        xt_free_node( node );
45       
46        jc = g_new0( struct jabber_chat, 1 );
47        jc->name = jabber_normalize( room );
48       
49        if( ( jc->me = jabber_buddy_add( ic, roomjid ) ) == NULL )
50        {
51                g_free( roomjid );
52                g_free( jc->name );
53                g_free( jc );
54                return NULL;
55        }
[9c9b37c]56       
57        /* roomjid isn't normalized yet, and we need an original version
58           of the nick to send a proper presence update. */
59        jc->my_full_jid = roomjid;
[e35d1a1]60       
61        c = imcb_chat_new( ic, room );
62        c->data = jc;
63       
64        return c;
65}
66
[43671b9]67int jabber_chat_msg( struct groupchat *c, char *message, int flags )
68{
69        struct im_connection *ic = c->ic;
70        struct jabber_chat *jc = c->data;
71        struct xt_node *node;
72       
73        node = xt_new_node( "body", message, NULL );
74        node = jabber_make_packet( "message", "groupchat", jc->name, node );
75       
76        if( !jabber_write_packet( ic, node ) )
77        {
78                xt_free_node( node );
79                return 0;
80        }
81        xt_free_node( node );
82       
83        return 1;
84}
85
[e35d1a1]86int jabber_chat_leave( struct groupchat *c, const char *reason )
87{
88        struct im_connection *ic = c->ic;
89        struct jabber_chat *jc = c->data;
90        struct xt_node *node;
91       
92        node = xt_new_node( "x", NULL, NULL );
93        xt_add_attr( node, "xmlns", XMLNS_MUC );
[9c9b37c]94        node = jabber_make_packet( "presence", "unavailable", jc->my_full_jid, node );
[e35d1a1]95       
96        if( !jabber_write_packet( ic, node ) )
97        {
98                xt_free_node( node );
99                return 0;
100        }
101        xt_free_node( node );
102       
103        return 1;
104}
105
106/* Not really the same syntax as the normal pkt_ functions, but this isn't
[0e7ab64]107   called by the xmltree parser directly and this way I can add some extra
[e35d1a1]108   parameters so we won't have to repeat too many things done by the caller
109   already. */
110void jabber_chat_pkt_presence( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node )
111{
112        struct groupchat *chat;
113        struct xt_node *c;
114        char *type = xt_find_attr( node, "type" );
115        struct jabber_chat *jc;
116        char *s;
117       
118        if( ( chat = jabber_chat_by_name( ic, bud->bare_jid ) ) == NULL )
119        {
120                /* How could this happen?? We could do kill( self, 11 )
121                   now or just wait for the OS to do it. :-) */
122                return;
123        }
124       
125        jc = chat->data;
126       
127        if( type == NULL && !( bud->flags & JBFLAG_IS_CHATROOM ) )
128        {
129                bud->flags |= JBFLAG_IS_CHATROOM;
130                /* If this one wasn't set yet, this buddy just joined the chat.
131                   Slightly hackish way of finding out eh? ;-) */
132               
[b9f8b87]133                /* This is pretty messy... Here it sets ext_jid to the real
134                   JID of the participant. Works for non-anonymized channels.
135                   Might break if someone joins a chat twice, though. */
[e35d1a1]136                for( c = node->children; ( c = xt_find_node( c, "x" ) ); c = c->next )
137                        if( ( s = xt_find_attr( c, "xmlns" ) ) &&
138                            ( strcmp( s, XMLNS_MUC_USER ) == 0 ) )
139                        {
140                                c = xt_find_node( c->children, "item" );
141                                if( ( s = xt_find_attr( c, "jid" ) ) )
142                                {
143                                        /* Yay, found what we need. :-) */
[b9f8b87]144                                        bud->ext_jid = jabber_normalize( s );
[e35d1a1]145                                        break;
146                                }
147                        }
148               
[6286f80]149                /* Make up some other handle, if necessary. */
150                if( bud->ext_jid == NULL )
151                {
[54f2f55]152                        if( bud == jc->me )
[5d7dc00]153                        {
[b9f8b87]154                                bud->ext_jid = jabber_normalize( ic->acc->user );
[5d7dc00]155                        }
[54f2f55]156                        else
[5d7dc00]157                        {
158                                int i;
159                               
[54f2f55]160                                /* Don't want the nick to be at the end, so let's
161                                   think of some slightly different notation to use
162                                   for anonymous groupchat participants in BitlBee. */
163                                bud->ext_jid = g_strdup_printf( "%s=%s", bud->resource, bud->bare_jid );
[5d7dc00]164                               
165                                /* And strip any unwanted characters. */
166                                for( i = 0; bud->resource[i]; i ++ )
167                                        if( bud->ext_jid[i] == '=' || bud->ext_jid[i] == '@' )
168                                                bud->ext_jid[i] = '_';
169                        }
[6286f80]170                        bud->flags |= JBFLAG_IS_ANONYMOUS;
171                }
[e35d1a1]172               
[c570c86]173                if( bud != jc->me )
174                {
175                        imcb_add_buddy( ic, bud->ext_jid, NULL );
176                        imcb_buddy_nick_hint( ic, bud->ext_jid, bud->resource );
177                }
178               
[6286f80]179                s = strchr( bud->ext_jid, '/' );
[e35d1a1]180                if( s ) *s = 0; /* Should NEVER be NULL, but who knows... */
[6286f80]181                imcb_chat_add_buddy( chat, bud->ext_jid );
[e35d1a1]182                if( s ) *s = '/';
183        }
[998b103]184        else if( type ) /* type can only be NULL or "unavailable" in this function */
[e35d1a1]185        {
[6286f80]186                s = strchr( bud->ext_jid, '/' );
187                if( s ) *s = 0;
188                imcb_chat_remove_buddy( chat, bud->ext_jid, NULL );
[998b103]189                if( bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS )
190                        imcb_remove_buddy( ic, bud->ext_jid, NULL );
[e35d1a1]191                if( s ) *s = '/';
192               
193                if( bud == jc->me )
194                {
[2d317bb]195                        jabber_buddy_remove_bare( ic, jc->name );
196                       
[e35d1a1]197                        g_free( jc->name );
198                        g_free( jc );
199                        imcb_chat_free( chat );
200                }
201        }
202}
203
204void jabber_chat_pkt_message( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node )
205{
206        struct xt_node *body = xt_find_node( node->children, "body" );
207        struct groupchat *chat;
208        char *s;
209       
210        if( bud == NULL )
211        {
212                s = xt_find_attr( node, "from" ); /* pkt_message() already NULL-checked this one. */
213                if( strchr( s, '/' ) == NULL )
214                        /* This is fine, the groupchat itself isn't in jd->buddies. */
215                        imcb_log( ic, "System message from groupchat %s: %s", s, body? body->text : "NULL" );
216                else
217                        /* This, however, isn't fine! */
218                        imcb_log( ic, "Groupchat message from unknown participant %s: %s", s, body ? body->text : "NULL" );
219               
220                return;
221        }
222        else if( ( chat = jabber_chat_by_name( ic, bud->bare_jid ) ) == NULL )
223        {
224                /* How could this happen?? We could do kill( self, 11 )
225                   now or just wait for the OS to do it. :-) */
226                return;
227        }
228       
229        if( body && body->text_len > 0 )
230        {
[6286f80]231                s = strchr( bud->ext_jid, '/' );
[e35d1a1]232                if( s ) *s = 0;
[6286f80]233                imcb_chat_msg( chat, bud->ext_jid, body->text, 0, jabber_get_timestamp( node ) );
[e35d1a1]234                if( s ) *s = '/';
235        }
236}
Note: See TracBrowser for help on using the repository browser.