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 | |
---|
28 | static const struct irc_channel_funcs control_channel_funcs; |
---|
29 | |
---|
30 | irc_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 | |
---|
50 | irc_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 | |
---|
65 | int 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 | |
---|
82 | int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
83 | { |
---|
84 | if( g_slist_find( ic->users, iu ) != NULL ) |
---|
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 | |
---|
98 | int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
99 | { |
---|
100 | if( g_slist_find( ic->users, iu ) == NULL ) |
---|
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 | int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu ) |
---|
115 | { |
---|
116 | g_free( ic->topic ); |
---|
117 | ic->topic = g_strdup( topic ); |
---|
118 | |
---|
119 | g_free( ic->topic_who ); |
---|
120 | if( iu ) |
---|
121 | ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host ); |
---|
122 | else |
---|
123 | ic->topic_who = NULL; |
---|
124 | |
---|
125 | ic->topic_time = time( NULL ); |
---|
126 | |
---|
127 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
128 | irc_send_topic( ic, TRUE ); |
---|
129 | |
---|
130 | return 1; |
---|
131 | } |
---|
132 | |
---|
133 | gboolean irc_channel_name_ok( const char *name ) |
---|
134 | { |
---|
135 | return strchr( CTYPES, name[0] ) != NULL && nick_ok( name + 1 ); |
---|
136 | } |
---|
137 | |
---|
138 | /* Channel-type dependent functions, for control channels: */ |
---|
139 | static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg ) |
---|
140 | { |
---|
141 | g_free( ic->irc->last_root_cmd ); |
---|
142 | ic->irc->last_root_cmd = g_strdup( ic->name ); |
---|
143 | |
---|
144 | root_command_string( ic->irc, msg ); |
---|
145 | |
---|
146 | return TRUE; |
---|
147 | } |
---|
148 | |
---|
149 | static const struct irc_channel_funcs control_channel_funcs = { |
---|
150 | control_channel_privmsg, |
---|
151 | }; |
---|