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