source: irc_channel.c @ f3b6764

Last change on this file since f3b6764 was 006a84f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-04T20:40:15Z

Kick the user instead of parting him/her when cleaning up a channel. This is
what the older version also did so that Irssi won't clean up the window.

  • Property mode set to 100644
File size: 15.9 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 char *set_eval_channel_type( set_t *set, char *value );
29static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ );
30static const struct irc_channel_funcs control_channel_funcs;
31
32extern const struct irc_channel_funcs irc_channel_im_chat_funcs;
33
34irc_channel_t *irc_channel_new( irc_t *irc, const char *name )
35{
36        irc_channel_t *ic;
37       
38        if( !irc_channel_name_ok( name ) || irc_channel_by_name( irc, name ) )
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       
48        irc->channels = g_slist_append( irc->channels, ic );
49       
50        set_add( &ic->set, "auto_join", "false", set_eval_bool, ic );
51        set_add( &ic->set, "type", "control", set_eval_channel_type, ic );
52       
53        if( name[0] == '&' )
54                set_setstr( &ic->set, "type", "control" );
55        else /* if( name[0] == '#' ) */
56                set_setstr( &ic->set, "type", "chat" );
57       
58        return ic;
59}
60
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               
69                if( irc_channel_name_cmp( name, ic->name ) == 0 )
70                        return ic;
71        }
72       
73        return NULL;
74}
75
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
116int irc_channel_free( irc_channel_t *ic )
117{
118        irc_t *irc = ic->irc;
119       
120        if( ic->flags & IRC_CHANNEL_JOINED )
121                irc_channel_del_user( ic, irc->user, IRC_CDU_KICK, "Cleaning up channel" );
122       
123        if( ic->f->_free )
124                ic->f->_free( ic );
125       
126        while( ic->set )
127                set_del( &ic->set, ic->set->key );
128       
129        irc->channels = g_slist_remove( irc->channels, ic );
130        while( ic->users )
131        {
132                g_free( ic->users->data );
133                ic->users = g_slist_remove( ic->users, ic->users->data );
134        }
135       
136        g_free( ic->name );
137        g_free( ic->topic );
138        g_free( ic->topic_who );
139        g_free( ic );
140       
141        return 1;
142}
143
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
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 )
186                new = &irc_channel_im_chat_funcs;
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
202int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu )
203{
204        irc_channel_user_t *icu;
205       
206        if( irc_channel_has_user( ic, iu ) )
207                return 0;
208       
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 );
213       
214        irc_channel_update_ops( ic, set_getstr( &ic->irc->b->set, "ops" ) );
215       
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
225int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu, irc_channel_del_user_type_t type, const char *msg )
226{
227        irc_channel_user_t *icu;
228       
229        if( !( icu = irc_channel_has_user( ic, iu ) ) )
230                return 0;
231       
232        ic->users = g_slist_remove( ic->users, icu );
233        g_free( icu );
234       
235        if( !( ic->flags & IRC_CHANNEL_JOINED ) || type == IRC_CDU_SILENT ) {}
236                /* Do nothing. The caller should promise it won't screw
237                   up state of the IRC client. :-) */
238        else if( type == IRC_CDU_PART )
239                irc_send_part( ic, iu, msg );
240        else if( type == IRC_CDU_KICK )
241                irc_send_kick( ic, iu, ic->irc->root, msg );
242       
243        if( iu == ic->irc->user )
244        {
245                ic->flags &= ~IRC_CHANNEL_JOINED;
246               
247                if( ic->irc->status & USTATUS_SHUTDOWN )
248                {
249                        /* Don't do anything fancy when we're shutting down anyway. */
250                }
251                else if( ic->flags & IRC_CHANNEL_TEMP )
252                {
253                        irc_channel_free_soon( ic );
254                }
255                else
256                {
257                        /* Flush userlist now. The user won't see it anyway. */
258                        while( ic->users )
259                        {
260                                g_free( ic->users->data );
261                                ic->users = g_slist_remove( ic->users, ic->users->data );
262                        }
263                        irc_channel_add_user( ic, ic->irc->root );
264                }
265        }
266       
267        return 1;
268}
269
270irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu )
271{
272        GSList *l;
273       
274        for( l = ic->users; l; l = l->next )
275        {
276                irc_channel_user_t *icu = l->data;
277               
278                if( icu->iu == iu )
279                        return icu;
280        }
281       
282        return NULL;
283}
284
285int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu )
286{
287        g_free( ic->topic );
288        ic->topic = g_strdup( topic );
289       
290        g_free( ic->topic_who );
291        if( iu )
292                ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host );
293        else
294                ic->topic_who = NULL;
295       
296        ic->topic_time = time( NULL );
297       
298        if( ic->flags & IRC_CHANNEL_JOINED )
299                irc_send_topic( ic, TRUE );
300       
301        return 1;
302}
303
304void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags )
305{
306        irc_channel_user_t *icu = irc_channel_has_user( ic, iu );
307       
308        if( !icu || icu->flags == flags )
309                return;
310       
311        if( ic->flags & IRC_CHANNEL_JOINED )
312                irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags );
313       
314        icu->flags = flags;
315}
316
317void irc_channel_auto_joins( irc_t *irc, account_t *acc )
318{
319        GSList *l;
320       
321        for( l = irc->channels; l; l = l->next )
322        {
323                irc_channel_t *ic = l->data;
324                gboolean aj = set_getbool( &ic->set, "auto_join" );
325                char *type;
326               
327                if( acc &&
328                    ( type = set_getstr( &ic->set, "chat_type" ) ) &&
329                    strcmp( type, "room" ) == 0 )
330                {
331                        /* Bit of an ugly special case: Handle chatrooms here, we
332                           can only auto-join them if their account is online. */
333                        char *acc_s;
334                       
335                        if( !aj && !( ic->flags & IRC_CHANNEL_JOINED ) )
336                                /* Only continue if this one's marked as auto_join
337                                   or if we're in it already. (Possible if the
338                                   client auto-rejoined it before identyfing.) */
339                                continue;
340                        else if( !( acc_s = set_getstr( &ic->set, "account" ) ) )
341                                continue;
342                        else if( account_get( irc->b, acc_s ) != acc )
343                                continue;
344                        else if( acc->ic == NULL || !( acc->ic->flags & OPT_LOGGED_IN ) )
345                                continue;
346                        else
347                                ic->f->join( ic );
348                }
349                else if( aj )
350                {
351                        irc_channel_add_user( ic, irc->user );
352                }
353        }
354}
355
356void irc_channel_printf( irc_channel_t *ic, char *format, ... )
357{
358        va_list params;
359        char *text;
360       
361        va_start( params, format );
362        text = g_strdup_vprintf( format, params );
363        va_end( params );
364       
365        irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL );
366        g_free( text );
367}
368
369gboolean irc_channel_name_ok( const char *name_ )
370{
371        const unsigned char *name = (unsigned char*) name_;
372        int i;
373       
374        if( name_[0] == '\0' )
375                return FALSE;
376       
377        /* Check if the first character is in CTYPES (#&) */
378        if( strchr( CTYPES, name_[0] ) == NULL )
379                return FALSE;
380       
381        /* RFC 1459 keeps amazing me: While only a "few" chars are allowed
382           in nicknames, channel names can be pretty much anything as long
383           as they start with # or &. I'll be a little bit more strict and
384           disallow all non-printable characters. */
385        for( i = 1; name[i]; i ++ )
386                if( name[i] <= ' ' || name[i] == ',' )
387                        return FALSE;
388       
389        return TRUE;
390}
391
392void irc_channel_name_strip( char *name )
393{
394        int i, j;
395       
396        for( i = j = 0; name[i]; i ++ )
397                if( name[i] > ' ' && name[i] != ',' )
398                        name[j++] = name[i];
399       
400        name[j] = '\0';
401}
402
403int irc_channel_name_cmp( const char *a_, const char *b_ )
404{
405        static unsigned char case_map[256];
406        const unsigned char *a = (unsigned char*) a_, *b = (unsigned char*) b_;
407        int i;
408       
409        if( case_map['A'] == '\0' )
410        {
411                for( i = 33; i < 256; i ++ )
412                        if( i != ',' )
413                                case_map[i] = i;
414               
415                for( i = 0; i < 26; i ++ )
416                        case_map['A'+i] = 'a' + i;
417               
418                case_map['['] = '{';
419                case_map[']'] = '}';
420                case_map['~'] = '`';
421                case_map['\\'] = '|';
422        }
423       
424        if( !irc_channel_name_ok( a_ ) || !irc_channel_name_ok( b_ ) )
425                return -1;
426       
427        for( i = 0; a[i] && b[i] && case_map[a[i]] && case_map[b[i]]; i ++ )
428        {
429                if( case_map[a[i]] == case_map[b[i]] )
430                        continue;
431                else
432                        return case_map[a[i]] - case_map[b[i]];
433        }
434       
435        return case_map[a[i]] - case_map[b[i]];
436}
437
438static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ )
439{
440        const irc_channel_user_t *a = a_, *b = b_;
441       
442        return irc_user_cmp( a->iu, b->iu );
443}
444
445void irc_channel_update_ops( irc_channel_t *ic, char *value )
446{
447        irc_channel_user_set_mode( ic, ic->irc->root,
448                ( strcmp( value, "both" ) == 0 ||
449                  strcmp( value, "root" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 );
450        irc_channel_user_set_mode( ic, ic->irc->user,
451                ( strcmp( value, "both" ) == 0 ||
452                  strcmp( value, "user" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 );
453}
454
455char *set_eval_irc_channel_ops( set_t *set, char *value )
456{
457        irc_t *irc = set->data;
458        GSList *l;
459       
460        if( strcmp( value, "both" ) != 0 && strcmp( value, "none" ) != 0 && 
461            strcmp( value, "user" ) != 0 && strcmp( value, "root" ) != 0 )
462                return SET_INVALID;
463       
464        for( l = irc->channels; l; l = l->next )
465                irc_channel_update_ops( l->data, value );
466       
467        return value;
468}
469
470/* Channel-type dependent functions, for control channels: */
471static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg )
472{
473        irc_t *irc = ic->irc;
474        const char *s;
475       
476        /* Scan for non-whitespace chars followed by a colon: */
477        for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {}
478       
479        if( *s == ':' || *s == ',' )
480        {
481                char to[s-msg+1];
482                irc_user_t *iu;
483               
484                memset( to, 0, sizeof( to ) );
485                strncpy( to, msg, s - msg );
486                while( *(++s) && isspace( *s ) ) {}
487               
488                iu = irc_user_by_name( irc, to );
489                if( iu && iu->f->privmsg )
490                {
491                        iu->last_channel = ic;
492                        iu->f->privmsg( iu, s );
493                }
494                else
495                {
496                        irc_channel_printf( ic, "User does not exist: %s", to );
497                }
498        }
499        else
500        {
501                /* TODO: Maybe just use root->privmsg here now? */
502                char cmd[strlen(msg)+1];
503               
504                g_free( ic->irc->last_root_cmd );
505                ic->irc->last_root_cmd = g_strdup( ic->name );
506               
507                strcpy( cmd, msg );
508                root_command_string( ic->irc, cmd );
509        }
510       
511        return TRUE;
512}
513
514static gboolean control_channel_invite( irc_channel_t *ic, irc_user_t *iu )
515{
516        struct irc_control_channel *icc = ic->data;
517        bee_user_t *bu = iu->bu;
518       
519        if( bu == NULL )
520                return FALSE;
521       
522        if( icc->type != IRC_CC_TYPE_GROUP )
523        {
524                irc_send_num( ic->irc, 482, "%s :Invitations are only possible to fill_by=group channels", ic->name );
525                return FALSE;
526        }
527       
528        bu->ic->acc->prpl->add_buddy( bu->ic, bu->handle,
529                                      icc->group ? icc->group->name : NULL );
530       
531        return TRUE;
532}
533
534static char *set_eval_by_account( set_t *set, char *value );
535static char *set_eval_fill_by( set_t *set, char *value );
536static char *set_eval_by_group( set_t *set, char *value );
537static char *set_eval_by_protocol( set_t *set, char *value );
538
539static gboolean control_channel_init( irc_channel_t *ic )
540{
541        struct irc_control_channel *icc;
542       
543        set_add( &ic->set, "account", NULL, set_eval_by_account, ic );
544        set_add( &ic->set, "fill_by", "all", set_eval_fill_by, ic );
545        set_add( &ic->set, "group", NULL, set_eval_by_group, ic );
546        set_add( &ic->set, "protocol", NULL, set_eval_by_protocol, ic );
547       
548        ic->data = icc = g_new0( struct irc_control_channel, 1 );
549        icc->type = IRC_CC_TYPE_DEFAULT;
550       
551        if( bee_group_by_name( ic->irc->b, ic->name + 1, FALSE ) )
552        {
553                set_setstr( &ic->set, "group", ic->name + 1 );
554                set_setstr( &ic->set, "fill_by", "group" );
555        }
556        else if( set_setstr( &ic->set, "protocol", ic->name + 1 ) )
557        {
558                set_setstr( &ic->set, "fill_by", "protocol" );
559        }
560        else if( set_setstr( &ic->set, "account", ic->name + 1 ) )
561        {
562                set_setstr( &ic->set, "fill_by", "account" );
563        }
564        else
565        {
566                bee_irc_channel_update( ic->irc, ic, NULL );
567        }
568       
569        return TRUE;
570}
571
572static gboolean control_channel_join( irc_channel_t *ic )
573{
574        bee_irc_channel_update( ic->irc, ic, NULL );
575       
576        return TRUE;
577}
578
579static char *set_eval_by_account( set_t *set, char *value )
580{
581        struct irc_channel *ic = set->data;
582        struct irc_control_channel *icc = ic->data;
583        account_t *acc;
584       
585        if( !( acc = account_get( ic->irc->b, value ) ) )
586                return SET_INVALID;
587       
588        icc->account = acc;
589        if( icc->type == IRC_CC_TYPE_ACCOUNT )
590                bee_irc_channel_update( ic->irc, ic, NULL );
591       
592        return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user );
593}
594
595static char *set_eval_fill_by( set_t *set, char *value )
596{
597        struct irc_channel *ic = set->data;
598        struct irc_control_channel *icc = ic->data;
599       
600        if( strcmp( value, "all" ) == 0 )
601                icc->type = IRC_CC_TYPE_DEFAULT;
602        else if( strcmp( value, "rest" ) == 0 )
603                icc->type = IRC_CC_TYPE_REST;
604        else if( strcmp( value, "group" ) == 0 )
605                icc->type = IRC_CC_TYPE_GROUP;
606        else if( strcmp( value, "account" ) == 0 )
607                icc->type = IRC_CC_TYPE_ACCOUNT;
608        else if( strcmp( value, "protocol" ) == 0 )
609                icc->type = IRC_CC_TYPE_PROTOCOL;
610        else
611                return SET_INVALID;
612       
613        bee_irc_channel_update( ic->irc, ic, NULL );
614        return value;
615}
616
617static char *set_eval_by_group( set_t *set, char *value )
618{
619        struct irc_channel *ic = set->data;
620        struct irc_control_channel *icc = ic->data;
621       
622        icc->group = bee_group_by_name( ic->irc->b, value, TRUE );
623        if( icc->type == IRC_CC_TYPE_GROUP )
624                bee_irc_channel_update( ic->irc, ic, NULL );
625       
626        return g_strdup( icc->group->name );
627}
628
629static char *set_eval_by_protocol( set_t *set, char *value )
630{
631        struct irc_channel *ic = set->data;
632        struct irc_control_channel *icc = ic->data;
633        struct prpl *prpl;
634       
635        if( !( prpl = find_protocol( value ) ) )
636                return SET_INVALID;
637       
638        icc->protocol = prpl;
639        if( icc->type == IRC_CC_TYPE_PROTOCOL )
640                bee_irc_channel_update( ic->irc, ic, NULL );
641       
642        return value;
643}
644
645static gboolean control_channel_free( irc_channel_t *ic )
646{
647        struct irc_control_channel *icc = ic->data;
648       
649        set_del( &ic->set, "account" );
650        set_del( &ic->set, "fill_by" );
651        set_del( &ic->set, "group" );
652        set_del( &ic->set, "protocol" );
653       
654        g_free( icc );
655        ic->data = NULL;
656       
657        return TRUE;
658}
659
660static const struct irc_channel_funcs control_channel_funcs = {
661        control_channel_privmsg,
662        control_channel_join,
663        NULL,
664        NULL,
665        control_channel_invite,
666       
667        control_channel_init,
668        control_channel_free,
669};
Note: See TracBrowser for help on using the repository browser.