source: irc_channel.c @ 9052bc1

Last change on this file since 9052bc1 was 9052bc1, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-30T23:15:44Z

Flush channels when the user leaves them. Also, don't update a control
channel if the user isn't in it.

  • Property mode set to 100644
File size: 14.3 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 );
47       
[36562b0]48        irc->channels = g_slist_append( irc->channels, ic );
[4be8239]49       
[2b8473c]50        set_add( &ic->set, "type", "control", set_eval_channel_type, ic );
51       
[eb37735]52        if( name[0] == '&' )
[2b8473c]53                set_setstr( &ic->set, "type", "control" );
[eb37735]54        else /* if( name[0] == '#' ) */
[2b8473c]55                set_setstr( &ic->set, "type", "chat" );
[9ac3ed1]56       
[4be8239]57        return ic;
58}
59
[b9e020a]60irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name )
61{
62        GSList *l;
63       
64        for( l = irc->channels; l; l = l->next )
65        {
66                irc_channel_t *ic = l->data;
67               
[6b90431]68                if( irc_channel_name_cmp( name, ic->name ) == 0 )
[b9e020a]69                        return ic;
70        }
71       
72        return NULL;
73}
74
[36562b0]75irc_channel_t *irc_channel_get( irc_t *irc, char *id )
76{
77        irc_channel_t *ic, *ret = NULL;
78        GSList *l;
79        int nr;
80       
81        if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 )
82        {
83                for( l = irc->channels; l; l = l->next )
84                {
85                        ic = l->data;
86                        if( ( nr-- ) == 0 ) 
87                                return ic;
88                }
89               
90                return NULL;
91        }
92       
93        /* Exact match first: Partial match only sucks if there's a channel
94           #aa and #aabb */
95        if( ( ret = irc_channel_by_name( irc, id ) ) )
96                return ret;
97       
98        for( l = irc->channels; l; l = l->next )
99        {
100                ic = l->data;
101               
102                if( strstr( ic->name, id ) )
103                {
104                        /* Make sure it's a unique match. */
105                        if( !ret )
106                                ret = ic;
107                        else
108                                return NULL;
109                }
110        }
111       
112        return ret;
113}
114
[63a520b]115int irc_channel_free( irc_channel_t *ic )
116{
117        irc_t *irc = ic->irc;
118       
119        if( ic->flags & IRC_CHANNEL_JOINED )
[18da20b]120                irc_channel_del_user( ic, irc->user, FALSE, "Cleaning up channel" );
[63a520b]121       
[d7db346]122        if( ic->f->_free )
123                ic->f->_free( ic );
124       
125        while( ic->set )
126                set_del( &ic->set, ic->set->key );
127       
[63a520b]128        irc->channels = g_slist_remove( irc->channels, ic );
[e54112f]129        while( ic->users )
130        {
131                g_free( ic->users->data );
132                ic->users = g_slist_remove( ic->users, ic->users->data );
133        }
[63a520b]134       
135        g_free( ic->name );
136        g_free( ic->topic );
[d7db346]137        g_free( ic->topic_who );
[63a520b]138        g_free( ic );
139       
140        return 1;
141}
142
[ab6006c]143struct irc_channel_free_data
144{
145        irc_t *irc;
146        irc_channel_t *ic;
147        char *name;
148};
149
150static gboolean irc_channel_free_callback( gpointer data, gint fd, b_input_condition cond )
151{
152        struct irc_channel_free_data *d = data;
153       
154        if( g_slist_find( irc_connection_list, d->irc ) &&
155            irc_channel_by_name( d->irc, d->name ) == d->ic &&
156            !( d->ic->flags & IRC_CHANNEL_JOINED ) )
157                irc_channel_free( d->ic );
158
159        g_free( d->name );
160        g_free( d );
161        return FALSE;
162}
163
164/* Free the channel, but via the event loop, so after finishing whatever event
165   we're currently handling. */
166void irc_channel_free_soon( irc_channel_t *ic )
167{
168        struct irc_channel_free_data *d = g_new0( struct irc_channel_free_data, 1 );
169       
170        d->irc = ic->irc;
171        d->ic = ic;
172        d->name = g_strdup( ic->name );
173       
174        b_timeout_add( 0, irc_channel_free_callback, d );
175}
176
[2b8473c]177static char *set_eval_channel_type( set_t *set, char *value )
178{
179        struct irc_channel *ic = set->data;
180        const struct irc_channel_funcs *new;
181       
182        if( strcmp( value, "control" ) == 0 )
183                new = &control_channel_funcs;
184        else if( strcmp( value, "chat" ) == 0 )
[5a75d15]185                new = &irc_channel_im_chat_funcs;
[2b8473c]186        else
187                return SET_INVALID;
188       
189        /* TODO: Return values. */
190        if( ic->f && ic->f->_free )
191                ic->f->_free( ic );
192       
193        ic->f = new;
194       
195        if( ic->f && ic->f->_init )
196                ic->f->_init( ic );
197       
198        return value;
199}
200
[4be8239]201int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu )
202{
[e54112f]203        irc_channel_user_t *icu;
204       
[57c96f7]205        if( irc_channel_has_user( ic, iu ) )
[4be8239]206                return 0;
207       
[e54112f]208        icu = g_new0( irc_channel_user_t, 1 );
209        icu->iu = iu;
210       
211        ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp );
[4be8239]212       
[c5aefa4]213        irc_channel_update_ops( ic, set_getstr( &ic->irc->b->set, "ops" ) );
214       
[4be8239]215        if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED )
216        {
217                ic->flags |= IRC_CHANNEL_JOINED;
218                irc_send_join( ic, iu );
219        }
220       
221        return 1;
222}
223
[18da20b]224int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu, gboolean silent, const char *msg )
[4be8239]225{
[e54112f]226        irc_channel_user_t *icu;
227       
228        if( !( icu = irc_channel_has_user( ic, iu ) ) )
[4be8239]229                return 0;
230       
[e54112f]231        ic->users = g_slist_remove( ic->users, icu );
232        g_free( icu );
[4be8239]233       
[18da20b]234        if( ic->flags & IRC_CHANNEL_JOINED && !silent )
235                irc_send_part( ic, iu, msg );
[4be8239]236       
237        if( iu == ic->irc->user )
[1c40aa7]238        {
[4be8239]239                ic->flags &= ~IRC_CHANNEL_JOINED;
[1c40aa7]240               
241                if( ic->flags & IRC_CHANNEL_TEMP )
[ab6006c]242                        irc_channel_free_soon( ic );
[9052bc1]243                else
244                {
245                        /* Flush userlist now. The user won't see it anyway. */
246                        while( ic->users )
247                        {
248                                g_free( ic->users->data );
249                                ic->users = g_slist_remove( ic->users, ic->users->data );
250                        }
251                        irc_channel_add_user( ic, ic->irc->root );
252                }
[1c40aa7]253        }
[4be8239]254       
255        return 1;
256}
257
[e54112f]258irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu )
[0b5cc72]259{
[e54112f]260        GSList *l;
261       
262        for( l = ic->users; l; l = l->next )
263        {
264                irc_channel_user_t *icu = l->data;
265               
266                if( icu->iu == iu )
267                        return icu;
268        }
269       
270        return NULL;
[0b5cc72]271}
272
[83e92bf]273int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu )
[4be8239]274{
275        g_free( ic->topic );
276        ic->topic = g_strdup( topic );
277       
[83e92bf]278        g_free( ic->topic_who );
279        if( iu )
280                ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host );
281        else
282                ic->topic_who = NULL;
283       
284        ic->topic_time = time( NULL );
285       
[4be8239]286        if( ic->flags & IRC_CHANNEL_JOINED )
[83e92bf]287                irc_send_topic( ic, TRUE );
[4be8239]288       
289        return 1;
290}
[b919363]291
[6a9d068]292void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags )
293{
294        irc_channel_user_t *icu = irc_channel_has_user( ic, iu );
295       
[c5aefa4]296        if( !icu || icu->flags == flags )
[6a9d068]297                return;
298       
299        if( ic->flags & IRC_CHANNEL_JOINED )
300                irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags );
301       
302        icu->flags = flags;
303}
304
[9893da3]305void irc_channel_printf( irc_channel_t *ic, char *format, ... )
306{
307        va_list params;
308        char *text;
309       
310        va_start( params, format );
311        text = g_strdup_vprintf( format, params );
312        va_end( params );
313       
314        irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL );
315        g_free( text );
316}
317
[6b90431]318gboolean irc_channel_name_ok( const char *name_ )
[b919363]319{
[6b90431]320        const unsigned char *name = (unsigned char*) name_;
321        int i;
[4c17d19]322       
[a670aeb]323        if( name_[0] == '\0' )
324                return FALSE;
325       
[4c17d19]326        /* Check if the first character is in CTYPES (#&) */
[6b90431]327        if( strchr( CTYPES, name_[0] ) == NULL )
[4c17d19]328                return FALSE;
329       
[6b90431]330        /* RFC 1459 keeps amazing me: While only a "few" chars are allowed
331           in nicknames, channel names can be pretty much anything as long
332           as they start with # or &. I'll be a little bit more strict and
333           disallow all non-printable characters. */
334        for( i = 1; name[i]; i ++ )
335                if( name[i] <= ' ' || name[i] == ',' )
336                        return FALSE;
337       
338        return TRUE;
339}
340
[134a02c]341void irc_channel_name_strip( char *name )
342{
343        int i, j;
344       
345        for( i = j = 0; name[i]; i ++ )
346                if( name[i] > ' ' && name[i] != ',' )
347                        name[j++] = name[i];
348       
349        name[j] = '\0';
350}
351
[6b90431]352int irc_channel_name_cmp( const char *a_, const char *b_ )
353{
354        static unsigned char case_map[256];
355        const unsigned char *a = (unsigned char*) a_, *b = (unsigned char*) b_;
356        int i;
357       
358        if( case_map['A'] == '\0' )
359        {
360                for( i = 33; i < 256; i ++ )
361                        if( i != ',' )
362                                case_map[i] = i;
363               
364                for( i = 0; i < 26; i ++ )
365                        case_map['A'+i] = 'a' + i;
366               
367                case_map['['] = '{';
368                case_map[']'] = '}';
369                case_map['~'] = '`';
370                case_map['\\'] = '|';
371        }
372       
373        if( !irc_channel_name_ok( a_ ) || !irc_channel_name_ok( b_ ) )
374                return -1;
375       
376        for( i = 0; a[i] && b[i] && case_map[a[i]] && case_map[b[i]]; i ++ )
377        {
378                if( case_map[a[i]] == case_map[b[i]] )
379                        continue;
380                else
381                        return case_map[a[i]] - case_map[b[i]];
382        }
383       
384        return case_map[a[i]] - case_map[b[i]];
[b919363]385}
[280c56a]386
[e54112f]387static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ )
388{
389        const irc_channel_user_t *a = a_, *b = b_;
390       
391        return irc_user_cmp( a->iu, b->iu );
392}
393
[c5aefa4]394void irc_channel_update_ops( irc_channel_t *ic, char *value )
395{
396        irc_channel_user_set_mode( ic, ic->irc->root,
397                ( strcmp( value, "both" ) == 0 ||
398                  strcmp( value, "root" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 );
399        irc_channel_user_set_mode( ic, ic->irc->user,
400                ( strcmp( value, "both" ) == 0 ||
401                  strcmp( value, "user" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 );
402}
403
404char *set_eval_irc_channel_ops( set_t *set, char *value )
405{
406        irc_t *irc = set->data;
407        GSList *l;
408       
409        if( strcmp( value, "both" ) != 0 && strcmp( value, "none" ) != 0 && 
410            strcmp( value, "user" ) != 0 && strcmp( value, "root" ) != 0 )
411                return SET_INVALID;
412       
413        for( l = irc->channels; l; l = l->next )
414                irc_channel_update_ops( l->data, value );
415       
416        return value;
417}
418
[280c56a]419/* Channel-type dependent functions, for control channels: */
420static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg )
421{
[bce78c8]422        irc_t *irc = ic->irc;
423        const char *s;
[fb117aee]424       
[bce78c8]425        /* Scan for non-whitespace chars followed by a colon: */
[b0364dc]426        for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {}
[74f1cde]427       
[b0364dc]428        if( *s == ':' || *s == ',' )
[bce78c8]429        {
430                char to[s-msg+1];
431                irc_user_t *iu;
432               
[1a3ba05]433                memset( to, 0, sizeof( to ) );
[bce78c8]434                strncpy( to, msg, s - msg );
435                while( *(++s) && isspace( *s ) ) {}
436               
437                iu = irc_user_by_name( irc, to );
438                if( iu && iu->f->privmsg )
439                {
[92c8d41]440                        iu->last_channel = ic;
[bce78c8]441                        iu->f->privmsg( iu, s );
442                }
[1a3ba05]443                else
444                {
[9893da3]445                        irc_channel_printf( ic, "User does not exist: %s", to );
[1a3ba05]446                }
[bce78c8]447        }
448        else
449        {
450                /* TODO: Maybe just use root->privmsg here now? */
451                char cmd[strlen(msg)+1];
452               
453                g_free( ic->irc->last_root_cmd );
454                ic->irc->last_root_cmd = g_strdup( ic->name );
455               
456                strcpy( cmd, msg );
457                root_command_string( ic->irc, cmd );
458        }
[280c56a]459       
460        return TRUE;
461}
462
[46d215d]463static gboolean control_channel_invite( irc_channel_t *ic, irc_user_t *iu )
464{
465        struct irc_control_channel *icc = ic->data;
466        bee_user_t *bu = iu->bu;
467       
468        if( bu == NULL )
469                return FALSE;
470       
471        if( icc->type != IRC_CC_TYPE_GROUP )
472        {
473                irc_send_num( ic->irc, 482, "%s :Invitations are only possible to fill_by=group channels", ic->name );
474                return FALSE;
475        }
476       
477        bu->ic->acc->prpl->add_buddy( bu->ic, bu->handle,
478                                      icc->group ? icc->group->name : NULL );
479       
480        return TRUE;
481}
482
[2b8473c]483static char *set_eval_by_account( set_t *set, char *value );
484static char *set_eval_fill_by( set_t *set, char *value );
485static char *set_eval_by_group( set_t *set, char *value );
[7a6ba50]486static char *set_eval_by_protocol( set_t *set, char *value );
[2b8473c]487
[9ac3ed1]488static gboolean control_channel_init( irc_channel_t *ic )
489{
490        struct irc_control_channel *icc;
491       
[2b8473c]492        set_add( &ic->set, "account", NULL, set_eval_by_account, ic );
493        set_add( &ic->set, "fill_by", "all", set_eval_fill_by, ic );
494        set_add( &ic->set, "group", NULL, set_eval_by_group, ic );
[7a6ba50]495        set_add( &ic->set, "protocol", NULL, set_eval_by_protocol, ic );
[2b8473c]496       
[9ac3ed1]497        ic->data = icc = g_new0( struct irc_control_channel, 1 );
498        icc->type = IRC_CC_TYPE_DEFAULT;
499       
[2b8473c]500        if( bee_group_by_name( ic->irc->b, ic->name + 1, FALSE ) )
501        {
502                set_setstr( &ic->set, "group", ic->name + 1 );
503                set_setstr( &ic->set, "fill_by", "group" );
504        }
[217bf4e]505        else if( set_setstr( &ic->set, "protocol", ic->name + 1 ) )
506        {
507                set_setstr( &ic->set, "fill_by", "protocol" );
508        }
[2b8473c]509        else if( set_setstr( &ic->set, "account", ic->name + 1 ) )
510        {
511                set_setstr( &ic->set, "fill_by", "account" );
512        }
[cf1a979]513        else
514        {
515                bee_irc_channel_update( ic->irc, ic, NULL );
516        }
[2b8473c]517       
518        return TRUE;
519}
520
521static char *set_eval_by_account( set_t *set, char *value )
522{
523        struct irc_channel *ic = set->data;
524        struct irc_control_channel *icc = ic->data;
525        account_t *acc;
526       
527        if( !( acc = account_get( ic->irc->b, value ) ) )
528                return SET_INVALID;
529       
530        icc->account = acc;
[cf1a979]531        if( icc->type == IRC_CC_TYPE_ACCOUNT )
532                bee_irc_channel_update( ic->irc, ic, NULL );
[7a6ba50]533       
[2b8473c]534        return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user );
535}
536
537static char *set_eval_fill_by( set_t *set, char *value )
538{
539        struct irc_channel *ic = set->data;
540        struct irc_control_channel *icc = ic->data;
541       
542        if( strcmp( value, "all" ) == 0 )
543                icc->type = IRC_CC_TYPE_DEFAULT;
544        else if( strcmp( value, "rest" ) == 0 )
545                icc->type = IRC_CC_TYPE_REST;
546        else if( strcmp( value, "group" ) == 0 )
[13c1a9f]547                icc->type = IRC_CC_TYPE_GROUP;
[2b8473c]548        else if( strcmp( value, "account" ) == 0 )
[a067771]549                icc->type = IRC_CC_TYPE_ACCOUNT;
[7a6ba50]550        else if( strcmp( value, "protocol" ) == 0 )
551                icc->type = IRC_CC_TYPE_PROTOCOL;
[2b8473c]552        else
553                return SET_INVALID;
554       
[cf1a979]555        bee_irc_channel_update( ic->irc, ic, NULL );
[2b8473c]556        return value;
557}
558
559static char *set_eval_by_group( set_t *set, char *value )
560{
561        struct irc_channel *ic = set->data;
562        struct irc_control_channel *icc = ic->data;
[13c1a9f]563       
[2b8473c]564        icc->group = bee_group_by_name( ic->irc->b, value, TRUE );
[cf1a979]565        if( icc->type == IRC_CC_TYPE_GROUP )
566                bee_irc_channel_update( ic->irc, ic, NULL );
[7a6ba50]567       
[2b8473c]568        return g_strdup( icc->group->name );
569}
570
[7a6ba50]571static char *set_eval_by_protocol( set_t *set, char *value )
572{
573        struct irc_channel *ic = set->data;
574        struct irc_control_channel *icc = ic->data;
575        struct prpl *prpl;
576       
577        if( !( prpl = find_protocol( value ) ) )
578                return SET_INVALID;
579       
580        icc->protocol = prpl;
581        if( icc->type == IRC_CC_TYPE_PROTOCOL )
582                bee_irc_channel_update( ic->irc, ic, NULL );
583       
584        return value;
585}
586
[2b8473c]587static gboolean control_channel_free( irc_channel_t *ic )
588{
589        struct irc_control_channel *icc = ic->data;
590       
591        set_del( &ic->set, "account" );
592        set_del( &ic->set, "fill_by" );
593        set_del( &ic->set, "group" );
[9052bc1]594        set_del( &ic->set, "protocol" );
[2b8473c]595       
596        g_free( icc );
597        ic->data = NULL;
[13c1a9f]598       
[9ac3ed1]599        return TRUE;
600}
601
[280c56a]602static const struct irc_channel_funcs control_channel_funcs = {
603        control_channel_privmsg,
[9ac3ed1]604        NULL,
605        NULL,
606        NULL,
[46d215d]607        control_channel_invite,
[9ac3ed1]608       
609        control_channel_init,
[2b8473c]610        control_channel_free,
[280c56a]611};
Note: See TracBrowser for help on using the repository browser.