Score:0

Implementing arithmetic rules in sieve to filter recipient address, is it possible?

lu flag

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

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

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 avatar
fr flag
anx
Wonderful *codegolfing* query, but potentially a terrible idea for a mail server. What is the goal, why the complicated scheme?
stackname avatar
lu flag
@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.
Score:0
fr flag
anx

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 avatar
fr flag
anx
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).
stackname avatar
lu flag
Nice workaround. I finally got it working. Thanks.
anx avatar
fr flag
anx
@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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.