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 | |
---|
48 | irc->channels = g_slist_append( irc->channels, ic ); |
---|
49 | |
---|
50 | set_add( &ic->set, "auto_join", "false", set_eval_bool, ic ); |
---|
51 | set_add( &ic->set, "type", "control", set_eval_channel_type, ic ); |
---|
52 | |
---|
53 | if( name[0] == '&' ) |
---|
54 | set_setstr( &ic->set, "type", "control" ); |
---|
55 | else /* if( name[0] == '#' ) */ |
---|
56 | set_setstr( &ic->set, "type", "chat" ); |
---|
57 | |
---|
58 | return ic; |
---|
59 | } |
---|
60 | |
---|
61 | irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name ) |
---|
62 | { |
---|
63 | GSList *l; |
---|
64 | |
---|
65 | for( l = irc->channels; l; l = l->next ) |
---|
66 | { |
---|
67 | irc_channel_t *ic = l->data; |
---|
68 | |
---|
69 | if( irc_channel_name_cmp( name, ic->name ) == 0 ) |
---|
70 | return ic; |
---|
71 | } |
---|
72 | |
---|
73 | return NULL; |
---|
74 | } |
---|
75 | |
---|
76 | irc_channel_t *irc_channel_get( irc_t *irc, char *id ) |
---|
77 | { |
---|
78 | irc_channel_t *ic, *ret = NULL; |
---|
79 | GSList *l; |
---|
80 | int nr; |
---|
81 | |
---|
82 | if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) |
---|
83 | { |
---|
84 | for( l = irc->channels; l; l = l->next ) |
---|
85 | { |
---|
86 | ic = l->data; |
---|
87 | if( ( nr-- ) == 0 ) |
---|
88 | return ic; |
---|
89 | } |
---|
90 | |
---|
91 | return NULL; |
---|
92 | } |
---|
93 | |
---|
94 | /* Exact match first: Partial match only sucks if there's a channel |
---|
95 | #aa and #aabb */ |
---|
96 | if( ( ret = irc_channel_by_name( irc, id ) ) ) |
---|
97 | return ret; |
---|
98 | |
---|
99 | for( l = irc->channels; l; l = l->next ) |
---|
100 | { |
---|
101 | ic = l->data; |
---|
102 | |
---|
103 | if( strstr( ic->name, id ) ) |
---|
104 | { |
---|
105 | /* Make sure it's a unique match. */ |
---|
106 | if( !ret ) |
---|
107 | ret = ic; |
---|
108 | else |
---|
109 | return NULL; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | return ret; |
---|
114 | } |
---|
115 | |
---|
116 | int irc_channel_free( irc_channel_t *ic ) |
---|
117 | { |
---|
118 | irc_t *irc = ic->irc; |
---|
119 | |
---|
120 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
121 | irc_channel_del_user( ic, irc->user, IRC_CDU_KICK, "Cleaning up channel" ); |
---|
122 | |
---|
123 | if( ic->f->_free ) |
---|
124 | ic->f->_free( ic ); |
---|
125 | |
---|
126 | while( ic->set ) |
---|
127 | set_del( &ic->set, ic->set->key ); |
---|
128 | |
---|
129 | irc->channels = g_slist_remove( irc->channels, ic ); |
---|
130 | while( ic->users ) |
---|
131 | { |
---|
132 | g_free( ic->users->data ); |
---|
133 | ic->users = g_slist_remove( ic->users, ic->users->data ); |
---|
134 | } |
---|
135 | |
---|
136 | g_free( ic->name ); |
---|
137 | g_free( ic->topic ); |
---|
138 | g_free( ic->topic_who ); |
---|
139 | g_free( ic ); |
---|
140 | |
---|
141 | return 1; |
---|
142 | } |
---|
143 | |
---|
144 | struct irc_channel_free_data |
---|
145 | { |
---|
146 | irc_t *irc; |
---|
147 | irc_channel_t *ic; |
---|
148 | char *name; |
---|
149 | }; |
---|
150 | |
---|
151 | static gboolean irc_channel_free_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
152 | { |
---|
153 | struct irc_channel_free_data *d = data; |
---|
154 | |
---|
155 | if( g_slist_find( irc_connection_list, d->irc ) && |
---|
156 | irc_channel_by_name( d->irc, d->name ) == d->ic && |
---|
157 | !( d->ic->flags & IRC_CHANNEL_JOINED ) ) |
---|
158 | irc_channel_free( d->ic ); |
---|
159 | |
---|
160 | g_free( d->name ); |
---|
161 | g_free( d ); |
---|
162 | return FALSE; |
---|
163 | } |
---|
164 | |
---|
165 | /* Free the channel, but via the event loop, so after finishing whatever event |
---|
166 | we're currently handling. */ |
---|
167 | void irc_channel_free_soon( irc_channel_t *ic ) |
---|
168 | { |
---|
169 | struct irc_channel_free_data *d = g_new0( struct irc_channel_free_data, 1 ); |
---|
170 | |
---|
171 | d->irc = ic->irc; |
---|
172 | d->ic = ic; |
---|
173 | d->name = g_strdup( ic->name ); |
---|
174 | |
---|
175 | b_timeout_add( 0, irc_channel_free_callback, d ); |
---|
176 | } |
---|
177 | |
---|
178 | static char *set_eval_channel_type( set_t *set, char *value ) |
---|
179 | { |
---|
180 | struct irc_channel *ic = set->data; |
---|
181 | const struct irc_channel_funcs *new; |
---|
182 | |
---|
183 | if( strcmp( value, "control" ) == 0 ) |
---|
184 | new = &control_channel_funcs; |
---|
185 | else if( strcmp( value, "chat" ) == 0 ) |
---|
186 | new = &irc_channel_im_chat_funcs; |
---|
187 | else |
---|
188 | return SET_INVALID; |
---|
189 | |
---|
190 | /* TODO: Return values. */ |
---|
191 | if( ic->f && ic->f->_free ) |
---|
192 | ic->f->_free( ic ); |
---|
193 | |
---|
194 | ic->f = new; |
---|
195 | |
---|
196 | if( ic->f && ic->f->_init ) |
---|
197 | ic->f->_init( ic ); |
---|
198 | |
---|
199 | return value; |
---|
200 | } |
---|
201 | |
---|
202 | int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
203 | { |
---|
204 | irc_channel_user_t *icu; |
---|
205 | |
---|
206 | if( irc_channel_has_user( ic, iu ) ) |
---|
207 | return 0; |
---|
208 | |
---|
209 | icu = g_new0( irc_channel_user_t, 1 ); |
---|
210 | icu->iu = iu; |
---|
211 | |
---|
212 | ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp ); |
---|
213 | |
---|
214 | irc_channel_update_ops( ic, set_getstr( &ic->irc->b->set, "ops" ) ); |
---|
215 | |
---|
216 | if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED ) |
---|
217 | { |
---|
218 | ic->flags |= IRC_CHANNEL_JOINED; |
---|
219 | irc_send_join( ic, iu ); |
---|
220 | } |
---|
221 | |
---|
222 | return 1; |
---|
223 | } |
---|
224 | |
---|
225 | int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu, irc_channel_del_user_type_t type, const char *msg ) |
---|
226 | { |
---|
227 | irc_channel_user_t *icu; |
---|
228 | |
---|
229 | if( !( icu = irc_channel_has_user( ic, iu ) ) ) |
---|
230 | return 0; |
---|
231 | |
---|
232 | ic->users = g_slist_remove( ic->users, icu ); |
---|
233 | g_free( icu ); |
---|
234 | |
---|
235 | if( !( ic->flags & IRC_CHANNEL_JOINED ) || type == IRC_CDU_SILENT ) {} |
---|
236 | /* Do nothing. The caller should promise it won't screw |
---|
237 | up state of the IRC client. :-) */ |
---|
238 | else if( type == IRC_CDU_PART ) |
---|
239 | irc_send_part( ic, iu, msg ); |
---|
240 | else if( type == IRC_CDU_KICK ) |
---|
241 | irc_send_kick( ic, iu, ic->irc->root, msg ); |
---|
242 | |
---|
243 | if( iu == ic->irc->user ) |
---|
244 | { |
---|
245 | ic->flags &= ~IRC_CHANNEL_JOINED; |
---|
246 | |
---|
247 | if( ic->irc->status & USTATUS_SHUTDOWN ) |
---|
248 | { |
---|
249 | /* Don't do anything fancy when we're shutting down anyway. */ |
---|
250 | } |
---|
251 | else if( ic->flags & IRC_CHANNEL_TEMP ) |
---|
252 | { |
---|
253 | irc_channel_free_soon( ic ); |
---|
254 | } |
---|
255 | else |
---|
256 | { |
---|
257 | /* Flush userlist now. The user won't see it anyway. */ |
---|
258 | while( ic->users ) |
---|
259 | { |
---|
260 | g_free( ic->users->data ); |
---|
261 | ic->users = g_slist_remove( ic->users, ic->users->data ); |
---|
262 | } |
---|
263 | irc_channel_add_user( ic, ic->irc->root ); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | return 1; |
---|
268 | } |
---|
269 | |
---|
270 | irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
271 | { |
---|
272 | GSList *l; |
---|
273 | |
---|
274 | for( l = ic->users; l; l = l->next ) |
---|
275 | { |
---|
276 | irc_channel_user_t *icu = l->data; |
---|
277 | |
---|
278 | if( icu->iu == iu ) |
---|
279 | return icu; |
---|
280 | } |
---|
281 | |
---|
282 | return NULL; |
---|
283 | } |
---|
284 | |
---|
285 | int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu ) |
---|
286 | { |
---|
287 | g_free( ic->topic ); |
---|
288 | ic->topic = g_strdup( topic ); |
---|
289 | |
---|
290 | g_free( ic->topic_who ); |
---|
291 | if( iu ) |
---|
292 | ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host ); |
---|
293 | else |
---|
294 | ic->topic_who = NULL; |
---|
295 | |
---|
296 | ic->topic_time = time( NULL ); |
---|
297 | |
---|
298 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
299 | irc_send_topic( ic, TRUE ); |
---|
300 | |
---|
301 | return 1; |
---|
302 | } |
---|
303 | |
---|
304 | void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags ) |
---|
305 | { |
---|
306 | irc_channel_user_t *icu = irc_channel_has_user( ic, iu ); |
---|
307 | |
---|
308 | if( !icu || icu->flags == flags ) |
---|
309 | return; |
---|
310 | |
---|
311 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
312 | irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags ); |
---|
313 | |
---|
314 | icu->flags = flags; |
---|
315 | } |
---|
316 | |
---|
317 | void irc_channel_auto_joins( irc_t *irc, account_t *acc ) |
---|
318 | { |
---|
319 | GSList *l; |
---|
320 | |
---|
321 | for( l = irc->channels; l; l = l->next ) |
---|
322 | { |
---|
323 | irc_channel_t *ic = l->data; |
---|
324 | gboolean aj = set_getbool( &ic->set, "auto_join" ); |
---|
325 | char *type; |
---|
326 | |
---|
327 | if( acc && |
---|
328 | ( type = set_getstr( &ic->set, "chat_type" ) ) && |
---|
329 | strcmp( type, "room" ) == 0 ) |
---|
330 | { |
---|
331 | /* Bit of an ugly special case: Handle chatrooms here, we |
---|
332 | can only auto-join them if their account is online. */ |
---|
333 | char *acc_s; |
---|
334 | |
---|
335 | if( !aj && !( ic->flags & IRC_CHANNEL_JOINED ) ) |
---|
336 | /* Only continue if this one's marked as auto_join |
---|
337 | or if we're in it already. (Possible if the |
---|
338 | client auto-rejoined it before identyfing.) */ |
---|
339 | continue; |
---|
340 | else if( !( acc_s = set_getstr( &ic->set, "account" ) ) ) |
---|
341 | continue; |
---|
342 | else if( account_get( irc->b, acc_s ) != acc ) |
---|
343 | continue; |
---|
344 | else if( acc->ic == NULL || !( acc->ic->flags & OPT_LOGGED_IN ) ) |
---|
345 | continue; |
---|
346 | else |
---|
347 | ic->f->join( ic ); |
---|
348 | } |
---|
349 | else if( aj ) |
---|
350 | { |
---|
351 | irc_channel_add_user( ic, irc->user ); |
---|
352 | } |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | void irc_channel_printf( irc_channel_t *ic, char *format, ... ) |
---|
357 | { |
---|
358 | va_list params; |
---|
359 | char *text; |
---|
360 | |
---|
361 | va_start( params, format ); |
---|
362 | text = g_strdup_vprintf( format, params ); |
---|
363 | va_end( params ); |
---|
364 | |
---|
365 | irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL ); |
---|
366 | g_free( text ); |
---|
367 | } |
---|
368 | |
---|
369 | gboolean irc_channel_name_ok( const char *name_ ) |
---|
370 | { |
---|
371 | const unsigned char *name = (unsigned char*) name_; |
---|
372 | int i; |
---|
373 | |
---|
374 | if( name_[0] == '\0' ) |
---|
375 | return FALSE; |
---|
376 | |
---|
377 | /* Check if the first character is in CTYPES (#&) */ |
---|
378 | if( strchr( CTYPES, name_[0] ) == NULL ) |
---|
379 | return FALSE; |
---|
380 | |
---|
381 | /* RFC 1459 keeps amazing me: While only a "few" chars are allowed |
---|
382 | in nicknames, channel names can be pretty much anything as long |
---|
383 | as they start with # or &. I'll be a little bit more strict and |
---|
384 | disallow all non-printable characters. */ |
---|
385 | for( i = 1; name[i]; i ++ ) |
---|
386 | if( name[i] <= ' ' || name[i] == ',' ) |
---|
387 | return FALSE; |
---|
388 | |
---|
389 | return TRUE; |
---|
390 | } |
---|
391 | |
---|
392 | void irc_channel_name_strip( char *name ) |
---|
393 | { |
---|
394 | int i, j; |
---|
395 | |
---|
396 | for( i = j = 0; name[i]; i ++ ) |
---|
397 | if( name[i] > ' ' && name[i] != ',' ) |
---|
398 | name[j++] = name[i]; |
---|
399 | |
---|
400 | name[j] = '\0'; |
---|
401 | } |
---|
402 | |
---|
403 | int irc_channel_name_cmp( const char *a_, const char *b_ ) |
---|
404 | { |
---|
405 | static unsigned char case_map[256]; |
---|
406 | const unsigned char *a = (unsigned char*) a_, *b = (unsigned char*) b_; |
---|
407 | int i; |
---|
408 | |
---|
409 | if( case_map['A'] == '\0' ) |
---|
410 | { |
---|
411 | for( i = 33; i < 256; i ++ ) |
---|
412 | if( i != ',' ) |
---|
413 | case_map[i] = i; |
---|
414 | |
---|
415 | for( i = 0; i < 26; i ++ ) |
---|
416 | case_map['A'+i] = 'a' + i; |
---|
417 | |
---|
418 | case_map['['] = '{'; |
---|
419 | case_map[']'] = '}'; |
---|
420 | case_map['~'] = '`'; |
---|
421 | case_map['\\'] = '|'; |
---|
422 | } |
---|
423 | |
---|
424 | if( !irc_channel_name_ok( a_ ) || !irc_channel_name_ok( b_ ) ) |
---|
425 | return -1; |
---|
426 | |
---|
427 | for( i = 0; a[i] && b[i] && case_map[a[i]] && case_map[b[i]]; i ++ ) |
---|
428 | { |
---|
429 | if( case_map[a[i]] == case_map[b[i]] ) |
---|
430 | continue; |
---|
431 | else |
---|
432 | return case_map[a[i]] - case_map[b[i]]; |
---|
433 | } |
---|
434 | |
---|
435 | return case_map[a[i]] - case_map[b[i]]; |
---|
436 | } |
---|
437 | |
---|
438 | static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
439 | { |
---|
440 | const irc_channel_user_t *a = a_, *b = b_; |
---|
441 | |
---|
442 | return irc_user_cmp( a->iu, b->iu ); |
---|
443 | } |
---|
444 | |
---|
445 | void irc_channel_update_ops( irc_channel_t *ic, char *value ) |
---|
446 | { |
---|
447 | irc_channel_user_set_mode( ic, ic->irc->root, |
---|
448 | ( strcmp( value, "both" ) == 0 || |
---|
449 | strcmp( value, "root" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 ); |
---|
450 | irc_channel_user_set_mode( ic, ic->irc->user, |
---|
451 | ( strcmp( value, "both" ) == 0 || |
---|
452 | strcmp( value, "user" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 ); |
---|
453 | } |
---|
454 | |
---|
455 | char *set_eval_irc_channel_ops( set_t *set, char *value ) |
---|
456 | { |
---|
457 | irc_t *irc = set->data; |
---|
458 | GSList *l; |
---|
459 | |
---|
460 | if( strcmp( value, "both" ) != 0 && strcmp( value, "none" ) != 0 && |
---|
461 | strcmp( value, "user" ) != 0 && strcmp( value, "root" ) != 0 ) |
---|
462 | return SET_INVALID; |
---|
463 | |
---|
464 | for( l = irc->channels; l; l = l->next ) |
---|
465 | irc_channel_update_ops( l->data, value ); |
---|
466 | |
---|
467 | return value; |
---|
468 | } |
---|
469 | |
---|
470 | /* Channel-type dependent functions, for control channels: */ |
---|
471 | static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg ) |
---|
472 | { |
---|
473 | irc_t *irc = ic->irc; |
---|
474 | const char *s; |
---|
475 | |
---|
476 | /* Scan for non-whitespace chars followed by a colon: */ |
---|
477 | for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {} |
---|
478 | |
---|
479 | if( *s == ':' || *s == ',' ) |
---|
480 | { |
---|
481 | char to[s-msg+1]; |
---|
482 | irc_user_t *iu; |
---|
483 | |
---|
484 | memset( to, 0, sizeof( to ) ); |
---|
485 | strncpy( to, msg, s - msg ); |
---|
486 | while( *(++s) && isspace( *s ) ) {} |
---|
487 | |
---|
488 | iu = irc_user_by_name( irc, to ); |
---|
489 | if( iu && iu->f->privmsg ) |
---|
490 | { |
---|
491 | iu->last_channel = ic; |
---|
492 | iu->f->privmsg( iu, s ); |
---|
493 | } |
---|
494 | else |
---|
495 | { |
---|
496 | irc_channel_printf( ic, "User does not exist: %s", to ); |
---|
497 | } |
---|
498 | } |
---|
499 | else |
---|
500 | { |
---|
501 | /* TODO: Maybe just use root->privmsg here now? */ |
---|
502 | char cmd[strlen(msg)+1]; |
---|
503 | |
---|
504 | g_free( ic->irc->last_root_cmd ); |
---|
505 | ic->irc->last_root_cmd = g_strdup( ic->name ); |
---|
506 | |
---|
507 | strcpy( cmd, msg ); |
---|
508 | root_command_string( ic->irc, cmd ); |
---|
509 | } |
---|
510 | |
---|
511 | return TRUE; |
---|
512 | } |
---|
513 | |
---|
514 | static gboolean control_channel_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
515 | { |
---|
516 | struct irc_control_channel *icc = ic->data; |
---|
517 | bee_user_t *bu = iu->bu; |
---|
518 | |
---|
519 | if( bu == NULL ) |
---|
520 | return FALSE; |
---|
521 | |
---|
522 | if( icc->type != IRC_CC_TYPE_GROUP ) |
---|
523 | { |
---|
524 | irc_send_num( ic->irc, 482, "%s :Invitations are only possible to fill_by=group channels", ic->name ); |
---|
525 | return FALSE; |
---|
526 | } |
---|
527 | |
---|
528 | bu->ic->acc->prpl->add_buddy( bu->ic, bu->handle, |
---|
529 | icc->group ? icc->group->name : NULL ); |
---|
530 | |
---|
531 | return TRUE; |
---|
532 | } |
---|
533 | |
---|
534 | static char *set_eval_by_account( set_t *set, char *value ); |
---|
535 | static char *set_eval_fill_by( set_t *set, char *value ); |
---|
536 | static char *set_eval_by_group( set_t *set, char *value ); |
---|
537 | static char *set_eval_by_protocol( set_t *set, char *value ); |
---|
538 | static char *set_eval_show_users( set_t *set, char *value ); |
---|
539 | |
---|
540 | static gboolean control_channel_init( irc_channel_t *ic ) |
---|
541 | { |
---|
542 | struct irc_control_channel *icc; |
---|
543 | |
---|
544 | set_add( &ic->set, "account", NULL, set_eval_by_account, ic ); |
---|
545 | set_add( &ic->set, "fill_by", "all", set_eval_fill_by, ic ); |
---|
546 | set_add( &ic->set, "group", NULL, set_eval_by_group, ic ); |
---|
547 | set_add( &ic->set, "protocol", NULL, set_eval_by_protocol, ic ); |
---|
548 | |
---|
549 | /* When changing the default, also change it below. */ |
---|
550 | set_add( &ic->set, "show_users", "online+,away", set_eval_show_users, ic ); |
---|
551 | |
---|
552 | ic->data = icc = g_new0( struct irc_control_channel, 1 ); |
---|
553 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
554 | |
---|
555 | /* Have to run the evaluator to initialize icc->modes. */ |
---|
556 | set_setstr( &ic->set, "show_users", "online+,away" ); |
---|
557 | |
---|
558 | return TRUE; |
---|
559 | } |
---|
560 | |
---|
561 | static gboolean control_channel_join( irc_channel_t *ic ) |
---|
562 | { |
---|
563 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
564 | |
---|
565 | return TRUE; |
---|
566 | } |
---|
567 | |
---|
568 | static char *set_eval_by_account( set_t *set, char *value ) |
---|
569 | { |
---|
570 | struct irc_channel *ic = set->data; |
---|
571 | struct irc_control_channel *icc = ic->data; |
---|
572 | account_t *acc; |
---|
573 | |
---|
574 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
575 | return SET_INVALID; |
---|
576 | |
---|
577 | icc->account = acc; |
---|
578 | if( icc->type == IRC_CC_TYPE_ACCOUNT ) |
---|
579 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
580 | |
---|
581 | return g_strdup( acc->tag ); |
---|
582 | } |
---|
583 | |
---|
584 | static char *set_eval_fill_by( set_t *set, char *value ) |
---|
585 | { |
---|
586 | struct irc_channel *ic = set->data; |
---|
587 | struct irc_control_channel *icc = ic->data; |
---|
588 | |
---|
589 | if( strcmp( value, "all" ) == 0 ) |
---|
590 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
591 | else if( strcmp( value, "rest" ) == 0 ) |
---|
592 | icc->type = IRC_CC_TYPE_REST; |
---|
593 | else if( strcmp( value, "group" ) == 0 ) |
---|
594 | icc->type = IRC_CC_TYPE_GROUP; |
---|
595 | else if( strcmp( value, "account" ) == 0 ) |
---|
596 | icc->type = IRC_CC_TYPE_ACCOUNT; |
---|
597 | else if( strcmp( value, "protocol" ) == 0 ) |
---|
598 | icc->type = IRC_CC_TYPE_PROTOCOL; |
---|
599 | else |
---|
600 | return SET_INVALID; |
---|
601 | |
---|
602 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
603 | return value; |
---|
604 | } |
---|
605 | |
---|
606 | static char *set_eval_by_group( set_t *set, char *value ) |
---|
607 | { |
---|
608 | struct irc_channel *ic = set->data; |
---|
609 | struct irc_control_channel *icc = ic->data; |
---|
610 | |
---|
611 | icc->group = bee_group_by_name( ic->irc->b, value, TRUE ); |
---|
612 | if( icc->type == IRC_CC_TYPE_GROUP ) |
---|
613 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
614 | |
---|
615 | return g_strdup( icc->group->name ); |
---|
616 | } |
---|
617 | |
---|
618 | static char *set_eval_by_protocol( set_t *set, char *value ) |
---|
619 | { |
---|
620 | struct irc_channel *ic = set->data; |
---|
621 | struct irc_control_channel *icc = ic->data; |
---|
622 | struct prpl *prpl; |
---|
623 | |
---|
624 | if( !( prpl = find_protocol( value ) ) ) |
---|
625 | return SET_INVALID; |
---|
626 | |
---|
627 | icc->protocol = prpl; |
---|
628 | if( icc->type == IRC_CC_TYPE_PROTOCOL ) |
---|
629 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
630 | |
---|
631 | return value; |
---|
632 | } |
---|
633 | |
---|
634 | static char *set_eval_show_users( set_t *set, char *value ) |
---|
635 | { |
---|
636 | struct irc_channel *ic = set->data; |
---|
637 | struct irc_control_channel *icc = ic->data; |
---|
638 | char **parts = g_strsplit( value, ",", 0 ), **part; |
---|
639 | char modes[4]; |
---|
640 | |
---|
641 | memset( modes, 0, 4 ); |
---|
642 | for( part = parts; *part; part ++ ) |
---|
643 | { |
---|
644 | char last, modechar = IRC_CHANNEL_USER_NONE; |
---|
645 | |
---|
646 | if( **part == '\0' ) |
---|
647 | goto fail; |
---|
648 | |
---|
649 | last = (*part)[strlen(*part+1)]; |
---|
650 | if( last == '+' ) |
---|
651 | modechar = IRC_CHANNEL_USER_VOICE; |
---|
652 | else if( last == '%' ) |
---|
653 | modechar = IRC_CHANNEL_USER_HALFOP; |
---|
654 | else if( last == '@' ) |
---|
655 | modechar = IRC_CHANNEL_USER_OP; |
---|
656 | |
---|
657 | if( strncmp( *part, "offline", 7 ) == 0 ) |
---|
658 | modes[0] = modechar; |
---|
659 | else if( strncmp( *part, "away", 4 ) == 0 ) |
---|
660 | modes[1] = modechar; |
---|
661 | else if( strncmp( *part, "online", 6 ) == 0 ) |
---|
662 | modes[2] = modechar; |
---|
663 | else |
---|
664 | goto fail; |
---|
665 | } |
---|
666 | memcpy( icc->modes, modes, 4 ); |
---|
667 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
668 | |
---|
669 | g_strfreev( parts ); |
---|
670 | return value; |
---|
671 | |
---|
672 | fail: |
---|
673 | g_strfreev( parts ); |
---|
674 | return SET_INVALID; |
---|
675 | } |
---|
676 | |
---|
677 | static gboolean control_channel_free( irc_channel_t *ic ) |
---|
678 | { |
---|
679 | struct irc_control_channel *icc = ic->data; |
---|
680 | |
---|
681 | set_del( &ic->set, "account" ); |
---|
682 | set_del( &ic->set, "fill_by" ); |
---|
683 | set_del( &ic->set, "group" ); |
---|
684 | set_del( &ic->set, "protocol" ); |
---|
685 | |
---|
686 | g_free( icc ); |
---|
687 | ic->data = NULL; |
---|
688 | |
---|
689 | return TRUE; |
---|
690 | } |
---|
691 | |
---|
692 | static const struct irc_channel_funcs control_channel_funcs = { |
---|
693 | control_channel_privmsg, |
---|
694 | control_channel_join, |
---|
695 | NULL, |
---|
696 | NULL, |
---|
697 | control_channel_invite, |
---|
698 | |
---|
699 | control_channel_init, |
---|
700 | control_channel_free, |
---|
701 | }; |
---|