sieve.texi 33.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
@c This is part of the GNU Mailutils manual.
@c Copyright (C) 1999,2000,2001,2002,2003,2004,
@c 2007, 2008 Free Software Foundation, Inc.
@c See file mailutils.texi for copying conditions.
@comment *******************************************************************

The input language understood by the @sc{gnu} Sieve Library
is a superset of the Sieve language as described in RFC 3028.

@menu
* Lexical Structure::
* Syntax::
* Preprocessor::
* Require Statement::
* Comparators::
* Tests::
* Actions::
* GNU Extensions::
@end menu

@node Lexical Structure
@section Lexical Structure

@subheading Whitespace and Comments

Comments are semantically equivalent to whitespace and can be used
anyplace that whitespace is (with one exception in multi-line strings,
as described below).

There are two kinds of comments: hash comments, that begin with a
@samp{#} character that is not contained within a string and continue
until the next newline, and C-style or bracketed comments, that are
delimited by @samp{/*} and @samp{*/} tokens. The bracketed comments
may span multiple lines. E.g.:

@smallexample
if size :over 100K
  @{ # this is a comment
    discard;
  @}
  
if size :over 100K
  @{ /* this is a comment
       this is still a comment */ discard /* this is a comment again
     */ ;
  @}
@end smallexample

Like in C, bracketed comments do not nest.

@subheading Lexical Tokens

The basic lexical entities are @dfn{identifiers} and @dfn{literals}.

An @dfn{identifier} is a sequence of letters, digits and underscores, started
with a letter or underscore. For example, @code{header} and
@code{check_822_again} are valid identifiers, whereas @code{1st} is not.
A special form of identifier is @dfn{tag}: it is an identifier prefixed
with a colon (@samp{:}), e.g.: @code{:comparator}. 

A @dfn{literal} is a data that is not executed, merely evaluated ``as
is'', to be used as arguments to commands. There are four kinds of
literals:

@itemize
@item Number

@dfn{Numbers} are given as ordinary unsigned decimal numbers. An
optional suffix may be used to indicate a multiple of a power of two.
The suffixes are: @samp{K} specifying ``kibi-'', or 1,024 (2^10) times
the value of the number; @samp{M} specifying ``mebi-'', or 1,048,576
(2^20) times the value of the number; and @samp{G} specifying ``tebi-'',
or 1,073,741,824 (2^30) times the value of the number.

The numbers have 32 bits of magnitude.

@item String

A @dfn{string} is any sequence of characters enclosed in double quotes
(@samp{"}). A string cannot contain newlines and double quote
characters. This limitation will disappear in future releases.

@item Multiline Strings

A @dfn{multiline string} is used to represent large blocks of text
with embedded newlines and special characters. It starts with the
keyword @code{text:} followed by a newline and ends with a dot
(@samp{.}) on a newline by itself. Any characters between these two
markers are taken verbatim. For example:

@smallexample
text:
** This is an authomatic response from my message **
** filtering program.                             **

I can not attend your message right now. However it
will be saved, and I will read it as soon as I am back.

Regards,
Fred
.
@end smallexample

Notice that a hashed comment or whitespace may occur between
@code{text:} and the newline. However, when used inside the multiline
string a hash sign looses its special meaning (except in one case, see
below) and is taken as is, as well as bracketed comment delimiters.
In other words, no comments are allowed within a multiline string. E.g.:
    
@smallexample
text: # This is a comment

Sample text
# This line is taken verbatim
/* And this line too */
.
@end smallexample

The only exception to this rule is that preprocessor @code{include}
statement is expanded as usual when found within a multiline string
(@pxref{Preprocessor}), e.g.:

@smallexample
text:
#include <myresponse.txt>
.
@end smallexample

This results in the contents of file @file{myresponse.txt} being read
and interpreted as the contents of the multiline string.

GNU libsieve extends the described syntax as follows. If the keyword
@code{text:} is immediately followed by a dash (@samp{-}), then all
leading tab characters are stripped from input lines and the line
containing delimiter (@samp{.}). This allows multiline strings within
scripts to be indented in a natural fashion.

Furthermore, if the @code{text:} (optionally followed by @samp{-}) is
immediately followed by a word, this word will be used as ending
delimiter of multiline string instead of the default dot. For
example:        

@smallexample
@group
if header "from" "me@@example.com"
  @{
    reject text:-EOT
        I do not accept messages from
        this address.
        .
        .
        EOT
     # Notice that this the multiline string ends here.
     # The single dots above will be part of it.
    ;
  @}
@end group  
@end smallexample
@noindent

@item String Lists

A @dfn{string list} is a comma-delimited list of quoted strings, enclosed
in a pair of square brackets, e.g.:

@smallexample
["me@@example.com", "me00@@landru.example.edu"]
@end smallexample

For convenience, in any context where a list of strings is appropriate,
a single string is allowed without being a member of a list: it is
equivalent to a list with a single member.  For example, the following
two statements are equivalent:

@smallexample
exists "To";
exists ["To"];
@end smallexample

@end itemize

@node Syntax
@section Syntax

Being designed for the sole purpose of filtering mail, Sieve has a very
simple syntax. 

@menu
* Commands::
* Actions Described::
* Control Flow::
* Tests and Conditions::
@end menu

@node Commands
@subsection Commands

The basic syntax element is a @dfn{command}. It is defined as follows:

@smallexample
@var{command-name} [@var{tags}] @var{args}
@end smallexample
@noindent
where @var{command-name} is an identifier representing the name of the
command, @var{tags} is an optional list of @dfn{optional} or
@dfn{tagged arguments} and @var{args} is a list of @dfn{required} or
@dfn{positional arguments}.

Positional arguments are literals delimited with whitespace. They
provide the command with the information necessary to its proper
functioning. Each command has a fixed number of positional arguments. It
is an error to supply more arguments to the command or to give it fewer
arguments than it accepts.

Optional arguments allow to modify the behaviour of the command, like
command line options in UNIX do. They are a list of @dfn{tags}
(@pxref{Lexical Structure}) separated by whitespace. An optional
argument may have at most one parameter.

Each command understands a set of optional arguments. Supplying it tags
that it does not understand results in an error.

For example, consider the following command

@smallexample
header :mime :comparator "i;octet" ["to", "from"] "bug-mailutils@@gnu.org"
@end smallexample
@noindent
Here, given that @code{header} takes two positional arguments:
@code{header} is command name, the list @code{["to", "from"]} is first
positional argument and the string @code{"bug-mailutils@@gnu.org"} is second
positional argument. There are two optional arguments: @code{:mime} and
@code{:comparator}. The latter has a string @code{"i;octet"} as its
parameter.

@node Actions Described
@subsection Actions Described

An @dfn{action} is a Sieve command that performs some operation over
the message. Actions do the main job in any Sieve
program. Syntactically, an action is a command terminated with
semicolon, e.g.:

@smallexample
keep;

fileinto "mbox";
@end smallexample

@sc{gnu} Sieve provides the full set of actions described in RFC 3028.
It also allows to extend this set using loadable
actions. @xref{Actions}, for detailed discussion of actions.

@node Control Flow
@subsection Control Flow

The only control flow statement Sieve has is ``if'' statement. In its
simplest form it is:

@smallexample
if @code{condition} @{ @dots{} @}
@end smallexample

The effect of this statement is that the sequence of actions between the
curly braces is executed only if the @code{condition} evaluates to
@code{true}.

A more elaborate form of this statement allows to execute two
different sets of actions depending on whether the condition is
true or not:

@smallexample
if @code{condition} @{ @dots{} @} else @{ @dots{} @}
@end smallexample

The most advanced form of the ``if'' statement allows to select an
action depending on what condition from the set of conditions is met.

@smallexample
if @code{cond1} @{ @dots{} @} elsif @code{cond2} @{ @dots{} @} else @{ @dots{} @}
@end smallexample

There may be any number of ``elsif'' branches in an ``if''
statement. However it may have at most one ``else'' branch.
Notes for C programmers:

@enumerate
@item The braces surrounding each branch of an ``if'' statement are
required.
@item The ``else if'' construct is disallowed. Use ``elsif'' keyword
instead.
@end enumerate

Here's an example of ``if'' statement:

@smallexample
if header :contains "from" "coyote"
  @{
    discard;
  @}
elsif header :contains ["subject"] ["$$$"]
  @{
    discard;
  @}
else
  @{
    fileinto "INBOX";
  @}
@end smallexample

The following section describes in detail conditions used in ``if''
statements.

@node Tests and Conditions
@subsection Tests and Conditions

@dfn{Tests} are Sieve commands that return boolean value. E.g. the
test

@smallexample
header :contains "from" "coyote"
@end smallexample
@noindent
returns true only if the header ``From'' of the current message contains
substring ``coyote''.

The tests shipped with the @sc{gnu} Sieve are described in @ref{Tests}.

@dfn{Condition} is a Sieve expression that evaluates to @code{true} or
@code{false}. In its simplest form, condition is just a Sieve test.

To reverse the sense of a condition use keyword @code{not}, e.g.: 

@smallexample
not header :contains "from" "coyote"
@end smallexample

The results of several conditions may be joined together by logical
@code{and} and @code{or} operations. The special form @code{allof}
takes several tests as its arguments and computes the logical @code{and}
of their results. Similarly, the form @code{anyof} performs logical
@code{or} over the results of its arguments. E.g.:

@smallexample
if anyof (not exists ["From", "Date"],
          header :contains "from" "fool@@example.edu")
  @{
    discard;
  @}
@end smallexample
                   
@node Preprocessor
@section Preprocessor
@cindex Sieve preprocessor statements, a @sc{gnu} extension

The preprocessor statements are a @sc{gnu} extension to the Sieve language.
The syntax for a preprocessor statement is similar to that used in
@code{C} programming language, i.e.: a pound character (@samp{#})
followed by a preprocessor directive and its arguments. Any amount of
whitespace can be inserted between the @samp{#} and the directive.
Currently implemented directives are @code{include} and @code{searchpath}.

@menu
* #include::        Include the contents of a file.
* #searchpath::     Modify the current search path.
@end menu

@node #include
@subheading Sieve #include directive
@cindex #include, sieve

The @code{#include} directive reads in the contents of the given file.
The contents is ``inserted'' into the text being parsed starting at the
line where the directive appears. The directive takes two forms:

@table @code
@item #include "@var{filename}"
The @var{filename} is taken relative to the current directory.

@item #include <@var{filename}>"
The @var{filename} is searched in the list of include directories
as specified by the @option{-I} command line options.
@end table

If @var{filename} starts with a directory separator character
(@samp{/}) both forms have the same effect.

@node #searchpath
@subheading Sieve #searchpath directive
@cindex #searchpath, sieve

The @code{#searchpath} directive adds its argument to the list of
directories searched for loadable modules. It has the same effect
as @option{-L} command line switch used by @sc{gnu} sieve utility
(@FIXME-pxref{sieve group}). 

@node Require Statement
@section Require Statement

@smallexample
Syntax:   require @var{string};
          require @var{string-list};
@end smallexample

The require statement informs the parser that a script makes use of a certain
extension. Multiple capabilities can be declared using the second form
of the statement. The actual handling of a capability name depends on
its suffix.

If the name starts with @samp{comparator-}, it is understood
as a request to use the specified comparator. The comparator name
consists of the characters following the suffix.

If the name starts with @samp{test-}, it means a request to use
the given test. The test name consists of the characters following
the suffix.

Otherwise, the capability is understood as a name of an action to be
used.

The @code{require} statement, if present, must be used before any other
statement that is using the required capability. As an extension, the @sc{gnu}
sieve allows the @code{require} and any other statements to be
interspersed.

By default the following actions and comparators are always required:

@itemize
@item stop
@item keep
@item discard
@item i;octet
@item i;ascii-casemap
@end itemize

Example:

@smallexample
require ["fileinto", "reject"];

require "fileinto";

require "comparator-i;ascii-numeric";
@end smallexample

When processing arguments for @code{require} statement, @sc{gnu} libsieve
uses the following algorithm:

@enumerate 1
@item Look up the name in a symbol table. If the name begins with
@samp{comparator-} it is looked up in the comparator table. If it
begins with @samp{test-}, the test table is used instead. Otherwise
the name is looked up in the action table.

@item If the name is found, the search is terminated.

@item Otherwise, transform the name. First, any @samp{comparator-} or
@samp{test-} prefix is stripped. Then, any character other than
alphanumeric characters, @samp{.} and @samp{,} is replaced with
dash (@samp{-}). The name thus obtained is used as a file name
of an external loadable module. 

@item Try to load the module. The module is searched in the
following search paths (in the order given):

@enumerate 1
@item Mailutils module directory. By default it is
@file{$prefix/lib/mailutils}.

@item Sieve library path as given with the @option{-L} options in
the command line

@item Additional search directories specified with the
@code{#searchpath} directive.

@item The value of the environment variable @env{LTDL_LIBRARY_PATH}.

@item System library search path: The system dependent library
search path (e.g. on Linux it is set by the contents of the file
@file{/etc/ld.so.conf} and the value of the environment variable
@env{LD_LIBRARY_PATH}).
@end enumerate

The value of @env{LTDL_LIBRARY_PATH} and @env{LD_LIBRARY_PATH} must be a
colon-separated list of absolute directories, for example,
@samp{"/usr/lib/mypkg:/lib/foo"}.

In any of these directories, @command{libsieve} first attempts to find
and load the given filename. If this fails, it tries to append the
following suffixes to the file name:

@enumerate 1
@item the libtool archive extension @samp{.la}

@item the extension used for native dynamic libraries on the host
platform, e.g., @samp{.so}, @samp{.sl}, etc.
@end enumerate

@item If the module is found, @command{libsieve} executes its
initialization function (see below) and again looks up the name
in the symbol table. If found, search terminates successfully.

@item If either the module is not found, or the symbol wasn't
found after execution of the module initialization function,
search is terminated with an error status. @command{libsieve} then
issues the following diagnostic message:

@smallexample
source for the required action NAME is not available
@end smallexample
@end enumerate

@node Comparators
@section Comparators

@sc{gnu} libsieve supports the following built-in comparators:

@table @code
@item i;octet
This comparator simply compares the two arguments octet by octet

@item i;ascii-casemap
It treats uppercase and lowercase characters in the @sc{ascii} subset of
@sc{utf-8} as the same. This is the default comparator.

@item i;ascii-numeric
Treats the two arguments as @sc{ascii} representation of decimal
numbers and compares their numeric values. This comparator must
be explicitly required prior to use.
@end table 

@node Tests
@section Tests

This section describes the built-in tests supported by @sc{gnu} libsieve.
In the discussion below the following macro-notations are used:

@table @var
@item match-type
This tag specifies the matching type to be used with the test. It can
be one of the following:

@table @code
@item :is
The @code{:is} match type describes an absolute match; if the contents of
the first string are absolutely the same as the contents of the
second string, they match.  Only the string ``frobnitzm'' is the string
``frobnitzm''.  The null key ``:is'' and only ``:is'' the null value.
This is the default match-type.

@item :contains
The @code{:contains} match type describes a substring match.  If the value
argument contains the key argument as a substring, the match is true.
For instance, the string ``frobnitzm'' contains ``frob'' and ``nit'', but
not ``fbm''.  The null key ``'' is contained in all values.

@item :matches
The @code{:matches} version specifies a wildcard match using the
characters @samp{*} and @samp{?}. @samp{*} matches zero or more
characters, and @samp{?} matches a single character. @samp{?} and
@samp{*} may be escaped as @samp{\\?} and @samp{\\*} in strings to match
against themselves.  The first backslash escapes the second backslash;
together, they escape the @samp{*}.

@item :regex
The @code{:regex} version specifies a match using POSIX Extended Regular
Expressions.

@item :value @var{relation}
The @code{:value} match type does a relational comparison between
strings. Valid values for @var{relation} are:

@table @asis
@item "eq"
Equal

@item "ne"
Not Equal

@item "gt"
Greater Than

@item "ge"
Greater than or Equal

@item "lt"
Less Than

@item "le"
Less than or Equal
@end table

@item :count @var{relation}
This match type first determines the number of the specified entities
(headers, addresses, etc.) in the message and does a relational
comparison of the number of entities to the values specified in the
test expression. The test expression must be a list of one element.
@end table

@item comparator
A @var{comparator} syntax item is defined as follows:

@smallexample
:comparator "@var{comparator-name}"
@end smallexample
@noindent
It instructs sieve to use the given comparator with the test.
If @var{comparator-name} is not one of @samp{i;octet},
@samp{i;ascii-casemap} it must be required prior to using it.
For example:

@smallexample
require "comparator-i;ascii-numeric";

if header :comparator "i;ascii-numeric" :is "X-Num" "10"
  @{
    ...
@end smallexample

@item address-part
This syntax item is used when testing structured Internet addresses. It
specifies which part of an address must be used in comparisons.
Exactly one of the following tags may be used:

@table @code
@item :all
Use the whole address. This is the default.

@item :localpart
Use local part of the address.

@item :domain
Use domain part of the address.
@end table

@end table

@emph{Notice}, that @var{match-type} modifiers interact with
comparators. Some comparators are not suitable for matching with
@code{:contains} or @code{:matches}. If this occurs, sieve issues
an appropriate error message. For example, the statement:

@smallexample
if header :matches :comparator "i;ascii-numeric"
@end smallexample
@noindent
would result in the following error message:

@smallexample
comparator `i;ascii-numeric' is incompatible with match type `:matches'
in call to `header'
@end smallexample

@deffn Test false

This test always evaluates to ``false''.
@end deffn

@deffn Test true

This test always evaluates to ``true''.
@end deffn

@deffn Test address [@var{address-part}][@var{comparator}][@var{match-type}] @var{header-names} @var{key-list}

@noindent
Tagged arguments:

@table @var
@item address-part
Selects the address part to compare. Default is the whole email address
(@code{:all}).

@item comparator
Specifies the comparator to be used instead of the default @code{i;ascii-casemap}.

@item match-type
Specifies the match type to be used instead of the default @code{:is}.
@end table
@noindent
Required arguments:

@table @var
@item header-names
A list of header names.

@item key-list
A list of address values.
@end table
@noindent

The @code{address} test matches Internet addresses in structured headers
that contain addresses.  It returns @code{true} if any header contains any
key in the specified part of the address, as modified by 
@var{comparator} and @var{match-type} optional arguments.

This test returns @code{true} if any combination of the
@var{header-names} and @var{key-list} arguments match.

The @code{address} primitive never acts on the phrase part of an email
address, nor on comments within that address. Use the @code{header} test
instead. It also never acts on group names, although it does act on the
addresses within the group construct.

Example:

@smallexample
if address :is :all "from" "tim@@example.com"
  @{
     discard;
  @} 
@end smallexample
@end deffn

@deffn Test size [:over|:under] @var{number}
@noindent

The @code{size} test deals with the size of a message. The required
argument @var{number} represents the size of the message in bytes. It
may be suffixed with the following quantifiers:

@table @samp
@item k
@itemx K
The number is expressed in kilobytes.
@item m
@itemx M
The number is expressed in megabytes.
@item g
@itemx G
The number is expressed in gigabytes.
@end table

If the tagged argument is @samp{:over}, and the size of the message is greater
than @var{number}, the test is true; otherwise, it is false.

If the argument is @samp{:under}, and the size of the message is less than
the @var{number}, the test is true; otherwise, it is false.

Otherwise, the test is true only if the size of the message equals
exactly @var{number}. This is a @sc{gnu} extension.

The size of a message is defined to be the number of octets from the
initial header until the last character in the message body.
@end deffn

@deffn Test envelope [@var{address-part}][@var{comparator}][@var{match-type}] @var{envelope-part} @var{key-list}
@noindent

Tagged arguments:

@table @var
@item address-part
Selects the address part to compare. Default is the whole email address
(@code{:all}).

@item comparator
Specifies the comparator to be used instead of the default @code{i;ascii-casemap}.

@item match-type
Specifies the match type to be used instead of the default @code{:is}.
@end table
@noindent
Required arguments:

@table @var
@item envelope-parts
A list of envelope parts to operate upon.

@item key-list
A list of address values.
@end table
@noindent

The @code{envelope} test is true if the specified part of the @sc{smtp}
envelope matches the specified key.

If the envelope-part strings is (case insensitive) @samp{from},
then matching occurs against the FROM address used in the
@command{SMTP MAIL} command.  

@emph{Notice}, that due to the limitations imposed by @sc{smtp} envelope
structure the use of any other values in @var{envelope-parts} header is
meaningless.
@end deffn

@deffn Test exists @var{header-names}
@noindent

Required arguments:

@table @var
@item header-names
List of message header names.
@end table
@sp 1
@noindent

The @code{exists} test is @code{true} if the headers listed in
@var{header-names} argument exist within the message. All of the headers
must exist or the test is false.

The following example throws out mail that doesn't have a From header
and a Date header:

@smallexample
if not exists ["From","Date"]
  @{
     discard;
  @}
@end smallexample
@end deffn

@deffn Test header [@var{comparator}] [@var{match-type}] [:mime] @var{header-names} @var{key-list}
@sp 1
@noindent
Tagged arguments:

@table @asis
@item @var{comparator}
Specifies the comparator to be used instead of the default @code{i;ascii-casemap}.

@item @var{match-type}
Specifies the match type to be used instead of the default @code{:is}.

@item :mime
This tag instructs @code{header} to search through the mime headers in
multipart messages as well.

@end table
@sp 1
@noindent
Required arguments:

@table @var
@item header-names
A list of header names.

@item key-list
A list of header values.
@end table
@sp 1
@noindent
The @code{header} test evaluates to true if any header name matches any
key.  The type of match is specified by the optional match argument,
which defaults to ":is" if not explicitly given.

The test returns @code{true} if any combination of the @var{header-names}
and @var{key-list} arguments match.

If a header listed in @var{header-names} exists, it contains the null
key (@samp{""}).  However, if the named header is not present, it
does not contain the null key.  So if a message contained the header

@smallexample
X-Caffeine: C8H10N4O2
@end smallexample
@sp 1
@noindent
these tests on that header evaluate as follows:

@smallexample 
header :is ["X-Caffeine"] [""] @result{} false
header :contains ["X-Caffeine"] [""] @result{} true
@end smallexample
@end deffn

@deffn Test numaddr [:over|:under] @var{header-names} @var{number}
@noindent
This test is provided as an example of loadable extension tests. You
must use @samp{require "test-numaddr"} statement before actually using
it.

The @code{numaddr} test counts Internet addresses in structured headers
that contain addresses.  It returns true if the total number of
addresses satisfies the requested relation.

If the tagged argument is @samp{:over} and the number of addresses is
greater than @var{number}, the test is true; otherwise, it is false.

If the tagged argument is @samp{:under} and the number of addresses is
less than @var{number}, the test is true; otherwise, it is false.

If the tagged argument is not given, @samp{:over} is assumed. 
@end deffn
        
@node Actions
@section Actions

The @sc{gnu} libsieve supports the following default actions:

@itemize
@item stop
@item keep
@item discard
@item fileinto
@item reject
@item redirect
@end itemize

Among them the first three actions do not need to be explicitly required
by a @code{require} statement, while the others do.

These actions are described in detail below.

@deffn Action stop

The @code{stop} action ends all processing. If no actions have been
executed, then the @code{keep} action is taken.
@end deffn

@deffn Action keep

The effect of this action is to preserve the current message in the
mailbox. This action is executed if no other action has been executed.
@end deffn

@deffn Action discard

@code{Discard} silently throws away the current message. No notification
is returned to the sender, the message is deleted from the mailbox.

Example:
@smallexample
if header :contains ["from"] ["idiot@@example.edu"]
  @{
    discard;
  @}
@end smallexample
@end deffn

@deffn Action fileinto [:permissions @var{mode}] @var{folder}
@noindent

Required arguments:

@table @var
@item folder
A string representing the folder name
@end table

@noindent
Tagged arguments:

@table @code
@item :permissions @var{mode}
Specifies the permissions to use, if the mailbox is created.
@end table

The @code{fileinto} action delivers the message into the specified
folder.  If the folder is local, it is created using permissions
@samp{0600}, for regular files, and @samp{0700} for directories.  This
default can be changed by using the @code{:permissions} tag.  Its
argument is a mode specification, similar to that used by
@command{chmod} shell utility.  It is a list of permissions settings
separated by commas.  Each setting begins with one of the following
letters:

@table @asis
@item g
Set permissions for the users in the file group.

@item o
Set permissions for users not in the file's group.
@end table

This letter must be followed by either @samp{+} or @samp{=} and the
list of permissions to be set.  This latter list is a string
containing any one or both of the following characters:

@table @asis
@item r
Grant permission to read.

@item w
Grant permission to write.
@end table

For example, the following instruction creates the mailbox
@file{~/shared} which will be world readable and writable
for the group:

@smallexample
  fileinto :permissions "g=rw,o=r" "~/shared"
@end smallexample

Notice that:

@enumerate 1
@item
The @code{:permissions} setting are affected by the current umask
value.

@item
Only @code{r} and @code{w} permissions can be set, since other
permissions do not seem to be useful for mailboxes.  However, for
mailboxes that have a directory structure (such as maildir and MH),
any settings in @samp{g} and @samp{o} sets imply setting the
executable bit.

@item
Owner's permissions cannot be set.  The owner always has all
permissions on the mailbox he created.

@item
The @code{:permissions} settings apply only to local mailboxes.  They
are ignored for remote mailboxes.
@end enumerate

@end deffn

@deffn Action reject @var{reason}

The optional @code{reject} action refuses delivery of a message by sending
back a message delivery notification to the sender. It resends the
message to the sender, wrapping it in a ``reject'' form, noting that it
was rejected by the recipient. The required argument @var{reason} is
a string specifying the reason for rejecting the message.

Example:

If the message contained
@smallexample
Date: Tue, 1 Apr 1997 09:06:31 -0800 (PST)
From: coyote@@desert.example.org
To: roadrunner@@acme.example.com
Subject: I have a present for you

I've got some great birdseed over here at my place.
Want to buy it?
@end smallexample
@noindent
@sp 1
and the user's script contained:

@smallexample
if header :contains "from" "coyote@@desert.example.org"
  @{
    reject "I am not taking mail from you, and I don't want
            your birdseed, either!";
  @}
@end smallexample
@noindent
then the original sender <coyote@@desert.example.org> would receive the
following notification:

@smallexample
To: <coyote@@desert.example.org>
X-Authentication-Warning: roadrunner set sender using -f flag
Content-Type: multipart/mixed; boundary=----- =_aaaaaaaaaa0
MIME-Version: 1.0
----- =_aaaaaaaaaa0
The original message was received at
Tue, 1 Apr 1997 09:07:15 -0800 from
coyote@@desert.example.org.
Message was refused by recipient's mail filtering program.
Reason given was as follows:

I am not taking mail from you, and I don't want your birdseed, either!

----- =_aaaaaaaaaa0
Content-Type: message/delivery-status

Reporting-UA: sieve; GNU Mailutils 0.1.3
Arrival-Date: Tue, 1 Apr 1997 09:07:15 -0800
Final-Recipient: RFC822; roadrunner@@acme.example.com
Action: deleted
Disposition: automatic-action/MDN-sent-automatically;deleted
Last-Attempt-Date: Tue, 1 Apr 1997 09:07:15 -0800

----- =_aaaaaaaaaa0
Content-Type: message/rfc822

From: coyote@@desert.example.org
To: roadrunner@@acme.example.com
Subject: I have a present for you

I've got some great birdseed over here at my place.
Want to buy it?
----- =_aaaaaaaaaa0
@end smallexample

If the @var{reason} argument is rather long, the common approach is
to use the combination of the @code{text:} and @code{#include} keywords,
e.g.:

@smallexample
if header :mime :matches "Content-Type"
          [ "*application/msword;*", "*audio/x-midi*" ]
  @{
    reject text:
#include "nomsword.txt"
    .
    ;
  @}
@end smallexample      

@end deffn

@deffn Action redirect @var{address}
@noindent

The @code{redirect} action is used to send the message to another user at
a supplied @var{address}, as a mail forwarding feature does.  This action
makes no changes to the message body or existing headers, but it may add
new headers. It also modifies the envelope recipient.

The @code{redirect} command performs an MTA-style ``forward'' --- that
is, what you get from a @file{.forward} file using @code{sendmail} under
@sc{unix}.  The address on the SMTP envelope is replaced with the one on
the @code{redirect} command and the message is sent back
out. @emph{Notice}, that it differs from the MUA-style forward, which
creates a new message with a different sender and message ID, wrapping
the old message in a new one.
@end deffn

@node GNU Extensions
@section GNU Extensions

This section summarizes the @sc{gnu} extensions to the sieve language

@enumerate 1
@item Multiline strings syntax

GNU libsieve understands the following multiline string syntax:

@smallexample
@group
text:[-][@var{delimiter}]
....
@var{delimiter}
@end group
@end smallexample

The meaning of optional flags is the same as in shell ``here document''
construct: the dash strips all leading tab characters from the string body,
thus allowing it to be indented in a natural fashion; @var{delimiter}
introduces the new end-of-text delimiter instead of the default
dot. If @var{delimiter} starts with a backslash, no preprocessing will
be performed within a string.

@item Handling of the @code{require} statement.

@itemize
@item According to the RFC an error must occur if a @code{require} appears
after a command other than @code{require}. The @sc{gnu} sieve library allows
interspersing the @code{require} and other statements. The only
requirement is that @code{require} must occur before a statement that is
using the required capability (@pxref{Require Statement}).

@item Prefixing the required capability with ``test'' requires the use
of an extension test.
@end itemize

@item @code{header} test

The @code{header} takes an optional argument @code{:mime}, meaning to
scan the headers from each part of a multipart message.

@item @code{size} test

The @code{size} test allows to omit the optional argument
(:over|:under). In this case exact equality is assumed.

@item @code{envelope} test

The only value that can be meaningfully used as the first required
argument of an @code{envelope} test is @samp{from}. This limitation
may disappear from the subsequent releases.

@item Match type optional argument.

Along with the usual @code{:is}, @code{:matches} and @code{contains}
matching type, @sc{gnu} sieve library understands @code{:regex} type. This
matching type toggles POSIX Extended Regular Expression matching.
@end enumerate