source: irc_channel.c @ 5a673f3

Last change on this file since 5a673f3 was 47fae0f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-09T12:05:50Z

No. Run the part handler from the IRC /PART command, not from irc_channel.c.
This was causing troubles with Twitter at disconnect time.

  • Property mode set to 100644
File size: 6.6 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* The IRC-based UI - Representing (virtual) channels.                  */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#include "bitlbee.h"
27
28static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ );
29static const struct irc_channel_funcs control_channel_funcs;
30static const struct irc_channel_funcs groupchat_stub_funcs;
31
32irc_channel_t *irc_channel_new( irc_t *irc, const char *name )
33{
34        irc_channel_t *ic;
35       
36        if( !irc_channel_name_ok( name ) || irc_channel_by_name( irc, name ) )
37                return NULL;
38       
39        ic = g_new0( irc_channel_t, 1 );
40        ic->irc = irc;
41        ic->name = g_strdup( name );
42        strcpy( ic->mode, CMODE );
43       
44        irc_channel_add_user( ic, irc->root );
45        if( strcmp( set_getstr( &irc->b->set, "ops" ), "both" ) == 0 ||
46            strcmp( set_getstr( &irc->b->set, "ops" ), "root" ) == 0 )
47                irc_channel_user_set_mode( ic, irc->root, IRC_CHANNEL_USER_OP );
48       
49        irc->channels = g_slist_prepend( irc->channels, ic );
50       
51        if( name[0] == '&' )
52                ic->f = &control_channel_funcs;
53        else /* if( name[0] == '#' ) */
54                ic->f = &groupchat_stub_funcs;
55       
56        return ic;
57}
58
59irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name )
60{
61        GSList *l;
62       
63        for( l = irc->channels; l; l = l->next )
64        {
65                irc_channel_t *ic = l->data;
66               
67                if( name[0] == ic->name[0] && nick_cmp( name + 1, ic->name + 1 ) == 0 )
68                        return ic;
69        }
70       
71        return NULL;
72}
73
74int irc_channel_free( irc_channel_t *ic )
75{
76        irc_t *irc = ic->irc;
77       
78        if( ic->flags & IRC_CHANNEL_JOINED )
79                irc_channel_del_user( ic, irc->user );
80       
81        irc->channels = g_slist_remove( irc->channels, ic );
82        while( ic->users )
83        {
84                g_free( ic->users->data );
85                ic->users = g_slist_remove( ic->users, ic->users->data );
86        }
87       
88        g_free( ic->name );
89        g_free( ic->topic );
90        g_free( ic );
91       
92        return 1;
93}
94
95int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu )
96{
97        irc_channel_user_t *icu;
98       
99        if( irc_channel_has_user( ic, iu ) )
100                return 0;
101       
102        icu = g_new0( irc_channel_user_t, 1 );
103        icu->iu = iu;
104       
105        ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp );
106       
107        if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED )
108        {
109                ic->flags |= IRC_CHANNEL_JOINED;
110                irc_send_join( ic, iu );
111        }
112       
113        return 1;
114}
115
116int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu )
117{
118        irc_channel_user_t *icu;
119       
120        if( !( icu = irc_channel_has_user( ic, iu ) ) )
121                return 0;
122       
123        ic->users = g_slist_remove( ic->users, icu );
124        g_free( icu );
125       
126        if( ic->flags & IRC_CHANNEL_JOINED )
127                irc_send_part( ic, iu, "" );
128       
129        if( iu == ic->irc->user )
130                ic->flags &= ~IRC_CHANNEL_JOINED;
131       
132        return 1;
133}
134
135irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu )
136{
137        GSList *l;
138       
139        for( l = ic->users; l; l = l->next )
140        {
141                irc_channel_user_t *icu = l->data;
142               
143                if( icu->iu == iu )
144                        return icu;
145        }
146       
147        return NULL;
148}
149
150int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu )
151{
152        g_free( ic->topic );
153        ic->topic = g_strdup( topic );
154       
155        g_free( ic->topic_who );
156        if( iu )
157                ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host );
158        else
159                ic->topic_who = NULL;
160       
161        ic->topic_time = time( NULL );
162       
163        if( ic->flags & IRC_CHANNEL_JOINED )
164                irc_send_topic( ic, TRUE );
165       
166        return 1;
167}
168
169void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags )
170{
171        irc_channel_user_t *icu = irc_channel_has_user( ic, iu );
172       
173        if( icu->flags == flags )
174                return;
175       
176        if( ic->flags & IRC_CHANNEL_JOINED )
177                irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags );
178       
179        icu->flags = flags;
180}
181
182void irc_channel_printf( irc_channel_t *ic, char *format, ... )
183{
184        va_list params;
185        char *text;
186       
187        va_start( params, format );
188        text = g_strdup_vprintf( format, params );
189        va_end( params );
190       
191        irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL );
192        g_free( text );
193}
194
195gboolean irc_channel_name_ok( const char *name )
196{
197        return strchr( CTYPES, name[0] ) != NULL && nick_ok( name + 1 );
198}
199
200static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ )
201{
202        const irc_channel_user_t *a = a_, *b = b_;
203       
204        return irc_user_cmp( a->iu, b->iu );
205}
206
207/* Channel-type dependent functions, for control channels: */
208static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg )
209{
210        irc_t *irc = ic->irc;
211        const char *s;
212       
213        /* Scan for non-whitespace chars followed by a colon: */
214        for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {}
215       
216        if( *s == ':' || *s == ',' )
217        {
218                char to[s-msg+1];
219                irc_user_t *iu;
220               
221                memset( to, 0, sizeof( to ) );
222                strncpy( to, msg, s - msg );
223                while( *(++s) && isspace( *s ) ) {}
224               
225                iu = irc_user_by_name( irc, to );
226                if( iu && iu->f->privmsg )
227                {
228                        iu->flags &= ~IRC_USER_PRIVATE;
229                        iu->f->privmsg( iu, s );
230                }
231                else
232                {
233                        irc_channel_printf( ic, "User does not exist: %s", to );
234                }
235        }
236        else
237        {
238                /* TODO: Maybe just use root->privmsg here now? */
239                char cmd[strlen(msg)+1];
240               
241                g_free( ic->irc->last_root_cmd );
242                ic->irc->last_root_cmd = g_strdup( ic->name );
243               
244                strcpy( cmd, msg );
245                root_command_string( ic->irc, cmd );
246        }
247       
248        return TRUE;
249}
250
251static const struct irc_channel_funcs control_channel_funcs = {
252        control_channel_privmsg,
253};
254
255/* Groupchat stub: Only handles /INVITE at least for now. */
256static gboolean groupchat_stub_invite( irc_channel_t *ic, irc_user_t *iu )
257{
258        bee_user_t *bu = iu->bu;
259       
260        if( iu->bu->ic->acc->prpl->chat_with )
261        {
262                ic->flags |= IRC_CHANNEL_CHAT_PICKME;
263                iu->bu->ic->acc->prpl->chat_with( bu->ic, bu->handle );
264                ic->flags &= ~IRC_CHANNEL_CHAT_PICKME;
265                return TRUE;
266        }
267        else
268        {
269                irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name );
270                return FALSE;
271        }
272}
273
274static const struct irc_channel_funcs groupchat_stub_funcs = {
275        NULL,
276        NULL,
277        NULL,
278        NULL,
279        groupchat_stub_invite,
280};
Note: See TracBrowser for help on using the repository browser.