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