Switch *_FLOOD constants to sequential rather than bits and add flood type lookup table

Using sequential constants for *_FLOOD constants means that we can use a simple lookup table to convert them
into text for display and IGNORE_* constants.
This commit is contained in:
Kevin Easton
2017-01-21 14:17:42 +11:00
parent 81ddf52909
commit 69296c2c5a
2 changed files with 39 additions and 48 deletions

View File

@@ -8,22 +8,22 @@
#define __flood_h_ #define __flood_h_
enum flood_type { enum flood_type {
MSG_FLOOD = 0x0001, MSG_FLOOD,
PUBLIC_FLOOD = 0x0002, PUBLIC_FLOOD,
NOTICE_FLOOD = 0x0004, NOTICE_FLOOD,
WALL_FLOOD = 0x0008, WALL_FLOOD,
WALLOP_FLOOD = 0x0010, WALLOP_FLOOD,
CTCP_FLOOD = 0x0020, CTCP_FLOOD,
INVITE_FLOOD = 0x0040, INVITE_FLOOD,
CDCC_FLOOD = 0x0080, CDCC_FLOOD,
CTCP_ACTION_FLOOD = 0x0100, CTCP_ACTION_FLOOD,
NICK_FLOOD = 0x0200, NICK_FLOOD,
DEOP_FLOOD = 0x0400, DEOP_FLOOD,
KICK_FLOOD = 0x0800, KICK_FLOOD,
JOIN_FLOOD = 0x1000 JOIN_FLOOD
}; };
#define FLOOD_FLAG(t) ((unsigned)(t)) #define FLOOD_FLAG(t) (1U << (t))
int BX_check_flooding(char *nick, enum flood_type type, char *line, char *channel); int BX_check_flooding(char *nick, enum flood_type type, char *line, char *channel);
int BX_is_other_flood(ChannelList *channel, NickList *nick, enum flood_type type, int *t_flood); int BX_is_other_flood(ChannelList *channel, NickList *nick, enum flood_type type, int *t_flood);

View File

@@ -35,22 +35,25 @@ CVS_REVISION(flood_c)
#define MAIN_SOURCE #define MAIN_SOURCE
#include "modval.h" #include "modval.h"
static char *ignore_types[] = static const struct flood_table {
const char *text;
long ig_type;
} flood_table[] =
{ {
"", { "MSG", IGNORE_MSGS },
"MSG", { "PUBLIC", IGNORE_PUBLIC },
"PUBLIC", { "NOTICE", IGNORE_NOTICES },
"NOTICE", { "WALL", IGNORE_WALLS },
"WALL", { "WALLOP", IGNORE_WALLOPS },
"WALLOP", { "CTCP", IGNORE_CTCPS },
"CTCP", { "INVITE", IGNORE_INVITES },
"INVITE", { "CDCC", IGNORE_CDCC },
"CDCC", { "ACTION", IGNORE_CTCPS },
"ACTION", /* Flood types below here do not set ignores */
"NICK", { "NICK", IGNORE_NICKS },
"DEOP", { "DEOP", IGNORE_MODES },
"KICK", { "KICK", IGNORE_KICKS },
"JOIN" { "JOIN", IGNORE_JOINS }
}; };
#define FLOOD_HASHSIZE 31 #define FLOOD_HASHSIZE 31
@@ -64,17 +67,6 @@ extern int from_server;
#define NO_RESET 0 #define NO_RESET 0
#define RESET 1 #define RESET 1
static char *get_flood_types(unsigned int type)
{
int x = 0;
while (type)
{
type = type >> 1;
x++;
}
return ignore_types[x];
}
#if 0 #if 0
int get_flood_rate(int type, ChannelList * channel) int get_flood_rate(int type, ChannelList * channel)
{ {
@@ -334,7 +326,7 @@ int flood_rate = 0, flood_count = 0;
{ {
*t_flood = diff; *t_flood = diff;
flooded = 1; flooded = 1;
do_hook(FLOOD_LIST, "%s %s %s %s", tmpnick->nick, get_flood_types(type),channel?channel->channel:zero, tmpnick->host); do_hook(FLOOD_LIST, "%s %s %s %s", tmpnick->nick, flood_table[type].text,channel?channel->channel:zero, tmpnick->host);
} }
set_flood(type, flood_time, RESET, tmpnick); set_flood(type, flood_time, RESET, tmpnick);
return flooded; return flooded;
@@ -431,7 +423,7 @@ int BX_check_flooding(char *nick, enum flood_type type, char *line, char *channe
if (tmp->flood == 0) if (tmp->flood == 0)
{ {
tmp->flood = 1; tmp->flood = 1;
if ((ret = do_hook(FLOOD_LIST, "%s %s %s %s", nick, get_flood_types(type),channel?channel:zero, line)) != 1) if ((ret = do_hook(FLOOD_LIST, "%s %s %s %s", nick, flood_table[type].text, channel?channel:zero, line)) != 1)
return ret; return ret;
switch(type) switch(type)
{ {
@@ -448,7 +440,7 @@ int BX_check_flooding(char *nick, enum flood_type type, char *line, char *channe
break; break;
} }
if (get_int_var(FLOOD_WARNING_VAR)) if (get_int_var(FLOOD_WARNING_VAR))
put_it("%s", convert_output_format(fget_string_var(FORMAT_FLOOD_FSET), "%s %s %s %s %s", update_clock(GET_TIME), get_flood_types(type), nick, FromUserHost, channel?channel:"unknown")); put_it("%s", convert_output_format(fget_string_var(FORMAT_FLOOD_FSET), "%s %s %s %s %s", update_clock(GET_TIME), flood_table[type].text, nick, FromUserHost, channel?channel:"unknown"));
} }
return 1; return 1;
} }
@@ -495,7 +487,6 @@ int BX_flood_prot(char *nick, char *userhost, enum flood_type flood_type, int ig
char *uh; char *uh;
int old_window_display; int old_window_display;
int kick_on_flood = 1; int kick_on_flood = 1;
char *type = get_flood_types(flood_type);
if ((flood_type == CDCC_FLOOD || flood_type == CTCP_FLOOD || flood_type == CTCP_ACTION_FLOOD) && !get_int_var(CTCP_FLOOD_PROTECTION_VAR)) if ((flood_type == CDCC_FLOOD || flood_type == CTCP_FLOOD || flood_type == CTCP_ACTION_FLOOD) && !get_int_var(CTCP_FLOOD_PROTECTION_VAR))
return 0; return 0;
@@ -520,7 +511,7 @@ int BX_flood_prot(char *nick, char *userhost, enum flood_type flood_type, int ig
{ {
if (chan->have_op && (!Nick->userlist || (Nick->userlist && !(Nick->userlist->flags & ADD_FLOOD)))) if (chan->have_op && (!Nick->userlist || (Nick->userlist && !(Nick->userlist->flags & ADD_FLOOD))))
if (!nick_isop(Nick) || get_cset_int_var(chan->csets, KICK_OPS_CSET)) if (!nick_isop(Nick) || get_cset_int_var(chan->csets, KICK_OPS_CSET))
send_to_server("KICK %s %s :\002%s\002 flooder", chan->channel, nick, type); send_to_server("KICK %s %s :\002%s\002 flooder", chan->channel, nick, flood_table[flood_type].text);
} }
} }
} }
@@ -537,7 +528,7 @@ int BX_flood_prot(char *nick, char *userhost, enum flood_type flood_type, int ig
{ {
if ((!Nick->userlist || (Nick->userlist && !(Nick->userlist->flags & ADD_FLOOD)))) if ((!Nick->userlist || (Nick->userlist && !(Nick->userlist->flags & ADD_FLOOD))))
if (!nick_isop(Nick) || get_cset_int_var(chan->csets, KICK_OPS_CSET)) if (!nick_isop(Nick) || get_cset_int_var(chan->csets, KICK_OPS_CSET))
send_to_server("KICK %s %s :\002%s\002 flooder", chan->channel, nick, type); send_to_server("KICK %s %s :\002%s\002 flooder", chan->channel, nick, flood_table[flood_type].text);
} }
} }
} }
@@ -548,11 +539,11 @@ int BX_flood_prot(char *nick, char *userhost, enum flood_type flood_type, int ig
snprintf(tmp, sizeof tmp, "*!*%s", uh); snprintf(tmp, sizeof tmp, "*!*%s", uh);
old_window_display = window_display; old_window_display = window_display;
window_display = 0; window_display = 0;
ignore_nickname(tmp, flood_type == CTCP_ACTION_FLOOD ? IGNORE_CTCPS : ignore_type(type, strlen(type)), 0); ignore_nickname(tmp, flood_table[flood_type].ig_type, 0);
window_display = old_window_display; window_display = old_window_display;
snprintf(tmp, sizeof tmp, "%d ^IGNORE *!*%s NONE", ignoretime, uh); snprintf(tmp, sizeof tmp, "%d ^IGNORE *!*%s NONE", ignoretime, uh);
timercmd("TIMER", tmp, NULL, NULL); timercmd("TIMER", tmp, NULL, NULL);
bitchsay("Auto-ignoring %s for %d minutes [\002%s\002 flood]", nick, ignoretime/60, type); bitchsay("Auto-ignoring %s for %d minutes [\002%s\002 flood]", nick, ignoretime/60, flood_table[flood_type].text);
return 1; return 1;
} }