source: irc_channel.c @ 83e92bf

Last change on this file since 83e92bf was 83e92bf, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-27T12:30:00Z

Topic handling changes.

  • Property mode set to 100644
File size: 3.2 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
28irc_channel_t *irc_channel_new( irc_t *irc, const char *name )
29{
30        irc_channel_t *ic;
31       
32        if( strchr( CTYPES, name[0] ) == NULL || !nick_ok( name + 1 ) )
33                return NULL;
34       
35        ic = g_new0( irc_channel_t, 1 );
36        ic->irc = irc;
37        ic->name = g_strdup( name );
38        strcpy( ic->mode, CMODE );
39       
40        irc_channel_add_user( ic, irc->root );
41       
42        irc->channels = g_slist_prepend( irc->channels, ic );
43       
44        return ic;
45}
46
47irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name )
48{
49        GSList *l;
50       
51        for( l = irc->channels; l; l = l->next )
52        {
53                irc_channel_t *ic = l->data;
54               
55                if( name[0] == ic->name[0] && nick_cmp( name + 1, ic->name + 1 ) == 0 )
56                        return ic;
57        }
58       
59        return NULL;
60}
61
62int irc_channel_free( irc_channel_t *ic )
63{
64        irc_t *irc = ic->irc;
65       
66        if( ic->flags & IRC_CHANNEL_JOINED )
67                irc_channel_del_user( ic, irc->user );
68       
69        irc->channels = g_slist_remove( irc->channels, ic );
70        g_slist_free( ic->users );
71       
72        g_free( ic->name );
73        g_free( ic->topic );
74        g_free( ic );
75       
76        return 1;
77}
78
79int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu )
80{
81        if( g_slist_find( ic->users, iu ) != NULL )
82                return 0;
83       
84        ic->users = g_slist_insert_sorted( ic->users, iu, irc_user_cmp );
85       
86        if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED )
87        {
88                ic->flags |= IRC_CHANNEL_JOINED;
89                irc_send_join( ic, iu );
90        }
91       
92        return 1;
93}
94
95int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu )
96{
97        if( g_slist_find( ic->users, iu ) == NULL )
98                return 0;
99       
100        ic->users = g_slist_remove( ic->users, iu );
101       
102        if( ic->flags & IRC_CHANNEL_JOINED )
103                irc_send_part( ic, iu, "" );
104       
105        if( iu == ic->irc->user )
106                ic->flags &= ~IRC_CHANNEL_JOINED;
107       
108        return 1;
109}
110
111int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu )
112{
113        g_free( ic->topic );
114        ic->topic = g_strdup( topic );
115       
116        g_free( ic->topic_who );
117        if( iu )
118                ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host );
119        else
120                ic->topic_who = NULL;
121       
122        ic->topic_time = time( NULL );
123       
124        if( ic->flags & IRC_CHANNEL_JOINED )
125                irc_send_topic( ic, TRUE );
126       
127        return 1;
128}
Note: See TracBrowser for help on using the repository browser.