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, "type", "control", set_eval_channel_type, ic ); |
---|
51 | |
---|
52 | if( name[0] == '&' ) |
---|
53 | set_setstr( &ic->set, "type", "control" ); |
---|
54 | else /* if( name[0] == '#' ) */ |
---|
55 | set_setstr( &ic->set, "type", "chat" ); |
---|
56 | |
---|
57 | return ic; |
---|
58 | } |
---|
59 | |
---|
60 | irc_channel_t *irc_channel_by_name( irc_t *irc, const char *name ) |
---|
61 | { |
---|
62 | GSList *l; |
---|
63 | |
---|
64 | for( l = irc->channels; l; l = l->next ) |
---|
65 | { |
---|
66 | irc_channel_t *ic = l->data; |
---|
67 | |
---|
68 | if( irc_channel_name_cmp( name, ic->name ) == 0 ) |
---|
69 | return ic; |
---|
70 | } |
---|
71 | |
---|
72 | return NULL; |
---|
73 | } |
---|
74 | |
---|
75 | irc_channel_t *irc_channel_get( irc_t *irc, char *id ) |
---|
76 | { |
---|
77 | irc_channel_t *ic, *ret = NULL; |
---|
78 | GSList *l; |
---|
79 | int nr; |
---|
80 | |
---|
81 | if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) |
---|
82 | { |
---|
83 | for( l = irc->channels; l; l = l->next ) |
---|
84 | { |
---|
85 | ic = l->data; |
---|
86 | if( ( nr-- ) == 0 ) |
---|
87 | return ic; |
---|
88 | } |
---|
89 | |
---|
90 | return NULL; |
---|
91 | } |
---|
92 | |
---|
93 | /* Exact match first: Partial match only sucks if there's a channel |
---|
94 | #aa and #aabb */ |
---|
95 | if( ( ret = irc_channel_by_name( irc, id ) ) ) |
---|
96 | return ret; |
---|
97 | |
---|
98 | for( l = irc->channels; l; l = l->next ) |
---|
99 | { |
---|
100 | ic = l->data; |
---|
101 | |
---|
102 | if( strstr( ic->name, id ) ) |
---|
103 | { |
---|
104 | /* Make sure it's a unique match. */ |
---|
105 | if( !ret ) |
---|
106 | ret = ic; |
---|
107 | else |
---|
108 | return NULL; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | return ret; |
---|
113 | } |
---|
114 | |
---|
115 | int irc_channel_free( irc_channel_t *ic ) |
---|
116 | { |
---|
117 | irc_t *irc = ic->irc; |
---|
118 | |
---|
119 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
120 | irc_channel_del_user( ic, irc->user, FALSE, "Cleaning up channel" ); |
---|
121 | |
---|
122 | if( ic->f->_free ) |
---|
123 | ic->f->_free( ic ); |
---|
124 | |
---|
125 | while( ic->set ) |
---|
126 | set_del( &ic->set, ic->set->key ); |
---|
127 | |
---|
128 | irc->channels = g_slist_remove( irc->channels, ic ); |
---|
129 | while( ic->users ) |
---|
130 | { |
---|
131 | g_free( ic->users->data ); |
---|
132 | ic->users = g_slist_remove( ic->users, ic->users->data ); |
---|
133 | } |
---|
134 | |
---|
135 | g_free( ic->name ); |
---|
136 | g_free( ic->topic ); |
---|
137 | g_free( ic->topic_who ); |
---|
138 | g_free( ic ); |
---|
139 | |
---|
140 | return 1; |
---|
141 | } |
---|
142 | |
---|
143 | static char *set_eval_channel_type( set_t *set, char *value ) |
---|
144 | { |
---|
145 | struct irc_channel *ic = set->data; |
---|
146 | const struct irc_channel_funcs *new; |
---|
147 | |
---|
148 | if( strcmp( value, "control" ) == 0 ) |
---|
149 | new = &control_channel_funcs; |
---|
150 | else if( strcmp( value, "chat" ) == 0 ) |
---|
151 | new = &irc_channel_im_chat_funcs; |
---|
152 | else |
---|
153 | return SET_INVALID; |
---|
154 | |
---|
155 | /* TODO: Return values. */ |
---|
156 | if( ic->f && ic->f->_free ) |
---|
157 | ic->f->_free( ic ); |
---|
158 | |
---|
159 | ic->f = new; |
---|
160 | |
---|
161 | if( ic->f && ic->f->_init ) |
---|
162 | ic->f->_init( ic ); |
---|
163 | |
---|
164 | return value; |
---|
165 | } |
---|
166 | |
---|
167 | int irc_channel_add_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
168 | { |
---|
169 | irc_channel_user_t *icu; |
---|
170 | |
---|
171 | if( irc_channel_has_user( ic, iu ) ) |
---|
172 | return 0; |
---|
173 | |
---|
174 | icu = g_new0( irc_channel_user_t, 1 ); |
---|
175 | icu->iu = iu; |
---|
176 | |
---|
177 | ic->users = g_slist_insert_sorted( ic->users, icu, irc_channel_user_cmp ); |
---|
178 | |
---|
179 | irc_channel_update_ops( ic, set_getstr( &ic->irc->b->set, "ops" ) ); |
---|
180 | |
---|
181 | if( iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED ) |
---|
182 | { |
---|
183 | ic->flags |= IRC_CHANNEL_JOINED; |
---|
184 | irc_send_join( ic, iu ); |
---|
185 | } |
---|
186 | |
---|
187 | return 1; |
---|
188 | } |
---|
189 | |
---|
190 | int irc_channel_del_user( irc_channel_t *ic, irc_user_t *iu, gboolean silent, const char *msg ) |
---|
191 | { |
---|
192 | irc_channel_user_t *icu; |
---|
193 | |
---|
194 | if( !( icu = irc_channel_has_user( ic, iu ) ) ) |
---|
195 | return 0; |
---|
196 | |
---|
197 | ic->users = g_slist_remove( ic->users, icu ); |
---|
198 | g_free( icu ); |
---|
199 | |
---|
200 | if( ic->flags & IRC_CHANNEL_JOINED && !silent ) |
---|
201 | irc_send_part( ic, iu, msg ); |
---|
202 | |
---|
203 | if( iu == ic->irc->user ) |
---|
204 | { |
---|
205 | ic->flags &= ~IRC_CHANNEL_JOINED; |
---|
206 | |
---|
207 | if( ic->flags & IRC_CHANNEL_TEMP ) |
---|
208 | irc_channel_free( ic ); |
---|
209 | } |
---|
210 | |
---|
211 | return 1; |
---|
212 | } |
---|
213 | |
---|
214 | irc_channel_user_t *irc_channel_has_user( irc_channel_t *ic, irc_user_t *iu ) |
---|
215 | { |
---|
216 | GSList *l; |
---|
217 | |
---|
218 | for( l = ic->users; l; l = l->next ) |
---|
219 | { |
---|
220 | irc_channel_user_t *icu = l->data; |
---|
221 | |
---|
222 | if( icu->iu == iu ) |
---|
223 | return icu; |
---|
224 | } |
---|
225 | |
---|
226 | return NULL; |
---|
227 | } |
---|
228 | |
---|
229 | int irc_channel_set_topic( irc_channel_t *ic, const char *topic, const irc_user_t *iu ) |
---|
230 | { |
---|
231 | g_free( ic->topic ); |
---|
232 | ic->topic = g_strdup( topic ); |
---|
233 | |
---|
234 | g_free( ic->topic_who ); |
---|
235 | if( iu ) |
---|
236 | ic->topic_who = g_strdup_printf( "%s!%s@%s", iu->nick, iu->user, iu->host ); |
---|
237 | else |
---|
238 | ic->topic_who = NULL; |
---|
239 | |
---|
240 | ic->topic_time = time( NULL ); |
---|
241 | |
---|
242 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
243 | irc_send_topic( ic, TRUE ); |
---|
244 | |
---|
245 | return 1; |
---|
246 | } |
---|
247 | |
---|
248 | void irc_channel_user_set_mode( irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags ) |
---|
249 | { |
---|
250 | irc_channel_user_t *icu = irc_channel_has_user( ic, iu ); |
---|
251 | |
---|
252 | if( !icu || icu->flags == flags ) |
---|
253 | return; |
---|
254 | |
---|
255 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
256 | irc_send_channel_user_mode_diff( ic, iu, icu->flags, flags ); |
---|
257 | |
---|
258 | icu->flags = flags; |
---|
259 | } |
---|
260 | |
---|
261 | void irc_channel_printf( irc_channel_t *ic, char *format, ... ) |
---|
262 | { |
---|
263 | va_list params; |
---|
264 | char *text; |
---|
265 | |
---|
266 | va_start( params, format ); |
---|
267 | text = g_strdup_vprintf( format, params ); |
---|
268 | va_end( params ); |
---|
269 | |
---|
270 | irc_send_msg( ic->irc->root, "PRIVMSG", ic->name, text, NULL ); |
---|
271 | g_free( text ); |
---|
272 | } |
---|
273 | |
---|
274 | gboolean irc_channel_name_ok( const char *name_ ) |
---|
275 | { |
---|
276 | const unsigned char *name = (unsigned char*) name_; |
---|
277 | int i; |
---|
278 | |
---|
279 | /* Check if the first character is in CTYPES (#&) */ |
---|
280 | if( strchr( CTYPES, name_[0] ) == NULL ) |
---|
281 | return FALSE; |
---|
282 | |
---|
283 | /* RFC 1459 keeps amazing me: While only a "few" chars are allowed |
---|
284 | in nicknames, channel names can be pretty much anything as long |
---|
285 | as they start with # or &. I'll be a little bit more strict and |
---|
286 | disallow all non-printable characters. */ |
---|
287 | for( i = 1; name[i]; i ++ ) |
---|
288 | if( name[i] <= ' ' || name[i] == ',' ) |
---|
289 | return FALSE; |
---|
290 | |
---|
291 | return TRUE; |
---|
292 | } |
---|
293 | |
---|
294 | void irc_channel_name_strip( char *name ) |
---|
295 | { |
---|
296 | int i, j; |
---|
297 | |
---|
298 | for( i = j = 0; name[i]; i ++ ) |
---|
299 | if( name[i] > ' ' && name[i] != ',' ) |
---|
300 | name[j++] = name[i]; |
---|
301 | |
---|
302 | name[j] = '\0'; |
---|
303 | } |
---|
304 | |
---|
305 | int irc_channel_name_cmp( const char *a_, const char *b_ ) |
---|
306 | { |
---|
307 | static unsigned char case_map[256]; |
---|
308 | const unsigned char *a = (unsigned char*) a_, *b = (unsigned char*) b_; |
---|
309 | int i; |
---|
310 | |
---|
311 | if( case_map['A'] == '\0' ) |
---|
312 | { |
---|
313 | for( i = 33; i < 256; i ++ ) |
---|
314 | if( i != ',' ) |
---|
315 | case_map[i] = i; |
---|
316 | |
---|
317 | for( i = 0; i < 26; i ++ ) |
---|
318 | case_map['A'+i] = 'a' + i; |
---|
319 | |
---|
320 | case_map['['] = '{'; |
---|
321 | case_map[']'] = '}'; |
---|
322 | case_map['~'] = '`'; |
---|
323 | case_map['\\'] = '|'; |
---|
324 | } |
---|
325 | |
---|
326 | if( !irc_channel_name_ok( a_ ) || !irc_channel_name_ok( b_ ) ) |
---|
327 | return -1; |
---|
328 | |
---|
329 | for( i = 0; a[i] && b[i] && case_map[a[i]] && case_map[b[i]]; i ++ ) |
---|
330 | { |
---|
331 | if( case_map[a[i]] == case_map[b[i]] ) |
---|
332 | continue; |
---|
333 | else |
---|
334 | return case_map[a[i]] - case_map[b[i]]; |
---|
335 | } |
---|
336 | |
---|
337 | return case_map[a[i]] - case_map[b[i]]; |
---|
338 | } |
---|
339 | |
---|
340 | static gint irc_channel_user_cmp( gconstpointer a_, gconstpointer b_ ) |
---|
341 | { |
---|
342 | const irc_channel_user_t *a = a_, *b = b_; |
---|
343 | |
---|
344 | return irc_user_cmp( a->iu, b->iu ); |
---|
345 | } |
---|
346 | |
---|
347 | void irc_channel_update_ops( irc_channel_t *ic, char *value ) |
---|
348 | { |
---|
349 | irc_channel_user_set_mode( ic, ic->irc->root, |
---|
350 | ( strcmp( value, "both" ) == 0 || |
---|
351 | strcmp( value, "root" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 ); |
---|
352 | irc_channel_user_set_mode( ic, ic->irc->user, |
---|
353 | ( strcmp( value, "both" ) == 0 || |
---|
354 | strcmp( value, "user" ) == 0 ) ? IRC_CHANNEL_USER_OP : 0 ); |
---|
355 | } |
---|
356 | |
---|
357 | char *set_eval_irc_channel_ops( set_t *set, char *value ) |
---|
358 | { |
---|
359 | irc_t *irc = set->data; |
---|
360 | GSList *l; |
---|
361 | |
---|
362 | if( strcmp( value, "both" ) != 0 && strcmp( value, "none" ) != 0 && |
---|
363 | strcmp( value, "user" ) != 0 && strcmp( value, "root" ) != 0 ) |
---|
364 | return SET_INVALID; |
---|
365 | |
---|
366 | for( l = irc->channels; l; l = l->next ) |
---|
367 | irc_channel_update_ops( l->data, value ); |
---|
368 | |
---|
369 | return value; |
---|
370 | } |
---|
371 | |
---|
372 | /* Channel-type dependent functions, for control channels: */ |
---|
373 | static gboolean control_channel_privmsg( irc_channel_t *ic, const char *msg ) |
---|
374 | { |
---|
375 | irc_t *irc = ic->irc; |
---|
376 | const char *s; |
---|
377 | |
---|
378 | /* Scan for non-whitespace chars followed by a colon: */ |
---|
379 | for( s = msg; *s && !isspace( *s ) && *s != ':' && *s != ','; s ++ ) {} |
---|
380 | |
---|
381 | if( *s == ':' || *s == ',' ) |
---|
382 | { |
---|
383 | char to[s-msg+1]; |
---|
384 | irc_user_t *iu; |
---|
385 | |
---|
386 | memset( to, 0, sizeof( to ) ); |
---|
387 | strncpy( to, msg, s - msg ); |
---|
388 | while( *(++s) && isspace( *s ) ) {} |
---|
389 | |
---|
390 | iu = irc_user_by_name( irc, to ); |
---|
391 | if( iu && iu->f->privmsg ) |
---|
392 | { |
---|
393 | iu->last_channel = ic; |
---|
394 | iu->f->privmsg( iu, s ); |
---|
395 | } |
---|
396 | else |
---|
397 | { |
---|
398 | irc_channel_printf( ic, "User does not exist: %s", to ); |
---|
399 | } |
---|
400 | } |
---|
401 | else |
---|
402 | { |
---|
403 | /* TODO: Maybe just use root->privmsg here now? */ |
---|
404 | char cmd[strlen(msg)+1]; |
---|
405 | |
---|
406 | g_free( ic->irc->last_root_cmd ); |
---|
407 | ic->irc->last_root_cmd = g_strdup( ic->name ); |
---|
408 | |
---|
409 | strcpy( cmd, msg ); |
---|
410 | root_command_string( ic->irc, cmd ); |
---|
411 | } |
---|
412 | |
---|
413 | return TRUE; |
---|
414 | } |
---|
415 | |
---|
416 | static gboolean control_channel_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
417 | { |
---|
418 | struct irc_control_channel *icc = ic->data; |
---|
419 | bee_user_t *bu = iu->bu; |
---|
420 | |
---|
421 | if( bu == NULL ) |
---|
422 | return FALSE; |
---|
423 | |
---|
424 | if( icc->type != IRC_CC_TYPE_GROUP ) |
---|
425 | { |
---|
426 | irc_send_num( ic->irc, 482, "%s :Invitations are only possible to fill_by=group channels", ic->name ); |
---|
427 | return FALSE; |
---|
428 | } |
---|
429 | |
---|
430 | bu->ic->acc->prpl->add_buddy( bu->ic, bu->handle, |
---|
431 | icc->group ? icc->group->name : NULL ); |
---|
432 | |
---|
433 | return TRUE; |
---|
434 | } |
---|
435 | |
---|
436 | static char *set_eval_by_account( set_t *set, char *value ); |
---|
437 | static char *set_eval_fill_by( set_t *set, char *value ); |
---|
438 | static char *set_eval_by_group( set_t *set, char *value ); |
---|
439 | |
---|
440 | static gboolean control_channel_init( irc_channel_t *ic ) |
---|
441 | { |
---|
442 | struct irc_control_channel *icc; |
---|
443 | |
---|
444 | set_add( &ic->set, "account", NULL, set_eval_by_account, ic ); |
---|
445 | set_add( &ic->set, "fill_by", "all", set_eval_fill_by, ic ); |
---|
446 | set_add( &ic->set, "group", NULL, set_eval_by_group, ic ); |
---|
447 | |
---|
448 | ic->data = icc = g_new0( struct irc_control_channel, 1 ); |
---|
449 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
450 | |
---|
451 | if( bee_group_by_name( ic->irc->b, ic->name + 1, FALSE ) ) |
---|
452 | { |
---|
453 | set_setstr( &ic->set, "group", ic->name + 1 ); |
---|
454 | set_setstr( &ic->set, "fill_by", "group" ); |
---|
455 | } |
---|
456 | else if( set_setstr( &ic->set, "account", ic->name + 1 ) ) |
---|
457 | { |
---|
458 | set_setstr( &ic->set, "fill_by", "account" ); |
---|
459 | } |
---|
460 | else |
---|
461 | { |
---|
462 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
463 | } |
---|
464 | |
---|
465 | return TRUE; |
---|
466 | } |
---|
467 | |
---|
468 | static char *set_eval_by_account( set_t *set, char *value ) |
---|
469 | { |
---|
470 | struct irc_channel *ic = set->data; |
---|
471 | struct irc_control_channel *icc = ic->data; |
---|
472 | account_t *acc; |
---|
473 | |
---|
474 | if( !( acc = account_get( ic->irc->b, value ) ) ) |
---|
475 | return SET_INVALID; |
---|
476 | |
---|
477 | icc->account = acc; |
---|
478 | if( icc->type == IRC_CC_TYPE_ACCOUNT ) |
---|
479 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
480 | return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user ); |
---|
481 | } |
---|
482 | |
---|
483 | static char *set_eval_fill_by( set_t *set, char *value ) |
---|
484 | { |
---|
485 | struct irc_channel *ic = set->data; |
---|
486 | struct irc_control_channel *icc = ic->data; |
---|
487 | |
---|
488 | if( strcmp( value, "all" ) == 0 ) |
---|
489 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
490 | else if( strcmp( value, "rest" ) == 0 ) |
---|
491 | icc->type = IRC_CC_TYPE_REST; |
---|
492 | else if( strcmp( value, "group" ) == 0 ) |
---|
493 | icc->type = IRC_CC_TYPE_GROUP; |
---|
494 | else if( strcmp( value, "account" ) == 0 ) |
---|
495 | icc->type = IRC_CC_TYPE_ACCOUNT; |
---|
496 | else |
---|
497 | return SET_INVALID; |
---|
498 | |
---|
499 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
500 | return value; |
---|
501 | } |
---|
502 | |
---|
503 | static char *set_eval_by_group( set_t *set, char *value ) |
---|
504 | { |
---|
505 | struct irc_channel *ic = set->data; |
---|
506 | struct irc_control_channel *icc = ic->data; |
---|
507 | |
---|
508 | icc->group = bee_group_by_name( ic->irc->b, value, TRUE ); |
---|
509 | if( icc->type == IRC_CC_TYPE_GROUP ) |
---|
510 | bee_irc_channel_update( ic->irc, ic, NULL ); |
---|
511 | return g_strdup( icc->group->name ); |
---|
512 | } |
---|
513 | |
---|
514 | static gboolean control_channel_free( irc_channel_t *ic ) |
---|
515 | { |
---|
516 | struct irc_control_channel *icc = ic->data; |
---|
517 | |
---|
518 | set_del( &ic->set, "account" ); |
---|
519 | set_del( &ic->set, "fill_by" ); |
---|
520 | set_del( &ic->set, "group" ); |
---|
521 | |
---|
522 | g_free( icc ); |
---|
523 | ic->data = NULL; |
---|
524 | |
---|
525 | return TRUE; |
---|
526 | } |
---|
527 | |
---|
528 | static const struct irc_channel_funcs control_channel_funcs = { |
---|
529 | control_channel_privmsg, |
---|
530 | NULL, |
---|
531 | NULL, |
---|
532 | NULL, |
---|
533 | control_channel_invite, |
---|
534 | |
---|
535 | control_channel_init, |
---|
536 | control_channel_free, |
---|
537 | }; |
---|