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
  
1796
  
1797
  
1798
  
1799
  
1800
  
1801
  
1802
  
1803
  
1804
  
1805
  
1806
  
1807
  
1808
  
1809
  
1810
  
1811
  
1812
  
1813
  
1814
  
1815
  
1816
  
1817
  
1818
  
1819
  
1820
  
1821
  
1822
  
1823
  
1824
  
1825
  
1826
  
1827
  
1828
  
1829
  
1830
  
1831
  
1832
  
1833
  
1834
  
1835
  
1836
  
1837
  
1838
  
1839
  
1840
  
1841
  
1842
  
// This file is part of Roxen WebServer. 
// Copyright © 2001, Roxen IS. 
 
#include <stat.h> 
#include <config.h> 
#include <module.h> 
#include <variables.h> 
#include <module_constants.h> 
constant cvs_version="$Id: prototypes.pike,v 1.87 2004/04/28 16:23:46 grubba Exp $"; 
 
#ifdef DAV_DEBUG 
#define DAV_WERROR(X...)    werror(X) 
#else /* !DAV_DEBUG */ 
#define DAV_WERROR(X...) 
#endif /* DAV_DEBUG */ 
 
// To avoid reference cycles. Set to the Roxen module object by 
// roxenloader.pike. 
object Roxen; 
 
class Variable 
{ 
  constant is_variable = 1; 
  constant type = "Basic"; 
 
  string get_warnings(); 
  int get_flags(); 
  void set_flags( int flags ); 
  int check_visibility( RequestID id, 
                        int more_mode, 
                        int expert_mode, 
                        int devel_mode, 
                        int initial, 
                        int|void variable_in_cfif ); 
  void set_invisibility_check_callback( function(RequestID,Variable:int) cb ); 
  function(Variable:void) get_changed_callback( ); 
  void set_changed_callback( function(Variable:void) cb ); 
  void add_changed_callback( function(Variable:void) cb ); 
  function(RequestID,Variable:int) get_invisibility_check_callback() ; 
  string doc(  ); 
  string name(  ); 
  string type_hint(  ); 
  mixed default_value(); 
  void set_warning( string to ); 
  int set( mixed to ); 
  int low_set( mixed to );   
  mixed query(); 
  int is_defaulted(); 
  array(string|mixed) verify_set( mixed new_value ); 
  mapping(string:string) get_form_vars( RequestID id ); 
  mixed transform_from_form( string what ); 
  void set_from_form( RequestID id ); 
  string path(); 
  void set_path( string to ); 
  string render_form( RequestID id, void|mapping additional_args ); 
  string render_view( RequestID id ); 
} 
 
class BasicDefvar 
{ 
  mapping(string:Variable)  variables=([]); 
  Variable getvar( string name ); 
  int deflocaledoc( string locale, string variable, 
                    string name, string doc, mapping|void translate ); 
  void set(string var, mixed value); 
  int killvar(string var); 
  void setvars( mapping (string:mixed) vars ); 
  Variable defvar(string var, mixed value,  
                  mapping|string|void|object name, 
                  int|void type,  
                  mapping|string|void|object doc_str,  
                  mixed|void misc, 
                  int|function|void not_in_config, 
                  mapping|void option_translations); 
  mixed query(string|void var, int|void ok); 
  void definvisvar(string name, mixed value, int type, array|void misc); 
} 
 
 
 
class StringFile( string data, mixed|void _st ) 
{ 
  int offset; 
 
  string _sprintf() 
  { 
    return "StringFile("+strlen(data)+","+offset+")"; 
  } 
 
  string read(int nbytes) 
  { 
    if(!nbytes) 
    { 
      offset = strlen(data); 
      return data; 
    } 
    string d = data[offset..offset+nbytes-1]; 
    offset += strlen(d); 
    return d; 
  } 
 
  array stat() 
  { 
    if( _st ) return (array)_st; 
    return ({ 0, strlen(data), time(), time(), time(), 0, 0, 0 }); 
  } 
 
  void write(mixed ... args) 
  { 
    throw( ({ "File not open for write\n", backtrace() }) ); 
  } 
 
  void seek(int to) 
  { 
    offset = to; 
  } 
} 
 
class ModuleInfo 
{ 
  string sname; 
  string filename; 
 
  int last_checked; 
  int type, multiple_copies; 
  int|string locked; 
  mapping(Configuration:int) config_locked; 
   
  string get_name(); 
  string get_description(); 
  RoxenModule instance( object conf, void|int silent ); 
  void save(); 
  void update_with( RoxenModule mod, string what ); 
  int init_module( string what ); 
  int rec_find_module( string what, string dir ); 
  int find_module( string sn ); 
  int check (void|int force); 
} 
 
class ModuleCopies 
{ 
  mapping copies = ([]); 
  mixed `[](mixed q ) 
  { 
    return copies[q]; 
  } 
  mixed `[]=(mixed q,mixed w ) 
  { 
    return copies[q]=w; 
  } 
  array _indices() 
  { 
    return indices(copies); 
  } 
  array _values() 
  { 
    return values(copies); 
  } 
  string _sprintf( ) { return "ModuleCopies("+sizeof(copies)+")"; } 
} 
 
class DAVLock(string locktoken, 
              string path, 
              string lockscope, 
              int(0..1) recursive) 
{ 
} 
 
class Configuration 
{ 
  inherit BasicDefvar; 
  constant is_configuration = 1; 
  mapping enabled_modules = ([]); 
  mapping(string:array(int)) error_log=([]); 
 
#ifdef PROFILE 
  mapping profile_map = ([]); 
#endif 
 
  class Priority 
  { 
    string _sprintf() 
    { 
      return "Priority()"; 
    } 
 
    array (RoxenModule) url_modules = ({ }); 
    array (RoxenModule) logger_modules = ({ }); 
    array (RoxenModule) location_modules = ({ }); 
    array (RoxenModule) filter_modules = ({ }); 
    array (RoxenModule) last_modules = ({ }); 
    array (RoxenModule) first_modules = ({ }); 
    mapping (string:array(RoxenModule)) file_extension_modules = ([ ]); 
    mapping (RoxenModule:multiset(string)) provider_modules = ([ ]); 
  } 
 
  class DataCache 
  { 
    int current_size, max_size, max_file_size; 
    int hits, misses; 
    void flush(); 
    void expire_entry( string url ); 
    void set( string url, string data, mapping meta, int expire ); 
    array(string|mapping(string:mixed)) get( string url ); 
    void init_from_variables( ); 
  }; 
 
  array(Priority) allocate_pris(); 
 
  object      throttler; 
  RoxenModule types_module; 
  RoxenModule dir_module; 
  function    types_fun; 
 
  string name; 
  int inited; 
 
  // Protocol specific statistics. 
  int requests, sent, hsent, received; 
 
  function(string:int) log_function; 
  DataCache datacache; 
   
  int get_config_id(); 
  string get_doc_for( string region, string variable ); 
  string query_internal_location(RoxenModule|void mod); 
  string query_name(); 
  string comment(); 
  void unregister_urls(); 
  void stop(void|int asynch); 
  string|array(string) type_from_filename( string file, int|void to, 
                                           string|void myext ); 
 
  string get_url(); 
 
  array (RoxenModule) get_providers(string provides); 
  RoxenModule get_provider(string provides); 
  array(mixed) map_providers(string provides, string fun, mixed ... args); 
  mixed call_provider(string provides, string fun, mixed ... args); 
  array(function) file_extension_modules(string ext); 
  array(function) url_modules(); 
  mapping api_functions(void|RequestID id); 
  array(function) logger_modules(); 
  array(function) last_modules(); 
  array(function) first_modules(); 
  array location_modules(); 
  array(function) filter_modules(); 
  void init_log_file(); 
  int|mapping check_security(function|object a, RequestID id, void|int slevel); 
  void invalidate_cache(); 
  void clear_memory_caches(); 
  string examine_return_mapping(mapping m); 
  mapping|int(-1..0) low_get_file(RequestID id, int|void no_magic); 
  mapping get_file(RequestID id, int|void no_magic, int|void internal_get); 
  array(string) find_dir(string file, RequestID id, void|int(0..1) verbose); 
  array(int)|object(Stdio.Stat) stat_file(string file, RequestID id); 
  array open_file(string fname, string mode, RequestID id, void|int ig, void|int rc); 
  mapping(string:array(mixed)) find_dir_stat(string file, RequestID id); 
  array access(string file, RequestID id); 
  string real_file(string file, RequestID id); 
  int|string try_get_file(string s, RequestID id, 
                          int|void status, int|void nocache, 
                          int|void not_internal, mapping|void result_mapping); 
  int(0..1) is_file(string virt_path, RequestID id, int(0..1)|void internal); 
  void start(int num); 
  void save_me(); 
  int save_one( RoxenModule o ); 
  RoxenModule reload_module( string modname ); 
  RoxenModule enable_module( string modname, RoxenModule|void me,  
                             ModuleInfo|void moduleinfo,  
                             int|void nostart, 
                             int|void nosave ); 
  void call_start_callbacks( RoxenModule me,  
                             ModuleInfo moduleinfo,  
                             ModuleCopies module ); 
  void call_low_start_callbacks( RoxenModule me,  
                                 ModuleInfo moduleinfo,  
                                 ModuleCopies module ); 
  int disable_module( string modname, int|void nodest ); 
  int add_modules( array(string) mods, int|void now ); 
  RoxenModule find_module(string name); 
#if ROXEN_COMPAT < 2.2 
  Sql.Sql sql_cache_get(string what); 
  Sql.Sql sql_connect(string db); 
#endif 
  void enable_all_modules(); 
  void low_init(void|int modules_already_enabled); 
 
 
  string parse_rxml(string what, RequestID id, 
                    void|Stdio.File file, 
                    void|mapping defines ); 
  void add_parse_module (RoxenModule mod); 
  void remove_parse_module (RoxenModule mod); 
 
  string real_file(string a, RequestID b); 
 
 
  mapping authenticate_throw( RequestID id, string realm, 
                              UserDB|void database, 
                              AuthModule|void method); 
  User authenticate( RequestID id, 
                     UserDB|void database, 
                     AuthModule|void method ); 
 
  array(AuthModule) auth_modules(); 
  array(UserDB) user_databases(); 
 
  AuthModule find_auth_module( string name ); 
  UserDB find_user_database( string name ); 
   
  static string _sprintf( ) 
  { 
    return "Configuration("+name+")"; 
  } 
} 
 
//! @appears Protocol 
class Protocol  
{ 
  inherit BasicDefvar; 
 
  constant name = "unknown"; 
  //! Name used for internal identification. 
 
  constant prot_name = "unknown"; 
  //! Name of the protocol as seen in the protocol part of a url. 
 
  constant supports_ipless = 0; 
  constant requesthandlerfile = ""; 
  constant default_port = 4711; 
 
  int bound; 
  int refs; 
 
  program requesthandler; 
 
  string path; 
  int port; 
  string ip; 
  array(string) sorted_urls = ({}); 
  mapping(string:mapping) urls = ([]); 
  mapping(Configuration:mapping) conf_data = ([]); 
 
  void ref(string url, mapping data); 
  void unref(string url); 
  Configuration find_configuration_for_url( string url, RequestID id,  
                                            int|void no_default ); 
  string get_key(); 
  void save(); 
  void restore(); 
}; 
 
 
class FakedVariables( mapping real_variables ) 
{ 
  static array _indices() 
  { 
    return indices( real_variables ); 
  } 
 
  static array _values() 
  { 
    return map( _indices(), `[] ); 
  } 
 
  static mixed fix_value( mixed what ) 
  { 
    if( !what ) return what; 
    if( !arrayp(what) ) return what; // huh 
 
    if( sizeof( what ) == 1 ) 
      return what[0]; 
    return what*"\0"; 
  } 
 
  static mixed `[]( string ind ) { 
    return fix_value( real_variables[ ind ] ); 
  } 
 
  static mixed `->(string ind ) { 
    return `[]( ind ); 
  } 
 
  static mixed `[]=( string ind, mixed what ) { 
    real_variables[ ind ] = ({ what }); 
    return what; 
  } 
 
  static mixed `->=(string ind, mixed what ) { 
    return `[]=( ind,what ); 
  } 
 
  static mixed _m_delete( mixed what ) { 
//     report_debug(" _m_delete( %O )\n", what ); 
    return fix_value( m_delete( real_variables, what ) ); 
  } 
 
  static int _equal( mixed what ) { 
    return `==(what); 
  } 
 
  static int `==( mixed what ) { 
    if( mappingp( what ) && (real_variables == what) ) 
      return 1; 
  } 
 
  static string _sprintf( int f ) 
  { 
    switch( f ) 
    { 
      case 'O': 
        return sprintf( "FakedVariables(%O)", real_variables ); 
      default: 
        return sprintf( sprintf("%%%c", f ), real_variables ); 
    } 
  } 
 
  static this_program `|( mapping what ) 
  { 
    foreach( indices(what), string q )`[]=( q,what[q] ); 
    return this_object(); 
  } 
 
  static this_program `+=( mapping what ) 
  { 
    foreach( indices(what), string q )`[]=( q,what[q] ); 
    return this_object(); 
  } 
 
  static this_program `+( mapping what ) 
  { 
    foreach( indices(what), string q )`[]=( q,what[q] ); 
    return this_object(); 
  } 
 
  static mapping cast(string to) 
  { 
    if (to[..6]=="mapping") 
    { 
       array v=indices(real_variables); 
       return mkmapping(v,map(v,`[])); 
    }          
           
    error("can't cast to %O\n",to); 
  } 
} 
 
class PrefLanguages 
//! @appears PrefLanguages 
//! Support for language preferences. This object is typically 
//! accessed through @tt{id->misc->pref_languages@}. 
{ 
  int decoded=0; 
  int sorted=0; 
  array(string) subtags=({}); 
  array(string) languages=({}); 
  array(float) qualities=({}); 
 
  array(string) get_languages() { 
    sort_lang(); 
    return languages; 
  } 
 
  string get_language() { 
    if(!languages || !sizeof(languages)) return 0; 
    sort_lang(); 
    return languages[0]; 
  } 
 
  array(float) get_qualities() { 
    sort_lang(); 
    return qualities; 
  } 
 
  float get_quality() { 
    if(!qualities || !sizeof(qualities)) return 0.0; 
    sort_lang(); 
    return qualities[0]; 
  } 
 
  void set_sorted(array(string) lang, void|array(float) q) { 
    languages=lang; 
    if(q && sizeof(q)==sizeof(lang)) 
      qualities=q; 
    else 
      qualities=({1.0})*sizeof(lang); 
    sorted=1; 
    decoded=1; 
  } 
 
  void sort_lang() { 
    if(sorted && decoded) return; 
    array(float) q; 
    array(string) s=reverse(languages)-({""}), u=({}); 
 
    if(!decoded) { 
      q=({}); 
      s=Array.map(s, lambda(string x) { 
                       float n=1.0; 
                       string sub=""; 
                       sscanf(lower_case(x), "%s;q=%f", x, n); 
                       if(n==0.0) return ""; 
                       sscanf(x, "%s-%s", x, sub); 
                       q+=({n}); 
                       u+=({sub}); 
                       return x; 
                     }); 
      s-=({""}); 
      decoded=1; 
    } 
    else 
      q=reverse(qualities); 
 
    sort(q,s,u); 
    languages=reverse(s); 
    qualities=reverse(q); 
    subtags=reverse(u); 
    sorted=1; 
  } 
} 
 
 
//  Kludge for resolver problems 
static function _charset_decoder_func; 
 
class RequestID 
//! @appears RequestID 
//! The request information object contains all request-local information and 
//! server as the vessel for most forms of intercommunication between modules, 
//! scripts, RXML and so on. It gets passed round to almost all API callbacks 
//! worth mentioning. A RequestID object is born when an incoming request is 
//! encountered, and its life expectancy is short, as it dies again when the 
//! request has passed through all levels of the <ref>module type calling 
//! sequence</ref>. 
{ 
  Configuration conf; 
 
  Protocol port_obj; 
  //! The port object this request came from. 
 
  int time; 
  //! Time of the request, standard unix time (seconds since the epoch; 1970). 
 
  string raw_url; 
  //! The nonparsed, nontouched, non-* URL requested by the client. 
  //! Hence, this path is unlike <ref>not_query</ref> and 
  //! <ref>virtfile</ref> not relative to the server URL and must be 
  //! used in conjunction with the former to generate absolute paths 
  //! within the server. Be aware that this string will contain any 
  //! URL variables present in the request as well as the file path. 
 
  int do_not_disconnect; 
  //! Typically 0, meaning the channel to the client will be disconnected upon 
  //! finishing the request and the RequestID object destroyed with it. 
 
  mapping (string:array) real_variables; 
  //! Form variables submitted by the client browser, as found in the 
  //! <tt>form</tt> scope in RXML. Both query (as found in the query part of 
  //! the URL) and POST (submitted in the request body) variables share this 
  //! scope, with query variables having priority over POST ones. In other 
  //! words, the query part of the URL overrides whatever variables are sent 
  //! in the request body. 
  //! 
  //! The indices and values of this mapping map to the names and values of 
  //! the variable names. All data (names and values) are decoded from their 
  //! possible transport encoding. 
  //! 
  //! The value is always an array 
 
  mapping(string:mixed)|FakedVariables variables; 
  //! @decl mapping(string:mixed) variables; 
  //! 
  //! The variables mapping is more or less identical to the 
  //! real_variables maping, but each variable can only have one 
  //! value, if the form variable was sent multiple times from the 
  //! client (this happens, as an example, if you have checkbox 
  //! variables with the same name but different values), the values 
  //! will be separated with \0 (the null character) in this mapping. 
   
  mapping (string:mixed) misc; 
  //! This mapping contains miscellaneous non-standardized information, and 
  //! is the typical location to store away your own request-local data for 
  //! passing between modules et cetera. Be sure to use a key unique to your 
  //! own application. 
 
  mapping (string:mixed) connection_misc; 
  //! This mapping contains miscellaneous non-standardized information, and 
  //! is the typical location to store away your own connection-local data 
  //! for passing between requests on the same connection et cetera. Be sure 
  //! to use a key unique to your own application. 
 
  mapping (string:string) cookies; 
  //! The indices and values map to the names and values of the cookies sent 
  //! by the client for the requested page. All data (names and values) are 
  //! decoded from their possible transport encoding. 
 
  mapping (string:array(string)|string) request_headers; 
  //! Indices and values map to the names and values of all HTTP headers sent 
  //! with the request; all data has been transport decoded, and the header 
  //! names are canonized (lowercased) on top of that. Here is where you look 
  //! for the "user-agent" header, the "referer" [sic!] header and similar 
  //! interesting data provided by the client. 
 
  mapping (string:mixed) throttle; 
  // ? 
 
  mapping (string:mixed) client_var; 
  //! The client scope; a mapping of various client-related variables, indices 
  //! being the entity names and the values being their values respectively. 
 
  multiset(string) prestate; 
  //! A multiset of all prestates harvested from the URL. Prestates are boolean 
  //! flags, who are introduced in an extra leading path segment of the URL 
  //! path put within parentheses, as in <a 
  //! href="http://docs.roxen.com/(tables)/">docs://www.roxen.com/(tables)/</a>, 
  //! this rendering a prestate multiset <pi>(&lt; "tables" &gt;)</pi>. 
  //! 
  //! Prestates are mostly useful for debugging purposes, since prestates 
  //! generally lead to multiple URLs for identical documents resulting in 
  //! poor usage of browser/proxy caches and the like. See <ref>config</ref>. 
 
  multiset(string) config; 
  //! Much like prestates, the id->config multiset is typically used for 
  //! boolean information of state supplied by the client. The config state, 
  //! however, is hidden in a client-side cookie treated specially by roxen, 
  //! namely the <tt>RoxenConfig</tt> cookie. 
 
  multiset(string) supports; 
  //! All flags set by the supports system. 
 
  multiset(string) pragma; 
  //! All pragmas (lower-cased for canonization) sent with the request. For 
  //! real-world applications typically only <pi>pragma["no-cache"]</pi> is of 
  //! any particular interest, this being sent when the user does a forced 
  //! reload of the page. 
 
  array(string) client; 
  array(string) referer; 
 
  Stdio.File my_fd; 
  // Don't touch; use the returned file descriptor from connection() instead. 
 
  string prot; 
  //! The protocol used for the request, e g "FTP", "HTTP/1.0", "HTTP/1.1". 
  //! (Se also <ref>clientprot</ref>.) 
 
  string clientprot; 
  //! The protocol the client wanted to use in the request. This may 
  //! not be the same as <ref>prot</ref>, if the client wanted to talk 
  //! a higher protocol version than the server supports to date. 
 
  string method; 
  //! The method used by the client in this request, e g "GET", "POST". 
 
  string realfile; 
  //! When the the requested resource is an actual file in the real 
  //! filesystem, this is its path. 
 
  string virtfile; 
  //! The mountpoint of the location module that provided the requested file. 
  //! Note that this is not accessable from location modules; you need to keep 
  //! track of your mountpoint on your own using <ref>defvar()</ref> and 
  //! <ref>query()</ref>. This mountpoint is relative to the server URL. 
 
  string rest_query; 
  //! The scraps and leftovers of the requested URL's query part after 
  //! removing all variables (that is, all key=value pairs) from it. 
 
  string raw; 
  //! The raw, untouched request in its entirety. 
 
  string query; 
  //! The entire raw query part (all characters after the first question mark, 
  //! '?') of the requested URL. 
 
  string not_query; 
  //! The part of the path segment of the requested URL that is below 
  //! the virtual server's mountpoint. For a typical server 
  //! registering a URL with no ending path component, not_query will 
  //! contain all characters from the leading '/' to, but not 
  //! including, the first question mark ('?') of the URL. 
 
  string extra_extension; 
 
  string data; 
  //! The raw request body, containing non-decoded post variables et cetera. 
 
  string leftovers; 
  string rawauth, realauth; // Used by many modules, so let's keep this. 
  string since; 
 
  string remoteaddr; 
  //! The client's IP address. 
 
  string host; 
  //! The client's hostname, if resolved. 
 
  multiset(string) cache_status = (<>); 
  //! Contains the caches that was hit when the request was served. 
  //! See the docstring for @tt{$cache-status@} in the @tt{LogFormat@} 
  //! global variable for known values, but note that the multiset 
  //! actually never contains the value "nocache"; it's only written 
  //! when the multiset is empty. 
 
  object root_id; 
  //! @decl RequestID root_id; 
  //!   The root id object directly associated with the request - remains 
  //!   the same for all id objects generated by <insert href> tags and 
  //!   similar conditions that invoke @[clone_me()]. 
 
  static void create(Stdio.File fd, Protocol port, Configuration conf){} 
  void send(string|object what, int|void len){} 
 
#if constant(Parser.XML.Tree.XMLNSParser) 
  static Parser.XML.Tree.Node xml_data;       // XML data for the request. 
 
  Parser.XML.Tree.Node get_xml_data() 
  { 
    if (!sizeof(data)) return 0; 
    if (xml_data) return xml_data; 
    // FIXME: Probably ought to check that the content-type for 
    //        the request is text/xml. 
    DAV_WERROR("Parsing XML data: %O\n", data); 
    return xml_data = Parser.XML.Tree.parse_input(data, 0, 0, 0, 1); 
  } 
#endif /* Parser.XML.Tree.XMLNSParser */ 
 
  static string cached_url_base; 
 
  string url_base() 
  //! Returns the base part of the URL, i.e. what should be added in 
  //! front of a path in the virtual filesystem to get the absolute 
  //! URL to the page. The returned string ends with a "/", or is "" 
  //! if no server base could be found. 
  //! 
  //! This function gets the correct host for protocols that handles 
  //! IP-less hosts. 
  { 
    // Note: Code duplication in protocols/http.pike. 
 
    if (!cached_url_base) { 
      string tmp; 
 
      // First consult the port object. 
      if (port_obj) { 
        string host = port_obj->conf_data[conf]->hostname; 
        if (host == "*" && conf && sizeof (host = conf->get_url())) 
          if (sscanf (host, "%*s://%[^:/]", host) < 2) 
            host = port_obj->ip; 
        cached_url_base = port_obj->prot_name + "://" + host; 
        if (port_obj->port != port_obj->default_port) 
          cached_url_base += ":" + port_obj->port; 
      } 
 
      // Then try the configuration url. 
      else if (conf && sizeof (tmp = conf->get_url())) 
        cached_url_base = tmp[..sizeof (tmp) - 2]; // Remove trailing '/'. 
 
      // Lastly use a pathetic fallback. With this the produced urls 
      // will still be relative, which has some chance of working. 
      else return cached_url_base = ""; 
 
      if (string p = misc->site_prefix_path) cached_url_base += p; 
      cached_url_base += "/"; 
    } 
    return cached_url_base; 
  } 
 
  void add_response_header (string name, string value) 
  //! Adds a header @[name] with the value @[value] to be sent in the 
  //! http response. An existing header with the same name will not be 
  //! overridden, instead another (duplicate) header line will be sent 
  //! in the response. 
  //! 
  //! @note 
  //! If used from within an RXML parse session, this function will 
  //! ensure that the new header is registered properly in the RXML 
  //! p-code cache. That's the primary reason to used it instead of 
  //! adding the header directly to @tt{misc->moreheads@} or 
  //! @tt{misc->defines[" _extra_heads"]@}. 
  { 
    mapping hdrs = misc->defines && misc->defines[" _extra_heads"] || misc->moreheads; 
    if (!hdrs) hdrs = misc->moreheads = ([]); 
 
    // Essentially Roxen.add_http_header inlined. Can't refer to it 
    // from here due to the recursive resolver problems in Pike. 
    array|string cur_val = hdrs[name]; 
    if(cur_val) { 
      if(arrayp(cur_val)) { 
        if (!has_value(cur_val, value)) 
          cur_val += ({ value }); 
      } else { 
        if (cur_val != value) 
          cur_val = ({ cur_val, value }); 
      } 
    } 
    else 
      cur_val = value; 
 
    if (hdrs == misc->moreheads) 
      hdrs[name] = cur_val; 
    else if (object/*(RXML.Context)*/ ctx = RXML_CONTEXT) 
      ctx->set_var (name, cur_val, "header"); 
    else 
      hdrs[name] = cur_val; 
  } 
 
  void set_response_header (string name, string value) 
  //! Sets the header @[name] to the value @[value] to be sent in the 
  //! http response. If an existing header with the same name exists, 
  //! its value(s) will be overridden. This is useful for headers like 
  //! "Expire-Time", otherwise @[add_response_header] is typically a 
  //! better choice. 
  //! 
  //! @note 
  //! If used from within an RXML parse session, this function will 
  //! ensure that the new header is registered properly in the RXML 
  //! p-code cache. That's the primary reason to used it instead of 
  //! adding the header directly to @tt{misc->moreheads@} or 
  //! @tt{misc->defines[" _extra_heads"]@}. 
  { 
    if (misc->defines && misc->defines[" _extra_heads"]) { 
      misc->defines[" _extra_heads"][name] = value; 
      if (object/*(RXML.Context)*/ ctx = RXML_CONTEXT) 
        ctx->signal_var_change (name, "header"); 
    } 
    else { 
      if (!misc->moreheads) misc->moreheads = ([]); 
      misc->moreheads[name] = value; 
    } 
  } 
 
 
  //  Charset handling 
   
  array(string) output_charset = ({}); 
  string input_charset; 
 
  void set_output_charset( string|function to, int|void mode ) 
  { 
    if (object/*(RXML.Context)*/ ctx = RXML_CONTEXT) 
      ctx->add_p_code_callback ("set_output_charset", to, mode); 
 
    if( search( output_charset, to ) != -1 ) // Already done. 
      return; 
 
    switch( mode ) 
    { 
      case 0: // Really set. 
        output_charset = ({ to }); 
        break; 
 
      case 1: // Only set if not already set. 
        if( !sizeof( output_charset ) ) 
          output_charset = ({ to }); 
        break; 
 
      case 2: // Join. 
        output_charset |= ({ to }); 
        break; 
    } 
  } 
 
  static string charset_name(function|string what) 
  { 
    switch (what) { 
    case string_to_unicode: return "ISO10646-1"; 
    case string_to_utf8:    return "UTF-8"; 
    default:                return upper_case((string) what); 
    } 
  } 
 
  static function charset_function(function|string what, int allow_entities) 
  { 
    switch (what) { 
    case "ISO-10646-1": 
    case "ISO10646-1": 
    case string_to_unicode: 
      return string_to_unicode; 
       
    case "UTF-8": 
    case string_to_utf8: 
      return string_to_utf8; 
       
    default: 
      catch { 
        //  Use entity fallback if content type allows it 
        function fallback_func = 
          allow_entities && 
          lambda(string char) { 
            return sprintf("&#x%x;", char[0]); 
          }; 
         
        _charset_decoder_func = 
          _charset_decoder_func || Roxen->_charset_decoder; 
        return 
          _charset_decoder_func(Locale.Charset.encoder((string) what, "", 
                                                       fallback_func)) 
          ->decode; 
      }; 
    } 
    return lambda(string what) { return what; }; 
  } 
   
  static array(string) join_charset(string old, 
                                    function|string add, 
                                    function oldcodec, 
                                    int allow_entities) 
  { 
    switch (old && upper_case(old)) { 
    case 0: 
      return ({ charset_name(add), charset_function(add, allow_entities) }); 
    case "ISO10646-1": 
    case "UTF-8": 
      return ({ old, oldcodec }); // Everything goes here. :-) 
    case "ISO-2022": 
      return ({ old, oldcodec }); // Not really true, but how to know this? 
    default: 
      // Not true, but there is no easy way to add charsets yet... 
      return ({ charset_name(add), charset_function(add, allow_entities) }); 
    } 
  } 
   
  array(string) output_encode(string what, int|void allow_entities, 
                              string|void force_charset) 
  { 
    //  Performance optimization for unneeded ISO-8859-1 recoding of 
    //  strings which already are narrow. 
    if (String.width(what) == 8) { 
      if (force_charset) { 
        if (upper_case(force_charset) == "ISO-8859-1") 
          return ({ "ISO-8859-1", what }); 
      } else { 
        if (sizeof(output_charset) == 1 && 
            upper_case(output_charset[0]) == "ISO-8859-1") 
          return ({ "ISO-8859-1", what }); 
      } 
    } 
     
    if (!force_charset) { 
      string charset; 
      function encoder; 
       
      foreach( output_charset, string|function f ) 
        [charset,encoder] = join_charset(charset, f, encoder, allow_entities); 
      if (!encoder) 
        if (String.width(what) > 8) { 
          charset = "UTF-8"; 
          encoder = string_to_utf8; 
        } 
      if (encoder) 
        what = encoder(what); 
      return ({ charset, what }); 
    } else 
      return ({ 
        0, 
        Locale.Charset.encoder((force_charset / "=")[-1])->feed(what)->drain() 
      }); 
  } 
 
 
  string scan_for_query( string f ) 
  { 
    if(sscanf(f,"%s?%s", f, query) == 2) 
    { 
      string v, a, b; 
 
      foreach(query / "&", v) 
        if(sscanf(v, "%s=%s", a, b) == 2) 
        { 
          a = _Roxen.http_decode_string(replace(a, "+", " ")); 
          b = _Roxen.http_decode_string(replace(b, "+", " ")); 
          real_variables[ a ] += ({ b }); 
        } else 
          if(strlen( rest_query )) 
            rest_query += "&" + _Roxen.http_decode_string( v ); 
          else 
            rest_query = _Roxen.http_decode_string( v ); 
      rest_query=replace(rest_query, "+", "\000"); 
    } 
    return f; 
  } 
 
  mapping(string:string) make_response_headers (mapping(string:mixed) file) 
  //! Make the response headers from a response mapping for this 
  //! request. The headers associated with transfer modifications of 
  //! the response, e.g. 206 Partial Content and 304 Not Modified, are 
  //! not calculated here. 
  //! 
  //! @note 
  //! Is destructive on @[file] and on various data in the request; 
  //! should only be called once for a @[RequestID] instance. 
  { 
    if (!file->stat) file->stat = misc->stat; 
    if(objectp(file->file)) { 
      if(!file->stat) 
        file->stat = file->file->stat(); 
      if (zero_type(misc->cacheable) && file->file->is_file) { 
        // Assume a cacheablity on the order of the age of the file. 
        misc->cacheable = (predef::time(1) - file->stat[ST_MTIME])/4; 
      } 
    } 
 
    if( Stat fstat = file->stat ) 
    { 
      if( !file->len && fstat[1] >= 0 ) 
        file->len = fstat[1]; 
      if ( fstat[ST_MTIME] > misc->last_modified ) 
        misc->last_modified = fstat[ST_MTIME]; 
    }        
 
    if (!file->error) 
      file->error = Protocols.HTTP.HTTP_OK; 
 
    if(!file->type) file->type="text/plain"; 
 
    mapping(string:string) heads = ([]); 
 
    if( !zero_type(misc->cacheable) && 
        (misc->cacheable != INITIAL_CACHEABLE) ) { 
      if (!misc->cacheable) { 
        // It expired a year ago. 
        heads["Expires"] = Roxen->http_date( predef::time(1)-31557600 ); 
      } else 
        heads["Expires"] = Roxen->http_date( predef::time(1)+misc->cacheable ); 
      if (misc->cacheable < INITIAL_CACHEABLE) { 
        // Data with expiry is assumed to have been generated at the 
        // same instant. 
        misc->last_modified = predef::time(1); 
      } 
    } 
 
    if (misc->last_modified) 
      heads["Last-Modified"] = Roxen->http_date(misc->last_modified); 
 
    { 
      string charset=""; 
      if( stringp(file->data) ) 
      { 
        if (sizeof (output_charset) || 
            has_prefix (file->type, "text/") || 
            (String.width(file->data) > 8)) 
        { 
          int allow_entities = 
            has_prefix(file->type, "text/xml") || 
            has_prefix(file->type, "text/html"); 
          [charset,file->data] = output_encode( file->data, allow_entities ); 
          if( charset && (search(file["type"], "; charset=") == -1)) 
            charset = "; charset="+charset; 
          else 
            charset = ""; 
        } 
        file->len = strlen(file->data); 
      } 
      heads["Content-Type"] = file->type + charset; 
    } 
 
    heads["Accept-Ranges"] = "bytes"; 
    heads["Server"] = replace(roxenp()->version(), " ", "·"); 
    if( misc->connection ) 
      heads["Connection"] = misc->connection; 
 
    if(file->encoding) heads["Content-Encoding"] = file->encoding; 
 
    heads->Date = Roxen->http_date(predef::time(1)); 
    if(file->expires) 
      heads->Expires = Roxen->http_date(file->expires); 
 
    //if( file->len > 0 || (file->error != 200) ) 
    heads["Content-Length"] = (string)file->len; 
 
    if (misc->etag) 
      heads->ETag = misc->etag; 
 
#ifdef RAM_CACHE 
    if (!misc->etag && file->len && 
        (file->data || file->file) && 
        file->error == 200 && (<"HEAD", "GET">)[method] && 
        (file->len < conf->datacache->max_file_size)) { 
      string data = ""; 
      if (file->file) { 
        data = file->file->read(file->len); 
        if (file->data && (sizeof(data) < file->len)) { 
          data += file->data[..file->len - (sizeof(data)+1)]; 
        } 
        m_delete(file, "file"); 
      } else if (file->data) { 
        data = file->data[..file->len - 1]; 
      } 
      file->data = data; 
      heads->ETag = misc->etag = 
        Crypto.string_to_hex(Crypto.md5()->update(data)->digest()); 
      heads->Vary = "ETag"; 
    } 
#endif /* RAM_CACHE */ 
 
    if(mappingp(file->extra_heads)) 
      heads |= file->extra_heads; 
 
    if(mappingp(misc->moreheads)) 
      heads |= misc->moreheads; 
 
    return heads; 
  } 
 
  void adjust_for_config_path( string p ) 
  { 
    if( not_query )  not_query = not_query[ strlen(p).. ]; 
    raw_url = raw_url[ strlen(p).. ]; 
    misc->site_prefix_path = p; 
  } 
 
  void end(string|void s, int|void keepit){} 
  void ready_to_receive(){} 
  void send_result(mapping|void result){} 
  RequestID clone_me() 
  { 
    object c,t; 
    c=object_program(t=this_object())(0, port_obj, conf); 
 
    c->port_obj = port_obj; 
    c->conf = conf; 
    c->root_id = root_id; 
    c->time = time; 
    c->raw_url = raw_url; 
 
    c->real_variables = copy_value( real_variables ); 
    c->variables = FakedVariables( c->real_variables ); 
    c->misc = copy_value( misc ); 
    c->misc->orig = t; 
 
    c->connection_misc = connection_misc; 
 
    c->prestate = prestate; 
    c->supports = supports; 
    c->config = config; 
    c->client_var = client_var; 
 
    c->remoteaddr = remoteaddr; 
    c->host = host; 
 
    c->client = client; 
    c->referer = referer; 
    c->pragma = pragma; 
 
    c->cookies = cookies; 
    c->my_fd = 0; 
    c->prot = prot; 
    c->clientprot = clientprot; 
    c->method = method; 
 
    c->rest_query = rest_query; 
    c->raw = raw; 
    c->query = query; 
    c->not_query = not_query; 
    c->data = data; 
    c->extra_extension = extra_extension; 
 
    c->realauth = realauth; 
    c->rawauth = rawauth; 
    c->since = since; 
    return c; 
  } 
 
  Stdio.File connection( ) 
  //! Returns the file descriptor used for the connection to the client. 
  { 
    return my_fd; 
  } 
 
  Configuration configuration() 
  //! Returns the <ref>Configuration</ref> object of the virtual server that 
  //! is handling the request. 
  { 
    return conf; 
  } 
} 
 
#if constant(Parser.XML.Tree.XMLNSParser) 
 
static constant Node = Parser.XML.Tree.Node; 
static constant TextNode = Parser.XML.Tree.TextNode; 
static constant ElementNode = Parser.XML.Tree.ElementNode; 
 
class XMLStatusNode 
{ 
  inherit ElementNode; 
  static void create(int|string code, void|string message) 
  { 
    ::create("DAV:status", ([])); 
    if (intp(code)) { 
      code = sprintf("HTTP/1.1 %d %s", code, 
                     message||errors[code]||"Unknown status"); 
    } 
    add_child(TextNode(code)); 
  } 
} 
 
class XMLPropStatNode 
{ 
  inherit Parser.XML.Tree.ElementNode; 
 
  static mapping(string:Node) properties = ([]); 
  static multiset(string) descriptions = (<>); 
 
  static Node prop_node; 
  static Node description_node; 
 
  void add_property(string prop_name, void|string|array(Node)|Node value) 
  { 
    Node n; 
    if (!(n = properties[prop_name])) { 
      string type; 
      // The DAV client in Windows XP Pro (at least) requries types on 
      // the date fields to parse them correctly. The type system is 
      // of course some MS goo. 
      switch (prop_name) { 
        case "DAV:creationdate":     type = "dateTime.tz"; break; 
        case "DAV:getlastmodified":  type = "dateTime.rfc1123"; break; 
          // MS header - format unknown. 
          //case "DAV:lastaccessed": type = "dateTime.tz"; break; 
      } 
      n = ElementNode(prop_name, 
                      type ? 
                      (["urn:schemas-microsoft-com:datatypesdt": type]) : 
                      ([])); 
      properties[prop_name] = n; 
      prop_node->add_child(n); 
    } 
    if (value) { 
      if (stringp(value)) { 
        value = TextNode(value); 
      } 
      if (arrayp(value)) { 
        n->replace_children(value); 
      } else { 
        n->replace_children(({ value })); 
      }       
    } else { 
      n->replace_children(({})); 
    } 
  } 
 
  void add_description(string description) 
  { 
    if (descriptions[description]) return; 
    descriptions[description] = 1; 
    if (!description_node) { 
      description_node = ElementNode("DAV:responsedescription", ([])); 
      add_child(description_node); 
    } 
    description_node->add_child(TextNode(description + "\n")); 
  } 
 
  int http_code; 
 
  static void create(int|void code, string|void message) 
  { 
    http_code = code || 200; 
 
    ::create("DAV:propstat", ([])); 
    add_child(XMLStatusNode(code, message)); 
    add_child(prop_node = ElementNode("DAV:prop", ([]))); 
  } 
} 
 
class MultiStatus 
{ 
  static mapping(string:array(Node)) status_set = ([]); 
 
  int(0..1) is_empty() 
  { 
    return !sizeof(status_set); 
  } 
 
  void add_response(string href, Node response_node) 
  { 
    if (!status_set[href]) { 
      status_set[href] = ({ response_node }); 
    } else { 
      status_set[href] += ({ response_node }); 
    } 
  } 
 
  //! Add DAV:propstat information about the property @[prop_name] for 
  //! the resource @[href]. 
  //! 
  //! @param prop_value 
  //!   Optional property value. It can be one of the following: 
  //!   @mixed prop_value 
  //!     @type void|int(0..0) 
  //!       Operation performed ok, no value. 
  //!     @type string|array(Node)|Node 
  //!       Property has value @[prop_value]. 
  //!     @type mapping(string:mixed) 
  //!       Operation failed as described by the mapping. 
  //!   @endmixed 
  void add_property(string href, string prop_name, 
                    void|int(0..0)|string|array(Node)|Node| 
                    mapping(string:mixed) prop_value) 
  { 
    array(XMLStatusNode) stat_nodes; 
    XMLStatusNode stat_node; 
    int code = 200; 
    string message; 
 
    if (mappingp(prop_value)) { 
      code = prop_value->error; 
      message = prop_value->rettext; 
    } 
 
    DAV_WERROR("Adding property %O code:%O val:%O\n", 
               prop_name, code, prop_value); 
    if (!(stat_nodes = status_set[href])) { 
      status_set[href] = ({ stat_node = XMLPropStatNode(code, message) }); 
    } else { 
      int index; 
      for (; index < sizeof (stat_nodes); index++) { 
        XMLStatusNode n = stat_nodes[index]; 
        if (n->http_code == code) { 
          stat_node = n; 
          break; 
        } 
        if (n->http_code > code) break; 
      } 
      if (!stat_node) { 
        status_set[href] = 
          status_set[href][..index-1] + 
          ({ stat_node = XMLPropStatNode(code, message) }) + 
          status_set[href][index..]; 
      } 
    } 
    if (message) { 
      // FIXME: Add a global error description? 
      stat_node->add_description(message); 
      prop_value = 0; 
    } 
    stat_node->add_property(prop_name, prop_value); 
  } 
 
  Node get_xml_node() 
  { 
    Node root = 
      Parser.XML.Tree.parse_input( 
        "<?xml version='1.0' encoding='utf-8'?>" 
        "<DAV:multistatus xmlns:DAV='DAV:' " 
        // MS namespace for data types; see comment in 
        // XMLPropStatNode.add_property. Note: The XML parser in the 
        // MS DAV client is broken and requires the break of the last 
        // word "datatypesdt" to be exactly at this point. 
        "xmlns:MS='urn:schemas-microsoft-com:datatypes'>" 
        "</DAV:multistatus>"); 
    array(Node) response_xml = allocate(sizeof(status_set)); 
    int i; 
 
    DAV_WERROR("Generating XML Nodes for status_set:%O\n", 
               status_set); 
 
    foreach(sort(indices(status_set)), string href) { 
      array(Node) responses = status_set[href]; 
      Node href_node = ElementNode("DAV:href", ([])); 
      href_node->add_child(TextNode(href)); 
      (response_xml[i++] = 
        ElementNode("DAV:response", ([])))-> 
          replace_children(({href_node})+responses); 
    } 
    root->get_first_element("DAV:multistatus", 1)-> 
      replace_children(response_xml); 
    return root; 
  } 
 
  mapping(string:mixed) http_answer() 
  { 
    string xml = get_xml_node()->render_xml(); 
    return ([ 
      "error": 207, 
      "data": xml, 
      "len": sizeof(xml), 
      "type": "text/xml; charset=\"utf-8\"", 
    ]); 
  } 
 
  class prefix(static string href_prefix) { 
    int(0..1) is_empty() 
    { 
      return MultiStatus::is_empty(); 
    } 
    void add_response(string href, Node response_node) { 
      MultiStatus::add_response(href_prefix + href, response_node); 
    } 
    void add_property(string path, string prop_name, 
                      void|int(0..0)|string|Node|mapping(string:mixed) 
                      prop_value) 
    { 
      MultiStatus::add_property(href_prefix + path, prop_name, prop_value); 
    } 
    this_program prefix(string href_prefix) { 
      return MultiStatus::prefix(this_program::href_prefix + href_prefix); 
    } 
    Node get_xml_node() 
    { 
      return MultiStatus::get_xml_node(); 
    } 
    mapping(string:mixed) http_answer() 
    { 
      return MultiStatus::http_answer(); 
    } 
  } 
} 
 
#endif /* Parser.XML.Tree.XMLNSParser */ 
 
// Only for protyping and breaking of circularities. 
static class PropertySet 
{ 
  RequestID id; 
  string path; 
  Stat st; 
  multiset(string) query_all_properties(); 
  string|array(Parser.XML.Tree.Node)|mapping(string:mixed) 
    query_property(string prop_name); 
  mapping(string:mixed) start(); 
  void unroll(); 
  void commit(); 
  mapping(string:mixed) set_property(string prop_name, 
                                     string|array(Parser.XML.Tree.Node) value); 
  mapping(string:mixed) set_dead_property(string prop_name, 
                                          array(Parser.XML.Tree.Node) value); 
  mapping(string:mixed) remove_property(string prop_name); 
  mapping(string:mixed) find_properties(string mode, 
                                        MultiStatus result, 
                                        multiset(string)|void filt); 
} 
 
class RoxenModule 
{ 
  inherit BasicDefvar; 
  constant is_module = 1; 
  constant module_type = 0; 
  constant module_unique = 1; 
  LocaleString module_name; 
  LocaleString module_doc; 
 
  string module_identifier(); 
  string module_local_id(); 
 
  array(int|string|mapping) register_module(); 
  string file_name_and_stuff(); 
 
  void start(void|int num, void|object conf); 
 
  string query_internal_location(); 
  string query_location(); 
  string query_provides(); 
  function(RequestID:int|mapping) query_seclevels(); 
  array(int)|object(Stdio.Stat) stat_file(string f, RequestID id); 
  array(string) find_dir(string f, RequestID id); 
  mapping(string:array(mixed)) find_dir_stat(string f, RequestID id); 
  string real_file(string f, RequestID id); 
  void save(); 
  mapping api_functions(); 
  mapping query_tag_callers(); 
  mapping query_container_callers(); 
 
  string info(object conf); 
  string comment(); 
 
  PropertySet|mapping(string:mixed) query_properties(string path, RequestID id); 
  string|array(Parser.XML.Tree.Node)|mapping(string:mixed) 
    query_property(string path, string prop_name, RequestID id); 
  void recurse_find_properties(string path, string mode, int depth, 
                               MultiStatus result, RequestID id, 
                               multiset(string)|void filt); 
  mapping(string:mixed) patch_properties(string path, 
                                         array(PatchPropertyCommand) instructions, 
                                         MultiStatus result, RequestID id); 
  mapping(string:mixed) set_property (string path, string prop_name, 
                                      string|array(Parser.XML.Tree.Node) value, 
                                      RequestID id); 
  mapping(string:mixed) remove_property (string path, string prop_name, 
                                         RequestID id); 
  multiset(DAVLock) find_all_locks(string path, 
                                   int(0..1) recursive, 
                                   RequestID id); 
  DAVLock|int(-1..1) find_user_lock(string path, string user, RequestID id); 
  multiset(DAVLock)|int(-1..1) recur_find_user_locks(string path, 
                                                     string user, 
                                                     RequestID id); 
  void register_lock(string path, DAVLock lock, string user); 
  DAVLock|mapping(string:mixed) lock_file(string path, 
                                          string locktype, 
                                          string lockscope, 
                                          string locktoken, 
                                          int(0..1) recursive, 
                                          string user, 
                                          RequestID id); 
  mapping(string:mixed)|int(-1..0)|Stdio.File find_file(string path, 
                                                        RequestID id); 
  mapping(string:mixed) delete_file(string path, RequestID id); 
  int(0..1) recurse_delete_files(string path, MultiStatus result, RequestID id); 
} 
 
class PatchPropertyCommand 
{ 
  constant command = ""; 
  string property_name; 
  mapping(string:mixed) execute(PropertySet context); 
} 
 
class _roxen 
{ 
  mapping(string:object) variables; 
  constant real_version = ""; 
  object locale; 
  int start_time; 
  array(Configuration) configurations; 
 
  mixed  query(string a); 
  void   store(string a, mapping b, int c, object d); 
  mapping(string:mixed) retrieve(string a, object b); 
  void   remove(string a, object b); 
  string version(); 
  void   dump(string a); 
  void   nwrite(string a, int|void b, int|void c, void|mixed ... d); 
  int    main(int a, array(string) b); 
} 
 
class AuthModule 
//! @appears AuthModule 
//! The interface an authentication module must implement 
{ 
  inherit RoxenModule; 
  constant module_type = MODULE_AUTH; 
  constant thread_safe=1; 
 
  constant name = "method name"; 
   
  User authenticate( RequestID id, UserDB db ); 
  //! Try to authenticate the request with users from the specified user 
  //! database. If no @[db] is specified, all datbases in the current 
  //! configuration are searched in order, then the configuration user 
  //! database. 
 
  mapping authenticate_throw( RequestID id, string realm, UserDB db ); 
  //! Returns a reply mapping, similar to @[Roxen.http_rxml_reply] with 
  //! friends. If no @[db] is specified,  all datbases in the current 
  //! configuration are searched in order, then the configuration user 
  //! database. 
} 
 
static mapping(string:function(void:void)) user_sql_inited = ([]); 
static Sql.Sql user_mysql; 
static void init_user_sql(string table) 
{ 
  string db = all_constants()->REPLICATE?"replicate":"local"; 
  if( !user_mysql ) 
    user_mysql = master()->resolv("DBManager.get")( db ); 
  if(catch(user_mysql->query( "SELECT module FROM "+ 
                              table+" WHERE module=''"))) 
  { 
    user_mysql->query( "CREATE TABLE "+table+" " 
                       " (module varchar(30) NOT NULL,  " 
                       "  name   varchar(30) NOT NULL, " 
                       "  user   varchar(30) NOT NULL, " 
                       "  value  blob, " 
                       "  raw    int not null, " 
                       " INDEX foo (module,name,user))" ); 
    master()->resolv("DBManager.is_module_table")( 0, db, table, 
                               "Contains metadata about users. " 
                               "Userdatabases can store information here " 
                               "at the request of other modules, or they " 
                               "can keep their own state in this table" ); 
  } 
  user_sql_inited[ table ]= 
    lambda(){user_mysql = master()->resolv("DBManager.get")( db );}; 
} 
 
//! @appears Group 
class Group( UserDB database ) 
{ 
  string name(); 
  //! The group name 
 
  array(string) members() 
  //! All users that are members of this group. The default 
  //! implementation loops over all users handled by the user database 
  //! and looks for users with the same gid as this group, or who is a 
  //! member of it when the groups() method are called. 
  { 
    array res = ({}); 
    User uid; 
    int id = gid(); 
    foreach( database->list_users(), string u ) 
      if( (uid = database->find_user( u )) && 
          ((uid->gid() == id) || has_value(uid->groups(), name()))) 
        res += ({ u }); 
    return res; 
  } 
   
  int gid(); 
  //! A numerical GID, or -1 if not applicable 
 
 
  int set_name( string new_name )  {    return 0;  } 
  int set_gid( int new_gid )  {    return 0;  } 
  int set_members( array(string) members ) {   return 0;  } 
  //! Returns 1 if it was possible to set the variable. 
   
} 
 
#ifdef THREADS 
static Thread.Mutex mutex = Thread.Mutex(); 
#endif 
 
//! @appears User 
class User( UserDB database ) 
{ 
  static string table; 
 
  string name(); 
  //! The user (short) name 
 
  string real_name(); 
  //! The real name of the user 
 
  int password_authenticate(string password) 
  //! Return 1 if the password is correct, 0 otherwise. The default 
  //! implementation uses the crypted_password() method. 
  { 
    string c = crypted_password(); 
    return !sizeof(c) || crypt(password, c); 
  } 
 
  int uid(); 
  //! A numerical UID, or -1 if not applicable 
 
  int gid(); 
  //! A numerical GID, or -1 if not applicable 
 
  string shell(); 
  //! The shell, or 0 if not applicable 
   
  string gecos() 
  //! The gecos field, defaults to return the real name 
  { 
    return real_name(); 
  } 
 
  string homedir(); 
  string crypted_password() { return "x"; } 
  //! Used by compat_userinfo(). The default implementation returns "x" 
 
  array(string) groups() 
  //! Return all groups this user is a member in. The default 
  //! implementation returns ({}) 
  { 
    return ({}); 
  } 
 
  int set_name(string name)               {} 
  int set_real_name(string rname)         {} 
  int set_uid(int uid)                    {} 
  int set_gid(int gid)                    {} 
  int set_shell(string shell)             {} 
  int set_gecos(string gecos)             {} 
  int set_homedir(string hodir)           {} 
  int set_crypted_password(string passwd) {} 
  int set_password(string passwd)         {} 
  //! Returns 1 if it was possible to set the variable. 
   
  array compat_userinfo( ) 
  //! Return a unix passwd compatible array with user information. The 
  //! defualt implementation uses the other methods to assemble this 
  //! information. 
  //! 
  //! Basically: 
  //!  return ({ name(), crypted_password(), 
  //!            uid(), gid(), gecos(), homedir(), 
  //!          shell() }); 
  { 
    return ({name(),crypted_password(),uid(),gid(),gecos(),homedir(),shell()}); 
  } 
 
 
#define INIT_SQL() do{ \ 
    if(!table) table = replace(database->my_configuration()->name," ","_")+"_user_variables"; \ 
    if(!user_sql_inited[ table ] )init_user_sql( table );else user_sql_inited[ table ](); \ 
  } while( 0 ) 
 
 
#ifdef THREADS 
#define LOCK() mixed ___key = mutex->lock() 
#else 
#define LOCK() 
#endif 
 
  static string module_name( RoxenModule module ) 
  { 
    if( !module ) 
      // NULL does not work together with indexes, but this is 
      // not a valid modulename, so it's not a problem. 
      return "'0'"; 
    else 
      return replace("'"+user_mysql->quote(module->sname())+"'","%","%%"); 
  } 
   
  mixed set_var( RoxenModule module, string index, mixed value ) 
  //! Set a specified variable in the user. If @[value] is a string, 
  //! it's stored as is in the database, otherwise it's encoded using 
  //! encode_value before it's stored. Returns the value. 
  //! 
  //! You can use 0 for the @[module] argument. 
  //!  
  //! The default implementation stores the value in a mysql table 
  //! '*_user_data' in the 'shared' database. 
  //! 
  //! Use @[get_var] to retrieve the value, and @[delete_var] to 
  //! delete it. 
  { 
    delete_var( module, index ); 
    mixed oval = value; 
    LOCK(); 
    INIT_SQL(); 
    int encoded; 
 
    if( stringp( value ) ) 
      value = string_to_utf8( value ); 
    else 
    { 
      value = encode_value( value ); 
      encoded = 1; 
    } 
 
    user_mysql->query( 
      "INSERT INTO "+table+" (module,name,user,value,raw) " 
      "VALUES ("+module_name( module )+", %s, %s, %s, %d)", 
        index, name(), value, encoded 
    ); 
    return oval; 
  } 
 
  mixed get_var( RoxenModule module, string index ) 
  //! Return the value of a variable previously set with @[set_var] 
  { 
    array rows; 
    LOCK(); 
    INIT_SQL(); 
    rows = user_mysql->query( "SELECT * FROM "+table+ 
                              " WHERE module="+module_name( module ) 
                              +" AND name=%s AND user=%s", 
                              index, name() ); 
    if( !sizeof( rows ) ) 
      return 0; 
    mapping m = rows[0]; 
 
    if( (int)m->raw ) 
      return decode_value( m->value ); 
    return utf8_to_string( m->value ); 
  } 
 
  void delete_var( RoxenModule module, string index ) 
  //! Delete a variable previously created with @[set_var] 
  { 
    LOCK(); 
    INIT_SQL(); 
    user_mysql->query( "DELETE FROM "+table+" WHERE (module="+ 
                       module_name( module )+ 
                       " AND name=%s AND user=%s)", index, name() ); 
  } 
#undef INIT_SQL 
#undef LOCK 
} 
 
class UserDB 
//! @appears UserDB 
//! The interface a UserDB module must implement. 
{ 
  inherit RoxenModule; 
  constant module_type = MODULE_USERDB; 
  constant thread_safe=1; 
 
  constant name = "db name"; 
 
  User find_user( string s, RequestID|void id ); 
  //! Find a user 
 
  User find_user_from_uid( int uid, RequestID|void id ) 
  //! Find a user given a UID. The default implementation loops over 
  //! list_users() and checks the uid() of each one. 
  { 
    User user; 
    foreach( list_users(), string u ) 
      if( (user = find_user( u )) && (user->uid() == uid) ) 
        return user; 
  }     
 
  Group find_group( string group, RequestID|void id ) 
  //! Find a group object given a group name. 
  //! The default implementation returns 0. 
  { 
  } 
   
  Group find_group_from_gid( int gid, RequestID|void id ) 
  //! Find a group given a GID. The default implementation loops over 
  //! list_groups() and checks the gid() of each one. 
  { 
    Group group; 
    foreach( list_groups(), string u ) 
      if( (group = find_group( u )) && (group->gid() == gid) ) 
        return group; 
  } 
   
  array(string) list_groups( RequestID|void id ) 
  //! Return a list of all groups handled by this database module. 
  //! The default implementation returns the empty array. 
  { 
    return ({}); 
  } 
 
  array(string) list_users( RequestID|void id ); 
  //! Return a list of all users handled by this database module. 
 
  User create_user( string s ) 
  //! Not necessarily implemented, as an example, it's not possible to 
  //! create users in the system user database from Roxen WebServer. 
  //! The default implementation returns 0. 
  { 
    return 0; 
  } 
 
  Group create_group( string s ) 
  //! Not necessarily implemented, as an example, it's not possible to 
  //! create groups in the system user database from Roxen WebServer. 
  //! The default implementation returns 0. 
  { 
    return 0; 
  } 
}