source: irc_channel.c @ 5c7b45c

Last change on this file since 5c7b45c was c8eeadd, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-04T10:16:07Z

Added automatic joining of channels. Auto-rejoin functionality for
groupchats not reimplemented yet but that's the next step.

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