User Tools

Site Tools


doc:appunti:linux:sa:sieve_filtering_examples

This is an old revision of the document!


Filtering with Sieve

Sender

Storing in different folder upon (envelope) sender:

require ["fileinto", "envelope"];
if envelope "from" "user1@rigacci.org" {
    fileinto "FromUser1";
}

Recipient

Storing in different folder upon destination address:

require ["fileinto"];
if address :is "to" "MyAlias1@rigacci.org" {
    fileinto "ToAlias1";
}

Forward and keep a local copy

The redirect built-in action can be modified by the :copy tag (which is a built-in extension, but it must enabled using the require statement). The :copy tag means that the message is not completed by the redirect action; eventually the keep action will be finally executed.

require ["copy"];
# Forward the messages unconditionally and continue processing.
redirect :copy "niccolo@texnet.it";

Forward with From rewrite (to pass SPF check)

Rewrite the From header before forwarding, to ensure to pass the SPF checks at destination. This does not work, beacuse it rewrites only the From header, not the envelope sender (which is generally reported as Return-Path).

require ["editheader"];
deleteheader "from";
addheader "From" "user1@forwarding.domain";
redirect "user2@forward.destination.domain";

NOTICE: To use the editheader plugin you must enable it into Dovecot configuration file (in Debian edit /etc/dovecot/conf.d/90-sieve.conf), adding it into the sieve_extensions list:

plugin {
  ...
  sieve_extensions = +vnd.dovecot.filter +editheader
  ...
}

Pipe a message to an external program

To enable the execute Sieve command using Debian 11 you have to edit some configuration files contained into the /etc/dovecot/conf.d/ directory. Set the sieve_extension option editing the file 90-sieve.conf. In this example both the filter and execute extensions are loaded:

plugin {
   ...
   sieve_extensions = +vnd.dovecot.filter +vnd.dovecot.execute
   ...
}

Then you have to edit the 90-sieve-extprograms.conf configuration file and define the sieve_execute_socket_dir and sieve_execute_bin_dir options:

plugin {
  ...
  sieve_execute_socket_dir = sieve-execute
  sieve_execute_bin_dir = /usr/local/lib/dovecot/sieve-execute
  ...
}

Only the executable files contained into sieve_execute_bin_dir can be used as execute commands in sieve scripts.

require ["vnd.dovecot.execute"];
# Specially crafted messages are piped to an SMS gateway.
if allof (
    anyof (
        address :is "from" "user1@rigacci.org",
        address :is "from" "user2@rigacci.org"),
    header :contains "Subject" "SMS" ) {
        execute :pipe "sms-gateway";
}

The execute command is not considered a final action (which “consumes” the message), so the keep action is eventually taken.

doc/appunti/linux/sa/sieve_filtering_examples.1679652379.txt.gz · Last modified: 2023/03/24 11:06 by niccolo