source: irc_channel.c @ d088ee8

Last change on this file since d088ee8 was d088ee8, checked in by dequis <dx@…>, at 2015-11-26T04:46:51Z

irc: Send numeric error when failing to join a channel

This fixes issues like getting a blank window with a channel that the
irc client thinks the user is in but bitlbee doesn't.

The error is sent either by returning NULL in the chat_join prpl
function, or by calling imcb_chat_free() before the user is added to the
channel.

This wasn't possible before since purple returned NULL in its chat_join,
which resulted in other bugs too. Since that's fixed, I can finally
apply this, which has been in my stash for a very long while.

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