<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd">

<rfc ipr="trust200902" category="std" docName="draft-ietf-jmap-sieve-04">
  <?rfc toc="yes"?>
  <?rfc symrefs="yes"?>
  <?rfc sortrefs="yes"?>
  <?rfc compact="yes"?>
  <?rfc subcompact="no"?>
  <?rfc private=""?>
  <?rfc topblock="yes"?>
  <?rfc comments="no"?>

  <front>
    <title abbrev="JMAP Sieve">JMAP for Sieve Scripts</title>

    <author initials="K." surname="Murchison" fullname="Kenneth Murchison">
      <organization abbrev="Fastmail">Fastmail US LLC</organization>
      <address>
        <postal>
          <street>1429 Walnut Street - Suite 1201</street>
          <city>Philadelphia</city> <region>PA</region>
          <code>19102</code> <country>USA</country>
        </postal>
        <email>murch@fastmailteam.com</email>
      </address>
    </author>

    <date />

    <area>ART</area>
    <!-- <workgroup>Independent Submission</workgroup>-->
    <workgroup>JMAP</workgroup>
    <keyword>JMAP</keyword>
    <keyword>JSON</keyword>
    <keyword>Sieve</keyword>

    <abstract>
      <t>This document specifies a data model for managing Sieve
      scripts on a server using the JSON Meta Application Protocol (JMAP).
      </t>
    </abstract>
<!--
    <note title="Open Issues">
      <t>
        <list style="symbols">
        </list>
      </t>
    </note>
-->
  </front>

  <middle>

    <section title="Introduction">
      <t><xref target="RFC8620">JMAP</xref> (JSON Meta Application
      Protocol) is a generic protocol for synchronizing data, such as
      mail, calendars or contacts, between a client and a server.
      It is optimized for mobile and web environments, and aims to
      provide a consistent interface to different data types.
      </t>

      <t>This specification defines a data model for managing
      <xref target="RFC5228">Sieve</xref> scripts on a server using
      JMAP.
      The data model is designed to allow a server to provide
      consistent access to the same scripts via
      <xref target="RFC5804">ManageSieve</xref> as well as JMAP,
      however the functionality offered over the two protocols may
      differ.
      </t>

      <section title="Notational Conventions">
        <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL",
        "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT
        RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
        interpreted as described in BCP 14 <xref target="RFC2119"/>
        <xref target="RFC8174"/> when, and only when, they appear in
        all capitals, as shown here.
        </t>

        <t>Type signatures, examples, and property descriptions in
        this document follow the conventions established in Section
        1.1 of <xref target="RFC8620"/>.  Data types defined in the
        core specification are also used in this document. 
        </t>

        <t>Servers MUST support all properties specified for the new
        data type defined in this document.</t>

        <t>For compatibility with publishing requirements, line breaks
        have been inserted inside long JSON strings, with the
        following continuation lines indented.  To form the valid JSON
        example, any line breaks inside a string must be replaced with
        a space and any other white space after the line break
        removed.</t>
      </section>

      <section title="Terminology">
        <t>The same terminology is used in this document as in the
        core JMAP specification, see <xref target="RFC8620"/>, Section
        1.6.
        </t>

        <t>The term SieveScript (with this specific capitalization) is
        used to refer to the data type defined in this document and
        instances of those data types. 
        </t>
      </section>

      <section title="Addition to the Capabilities Object">
        <t>The capabilities object is returned as part of the JMAP
        Session object; see <xref target="RFC8620"/>, Section 2. This
        document defines one additional capability URI.
        </t>

        <section title="urn:ietf:params:jmap:sieve" anchor="capa">
          <t>This represents support for the SieveScript data type and
          associated API methods.
          The value of this property in the JMAP Session
          capabilities property is an empty object.
          </t>

          <t>The value of this property in an account’s
          accountCapabilities property is an object that MUST contain
          the following information on server capabilities:

            <list style="symbols">
              <t>
                <spanx style="strong">supportsTest</spanx>:
                <spanx style="verb">Boolean</spanx>
                <vspace blankLines="1"/>
                If true, the server supports the
                <xref target="test">SieveScript/test</xref> method.
                <vspace blankLines="1"/>
              </t>

              <t>
                <spanx style="strong">maxSizeScriptName</spanx>:
                <spanx style="verb">UnsignedInt</spanx>
                <vspace blankLines="1"/>
                The maximum length, in (UTF-8) octets, allowed for the
                name of a SieveScript.
                For compatibility with ManageSieve, this MUST be at
                least 512 (up to 128 Unicode characters).
                <vspace blankLines="1"/>
              </t>

              <t>
                <spanx style="strong">maxSizeScript</spanx>:
                <spanx style="verb">UnsignedInt|null</spanx>
                <vspace blankLines="1"/>
                The maximum size (in octets) of a Sieve script the
                server is willing to store for the user,
                or <spanx style="verb">null</spanx> for no limit.
                <vspace blankLines="1"/>
              </t>

              <t>
                <spanx style="strong">maxNumberScripts</spanx>:
                <spanx style="verb">UnsignedInt|null</spanx>
                <vspace blankLines="1"/>
                The maximum number of Sieve scripts the server is
                willing to store for the user,
                or <spanx style="verb">null</spanx> for no limit.
                <vspace blankLines="1"/>
              </t>

              <t>
                <spanx style="strong">maxNumberRedirects</spanx>:
                <spanx style="verb">UnsignedInt|null</spanx>
                <vspace blankLines="1"/>
                The maximum number of Sieve "redirect" actions a
                script can perform during a single evaluation
                or <spanx style="verb">null</spanx> for no limit.
                Note that this is different from the total number of
                "redirect" actions a script can contain.
                <vspace blankLines="1"/>
              </t>

              <t>
                <spanx style="strong">sieveExtensions</spanx>:
                <spanx style="verb">String[]</spanx>
                <vspace blankLines="1"/>
                A list of case-sensitive Sieve capability strings (as
                listed in Sieve "require" action; see
                <xref target="RFC5228"/>, Section 3.2) indicating the
                extensions supported by the Sieve engine.
                <vspace blankLines="1"/>
              </t>

              <t>
                <spanx style="strong">notificationMethods</spanx>:
                <spanx style="verb">String[]|null</spanx>
                <vspace blankLines="1"/>
                A list of <xref target="RFC3986">URI schema parts</xref>
                for notification methods supported by the Sieve
                <xref target="RFC5435">"enotify"</xref> extension,
                or <spanx style="verb">null</spanx> if the extension
                is not supported by the Sieve engine.
                <vspace blankLines="1"/>
              </t>

              <t>
                <spanx style="strong">externalLists</spanx>:
                <spanx style="verb">String[]|null</spanx>
                <vspace blankLines="1"/>
                A list of <xref target="RFC3986">URI schema parts</xref>
                for externally stored list types supported by the
                Sieve <xref target="RFC6134">"extlists"</xref> extension,
                or <spanx style="verb">null</spanx> if the extension
                is not supported by the Sieve engine.
              </t>
            </list>
          </t>
        </section>

      </section>
    </section>

    <section title="Sieve Scripts" anchor="object">
      <t>A <spanx style="strong">SieveScript</spanx> object represents
      a single <xref target='RFC5228'>Sieve</xref> script for
      filtering email messages at time of final delivery.
      </t>

      <t>A <spanx style="strong">SieveScript</spanx> object has the
      following properties:

        <list style="symbols">
          <t>
            <spanx style="strong">id</spanx>:
            <spanx style="verb">Id</spanx>
            (immutable; server-set)
            <vspace blankLines="1"/>
            The id of the script.
            <vspace blankLines="1"/>
          </t>
          <t>
            <spanx style="strong">name</spanx>:
            <spanx style="verb">String|null</spanx>
            (optional; default is server-dependent)
            <vspace blankLines="1"/>
            User-visible name for the SieveScript.
            If non-null, this MUST be a <xref target="RFC5198">Net-Unicode</xref>
            string of at least 1 character in length, subject to the
            maximum size given in the capability object.

            For compatibility with ManageSieve, servers MUST reject
            names that contain control characters.  Servers MAY reject
            names that violate server policy (e.g., names containing
            slash (/)).

            The name MUST be unique among all SieveScripts within an
            account.
            <vspace blankLines="1"/>
          </t>
          <t>
            <spanx style="strong">blobId</spanx>:
            <spanx style="verb">Id</spanx>
            <vspace blankLines="1"/>
            The id of the blob containing the raw octets of the script.
            <vspace blankLines="1"/>

            The script MUST be <xref target="RFC3629">UTF-8</xref>
            content of at least 1 character in length, subject to the
            syntax of <xref target="RFC5228">Sieve</xref>.
            The script MUST NOT contain any "require" statement(s)
            mentioning Sieve capabiltity strings not present in the
            <xref target="capa">capability</xref> object.
            Note that if the Sieve <xref target="RFC5463">"ihave"</xref>
            capability string is present in the capability object,
            the script MAY mention unrecognized/unsupported extensions
            in the "ihave" test.
            <vspace blankLines="1"/>
          </t>
          <t>
            <spanx style="strong">isActive</spanx>:
            <spanx style="verb">Boolean</spanx>
            (server-set; default: false)
            <vspace blankLines="1"/>
            A user may have multiple SieveScripts on the server, yet
            only one script may be used for filtering of incoming
            messages.  This is the active script.  Users may have zero
            or one active script.
            The <xref target='set'>SieveScript/set</xref> method
            is used for changing the active script or disabling Sieve
            processing.
            <vspace blankLines="1"/>
          </t>
        </list>
      </t>

      <section title="SieveScript/get" anchor="get">
        <t>This is a standard "/get" method as described in
        <xref target="RFC8620"/>, Section 5.1.
        The <spanx style="emph">ids</spanx> argument may be
        <spanx style="verb">null</spanx> to fetch all at once. 
        </t>

        <t>This method provides similar functionality to the GETSCRIPT
        and LISTSCRIPTS commands in <xref target="RFC5804"/>.
        </t>
      </section>

      <section title="SieveScript/set" anchor="set">
        <t>This is a standard "/set" method as described in
        <xref target="RFC8620"/>, Section 5.3 but with the following
        additional request argument, which may be omitted:

        <list style="symbols">
          <t>
            <spanx style="strong">onSuccessActivateScript</spanx>:
            <spanx style="verb">Id|null</spanx>
            (optional)
            <vspace blankLines="1"/>
            If <spanx style="verb">null</spanx>, the currently active
            SieveScript (if any) will be deactivated if and only if
            all of the creations, modifications, and destructions (if
            any) succeed.
            Otherwise, the id of the SieveScript to activate if and
            only if all of the creations, modifications, and
            destructions (if any) succeed.
            (For references to SieveScript creations, this is
            equivalent to a creation-reference, so the id will be the
            creation id prefixed with a "#".)
            If this argument is not present in the request, the
            currently active SieveScript (if any) will remain as such.
            <vspace blankLines="1"/>

            The id of any activated SieveScript MUST be reported in
            either the "created" or "updated" argument in the response
            as appropriate.
            The id of any deactivated SieveScript MUST be reported in
            the "updated" argument in the response.
            <vspace blankLines="1"/>
          </t>
        </list>
        </t>

        <t>This method provides similar functionality to the
        PUTSCRIPT, DELETESCRIPT, RENAMESCRIPT, and SETACTIVE commands
        in <xref target="RFC5804"/>.
        </t>

        <t>Script content must first be uploaded as a blob using
        either the standard upload mechanism (see <xref target="RFC8620"/>
        Section 6.1) or the JMAP Blob management extension
        (see <xref target="I-D.gondwana-jmap-blob"/> Section 3.1).
        </t>

        <t>If the SieveScript can not be created or updated because it
        would result in two SieveScripts with the same name, the
        server MUST reject the request with an "alreadyExists"
        SetError.
        An "existingId" property of type "Id" MUST be included on the
        SetError object with the id of the existing SieveScript.</t>

        <t>If the SieveScript can not be created or updated because
        its size exceeds the "maxSizeScript" limit, the server MUST
        reject the request with a "tooLarge" SetError.</t>

        <t>If the Sieve Script can not be created because it would
        exceed the "maxNumberScripts" limit, the server MUST
        reject the request with an "overQuota" SetError.</t>

        <t>The active SieveScript MUST NOT be destroyed
        unless it is first deactivated in a separate SieveScript/set
        method call.</t>

        <t>The following extra SetError types are defined:
        </t>
        <t>For "create" and "update":
          <list style="symbols">
            <t>
              <spanx style="strong">invalidScript</spanx>:
              <vspace blankLines="1"/>
              The SieveScript content violates the
              <xref target='RFC5228'>Sieve</xref> grammar and/or one
              or more extensions mentioned in the script's "require"
              statement(s) are not supported by the Sieve interpreter.
              The <spanx style="emph">description</spanx> property on
              the SetError object SHOULD contain a specific error
              message giving at least the line number of the first error.
              <vspace blankLines="1"/>
            </t>
          </list>
        </t>

        <t>For "destroy":
          <list style="symbols">
            <t>
              <spanx style="strong">scriptIsActive</spanx>:
              <vspace blankLines="1"/>
              The SieveScript is active.
              <vspace blankLines="1"/>
            </t>
          </list>
        </t>

        <section title="Examples">

	  <t>
            <figure>
	      <preamble>
                Request (and response) to upload a script
                requiring the Imap4Flags <xref target="RFC5232"/>
                Extension (assuming that the JMAP Upload URL has been
                advertised in the JMAP Session object as having a path of
                "/jmap/upload/{accountId}/"):
              </preamble>
              <artwork><![CDATA[
POST /jmap/upload/ken/ HTTP/1.1
Host: jmap.example.com
Authorization: Basic a2VuOnBhc3N3b3Jk
Content-Type: application/sieve
Content-Length: 98

require "imapflags";

if address :is ["To", "Cc"] "jmap@ietf.org" {
  setflag "\\Flagged";
}


HTTP/1.1 201 Created
Date: Thu, 10 Dec 2020 17:14:31 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 171

{
  "accountId": "ken",
  "blobId": "Gabcc83e44a6e19991c4568d0b94e1767c83dd123",
  "type": "application/sieve"
  "size": 98
}
]]></artwork>
            </figure>

            <figure>
	      <preamble>
                Request (and response) to create and activate a script
                using the uploaded blob:
              </preamble>
              <artwork><![CDATA[
{
  "using": [ "urn:ietf:params:jmap:core",
             "urn:ietf:params:jmap:sieve" ],
  "methodCalls": [
    ["SieveScript/set", {
      "accountId": "ken",
      "create": { "A": {
        "name": null,
        "blobId": "Gabcc83e44a6e19991c4568d0b94e1767c83dd123"
      },
      "onSuccessActivateScript": "#A"
    }, "0"]
  ]
}

{
  "methodResponses": [
    [
      "SieveScript/set",
      {
        "oldState": "1603741717.50737918-4096",
        "newState": "1603741751.227268529-4096",
        "created": {
          "A": {
            "id": "dd1b164f-8cdc-448c-9f54-60210b5f14ae",
            "name": "ken-20201210T171432-0",
            "blobId": "Sdd1b164f-8cdc-448c-9f54-60210b5f14ae",
            "isActive": true
          }
        },
        "updated": null,
        "destroyed": null,
        "notCreated": null,
        "notUpdated": null,
        "notDestroyed": null,
        "accountId": "ken"
      },
      "0"
    ]
  ]
}
]]></artwork>
            </figure>

            <figure>
	      <preamble>
                Request (and response) to update script content using
                the <xref target="I-D.gondwana-jmap-blob">
                JMAP Blob management extension</xref>:
              </preamble>
              <artwork><![CDATA[
{
  "using": [ "urn:ietf:params:jmap:core",
             "urn:ietf:params:jmap:sieve",
             "urn:ietf:params:jmap:blob" ],
  "methodCalls": [
    ["Blob/set", {
      "accountId": "ken",
      "create": { "B": {
        "data:asText": "redirect \"ken@example.com\"\r\n;",
        "type": "application/sieve"
      }
    }, "1"],
    ["SieveScript/set", {
      "accountId": "ken",
      "update": { "dd1b164f-8cdc-448c-9f54-60210b5f14ae": {
        "blobId": "#B"
        }
      }
    }, "2"]
  ]
}

{
  "methodResponses": [
    [
      "Blob/set",
      {
        "oldState": null,
        "newState": "1603741700.309607123-0128",
        "created": {
          "B": {
            "id": "G969c83e44a6e10871c4568d0b94e1767c83ddeae",
            "blobId": "G969c83e44a6e10871c4568d0b94e1767c83ddeae",
            "type": "application/sieve",
            "size": 29
          }
        },
        "updated": null,
        "destroyed": null,
        "notCreated": null,
        "notUpdated": null,
        "notDestroyed": null,
        "accountId": "ken"
      },
      "1"
    ],
    [
      "SieveScript/set",
      {
        "oldState": "1603741751.227268529-4096",
        "newState": "1603742603.309607868-4096",
        "created": null,
        "updated": {
          "dd1b164f-8cdc-448c-9f54-60210b5f14ae": null
        },
        "destroyed": null,
        "notCreated": null,
        "notUpdated": null,
        "notDestroyed": null,
        "accountId": "ken"
      },
      "2"
    ]
  ]
}
]]></artwork>
            </figure>

            <figure>
	      <preamble>
                Request (and response) to update script name and deactivate:
              </preamble>
              <artwork><![CDATA[
{
  "using": [ "urn:ietf:params:jmap:core",
             "urn:ietf:params:jmap:sieve" ],
  "methodCalls": [
    ["SieveScript/set", {
      "accountId": "ken",
      "update": { "dd1b164f-8cdc-448c-9f54-60210b5f14ae": {
        "name": "myscript"
        }
      },
      "onSuccessActivateScript": null
    }, "3"]
  ]
}

{
  "methodResponses": [
    [
      "SieveScript/set",
      {
        "oldState": "1603742603.309607868-4096",
        "newState": "1603742967.852315428-4096",
        "created": null,
        "updated": {
          "dd1b164f-8cdc-448c-9f54-60210b5f14ae": {
            "isActive": false
          }
        },
        "destroyed": null,
        "notCreated": null,
        "notUpdated": null,
        "notDestroyed": null,
        "accountId": "ken"
      },
      "3"
    ]
  ]
}
]]></artwork>
            </figure>

            <figure>
	      <preamble>
                Request (and response) to activate a script:
              </preamble>
              <artwork><![CDATA[
{
  "using": [ "urn:ietf:params:jmap:core",
             "urn:ietf:params:jmap:sieve" ],
  "methodCalls": [
    ["SieveScript/set", {
      "accountId": "ken",
      "onSuccessActivateScript": "dd1b164f-8cdc-448c-9f54-60210b5f14ae"
    }, "4"]
  ]
}

{
  "methodResponses": [
    [
      "SieveScript/set",
      {
        "oldState": "1603742967.852315428-4096",
        "newState": "1603744460.316617118-4096",
        "created": null,
        "updated": {
          "dd1b164f-8cdc-448c-9f54-60210b5f14ae": {
            "isActive": true
          }
        },
        "destroyed": null,
        "notCreated": null,
        "notUpdated": null,
        "notDestroyed": null,
        "accountId": "ken"
      },
      "4"
    ]
  ]
}
]]></artwork>
            </figure>

            <figure>
	      <preamble>
                Requests (and responses) to deactivate and destroy the
                active script:
              </preamble>
              <artwork><![CDATA[
{
  "using": [ "urn:ietf:params:jmap:core",
             "urn:ietf:params:jmap:sieve" ],
  "methodCalls": [
    ["SieveScript/set", {
      "accountId": "ken",
      "onSuccessActivateScript": null
    }, "5"],
    ["SieveScript/set", {
      "accountId": "ken",
      "destroy": [ "dd1b164f-8cdc-448c-9f54-60210b5f14ae" ]
    }, "6"]
  ]
}

{
  "methodResponses": [
    [
      "SieveScript/set",
      {
        "oldState": "1603744460.316617118-4096",
        "newState": "1603744637.575375572-4096",
        "created": null,
        "updated": null,
        "updated": {
          "dd1b164f-8cdc-448c-9f54-60210b5f14ae": {
            "isActive": false
          }
        },
        "destroyed": null,
        "notCreated": null,
        "notUpdated": null,
        "notDestroyed": null,
        "accountId": "ken"
      },
      "5"
    ],
    [
      "SieveScript/set",
      {
        "oldState": "1603744637.575375572-4096",
        "newState": "1603744637.854390875-4096",
        "created": null,
        "updated": null,
        "destroyed": [
          "dd1b164f-8cdc-448c-9f54-60210b5f14ae"
        ],
        "notCreated": null,
        "notUpdated": null,
        "notDestroyed": null,
        "accountId": "ken"
      },
      "6"
    ]
  ]
}
]]></artwork>
            </figure>
          </t>
        </section>

      </section> <!-- /set -->

      <section title="SieveScript/query">
        <t>This is a standard "/query" method as described in
        <xref target="RFC8620"/>, Section 5.5.

        A <spanx style="emph">FilterCondition</spanx> object has the
        following properties, either of which may be omitted:

        <list style='symbols'>
          <t>
            <spanx style="strong">name</spanx>:
            <spanx style="verb">String</spanx>
            <vspace blankLines="1"/>
            The SieveScript "name" property contains the given string.
            <vspace blankLines="1"/>
          </t>
          <t>
            <spanx style="strong">isActive</spanx>:
            <spanx style="verb">Boolean</spanx>
            <vspace blankLines="1"/>
            The "isActive" property of the SieveScript must be
            identical to the value given to match the condition.
            <vspace blankLines="1"/>
          </t>
<!--
          <t>
            <spanx style="strong">isIncluded</spanx>:
            <spanx style="verb">Boolean</spanx>
            <vspace blankLines="1"/>
            If true, the SieveScript must be included
            (see <xref target="RFC6609" />) by the content of another
            SieveScript to match the condition.
            Otherwise, the SieveScript must not be included by the
            content of another SieveScript to match the condition.
            <vspace blankLines="1"/>
          </t>
-->
        </list>
        </t>

        <t>The following SieveScript properties MUST be supported for
        sorting:

        <list style='symbols'>
          <t>
            <spanx style="strong">name</spanx>
            <vspace blankLines="1"/>
          </t>
          <t>
            <spanx style="strong">isActive</spanx>
          </t>
        </list>
        </t>
      </section>

      <section title="SieveScript/validate">
        <t>This method is used by the client to verify Sieve script
        validity without storing the script on the server,
        providing similar functionality to the
        CHECKSCRIPT command in <xref target="RFC5804"/>.
        </t>

        <t>The method takes the following arguments: 

          <list style="symbols">
            <t>
              <spanx style="strong">accountId</spanx>:
              <spanx style="verb">Id</spanx>
              <vspace blankLines="1"/>
              The id of the account to use.
              <vspace blankLines="1"/>
            </t>
            <t>
              <spanx style="strong">blobId</spanx>:
              <spanx style="verb">Id</spanx>
              <vspace blankLines="1"/>
              The id of the blob containing the raw octets of the
              script to validate,
              subject to the same requirements in
              <xref target="object" />.
              <vspace blankLines="1"/>
            </t>
          </list>
        </t>

        <t>The response has the following arguments: 

          <list style="symbols">
            <t>
              <spanx style="strong">accountId</spanx>:
              <spanx style="verb">Id</spanx>
              <vspace blankLines="1"/>
              The id of the account used for this call.
              <vspace blankLines="1"/>
            </t>
            <t>
              <spanx style="strong">error</spanx>:
              <spanx style="verb">SetError|null</spanx>
              <vspace blankLines="1"/>
              A "invalidScript" SetError object if the script content
              is invalid (see <xref target="set"/>),
              or <spanx style="verb">null</spanx> if the
              script content is valid.
              <vspace blankLines="1"/>
            </t>
          </list>
        </t>

        <t>As with the <xref target='set'>SieveScript/set</xref> method,
        script content must first be uploaded as a blob using either
        the standard upload mechanism (see <xref target="RFC8620"/>
        Section 6.1) or the JMAP Blob management extension
        (see <xref target="I-D.gondwana-jmap-blob"/> Section 3.1).
        </t>

      </section> <!-- /validate -->

      <section title="SieveScript/test" anchor="test">
        <t>This method is used by the client to ask the Sieve
        interpreter to evaluate a Sieve script against a set of emails
        and report the actions that would be performed for each.
        </t>

        <t>When calling this method the "using" property of the
        Request object MUST contain the capabilities
        "urn:ietf:params:jmap:sieve" and "urn:ietf:params:jmap:mail".
        The latter is required due to the use of blob ids which may
        reference Email objects and the use of the Envelope object, as
        described below.</t>

        <t>The <spanx style="strong">SieveScript/test</spanx> method
        takes the following arguments: 

          <list style="symbols">
            <t>
              <spanx style="strong">accountId</spanx>:
              <spanx style="verb">Id</spanx>
              <vspace blankLines="1"/>
              The id of the account to use.
              <vspace blankLines="1"/>
            </t>
            <t>
              <spanx style="strong">scriptBlobId</spanx>:
              <spanx style="verb">String</spanx>
              <vspace blankLines="1"/>
              The id of the blob containing the raw octets of the
              script to validate,
              subject to the same requirements in
              <xref target="object" />.
              <vspace blankLines="1"/>
            </t>
            <t>
              <spanx style="strong">emailBlobIds</spanx>:
              <spanx style="verb">Id[]</spanx>
              <vspace blankLines="1"/>
              The ids representing the raw octets of the
              <xref target="RFC5322" /> messages to test against.
              <vspace blankLines="1"/>
            </t>
            <t>
              <spanx style="strong">envelope</spanx>:
              <spanx style="verb">Envelope|null</spanx>
              <vspace blankLines="1"/>
              Information that the Sieve interpreter should assume was
              present in the SMTP transaction that delivered the
              message when evaluating "envelope" tests.
              If <spanx style="verb">null</spanx>, all "envelope"
              tests MUST evaluate to false.
              See Section 7 of <xref target="RFC8621" /> for
              the contents of the Envelope object.
              <vspace blankLines="1"/>
            </t>
            <t>
              <spanx style="strong">lastVacationResponse</spanx>:
              <spanx style="verb">UTCDate|null</spanx>
              <vspace blankLines="1"/>

              The UTC date-time at which the Sieve interpreter should
              assume that it last auto-replied to the sender of the
              message, or <spanx style="verb">null</spanx> if the Sieve
              interpreter should assume that it has not auto-replied
              to the sender.
              <vspace blankLines="1"/>
            </t>
          </list>
        </t>

        <t>The response has the following arguments: 

          <list style="symbols">
            <t>
              <spanx style="strong">accountId</spanx>:
              <spanx style="verb">Id</spanx>
              <vspace blankLines="1"/>
              The id of the account used for this call.
              <vspace blankLines="1"/>
            </t>
            <t>
              <spanx style="strong">completed</spanx>:
              <spanx style="verb">Id[Action[]]|null</spanx>
              <vspace blankLines="1"/>
              A map of the blob id to a set of
              <spanx style="emph">Action</spanx> types for each message
              successfully processed by the script, or
              <spanx style="verb">null</spanx> if none.

              The <spanx style="emph">Action</spanx> data type is a
              tuple, represented as a JSON array containing three elements:
              <vspace blankLines="1"/>
              
              <list style="numbers">
                <t>
                  A <spanx style="verb">String</spanx>
                  <spanx style="strong">name</spanx> of the Sieve
                  action (e.g., "keep").
                  <vspace blankLines="1"/>
                </t>
                <t>
                  A <spanx style="verb">String[*]</spanx> object
                  containing any named (tagged) arguments for the action.
                  The name MUST be the tag for the argument as given
                  in the specification of the action (e.g., ":flags").
                  This may be an empty object if the action does not
                  have any tagged arguments, or none were specified in
                  the Sieve script
                  (e.g., <xref target='RFC5228'>discard</xref> or
                  <xref target='RFC5429'>ereject</xref> action).
                  <vspace blankLines="1"/>
                </t>
                <t>
                  An <spanx style="verb">*[]</spanx> array containing
                  any positional arguments for the action in the
                  order as given in the specification of the action.
                  This may be an empty array if the action does not
                  have any positional arguments
                  (e.g., <xref target='RFC5228'>discard</xref> or
                  <xref target='RFC5228'>keep</xref> action).
                  <vspace blankLines="1"/>
                </t>
              </list>
            </t>
            <t>
              <spanx style="strong">notCompleted</spanx>:
              <spanx style="verb">Id[SetError]|null</spanx>
              <vspace blankLines="1"/>
              A map of the blob id to a SetError object for each message
              that was not successfully processed by the script, or
              <spanx style="verb">null</spanx> if none.
              A "serverFail" SetError (see Section 3.6.2 of
              <xref target="RFC8620"/>) MUST be used to indicate a
              Sieve interpreter run-time error.
              <vspace blankLines="1"/>
            </t>
          </list>
        </t>

        <t>The following additional errors may be returned instead of
        the "SieveScript/test" response:

          <list style="symbols">
            <t>
              "invalidScript": The script content is invalid
              (see <xref target="set"/>).
            </t>

            <t>
              "notFound": The script referenced by the id could
              not be found.
            </t>

            <t>
              "rateLimit": The number of recent test method calls has
              reached a server-defined limit.
            </t>

            <t>
              "requestTooLarge": The total number of emailBlobIds
              exceeds the maximum number the server is willing to
              process in a single test method call.
            </t>

            <t>
              "serverFail": The script failed preparation to
              be executed for some other reason.
            </t>
          </list>
          <vspace blankLines="1"/>
        </t>

        <t>
          The JSON data type to use for each argument value is a
          direct mapping from its Sieve data type, per the following
          table:
        </t>

        <texttable>
          <ttcol align='center'>Sieve Type</ttcol>
          <ttcol align='center'>JSON Type</ttcol>
          <c>Number</c>
          <c>Number</c>
          <c>String</c>
          <c>String</c>
          <c>String List</c>
          <c>String[]</c>
          <c>tag with no value</c>
          <c>Boolean (true)</c>
        </texttable>

        <t>
          Recommendations for constructing the list of arguments are
          as follows:

          <list style="symbols">
            <t>Optional arguments in which the value is supplied by
            the Sieve interpreter SHOULD be included (e.g., ":from" and
            ":subject" arguments to the
            <xref target="RFC5230">"vacation"</xref> action).</t>

            <t>Optional arguments in which the value is implicitly
            supplied by
            a Sieve variable SHOULD be included (e.g., "keep" or
            "fileinto" actions without an explicit ":flags" argument,
            but <xref target="RFC5232">"imap4flags"</xref> have been
            set on the internal variable).</t>

            <t>Optional arguments in which the value is the specfied
            default MAY be omitted.</t>

            <t>Tagged arguments that are only used to determine
            whether the action will be executed and have no impact on
            the result of the action MAY be omitted (e.g., ":days" and
            ":addresses" arguments to the vacation action).</t>
          </list>
        </t>

        <section title="Example">
          <t>
            <figure>
	      <preamble>
                Assume that the following script has been created and
                has blob id "S123".
              </preamble>
              <artwork><![CDATA[
require [ "imapflags", "editheader", "vacation", "fcc" ];
setflag "$SieveFiltered";
addheader :last "X-Sieve-Filtered" "yes";
vacation :days 3 :fcc "INBOX.Sent" :flags "\\Answered" text:
Gone fishing.
.
;
]]></artwork>
            </figure>
          </t>

          <t>
            <figure>
	      <preamble>
                Assume that the following email has been uploaded and
                assigned blob id "B456".
              </preamble>
              <artwork><![CDATA[
From: "Some Example Sender" <example@example.net>
To: ken@example.com
Subject: test email
Date: Wed, 23 Sep 2020 12:11:11 -0500
Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0

This is a test email.
]]></artwork>
            </figure>
          </t>

          <t>
            <figure>
	      <preamble>
                The following request executes the script against the
                email and provides envelope information for use by the
                "vacation" action.
              </preamble>
              <artwork><![CDATA[
{
  "using": [
    "urn:ietf:params:jmap:core",
    "urn:ietf:params:jmap:sieve",
    "urn:ietf:params:jmap:mail"
  ],
  "methodCalls": [
    [
      "SieveScript/test",
      {
        "accountId": "ken",
        "scriptBlobId": "S123",
        "emailBlobIds": [
          "B456"
        ],
        "envelope": {
          "mailFrom": {
            "email": "example@example.net",
            "parameters": null
          },
          "rcptTo": [
            {
              "email": "ken@example.com",
              "parameters": null
            }
          ]
        },
        "lastVacationResponse": null
      },
      "R1"
    ]
  ]
}
]]></artwork>
            </figure>
          </t>

          <t>
            <figure>
	      <preamble>
                The following response lists the actions that would be
                performed by the script.
              </preamble>
              <artwork><![CDATA[
{
  "methodResponses": [
    [
      "SieveScript/test",
      {
        "completed": {
          "B456": [
            [
              "addheader",
              {
                ":last": true
              },
              [ "X-Sieve-Filtered", "yes" ]
            ],
            [
              "vacation",
              {
                ":fcc": "INBOX.Sent",
                ":flags": [
                  "\\answered"
                ],
                ":subject": "Auto: test email",
                ":from": "ken@example.com"
              },
              [ "Gone fishing." ]
            ],
            [
              "keep",
              {
                ":flags": [
                  "$SieveFiltered"
                ]
              },
              [ ]
            ]
          ]
        },
        "notCompleted": null,
        "accountId": "ken",
      },
      "R1"
    ]
  ]
}
]]></artwork>
            </figure>
          </t>
        </section> <!-- /test example -->

      </section> <!-- /test -->

    </section>


    <section title="Compatibility with JMAP Vacation Response"
             anchor="vacation">
      <t>Section 8 of <xref target="RFC8621"/> defines a
      VacationResponse object to represent an autoresponder to
      incoming email messages.
      Servers that implement the VacationResponse as a Sieve script
      that resides amongst other user scripts are subject to the
      following requirements:

      <list style='symbols'>
        <t>MUST allow the VacationResponse Sieve script to be fetched
        by the <xref target='get'>SieveScript/get</xref> method.
        </t>

        <t>MUST allow the VacationResponse Sieve script to be
        [de]activated via the "onSuccessActivateScript" argument to
        the <xref target='set'>SieveScript/set</xref> method. 
        </t>

        <t>MUST NOT allow the VacationResponse Sieve script to be
        destroyed or have its content updated by the
        <xref target='set'>SieveScript/set</xref> method.
        Any such request MUST be rejected with a "forbidden" SetError.
        A "description" property MAY be present with an explanation
        that the script can only be modified by a VacationResponse/set
        method.
        </t>
      </list>
      </t>
    </section>

    <section title="Security Considerations" anchor="security">
      <t>All security considerations of <xref target="RFC8620">JMAP</xref>
      and <xref target="RFC5228">Sieve</xref> apply to this specification.
      </t>
    </section>

    <section title="IANA Considerations">

      <section title="JMAP Capability Registration for &quot;sieve&quot;">
        <t>IANA will register the "sieve" JMAP Capability as follows:
        </t>
        <t>Capability Name:
        <spanx style="verb">urn:ietf:params:jmap:sieve</spanx>
        </t>
        <t>Specification document: this document
        </t>
        <t>Intended use: common
        </t>
        <t>Change Controller: IETF
        </t>
        <t>Security and privacy considerations: this document,
        <xref target="security" />
        </t>
      </section>

      <section title="JMAP Error Codes Registry">
        <t>
          The following sub-sections register two new error codes in the
          JMAP Error Codes registry, as defined in <xref target="RFC8620"/>.
        </t>

        <section title="invalidScript">
          <t>JMAP Error Code: invalidScript </t>
          <t>Intended use: common </t>
          <t>Change controller: IETF </t>
          <t>Reference: This document, <xref target="set"/> </t>
          <t>Description: The SieveScript violates the
          <xref target='RFC5228'>Sieve grammar</xref> and/or one
          or more extensions mentioned in the script's "require"
          statement(s) are not supported by the Sieve interpreter.</t>
        </section>

        <section title="scriptIsActive">
          <t>JMAP Error Code: scriptIsActive </t>
          <t>Intended use: common </t>
          <t>Change controller: IETF </t>
          <t>Reference: This document, <xref target="set"/> </t>
          <t>Description: The client tried to destroy the active
          SieveScript.</t>
        </section>
      </section>

    </section>

    <section title='Acknowledgments'>
      <t>The concepts in this document are based largely on those in
      <xref target='RFC5804'/>. 
      The author would like to thank the authors of that document for
      providing both inspiration and some borrowed text for this
      document.</t>
      <t>The author would also like to thank the following
      individuals for contributing their ideas and support for
      writing this specification: Bron Gondwana, Neil Jenkins, Alexey
      Melnikov, and Ricardo Signes.</t>
    </section>

  </middle>

  <back>
    <references title="Normative References">
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2119.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.8174.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.8620.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.8621.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5228.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.3986.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5322.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5435.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6134.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.3629.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5198.xml"?>
    </references>

    <references title="Informative References">
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5230.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5232.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5429.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5463.xml"?>
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5804.xml"?>
<!--      <?rfc
     include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.6609.xml"?> -->
      <?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml3/reference.I-D.gondwana-jmap-blob.xml"?>
    </references>

    <section title="Change History (To be removed by RFC Editor before
                    publication)">
      <t>Changes since ietf-03:
      <list style='symbols'>
        <t>SieveScript/test: Moved positional arguments into their own
        array (because the specfications don't use a consistent method for
        defining the action syntax or naming of positional arguments).</t>
      </list>
      </t>

      <t>Changes since ietf-02:
      <list style='symbols'>
        <t>Removed open issues.</t>
        <t>Reverted back to using only blob ids for script content.</t>
        <t>Added "rateLimit" and "requestTooLarge" to the list of
        possible error codes for /set method.</t>
        <t>Added Compatibility with JMAP Vacation Response
        section.</t>
        <t>Added RFC5228 to Security Considerations.</t>
        <t>Miscellaneous editorial changes.</t>
      </list>
      </t>

      <t>Changes since ietf-01:
      <list style='symbols'>
        <t>Removed normative references to ManageSieve (RFC 5804).</t>
        <t>Added the 'maxSizeScriptName' capability.</t>
        <t>Made the 'name' property in the SieveScript object
        optional.</t>
        <t>Added requirements for the 'name' property in the
        SieveScript object.</t>
        <t>Removed the 'blobId' property from the SieveScript
        object.</t>
        <t>Removed the 'replaceOnCreate' argument from the /set
        method.</t>
        <t>Removed the 'blobId' argument from the /validate method.</t>
        <t>Removed the 'scriptBlobId' argument from, and added the
        'scriptContent' argument to, the /test method.</t>
        <t>Editorial fixes from Neil Jenkins and Ricardo Signes.</t>
        <t>Other miscellaneous text reorganization and editorial fixes.</t>
      </list>
      </t>

      <t>Changes since ietf-00:
      <list style='symbols'>
        <t>Specified that changes made by onSuccessActivateScript MUST
        be reported in the /set response as created and/or updated as
        appropriate.</t>
        <t>Reworked and specified more of the /test response based on
        implementation experience.</t>
      </list>
      </t>

      <t>Changes since murchison-01:
      <list style='symbols'>
        <t>Explicitly stated that Sieve capability strings are
        case-sensitive.</t>
        <t>errorDescription is now String|null.</t>
        <t>Added /query method.</t>
        <t>Added /test method.</t>
      </list>
      </t>

      <t>Changes since murchison-00:
      <list style='symbols'>
        <t>Added IANA registration for "scriptIsActive" JMAP error code.</t>

        <t>Added open issue about /set{create} with an existing script name.</t>
      </list>
      </t>
    </section>

  </back>
</rfc>
