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