summaryrefslogtreecommitdiff
path: root/doc/rfc/rfc5784.txt
blob: 5222ecf77139ceeb025f6a0983fb0138a2f0fb04 (plain) (blame)
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
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
Internet Engineering Task Force (IETF)                          N. Freed
Request for Comments: 5784                                      S. Vedam
Category: Standards Track                               Sun Microsystems
ISSN: 2070-1721                                               March 2010


      Sieve Email Filtering:  Sieves and Display Directives in XML

Abstract

   This document describes a way to represent Sieve email filtering
   language scripts in XML.  Representing Sieves in XML is intended not
   as an alternate storage format for Sieve but rather as a means to
   facilitate manipulation of scripts using XML tools.

   The XML representation also defines additional elements that have no
   counterparts in the regular Sieve language.  These elements are
   intended for use by graphical user interfaces and provide facilities
   for labeling or grouping sections of a script so they can be
   displayed more conveniently.  These elements are represented as
   specially structured comments in regular Sieve format.

Status of This Memo

   This is an Internet Standards Track document.

   This document is a product of the Internet Engineering Task Force
   (IETF).  It represents the consensus of the IETF community.  It has
   received public review and has been approved for publication by the
   Internet Engineering Steering Group (IESG).  Further information on
   Internet Standards is available in Section 2 of RFC 5741.

   Information about the current status of this document, any errata,
   and how to provide feedback on it may be obtained at
   http://www.rfc-editor.org/info/rfc5784.
















Freed & Vedam                Standards Track                    [Page 1]
^L
RFC 5784             An XML Representation for Sieve          March 2010


Copyright Notice

   Copyright (c) 2010 IETF Trust and the persons identified as the
   document authors.  All rights reserved.

   This document is subject to BCP 78 and the IETF Trust's Legal
   Provisions Relating to IETF Documents
   (http://trustee.ietf.org/license-info) in effect on the date of
   publication of this document.  Please review these documents
   carefully, as they describe your rights and restrictions with respect
   to this document.  Code Components extracted from this document must
   include Simplified BSD License text as described in Section 4.e of
   the Trust Legal Provisions and are provided without warranty as
   described in the Simplified BSD License.

Table of Contents

   1.  Introduction . . . . . . . . . . . . . . . . . . . . . . . . .  3
   2.  Conventions Used in This Document  . . . . . . . . . . . . . .  4
   3.  Grammatical Structure of Sieve . . . . . . . . . . . . . . . .  4
   4.  XML Representation of Sieve  . . . . . . . . . . . . . . . . .  6
     4.1.  XML Display Directives . . . . . . . . . . . . . . . . . .  9
     4.2.  Structured Comments  . . . . . . . . . . . . . . . . . . . 10
     4.3.  Validation . . . . . . . . . . . . . . . . . . . . . . . . 11
   5.  Security Considerations  . . . . . . . . . . . . . . . . . . . 11
   6.  IANA Considerations  . . . . . . . . . . . . . . . . . . . . . 11
   7.  References . . . . . . . . . . . . . . . . . . . . . . . . . . 12
     7.1.  Normative References . . . . . . . . . . . . . . . . . . . 12
     7.2.  Informative References . . . . . . . . . . . . . . . . . . 13
   Appendix A.  Extended Example  . . . . . . . . . . . . . . . . . . 14
   Appendix B.  XML Schema for Sieves in XML  . . . . . . . . . . . . 21
   Appendix C.  Relax NG Schema for Sieves in XML . . . . . . . . . . 24
   Appendix D.  Stylesheet for Conversion from XML  . . . . . . . . . 26
   Appendix E.  Acknowledgements  . . . . . . . . . . . . . . . . . . 32

















Freed & Vedam                Standards Track                    [Page 2]
^L
RFC 5784             An XML Representation for Sieve          March 2010


1.  Introduction

   Sieve [RFC5228] is a language for filtering email messages at or
   around the time of final delivery.  It is designed to be
   implementable on either a mail client or mail server.  It is meant to
   be extensible, simple, and independent of access protocol, mail
   architecture, and operating system, and it is intended to be
   manipulated by a variety of different user interfaces.

   Some user interface environments have extensive existing facilities
   for manipulating material represented in XML [XML].  While adding
   support for alternate data syntaxes may be possible in most if not
   all of these environments, it may not be particularly convenient to
   do so.  The obvious way to deal with this issue is to map Sieves into
   XML, possibly on a separate back-end system, manipulate the XML, and
   convert it back to normal Sieve format.

   The fact that conversion into and out of XML may be done as a
   separate operation on a different system argues strongly for defining
   a common XML representation for Sieve.  This way, different front-end
   user interfaces can be used with different back-end mapping and
   storage facilities.

   Another issue with the creation and manipulation of Sieve scripts by
   user interfaces is that the language is strictly focused on
   describing email filtering operations.  The language contains no
   mechanisms for indicating how a given script should be presented in a
   user interface.  Such information can be represented in XML very
   easily, so it makes sense to define a framework to do this as part of
   the XML format.  A structured comment convention is then used to
   retain this information when the script is converted to normal Sieve
   format.

   It should be noted, however, that the ability for different front
   ends to use the same mapping and storage facilities does not mean
   that the additional XML information produced by different front ends
   will interoperate.

   Various Sieve extensions have already been defined, e.g., [RFC5183],
   [RFC5229], [RFC5230], [RFC5231], [RFC5232], [RFC5233], [RFC5235],
   [RFC5293], and more are planned.  The set of extensions available
   varies from one implementation to the next and may even change as a
   result of configuration choices.  It is therefore essential that the
   XML representation of Sieve be able to accommodate Sieve extensions
   without requiring schema changes.  It is also desirable that Sieve
   extensions not require changes to the code that converts to and from
   the XML representation.




Freed & Vedam                Standards Track                    [Page 3]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   This specification defines an XML representation for Sieve scripts
   and explains how the conversion process to and from XML works.  The
   XML representation is capable of accommodating any future Sieve
   extension as long as the underlying Sieve grammar remains unchanged.
   Furthermore, code that converts from XML to the normal Sieve format
   requires no changes to accommodate extensions, while code used to
   convert from normal Sieve format to XML only requires changes when
   new control commands are added -- a rare event.  An XML Schema, Relax
   NG Schema, and a sample stylesheet to convert from XML format are
   also provided in the appendices.

2.  Conventions Used in This Document

   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 RFC 2119 [RFC2119].

   The term "processor" is used throughout this document to refer to
   agents that convert Sieve to and from the XML representation.  The
   term "editor" refers to agents that operate on, possibly creating or
   modifying, Sieves in XML format.

3.  Grammatical Structure of Sieve

   The Sieve language is designed to be highly extensible without making
   any changes to the basic language syntax.  Accordingly, the syntax of
   Sieve, defined in Section 8 of [RFC5228], is entirely structural in
   nature and employs no reserved words of any sort.

   Structurally, a Sieve script consists of a series of commands.  Each
   command, in turn, consists of an identifier, zero or more arguments,
   an optional test or test-list, and finally an optional block
   containing another series of commands.  Commands are further broken
   down into controls and actions, although this distinction cannot be
   determined from the grammar.

   Some example Sieve controls are:

   stop;               <-- No arguments, test, or command block
   require "fileinto"; <-- Control with a single argument
   if true {stop;}     <-- Control with test and command block

   Some examples of Sieve actions are:

discard;            <-- Action with no arguments, test, or command block
fileinto "folder";  <-- Action with an argument





Freed & Vedam                Standards Track                    [Page 4]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   At the time of this writing, there are no controls defined that
   accept both arguments and a test.  Similarly, there are currently no
   defined actions that allow either a test or a command block.
   Nevertheless, the Sieve grammar allows such constructs to be defined
   by some future extension.

   A test consists of an identifier followed by zero or more arguments,
   then another test or test-list.  Unlike commands, tests cannot be
   followed by a command block.

   Here are some examples of Sieve tests.  Note that such tests have to
   appear as part of a command in order to be syntactically valid:

   true                <-- Test with no argument or subordinate test
   envelope "to" "me@example.com" <-- Test with several arguments
   header :is "from" "you@example.com" <-- Test with tagged argument

   Command or test arguments can be either string lists, whole numbers,
   or tags.  (Tags are simply identifiers preceded by a colon.)  Note
   that although the Sieve grammar treats single strings as a degenerate
   case of a string list, some tests or actions have arguments that can
   only be individual strings, not lists.

   Here is an example showing the use of both a test-list and a string
   list:

   if anyof (not exists ["From", "Date"],
             header :contains "from" "fool@example.edu") {
     discard;
   }

   Extensions can add new controls, actions, tests, or arguments to
   existing controls or actions.  Extensions have also changed how
   string content is interpreted, although this is not relevant to this
   specification.  However, it is especially important to note that so
   far, only one of the many defined Sieve extensions -- the
   foreverypart extension [RFC5703] -- has added new controls to the
   language.  It seems safe to assume that, due to their nature, future
   addition of controls will be relatively rare.

   Finally, comments are allowed between lexical elements in a Sieve
   script.  One important use case for comments is encoding meta-data
   about the script, a facility that is lacking in the Sieve language.
   Therefore, comments need to be preserved in the XML representation.







Freed & Vedam                Standards Track                    [Page 5]
^L
RFC 5784             An XML Representation for Sieve          March 2010


4.  XML Representation of Sieve

   Sieve controls and actions are represented in XML as "control" or
   "action" elements, respectively.  The command's identifier appears as
   a name attribute on the element itself.  This is the only attribute
   allowed on controls and actions -- arguments, tests, test-lists, and
   nested command blocks are all represented as nested elements.  While
   naming the element after the control or action itself may seem like a
   better choice, doing so would result in extensions requiring frequent
   corresponding schema changes.

   The example Sieve controls shown in the previous section would be
   represented as these XML fragments:

    <control name="stop"/>

    <control name="require"><str>fileinto</str></control>

    <control name="if">
     <test name="true"/><control name="stop"/>
    </control>

   The example Sieve actions shown above would appear in XML as:

   <action name="discard"/>
   <action name="fileinto"><str>folder</str></action>

   The separation of controls from actions in the XML representation
   means that conversion from normal Sieve format to XML has to be able
   to distinguish between controls and actions.  This is easily done by
   maintaining a list of all known controls since experience indicates
   new controls are rarely added.  At the time of this writing, the list
   of defined controls consists of:

   1.  if [RFC5228],

   2.  stop [RFC5228],

   3.  require [RFC5228],

   4.  foreverypart [RFC5703], and

   5.  break [RFC5703].

   It should be noted that with this approach unknown controls will
   simply be treated as actions and can be passed back and forth between
   the two representations.  The treatment of a control as an action is




Freed & Vedam                Standards Track                    [Page 6]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   unlikely to cause other issues since knowledge of a control's
   language semantics is almost always required to take advantage of it.

   Tests are represented in the same basic way as controls and actions,
   that is, as a "test" element with a name attribute giving the test
   identifier.  For example, the following XML fragments show various
   types of tests:

   <test name="true"/>

   <test name="envelope"/>
     <str>to</str><str>me@example.com</str>
   </test>

   <test name="header"/>
     <tag>is</tag><str>from</str><str>you@example.com</str>
   </test>

   String, number, and tag arguments are represented as "str", "num",
   and "tag" elements, respectively.  The actual string, number, or tag
   identifier appears as text inside the element.  None of these
   elements have any defined attributes.  Several examples of arguments
   have already appeared in the preceding control, action, and test
   examples.  Any whitespace in the str body content MUST be preserved
   by the processor.  Also, note that since strings and tags are
   represented as element text, any quotes or other syntactic elements
   required in the regular Sieve representation are dropped rather than
   being carried over into the XML.

   String list arguments are represented as a "list" element, which in
   turn contains one or more str elements.  Note that this allows the
   distinction between a single string and a string list containing a
   single string to be preserved.  This is not essential since a list
   containing a single string could simply be mapped to a string, but it
   seems prudent to maintain the distinction when mapping to and from
   XML.

   Nested command blocks appear as a series of control or action
   elements inside of an outer control or action element.  No block
   element is needed since an inner command block can only appear once
   and only after any arguments, tests, or test-lists.  For example:










Freed & Vedam                Standards Track                    [Page 7]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   <control name="if">
     <test name="anyof">
       <test name="not">
         <test name="exists">
           <list><str>From</str><str>Date</str></list>
         </test>
       </test>
       <test name="header">
         <tag>contains</tag>
         <str>from</str>
         <str>fool@example.edu</str>
       </test>
     </test>
     <action name="discard"/>
   </control>

   Sieve comments are mapped to a special "comment" element in XML.
   Both hash and bracketed comments are mapped to the same construct so
   the distinction between the two is lost in XML.  XML comments are not
   used because some XML tools do not make it convenient to access
   comment nodes.

   Comments are allowed between commands and inside "test" elements.
   But comments aren't allowed directly inside commands -- due to
   limitations of XML Schema, special enclosing "preamble" and
   "postamble" elements are needed:

   <control name="if">
     <preamble>
       <comment>Check to see if message should be discarded</comment>
     </preamble>
     <test name="anyof">
       <test name="not">
         <test name="exists">
           <list><str>From</str><str>Date</str></list>
         </test>
       </test>
       <test name="header">
         <tag>contains</tag>
         <str>from</str>
         <str>fool@example.edu</str>
       </test>
     </test>
     <action name="discard"/>
     <postamble>
       <comment>End of discard check</comment>
     </postamble>
   </control>



Freed & Vedam                Standards Track                    [Page 8]
^L
RFC 5784             An XML Representation for Sieve          March 2010


4.1.  XML Display Directives

   Sometimes graphical user interfaces are a convenient way to provide
   Sieve management functions to users.  These interfaces typically
   summarize/annotate/group/display Sieve script(s) in an intuitive way
   for end users.

   To do this effectively, the graphical user interface may require
   additional information about the Sieve script itself.  That
   information or "meta-data" might include, but is not limited to, a
   Sieve name (identifying the current Sieve), whether the Sieve is
   enabled or disabled, and the order in which the parts of the Sieve
   are presented to the user.  The graphical user interface may also
   choose to provide mechanisms to allow the user to modify the script.

   It is often useful for a graphical user interface to group related
   Sieve script elements and provide an interface that displays these
   groups separately so they can be managed as a single object.  Some
   examples include Sieve statements that together provide vacation
   responders, blacklists/whitelists, and other types of filtering
   controls.

   Some advanced graphical user interfaces may even provide a natural
   language representation of a Sieve script and/or an advanced
   interface to present Sieve statements directly to the user.

   A graphical user interface may also choose to support only a subset
   of action commands in the Sieve language (and its extensions), and so
   a mechanism to indicate the extent of support and characterize the
   relationships between those supported action commands and the test
   (with its arguments) is immensely useful and probably required for
   clients that may not have complete knowledge of Sieve grammar and
   semantics.

   The Sieve language contains no mechanisms for indicating how a given
   script should be presented in a user interface.  The language also
   does not contain any specific mechanisms to represent other sorts of
   meta-data about the script.  Providing support for such meta-data as
   part of a Sieve script is currently totally implementation specific
   and is usually done by imposing some type of structure on comments.

   However, such information can be represented in XML very easily, so
   it makes sense to define a framework to do this as part of the XML
   format.  Implementations MAY choose to use structured comments to
   retain this information when the script is converted to normal Sieve
   format.





Freed & Vedam                Standards Track                    [Page 9]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   The sample schemata for the XML representation of Sieve allows XML in
   foreign namespaces to be inserted in the same places "comment"
   elements can appear in Sieve scripts.  This is the preferred means of
   including additional information.  Alternately, the schema defines
   two display directives -- displayblock and displaydata -- as
   containers for meta-data needed by graphical user interfaces.

   Editors MAY use displayblock, displaydata, and foreign namespaces to
   associate meta-data.  Some editors find it inconvenient to preserve
   this additional data during an editing session.  Editors MAY preserve
   this data during an editing session for compatibility with other
   editors.

   The displayblock element can be used to enclose any number of Sieve
   statements at any level.  It is semantically meaningless to the Sieve
   script itself.  It allows an arbitrary set of attributes.
   Implementations MAY use this to provide many simple, display-related
   meta-data for the Sieve such as Sieve identifier, group identifier,
   order of processing, etc.

   The displaydata element supports any number of arbitrary child
   elements.  Implementations MAY use this to represent complex data
   about that Sieve such as a natural language representation of Sieve
   or a way to provide the Sieve script directly.

4.2.  Structured Comments

   Since the XML representation is not intended as a storage format,
   there needs to be a way to preserve the additional information that
   can be included in the XML representation in the normal Sieve syntax.
   This is done through the use of three structured comment conventions:

   1.  XML content in other namespaces is placed in Sieve bracketed
       comments beginning with the string "/* [/" and ending with the
       string "/] */".

   2.  The content of displaydata elements is placed in Sieve bracketed
       comments beginning with the string "/* [|" and ending with the
       string "|] */".

   3.  The beginning of a displayblock element is mapped to a bracketed
       Sieve comment beginning with the string "/* [*", which then lists
       any displayblock attribute names and values in XML format.  The
       end of a displayblock element is mapped to a comment of the form
       "*] */".

   Processors MUST preserve the additional information allowed in the
   XML format and SHOULD use the structured comment format shown above.



Freed & Vedam                Standards Track                   [Page 10]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   Note: If "*/" is found in the XML content, when mapped into a
   comment, it would prematurely terminate that comment.  Escaping this
   sequence would often be inconvenient for processors.  Editors SHALL
   NOT include "*/" within displayblock, displaydata, or foreign markup.
   Processors MAY regard documents containing "*/" in foreign markup,
   displayblock, or displaydata as invalid.

4.3.  Validation

   A processor MAY validate documents against a schema, and it MAY
   reject any that do not conform.  For any document that a processor
   does not reject as invalid, any markup that the processor cannot
   understand by reference to this specification MAY be discarded.

   Note that example Relax NG and XML Schema are given in the appendices
   below.

5.  Security Considerations

   Any syntactically valid Sieve script can be represented in XML.
   Accordingly, all security considerations applicable to Sieve and any
   extensions used also apply to the XML representation.

   The use of XML carries its own security risks.  Section 7 of RFC 3470
   [RFC3470] discusses these risks.

   It is axiomatic that a Sieve editor must be trusted to do what the
   user specifies.  If XML formats are used, this trust necessarily must
   extend to the components involved in converting to and from XML
   format.

   Arbitrary data can be included using other namespaces or placed in
   the extensible displayblock and displaydata constructs defined in
   this specification, possibly including entire scripts and other
   executable content in languages other than Sieve.  Such material will
   necessarily have its own security considerations, which are beyond
   the scope of this document.

6.  IANA Considerations

   This section registers a new XML namespace per the procedures in RFC
   3688 [RFC3688].









Freed & Vedam                Standards Track                   [Page 11]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   URI:  urn:ietf:params:xml:ns:sieve

   Registrant Contact:  IETF Sieve working group
                        <ietf-mta-filters@imc.org>

   XML:

     BEGIN
     <?xml version="1.0"?>
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
       "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
       <meta http-equiv="content-type"
             content="text/html;charset=iso-8859-1"/>
       <title>Sieve Namespace</title>
     </head>
     <body>
       <h1>Namespace for Sieve Language objects expressed in XML</h1>
       <h2>urn:ietf:params:xml:ns:sieve</h2>
       <p>See <a href="http://www.rfc-editor.org/rfc/rfc5784.txt">
        RFC 5784</a>.
       </p>
     </body>
     </html>
     END

7.  References

7.1.  Normative References

   [OASISRNC]  Clark, J., "RELAX NG Compact Syntax", OASIS Committee
               Specification rnc, November 2002.

   [RFC2119]   Bradner, S., "Key words for use in RFCs to Indicate
               Requirement Levels", BCP 14, RFC 2119, March 1997.

   [RFC3470]   Hollenbeck, S., Rose, M., and L. Masinter, "Guidelines
               for the Use of Extensible Markup Language (XML)
               within IETF Protocols", BCP 70, RFC 3470, January 2003.

   [RFC3688]   Mealling, M., "The IETF XML Registry", BCP 81, RFC 3688,
               January 2004.

   [RFC5228]   Guenther, P. and T. Showalter, "Sieve: An Email Filtering
               Language", RFC 5228, January 2008.





Freed & Vedam                Standards Track                   [Page 12]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   [XML]       Bray, T., Paoli, J., Sperberg-McQueen, C., Maler, E., and
               F. Yergeau, "Extensible Markup Language (XML) 1.0 (Fifth
               Edition)", W3C REC-xml-20081126, November 2008,
               <http://www.w3.org/TR/2008/REC-xml-20081126/>.

7.2.  Informative References

   [RFC5183]   Freed, N., "Sieve Email Filtering: Environment
               Extension", RFC 5183, May 2008.

   [RFC5229]   Homme, K., "Sieve Email Filtering: Variables Extension",
               RFC 5229, January 2008.

   [RFC5230]   Showalter, T. and N. Freed, "Sieve Email Filtering:
               Vacation Extension", RFC 5230, January 2008.

   [RFC5231]   Segmuller, W. and B. Leiba, "Sieve Email Filtering:
               Relational Extension", RFC 5231, January 2008.

   [RFC5232]   Melnikov, A., "Sieve Email Filtering: Imap4flags
               Extension", RFC 5232, January 2008.

   [RFC5233]   Murchison, K., "Sieve Email Filtering: Subaddress
               Extension", RFC 5233, January 2008.

   [RFC5235]   Daboo, C., "Sieve Email Filtering: Spamtest and Virustest
               Extensions", RFC 5235, January 2008.

   [RFC5293]   Degener, J. and P. Guenther, "Sieve Email Filtering:
               Editheader Extension", RFC 5293, August 2008.

   [RFC5703]   Hansen, T. and C. Daboo, "Sieve Email Filtering: MIME
               Part Tests, Iteration, Extraction, Replacement, and
               Enclosure", RFC 5703, October 2009.

















Freed & Vedam                Standards Track                   [Page 13]
^L
RFC 5784             An XML Representation for Sieve          March 2010


Appendix A.  Extended Example

   The example Sieve script given in Section 9 of [RFC5228] would be
   represented in XML as the following code component:

   <sieve xmlns="urn:ietf:params:xml:ns:sieve">
     <comment>
       Example Sieve Filter
       Declare any optional features or extensions used by the script
     </comment>

     <control name="require">
       <str>fileinto</str>
     </control>
     <comment>
       Handle messages from known mailing lists.
       Move messages from IETF filter discussion list to filter mailbox.
     </comment>
     <control name="if">
       <test name="header">
         <tag>is</tag>
         <str>Sender</str>
         <str>owner-ietf-mta-filters@imc.org</str>
       </test>
       <action name="fileinto">
         <str>filter</str>
       </action>
       <postamble>
         <comment>move to "filter" mailbox</comment>
       </postamble>
     </control>

     <comment>
       Keep all messages to or from people in my company.
     </comment>
     <control name="elsif">
       <test name="address">
         <tag>domain</tag>
         <tag>is</tag>
         <list>
           <str>From</str>
           <str>To</str>
         </list>
         <str>example.com</str>
       </test>
       <action name="keep"/>
     </control>




Freed & Vedam                Standards Track                   [Page 14]
^L
RFC 5784             An XML Representation for Sieve          March 2010


     <comment>
       Try and catch unsolicited email.  If a message is not to me,
       or it contains a subject known to be spam, file it away.
     </comment>
     <control name="elsif">
       <test name="anyof">
         <test name="not">
           <test name="address">
             <tag>all</tag>
             <tag>contains</tag>
             <list>
               <str>To</str>
               <str>Cc</str>
               <str>Bcc</str>
             </list>
             <str>me@example.com</str>
           </test>
         </test>
         <test name="header">
           <tag>matches</tag>
           <str>subject</str>
           <list>
             <str>*make*money*fast*</str>
             <str>*university*dipl*mas*</str>
           </list>
         </test>
       </test>
       <action name="fileinto">
         <str>spam</str>
       </action>
     </control>
     <control name="else">
       <preamble>
         <comment>
           Move all other (non-company) mail to "personal"
           mailbox.
         </comment>
       </preamble>
       <action name="fileinto">
         <str>personal</str>
       </action>
     </control>

   </sieve>







Freed & Vedam                Standards Track                   [Page 15]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   The same script could be annotated with graphical display hints in a
   variety of ways.  Three possible code components that do this are:

   <sieve xmlns="urn:ietf:params:xml:ns:sieve">

     <control name="require">
       <str>fileinto</str>
     </control>

     <displayblock name="File filter list mail" order="1"
                   group="FILE_TO_FOLDER" enable="true">
       <control name="if">
         <test name="header">
           <tag>is</tag>
           <str>Sender</str>
           <str>owner-ietf-mta-filters@imc.org</str>
         </test>
         <action name="fileinto">
           <str>filter</str>
         </action>
       </control>
     </displayblock>

     <displayblock name="Keep all company mail" order="2"
                   group="KEEP_MESSAGE" enable="true">
       <control name="elsif">
         <test name="address">
           <tag>domain</tag>
           <tag>is</tag>
           <list>
             <str>From</str>
             <str>To</str>
           </list>
           <str>example.com</str>
         </test>
         <action name="keep"/>
      </control>
     </displayblock>

     <displayblock name="File suspected spam" order="3"
                   group="FILE_TO_FOLDER" enable="true">
        <control name="elsif">
          <test name="anyof">
            <test name="not">
              <test name="address">
                <tag>all</tag>
                <tag>contains</tag>
                <list>



Freed & Vedam                Standards Track                   [Page 16]
^L
RFC 5784             An XML Representation for Sieve          March 2010


                  <str>To</str>
                  <str>Cc</str>
                  <str>Bcc</str>
                </list>
                <str>me@example.com</str>
              </test>
            </test>
            <test name="header">
              <tag>matches</tag>
              <str>subject</str>
              <list>
                <str>*make*money*fast*</str>
                <str>*university*dipl*mas*</str>
              </list>
            </test>
          </test>
          <action name="fileinto">
            <str>spam</str>
          </action>
        </control>
      </displayblock>

     <displayblock name="File noncompany mail as personal" order="4"
                   group="FILE_TO_FOLDER" enable="true">
       <control name="else">
         <action name="fileinto">
           <str>personal</str>
         </action>
       </control>
     </displayblock>

   </sieve>

   Note that since displayblock elements are semantically null as far as
   the script itself is concerned, they can be used to group structures
   like elsif and else that are tied to statements in other groups.















Freed & Vedam                Standards Track                   [Page 17]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   The representation of this script in regular Sieve syntax uses
   structured comments:

   require "fileinto";
   /* [* name="File filter list mail" order="1"
         group="FILE_TO_FOLDER" enable="true" */
   if header :is "Sender" "owner-ietf-mta-filters@imc.org"
   {
     fileinto "filter";
   }
   /* *] */
   /* [* name="Keep all company mail" order="2"
         group="KEEP_MESSAGE" enable="true" */
   elsif address :domain :is [ "From", "To" ] "example.com"
   {
     keep;
   }
   /* *] */
   /* [* name="File suspected spam" order="3"
         group="FILE_TO_FOLDER" enable="true" */
   elsif anyof ( not ( address :all :contains [ "To", "Cc", "Bcc" ]
                       "me@example.com" ),
                 header :matches "subject" [ "*make*money*fast*",
                                             "*university*dipl*mas*" ] )
   {
     fileinto "spam";
   }
   /* *] */
   /* [* name="File noncompany mail as personal" order="4"
         group="FILE_TO_FOLDER" enable="true" */
   else
   {
     fileinto "personal";
   }
   /* *] */
















Freed & Vedam                Standards Track                   [Page 18]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   A separate namespace can be used to embed text or structured
   information:

   <sieve xmlns="urn:ietf:params:xml:ns:sieve"
          xmlns:nls="http://example.com/nls">

     <nls:interpretation>
       If the email header "Sender" is owner-ietf-mta-filters@imc.org,
       then file it into the "filter" folder.

       Otherwise, if the address in the "From" or "To" has a domain
       that is "example.com", then keep it.

       Otherwise, messages meeting with any of these conditions:

       (1) None of the addresses in "To", "Cc", or "Bcc" contain
           the domain "example.com".

       (2) The "Subject" field matches the pattern *make*money*fast*
           or *university*dipl*mas*, then file it into the "spam"
           folder.

       If all else fails, then file the message in the "personal"
       folder.
     </nls:interpretation>

     ... the actual Sieve script ...

   </sieve>






















Freed & Vedam                Standards Track                   [Page 19]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   Alternately, displaydata elements can be used to accomplish the same
   thing:

   <sieve xmlns="urn:ietf:params:xml:ns:sieve">

     <displaydata>
       <nls-interpretation>
         If the email header "Sender" is owner-ietf-mta-filters@imc.org,
         then file it into the "filter" folder.

         Otherwise, if the address in the "From" or "To" has a domain
         that is "example.com", then keep it.

         Otherwise, messages meeting with any of these conditions:

         (1) None of the addresses in "To","Cc", or "Bcc" contain
             the domain "example.com".

         (2) The "Subject" field matches the pattern *make*money*fast*
             or *university*dipl*mas*, then file it into the "spam"
             folder.

         If all else fails, then file the message in the "personal"
         folder.
       </nls-interpretation>
     </displaydata>

     ... the actual Sieve script ...

   </sieve>





















Freed & Vedam                Standards Track                   [Page 20]
^L
RFC 5784             An XML Representation for Sieve          March 2010


   Again, structured comments are used to represent this in regular
   Sieve syntax:

     /* [|
       <nls-interpretation>
         If the email header "Sender" is owner-ietf-mta-filters@imc.org,
         then file it into the "filter" folder.

         Otherwise, if the address in the "From" or "To" has a domain
         that is "example.com", then keep it.

         Otherwise, messages meeting with any of these conditions:

         (1) None of the addresses in "To","Cc", or "Bcc" contain
             the domain "example.com".

         (2) The "Subject" field matches the pattern *make*money*fast*
             or *university*dipl*mas*, then file it into the "spam"
             folder.

         If all else fails, then file the message in the "personal"
         folder.
       </nls-interpretation>
     |] */

     ... the actual Sieve script ...

Appendix B.  XML Schema for Sieves in XML

   This appendix is informative.  The following code component is an XML
   Schema for the XML representation of Sieve scripts.  Most of the
   elements employing a complex content model allow use of elements in
   other namespaces, subject to lax XML Schema validation rules.
   Additionally, displaydata elements can be used to encapsulate
   arbitrary XML content.  Finally, displayblock elements can be used as
   a general-purpose grouping mechanism -- arbitrary attributes are
   allowed on displayblock elements.

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

   <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns="urn:ietf:params:xml:ns:sieve"
               targetNamespace="urn:ietf:params:xml:ns:sieve">

     <xsd:element name="sieve">
       <xsd:complexType>
         <xsd:sequence>
           <xsd:choice maxOccurs="unbounded" minOccurs="0">



Freed & Vedam                Standards Track                   [Page 21]
^L
RFC 5784             An XML Representation for Sieve          March 2010


             <xsd:element ref="control"/>
             <xsd:element ref="action"/>
             <xsd:element ref="displayblock"/>
             <xsd:element ref="displaydata"/>
             <xsd:element ref="comment"/>
             <xsd:any namespace="##other" processContents="lax"/>
           </xsd:choice>
         </xsd:sequence>
       </xsd:complexType>
     </xsd:element>

     <xsd:element name="comment" type="xsd:string"/>

     <xsd:complexType name="ambles">
       <xsd:choice minOccurs="0" maxOccurs="unbounded">
         <xsd:element ref="displaydata"/>
         <xsd:element ref="comment"/>
         <xsd:any namespace="##other" processContents="lax"/>
      </xsd:choice>
     </xsd:complexType>

     <xsd:element name="preamble" type="ambles"/>
     <xsd:element name="postamble" type="ambles"/>

     <xsd:complexType name="command">
       <xsd:sequence>
         <xsd:element ref="preamble" minOccurs="0" maxOccurs="1"/>
         <xsd:choice minOccurs="0" maxOccurs="unbounded">
           <xsd:element ref="str"/>
           <xsd:element ref="num"/>
           <xsd:element ref="list"/>
           <xsd:element ref="tag"/>
         </xsd:choice>
         <xsd:element ref="test" minOccurs="0" maxOccurs="1"/>
         <xsd:choice minOccurs="0" maxOccurs="unbounded">
           <xsd:element ref="control"/>
           <xsd:element ref="action"/>
           <xsd:element ref="displayblock"/>
         </xsd:choice>
         <xsd:element ref="postamble" minOccurs="0" maxOccurs="1"/>
       </xsd:sequence>
       <xsd:attribute use="required" name="name" type="identifier"/>
     </xsd:complexType>

     <xsd:element name="control" type="command"/>
     <xsd:element name="action" type="command"/>

     <xsd:element name="test">



Freed & Vedam                Standards Track                   [Page 22]
^L
RFC 5784             An XML Representation for Sieve          March 2010


       <xsd:complexType>
         <xsd:sequence>
           <xsd:choice minOccurs="0" maxOccurs="unbounded">
             <xsd:element ref="str"/>
             <xsd:element ref="num"/>
             <xsd:element ref="list"/>
             <xsd:element ref="tag"/>
             <xsd:element ref="displaydata"/>
             <xsd:element ref="comment"/>
             <xsd:any namespace="##other" processContents="lax"/>
           </xsd:choice>
           <xsd:element ref="test" minOccurs="0"
                        maxOccurs="unbounded"/>
         </xsd:sequence>
         <xsd:attribute use="required" name="name" type="identifier"/>
       </xsd:complexType>
     </xsd:element>

     <xsd:element name="list">
       <xsd:complexType>
         <xsd:sequence>
           <xsd:element ref="str" minOccurs="1"
                        maxOccurs="unbounded"/>
         </xsd:sequence>
       </xsd:complexType>
     </xsd:element>

     <xsd:element name="tag" type="identifier"/>

     <xsd:element name="str" type="xsd:string"/>

     <xsd:element name="num" type="xsd:nonNegativeInteger"/>

     <xsd:simpleType name="identifier">
       <xsd:restriction base="xsd:token">
         <xsd:pattern value="[A-Za-z_][A-Za-z0-9_]*"/>
       </xsd:restriction>
     </xsd:simpleType>

     <xsd:element name="displayblock">
       <xsd:complexType>
         <xsd:sequence>
           <xsd:choice minOccurs="0" maxOccurs="unbounded">
             <xsd:element ref="control"/>
             <xsd:element ref="action"/>
             <xsd:element ref="displayblock"/>
             <xsd:element ref="displaydata"/>
             <xsd:element ref="comment"/>



Freed & Vedam                Standards Track                   [Page 23]
^L
RFC 5784             An XML Representation for Sieve          March 2010


             <xsd:any namespace="##other" processContents="lax"/>
           </xsd:choice>
         </xsd:sequence>
         <xsd:anyAttribute processContents="skip"/>
       </xsd:complexType>
     </xsd:element>

     <xsd:element name="displaydata">
       <xsd:complexType>
         <xsd:sequence minOccurs="0" maxOccurs="unbounded">
           <xsd:any processContents="skip"/>
         </xsd:sequence>
       </xsd:complexType>
     </xsd:element>

   </xsd:schema>

Appendix C.  Relax NG Schema for Sieves in XML

   This appendix is informative.  The following code component defines a
   Relax NG Schema using compact notation OASISRNC [OASISRNC] for the
   XML representation of Sieve scripts.  Most of the elements employing
   a complex content model allow unrestricted use of elements in other
   namespaces.  Additionally, displaydata elements can be used to
   encapsulate arbitrary XML content.  Finally, displayblock elements
   can be used as a general-purpose grouping mechanism -- arbitrary
   attributes are allowed on displayblock elements.

   namespace sieve = "urn:ietf:params:xml:ns:sieve"

   start = element sieve:sieve { ( control | action | displayblock |
                                   displaydata | comment | ext )* }

   comment = element sieve:comment { xsd:string }

   ambles =
      (
         ( displaydata | comment | ext )*
      ),
      empty

   preamble = element sieve:preamble { ambles }
   postamble = element sieve:postamble { ambles }

   command =
      (
         attribute name {
           xsd:token {



Freed & Vedam                Standards Track                   [Page 24]
^L
RFC 5784             An XML Representation for Sieve          March 2010


             pattern = "[A-Za-z_][A-Za-z0-9_]*" } },
         preamble?,
         ( str | num | \list | tag )*,
         test?,
         ( control | action | displayblock)*,
         postamble?
      ),
      empty

   control = element sieve:control { command }
   action = element sieve:action { command }

   test =
      element sieve:test
      {
         attribute name {
           xsd:token {
             pattern = "[A-Za-z_][A-Za-z0-9_]*" } },
         ( str | num | \list | tag | comment | ext )*,
         test*
      }

   \list = element sieve:list { str+ }

   tag = element sieve:tag {
           xsd:token {
             pattern = "[A-Za-z_][A-Za-z0-9_]*" } }

   str = element sieve:str { xsd:string }

   num = element sieve:num { xsd:nonNegativeInteger }

   any = ( element * { any } | attribute * { text } | text )*

   ext = element * - sieve:* { any }*

   displayblock =
      element sieve:displayblock
      {
         ( control | action | displayblock |
           displaydata | comment | ext )*,
         attribute * { text }*
      }

   displaydata = element sieve:displaydata { any* }






Freed & Vedam                Standards Track                   [Page 25]
^L
RFC 5784             An XML Representation for Sieve          March 2010


Appendix D.  Stylesheet for Conversion from XML

   This appendix is informative.  The following code component is a
   stylesheet that can be used to convert the Sieve in XML
   representation to regular Sieve format.  Content in other namespaces,
   displaydata, and displayblock elements are converted to structured
   comments as appropriate.

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

   <!-- Convert Sieve in XML to standard Sieve syntax -->

   <xsl:stylesheet version="1.0"
                   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                   xmlns:sieve="urn:ietf:params:xml:ns:sieve">

     <xsl:output method="text" encoding="UTF-8"
                 media-type="application/sieve"/>

     <!-- Only preserve whitespace in str elements -->
     <xsl:strip-space elements="*"/>
     <xsl:preserve-space elements="sieve:str"/>

     <!-- Match top level sieve node,
          start processing in sieve mode -->

     <xsl:template match="sieve:sieve">
       <xsl:apply-templates select="*" mode="sieve">
         <xsl:with-param name="prefix" select="''"/>
       </xsl:apply-templates>
     </xsl:template>

     <!-- Routine to properly literalize quotes in Sieve strings -->

     <xsl:template name="quote-string">
       <xsl:param name="str"/>
       <xsl:choose>
         <xsl:when test="not($str)"/>
         <xsl:when test="contains($str, '&quot;')">
           <xsl:call-template name="quote-string">
             <xsl:with-param name="str"
                select="substring-before($str, '&quot;')"/>
           </xsl:call-template>
           <xsl:text>\&quot;</xsl:text>
           <xsl:call-template name="quote-string">
             <xsl:with-param name="str"
                select="substring-after($str, '&quot;')"/>
           </xsl:call-template>



Freed & Vedam                Standards Track                   [Page 26]
^L
RFC 5784             An XML Representation for Sieve          March 2010


         </xsl:when>
         <xsl:when test="contains($str, '\')">
           <xsl:call-template name="quote-string">
             <xsl:with-param name="str"
                select="substring-before($str, '\')"/>
           </xsl:call-template>
           <xsl:text>\\</xsl:text>
           <xsl:call-template name="quote-string">
             <xsl:with-param name="str"
                select="substring-after($str, '\')"/>
           </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
           <xsl:value-of select="$str"/>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:template>

     <!-- Sieve mode processing templates -->

     <xsl:template match="sieve:control|sieve:action" mode="sieve">
       <xsl:param name="prefix"/>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:value-of select="@name"/>
       <xsl:variable name="blockbegin"
               select="generate-id(sieve:control|sieve:action)"/>
       <xsl:for-each select="*">
         <xsl:choose>
           <xsl:when test="self::sieve:str|self::sieve:num|
                           self::sieve:list|self::sieve:tag|
                           self::sieve:test">
             <xsl:apply-templates select="." mode="sieve"/>
           </xsl:when>
           <xsl:when test="generate-id(.) = $blockbegin">
             <xsl:text xml:space="preserve">
   </xsl:text>
             <xsl:value-of select="$prefix"/>
             <xsl:text>{</xsl:text>
             <xsl:apply-templates select="." mode="sieve">
               <xsl:with-param name="prefix"
                  select="concat($prefix, '  ')"/>
             </xsl:apply-templates>
           </xsl:when>
           <xsl:otherwise>
             <xsl:apply-templates select="." mode="sieve">
               <xsl:with-param name="prefix"



Freed & Vedam                Standards Track                   [Page 27]
^L
RFC 5784             An XML Representation for Sieve          March 2010


                  select="concat($prefix, '  ')"/>
             </xsl:apply-templates>
           </xsl:otherwise>
         </xsl:choose>
       </xsl:for-each>
       <xsl:choose>
         <xsl:when test="count(sieve:control|sieve:action) &gt; 0">
           <xsl:text xml:space="preserve">
   </xsl:text>
           <xsl:value-of select="$prefix"/>
           <xsl:text>}</xsl:text>
         </xsl:when>
         <xsl:otherwise>
           <xsl:text>;</xsl:text>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:template>

     <xsl:template match="sieve:preamble|sieve:postamble" mode="sieve">
       <xsl:param name="prefix"/>
       <xsl:apply-templates mode="sieve">
         <xsl:with-param name="prefix" select="$prefix"/>
       </xsl:apply-templates>
     </xsl:template>

     <xsl:template match="sieve:test" mode="sieve">
       <xsl:text xml:space="preserve"> </xsl:text>
       <xsl:value-of select="@name"/>
       <xsl:apply-templates select="*[not(self::sieve:test)]"
                            mode="sieve"/>
       <xsl:if test="count(descendant::sieve:test) &gt; 0">
         <xsl:text> (</xsl:text>
         <xsl:for-each select="sieve:test">
           <xsl:apply-templates select="." mode="sieve"/>
           <xsl:if test="count(following-sibling::sieve:test) &gt; 0">
             <xsl:text>,</xsl:text>
           </xsl:if>
         </xsl:for-each>
         <xsl:text> )</xsl:text>
       </xsl:if>
     </xsl:template>

     <xsl:template match="sieve:str" mode="sieve">
       <xsl:text> &quot;</xsl:text>
       <xsl:call-template name="quote-string">
         <xsl:with-param name="str" select="text()"/>
       </xsl:call-template>
       <xsl:text>&quot;</xsl:text>



Freed & Vedam                Standards Track                   [Page 28]
^L
RFC 5784             An XML Representation for Sieve          March 2010


     </xsl:template>

     <xsl:template match="sieve:num" mode="sieve">
       <xsl:text xml:space="preserve"> </xsl:text>
       <!-- Use numeric suffixes when possible -->
       <xsl:choose>
         <xsl:when test="(number(text()) mod 1073741824) = 0">
           <xsl:value-of select="number(text()) div 1073741824"/>
           <xsl:text>G</xsl:text>
         </xsl:when>
         <xsl:when test="(number(text()) mod 1048576) = 0">
           <xsl:value-of select="number(text()) div 1048576"/>
           <xsl:text>M</xsl:text>
         </xsl:when>
         <xsl:when test="(number(text()) mod 1024) = 0">
           <xsl:value-of select="number(text()) div 1024"/>
           <xsl:text>K</xsl:text>
         </xsl:when>
         <xsl:otherwise>
           <xsl:value-of select="text()"/>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:template>

     <xsl:template match="sieve:list" mode="sieve">
       <xsl:text> [</xsl:text>
       <xsl:for-each select="sieve:str">
         <xsl:apply-templates select="." mode="sieve"/>
         <xsl:if test="count(following-sibling::sieve:str) &gt; 0">
           <xsl:text>,</xsl:text>
         </xsl:if>
       </xsl:for-each>
       <xsl:text> ]</xsl:text>
     </xsl:template>

     <xsl:template match="sieve:tag" mode="sieve">
       <xsl:text> :</xsl:text>
       <xsl:value-of select="text()"/>
     </xsl:template>

     <xsl:template match="sieve:comment" mode="sieve">
       <xsl:param name="prefix"/>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>/*</xsl:text>
       <xsl:value-of select="."/>
       <xsl:value-of select="$prefix"/>



Freed & Vedam                Standards Track                   [Page 29]
^L
RFC 5784             An XML Representation for Sieve          March 2010


       <xsl:text>*/</xsl:text>
     </xsl:template>

     <!-- Convert display information into structured comments -->

     <xsl:template match="sieve:displayblock" mode="sieve">
       <xsl:param name="prefix"/>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>/* [*</xsl:text>
       <xsl:apply-templates select="@*" mode="copy"/>
       <xsl:text> */</xsl:text>
       <xsl:apply-templates select="*" mode="sieve">
         <xsl:with-param name="prefix" select="$prefix"/>
       </xsl:apply-templates>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>/* *] */</xsl:text>
     </xsl:template>

     <xsl:template match="sieve:displaydata" mode="sieve">
       <xsl:param name="prefix"/>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>/* [|</xsl:text>
       <xsl:apply-templates mode="copy">
         <xsl:with-param name="prefix"
                         select="concat($prefix, '  ')"/>
       </xsl:apply-templates>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>   |] */</xsl:text>
     </xsl:template>

     <!-- Copy unrecnognized nodes and their descendants -->

     <xsl:template match="*" mode="sieve">
       <xsl:param name="prefix"/>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>/* [/</xsl:text>
       <xsl:apply-templates select="." mode="copy">
         <xsl:with-param name="prefix"



Freed & Vedam                Standards Track                   [Page 30]
^L
RFC 5784             An XML Representation for Sieve          March 2010


                         select="concat($prefix, '  ')"/>
       </xsl:apply-templates>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>   /] */</xsl:text>
     </xsl:template>

     <!-- Copy mode processing templates -->

     <xsl:template match="*[not(node())]" mode="copy">
       <xsl:param name="prefix"/>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>&lt;</xsl:text>
       <xsl:value-of select="name()"/>
       <xsl:apply-templates select="@*" mode="copy"/>
       <xsl:text>/&gt;</xsl:text>
     </xsl:template>

     <xsl:template match="*[node()]" mode="copy">
       <xsl:param name="prefix"/>
       <xsl:text xml:space="preserve">
   </xsl:text>
       <xsl:value-of select="$prefix"/>
       <xsl:text>&lt;</xsl:text>
       <xsl:value-of select="name()"/>
       <xsl:apply-templates select="@*" mode="copy"/>
       <xsl:text>&gt;</xsl:text>
       <xsl:apply-templates mode="copy">
         <xsl:with-param name="prefix"
                         select="concat($prefix, '  ')"/>
       </xsl:apply-templates>
       <xsl:if test="*[last()][not(text())]">
         <xsl:text xml:space="preserve">
   </xsl:text>
         <xsl:value-of select="$prefix"/>
       </xsl:if>
       <xsl:text>&lt;/</xsl:text>
       <xsl:value-of select="name()"/>
       <xsl:text>&gt;</xsl:text>
     </xsl:template>

     <xsl:template match="@*" mode="copy">
       <xsl:text> </xsl:text>
       <xsl:value-of select="name()"/>
       <xsl:text>="</xsl:text>



Freed & Vedam                Standards Track                   [Page 31]
^L
RFC 5784             An XML Representation for Sieve          March 2010


       <xsl:value-of select="."/>
       <xsl:text>"</xsl:text>
     </xsl:template>

   </xsl:stylesheet>

Appendix E.  Acknowledgements

   The stylesheet copy mode code is loosely based on a sample code
   posted to the xsl-list list by Americo Albuquerque.  Jari Arkko,
   Robert Burrell Donkin, Andrew McKeon, Alexey Melnikov, and Aaron
   Stone provided useful comments on the document.

Authors' Addresses

   Ned Freed
   Sun Microsystems
   800 Royal Oaks
   Monrovia, CA  91016-6347
   USA

   Phone: +1 909 457 4293
   EMail: ned.freed@mrochek.com


   Srinivas Saisatish Vedam
   Sun Microsystems

   Phone: +91 80669 27577
   EMail: Srinivas.Sv@Sun.COM





















Freed & Vedam                Standards Track                   [Page 32]
^L