I can think of a potential solution, but its not very nice: Use the variables
capability to store the result of your regex extraction and process it. First, do extract the numbers and replace them with an value-dependant number of dots. Add them together and remove consecutive occurrences of 10 dots. Then use the :length
modifier to count them. Then compare that last variable with the extracted 3rd digit and make your decision in processing.
require ["variables", "ereject", "regex"];
if envelope :regex "to" "^abc.23@testdomain\.com$" {
set "first" "${1}";
}
if string :matches " ${first} " "1" {
set "firstdot" ".";
}
if string :matches " ${first} " "2" {
set "firstdot" "..";
}
...
set "sum" "${firstdot}${seconddot}";
if string :regex " ${sum} " "(..........)*(.*)" {
set "summod" "${2}";
}
set :length "thirdexpect" "${summod}";
if string :matches "${third}" "${thirdexpect}" {
ereject "recipient address does not match expected pattern";
}
else
{
keep;
stop;
}
Certainly possible, certainly not easy to read or diagnose.
However: I do not know of any sieve implementation that can properly reject (at SMTP stage, ereject
) messages, only return a copy and discard ("bounce"). Which is not a very nice thing to do to other servers, when your rules are in fact static and easily computable at receipt time. I therefore strongly recommend you implement this rule at the receiving server.
If that server is postfix, you can add to your smtpd_recipient_restrictions another check_recipient_access type:table
entry with a lookup type that lets you express your pattern more easily. You may find my previous answer to a similar "rule-based address" question here useful in constructing your mechanism, which I expect is fairly straightforward in SQL.