X-Git-Url: https://jfr.im/git/solanum.git/blobdiff_plain/a8f402e909a87c5ccf767e54e76a9ae3d3658e72..78825899cd6b68037fd1b3c6c77afb7d10c4774e:/extensions/filter.c diff --git a/extensions/filter.c b/extensions/filter.c index 8c91b0c8..bebcbdfc 100644 --- a/extensions/filter.c +++ b/extensions/filter.c @@ -55,6 +55,7 @@ static const char filter_desc[] = "Filter messages using a precompiled Hyperscan static void filter_msg_user(void *data); static void filter_msg_channel(void *data); +static void filter_client_quit(void *data); static void on_client_exit(void *data); static void mo_setfilter(struct MsgBuf *, struct Client *, struct Client *, int, const char **); @@ -70,6 +71,7 @@ static int filter_enable = 1; static const char *cmdname[MESSAGE_TYPE_COUNT] = { [MESSAGE_TYPE_PRIVMSG] = "PRIVMSG", [MESSAGE_TYPE_NOTICE] = "NOTICE", + [MESSAGE_TYPE_PART] = "PART", }; enum filter_state { @@ -88,9 +90,10 @@ static char check_str[21] = ""; static unsigned filter_chmode, filter_umode; mapi_hfn_list_av1 filter_hfnlist[] = { - { "privmsg_user", (hookfn) filter_msg_user }, - { "privmsg_channel", (hookfn) filter_msg_channel }, - { "client_exit", (hookfn) on_client_exit }, + { "privmsg_user", filter_msg_user }, + { "privmsg_channel", filter_msg_channel }, + { "client_quit", filter_client_quit }, + { "client_exit", on_client_exit }, { NULL, NULL } }; @@ -100,12 +103,13 @@ struct Message setfilter_msgtab = { {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_setfilter, 2}, {mo_setfilter, 2}} }; -static void +static int modinit(void) { filter_umode = user_modes['u'] = find_umode_slot(); construct_umodebuf(); filter_chmode = cflag_add('u', chm_simple); + return 0; } static void @@ -137,13 +141,13 @@ setfilter(const char *check, const char *data, const char **error) if (!strcasecmp(data, "disable")) { filter_enable = 0; - sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, + sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Filtering disabled."); return 0; } if (!strcasecmp(data, "enable")) { filter_enable = 1; - sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, + sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Filtering enabled."); return 0; } @@ -205,6 +209,7 @@ setfilter(const char *check, const char *data, const char **error) r = hs_alloc_scratch(db, &filter_scratch); if (r != HS_SUCCESS) { if (error) *error = "couldn't allocate scratch"; + hs_free_database(db); return -1; } if (filter_db) { @@ -212,7 +217,7 @@ setfilter(const char *check, const char *data, const char **error) } state = FILTER_LOADED; filter_db = db; - sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, + sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "New filters loaded."); rb_free(filter_data); filter_data = 0; @@ -229,7 +234,7 @@ setfilter(const char *check, const char *data, const char **error) if (state == FILTER_FILLING) { int dl; - unsigned char *d = rb_base64_decode(data, strlen(data), &dl); + unsigned char *d = rb_base64_decode((unsigned char *)data, strlen(data), &dl); if (!d) { if (error) *error = "invalid data"; return -1; @@ -341,7 +346,9 @@ unsigned match_message(const char *prefix, return 0; if (!filter_db) return 0; - snprintf(check_buffer, sizeof check_buffer, "%s:%s!%s@%s#%c %s %s :%s", + if (!command) + return 0; + snprintf(check_buffer, sizeof check_buffer, "%s:%s!%s@%s#%c %s%s%s :%s", prefix, #if FILTER_NICK source->name, @@ -359,7 +366,9 @@ unsigned match_message(const char *prefix, "*", #endif source->user && source->user->suser[0] != '\0' ? '1' : '0', - command, target, + command, + target ? " " : "", + target ? target : "", msg); hs_error_t r = hs_scan(filter_db, check_buffer, strlen(check_buffer), 0, filter_scratch, match_callback, &state); if (r != HS_SUCCESS && r != HS_SCAN_TERMINATED) @@ -399,7 +408,7 @@ filter_msg_user(void *data_) data->approved = 1; } if (r & ACT_ALARM) { - sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, + sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "FILTER: %s!%s@%s [%s]", s->name, s->username, s->host, s->sockhost); } @@ -440,7 +449,7 @@ filter_msg_channel(void *data_) data->approved = 1; } if (r & ACT_ALARM) { - sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, + sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "FILTER: %s!%s@%s [%s]", s->name, s->username, s->host, s->sockhost); } @@ -450,6 +459,30 @@ filter_msg_channel(void *data_) } } +void +filter_client_quit(void *data_) +{ + hook_data_client_quit *data = data_; + struct Client *s = data->client; + if (IsOper(s)) { + return; + } + char *text = strcpy(clean_buffer, data->orig_reason); + strip_colour(text); + strip_unprintable(text); + unsigned r = match_message("0", s, "QUIT", NULL, data->orig_reason) | + match_message("1", s, "QUIT", NULL, text); + if (r & ACT_DROP) { + data->reason = NULL; + } + if (r & ACT_ALARM) { + sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, + "FILTER: %s!%s@%s [%s]", + s->name, s->username, s->host, s->sockhost); + } + /* No point in doing anything with ACT_KILL */ +} + void on_client_exit(void *data_) { @@ -462,4 +495,3 @@ on_client_exit(void *data_) state = filter_db ? FILTER_LOADED : FILTER_EMPTY; } } -