source: irc_channel.c @ cf1a979

Last change on this file since cf1a979 was cf1a979, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-05T01:07:04Z

Bug fixes, control channel behaviour is mostly okay again.

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