source: irc_channel.c @ e54112f

Last change on this file since e54112f was e54112f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-02T23:44:33Z

Put a channel userlist in irc_channel_user elements so we can save flags
(i.e. modes).

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