degesch: further refactor MODE processing

This commit is contained in:
Přemysl Eric Janouch 2015-06-07 05:16:25 +02:00
parent 2b258007f0
commit de2eff7399
1 changed files with 35 additions and 57 deletions

View File

@ -3970,7 +3970,7 @@ struct mode_processor
}; };
/// Process a single mode character /// 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 * static const char *
mode_processor_next_param (struct mode_processor *self) mode_processor_next_param (struct mode_processor *self)
@ -3982,7 +3982,7 @@ mode_processor_next_param (struct mode_processor *self)
static void static void
mode_processor_run (struct mode_processor *self, 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; self->params = params;
@ -3994,12 +3994,28 @@ mode_processor_run (struct mode_processor *self,
{ {
if (self->mode_char == '+') self->adding = true; if (self->mode_char == '+') self->adding = true;
else if (self->mode_char == '-') self->adding = false; else if (self->mode_char == '-') self->adding = false;
else if (!apply_cb (self, self->mode_char)) else if (!apply_cb (self))
break; 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 static void
@ -4044,10 +4060,8 @@ mode_processor_do_param_always (struct mode_processor *self)
return; return;
char key[2] = { self->mode_char, 0 }; char key[2] = { self->mode_char, 0 };
if (self->adding) str_map_set (&self->channel->param_modes, key,
str_map_set (&self->channel->param_modes, key, xstrdup (param)); self->adding ? xstrdup (param) : NULL);
else
str_map_set (&self->channel->param_modes, key, NULL);
} }
static void static void
@ -4058,47 +4072,24 @@ mode_processor_do_param_when_set (struct mode_processor *self)
return; return;
char key[2] = { self->mode_char, 0 }; char key[2] = { self->mode_char, 0 };
if (self->adding) str_map_set (&self->channel->param_modes, key,
str_map_set (&self->channel->param_modes, key, xstrdup (param)); self->adding ? xstrdup (param) : NULL);
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);
} }
static bool 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 (self->s->irc_chanuser_modes, self->mode_char))
if (strchr (s->irc_chanuser_modes, mode_char))
mode_processor_do_user (self); mode_processor_do_user (self);
else if (strchr (self->s->irc_chanmodes_list, self->mode_char))
else if (strchr (s->irc_chanmodes_list, mode_char)) // Nothing to do here, just skip the next argument if there's any
// Nothing to do here, really
(void) mode_processor_next_param (self); (void) mode_processor_next_param (self);
else if (strchr (self->s->irc_chanmodes_param_always, self->mode_char))
else if (strchr (s->irc_chanmodes_param_always, mode_char))
mode_processor_do_param_always (self); 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_when_set, self->mode_char))
mode_processor_do_param_when_set (self); mode_processor_do_param_when_set (self);
else if (strchr (s->irc_chanmodes_param_never, mode_char)) else if (strchr (self->s->irc_chanmodes_param_never, self->mode_char))
mode_processor_do_param_never (self); mode_processor_toggle (self, &self->channel->no_param_modes);
else else
// It's not safe to continue, results could be undesired // It's not safe to continue, results could be undesired
return false; return false;
@ -4116,22 +4107,9 @@ irc_handle_mode_channel
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static bool 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; mode_processor_toggle (self, &self->s->irc_user_mode);
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);
return true; return true;
} }