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