source: irc_channel.c @ b0364dc

Last change on this file since b0364dc was b0364dc, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-08T13:21:36Z

Also allow addressing people inside the control channel using a comma.

  • Property mode set to 100644
File size: 6.0 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;
30
31irc_channel_t *irc_channel_new( irc_t *irc, const char *name )
32{
33        irc_channel_t *ic;
34       
35        if( !irc_channel_name_ok( name ) || irc_channel_by_name( irc, name ) )
36                return NULL;
37       
38        ic = g_new0( irc_channel_t, 1 );
39        ic->f = &control_channel_funcs;
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        return ic;
52}
53
54irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name )
55{
56        GSList *l;
57       
58        for( l = irc->channels; l; l = l->next )
59        {
60                irc_channel_t *ic = l->data;
61               
62                if( name[0] == ic->name[0] && nick_cmp( name + 1, ic->name + 1 ) == 0 )
63                        return ic;
64        }
65       
66        return NULL;
67}
68
69int irc_channel_free( irc_channel_t *ic )
70{
71        irc_t *irc = ic->irc;
72       
73        if( ic->flags & IRC_CHANNEL_JOINED )
74                irc_channel_del_user( ic, irc->user );
75       
76        irc->channels = g_slist_remove( irc->channels, ic );
77        while( ic->users )
78        {
79                g_free( ic->users->data );
80                ic->users = g_slist_remove( ic->users, ic->users->data );
81        }
82       
83        g_free( ic->name );
84        g_free( ic->topic );
85        g_free( ic );
86       
87        return 1;
88}
89
90int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu )
91{
92        irc_channel_user_t *icu;
93       
94        if( irc_channel_has_user( ic, iu ) )
95                return 0;
96       
97        icu = g_new0( irc_channel_user_t, 1 );
98        icu->iu = iu;
99       
100        ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp );
101       
102        if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED )
103        {
104                ic->flags |= IRC_CHANNEL_JOINED;
105                irc_send_join( ic, iu );
106        }
107       
108        return 1;
109}
110
111int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu )
112{
113        irc_channel_user_t *icu;
114       
115        if( !( icu = irc_channel_has_user( ic, iu ) ) )
116                return 0;
117       
118        ic->users = g_slist_remove( ic->users, icu );
119        g_free( icu );
120       
121        if( ic->flags & IRC_CHANNEL_JOINED )
122                irc_send_part( ic, iu, "" );
123       
124        if( iu == ic->irc->user )
125        {
126                ic->flags &= ~IRC_CHANNEL_JOINED;
127                if( ic->f->part )
128                        ic->f->part( ic, NULL );
129        }
130       
131        return 1;
132}
133
134irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu )
135{
136        GSList *l;
137       
138        for( l = ic->users; l; l = l->next )
139        {
140                irc_channel_user_t *icu = l->data;
141               
142                if( icu->iu == iu )
143                        return icu;
144        }
145       
146        return NULL;
147}
148
149int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu )
150{
151        g_free( ic->topic );
152        ic->topic = g_strdup( topic );
153       
154        g_free( ic->topic_who );
155        if( iu )
156                ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host );
157        else
158                ic->topic_who = NULL;
159       
160        ic->topic_time = time( NULL );
161       
162        if( ic->flags & IRC_CHANNEL_JOINED )
163                irc_send_topic( ic, TRUE );
164       
165        return 1;
166}
167
168void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags )
169{
170        irc_channel_user_t *icu = irc_channel_has_user( ic, iu );
171       
172        if( icu->flags == flags )
173                return;
174       
175        if( ic->flags & IRC_CHANNEL_JOINED )
176                irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags );
177       
178        icu->flags = flags;
179}
180
181void irc_channel_printf( irc_channel_t *ic, char *format, ... )
182{
183        va_list params;
184        char *text;
185       
186        va_start( params, format );
187        text = g_strdup_vprintf( format, params );
188        va_end( params );
189       
190        irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL );
191        g_free( text );
192}
193
194gboolean irc_channel_name_ok( const char *name )
195{
196        return strchr( CTYPES, name[0] ) != NULL && nick_ok( name + 1 );
197}
198
199static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ )
200{
201        const irc_channel_user_t *a = a_, *b = b_;
202       
203        return irc_user_cmp( a->iu, b->iu );
204}
205
206/* Channel-type dependent functions, for control channels: */
207static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg )
208{
209        irc_t *irc = ic->irc;
210        const char *s;
211       
212        /* Scan for non-whitespace chars followed by a colon: */
213        for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {}
214       
215        if( *s == ':' || *s == ',' )
216        {
217                char to[s-msg+1];
218                irc_user_t *iu;
219               
220                memset( to, 0, sizeof( to ) );
221                strncpy( to, msg, s - msg );
222                while( *(++s) && isspace( *s ) ) {}
223               
224                iu = irc_user_by_name( irc, to );
225                if( iu && iu->f->privmsg )
226                {
227                        iu->flags &= ~IRC_USER_PRIVATE;
228                        iu->f->privmsg( iu, s );
229                }
230                else
231                {
232                        irc_channel_printf( ic, "User does not exist: %s", to );
233                }
234        }
235        else
236        {
237                /* TODO: Maybe just use root->privmsg here now? */
238                char cmd[strlen(msg)+1];
239               
240                g_free( ic->irc->last_root_cmd );
241                ic->irc->last_root_cmd = g_strdup( ic->name );
242               
243                strcpy( cmd, msg );
244                root_command_string( ic->irc, cmd );
245        }
246       
247        return TRUE;
248}
249
250static const struct irc_channel_funcs control_channel_funcs = {
251        control_channel_privmsg,
252};
Note: See TracBrowser for help on using the repository browser.