Wiki source code of Sieve Filterregeln
Last modified by Aysegül Omus on 2024/04/05 12:56
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{toc/}} | ||
2 | |||
3 | == 1. Syntax == | ||
4 | |||
5 | === 1.1. Compare === | ||
6 | |||
7 | Sieve contains a total of 6 comparisons, including 3 string comparisons (text) and 3 integer comparisons (numbers). | ||
8 | |||
9 | String comparisons: | ||
10 | |||
11 | * **:is** means that the content of the compared field must be exactly equal to the specified value. | ||
12 | * **:contains** causes the condition to be met if the **keyword** is **included** in the compared field. | ||
13 | * **:matches** means that the compared field must match the specified wildcard expression, i.e with '?' (exactly one character) and '*'** **(0 to infinitely many characters). To get a normal '?' and '*' you need to write '~\~\?' and '~\~\*'. | ||
14 | |||
15 | Integer comparisons: | ||
16 | |||
17 | * **:over** means that the value of the field must be above the specified value. | ||
18 | * **:under** means that the value of the field must be less than the specified value. | ||
19 | * **:count** counts the number of values in the specified field. | ||
20 | |||
21 | In addition, it is still possible to check whether a specific field exists in the header: | ||
22 | |||
23 | * **:exists** checks whether a field is set in the header and is particularly useful in combination with the "not" operator e.g. to test if a subject is set. | ||
24 | |||
25 | === 1.2. Control structures and blocks === | ||
26 | |||
27 | To make case distinctions Sieve uses the usual if, else, elsif structures: | ||
28 | |||
29 | * **:if** means that the actions specified in the following block will be executed if the specified condition is met. | ||
30 | * **:else** can be used after an "if" structure and the actions specified in the following block are executed if the condition of the preceding if structure is not met. | ||
31 | * **:elsif** combines together an "else" with an "if" structure and can be understood like an "if" structure inside an "else" block. | ||
32 | * **:allof ( ... )** represents a combination of several conditions and is satisfied when all "subconditions" are met (AND operation). These are given as a comma-separated list in brackets. | ||
33 | * **:anyof(...)** represents a combination of several conditions and is fulfilled if at least one \"sub-condition\" is fulfilled (OR operation). These are given as a comma-separated list in brackets. | ||
34 | * **:not...** negates the following condition. | ||
35 | * **:[ … ]** serves to list elements, e.g. a string list: ["Max", "Martin", "Manuel"]. | ||
36 | * **:{ … }**serves to delimit an action block. | ||
37 | |||
38 | === 1.3. Fields === | ||
39 | |||
40 | The following fields are available for comparisons: | ||
41 | |||
42 | * **:header **This field is probably the most diverse, since it allows access to all header fields . It requires no **"require"** statement. | ||
43 | * **:address **This field gives direct access to all** address fields **of the header. It supports at least the //From//, //To//, //Cc//, //Bcc//, //Sender//, //Resent-From//, //Resent-To// fields and implementation-dependent ones. Does not require a **"require"** statement. | ||
44 | * **:size **This field allows the comparison of the size of the email. Requires no **"require"** statement. | ||
45 | |||
46 | === 1.4. Actions === | ||
47 | |||
48 | **stop;** is used to **end** the execution of the script. Does not require a** "require"** statement. | ||
49 | |||
50 | * **reject "... "; ** causes the email** to be sent back to the sender**. Between the quotation marks a message can be entered, which the initial sender gets, e.g. the reason for the refusal. Requires ** no "require"** statement, but ** recommended**. | ||
51 | * **discard;** causes the email **to be irrevocably deleted** without informing the sender. Requires **no "require"** statement, but ** recommended**. | ||
52 | * **keep;** causes the email **to be saved in the default folder**. Overrides any preceding //discard// action. Requires **no "require"** statement. | ||
53 | * **fileinto "... ";** causes the email to be saved in the specified folder. The folder name is entered between the quotation marks. If the folder does not exist, the message is stored in the default folder and no further action is taken. **Requires a "require"** statement. | ||
54 | * **redirect "... ";** causes the email ** to be redirected **. The destination address is in quotation marks. Requires ** no "require"** statement, but ** recommended**. | ||
55 | |||
56 | === 1.5. Diverses === | ||
57 | |||
58 | **One-line comments** are started with a **'#'**. **Multi-line comments** are started with **"/* "** and ended with **" */"**. | ||
59 | |||
60 | Some functions have to be explicitly included at the beginning of the script. This is possible with a **require **statement. | ||
61 | |||
62 | Example: | ||
63 | |||
64 | {{html wiki="true"}} | ||
65 | {{code}}require ["fileinto", "reject"]; | ||
66 | {{/code}} | ||
67 | {{/html}} | ||
68 | |||
69 | |||
70 | == 2. Examples: == | ||
71 | |||
72 | === 2.1. Simple examples === | ||
73 | |||
74 | The following example sorts all emails from one of the 3 specified addresses into the "friends" folder: | ||
75 | |||
76 | |||
77 | {{html wiki="true"}} | ||
78 | {{code}}if address :is :all "From" ["bob@example.com", "alice@example2.com", "bff@somewhere.de"] { | ||
79 | fileinto "INBOX.friends"; | ||
80 | } | ||
81 | {{/code}} | ||
82 | {{/html}} | ||
83 | |||
84 | This example shows the use of an "if" structure, the "address" field, an exact comparison using ": is" and a list in "[...]", as well as the "fileinto" action. | ||
85 | |||
86 | It is also possible to search for keywords in the subject line: | ||
87 | |||
88 | {{html wiki="true"}} | ||
89 | {{code}}if header :contains "subject" "Facebook" { | ||
90 | discard; | ||
91 | } | ||
92 | {{/code}} | ||
93 | {{/html}} | ||
94 | |||
95 | As a result of this rule, all emails whose subject contains the keyword "Facebook" are simply deleted because they are definitely spam. | ||
96 | |||
97 | The following example rejects emails larger than **1MB**: | ||
98 | |||
99 | |||
100 | {{html wiki="true"}} | ||
101 | {{code}}if size :over 1M { | ||
102 | reject "Please do not send me emails with large attachments, since my email storage is limited"; | ||
103 | } | ||
104 | {{/code}} | ||
105 | {{/html}} | ||
106 | |||
107 | |||
108 | === 2.2. Move spam emails to a separate folder === | ||
109 | |||
110 | |||
111 | {{html wiki="true"}} | ||
112 | {{code}}if header :contains "X-Spam-Level" "*******" { | ||
113 | # Wenn der Spam als gelesen markiert werden soll: | ||
114 | # addflag "\\Seen"; | ||
115 | |||
116 | fileinto "INBOX.Junk"; | ||
117 | } | ||
118 | {{/code}} | ||
119 | {{/html}} | ||
120 | |||
121 | |||
122 | === 2.3. Ignore certain emails === | ||
123 | |||
124 | |||
125 | {{html wiki="true"}} | ||
126 | {{code}}if address "From" "virusalert@informatik.tu-muenchen.de" { | ||
127 | discard; | ||
128 | } | ||
129 | {{/code}} | ||
130 | {{/html}} | ||
131 | |||
132 | |||
133 | === 2.4. Forward or move by subject === | ||
134 | |||
135 | |||
136 | {{html wiki="true"}} | ||
137 | {{code}}if header :contains "subject" "my_project" { | ||
138 | # Zum Verschieben in einen Ordner | ||
139 | # fileinto "INBOX.project"; | ||
140 | |||
141 | # Zum Weiterleiten mit lokaler Kopie | ||
142 | redirect :copy "me@example.com"; | ||
143 | |||
144 | # Zum Weiterleiten ohne lokale Kopie | ||
145 | # redirect "me@example.com"; | ||
146 | } | ||
147 | {{/code}} | ||
148 | {{/html}} | ||
149 | |||
150 | For more information about redirects, please read [[here>>https://xwiki.rbg.tum.de/bin/view/Informatik/Helpdesk/IntumMatumEmailWeiterleitungAbwesenheitsnotiz]]. | ||
151 | |||
152 | === 2.5. Sort by Address Additions === | ||
153 | |||
154 | |||
155 | {{html wiki="true"}} | ||
156 | {{code}}require ["mailbox"]; | ||
157 | |||
158 | if envelope :matches "to" "*+abc@*" { | ||
159 | # Creates the respective folder auotmatically, if it does not exist yet and moves the email, which contains the squence *+abc@*. | ||
160 | fileinto :create "INBOX.abc"; | ||
161 | } | ||
162 | {{/code}} | ||
163 | {{/html}} |