<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE rfc SYSTEM 'rfc2629.dtd' [
  <!ENTITY rfc2119 PUBLIC '' 
  'http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml'>
  <!ENTITY rfc5228 PUBLIC '' 
  'http://xml.resource.org/public/rfc/bibxml/reference.RFC.5228.xml'>
  <!ENTITY rfc5229 PUBLIC '' 
  'http://xml.resource.org/public/rfc/bibxml/reference.RFC.5229.xml'>
]>

<?rfc toc="yes" ?>
<?rfc symrefs="yes" ?>
<?rfc sortrefs="yes"?>
<?rfc compact="yes"?>

<rfc ipr='pre5378Trust200902' docName='draft-daboo-sieve-include-06'>

<front>
<title abbrev="Sieve Extension: Include">Sieve Email Filtering: Include Extension</title>

<author initials='C.' surname='Daboo' fullname='Cyrus Daboo'>
<organization></organization>
<address>
<email>cyrus@daboo.name</email>
</address>
</author>

<author initials='A.' surname='Stone' fullname='Aaron Stone'>
<organization></organization>
<address>
<email>aaron@serendipity.palo-alto.ca.us</email>
</address>
</author>

<date month='March' year='2009' />
<area>Applications</area>

<abstract>
<t>The Sieve Email Filtering "include" extension permits users to include one Sieve script inside another. This can make managing large scripts or multiple sets of scripts much easier, as well as supporting common 'libraries' of scripts. Users are able to include their own personal scripts or site-wide scripts provided by the local Sieve implementation.</t>
</abstract>

<note title='Change History (to be removed prior to publication as an RFC)'>
<t>Changes from -05 to -06:</t>
<list style='letters'>
<t>Aaron Stone joins as author.</t>
<t>Removed | characters from the script examples.</t>
<t>Updated draft references to published RFCs.</t>
</list>
<t>Changes from -04 to -05:</t>
<list style='letters'>
<t>Fixed examples.</t>
<t>Relaxed requirement that imported/exported variables be set before being used.</t>
</list>
<t>Changes from -03 to -04:</t>
<list style='letters'>
<t>Fixed missing 2119 definitions.</t>
<t>Defined interaction with variables through use of import and export commands.</t>
</list>
<t>Changes from -02 to -03:</t>
<list style='letters'>
<t>Refreshing expired draft (updated for nits).</t>
<t>Syntax -> Usage.</t>
<t>Updated to 3028bis reference.</t>
</list>
<t>Changes from -01 to -02:</t>
<list style='letters'>
<t>Minor formatting changes only - refreshing expired draft.</t>
</list>
<t>Changes from -00 to -01:</t>
<list style='letters'>
<t>Added IPR boiler plate.</t>
<t>Re-ordered sections at start to conform to RFC style.</t>
<t>Moved recursion comment into General Considerations section.</t>
<t>Switched to using optional parameter to indicate personal vs global.</t>
<t>Explicitly state that an error occurs when a missing script is included.</t>
</list>
</note>

<note title='Open Issues (to be resolved prior to publication as an RFC)'>
<list style='letters'>
<t>Interaction with variables (scoping). Should variables be carried over between scripts that are included? Or should variables defined in an included script be local to that script only?</t>
</list>
</note>

</front>

<middle>

<section title='Introduction and Overview'>
<t>Its convenient to be able to break <xref target='RFC5228'>SIEVE</xref> scripts down into smaller components which can be reused in a variety of different circumstances. For example, users may want to have a default script and a special 'vacation' script, the later being activated when the user goes on vacation. In that case the default actions should continue to be run, but a vacation command should be executed first. One option is to edit the default script to add or remove the vacation command as needed. Another is to have a vacation script that simply has a vacation command and then includes the default script.</t>
</section>

<section title='Conventions Used in This Document'>
<t>Conventions for notations are as in <xref target='RFC5228' >SIEVE</xref> Section 1.1.</t>
<t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in <xref target='RFC2119'/>.</t>
</section>

<section title='Include Extension'>

<section title='General Considerations'>
<t>Sieve implementations that implement the "include", "return", "export" and "import" commands described below have an identifier of "include" for use with the capability mechanism. If any of the "include", "return", "export" or "import" commands are used in a script, the "include" capability MUST be listed in the "require" statement in that script.</t>

<t>Sieve implementations must track the use of actions in included scripts so that implicit "keep" behavior can be properly determined based on whether any actions have executed in any script.</t>

<t>Sieve implementations are allowed to limit the total number of nested included scripts, but MUST provide for a total of at least three levels of nested scripts including the top-level script. An error MUST be generated either when the script is uploaded to the Sieve repository, or when the script is executed, if any nesting limit is exceeded. If such an error is detected whilst processing a Sieve script, an implicit "keep" action MUST be executed to prevent loss of any messages.</t>

<t>Sieve implementations MUST ensure that recursive includes are not possible. i.e. if script "A" includes script "B", and script "B" includes script "A" an error MUST be generated either when the script is uploaded to the Sieve repository, or when the script is executed. If such an error is detected whilst processing a Sieve script, an implicit "keep" action MUST be executed to prevent loss of any messages.</t>

<t>Sieve implementations MUST handle missing scripts being referenced via an includes in an existing script. An error MUST be generated when a missing included script is discovered during execution. If such an error is detected an implicit "keep" action MUST be executed to prevent loss of any messages.</t>

<t>If the Sieve "variables" extension <xref target='RFC5229'/> is present, an issue arises with the "scope" of variables defined in scripts that may include each other. For example, if a script defines the variable "${status}" with one particular meaning or usage, and another defines "${status}" with a different meaning, then if one script includes the other there is an issue as to which "${status}" is being referenced. To solve this problem, Sieve implementations MUST follow the scoping rules defined in <xref target='variables'/> and support the "import" and "export" commands defined there.</t>
</section>

<section title='Control Structure Include'>
<figure>
<artwork><![CDATA[
        Usage:   include [<location: LOCATION>] <value: string>

        LOCATION = ":personal" / ":global"
]]></artwork>
</figure>
<t>The "include" command includes an optional "location" parameter, and a single string argument representing the name of the script to include in the main script at that point.</t>

<t>The "location" parameter defaults to ":personal" if not specified.
The "location" has the following meanings:</t>
<t></t>

<list>
<t>:personal</t>
<t></t>
<list>
<t>Indicates that the named script is stored in the user's own personal (private) Sieve repository.</t>
</list>
<t></t>
<t>:global</t>
<t></t>
<list>
<t>Indicates that the named script is stored in a site-wide Sieve repository, accessible to all users of the Sieve system.</t>
CA
</list>
</list>

<t>The included script MUST be a valid Sieve script, including having necessary "require" statements for all optional capabilities used by the script. The scope of a "require" statement in an included script is for that script only, not the including script. e.g. if script "A" includes script "B", and script "B" uses the "fileinto" extension, script "B" must have a "require" statement for "fileinto", irrespective of whether script "A" has one. In addition, if script "A" does not have a "require" statement for "fileinto", "fileinto" cannot be used anywhere in script "A", even after inclusion of script "B".</t>

<t>A "stop" command in an included script MUST stop all script processing, including the processing of the scripts that include the current one. The "return" command (described below) stops processing of the current script only, and allows the scripts that include it to continue.</t>

<t>Examples:</t>

<t>The user has four scripts stored in their personal repository:</t>
<t></t>
<t>"default"</t>
<t></t>
<list>
<t>This is the default active script that includes several others.</t>
</list>

<artwork><![CDATA[
            require ["include"];
            
            include :personal "always_allow";
            include :global "spam_tests";
            include :personal "my_spam_tests";
            include :personal "mailing_lists";
]]></artwork>

<t>"always_allow"</t>
<t></t>
<list>
<t>This script special cases some correspondent email addresses and makes sure any message containing those addresses are always kept.</t>
</list>

<artwork><![CDATA[
            if header :is "From" "boss@example.com"
            {
                keep;
            }
            elsif header :is "From" "ceo@example.com"
            {
                keep;
            }
]]></artwork>

<t>"my_spam_tests"</t>
<t></t>
<list>
<t>This script does some user-specific spam tests to catch spam messages not caught by the site-wide spam tests.</t>
</list>

<artwork><![CDATA[
            require ["reject"];
            
            if header :contains "Subject" "XXXX"
            {
                reject;
            }
            elsif header :is "From" "money@example.com"
            {
                reject;
            }
]]></artwork>

<t>"mailing_lists"</t>
<t></t>
<list>
<t>This script looks for messages from different mailing lists and files each into a mailbox specific to the mailing list.</t>
</list>

<artwork><![CDATA[
            require ["fileinto"];
            
            if header :is "Sender" "owner-ietf-mta-filters@imc.org"
            {
                fileinto "lists.sieve";
            }
            elsif header :is "Sender" "owner-ietf-imapext@imc.org"
            {
                fileinto "lists.imapext";
            }
]]></artwork>

<t>There is one script stored in the global repository:</t>

<t>"spam_tests"</t>
<t></t>
<list>
<t>This script does some site-wide spam tests which any user at the site can include in their own scripts at a suitable point. The script content is kept up to date by the site administrator.</t>
</list>

<artwork><![CDATA[
            require ["reject"];
            
            if anyof (header :contains "Subject" "$$",
                      header :contains "Subject" "Make money")
            {
                reject;
            }
]]></artwork>

<t>The "include" command may appear anywhere in the script where a control structure is legal.</t>

<t>Example:</t>

<artwork><![CDATA[
            require ["include"];
            
            if anyof (header :contains "Subject" "$$",
                      header :contains "Subject" "Make money")
            {
                include "my_reject_script";
            }
]]></artwork>

</section>

<section title='Control Structure Return'>
<figure>
<artwork><![CDATA[
        Usage: return
]]></artwork>
</figure>
<t>The "return" command stops processing of the currently included script only and returns processing control to the script which includes it. If used in the main script (i.e. not in an included script), it has the same effect as the "stop" command, including the appropriate "keep" action if no other actions have been executed up to that point.</t>
</section>

<section anchor='variables' title='Interaction with Variables'>
<t>
In order to avoid problems of variables in an included script "overwriting" those from the script that includes it, this specification requires that all variables defined in a script MUST be kept "private" to that script by default - i.e. they are not "visible" to other scripts. This ensures that two script authors cannot inadvertently cause problems by choosing the same name for a variable.
</t>
<t>
However, sometimes there is a need to make a variable defined in one script available to others. This specification defines the new commands "export" and "import" to alter the default behavior of variable scoping to allow variables to be "seen" by other scripts. The "export" command takes a list of variable names defined in a script and makes those available to any script that explicitly wants to use them. The "import" command allows a script to gain access to variables that have been explicitly made available by other scripts. The explicit use of "export and "import", coupled with the default behavior of variables only having local scope, ensures that multiple scripts cannot inadvertently overwrite each others variables.
</t>

<section title='Control Structure Import'>
<figure>
<artwork><![CDATA[
        Usage:   import <value: string-list>
]]></artwork>
</figure>
<t>The "import" command contains a string list argument that defines one or more names of variables exported by other scripts which should be made available to the current script.</t>

<t>
The "import" command, if present, MUST be used immediately after any "require" commands (at least one of which will be present listing the "include" extension).  Multiple "import" commands are allowed. An error occurs if an "import" command appears after a command other than "require" or "import". Use of the "import" command makes the listed variables immediately available for use in the body of the script that uses it.
</t>

<t>
If an "import" command lists a variable that has not been exported by any other script at that point during the Sieve execution process, then an error MUST occur.
</t>

<t>Example:</t>

<artwork><![CDATA[
            require ["variables", "fileinto", "include"];
            import "test";

            if header :contains "Subject" "${test}"
            {
                fileinto "INBOX.spam-${test};
            }
]]></artwork>
</section>

<section title='Control Structure Export'>
<figure>
<artwork><![CDATA[
        Usage:   export <value: string-list>
]]></artwork>
</figure>
<t>The "export" command contains a string list argument that defines one or more names of variables defined in the current script which should be made available to any scripts that run during the current Sieve script execution process.</t>

<t>
The "export" command, if present, MUST be used immediately after any "require" or "import"  commands.  Multiple "export" commands are allowed. An error occurs if an "export" command appears after a command other than "require", "import" or "export".
</t>

<t>
An error occurs if an "export" command lists a variable that is listed in an "import" command preceding it in the current script.
</t>

<t>Example:</t>

<artwork><![CDATA[
            require ["variables", "include"];
            export "test";

            set "test" "$$"
            include "spam_filter_script";

            set "test" "Make money"
            include "spam_filter_script";
]]></artwork>


<t>Example:</t>

<artwork><![CDATA[
            require ["variables", "include"];
            import "test";
            export "test-mailbox";

            if header :contains "Subject" "${test}"
            {
                set "test-mailbox" "INBOX.spam-${test};
                include "fileinto-script"
            }
]]></artwork>

</section>

</section>
</section>

<section title='Security Considerations'>
<t>Sieve implementations MUST ensure adequate security for the global script repository to prevent unauthorized changes to global scripts.</t>

<t>Beyond that, the "include" extension does not raise any security considerations that are not present in the base Sieve protocol, and these issues are discussed in Sieve.</t>
</section>

<section anchor="iana" title="IANA Considerations">
<t>The following template specifies the IANA registration of the Sieve extension specified in this document:</t>

<section anchor="iana-include" title="&quot;include&quot; Extension Registration">
<figure><artwork><![CDATA[
   Capability name: include
   Description:     add the "include" command to execute other Sieve
                    scripts.
   RFC number:      this RFC
   Contact address: the Sieve discussion list <ietf-mta-filters@imc.org>
]]></artwork></figure>

</section>
</section>

</middle>

<back>
<references title="Normative References">
&rfc2119;
&rfc5228;
&rfc5229;
</references>

<section title="Acknowledgments">

<t>Thanks to Ken Murchison, Rob Siemborski, Alexey Melnikov, Marc Mutz and Kjetil Torgrim Homme for comments and corrections.</t>

</section>

</back>

</rfc>
