From de2eff73993ce799a17c755546ee08520e8c1348 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sun, 7 Jun 2015 05:16:25 +0200 Subject: degesch: further refactor MODE processing --- degesch.c | 92 ++++++++++++++++++++++++--------------------------------------- 1 file changed, 35 insertions(+), 57 deletions(-) diff --git a/degesch.c b/degesch.c index 79242c7..790ee7b 100644 --- a/degesch.c +++ b/degesch.c @@ -3970,7 +3970,7 @@ struct mode_processor }; /// Process a single mode character -typedef bool (*mode_processor_fn) (struct mode_processor *, char); +typedef bool (*mode_processor_apply_fn) (struct mode_processor *); static const char * mode_processor_next_param (struct mode_processor *self) @@ -3982,7 +3982,7 @@ mode_processor_next_param (struct mode_processor *self) static void mode_processor_run (struct mode_processor *self, - char **params, mode_processor_fn apply_cb) + char **params, mode_processor_apply_fn apply_cb) { self->params = params; @@ -3994,12 +3994,28 @@ mode_processor_run (struct mode_processor *self, { if (self->mode_char == '+') self->adding = true; else if (self->mode_char == '-') self->adding = false; - else if (!apply_cb (self, self->mode_char)) + else if (!apply_cb (self)) break; } } } +static void +mode_processor_toggle (struct mode_processor *self, struct str *modes) +{ + const char *pos = strchr (modes->str, self->mode_char); + if (self->adding == !!pos) + return; + + if (self->adding) + { + str_append_c (modes, self->mode_char); + // TODO: sort the modes + } + else + str_remove_slice (modes, pos - modes->str, 1); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - static void @@ -4044,10 +4060,8 @@ mode_processor_do_param_always (struct mode_processor *self) return; char key[2] = { self->mode_char, 0 }; - if (self->adding) - str_map_set (&self->channel->param_modes, key, xstrdup (param)); - else - str_map_set (&self->channel->param_modes, key, NULL); + str_map_set (&self->channel->param_modes, key, + self->adding ? xstrdup (param) : NULL); } static void @@ -4058,47 +4072,24 @@ mode_processor_do_param_when_set (struct mode_processor *self) return; char key[2] = { self->mode_char, 0 }; - if (self->adding) - str_map_set (&self->channel->param_modes, key, xstrdup (param)); - else - str_map_set (&self->channel->param_modes, key, NULL); -} - -static void -mode_processor_do_param_never (struct mode_processor *self) -{ - struct str *modes = &self->channel->no_param_modes; - const char *pos = strchr (modes->str, self->mode_char); - if (self->adding == !!pos) - return; - - if (self->adding) - { - str_append_c (modes, self->mode_char); - // TODO: sort the modes - } - else - str_remove_slice (modes, pos - modes->str, 1); + str_map_set (&self->channel->param_modes, key, + self->adding ? xstrdup (param) : NULL); } static bool -mode_processor_apply_channel (struct mode_processor *self, char mode_char) +mode_processor_apply_channel (struct mode_processor *self) { - struct server *s = self->s; - - if (strchr (s->irc_chanuser_modes, mode_char)) - mode_processor_do_user (self); - - else if (strchr (s->irc_chanmodes_list, mode_char)) - // Nothing to do here, really + if (strchr (self->s->irc_chanuser_modes, self->mode_char)) + mode_processor_do_user (self); + else if (strchr (self->s->irc_chanmodes_list, self->mode_char)) + // Nothing to do here, just skip the next argument if there's any (void) mode_processor_next_param (self); - - else if (strchr (s->irc_chanmodes_param_always, mode_char)) - mode_processor_do_param_always (self); - else if (strchr (s->irc_chanmodes_param_when_set, mode_char)) + else if (strchr (self->s->irc_chanmodes_param_always, self->mode_char)) + mode_processor_do_param_always (self); + else if (strchr (self->s->irc_chanmodes_param_when_set, self->mode_char)) mode_processor_do_param_when_set (self); - else if (strchr (s->irc_chanmodes_param_never, mode_char)) - mode_processor_do_param_never (self); + else if (strchr (self->s->irc_chanmodes_param_never, self->mode_char)) + mode_processor_toggle (self, &self->channel->no_param_modes); else // It's not safe to continue, results could be undesired return false; @@ -4116,22 +4107,9 @@ irc_handle_mode_channel // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - static bool -mode_processor_apply_user (struct mode_processor *self, char mode_char) +mode_processor_apply_user (struct mode_processor *self) { - struct server *s = self->s; - - struct str *modes = &s->irc_user_mode; - const char *pos = strchr (modes->str, mode_char); - if (self->adding == !!pos) - return true; - - if (self->adding) - { - str_append_c (modes, mode_char); - // TODO: sort the modes - } - else - str_remove_slice (modes, pos - modes->str, 1); + mode_processor_toggle (self, &self->s->irc_user_mode); return true; } -- cgit v1.2.3-70-g09d2