1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2013 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., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 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 | set_t *s; |
---|
38 | |
---|
39 | if (!irc_channel_name_ok(name) || irc_channel_by_name(irc, name)) { |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | |
---|
43 | ic = g_new0(irc_channel_t, 1); |
---|
44 | ic->irc = irc; |
---|
45 | ic->name = g_strdup(name); |
---|
46 | strcpy(ic->mode, CMODE); |
---|
47 | |
---|
48 | irc_channel_add_user(ic, irc->root); |
---|
49 | |
---|
50 | irc->channels = g_slist_append(irc->channels, ic); |
---|
51 | |
---|
52 | set_add(&ic->set, "auto_join", "false", set_eval_bool, ic); |
---|
53 | |
---|
54 | s = set_add(&ic->set, "type", "control", set_eval_channel_type, ic); |
---|
55 | s->flags |= SET_NOSAVE; /* Layer violation (XML format detail) */ |
---|
56 | |
---|
57 | if (name[0] == '&') { |
---|
58 | set_setstr(&ic->set, "type", "control"); |
---|
59 | } else { /* if( name[0] == '#' ) */ |
---|
60 | set_setstr(&ic->set, "type", "chat"); |
---|
61 | } |
---|
62 | |
---|
63 | return ic; |
---|
64 | } |
---|
65 | |
---|
66 | irc_channel_t *irc_channel_by_name(irc_t *irc, const char *name) |
---|
67 | { |
---|
68 | GSList *l; |
---|
69 | |
---|
70 | for (l = irc->channels; l; l = l->next) { |
---|
71 | irc_channel_t *ic = l->data; |
---|
72 | |
---|
73 | if (irc_channel_name_cmp(name, ic->name) == 0) { |
---|
74 | return ic; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | return NULL; |
---|
79 | } |
---|
80 | |
---|
81 | irc_channel_t *irc_channel_get(irc_t *irc, char *id) |
---|
82 | { |
---|
83 | irc_channel_t *ic, *ret = NULL; |
---|
84 | GSList *l; |
---|
85 | int nr; |
---|
86 | |
---|
87 | if (sscanf(id, "%d", &nr) == 1 && nr < 1000) { |
---|
88 | for (l = irc->channels; l; l = l->next) { |
---|
89 | ic = l->data; |
---|
90 | if ((nr--) == 0) { |
---|
91 | return ic; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | return NULL; |
---|
96 | } |
---|
97 | |
---|
98 | /* Exact match first: Partial match only sucks if there's a channel |
---|
99 | #aa and #aabb */ |
---|
100 | if ((ret = irc_channel_by_name(irc, id))) { |
---|
101 | return ret; |
---|
102 | } |
---|
103 | |
---|
104 | for (l = irc->channels; l; l = l->next) { |
---|
105 | ic = l->data; |
---|
106 | |
---|
107 | if (strstr(ic->name, id)) { |
---|
108 | /* Make sure it's a unique match. */ |
---|
109 | if (!ret) { |
---|
110 | ret = ic; |
---|
111 | } else { |
---|
112 | return NULL; |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | return ret; |
---|
118 | } |
---|
119 | |
---|
120 | int irc_channel_free(irc_channel_t *ic) |
---|
121 | { |
---|
122 | irc_t *irc; |
---|
123 | GSList *l; |
---|
124 | |
---|
125 | if (ic == NULL) { |
---|
126 | return 0; |
---|
127 | } |
---|
128 | irc = ic->irc; |
---|
129 | |
---|
130 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
131 | irc_channel_del_user(ic, irc->user, IRC_CDU_KICK, "Cleaning up channel"); |
---|
132 | } |
---|
133 | |
---|
134 | if (ic->f->_free) { |
---|
135 | ic->f->_free(ic); |
---|
136 | } |
---|
137 | |
---|
138 | while (ic->set) { |
---|
139 | set_del(&ic->set, ic->set->key); |
---|
140 | } |
---|
141 | |
---|
142 | irc->channels = g_slist_remove(irc->channels, ic); |
---|
143 | while (ic->users) { |
---|
144 | g_free(ic->users->data); |
---|
145 | ic->users = g_slist_remove(ic->users, ic->users->data); |
---|
146 | } |
---|
147 | |
---|
148 | for (l = irc->users; l; l = l->next) { |
---|
149 | irc_user_t *iu = l->data; |
---|
150 | |
---|
151 | if (iu->last_channel == ic) { |
---|
152 | iu->last_channel = irc->default_channel; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | if (ic->pastebuf_timer) { |
---|
157 | b_event_remove(ic->pastebuf_timer); |
---|
158 | } |
---|
159 | |
---|
160 | g_free(ic->name); |
---|
161 | g_free(ic->topic); |
---|
162 | g_free(ic->topic_who); |
---|
163 | g_free(ic); |
---|
164 | |
---|
165 | return 1; |
---|
166 | } |
---|
167 | |
---|
168 | struct irc_channel_free_data { |
---|
169 | irc_t *irc; |
---|
170 | irc_channel_t *ic; |
---|
171 | char *name; |
---|
172 | }; |
---|
173 | |
---|
174 | static gboolean irc_channel_free_callback(gpointer data, gint fd, b_input_condition cond) |
---|
175 | { |
---|
176 | struct irc_channel_free_data *d = data; |
---|
177 | |
---|
178 | if (g_slist_find(irc_connection_list, d->irc) && |
---|
179 | irc_channel_by_name(d->irc, d->name) == d->ic && |
---|
180 | !(d->ic->flags & IRC_CHANNEL_JOINED)) { |
---|
181 | irc_channel_free(d->ic); |
---|
182 | } |
---|
183 | |
---|
184 | g_free(d->name); |
---|
185 | g_free(d); |
---|
186 | return FALSE; |
---|
187 | } |
---|
188 | |
---|
189 | /* Free the channel, but via the event loop, so after finishing whatever event |
---|
190 | we're currently handling. */ |
---|
191 | void irc_channel_free_soon(irc_channel_t *ic) |
---|
192 | { |
---|
193 | struct irc_channel_free_data *d = g_new0(struct irc_channel_free_data, 1); |
---|
194 | |
---|
195 | d->irc = ic->irc; |
---|
196 | d->ic = ic; |
---|
197 | d->name = g_strdup(ic->name); |
---|
198 | |
---|
199 | b_timeout_add(0, irc_channel_free_callback, d); |
---|
200 | } |
---|
201 | |
---|
202 | static char *set_eval_channel_type(set_t *set, char *value) |
---|
203 | { |
---|
204 | struct irc_channel *ic = set->data; |
---|
205 | const struct irc_channel_funcs *new; |
---|
206 | |
---|
207 | if (strcmp(value, "control") == 0) { |
---|
208 | new = &control_channel_funcs; |
---|
209 | } else if (ic != ic->irc->default_channel && strcmp(value, "chat") == 0) { |
---|
210 | new = &irc_channel_im_chat_funcs; |
---|
211 | } else { |
---|
212 | return SET_INVALID; |
---|
213 | } |
---|
214 | |
---|
215 | /* TODO: Return values. */ |
---|
216 | if (ic->f && ic->f->_free) { |
---|
217 | ic->f->_free(ic); |
---|
218 | } |
---|
219 | |
---|
220 | ic->f = new; |
---|
221 | |
---|
222 | if (ic->f && ic->f->_init) { |
---|
223 | ic->f->_init(ic); |
---|
224 | } |
---|
225 | |
---|
226 | return value; |
---|
227 | } |
---|
228 | |
---|
229 | int irc_channel_add_user(irc_channel_t *ic, irc_user_t *iu) |
---|
230 | { |
---|
231 | irc_channel_user_t *icu; |
---|
232 | |
---|
233 | if (irc_channel_has_user(ic, iu)) { |
---|
234 | return 0; |
---|
235 | } |
---|
236 | |
---|
237 | icu = g_new0(irc_channel_user_t, 1); |
---|
238 | icu->iu = iu; |
---|
239 | |
---|
240 | ic->users = g_slist_insert_sorted(ic->users, icu, irc_channel_user_cmp); |
---|
241 | |
---|
242 | irc_channel_update_ops(ic, set_getstr(&ic->irc->b->set, "ops")); |
---|
243 | |
---|
244 | if (iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED) { |
---|
245 | ic->flags |= IRC_CHANNEL_JOINED; |
---|
246 | irc_send_join(ic, iu); |
---|
247 | } |
---|
248 | |
---|
249 | return 1; |
---|
250 | } |
---|
251 | |
---|
252 | int irc_channel_del_user(irc_channel_t *ic, irc_user_t *iu, irc_channel_del_user_type_t type, const char *msg) |
---|
253 | { |
---|
254 | irc_channel_user_t *icu; |
---|
255 | |
---|
256 | if (!(icu = irc_channel_has_user(ic, iu))) { |
---|
257 | return 0; |
---|
258 | } |
---|
259 | |
---|
260 | ic->users = g_slist_remove(ic->users, icu); |
---|
261 | g_free(icu); |
---|
262 | |
---|
263 | if (!(ic->flags & IRC_CHANNEL_JOINED) || type == IRC_CDU_SILENT) { |
---|
264 | } |
---|
265 | /* Do nothing. The caller should promise it won't screw |
---|
266 | up state of the IRC client. :-) */ |
---|
267 | else if (type == IRC_CDU_PART) { |
---|
268 | irc_send_part(ic, iu, msg); |
---|
269 | } else if (type == IRC_CDU_KICK) { |
---|
270 | irc_send_kick(ic, iu, ic->irc->root, msg); |
---|
271 | } |
---|
272 | |
---|
273 | if (iu == ic->irc->user) { |
---|
274 | ic->flags &= ~IRC_CHANNEL_JOINED; |
---|
275 | |
---|
276 | if (ic->irc->status & USTATUS_SHUTDOWN) { |
---|
277 | /* Don't do anything fancy when we're shutting down anyway. */ |
---|
278 | } else if (ic->flags & IRC_CHANNEL_TEMP) { |
---|
279 | irc_channel_free_soon(ic); |
---|
280 | } else { |
---|
281 | /* Flush userlist now. The user won't see it anyway. */ |
---|
282 | while (ic->users) { |
---|
283 | g_free(ic->users->data); |
---|
284 | ic->users = g_slist_remove(ic->users, ic->users->data); |
---|
285 | } |
---|
286 | irc_channel_add_user(ic, ic->irc->root); |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | return 1; |
---|
291 | } |
---|
292 | |
---|
293 | irc_channel_user_t *irc_channel_has_user(irc_channel_t *ic, irc_user_t *iu) |
---|
294 | { |
---|
295 | GSList *l; |
---|
296 | |
---|
297 | for (l = ic->users; l; l = l->next) { |
---|
298 | irc_channel_user_t *icu = l->data; |
---|
299 | |
---|
300 | if (icu->iu == iu) { |
---|
301 | return icu; |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | return NULL; |
---|
306 | } |
---|
307 | |
---|
308 | /* Find a channel we're currently in, that currently has iu in it. */ |
---|
309 | struct irc_channel *irc_channel_with_user(irc_t *irc, irc_user_t *iu) |
---|
310 | { |
---|
311 | GSList *l; |
---|
312 | |
---|
313 | for (l = irc->channels; l; l = l->next) { |
---|
314 | irc_channel_t *ic = l->data; |
---|
315 | |
---|
316 | if (strcmp(set_getstr(&ic->set, "type"), "control") != 0) { |
---|
317 | continue; |
---|
318 | } |
---|
319 | |
---|
320 | if ((ic->flags & IRC_CHANNEL_JOINED) && |
---|
321 | irc_channel_has_user(ic, iu)) { |
---|
322 | return ic; |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | /* If there was no match, try once more but just see if the user |
---|
327 | *would* be in the channel, i.e. if s/he were online. */ |
---|
328 | if (iu->bu == NULL) { |
---|
329 | return NULL; |
---|
330 | } |
---|
331 | |
---|
332 | for (l = irc->channels; l; l = l->next) { |
---|
333 | irc_channel_t *ic = l->data; |
---|
334 | |
---|
335 | if (strcmp(set_getstr(&ic->set, "type"), "control") != 0) { |
---|
336 | continue; |
---|
337 | } |
---|
338 | |
---|
339 | if ((ic->flags & IRC_CHANNEL_JOINED) && |
---|
340 | irc_channel_wants_user(ic, iu)) { |
---|
341 | return ic; |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | return NULL; |
---|
346 | } |
---|
347 | |
---|
348 | int irc_channel_set_topic(irc_channel_t *ic, const char *topic, const irc_user_t *iu) |
---|
349 | { |
---|
350 | g_free(ic->topic); |
---|
351 | ic->topic = g_strdup(topic); |
---|
352 | |
---|
353 | g_free(ic->topic_who); |
---|
354 | if (iu) { |
---|
355 | ic->topic_who = g_strdup_printf("%s!%s@%s", iu->nick, iu->user, iu->host); |
---|
356 | } else { |
---|
357 | ic->topic_who = NULL; |
---|
358 | } |
---|
359 | |
---|
360 | ic->topic_time = time(NULL); |
---|
361 | |
---|
362 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
363 | irc_send_topic(ic, TRUE); |
---|
364 | } |
---|
365 | |
---|
366 | return 1; |
---|
367 | } |
---|
368 | |
---|
369 | void irc_channel_user_set_mode(irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags) |
---|
370 | { |
---|
371 | irc_channel_user_t *icu = irc_channel_has_user(ic, iu); |
---|
372 | |
---|
373 | if (!icu || icu->flags == flags) { |
---|
374 | return; |
---|
375 | } |
---|
376 | |
---|
377 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
378 | irc_send_channel_user_mode_diff(ic, iu, icu->flags, flags); |
---|
379 | } |
---|
380 | |
---|
381 | icu->flags = flags; |
---|
382 | } |
---|
383 | |
---|
384 | void irc_channel_set_mode(irc_channel_t *ic, const char *s) |
---|
385 | { |
---|
386 | irc_t *irc = ic->irc; |
---|
387 | char m[128], st = 1; |
---|
388 | const char *t; |
---|
389 | int i; |
---|
390 | char changes[512], *p, st2 = 2; |
---|
391 | |
---|
392 | memset(m, 0, sizeof(m)); |
---|
393 | |
---|
394 | for (t = ic->mode; *t; t++) { |
---|
395 | if (*t < sizeof(m)) { |
---|
396 | m[(int) *t] = 1; |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | p = changes; |
---|
401 | for (t = s; *t; t++) { |
---|
402 | if (*t == '+' || *t == '-') { |
---|
403 | st = *t == '+'; |
---|
404 | } else if (strchr(CMODES, *t)) { |
---|
405 | if (m[(int) *t] != st) { |
---|
406 | if (st != st2) { |
---|
407 | st2 = st, *p++ = st ? '+' : '-'; |
---|
408 | } |
---|
409 | *p++ = *t; |
---|
410 | } |
---|
411 | m[(int) *t] = st; |
---|
412 | } |
---|
413 | } |
---|
414 | *p = '\0'; |
---|
415 | |
---|
416 | memset(ic->mode, 0, sizeof(ic->mode)); |
---|
417 | |
---|
418 | for (i = 'A'; i <= 'z' && strlen(ic->mode) < (sizeof(ic->mode) - 1); i++) { |
---|
419 | if (m[i]) { |
---|
420 | ic->mode[strlen(ic->mode)] = i; |
---|
421 | } |
---|
422 | } |
---|
423 | |
---|
424 | if (*changes && (ic->flags & IRC_CHANNEL_JOINED)) { |
---|
425 | irc_write(irc, ":%s!%s@%s MODE %s :%s", irc->root->nick, |
---|
426 | irc->root->user, irc->root->host, ic->name, |
---|
427 | changes); |
---|
428 | } |
---|
429 | } |
---|
430 | |
---|
431 | char irc_channel_user_get_prefix(irc_channel_user_t *icu) |
---|
432 | { |
---|
433 | if (icu->flags & IRC_CHANNEL_USER_OP) { |
---|
434 | return '@'; |
---|
435 | } else if (icu->flags & IRC_CHANNEL_USER_HALFOP) { |
---|
436 | return '%'; |
---|
437 | } else if (icu->flags & IRC_CHANNEL_USER_VOICE) { |
---|
438 | return '+'; |
---|
439 | } |
---|
440 | return 0; |
---|
441 | } |
---|
442 | |
---|
443 | void irc_channel_auto_joins(irc_t *irc, account_t *acc) |
---|
444 | { |
---|
445 | GSList *l; |
---|
446 | |
---|
447 | for (l = irc->channels; l; l = l->next) { |
---|
448 | irc_channel_t *ic = l->data; |
---|
449 | gboolean aj = set_getbool(&ic->set, "auto_join"); |
---|
450 | char *type; |
---|
451 | |
---|
452 | if (acc && |
---|
453 | (type = set_getstr(&ic->set, "chat_type")) && |
---|
454 | strcmp(type, "room") == 0) { |
---|
455 | /* Bit of an ugly special case: Handle chatrooms here, we |
---|
456 | can only auto-join them if their account is online. */ |
---|
457 | char *acc_s; |
---|
458 | |
---|
459 | if (!aj && !(ic->flags & IRC_CHANNEL_JOINED)) { |
---|
460 | /* Only proceed if this one's marked as auto_join |
---|
461 | or if we're in it already. (Very likely the IRC |
---|
462 | client auto-(re)joining at reconnect time.) */ |
---|
463 | continue; |
---|
464 | } else if (!(acc_s = set_getstr(&ic->set, "account"))) { |
---|
465 | continue; |
---|
466 | } else if (account_get(irc->b, acc_s) != acc) { |
---|
467 | continue; |
---|
468 | } else if (acc->ic == NULL || !(acc->ic->flags & OPT_LOGGED_IN)) { |
---|
469 | continue; |
---|
470 | } else { |
---|
471 | ic->f->join(ic); |
---|
472 | } |
---|
473 | } else if (aj) { |
---|
474 | irc_channel_add_user(ic, irc->user); |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | void irc_channel_printf(irc_channel_t *ic, char *format, ...) |
---|
480 | { |
---|
481 | va_list params; |
---|
482 | char *text; |
---|
483 | |
---|
484 | va_start(params, format); |
---|
485 | text = g_strdup_vprintf(format, params); |
---|
486 | va_end(params); |
---|
487 | |
---|
488 | irc_send_msg(ic->irc->root, "PRIVMSG", ic->name, text, NULL); |
---|
489 | g_free(text); |
---|
490 | } |
---|
491 | |
---|
492 | gboolean irc_channel_name_ok(const char *name_) |
---|
493 | { |
---|
494 | const unsigned char *name = (unsigned char *) name_; |
---|
495 | int i; |
---|
496 | |
---|
497 | if (name_[0] == '\0') { |
---|
498 | return FALSE; |
---|
499 | } |
---|
500 | |
---|
501 | /* Check if the first character is in CTYPES (#&) */ |
---|
502 | if (strchr(CTYPES, name_[0]) == NULL) { |
---|
503 | return FALSE; |
---|
504 | } |
---|
505 | |
---|
506 | /* RFC 1459 keeps amazing me: While only a "few" chars are allowed |
---|
507 | in nicknames, channel names can be pretty much anything as long |
---|
508 | as they start with # or &. I'll be a little bit more strict and |
---|
509 | disallow all non-printable characters. */ |
---|
510 | for (i = 1; name[i]; i++) { |
---|
511 | if (name[i] <= ' ' || name[i] == ',') { |
---|
512 | return FALSE; |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | return TRUE; |
---|
517 | } |
---|
518 | |
---|
519 | void irc_channel_name_strip(char *name) |
---|
520 | { |
---|
521 | int i, j; |
---|
522 | |
---|
523 | for (i = j = 0; name[i]; i++) { |
---|
524 | if (name[i] > ' ' && name[i] != ',') { |
---|
525 | name[j++] = name[i]; |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | name[j] = '\0'; |
---|
530 | } |
---|
531 | |
---|
532 | int irc_channel_name_cmp(const char *a_, const char *b_) |
---|
533 | { |
---|
534 | static unsigned char case_map[256]; |
---|
535 | const unsigned char *a = (unsigned char *) a_, *b = (unsigned char *) b_; |
---|
536 | int i; |
---|
537 | |
---|
538 | if (case_map['A'] == '\0') { |
---|
539 | for (i = 33; i < 256; i++) { |
---|
540 | if (i != ',') { |
---|
541 | case_map[i] = i; |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | for (i = 0; i < 26; i++) { |
---|
546 | case_map['A' + i] = 'a' + i; |
---|
547 | } |
---|
548 | |
---|
549 | case_map['['] = '{'; |
---|
550 | case_map[']'] = '}'; |
---|
551 | case_map['~'] = '`'; |
---|
552 | case_map['\\'] = '|'; |
---|
553 | } |
---|
554 | |
---|
555 | if (!irc_channel_name_ok(a_) || !irc_channel_name_ok(b_)) { |
---|
556 | return -1; |
---|
557 | } |
---|
558 | |
---|
559 | for (i = 0; a[i] && b[i] && case_map[a[i]] && case_map[b[i]]; i++) { |
---|
560 | if (case_map[a[i]] == case_map[b[i]]) { |
---|
561 | continue; |
---|
562 | } else { |
---|
563 | return case_map[a[i]] - case_map[b[i]]; |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | return case_map[a[i]] - case_map[b[i]]; |
---|
568 | } |
---|
569 | |
---|
570 | gboolean irc_channel_is_unused(irc_t *irc, char *name) |
---|
571 | { |
---|
572 | char *type, *chat_type; |
---|
573 | irc_channel_t *oic; |
---|
574 | |
---|
575 | if (!irc_channel_name_ok(name)) { |
---|
576 | return FALSE; |
---|
577 | } |
---|
578 | |
---|
579 | if (!(oic = irc_channel_by_name(irc, name))) { |
---|
580 | return TRUE; |
---|
581 | } |
---|
582 | |
---|
583 | type = set_getstr(&oic->set, "type"); |
---|
584 | chat_type = set_getstr(&oic->set, "chat_type"); |
---|
585 | |
---|
586 | if (type && chat_type && oic->data == FALSE && |
---|
587 | strcmp(type, "chat") == 0 && |
---|
588 | strcmp(chat_type, "groupchat") == 0) { |
---|
589 | /* There's a channel with this name already, but it looks |
---|
590 | like it's not in use yet. Most likely the IRC client |
---|
591 | rejoined the channel after a reconnect. Remove it so |
---|
592 | we can reuse its name. */ |
---|
593 | irc_channel_free(oic); |
---|
594 | return TRUE; |
---|
595 | } |
---|
596 | |
---|
597 | return FALSE; |
---|
598 | } |
---|
599 | |
---|
600 | char *irc_channel_name_gen(irc_t *irc, const char *hint) |
---|
601 | { |
---|
602 | char name[MAX_NICK_LENGTH + 1] = { 0 }; |
---|
603 | char *translit_name; |
---|
604 | gsize bytes_written; |
---|
605 | |
---|
606 | translit_name = g_convert_with_fallback(hint, -1, "ASCII//TRANSLIT", "UTF-8", "", NULL, &bytes_written, NULL); |
---|
607 | |
---|
608 | if (!translit_name) { |
---|
609 | /* Same thing as in nick_gen() in nick.c, try again without //TRANSLIT */ |
---|
610 | translit_name = g_convert_with_fallback(hint, -1, "ASCII", "UTF-8", "", NULL, &bytes_written, NULL); |
---|
611 | } |
---|
612 | |
---|
613 | if (!translit_name) { |
---|
614 | return NULL; |
---|
615 | } |
---|
616 | |
---|
617 | if (bytes_written > MAX_NICK_LENGTH) { |
---|
618 | translit_name[MAX_NICK_LENGTH] = '\0'; |
---|
619 | } |
---|
620 | |
---|
621 | name[0] = '#'; |
---|
622 | strncpy(name + 1, translit_name, MAX_NICK_LENGTH - 1); |
---|
623 | name[MAX_NICK_LENGTH] = '\0'; |
---|
624 | |
---|
625 | g_free(translit_name); |
---|
626 | |
---|
627 | irc_channel_name_strip(name); |
---|
628 | |
---|
629 | if (set_getbool(&irc->b->set, "lcnicks")) { |
---|
630 | nick_lc(irc, name + 1); |
---|
631 | } |
---|
632 | |
---|
633 | while (!irc_channel_is_unused(irc, name)) { |
---|
634 | underscore_dedupe(name); |
---|
635 | } |
---|
636 | |
---|
637 | return g_strdup(name); |
---|
638 | } |
---|
639 | |
---|
640 | gboolean irc_channel_name_hint(irc_channel_t *ic, const char *name) |
---|
641 | { |
---|
642 | irc_t *irc = ic->irc; |
---|
643 | char *full_name; |
---|
644 | |
---|
645 | /* Don't rename a channel if the user's in it already. */ |
---|
646 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
647 | return FALSE; |
---|
648 | } |
---|
649 | |
---|
650 | if (!(full_name = irc_channel_name_gen(irc, name))) { |
---|
651 | return FALSE; |
---|
652 | } |
---|
653 | |
---|
654 | g_free(ic->name); |
---|
655 | ic->name = full_name; |
---|
656 | |
---|
657 | return TRUE; |
---|
658 | } |
---|
659 | |
---|
660 | static gint irc_channel_user_cmp(gconstpointer a_, gconstpointer b_) |
---|
661 | { |
---|
662 | const irc_channel_user_t *a = a_, *b = b_; |
---|
663 | |
---|
664 | return irc_user_cmp(a->iu, b->iu); |
---|
665 | } |
---|
666 | |
---|
667 | void irc_channel_update_ops(irc_channel_t *ic, char *value) |
---|
668 | { |
---|
669 | irc_channel_user_set_mode(ic, ic->irc->root, |
---|
670 | (strcmp(value, "both") == 0 || |
---|
671 | strcmp(value, "root") == 0) ? IRC_CHANNEL_USER_OP : 0); |
---|
672 | irc_channel_user_set_mode(ic, ic->irc->user, |
---|
673 | (strcmp(value, "both") == 0 || |
---|
674 | strcmp(value, "user") == 0) ? IRC_CHANNEL_USER_OP : 0); |
---|
675 | } |
---|
676 | |
---|
677 | char *set_eval_irc_channel_ops(set_t *set, char *value) |
---|
678 | { |
---|
679 | irc_t *irc = set->data; |
---|
680 | GSList *l; |
---|
681 | |
---|
682 | if (strcmp(value, "both") != 0 && strcmp(value, "none") != 0 && |
---|
683 | strcmp(value, "user") != 0 && strcmp(value, "root") != 0) { |
---|
684 | return SET_INVALID; |
---|
685 | } |
---|
686 | |
---|
687 | for (l = irc->channels; l; l = l->next) { |
---|
688 | irc_channel_update_ops(l->data, value); |
---|
689 | } |
---|
690 | |
---|
691 | return value; |
---|
692 | } |
---|
693 | |
---|
694 | /* Channel-type dependent functions, for control channels: */ |
---|
695 | static gboolean control_channel_privmsg(irc_channel_t *ic, const char *msg) |
---|
696 | { |
---|
697 | irc_t *irc = ic->irc; |
---|
698 | irc_user_t *iu; |
---|
699 | const char *s; |
---|
700 | |
---|
701 | /* Scan for non-whitespace chars followed by a colon: */ |
---|
702 | for (s = msg; *s && !g_ascii_isspace(*s) && *s != ':' && *s != ','; s++) { |
---|
703 | } |
---|
704 | |
---|
705 | if (*s == ':' || *s == ',') { |
---|
706 | char to[s - msg + 1]; |
---|
707 | |
---|
708 | memset(to, 0, sizeof(to)); |
---|
709 | strncpy(to, msg, s - msg); |
---|
710 | while (*(++s) && g_ascii_isspace(*s)) { |
---|
711 | } |
---|
712 | msg = s; |
---|
713 | |
---|
714 | if (!(iu = irc_user_by_name(irc, to))) { |
---|
715 | irc_channel_printf(ic, "User does not exist: %s", to); |
---|
716 | } else { |
---|
717 | ic->last_target = iu; |
---|
718 | } |
---|
719 | } else if (g_strcasecmp(set_getstr(&irc->b->set, "default_target"), "last") == 0 && |
---|
720 | ic->last_target && g_slist_find(irc->users, ic->last_target)) { |
---|
721 | iu = ic->last_target; |
---|
722 | } else { |
---|
723 | iu = irc->root; |
---|
724 | } |
---|
725 | |
---|
726 | if (iu && iu->f->privmsg) { |
---|
727 | iu->last_channel = ic; |
---|
728 | iu->f->privmsg(iu, msg); |
---|
729 | } |
---|
730 | |
---|
731 | return TRUE; |
---|
732 | } |
---|
733 | |
---|
734 | static gboolean control_channel_invite(irc_channel_t *ic, irc_user_t *iu) |
---|
735 | { |
---|
736 | struct irc_control_channel *icc = ic->data; |
---|
737 | bee_user_t *bu = iu->bu; |
---|
738 | |
---|
739 | if (bu == NULL) { |
---|
740 | return FALSE; |
---|
741 | } |
---|
742 | |
---|
743 | if (icc->type != IRC_CC_TYPE_GROUP) { |
---|
744 | irc_send_num(ic->irc, 482, "%s :Invitations are only possible to fill_by=group channels", ic->name); |
---|
745 | return FALSE; |
---|
746 | } |
---|
747 | |
---|
748 | bu->ic->acc->prpl->add_buddy(bu->ic, bu->handle, |
---|
749 | icc->group ? icc->group->name : NULL); |
---|
750 | |
---|
751 | return TRUE; |
---|
752 | } |
---|
753 | |
---|
754 | static void control_channel_kick(irc_channel_t *ic, irc_user_t *iu, const char *msg) |
---|
755 | { |
---|
756 | struct irc_control_channel *icc = ic->data; |
---|
757 | bee_user_t *bu = iu->bu; |
---|
758 | |
---|
759 | if (bu == NULL) { |
---|
760 | return; |
---|
761 | } |
---|
762 | |
---|
763 | if (icc->type != IRC_CC_TYPE_GROUP) { |
---|
764 | irc_send_num(ic->irc, 482, "%s :Kicks are only possible to fill_by=group channels", ic->name); |
---|
765 | return; |
---|
766 | } |
---|
767 | |
---|
768 | bu->ic->acc->prpl->remove_buddy(bu->ic, bu->handle, |
---|
769 | icc->group ? icc->group->name : NULL); |
---|
770 | } |
---|
771 | |
---|
772 | static char *set_eval_by_account(set_t *set, char *value); |
---|
773 | static char *set_eval_fill_by(set_t *set, char *value); |
---|
774 | static char *set_eval_by_group(set_t *set, char *value); |
---|
775 | static char *set_eval_by_protocol(set_t *set, char *value); |
---|
776 | static char *set_eval_show_users(set_t *set, char *value); |
---|
777 | |
---|
778 | static gboolean control_channel_init(irc_channel_t *ic) |
---|
779 | { |
---|
780 | struct irc_control_channel *icc; |
---|
781 | |
---|
782 | set_add(&ic->set, "account", NULL, set_eval_by_account, ic); |
---|
783 | set_add(&ic->set, "fill_by", "all", set_eval_fill_by, ic); |
---|
784 | set_add(&ic->set, "group", NULL, set_eval_by_group, ic); |
---|
785 | set_add(&ic->set, "protocol", NULL, set_eval_by_protocol, ic); |
---|
786 | |
---|
787 | /* When changing the default, also change it below. */ |
---|
788 | set_add(&ic->set, "show_users", "online+,special%,away", set_eval_show_users, ic); |
---|
789 | |
---|
790 | ic->data = icc = g_new0(struct irc_control_channel, 1); |
---|
791 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
792 | |
---|
793 | /* Have to run the evaluator to initialize icc->modes. */ |
---|
794 | set_setstr(&ic->set, "show_users", "online+,special%,away"); |
---|
795 | |
---|
796 | /* For scripts that care. */ |
---|
797 | irc_channel_set_mode(ic, "+C"); |
---|
798 | |
---|
799 | return TRUE; |
---|
800 | } |
---|
801 | |
---|
802 | static gboolean control_channel_join(irc_channel_t *ic) |
---|
803 | { |
---|
804 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
805 | |
---|
806 | return TRUE; |
---|
807 | } |
---|
808 | |
---|
809 | static char *set_eval_by_account(set_t *set, char *value) |
---|
810 | { |
---|
811 | struct irc_channel *ic = set->data; |
---|
812 | struct irc_control_channel *icc = ic->data; |
---|
813 | account_t *acc; |
---|
814 | |
---|
815 | if (!(acc = account_get(ic->irc->b, value))) { |
---|
816 | return SET_INVALID; |
---|
817 | } |
---|
818 | |
---|
819 | icc->account = acc; |
---|
820 | if ((icc->type & IRC_CC_TYPE_MASK) == IRC_CC_TYPE_ACCOUNT) { |
---|
821 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
822 | } |
---|
823 | |
---|
824 | return g_strdup(acc->tag); |
---|
825 | } |
---|
826 | |
---|
827 | static char *set_eval_fill_by(set_t *set, char *value) |
---|
828 | { |
---|
829 | struct irc_channel *ic = set->data; |
---|
830 | struct irc_control_channel *icc = ic->data; |
---|
831 | char *s; |
---|
832 | |
---|
833 | icc->type &= ~(IRC_CC_TYPE_MASK | IRC_CC_TYPE_INVERT); |
---|
834 | |
---|
835 | s = value; |
---|
836 | if (s[0] == '!') { |
---|
837 | icc->type |= IRC_CC_TYPE_INVERT; |
---|
838 | s++; |
---|
839 | } |
---|
840 | |
---|
841 | if (strcmp(s, "all") == 0) { |
---|
842 | icc->type |= IRC_CC_TYPE_DEFAULT; |
---|
843 | } else if (strcmp(s, "rest") == 0) { |
---|
844 | icc->type |= IRC_CC_TYPE_REST; |
---|
845 | } else if (strcmp(s, "group") == 0) { |
---|
846 | icc->type |= IRC_CC_TYPE_GROUP; |
---|
847 | } else if (strcmp(s, "account") == 0) { |
---|
848 | icc->type |= IRC_CC_TYPE_ACCOUNT; |
---|
849 | } else if (strcmp(s, "protocol") == 0) { |
---|
850 | icc->type |= IRC_CC_TYPE_PROTOCOL; |
---|
851 | } else { |
---|
852 | return SET_INVALID; |
---|
853 | } |
---|
854 | |
---|
855 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
856 | return value; |
---|
857 | } |
---|
858 | |
---|
859 | static char *set_eval_by_group(set_t *set, char *value) |
---|
860 | { |
---|
861 | struct irc_channel *ic = set->data; |
---|
862 | struct irc_control_channel *icc = ic->data; |
---|
863 | |
---|
864 | icc->group = bee_group_by_name(ic->irc->b, value, TRUE); |
---|
865 | if ((icc->type & IRC_CC_TYPE_MASK) == IRC_CC_TYPE_GROUP) { |
---|
866 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
867 | } |
---|
868 | |
---|
869 | return g_strdup(icc->group->name); |
---|
870 | } |
---|
871 | |
---|
872 | static char *set_eval_by_protocol(set_t *set, char *value) |
---|
873 | { |
---|
874 | struct irc_channel *ic = set->data; |
---|
875 | struct irc_control_channel *icc = ic->data; |
---|
876 | struct prpl *prpl; |
---|
877 | |
---|
878 | if (!(prpl = find_protocol(value))) { |
---|
879 | return SET_INVALID; |
---|
880 | } |
---|
881 | |
---|
882 | icc->protocol = prpl; |
---|
883 | if ((icc->type & IRC_CC_TYPE_MASK) == IRC_CC_TYPE_PROTOCOL) { |
---|
884 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
885 | } |
---|
886 | |
---|
887 | return value; |
---|
888 | } |
---|
889 | |
---|
890 | static char *set_eval_show_users(set_t *set, char *value) |
---|
891 | { |
---|
892 | struct irc_channel *ic = set->data; |
---|
893 | struct irc_control_channel *icc = ic->data; |
---|
894 | char **parts = g_strsplit(value, ",", 0), **part; |
---|
895 | char modes[5]; |
---|
896 | |
---|
897 | memset(modes, 0, 5); |
---|
898 | for (part = parts; *part; part++) { |
---|
899 | char last, modechar = IRC_CHANNEL_USER_NONE; |
---|
900 | |
---|
901 | if (**part == '\0') { |
---|
902 | goto fail; |
---|
903 | } |
---|
904 | |
---|
905 | last = (*part)[strlen(*part + 1)]; |
---|
906 | if (last == '+') { |
---|
907 | modechar = IRC_CHANNEL_USER_VOICE; |
---|
908 | } else if (last == '%') { |
---|
909 | modechar = IRC_CHANNEL_USER_HALFOP; |
---|
910 | } else if (last == '@') { |
---|
911 | modechar = IRC_CHANNEL_USER_OP; |
---|
912 | } |
---|
913 | |
---|
914 | if (strncmp(*part, "offline", 7) == 0) { |
---|
915 | modes[0] = modechar; |
---|
916 | } else if (strncmp(*part, "away", 4) == 0) { |
---|
917 | modes[1] = modechar; |
---|
918 | } else if (strncmp(*part, "special", 7) == 0) { |
---|
919 | modes[2] = modechar; |
---|
920 | } else if (strncmp(*part, "online", 6) == 0) { |
---|
921 | modes[3] = modechar; |
---|
922 | } else { |
---|
923 | goto fail; |
---|
924 | } |
---|
925 | } |
---|
926 | memcpy(icc->modes, modes, 5); |
---|
927 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
928 | |
---|
929 | g_strfreev(parts); |
---|
930 | return value; |
---|
931 | |
---|
932 | fail: |
---|
933 | g_strfreev(parts); |
---|
934 | return SET_INVALID; |
---|
935 | } |
---|
936 | |
---|
937 | /* Figure out if a channel is supposed to have the user, assuming s/he is |
---|
938 | online or otherwise also selected by the show_users setting. Only works |
---|
939 | for control channels, but does *not* check if this channel is of that |
---|
940 | type. Beware! */ |
---|
941 | gboolean irc_channel_wants_user(irc_channel_t *ic, irc_user_t *iu) |
---|
942 | { |
---|
943 | struct irc_control_channel *icc = ic->data; |
---|
944 | gboolean ret = FALSE; |
---|
945 | |
---|
946 | if (iu->bu == NULL) { |
---|
947 | return FALSE; |
---|
948 | } |
---|
949 | |
---|
950 | switch (icc->type & IRC_CC_TYPE_MASK) { |
---|
951 | case IRC_CC_TYPE_GROUP: |
---|
952 | ret = iu->bu->group == icc->group; |
---|
953 | break; |
---|
954 | case IRC_CC_TYPE_ACCOUNT: |
---|
955 | ret = iu->bu->ic->acc == icc->account; |
---|
956 | break; |
---|
957 | case IRC_CC_TYPE_PROTOCOL: |
---|
958 | ret = iu->bu->ic->acc->prpl == icc->protocol; |
---|
959 | break; |
---|
960 | case IRC_CC_TYPE_DEFAULT: |
---|
961 | default: |
---|
962 | ret = TRUE; |
---|
963 | break; |
---|
964 | } |
---|
965 | |
---|
966 | if (icc->type & IRC_CC_TYPE_INVERT) { |
---|
967 | ret = !ret; |
---|
968 | } |
---|
969 | |
---|
970 | return ret; |
---|
971 | } |
---|
972 | |
---|
973 | static gboolean control_channel_free(irc_channel_t *ic) |
---|
974 | { |
---|
975 | struct irc_control_channel *icc = ic->data; |
---|
976 | |
---|
977 | set_del(&ic->set, "account"); |
---|
978 | set_del(&ic->set, "fill_by"); |
---|
979 | set_del(&ic->set, "group"); |
---|
980 | set_del(&ic->set, "protocol"); |
---|
981 | set_del(&ic->set, "show_users"); |
---|
982 | |
---|
983 | g_free(icc); |
---|
984 | ic->data = NULL; |
---|
985 | |
---|
986 | /* For scripts that care. */ |
---|
987 | irc_channel_set_mode(ic, "-C"); |
---|
988 | |
---|
989 | return TRUE; |
---|
990 | } |
---|
991 | |
---|
992 | static const struct irc_channel_funcs control_channel_funcs = { |
---|
993 | control_channel_privmsg, |
---|
994 | control_channel_join, |
---|
995 | NULL, |
---|
996 | NULL, |
---|
997 | control_channel_invite, |
---|
998 | control_channel_kick, |
---|
999 | |
---|
1000 | control_channel_init, |
---|
1001 | control_channel_free, |
---|
1002 | }; |
---|