source: irc_channel.c @ bce78c8

Last change on this file since bce78c8 was bce78c8, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-02T18:48:26Z

Restored classical control channel behaviour (the ability to talk to
contacts in there).

  • Property mode set to 100644
File size: 4.5 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 const struct irc_channel_funcs control_channel_funcs;
29
30irc_channel_t *irc_channel_new( irc_t *irc, const char *name )
31{
32        irc_channel_t *ic;
33       
34        if( strchr( CTYPES, name[0] ) == NULL || !nick_ok( name + 1 ) )
35                return NULL;
36       
37        ic = g_new0( irc_channel_t, 1 );
38        ic->f = &control_channel_funcs;
39        ic->irc = irc;
40        ic->name = g_strdup( name );
41        strcpy( ic->mode, CMODE );
42       
43        irc_channel_add_user( ic, irc->root );
44       
45        irc->channels = g_slist_prepend( irc->channels, ic );
46       
47        return ic;
48}
49
50irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name )
51{
52        GSList *l;
53       
54        for( l = irc->channels; l; l = l->next )
55        {
56                irc_channel_t *ic = l->data;
57               
58                if( name[0] == ic->name[0] && nick_cmp( name + 1, ic->name + 1 ) == 0 )
59                        return ic;
60        }
61       
62        return NULL;
63}
64
65int irc_channel_free( irc_channel_t *ic )
66{
67        irc_t *irc = ic->irc;
68       
69        if( ic->flags & IRC_CHANNEL_JOINED )
70                irc_channel_del_user( ic, irc->user );
71       
72        irc->channels = g_slist_remove( irc->channels, ic );
73        g_slist_free( ic->users );
74       
75        g_free( ic->name );
76        g_free( ic->topic );
77        g_free( ic );
78       
79        return 1;
80}
81
82int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu )
83{
84        if( irc_channel_has_user( ic, iu ) )
85                return 0;
86       
87        ic->users = g_slist_insert_sorted( ic->users, iu, irc_user_cmp );
88       
89        if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED )
90        {
91                ic->flags |= IRC_CHANNEL_JOINED;
92                irc_send_join( ic, iu );
93        }
94       
95        return 1;
96}
97
98int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu )
99{
100        if( !irc_channel_has_user( ic, iu ) )
101                return 0;
102       
103        ic->users = g_slist_remove( ic->users, iu );
104       
105        if( ic->flags & IRC_CHANNEL_JOINED )
106                irc_send_part( ic, iu, "" );
107       
108        if( iu == ic->irc->user )
109                ic->flags &= ~IRC_CHANNEL_JOINED;
110       
111        return 1;
112}
113
114/* Currently a fairly stupid one-liner but I fear it's going to get worse. :-) */
115gboolean irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu )
116{
117        return g_slist_find( ic->users, iu ) != NULL;
118}
119
120int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu )
121{
122        g_free( ic->topic );
123        ic->topic = g_strdup( topic );
124       
125        g_free( ic->topic_who );
126        if( iu )
127                ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host );
128        else
129                ic->topic_who = NULL;
130       
131        ic->topic_time = time( NULL );
132       
133        if( ic->flags & IRC_CHANNEL_JOINED )
134                irc_send_topic( ic, TRUE );
135       
136        return 1;
137}
138
139gboolean irc_channel_name_ok( const char *name )
140{
141        return strchr( CTYPES, name[0] ) != NULL && nick_ok( name + 1 );
142}
143
144/* Channel-type dependent functions, for control channels: */
145static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg )
146{
147        irc_t *irc = ic->irc;
148        const char *s;
149       
150        /* Scan for non-whitespace chars followed by a colon: */
151        for( s = msg; *s && !isspace( *s ) && *s != ':'; s ++ ) {}
152       
153        if( *s == ':' )
154        {
155                char to[s-msg+1];
156                irc_user_t *iu;
157               
158                strncpy( to, msg, s - msg );
159                while( *(++s) && isspace( *s ) ) {}
160               
161                iu = irc_user_by_name( irc, to );
162                if( iu && iu->f->privmsg )
163                {
164                        iu->flags &= ~IRC_USER_PRIVATE;
165                        iu->f->privmsg( iu, s );
166                }
167        }
168        else
169        {
170                /* TODO: Maybe just use root->privmsg here now? */
171                char cmd[strlen(msg)+1];
172               
173                g_free( ic->irc->last_root_cmd );
174                ic->irc->last_root_cmd = g_strdup( ic->name );
175               
176                strcpy( cmd, msg );
177                root_command_string( ic->irc, cmd );
178        }
179       
180        return TRUE;
181}
182
183static const struct irc_channel_funcs control_channel_funcs = {
184        control_channel_privmsg,
185};
Note: See TracBrowser for help on using the repository browser.