0

For example, the receiver's address should have the following pattern:

last digit = (1st digit + 2nd digit)%10.

  • abc123@testdomain.com, PASS, 1+2=3, and the last digit is 3.
  • abc124@testdomain.com, FAIL, 1+2=3, but the last digit is 4.
  • abc678@testdomain.com, FAIL, (6+7)%10=3, but the last digit is 8.
  • abc673@testdomain.com, PASS, (6+7)%10=3, and the last digit is 3.

If I find someone sends me an email while the rule is not followed, it shall be rejected. Is it possible to create a sieve filter like this?

anx
  • 8,963
  • 5
  • 24
  • 48
stackname
  • 1
  • 1
  • 1
    Wonderful *codegolfing* query, but potentially a terrible idea for a mail server. What is the goal, why the complicated scheme? – anx Nov 08 '22 at 10:48
  • @anx This idea is used in the case when I enable the catch-all function and create email addresses followed by my personal-specific rule. Therefore, I can utilize the catch-all function without worrying about spam emails. – stackname Nov 08 '22 at 16:55

1 Answers1

0

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.

anx
  • 8,963
  • 5
  • 24
  • 48
  • Whatever is possible to unambiguously extract with *regular expressions* should work, e.g. the second digit would be `^[^0-9]*[0-9][^0-9]*([0-9])` (at the start of the address, any # of non-digits, followed by a digit, followed by any # of non-digits, next digit is the one you want to extract). – anx Nov 09 '22 at 04:01
  • Nice workaround. I finally got it working. Thanks. – stackname Nov 18 '22 at 17:20
  • @stackname Feel free to [improve](https://serverfault.com/posts/1115104/edit) my answer - I expect there will still have been some differences between what I typed from memory and what you have tested as fully functioning. – anx Nov 18 '22 at 18:46