1
0
mirror of https://github.com/unrealircd/unrealircd.git synced 2026-07-05 06:13:14 +02:00

Add unicode_count() crule, e.g. unicode_count('Emoticons')

This will return the number of characters that are in the unicode block
with that name.

spamfilter {
	rule "unicode_count('Emoticons')>2";
	target { private; channel; private-notice; channel-notice; }
	action block;
	reason "Too much emotion";
}

In this commit we also make it so we pass the ClientContext (including
clictx->textanalysis) in crule_context.
This commit is contained in:
Bram Matthys
2025-03-23 18:04:17 +01:00
parent fafe16a673
commit cc75840189
3 changed files with 19 additions and 0 deletions
+1
View File
@@ -1116,6 +1116,7 @@ struct crule_context
Client *client; /**< Client that is being processed (can be NULL) */
const char *text; /**< The input string, if any (can be NULL) */
const char *destination; /**< Destination of the message, like '#xyz' for spamfilter (can be NULL, eg for 'u') */
ClientContext *clictx; /**< Client context (can be NULL) */
};
/** Evaluation function for a connection rule. */
+16
View File
@@ -144,6 +144,7 @@ static int crule_match_country(crule_context *, int, void **);
static int crule_match_asn(crule_context *, int, void **);
static int crule_match_certfp(crule_context *, int, void **);
static int crule_match_realname(crule_context *, int, void **);
static int crule_unicode_count(crule_context *, int, void **);
/* parsing function prototypes - local! */
static int crule_gettoken(crule_token *next_tokp, const char **str);
@@ -204,6 +205,7 @@ struct crule_funclistent crule_funclist[] = {
{"match_asn", 1, crule_match_asn},
{"match_certfp", 1, crule_match_certfp},
{"match_realname", 1, crule_match_realname},
{"unicode_count", 1, crule_unicode_count},
{"", 0, NULL} /* this must be here to mark end of list */
};
@@ -550,6 +552,20 @@ static int crule_match_realname(crule_context *context, int numargs, void *crule
return 0;
}
static int crule_unicode_count(crule_context *context, int numargs, void *crulearg[])
{
const char *arg = (char *)crulearg[0];
if (context && context->clictx && context->clictx->textanalysis)
{
int i = utf8_get_block_number(arg);
if (i < 0)
return -1; /* Block name not found */
return context->clictx->textanalysis->unicode_blockmap[i];
}
return 0;
}
/** Evaluate a connection rule.
* @param[in] rule Rule to evalute.
* @return Non-zero if the rule allows the connection, zero otherwise.
+2
View File
@@ -5516,6 +5516,7 @@ int _match_spamfilter(Client *client, const char *str_in, int target, const char
context.client = client;
context.text = str_in;
context.destination = destination;
context.clictx = clictx;
/* Client exempt from spamfilter checking?
* Let's check that early: going through elines is likely faster than running the regex(es).
@@ -5678,6 +5679,7 @@ int _match_spamfilter(Client *client, const char *str_in, int target, const char
context.client = client;
context.text = str_in;
context.destination = destination;
context.clictx = clictx;
if (!crule_eval(&context, tkl->ptr.spamfilter->rule))
continue;