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
  
// -*- Pike -*- 
// 
// RFC1521 functionality for Pike 
// 
// Marcus Comstedt 1996-1999 
 
 
//! @rfc{1521@}, the @b{Multipurpose Internet Mail Extensions@} memo, defines a 
//! structure which is the base for all messages read and written by 
//! modern mail and news programs.  It is also partly the base for the 
//! HTTP protocol.  Just like @rfc{822@}, MIME declares that a message should 
//! consist of two entities, the headers and the body.  In addition, the 
//! following properties are given to these two entities: 
//! 
//! @dl 
//!  @item Headers 
//!  @ul 
//!   @item 
//!     A MIME-Version header must be present to signal MIME compatibility 
//!   @item 
//!     A Content-Type header should be present to describe the nature of 
//!     the data in the message body.  Seven major types are defined, and an 
//!     extensive number of subtypes are available.  The header can also 
//!     contain attributes specific to the type and subtype. 
//!   @item 
//!     A Content-Transfer-Encoding may be present to notify that the data 
//!     of the body is encoded in some particular encoding. 
//!  @endul 
//!  @item Body 
//!  @ul 
//!   @item 
//!     Raw data to be interpreted according to the Content-Type header 
//!   @item 
//!     Can be encoded using one of several Content-Transfer-Encodings to 
//!     allow transport over non 8bit clean channels 
//!  @endul 
//! @enddl 
//! 
//! The MIME module can extract and analyze these two entities from a stream 
//! of bytes.  It can also recreate such a stream from these entities. 
//! To encapsulate the headers and body entities, the class @[MIME.Message] is 
//! used.  An object of this class holds all the headers as a mapping from 
//! string to string, and it is possible to obtain the body data in either 
//! raw or encoded form as a string.  Common attributes such as message type 
//! and text char set are also extracted into separate variables for easy 
//! access. 
//! 
//! The Message class does not make any interpretation of the body 
//! data, unless the content type is @tt{multipart@}.  A multipart 
//! message contains several individual messages separated by boundary 
//! strings.  The @[Message->create] method of the Message class will 
//! divide a multipart body on these boundaries, and then create 
//! individual Message objects for each part.  These objects will be 
//! collected in the array @[Message->body_parts] within the original 
//! Message object. If any of the new @[Message] objects have a body of 
//! type multipart, the process is of course repeated recursively. 
 
 
#pike __REAL_VERSION__ 
inherit ___MIME; 
 
//! Class representing a substring of a larger string. 
//! 
//! This class is used to reduce the number of string copies 
//! during parsing of @[MIME.Message]s. 
protected class StringRange 
{ 
  string data; 
  int start;        // Inclusive. 
  int end;  // Exclusive. 
  protected void create(string|StringRange s, int start, int end) 
  { 
    if (start == end) { 
      data = ""; 
      this::start = this::end = 0; 
      return; 
    } 
    if (start < 0) start = 0; 
    if (end < 0) end = 0; 
    if (objectp(s)) { 
      start += s->start; 
      if (start > s->end) start = s->end; 
      end += s->start; 
      if (end > s->end) end = s->end; 
      s = s->data; 
    } 
    if ((end - start)*16 < sizeof(s)) { 
      s = s[start..end-1]; 
      end -= start; 
      start = 0; 
    } 
    data = s; 
    this::start = start; 
    this::end = end; 
  } 
  protected int _sizeof() 
  { 
    return end-start; 
  } 
  protected string|StringRange `[..](int low, int ltype, int high, int htype) 
  { 
    int len = end - start; 
    if (ltype == Pike.INDEX_FROM_END) { 
      low = len - (low + 1); 
    } 
    high += 1; 
    if (htype == Pike.INDEX_FROM_END) { 
      high = len - high; 
    } else if (htype == Pike.OPEN_BOUND) { 
      high = len; 
    } 
    if (low < 0) low = 0; 
    if (high < 0) high = 0; 
    if (low > len) low = len; 
    if (high > len) high = len; 
    if (!low && (high == len)) return this; 
    if ((high - low) < 65536) return data[start+low..start+high-1]; 
    return StringRange(this, low, high); 
  } 
  protected int `[](int pos) 
  { 
    int npos = pos; 
    if (npos < 0) { 
      npos += end; 
      if (npos < start) { 
        error("Index out of range [-%d..%d]\n", 1 + end-start, end-start); 
      } 
    } else { 
      npos += start; 
      if (npos >= end) { 
        error("Index out of range [-%d..%d]\n", 1 + end-start, end-start); 
      } 
    } 
    return data[npos]; 
  } 
  protected mixed cast(string type) 
  { 
    if( type == "string" ) 
      return data[start..end-1]; 
    return UNDEFINED; 
  } 
  protected int _search(string frag, int|void pos) 
  { 
    if (pos < 0) 
      error("Start must be greater or equal to zero.\n"); 
    int npos = pos + start; 
    if (npos > end) 
      error("Start must not be greater than the length of the string.\n"); 
    if ((npos + sizeof(frag)) > end) return -1; 
    npos = search(data, frag, npos); 
    if (npos < 0) return npos; 
    if ((npos + sizeof(frag)) > end) return -1; 
    return npos - start; 
  } 
  protected string _sprintf(int c) 
  { 
    if (c == 'O') 
      return sprintf("StringRange(%d bytes[%d..%d] %O)", 
                     data && sizeof(data), start, end-1, data && data[..40]); 
    return (string)this; 
  } 
} 
 
#if (__REAL_VERSION__ < 7.8) || ((__REAL_VERSION__) < 7.9 && (__REAL_BUILD__ < 413)) 
// Compat with some older Pikes... 
 
// Support has_prefix on objects. 
protected int(0..1) has_prefix(string|object s, string prefix) 
{ 
  if (!objectp(s)) return predef::has_prefix(s, prefix); 
  for(int i = 0; i < sizeof(prefix); i++) { 
    if (s[i] != prefix[i]) return 0; 
  } 
  return 1; 
} 
 
#endif 
 
//! This function will create a string that can be used as a separator string 
//! for multipart messages.  The generated string is guaranteed not to appear 
//! in @tt{base64@}, @tt{quoted-printable@}, or @tt{x-uue@} encoded data. 
//! It is also unlikely to appear in normal text.  This function is used by 
//! the cast method of the @tt{Message@} class if no boundary string is 
//! specified. 
//! 
string generate_boundary( ) 
{ 
  return "'ThIs-RaNdOm-StRiNg-/=_."+random( 1000000000 )+":"; 
} 
 
//! Extract raw data from an encoded string suitable for transport between 
//! systems. 
//! 
//! The encoding can be any of 
//! @string 
//!   @value "7bit" 
//!   @value "8bit" 
//!   @value "base64" 
//!   @value "binary" 
//!   @value "quoted-printable" 
//!   @value "x-uue" 
//!   @value "x-uuencode" 
//! @endstring 
//! 
//! The encoding string is not case sensitive. 
//! 
//! @seealso 
//! @[MIME.encode()] 
//! 
string|StringRange decode( string|StringRange data, string encoding ) 
{ 
  switch (lower_case( encoding || "binary" )) { 
  case "base64": 
    return decode_base64( (string)data ); 
  case "quoted-printable": 
    return decode_qp( (string)data ); 
  case "x-uue": 
  case "x-uuencode": 
    return decode_uue( (string)data ); 
  case "7bit": 
  case "8bit": 
  case "binary": 
    return data; 
  default: 
    error("Unknown transfer encoding %s.\n", encoding); 
  } 
} 
 
//! Encode raw data into something suitable for transport to other systems. 
//! 
//! The encoding can be any of 
//! @string 
//!   @value "7bit" 
//!   @value "8bit" 
//!   @value "base64" 
//!   @value "binary" 
//!   @value "quoted-printable" 
//!   @value "x-uue" 
//!   @value "x-uuencode" 
//! @endstring 
//! 
//! The encoding string is not case sensitive.  For the @tt{x-uue@} encoding, 
//! an optional @[filename] string may be supplied. 
//! 
//! If a nonzero value is passed as @[no_linebreaks], the result string 
//! will not contain any linebreaks (@tt{base64@} and @tt{quoted-printable@} 
//! only). 
//! 
//! @seealso 
//! @[MIME.decode()] 
//! 
string encode( string data, string encoding, void|string filename, 
               void|int no_linebreaks ) 
{ 
  switch (lower_case( encoding || "binary" )) { 
  case "base64": 
    return encode_base64( data, no_linebreaks ); 
  case "quoted-printable": 
    return encode_qp( data, no_linebreaks ); 
  case "x-uue": 
  case "x-uuencode": 
    return encode_uue( data, filename ); 
  case "7bit": 
  case "8bit": 
  case "binary": 
    return data; 
  default: 
    error("Unknown transfer encoding %s.\n", encoding); 
  } 
} 
 
//! Extracts the textual content and character set from an @i{encoded word@} 
//! as specified by @rfc{1522@}.  The result is an array where the first element 
//! is the raw text, and the second element the name of the character set. 
//! If the input string is not an encoded word, the result is still an array, 
//! but the char set element will be set to 0. 
//! 
//! @note 
//! Note that this function can only be applied to individual encoded words. 
//! 
//! @seealso 
//! @[MIME.encode_word()] 
//! 
array(string) decode_word( string word ) 
{ 
  string charset, encoding, encoded_text; 
  if (sscanf( word, 
              "=?%[^][ \t()<>@,;:\"\\/?.=]?%[^][ \t()<>@,;:\"\\/?.=]?%s?=", 
              charset, encoding, encoded_text) == 3 ) { 
    switch (lower_case( encoding )) { 
    case "b": 
      encoding = "base64"; 
      break; 
    case "q": 
      encoding = "quoted-printable"; 
      break; 
    default: 
      error( "Invalid rfc1522 encoding %s.\n", encoding ); 
    } 
    return ({ decode( replace( encoded_text, "_", " " ), encoding ), 
              lower_case( charset ) }); 
  } else 
    return ({ word, 0 }); 
} 
 
//! Create an @i{encoded word@} as specified in @rfc{1522@} from an array 
//! containing a raw text string and a char set name. 
//! 
//! The text will be transfer encoded according to the encoding argument, 
//! which can be either @expr{"base64"@} or @expr{"quoted-printable"@} 
//! (or either @expr{"b"@} or @expr{"q"@} for short). 
//! 
//! If either the second element of the array (the char set name), or 
//! the encoding argument is 0, the raw text is returned as is. 
//! 
//! @seealso 
//! @[MIME.decode_word()] 
//! 
string encode_word( string|array(string) word, string encoding ) 
{ 
  if (stringp(word)) 
    return word; 
  if (!encoding || !word[1]) 
    return word[0]; 
  switch (lower_case(encoding)) { 
  case "b": 
  case "base64": 
    encoding = "base64"; 
    break; 
  case "q": 
  case "quoted-printable": 
    encoding = "quoted-printable"; 
    break; 
  default: 
    error( "Invalid rfc1522 encoding %s.\n", encoding); 
  } 
  string enc = encode( word[0], encoding, 0, 1 ); 
  if (encoding == "quoted-printable") 
    enc = replace( enc, ({ "?", "_", "(", ")", "\\", "\"" }), 
                   ({ "=3F", "=5F", "=28", "=29", "=5C", "=22" }) ); 
  return "=?"+word[1]+"?"+encoding[0..0]+"?"+ enc +"?="; 
} 
 
protected string remap(array(string) item) 
{ 
  if (sizeof(item)>1 && item[1]) 
    return Charset.decoder(item[1])->feed(item[0])->drain(); 
  else 
    return item[0]; 
} 
 
protected array(string) reremap(string word, string|function(string:string) selector, 
                             string|void replacement,function(string:string)|void repcb) 
{ 
  if(max(@values(word))<128) 
    return ({ word,0 }); 
  string s = stringp(selector)? selector : selector(word); 
  return s? 
    ({ Charset.encoder(s,replacement,repcb)->feed(word)->drain(), s }) : 
    ({ word,0 }); 
} 
 
//! Separates a header value containing @i{text@} into units and calls 
//! @[MIME.decode_word()] on them.  The result is an array where each element 
//! is a result from @[decode_word()]. 
//! 
//! @seealso 
//! @[MIME.decode_words_tokenized] 
//! @[MIME.decode_words_text_remapped] 
//! 
array(array(string)) decode_words_text( string txt ) 
{ 
  object r = Regexp("^(.*[ \t\n\r]|)(=\\?[^\1- ?]*\\?[^\1- ?]*\\?" 
                    "[^\1- ?]*\\?=)(([ \t\n\r]+)(.*)|)$"); 
  array a, res = ({}); 
  while ((a = r->split(txt))) 
  { 
    if(!sizeof(a[2])) a = a[..2]+({"",""}); 
    txt = a[0]||""; 
    if(!sizeof(res) || sizeof(a[4])) a[4]=a[3]+a[4]; 
    array w = decode_word(a[1]); 
    if (sizeof(a[4])) 
      res = ({ w, ({ a[4], 0 }) }) + res; 
    else 
      res = ({ w }) + res; 
  } 
  a = res; 
  res = ({}); 
  if (sizeof(txt)) res = ({ ({ txt, 0 }) }); 
  foreach(a, array(string) word) { 
    if (sizeof(res) && res[-1][1] && (res[-1][1] == word[1])) { 
      // Same character set as previous word -- Join the fragments. 
      // This is a workaround for MUA's that split 
      // the text in the middle of encoded characters. 
      // eg PHPMailer [version 1.73] 
      res[-1][0] += word[0]; 
    } else { 
      res += ({ word }); 
    } 
  } 
  return res; 
} 
 
//! Like @[MIME.decode_words_text()], but the extracted strings are 
//! also remapped from their specified character encoding into UNICODE, 
//! and then pasted together.  The result is thus a string in the original 
//! text format, without @rfc{1522@} escapes, and with all characters in UNICODE 
//! encoding. 
//! 
//! @seealso 
//! @[MIME.decode_words_tokenized_remapped] 
//! 
string decode_words_text_remapped( string txt ) 
{ 
  return Array.map(decode_words_text(txt), remap)*""; 
} 
 
//! Tokenizes a header value just like @[MIME.tokenize()], but also 
//! converts encoded words using @[MIME.decode_word()].  The result is 
//! an array where each element is either an @expr{int@} representing 
//! a special character, or an @expr{array@} as returned by 
//! @[decode_word()] representing an atom or a quoted string. 
//! 
//! @seealso 
//! @[MIME.decode_words_tokenized_labled] 
//! @[MIME.decode_words_tokenized_remapped] 
//! @[MIME.decode_words_text] 
//! 
array(array(string)|int) decode_words_tokenized( string phrase, int|void flags ) 
{ 
  return Array.map(tokenize(phrase, flags), 
                   lambda(string|int item) { 
                     return intp(item)? item : decode_word(item); 
                   }); 
} 
 
//! Like @[MIME.decode_words_tokenized()], but the extracted atoms are 
//! also remapped from their specified character encoding into UNICODE. 
//! The result is thus identical to that of @[MIME.tokenize()], but 
//! without @rfc{1522@} escapes, and with all characters in UNICODE encoding. 
//! 
//! @seealso 
//! @[MIME.decode_words_tokenized_labled_remapped] 
//! @[MIME.decode_words_text_remapped] 
//! 
array(string|int) decode_words_tokenized_remapped( string phrase, 
                                                   int|void flags ) 
{ 
  return Array.map(decode_words_tokenized(phrase, flags), 
                   lambda(array(string)|int item) { 
                     return intp(item)? item : remap(item); 
                   }); 
} 
 
//! Tokenizes and labels a header value just like @[MIME.tokenize_labled()], 
//! but also converts encoded words using @[MIME.decode_word()].  The result 
//! is an array where each element is an array of two or more elements, the 
//! first being the label.  The rest of the array depends on the label: 
//! 
//! @string 
//!   @value "special" 
//!     One additional element, containing the character code for the special 
//!     character as an @expr{int@}. 
//!   @value "word" 
//!     Two additional elements, the first being the word, and the second 
//!     being the character set of this word (or 0 if it did not originate 
//!     from an encoded word). 
//!   @value "domain-literal" 
//!     One additional element, containing the domain literal as a string. 
//!   @value "comment" 
//!     One additional element, containing an array as returned by 
//!     @[MIME.decode_words_text()]. 
//! @endstring 
//! 
//! @seealso 
//! @[MIME.decode_words_tokenized_labled_remapped] 
//! 
array(array(string|int|array(array(string)))) 
decode_words_tokenized_labled( string phrase, int|void flags ) 
{ 
  return Array.map( tokenize_labled( phrase, flags ), 
                    lambda(array(string|int) item) { 
                      switch(item[0]) { 
                      case "encoded-word": 
                        return ({ "word", @decode_word(item[1]) }); 
                      case "word": 
                        return item + ({ 0 }); 
                      case "comment": 
                        return ({ "comment", decode_words_text(item[1]) }); 
                      default: 
                        return item; 
                      } 
                    }); 
} 
 
//! Like @[MIME.decode_words_tokenized_labled()], but the extracted words are 
//! also remapped from their specified character encoding into UNICODE. 
//! The result is identical to that of @[MIME.tokenize_labled()], but 
//! without @rfc{1522@} escapes, and with all characters in UNICODE encoding. 
//! 
array(array(string|int)) 
decode_words_tokenized_labled_remapped(string phrase, int|void flags) 
{ 
  return Array.map(decode_words_tokenized_labled(phrase, flags), 
                   lambda(array(string|int|array(array(string|int))) item) { 
                     switch(item[0]) { 
                     case "word": 
                       return ({ "word", remap(item[1..]) }); 
                     case "comment": 
                       return ({ "comment", Array.map(item[1], remap)*"" }); 
                     default: 
                       return item; 
                     } 
                   }); 
} 
 
//! The inverse of @[decode_words_text()], this function accepts 
//! an array of strings or pairs of strings which will each be encoded 
//! by @[encode_word()], after which they are all pasted together. 
//! 
//! @param encoding 
//!   Either @expr{"base64"@} or @expr{"quoted-printable"@} 
//!  (or either @expr{"b"@} or @expr{"q"@} for short). 
//! 
//! @seealso 
//! @[MIME.encode_words_text_remapped] 
//! 
string encode_words_text(array(string|array(string)) phrase, string encoding) 
{ 
  phrase = filter(phrase, lambda(string|array(string) w) { 
                            return stringp(w)? sizeof(w) : 
                              sizeof(w[0]) || w[1]; 
                          }); 
  array(string) ephrase = map(phrase, encode_word, encoding); 
  if(!encoding) return ephrase*""; 
  string res=""; 
  for(int i=0; i<sizeof(ephrase); i++) 
    if(ephrase[i] != (stringp(phrase[i])? phrase[i] : phrase[i][0])) { 
      if(sizeof(res) && !(<' ','\t','\n','\r'>)[res[-1]]) 
        res += " "; 
      res += ephrase[i]; 
      if(i+1<sizeof(ephrase) && !(<' ','\t','\n','\r'>)[ephrase[i+1][0]]) 
        res += " "; 
    } else 
      res += ephrase[i]; 
  return res; 
} 
 
//! This is the reverse of @[MIME.decode_words_text_remapped()].  A 
//! single UNICODE string is provided, which is separated into 
//! fragments and transcoded to selected character sets by this 
//! function as needed. 
//! 
//! @param encoding 
//!   Either @expr{"base64"@} or @expr{"quoted-printable"@} 
//!  (or either @expr{"b"@} or @expr{"q"@} for short). 
//! @param charset 
//!   Either the name of a character set to use, or a function returning 
//!   a character set to use given a text fragment as input. 
//! @param replacement 
//!   The @[replacement] argument to use when calling @[Charset.encoder] 
//! @param repcb 
//!   The @[repcb] argument to use when calling @[Charset.encoder] 
//! 
//! @seealso 
//! @[MIME.encode_words_tokenized_remapped] 
//! 
string encode_words_text_remapped(string text, string encoding, 
                                  string|function(string:string) charset, 
                                  string|void replacement, 
                                  function(string:string)|void repcb) 
{ 
  array(array(string)) out = ({}); 
  string lastword = ""; 
  while(sizeof(text)) { 
    sscanf(text, "%[ \t\n\r]%[^ \t\n\r]%s", string ws, string word, text); 
    array(string) ww = reremap(word, charset, replacement, repcb); 
    if(sizeof(ws)) 
      if(!ww[1]) 
        ww[0] = ws + ww[0]; 
      else if(!sizeof(out)) 
        out = ({({ws,0})}); 
      else if(!out[-1][1]) 
        out[-1][0] += ws; 
      else { 
        /* Two encoded words joined by whitespace - not possible */ 
        word = lastword+ws+word; 
        ww = reremap(word, charset, replacement, repcb); 
        out = out[..<1]; 
      } 
    lastword = word; 
    out += ({ ww }); 
  } 
  return encode_words_text(out, encoding); 
} 
 
//! The inverse of @[decode_words_tokenized()], this functions accepts 
//! an array like the argument to @[quote()], but instead of simple strings 
//! for atoms and quoted-strings, it will also accept pairs of strings to 
//! be passed to @[encode_word()]. 
//! 
//! @param encoding 
//!   Either @expr{"base64"@} or @expr{"quoted-printable"@} 
//!  (or either @expr{"b"@} or @expr{"q"@} for short). 
//! 
//! @seealso 
//!   @[MIME.encode_words_quoted_remapped()] 
//!   @[MIME.encode_words_quoted_labled()] 
//! 
string encode_words_quoted(array(array(string)|int) phrase, string encoding) 
{ 
  return quote(Array.map(phrase, lambda(array(string)|int item) { 
                                   return intp(item)? item : 
                                     encode_word(item, encoding); 
                                 })); 
} 
 
//! The inverse of @[decode_words_tokenized_remapped()], this functions 
//! accepts an array equivalent to the argument of @[quote()], but also 
//! performs on demand word encoding in the same way as 
//! @[encode_words_text_remapped()]. 
//! 
//! @seealso 
//!   @[MIME.encode_words_text_remapped()] 
//!   @[MIME.encode_words_quoted_labled_remapped()] 
//! 
string encode_words_quoted_remapped(array(string|int) phrase, string encoding, 
                                    string|function(string:string) charset, 
                                    string|void replacement, 
                                    function(string:string)|void repcb) 
{ 
  return encode_words_quoted(map(phrase, lambda(string|int item) { 
                                           return intp(item)? item : 
                                             reremap(item, charset, 
                                                     replacement, repcb); 
                                         }), encoding); 
} 
 
//! The inverse of @[decode_words_tokenized_labled()], this functions accepts 
//! an array like the argument to @[quote_labled()], but "word" labled 
//! elements can optionally contain an additional string element specifying 
//! a character set, in which case an encoded-word will be used.  Also, the 
//! format for "comment" labled elements is entirely different; instead of 
//! a single string, an array of strings or pairs like the first argument to 
//! @[encode_words_text()] is expected. 
//! 
//! @param encoding 
//!   Either @expr{"base64"@} or @expr{"quoted-printable"@} 
//!  (or either @expr{"b"@} or @expr{"q"@} for short). 
//! 
//! @seealso 
//!   @[MIME.encode_words_quoted()] 
//!   @[MIME.encode_words_quoted_labled_remapped()] 
//! 
string encode_words_quoted_labled(array(array(string|int|array(string|array(string)))) phrase, string encoding) 
{ 
  return 
    quote_labled(Array.map(phrase, 
                           lambda(array(string|int|array(string)) item) { 
                             switch(item[0]) { 
                             case "word": 
                               if(sizeof(item)>2 && item[2]) 
                                 return ({ 
                                   "encoded-word", 
                                   encode_word(item[1..], encoding) }); 
                               else 
                                 return item; 
                             case "comment": 
                               return ({ 
                                 "comment", 
                                 encode_words_text(item[1], encoding) }); 
                             default: 
                               return item; 
                             } 
                           })); 
} 
 
//! The inverse of @[decode_words_tokenized_labled_remapped()], this function 
//! accepts an array equivalent to the argument of @[quote_labled()], but also 
//! performs on demand word encoding in the same way as 
//! @[encode_words_text_remapped()]. 
//! 
string encode_words_quoted_labled_remapped(array(array(string|int)) phrase, 
                                           string encoding, 
                                           string|function(string:string) charset, 
                                           string|void replacement, 
                                           function(string:string)|void repcb) 
{ 
  return quote_labled(map(phrase, lambda(array(string|int) item) { 
                                    switch(item[0]) { 
                                    case "word": 
                                      item = item[..0]+reremap(item[1], 
                                                               charset, 
                                                               replacement, 
                                                               repcb); 
                                      if(sizeof(item)>2 && item[2]) 
                                        return ({ 
                                          "encoded-word", 
                                          encode_word(item[1..], encoding) }); 
                                      else 
                                        return item; 
                                    case "comment": 
                                      return ({ 
                                        "comment", 
                                        encode_words_text_remapped(item[1], 
                                                                   encoding, 
                                                                   charset, 
                                                                   replacement, 
                                                                   repcb) }); 
                                    default: 
                                      return item; 
                                    } 
                                  })); 
} 
 
//! Provide a reasonable default for the subtype field. 
//! 
//! Some pre-@rfc{1521@} mailers provide only a type and no subtype in the 
//! Content-Type header field.  This function can be used to obtain a 
//! reasonable default subtype given the type of a message.  (This is done 
//! automatically by the @[MIME.Message] class.) 
//! 
//! Currently, the function uses the following guesses: 
//! @string 
//!   @value "text" 
//!     @expr{"plain"@} 
//!   @value "message" 
//!     @expr{"rfc822"@} 
//!   @value "multipart" 
//!     @expr{"mixed"@} 
//! @endstring 
//! 
string guess_subtype( string type ) 
{ 
  switch (type) { 
  case "text": 
    return "plain"; 
  case "message": 
    return "rfc822"; 
  case "multipart": 
    return "mixed"; 
  } 
  return 0; 
} 
 
//! @decl array(mapping(string:string)|string) parse_headers(string message) 
//! @decl array(mapping(string:array(string))|string) parse_headers( @ 
//!                                                     string message, @ 
//!                                                     int(1..1) use_multiple) 
//! 
//! This is a low level function that will separate the headers from the body 
//! of an encoded message.  It will also translate the headers into a mapping. 
//! It will however not try to analyze the meaning of any particular header. 
//! This means that the body is returned as is, with any transfer-encoding 
//! intact. 
//! 
//! It is possible to call this function with just the header part 
//! of a message, in which case an empty body will be returned. 
//! 
//! The result is returned in the form of an array containing two elements. 
//! The first element is a mapping containing the headers found.  The second 
//! element is a string containing the body. 
//! 
//! Headers that occurr multiple times will have their contents NUL separated, 
//! unless @[use_multiple] has been specified, in which case the contents will 
//! be arrays. 
//! 
array(mapping(string:string|array(string))|string|StringRange) 
  parse_headers(string|StringRange message, void|int(1..1) use_multiple) 
{ 
  string head, header, hname, hcontents; 
  string|StringRange body; 
  int mesgsep; 
  if (has_prefix(message, "\r\n") || has_prefix(message, "\n")) { 
    // No headers. 
    return ({ ([]), message[1 + (message[0] == '\r')..] }); 
  } else { 
    int mesgsep1 = search(message, "\r\n\r\n"); 
    int mesgsep2 = search(message, "\n\n"); 
    mesgsep = (mesgsep1<0? mesgsep2 : 
               (mesgsep2<0? mesgsep1 : 
                (mesgsep1<mesgsep2? mesgsep1 : mesgsep2))); 
    if (mesgsep<0) { 
      // No body, or only body. 
      head = (string)message; 
      body = ""; 
    } else if (mesgsep) { 
      head = (string)(mesgsep>0? message[..mesgsep-1]:""); 
      body = message[mesgsep+(message[mesgsep]=='\r'? 4:2)..]; 
    } 
  } 
  mapping(string:string|array) headers = ([ ]); 
  foreach( replace(head, ({"\r", "\n ", "\n\t"}), 
                   ({"", " ", " "}))/"\n", header ) 
  { 
    if(4==sscanf(header, "%[!-9;-~]%*[ \t]:%*[ \t]%s", hname, hcontents)) 
    { 
      hname = lower_case(hname); 
      if (use_multiple) 
        headers[hname] += ({hcontents}); 
      else 
        if(headers[hname]) 
          headers[hname] += "\0"+hcontents; 
        else 
          headers[hname] = hcontents; 
    } 
  } 
 
  if( mesgsep<0 && !sizeof(headers) ) 
    return ({ ([]), (string)message }); 
  return ({ headers, body }); 
} 
 
 
//! This class is used to hold a decoded MIME message. 
class Message { 
 
  import Array; 
 
  protected string|StringRange encoded_data; 
  protected string|StringRange decoded_data; 
 
  //! This mapping contains all the headers of the message. 
  //! 
  //! The key is the header name (in lower case) and the value is 
  //! the header value. 
  //! 
  //! Although the mapping contains all headers, some particular headers get 
  //! special treatment by the module and should @b{not@} be accessed through 
  //! this mapping. These fields are currently: 
  //! @string 
  //!   @value "content-type" 
  //!   @value "content-disposition" 
  //!   @value "content-length" 
  //!   @value "content-transfer-encoding" 
  //! @endstring 
  //! The contents of these fields can be accessed and/or modified through 
  //! a set of variables and methods available for this purpose. 
  //! 
  //! @seealso 
  //! @[type], @[subtype], @[charset], @[boundary], @[transfer_encoding], 
  //! @[params], @[disposition], @[disp_params], @[setencoding()], 
  //! @[setparam()], @[setdisp_param()], @[setcharset()], @[setboundary()] 
  //! 
  mapping(string:string) headers; 
 
  //! If the message is of type @tt{multipart@}, this is an array 
  //! containing one Message object for each part of the message. 
  //! If the message is not a multipart, this field is @expr{0@} (zero). 
  //! 
  //! @seealso 
  //! @[type], @[boundary] 
  //! 
  array(object) body_parts; 
 
  //! For multipart messages, this @tt{Content-Type@} parameter gives a 
  //! delimiter string for separating the individual messages.  As multiparts 
  //! are handled internally by the module, you should not need to access this 
  //! field. 
  //! 
  //! @seealso 
  //! @[setboundary()] 
  //! 
  string boundary; 
 
  //! One of the possible parameters of the @tt{Content-Type@} header is the 
  //! charset attribute. It determines the character encoding used in bodies of 
  //! type @tt{text@}. 
  //! 
  //! If there is no @tt{Content-Type@} header, the value of this field 
  //! is @expr{"us-ascii"@}. 
  //! 
  //! @seealso 
  //! @[type] 
  //! 
  string charset; 
 
  //! The @tt{Content-Type@} header contains a type, a subtype, and optionally 
  //! some parameters. This field contains the type attribute extracted 
  //! from the header. 
  //! 
  //! If there is no @tt{Content-Type@} header, the value of this field 
  //! is @expr{"text"@}. 
  //! 
  //! @seealso 
  //! @[subtype], @[params] 
  //! 
  string type; 
 
  //! The @tt{Content-Type@} header contains a type, a subtype, and optionally 
  //! some parameters. This field contains the subtype attribute extracted 
  //! from the header. 
  //! 
  //! If there is no @tt{Content-Type@} header, the value of this field 
  //! is @expr{"plain"@}. 
  //! 
  //! @seealso 
  //! @[type], @[params] 
  //! 
  string subtype; 
 
  //! The contents of the @tt{Content-Transfer-Encoding@} header. 
  //! 
  //! If no @tt{Content-Transfer-Encoding@} header is given, this field 
  //! is @expr{0@} (zero). 
  //! 
  //! Transfer encoding and decoding is done transparently by the module, 
  //! so this field should be interesting only to applications wishing to 
  //! do auto conversion of certain transfer encodings. 
  //! 
  //! @seealso 
  //! @[setencoding()] 
  //! 
  string transfer_encoding; 
 
  //! A mapping containing all the additional parameters to the 
  //! @tt{Content-Type@} header. 
  //! 
  //! Some of these parameters have fields of their own, which should 
  //! be accessed instead of this mapping wherever applicable. 
  //! 
  //! @seealso 
  //! @[charset], @[boundary], @[setparam()] 
  //! 
  mapping (string:string) params; 
 
  //! The first part of the @tt{Content-Disposition@} header, hinting on how 
  //! this part of a multipart message should be presented in an interactive 
  //! application. 
  //! 
  //! If there is no @tt{Content-Disposition@} header, this field 
  //! is @expr{0@}. 
  //! 
  string disposition; 
 
  //! A mapping containing all the additional parameters to the 
  //! @tt{Content-Disposition@} header. 
  //! 
  //! @seealso 
  //! @[setdisp_param()], @[get_filename()] 
  //! 
  mapping (string:string) disp_params; 
 
 
  //! This method tries to find a suitable filename should you want to save the 
  //! body data to disk. 
  //! 
  //! It will examine the @tt{filename@} attribute of the 
  //! @tt{Content-Disposition@} header, and failing that the @tt{name@} 
  //! attribute of the @tt{Content-Type@} header. If neither attribute is set, 
  //! the method returns 0. 
  //! 
  //! @note 
  //! An interactive application should always query the user for the actual 
  //! filename to use.  This method may provide a reasonable default though. 
  //! 
  string get_filename( ) 
  { 
    string fn = disp_params["filename"] || params["name"]; 
    return fn && decode_words_text_remapped(fn); 
  } 
 
  //! If this message is a part of a fragmented message (i.e. has a 
  //! Content-Type of @tt{message/partial@}), an array with three elements 
  //! is returned. 
  //! 
  //! The first element is an identifier string. This string should be used to 
  //! group this message with the other fragments of the message (which will 
  //! have the same id string). 
  //! 
  //! The second element is the sequence number of this fragment. The first 
  //! part will have number 1, the next number 2 etc. 
  //! 
  //! The third element of the array is either the total number of fragments 
  //! that the original message has been split into, or 0 of this information 
  //! is not available. 
  //! 
  //! If this method is called in a message that is not a part of a fragmented 
  //! message, it will return 0. 
  //! 
  //! @seealso 
  //! @[MIME.reconstruct_partial()] 
  //! 
  array(string|int) is_partial( ) 
  { 
    return (type == "message" && subtype == "partial") && 
      ({ params["id"], (int)params["number"], (int)(params["total"]||"0") }); 
  } 
 
  //! Replaces the body entity of the data with a new piece of raw data. 
  //! 
  //! The new data should comply to the format indicated by the 
  //! @[type] and @[subtype] attributes. 
  //! 
  //! @note 
  //! Do not use this method unless you know what you are doing. 
  //! 
  //! @seealso 
  //! @[getdata()], @[setencoded], @[data] 
  //! 
  void setdata( string data ) 
  { 
    if (data != decoded_data) { 
      decoded_data = data; 
      encoded_data = 0; 
    } 
  } 
 
  //! @decl string data 
  //! 
  //! This variable contains the raw data of the message body entity. 
  //! 
  //! The @[type] and @[subtype] attributes indicate how this data should 
  //! be interpreted. 
  //! 
  //! @note 
  //!   In Pike 7.6 and earlier you had to use @[getdata()] and 
  //!   @[setdata()] to access this value. 
  //! 
  //! @seealso 
  //!   @[getdata()], @[setdata()] 
 
  void `->data=(string data) 
  { 
    setdata(data); 
  } 
 
  //! This method returns the raw data of the message body entity. 
  //! 
  //! The @[type] and @[subtype] attributes indicate how this data should 
  //! be interpreted. 
  //! 
  //! @seealso 
  //! @[setdata()], @[getencoded()], @[data] 
  //! 
  string getdata( ) 
  { 
    if (encoded_data && !decoded_data) 
      decoded_data = decode( encoded_data, transfer_encoding ); 
    return decoded_data = (string)decoded_data; 
  } 
 
  string `->data() 
  { 
    return getdata(); 
  } 
 
  //! This method returns the data of the message body entity, encoded 
  //! using the current transfer encoding. 
  //! 
  //! You should never have to call this function. 
  //! 
  //! @seealso 
  //! @[getdata()] 
  //! 
  string getencoded( ) 
  { 
    if (decoded_data && !encoded_data) 
      encoded_data = encode( (string)decoded_data, transfer_encoding, 
                             get_filename() ); 
    return (string)encoded_data; 
  } 
 
  //! Select a new transfer encoding for this message. 
  //! 
  //! The @tt{Content-Transfer-Encoding@} header will be modified accordingly, 
  //! and subsequent calls to @[getencoded] will produce data encoded using 
  //! the new encoding. 
  //! 
  //! See @[MIME.encode()] for a list of valid encodings. 
  //! 
  //! @seealso 
  //! @[getencoded()], @[MIME.encode()] 
  //! 
  void setencoding( string encoding ) 
  { 
    if(encoded_data && !decoded_data) 
      decoded_data = getdata( ); 
    headers["content-transfer-encoding"] = transfer_encoding = 
      lower_case( encoding ); 
    encoded_data = 0; 
  } 
 
  //! Set or modify the named parameter of the @tt{Content-Type@} header. 
  //! 
  //! Common parameters include @tt{charset@} for text messages, and 
  //! @tt{boundary@} for multipart messages. 
  //! 
  //! @note 
  //! It is not allowed to modify the @tt{Content-Type@} header directly, 
  //! please use this function instead. 
  //! 
  //! @seealso 
  //! @[setcharset()], @[setboundary()], @[setdisp_param()] 
  //! 
  void setparam( string param, string value ) 
  { 
    param = lower_case(param); 
    params[param] = value; 
    switch(param) { 
    case "charset": 
      charset = value; 
      break; 
    case "boundary": 
      boundary = value; 
      break; 
    case "name": 
      if(transfer_encoding != "x-uue" && transfer_encoding != "x-uuencode") 
        break; 
      if(encoded_data && !decoded_data) 
        decoded_data = getdata( ); 
      encoded_data = 0; 
      break; 
    } 
    headers["content-type"] = 
      quote(({ type, '/', subtype })+ 
            `+(@map(indices(params), lambda(string param) { 
              return ({ ';', param, '=', params[param] }); 
            }))); 
  } 
 
  //! Set or modify the named parameter of the @tt{Content-Disposition@} 
  //! header. 
  //! 
  //! A common parameters is e.g. @tt{filename@}. 
  //! 
  //! @note 
  //! It is not allowed to modify the @tt{Content-Disposition@} header 
  //! directly, please use this function instead. 
  //! 
  //! @seealso 
  //! @[setparam()], @[get_filename()] 
  //! 
  void setdisp_param( string param, string value ) 
  { 
    param = lower_case( param ); 
    disp_params[param] = value; 
    switch (param) { 
    case "filename": 
      if (transfer_encoding != "x-uue" && transfer_encoding != "x-uuencode") 
        break; 
      if (encoded_data && !decoded_data) 
        decoded_data = getdata( ); 
      encoded_data = 0; 
      break; 
    } 
    headers["content-disposition"] = 
      quote(({ disposition || "attachment" })+ 
            `+(@map(indices(disp_params), lambda(string param) { 
              return ({ ';', param, '=', disp_params[param] }); 
            }))); 
  } 
 
  //! Sets the @tt{charset@} parameter of the @tt{Content-Type@} header. 
  //! 
  //! This is equivalent of calling @expr{setparam("charset", @[charset])@}. 
  //! 
  //! @seealso 
  //! @[setparam()] 
  //! 
  void setcharset( string charset ) 
  { 
    setparam( "charset", charset ); 
  } 
 
  //! Sets the @tt{boundary@} parameter of the @tt{Content-Type@} header. 
  //! 
  //! This is equivalent of calling @expr{setparam("boundary", @[boundary])@}. 
  //! 
  //! @seealso 
  //! @[setparam()] 
  //! 
  void setboundary( string boundary ) 
  { 
    setparam( "boundary", boundary ); 
  } 
 
  //! Casting the message object to a string will yield a byte stream suitable 
  //! for transmitting the message over protocols such as ESMTP and NNTP. 
  //! 
  //! The body will be encoded using the current transfer encoding, and 
  //! subparts of a multipart will be collected recursively. If the message 
  //! is a multipart and no boundary string has been set, one will be 
  //! generated using @[generate_boundary()]. 
  //! 
  //! @seealso 
  //! @[create()] 
  protected string cast( string dest_type ) 
  { 
    string data; 
    object body_part; 
 
    if (dest_type != "string") 
      return UNDEFINED; 
 
    data = getencoded( ); 
 
    if (body_parts) { 
 
      if (!boundary) { 
        if (type != "multipart") { 
          type = "multipart"; 
          subtype = "mixed"; 
        } 
        setboundary( generate_boundary( ) ); 
      } 
 
      data += "\r\n"; 
      foreach( body_parts, body_part ) 
        data += "--"+boundary+"\r\n"+((string)body_part)+"\r\n"; 
      data += "--"+boundary+"--\r\n"; 
    } 
 
    headers["content-length"] = ""+sizeof(data); 
 
    return map( indices(headers), 
                lambda(string hname){ 
                  return map(arrayp(headers[hname]) ? headers[hname] : 
                             headers[hname]/"\0", 
                             lambda(string header,string hname) { 
                               return hname+": "+header; 
                             }, 
                             replace(map(hname/"-", 
                                         String.capitalize)*"-", 
                                     "Mime","MIME"))*"\r\n"; 
                } )*"\r\n" + "\r\n\r\n" + data; 
  } 
 
  protected string token_to_string(string|int token) 
  { 
    return intp(token) ? sprintf("%c", token) : token; 
  } 
 
  //! Parse a Content-Type or Content-Disposition parameter. 
  //! 
  //! @param params 
  //!   Mapping to add parameters to. 
  //! 
  //! @param entry 
  //!   Array of tokens containing a parameter declaration. 
  //! 
  //! @param header 
  //!   Name of the header from which @[entry] originated. 
  //!   This is only used to report errors. 
  //! 
  //! @param guess 
  //!   Make a best-effort attempt to parse broken entries. 
  //! 
  //! @param entry2 
  //!   Same as @[entry], but tokenized with @[MIME.TOKENIZE_KEEP_ESCAPES]. 
  //! 
  //! @seealso 
  //!   @[create()] 
  protected void parse_param(mapping(string:string) params, 
                             array(string|int) entry, 
                             string header, 
                             int|void guess, 
                             array(string|int)|void entry2) 
  { 
    if(sizeof(entry)) { 
      if(sizeof(entry)<3 || entry[1]!='=' || !stringp(entry[0])) 
        if(guess) { 
          if ((sizeof(entry) == 1) && stringp(entry[0])) { 
            if (sizeof(entry = (entry[0]/"-")) > 1) { 
              // Assume there's a typo where the '=' has been replaced 
              // with a '-'. 
              entry = ({ entry[0], '=', entry[1..]*"-" }); 
            } 
            // Just use the entry as a param with "" as the value. 
          } 
          else 
            return; // just ignore the broken data we failed to parse 
        } else 
          error("invalid parameter %O in %s %O (%O)\n", 
                entry[0], header, headers[lower_case(header)], guess); 
      string param = lower_case(entry[0]); 
      string val; 
      if (guess) { 
        val = map(entry[2..], token_to_string) * ""; 
      } else if (sizeof(filter(entry[2..], intp))) { 
        error("invalid quoting of parameter %O in %s %O (%O)\n", 
              entry[0], header, headers[lower_case(header)], guess); 
      } else { 
        val = entry[2..]*""; 
      } 
 
      params[param] = val; 
 
      // Check for MSIE: 
      // 
      // MSIE insists on sending the full local path to the file as 
      // the "filename" parameter, but forgets to quote the backslashes. 
      // 
      // Heuristic: 
      //   * If there are forward slashes, or properly quoted backslashes, 
      //     everything's alright. 
      //   * Note that UNC-paths (\\host\dir\file) look like they have 
      //     a properly quoted backslash as the first character, so 
      //     we disregard the first character. 
      if ((param == "filename") && guess && entry2 && 
          !has_value(val, "/") && !has_value(val[1..], "\\") && 
          (sizeof(entry2) >= 3) && (entry2[1] == '=') && 
          (lower_case(entry2[0]) == param)) { 
        val = map(entry2[2..], token_to_string) * ""; 
        if (has_value(val, "\\")) 
          params[param] = val; 
      } 
    } 
  } 
 
  //! @decl void create() 
  //! @decl void create(string message) 
  //! @decl void create(string message, @ 
  //!                   mapping(string:string|array(string)) headers, @ 
  //!                   array(object)|void parts) 
  //! @decl void create(string message, @ 
  //!                   mapping(string:string|array(string)) headers, @ 
  //!                   array(object)|void parts, int(0..1) guess) 
  //! 
  //! There are several ways to call the constructor of the Message class: 
  //! 
  //! @ul 
  //!   @item 
  //!     With zero arguments, you will get a dummy message with neither 
  //!     headers nor body. Not very useful. 
  //!   @item 
  //!     With one argument, the argument is taken to be a byte stream 
  //!     containing a message in encoded form. The constructor will analyze 
  //!     the string and extract headers and body. 
  //!   @item 
  //!     With two or three arguments, the first argument is taken to be the 
  //!     raw body data, and the second argument a desired set of headers. 
  //!     The keys of this mapping are not case-sensitive.  If the given 
  //!     headers indicate that the message should be of type multipart, 
  //!     an array of Message objects constituting the subparts should be 
  //!     given as a third argument. 
  //!   @item 
  //!     With the @[guess] argument set to 1 (@[headers] and @[parts] may be 0 
  //!     if you don't want to give any), you get a more forgiving MIME Message 
  //!     that will do its best to guess what broken input data really meant. It 
  //!     won't always guess right, but for applications like mail archives and 
  //!     similar where you can't get away with throwing an error at the user, 
  //!     this comes in handy. Only use the @[guess] mode only for situations 
  //!     where you @i{need@} to process broken MIME messages silently; the 
  //!     abuse of overly lax tools is what poisons standards. 
  //! @endul 
  //! 
  //! @seealso 
  //! @[cast()] 
  void create(void | string|StringRange message, 
              void | mapping(string:string|array(string)) hdrs, 
              void | array(object) parts, 
              void | int guess) 
  { 
    encoded_data = 0; 
    decoded_data = 0; 
    headers = ([ ]); 
    params = ([ ]); 
    disp_params = ([ ]); 
    body_parts = 0; 
    type = "text"; 
    subtype = "plain"; 
    charset = "us-ascii"; 
    boundary = 0; 
    disposition = 0; 
    if (message && stringp(message) && (sizeof(message) > 0x100000)) { 
      // Message is larger than 1 MB. 
      // Attempt to reduce memory use by using StringRange. 
      message = StringRange(message, 0, sizeof(message)); 
    } 
    if (hdrs || parts) { 
      string|array(string) hname; 
      decoded_data = message; 
      if (hdrs) 
        foreach( indices(hdrs), hname ) 
          headers[lower_case(hname)] = hdrs[hname]; 
      body_parts = parts; 
    } else if (message) 
      [ headers, encoded_data ] = parse_headers(message); 
 
    if (headers["content-type"]) { 
      array(array(string|int)) arr = 
        tokenize(headers["content-type"]) / ({';'}); 
      array(string|int) p; 
      if (guess && sizeof(arr[0]) > 3) { 
        // Workspace Webmail 5.6.17 is known to be able to 
        // send attachments with the content-type header 
        // "application/msword application; name=\"Foo.doc\";" 
        // Strip the extraneous tokens. 
        arr[0] = arr[0][..2]; 
      } 
      if(sizeof(arr[0])!=3 || arr[0][1]!='/' || 
         !stringp(arr[0][0]) || !stringp(arr[0][2])) 
        if(sizeof(arr[0])==1 && stringp(arr[0][0]) && 
           (subtype = guess_subtype(lower_case(type = arr[0][0])))) 
          arr = ({ ({ type, '/', subtype }) }) + arr[1..]; 
        else if(!guess) 
          error("invalid Content-Type %O\n", headers["content-type"]); 
        else 
          arr = ({ ({ "application", '/', "octet-stream" }) }) + arr[1..]; 
      type = lower_case(arr[0][0]); 
      subtype = lower_case(arr[0][2]); 
      foreach( arr[1..], p ) 
        parse_param(params, p, "Content-Type", guess); 
      charset = lower_case(params["charset"] || charset); 
      boundary = params["boundary"]; 
    } 
    if (headers["content-disposition"]) { 
      array(array(string|int)) arr; 
      array(array(string|int)) arr2; 
      mixed err = catch { 
          arr = tokenize(headers["content-disposition"]) / ({';'}); 
        }; 
      mixed err2 = catch { 
          arr2 = tokenize(headers["content-disposition"], 
                          MIME.TOKENIZE_KEEP_ESCAPES) / ({';'}); 
        }; 
      if (err) { 
        if (!guess || err2) throw(err); 
        // Known broken, probably MSIE. 
        arr = arr2; 
        arr2 = 0; 
      } 
 
      array(string|int) p; 
      if(sizeof(arr[0])!=1 || !stringp(arr[0][0])) 
      { 
        if(!guess) 
          error("invalid Content-Disposition in message\n"); 
      } else 
      { 
        disposition = lower_case(arr[0][0]); 
        foreach( arr[1..]; int i; p ) 
          parse_param(disp_params, p, "Content-Disposition", guess, 
                      arr2 && ((i+1) < sizeof(arr2)) && arr2[i+1]); 
      } 
    } 
    if (headers["content-transfer-encoding"]) { 
      array(string) arr=tokenize(headers["content-transfer-encoding"]); 
      if(sizeof(arr)!=1 || !stringp(arr[0])) 
      { 
        if(!guess) 
          error("invalid Content-Transfer-Encoding %O\n", 
                headers["content-transfer-encoding"]); 
      } else 
        transfer_encoding = lower_case(arr[0]); 
    } 
    if (boundary && type=="multipart" && !body_parts && 
       (encoded_data || decoded_data)) { 
 
      string|StringRange data = decoded_data || getdata(); 
      string separator = "--" + boundary; 
      array(string) parts = ({}); 
      int start = 0; 
      int found = 0; 
      encoded_data = 0; 
      decoded_data = 0; 
      while ((found = search(data, separator, found)) != -1) { 
        if (found) { 
          if (data[found-1] != '\n') { 
            found += sizeof(separator); 
            continue; 
          } 
          string part; 
 
          // Strip the terminating LF or CRLF. 
          if ((found > 1) && (data[found - 2] == '\r')) { 
            part = data[start..found-3]; 
          } else { 
            part = data[start..found-2]; 
          } 
          if (start) { 
            parts += ({ part }); 
          } else { 
            decoded_data = part; 
          } 
        } else { 
          decoded_data = ""; 
        } 
 
        // Skip past the separator and any white space after it. 
        found += sizeof(separator); 
        string terminator = data[found..found+1]; 
        if (terminator == "--") { 
          found += 2; 
        } else { 
          terminator = 0; 
        } 
        while ((found < sizeof(data)) && 
               ((data[found] == ' ') || (data[found] == '\t'))) { 
          found++; 
        } 
        if ((found < sizeof(data)) && (data[found] == '\n')) { 
          found++; 
        } else if ((found < sizeof(data)) && 
                   (data[found..found+1] == "\r\n")) { 
          found += 2; 
        } else if (!guess && !terminator) { 
          error("newline missing after multipart boundary\n"); 
        } 
 
        start = found; 
        if (terminator) break; 
      } 
      string epilogue = data[start..]; 
      if (!decoded_data) { 
        if (guess) { 
          decoded_data = epilogue; 
          epilogue = ""; 
        } else 
          error("boundary missing from multipart-body\n"); 
      } 
      if ((epilogue != "") && !guess) { 
        error("multipart message improperly terminated (%O%s)\n", 
              epilogue[..200], 
              sizeof(epilogue) > 201 ? "[...]" : ""); 
      } 
      body_parts = map(parts, this_program, 0, 0, guess); 
    } 
    if((hdrs || parts) && !decoded_data) { 
      decoded_data = (parts? 
                      "This is a multi-part message in MIME format.\r\n": 
                      ""); 
    } 
  } 
 
  protected string _sprintf(int c) 
  { 
    if (c == 'O') 
      return sprintf("Message(%O)", disp_params); 
    return (string)this; 
  } 
} 
 
//! This function will attempt to reassemble a fragmented message from its 
//! parts. 
//! 
//! The array @[collection] should contain @[MIME.Message] objects forming 
//! a complete set of parts for a single fragmented message. 
//! The order of the messages in the array is not important, but every part 
//! must exist at least once. 
//! 
//! Should the function succeed in reconstructing the original message, a 
//! new @[MIME.Message] object will be returned. 
//! 
//! If the function fails to reconstruct an original message, an integer 
//! indicating the reason for the failure will be returned: 
//! @int 
//!   @value 0 
//!     Returned if empty @[collection] is passed in, or one that contains 
//!     messages which are not of type @tt{message/partial@}, or parts of 
//!     different fragmented messages. 
//!   @value 1.. 
//!     If more fragments are needed to reconstruct the entire message, the 
//!     number of additional messages needed is returned. 
//!   @value -1 
//!     If more fragments are needed, but the function can't determine 
//!     exactly how many. 
//! @endint 
//! 
//! @note 
//! Note that the returned message may in turn be a part of another, 
//! larger, fragmented message. 
//! 
//! @seealso 
//! @[MIME.Message->is_partial()] 
//! 
int|object reconstruct_partial(array(object) collection) 
{ 
  int got = 0, maxgot = 0, top = sizeof(collection), total = 0; 
  mapping(int:object) parts = ([ ]); 
  string id; 
 
  if(!top) 
    return 0; 
 
  if(!(id = (collection[0]->is_partial()||({0}))[0])) 
    return 0; 
 
  foreach(collection, object m) { 
    array(int|string) p = m->is_partial(); 
    if(!(p && p[0] == id)) 
      return 0; 
    if((!total || p[1]==p[2]) && p[2]) 
      total = p[2]; 
    if(p[1]>maxgot) 
      maxgot = p[1]; 
    if(p[1]>0 && !parts[p[1]]) { 
      parts[p[1]] = m; 
      got++; 
    } 
  } 
 
  if(!total) 
    return -1; 
 
  if(got == total && maxgot == total) { 
    mapping(string:string) enclosing_headers = parts[1]->headers; 
 
    object reconstructed = 
      Message(`+(@Array.map(sort(indices(parts)), 
                            lambda(int i, mapping(int:object) parts){ 
        return parts[i]->getencoded(); 
      }, parts))); 
    foreach(indices(reconstructed->headers), string h) { 
      if(h != "message-id" && h != "encrypted" && h != "mime-version" && 
         h != "subject" && (sizeof(h)<8 || h[0..7] != "content-")) 
        m_delete(reconstructed->headers, h); 
    } 
    foreach(indices(enclosing_headers), string h) { 
      if(h != "message-id" && h != "encrypted" && h != "mime-version" && 
         h != "subject" && (sizeof(h)<8 || h[0..7] != "content-")) 
        reconstructed->headers[h] = enclosing_headers[h]; 
    } 
    return reconstructed; 
  } else return (maxgot>total? -1 : total-got); 
} 
 
//! Encode strings according to @rfc{4648@} base64url encoding. 
string encode_base64url(string x) 
{ 
  x = replace(encode_base64(x,1),({ "+", "/" }),({ "-", "_" })); 
  while( sizeof(x) && x[-1]=='=' ) x=x[..<1]; 
  return x; 
} 
 
//! Decode strings according to @rfc{4648@} base64url encoding. 
string decode_base64url(string x) 
{ 
  return decode_base64(replace(x,({ "-", "_" }),({ "+", "/" }))); 
}