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, FALSE, "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, gboolean silent, 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 && !silent ) |
---|
236 | irc_send_part( ic, iu, msg ); |
---|
237 | |
---|
238 | if( iu == ic->irc->user ) |
---|
239 | { |
---|
240 | ic->flags &= ~IRC_CHANNEL_JOINED; |
---|
241 | |
---|
242 | if( ic->irc->status & USTATUS_SHUTDOWN ) |
---|
243 | { |
---|
244 | /* Don't do anything fancy when we're shutting down anyway. */ |
---|
245 | } |
---|
246 | else if( ic->flags & IRC_CHANNEL_TEMP ) |
---|
247 | { |
---|
248 | irc_channel_free_soon( ic ); |
---|
249 | } |
---|
250 | else |
---|
251 | { |
---|
252 | /* Flush userlist now. The user won't see it anyway. */ |
---|
253 | while( ic->users ) |
---|
254 | { |
---|
255 | g_free( ic->users->data ); |
---|
256 | ic->users = g_slist_remove( ic->users, ic->users->data ); |
---|
257 | } |
---|
258 | irc_channel_add_user( ic, ic->irc->root ); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | return 1; |
---|
263 | } |
---|
264 | |
---|
265 | irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
266 | { |
---|
267 | GSList *l; |
---|
268 | |
---|
269 | for( l = ic->users; l; l = l->next ) |
---|
270 | { |
---|
271 | irc_channel_user_t *icu = l->data; |
---|
272 | |
---|
273 | if( icu->iu == iu ) |
---|
274 | return icu; |
---|
275 | } |
---|
276 | |
---|
277 | return NULL; |
---|
278 | } |
---|
279 | |
---|
280 | int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu ) |
---|
281 | { |
---|
282 | g_free( ic->topic ); |
---|
283 | ic->topic = g_strdup( topic ); |
---|
284 | |
---|
285 | g_free( ic->topic_who ); |
---|
286 | if( iu ) |
---|
287 | ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host ); |
---|
288 | else |
---|
289 | ic->topic_who = NULL; |
---|
290 | |
---|
291 | ic->topic_time = time( NULL ); |
---|
292 | |
---|
293 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
294 | irc_send_topic( ic, TRUE ); |
---|
295 | |
---|
296 | return 1; |
---|
297 | } |
---|
298 | |
---|
299 | void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags ) |
---|
300 | { |
---|
301 | irc_channel_user_t *icu = irc_channel_has_user( ic, iu ); |
---|
302 | |
---|
303 | if( !icu || icu->flags == flags ) |
---|
304 | return; |
---|
305 | |
---|
306 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
307 | irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags ); |
---|
308 | |
---|
309 | icu->flags = flags; |
---|
310 | } |
---|
311 | |
---|
312 | void irc_channel_auto_joins( irc_t *irc, account_t *acc ) |
---|
313 | { |
---|
314 | GSList *l; |
---|
315 | |
---|
316 | for( l = irc->channels; l; l = l->next ) |
---|
317 | { |
---|
318 | irc_channel_t *ic = l->data; |
---|
319 | gboolean aj = set_getbool( &ic->set, "auto_join" ); |
---|
320 | char *type; |
---|
321 | |
---|
322 | if( acc && |
---|
323 | ( type = set_getstr( &ic->set, "chat_type" ) ) && |
---|
324 | strcmp( type, "room" ) == 0 ) |
---|
325 | { |
---|
326 | /* Bit of an ugly special case: Handle chatrooms here, we |
---|
327 | can only auto-join them if their account is online. */ |
---|
328 | char *acc_s; |
---|
329 | |
---|
330 | if( !aj && !( ic->flags & IRC_CHANNEL_JOINED ) ) |
---|
331 | /* Only continue if this one's marked as auto_join |
---|
332 | or if we're in it already. (Possible if the |
---|
333 | client auto-rejoined it before identyfing.) */ |
---|
334 | continue; |
---|
335 | else if( !( acc_s = set_getstr( &ic->set, "account" ) ) ) |
---|
336 | continue; |
---|
337 | else if( account_get( irc->b, acc_s ) != acc ) |
---|
338 | continue; |
---|
339 | else if( acc->ic == NULL || !( acc->ic->flags & OPT_LOGGED_IN ) ) |
---|
340 | continue; |
---|
341 | else |
---|
342 | ic->f->join( ic ); |
---|
343 | } |
---|
344 | else if( aj ) |
---|
345 | { |
---|
346 | irc_channel_add_user( ic, irc->user ); |
---|
347 | } |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | void irc_channel_printf( irc_channel_t *ic, char *format, ... ) |
---|
352 | { |
---|
353 | va_list params; |
---|
354 | char *text; |
---|
355 | |
---|
356 | va_start( params, format ); |
---|
357 | text = g_strdup_vprintf( format, params ); |
---|
358 | va_end( params ); |
---|
359 | |
---|
360 | irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL ); |
---|
361 | g_free( text ); |
---|
362 | } |
---|
363 | |
---|
364 | gboolean irc_channel_name_ok( const char *name_ ) |
---|
365 | { |
---|
366 | const unsigned char *name = (unsigned char*) name_; |
---|
367 | int i; |
---|
368 | |
---|
369 | if( name_[0] == '\0' ) |
---|
370 | return FALSE; |
---|
371 | |
---|
372 | /* Check if the first character is in CTYPES (#&) */ |
---|
373 | if( strchr( CTYPES, name_[0] ) == NULL ) |
---|
374 | return FALSE; |
---|
375 | |
---|
376 | /* RFC 1459 keeps amazing me: While only a "few" chars are allowed |
---|
377 | in nicknames, channel names can be pretty much anything as long |
---|
378 | as they start with # or &. I'll be a little bit more strict and |
---|
379 | disallow all non-printable characters. */ |
---|
380 | for( i = 1; name[i]; i ++ ) |
---|
381 | if( name[i] <= ' ' || name[i] == ',' ) |
---|
382 | return FALSE; |
---|
383 | |
---|
384 | return TRUE; |
---|
385 | } |
---|
386 | |
---|
387 | void irc_channel_name_strip( char *name ) |
---|
388 | { |
---|
389 | int i, j; |
---|
390 | |
---|
391 | for( i = j = 0; name[i]; i ++ ) |
---|
392 | if( name[i] > ' ' && name[i] != ',' ) |
---|
393 | name[j++] = name[i]; |
---|
394 | |
---|
395 | name[j] = '\0'; |
---|
396 | } |
---|
397 | |
---|
398 | int irc_channel_name_cmp( const char *a_, const char *b_ ) |
---|
399 | { |
---|
400 | static unsigned char case_map[256]; |
---|
401 | const unsigned char *a = (unsigned char*) a_, *b = (unsigned char*) b_; |
---|
402 | int i; |
---|
403 | |
---|
404 | if( case_map['A'] == '\0' ) |
---|
405 | { |
---|
406 | for( i = 33; i < 256; i ++ ) |
---|
407 | if( i != ',' ) |
---|
408 | case_map[i] = i; |
---|
409 | |
---|
410 | for( i = 0; i < 26; i ++ ) |
---|
411 | case_map['A'+i] = 'a' + i; |
---|
412 | |
---|
413 | case_map['['] = '{'; |
---|
414 | case_map[']'] = '}'; |
---|
415 | case_map['~'] = '`'; |
---|
416 | case_map['\\'] = '|'; |
---|
417 | } |
---|
418 | |
---|
419 | if( !irc_channel_name_ok( a_ ) || !irc_channel_name_ok( b_ ) ) |
---|
420 | return -1; |
---|
421 | |
---|
422 | for( i = 0; a[i] && b[i] && case_map[a[i]] && case_map[b[i]]; i ++ ) |
---|
423 | { |
---|
424 | if( case_map[a[i]] == case_map[b[i]] ) |
---|
425 | continue; |
---|
426 | else |
---|
427 | return case_map[a[i]] - case_map[b[i]]; |
---|
428 | } |
---|
429 | |
---|
430 | return case_map[a[i]] - case_map[b[i]]; |
---|
431 | } |
---|
432 | |
---|
433 | static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
434 | { |
---|
435 | const irc_channel_user_t *a = a_, *b = b_; |
---|
436 | |
---|
437 | return irc_user_cmp( a->iu, b->iu ); |
---|
438 | } |
---|
439 | |
---|
440 | void irc_channel_update_ops( irc_channel_t *ic, char *value ) |
---|
441 | { |
---|
442 | irc_channel_user_set_mode( ic, ic->irc->root, |
---|
443 | ( strcmp( value, "both" ) == 0 || |
---|
444 | strcmp( value, "root" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 ); |
---|
445 | irc_channel_user_set_mode( ic, ic->irc->user, |
---|
446 | ( strcmp( value, "both" ) == 0 || |
---|
447 | strcmp( value, "user" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 ); |
---|
448 | } |
---|
449 | |
---|
450 | char *set_eval_irc_channel_ops( set_t *set, char *value ) |
---|
451 | { |
---|
452 | irc_t *irc = set->data; |
---|
453 | GSList *l; |
---|
454 | |
---|
455 | if( strcmp( value, "both" ) != 0 && strcmp( value, "none" ) != 0 && |
---|
456 | strcmp( value, "user" ) != 0 && strcmp( value, "root" ) != 0 ) |
---|
457 | return SET_INVALID; |
---|
458 | |
---|
459 | for( l = irc->channels; l; l = l->next ) |
---|
460 | irc_channel_update_ops( l->data, value ); |
---|
461 | |
---|
462 | return value; |
---|
463 | } |
---|
464 | |
---|
465 | /* Channel-type dependent functions, for control channels: */ |
---|
466 | static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg ) |
---|
467 | { |
---|
468 | irc_t *irc = ic->irc; |
---|
469 | const char *s; |
---|
470 | |
---|
471 | /* Scan for non-whitespace chars followed by a colon: */ |
---|
472 | for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {} |
---|
473 | |
---|
474 | if( *s == ':' || *s == ',' ) |
---|
475 | { |
---|
476 | char to[s-msg+1]; |
---|
477 | irc_user_t *iu; |
---|
478 | |
---|
479 | memset( to, 0, sizeof( to ) ); |
---|
480 | strncpy( to, msg, s - msg ); |
---|
481 | while( *(++s) && isspace( *s ) ) {} |
---|
482 | |
---|
483 | iu = irc_user_by_name( irc, to ); |
---|
484 | if( iu && iu->f->privmsg ) |
---|
485 | { |
---|
486 | iu->last_channel = ic; |
---|
487 | iu->f->privmsg( iu, s ); |
---|
488 | } |
---|
489 | else |
---|
490 | { |
---|
491 | irc_channel_printf( ic, "User does not exist: %s", to ); |
---|
492 | } |
---|
493 | } |
---|
494 | else |
---|
495 | { |
---|
496 | /* TODO: Maybe just use root->privmsg here now? */ |
---|
497 | char cmd[strlen(msg)+1]; |
---|
498 | |
---|
499 | g_free( ic->irc->last_root_cmd ); |
---|
500 | ic->irc->last_root_cmd = g_strdup( ic->name ); |
---|
501 | |
---|
502 | strcpy( cmd, msg ); |
---|
503 | root_command_string( ic->irc, cmd ); |
---|
504 | } |
---|
505 | |
---|
506 | return TRUE; |
---|
507 | } |
---|
508 | |
---|
509 | static gboolean control_channel_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
510 | { |
---|
511 | struct irc_control_channel *icc = ic->data; |
---|
512 | bee_user_t *bu = iu->bu; |
---|
513 | |
---|
514 | if( bu == NULL ) |
---|
515 | return FALSE; |
---|
516 | |
---|
517 | if( icc->type != IRC_CC_TYPE_GROUP ) |
---|
518 | { |
---|
519 | irc_send_num( ic->irc, 482, "%s :Invitations are only possible to fill_by=group channels", ic->name ); |
---|
520 | return FALSE; |
---|
521 | } |
---|
522 | |
---|
523 | bu->ic->acc->prpl->add_buddy( bu->ic, bu->handle, |
---|
524 | icc->group ? icc->group->name : NULL ); |
---|
525 | |
---|
526 | return TRUE; |
---|
527 | } |
---|
528 | |
---|
529 | static char *set_eval_by_account( set_t *set, char *value ); |
---|
530 | static char *set_eval_fill_by( set_t *set, char *value ); |
---|
531 | static char *set_eval_by_group( set_t *set, char *value ); |
---|
532 | static char *set_eval_by_protocol( set_t *set, char *value ); |
---|
533 | |
---|
534 | static gboolean control_channel_init( irc_channel_t *ic ) |
---|
535 | { |
---|
536 | struct irc_control_channel *icc; |
---|
537 | |
---|
538 | set_add( &ic->set, "account", NULL, set_eval_by_account, ic ); |
---|
539 | set_add( &ic->set, "fill_by", "all", set_eval_fill_by, ic ); |
---|
540 | set_add( &ic->set, "group", NULL, set_eval_by_group, ic ); |
---|
541 | set_add( &ic->set, "protocol", NULL, set_eval_by_protocol, ic ); |
---|
542 | |
---|
543 | ic->data = icc = g_new0( struct irc_control_channel, 1 ); |
---|
544 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
545 | |
---|
546 | if( bee_group_by_name( ic->irc->b, ic->name + 1, FALSE ) ) |
---|
547 | { |
---|
548 | set_setstr( &ic->set, "group", ic->name + 1 ); |
---|
549 | set_setstr( &ic->set, "fill_by", "group" ); |
---|
550 | } |
---|
551 | else if( set_setstr( &ic->set, "protocol", ic->name + 1 ) ) |
---|
552 | { |
---|
553 | set_setstr( &ic->set, "fill_by", "protocol" ); |
---|
554 | } |
---|
555 | else if( set_setstr( &ic->set, "account", ic->name + 1 ) ) |
---|
556 | { |
---|
557 | set_setstr( &ic->set, "fill_by", "account" ); |
---|
558 | } |
---|
559 | else |
---|
560 | { |
---|
561 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
562 | } |
---|
563 | |
---|
564 | return TRUE; |
---|
565 | } |
---|
566 | |
---|
567 | static gboolean control_channel_join( irc_channel_t *ic ) |
---|
568 | { |
---|
569 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
570 | |
---|
571 | return TRUE; |
---|
572 | } |
---|
573 | |
---|
574 | static char *set_eval_by_account( set_t *set, char *value ) |
---|
575 | { |
---|
576 | struct irc_channel *ic = set->data; |
---|
577 | struct irc_control_channel *icc = ic->data; |
---|
578 | account_t *acc; |
---|
579 | |
---|
580 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
581 | return SET_INVALID; |
---|
582 | |
---|
583 | icc->account = acc; |
---|
584 | if( icc->type == IRC_CC_TYPE_ACCOUNT ) |
---|
585 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
586 | |
---|
587 | return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user ); |
---|
588 | } |
---|
589 | |
---|
590 | static char *set_eval_fill_by( set_t *set, char *value ) |
---|
591 | { |
---|
592 | struct irc_channel *ic = set->data; |
---|
593 | struct irc_control_channel *icc = ic->data; |
---|
594 | |
---|
595 | if( strcmp( value, "all" ) == 0 ) |
---|
596 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
597 | else if( strcmp( value, "rest" ) == 0 ) |
---|
598 | icc->type = IRC_CC_TYPE_REST; |
---|
599 | else if( strcmp( value, "group" ) == 0 ) |
---|
600 | icc->type = IRC_CC_TYPE_GROUP; |
---|
601 | else if( strcmp( value, "account" ) == 0 ) |
---|
602 | icc->type = IRC_CC_TYPE_ACCOUNT; |
---|
603 | else if( strcmp( value, "protocol" ) == 0 ) |
---|
604 | icc->type = IRC_CC_TYPE_PROTOCOL; |
---|
605 | else |
---|
606 | return SET_INVALID; |
---|
607 | |
---|
608 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
609 | return value; |
---|
610 | } |
---|
611 | |
---|
612 | static char *set_eval_by_group( set_t *set, char *value ) |
---|
613 | { |
---|
614 | struct irc_channel *ic = set->data; |
---|
615 | struct irc_control_channel *icc = ic->data; |
---|
616 | |
---|
617 | icc->group = bee_group_by_name( ic->irc->b, value, TRUE ); |
---|
618 | if( icc->type == IRC_CC_TYPE_GROUP ) |
---|
619 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
620 | |
---|
621 | return g_strdup( icc->group->name ); |
---|
622 | } |
---|
623 | |
---|
624 | static char *set_eval_by_protocol( set_t *set, char *value ) |
---|
625 | { |
---|
626 | struct irc_channel *ic = set->data; |
---|
627 | struct irc_control_channel *icc = ic->data; |
---|
628 | struct prpl *prpl; |
---|
629 | |
---|
630 | if( !( prpl = find_protocol( value ) ) ) |
---|
631 | return SET_INVALID; |
---|
632 | |
---|
633 | icc->protocol = prpl; |
---|
634 | if( icc->type == IRC_CC_TYPE_PROTOCOL ) |
---|
635 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
636 | |
---|
637 | return value; |
---|
638 | } |
---|
639 | |
---|
640 | static gboolean control_channel_free( irc_channel_t *ic ) |
---|
641 | { |
---|
642 | struct irc_control_channel *icc = ic->data; |
---|
643 | |
---|
644 | set_del( &ic->set, "account" ); |
---|
645 | set_del( &ic->set, "fill_by" ); |
---|
646 | set_del( &ic->set, "group" ); |
---|
647 | set_del( &ic->set, "protocol" ); |
---|
648 | |
---|
649 | g_free( icc ); |
---|
650 | ic->data = NULL; |
---|
651 | |
---|
652 | return TRUE; |
---|
653 | } |
---|
654 | |
---|
655 | static const struct irc_channel_funcs control_channel_funcs = { |
---|
656 | control_channel_privmsg, |
---|
657 | control_channel_join, |
---|
658 | NULL, |
---|
659 | NULL, |
---|
660 | control_channel_invite, |
---|
661 | |
---|
662 | control_channel_init, |
---|
663 | control_channel_free, |
---|
664 | }; |
---|