source: irc_channel.c @ 18da20b

Last change on this file since 18da20b was 18da20b, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-06T00:33:33Z

Added /part msgs, and the ability to silently remove users from channels
(when sending a /quit instead, for example).

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[4be8239]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
[2b8473c]28static char *set_eval_channel_type( set_t *set, char *value );
[e54112f]29static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ );
[280c56a]30static const struct irc_channel_funcs control_channel_funcs;
[5a75d15]31
32extern const struct irc_channel_funcs irc_channel_im_chat_funcs;
[280c56a]33
[4be8239]34irc_channel_t *irc_channel_new( irc_t *irc, const char *name )
35{
36        irc_channel_t *ic;
37       
[9438323]38        if( !irc_channel_name_ok( name ) || irc_channel_by_name( irc, name ) )
[4be8239]39                return NULL;
40       
41        ic = g_new0( irc_channel_t, 1 );
42        ic->irc = irc;
43        ic->name = g_strdup( name );
44        strcpy( ic->mode, CMODE );
45       
46        irc_channel_add_user( ic, irc->root );
[6a9d068]47        if( strcmp( set_getstr( &irc->b->set, "ops" ), "both" ) == 0 ||
48            strcmp( set_getstr( &irc->b->set, "ops" ), "root" ) == 0 )
49                irc_channel_user_set_mode( ic, irc->root, IRC_CHANNEL_USER_OP );
[4be8239]50       
51        irc->channels = g_slist_prepend( irc->channels, ic );
52       
[2b8473c]53        set_add( &ic->set, "type", "control", set_eval_channel_type, ic );
54       
[eb37735]55        if( name[0] == '&' )
[2b8473c]56                set_setstr( &ic->set, "type", "control" );
[eb37735]57        else /* if( name[0] == '#' ) */
[2b8473c]58                set_setstr( &ic->set, "type", "chat" );
[9ac3ed1]59       
[4be8239]60        return ic;
61}
62
[b9e020a]63irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name )
64{
65        GSList *l;
66       
67        for( l = irc->channels; l; l = l->next )
68        {
69                irc_channel_t *ic = l->data;
70               
71                if( name[0] == ic->name[0] && nick_cmp( name + 1, ic->name + 1 ) == 0 )
72                        return ic;
73        }
74       
75        return NULL;
76}
77
[63a520b]78int irc_channel_free( irc_channel_t *ic )
79{
80        irc_t *irc = ic->irc;
81       
82        if( ic->flags & IRC_CHANNEL_JOINED )
[18da20b]83                irc_channel_del_user( ic, irc->user, FALSE, "Cleaning up channel" );
[63a520b]84       
85        irc->channels = g_slist_remove( irc->channels, ic );
[e54112f]86        while( ic->users )
87        {
88                g_free( ic->users->data );
89                ic->users = g_slist_remove( ic->users, ic->users->data );
90        }
[63a520b]91       
92        g_free( ic->name );
93        g_free( ic->topic );
94        g_free( ic );
95       
96        return 1;
97}
98
[2b8473c]99static char *set_eval_channel_type( set_t *set, char *value )
100{
101        struct irc_channel *ic = set->data;
102        const struct irc_channel_funcs *new;
103       
104        if( strcmp( value, "control" ) == 0 )
105                new = &control_channel_funcs;
106        else if( strcmp( value, "chat" ) == 0 )
[5a75d15]107                new = &irc_channel_im_chat_funcs;
[2b8473c]108        else
109                return SET_INVALID;
110       
111        /* TODO: Return values. */
112        if( ic->f && ic->f->_free )
113                ic->f->_free( ic );
114       
115        ic->f = new;
116       
117        if( ic->f && ic->f->_init )
118                ic->f->_init( ic );
119       
120        return value;
121}
122
[4be8239]123int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu )
124{
[e54112f]125        irc_channel_user_t *icu;
126       
[57c96f7]127        if( irc_channel_has_user( ic, iu ) )
[4be8239]128                return 0;
129       
[e54112f]130        icu = g_new0( irc_channel_user_t, 1 );
131        icu->iu = iu;
132       
133        ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp );
[4be8239]134       
135        if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED )
136        {
137                ic->flags |= IRC_CHANNEL_JOINED;
138                irc_send_join( ic, iu );
139        }
140       
141        return 1;
142}
143
[18da20b]144int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu, gboolean silent, const char *msg )
[4be8239]145{
[e54112f]146        irc_channel_user_t *icu;
147       
148        if( !( icu = irc_channel_has_user( ic, iu ) ) )
[4be8239]149                return 0;
150       
[e54112f]151        ic->users = g_slist_remove( ic->users, icu );
152        g_free( icu );
[4be8239]153       
[18da20b]154        if( ic->flags & IRC_CHANNEL_JOINED && !silent )
155                irc_send_part( ic, iu, msg );
[4be8239]156       
157        if( iu == ic->irc->user )
158                ic->flags &= ~IRC_CHANNEL_JOINED;
159       
160        return 1;
161}
162
[e54112f]163irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu )
[0b5cc72]164{
[e54112f]165        GSList *l;
166       
167        for( l = ic->users; l; l = l->next )
168        {
169                irc_channel_user_t *icu = l->data;
170               
171                if( icu->iu == iu )
172                        return icu;
173        }
174       
175        return NULL;
[0b5cc72]176}
177
[83e92bf]178int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu )
[4be8239]179{
180        g_free( ic->topic );
181        ic->topic = g_strdup( topic );
182       
[83e92bf]183        g_free( ic->topic_who );
184        if( iu )
185                ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host );
186        else
187                ic->topic_who = NULL;
188       
189        ic->topic_time = time( NULL );
190       
[4be8239]191        if( ic->flags & IRC_CHANNEL_JOINED )
[83e92bf]192                irc_send_topic( ic, TRUE );
[4be8239]193       
194        return 1;
195}
[b919363]196
[6a9d068]197void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags )
198{
199        irc_channel_user_t *icu = irc_channel_has_user( ic, iu );
200       
201        if( icu->flags == flags )
202                return;
203       
204        if( ic->flags & IRC_CHANNEL_JOINED )
205                irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags );
206       
207        icu->flags = flags;
208}
209
[9893da3]210void irc_channel_printf( irc_channel_t *ic, char *format, ... )
211{
212        va_list params;
213        char *text;
214       
215        va_start( params, format );
216        text = g_strdup_vprintf( format, params );
217        va_end( params );
218       
219        irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL );
220        g_free( text );
221}
222
[b919363]223gboolean irc_channel_name_ok( const char *name )
224{
[4c17d19]225        char name_[strlen(name)+1];
226       
227        /* Check if the first character is in CTYPES (#&) */
228        if( strchr( CTYPES, name[0] ) == NULL )
229                return FALSE;
230       
231        /* Check the rest of the name. Just checking name + 1 doesn't work
232           since it will fail if the first character is a number, or if
233           it's a one-char channel name - both of which are legal. */
234        name_[0] = '_';
235        strcpy( name_ + 1, name + 1 );
236        return nick_ok( name_ );
[b919363]237}
[280c56a]238
[e54112f]239static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ )
240{
241        const irc_channel_user_t *a = a_, *b = b_;
242       
243        return irc_user_cmp( a->iu, b->iu );
244}
245
[280c56a]246/* Channel-type dependent functions, for control channels: */
247static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg )
248{
[bce78c8]249        irc_t *irc = ic->irc;
250        const char *s;
[fb117aee]251       
[bce78c8]252        /* Scan for non-whitespace chars followed by a colon: */
[b0364dc]253        for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {}
[74f1cde]254       
[b0364dc]255        if( *s == ':' || *s == ',' )
[bce78c8]256        {
257                char to[s-msg+1];
258                irc_user_t *iu;
259               
[1a3ba05]260                memset( to, 0, sizeof( to ) );
[bce78c8]261                strncpy( to, msg, s - msg );
262                while( *(++s) && isspace( *s ) ) {}
263               
264                iu = irc_user_by_name( irc, to );
265                if( iu && iu->f->privmsg )
266                {
267                        iu->flags &= ~IRC_USER_PRIVATE;
268                        iu->f->privmsg( iu, s );
269                }
[1a3ba05]270                else
271                {
[9893da3]272                        irc_channel_printf( ic, "User does not exist: %s", to );
[1a3ba05]273                }
[bce78c8]274        }
275        else
276        {
277                /* TODO: Maybe just use root->privmsg here now? */
278                char cmd[strlen(msg)+1];
279               
280                g_free( ic->irc->last_root_cmd );
281                ic->irc->last_root_cmd = g_strdup( ic->name );
282               
283                strcpy( cmd, msg );
284                root_command_string( ic->irc, cmd );
285        }
[280c56a]286       
287        return TRUE;
288}
289
[2b8473c]290static char *set_eval_by_account( set_t *set, char *value );
291static char *set_eval_fill_by( set_t *set, char *value );
292static char *set_eval_by_group( set_t *set, char *value );
293
[9ac3ed1]294static gboolean control_channel_init( irc_channel_t *ic )
295{
296        struct irc_control_channel *icc;
297       
[2b8473c]298        set_add( &ic->set, "account", NULL, set_eval_by_account, ic );
299        set_add( &ic->set, "fill_by", "all", set_eval_fill_by, ic );
300        set_add( &ic->set, "group", NULL, set_eval_by_group, ic );
301       
[9ac3ed1]302        ic->data = icc = g_new0( struct irc_control_channel, 1 );
303        icc->type = IRC_CC_TYPE_DEFAULT;
304       
[2b8473c]305        if( bee_group_by_name( ic->irc->b, ic->name + 1, FALSE ) )
306        {
307                set_setstr( &ic->set, "group", ic->name + 1 );
308                set_setstr( &ic->set, "fill_by", "group" );
309        }
310        else if( set_setstr( &ic->set, "account", ic->name + 1 ) )
311        {
312                set_setstr( &ic->set, "fill_by", "account" );
313        }
[cf1a979]314        else
315        {
316                bee_irc_channel_update( ic->irc, ic, NULL );
317        }
[2b8473c]318       
319        return TRUE;
320}
321
322static char *set_eval_by_account( set_t *set, char *value )
323{
324        struct irc_channel *ic = set->data;
325        struct irc_control_channel *icc = ic->data;
326        account_t *acc;
327       
328        if( !( acc = account_get( ic->irc->b, value ) ) )
329                return SET_INVALID;
330       
331        icc->account = acc;
[cf1a979]332        if( icc->type == IRC_CC_TYPE_ACCOUNT )
333                bee_irc_channel_update( ic->irc, ic, NULL );
[2b8473c]334        return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user );
335}
336
337static char *set_eval_fill_by( set_t *set, char *value )
338{
339        struct irc_channel *ic = set->data;
340        struct irc_control_channel *icc = ic->data;
341       
342        if( strcmp( value, "all" ) == 0 )
343                icc->type = IRC_CC_TYPE_DEFAULT;
344        else if( strcmp( value, "rest" ) == 0 )
345                icc->type = IRC_CC_TYPE_REST;
346        else if( strcmp( value, "group" ) == 0 )
[13c1a9f]347                icc->type = IRC_CC_TYPE_GROUP;
[2b8473c]348        else if( strcmp( value, "account" ) == 0 )
[a067771]349                icc->type = IRC_CC_TYPE_ACCOUNT;
[2b8473c]350        else
351                return SET_INVALID;
352       
[cf1a979]353        bee_irc_channel_update( ic->irc, ic, NULL );
[2b8473c]354        return value;
355}
356
357static char *set_eval_by_group( set_t *set, char *value )
358{
359        struct irc_channel *ic = set->data;
360        struct irc_control_channel *icc = ic->data;
[13c1a9f]361       
[2b8473c]362        icc->group = bee_group_by_name( ic->irc->b, value, TRUE );
[cf1a979]363        if( icc->type == IRC_CC_TYPE_GROUP )
364                bee_irc_channel_update( ic->irc, ic, NULL );
[2b8473c]365        return g_strdup( icc->group->name );
366}
367
368static gboolean control_channel_free( irc_channel_t *ic )
369{
370        struct irc_control_channel *icc = ic->data;
371       
372        set_del( &ic->set, "account" );
373        set_del( &ic->set, "fill_by" );
374        set_del( &ic->set, "group" );
375       
376        g_free( icc );
377        ic->data = NULL;
[13c1a9f]378       
[9ac3ed1]379        return TRUE;
380}
381
[280c56a]382static const struct irc_channel_funcs control_channel_funcs = {
383        control_channel_privmsg,
[9ac3ed1]384        NULL,
385        NULL,
386        NULL,
387        NULL,
388       
389        control_channel_init,
[2b8473c]390        control_channel_free,
[280c56a]391};
Note: See TracBrowser for help on using the repository browser.