~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/main/features_config.c -rw-r--r-- 68.8 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 9 months ago
                                                                                
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
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013, Digium, Inc.
*
* Mark Michelson <mmichelson@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

#include "asterisk.h"

#include "asterisk/features_config.h"
#include "asterisk/config_options.h"
#include "asterisk/datastore.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/cli.h"

#include "features_config.h"

/*** DOCUMENTATION
	<configInfo name="features" language="en_US">
		<synopsis>Features Configuration</synopsis>
		<configFile name="features.conf">
			<configObject name="globals">
				<synopsis>
				</synopsis>
				<configOption name="featuredigittimeout" default="1000">
					<synopsis>Milliseconds allowed between digit presses when entering a feature code.</synopsis>
				</configOption>
				<configOption name="courtesytone">
					<synopsis>Sound to play when automon or automixmon is activated</synopsis>
				</configOption>
				<configOption name="recordingfailsound">
					<synopsis>Sound to play when automon or automixmon is attempted but fails to start</synopsis>
				</configOption>
				<configOption name="transferdigittimeout" default="3">
					<synopsis>Seconds allowed between digit presses when dialing a transfer destination</synopsis>
				</configOption>
				<configOption name="atxfernoanswertimeout" default="15">
					<synopsis>Seconds to wait for attended transfer destination to answer</synopsis>
				</configOption>
				<configOption name="atxferdropcall" default="no">
					<synopsis>Hang up the call entirely if the attended transfer fails</synopsis>
					<description>
						<para>When this option is set to <literal>no</literal>, then Asterisk will attempt to
						re-call the transferrer if the call to the transfer target fails. If the call to the
						transferrer fails, then Asterisk will wait <replaceable>atxferloopdelay</replaceable>
						milliseconds and then attempt to dial the transfer target again. This process will
						repeat until <replaceable>atxfercallbackretries</replaceable> attempts to re-call
						the transferrer have occurred.</para>
						<para>When this option is set to <literal>yes</literal>, then Asterisk will not attempt
						to re-call the transferrer if the call to the transfer target fails. Asterisk will instead
						hang up all channels involved in the transfer.</para>
					</description>
				</configOption>
				<configOption name="atxferloopdelay" default="10">
					<synopsis>Seconds to wait between attempts to re-dial transfer destination</synopsis>
					<see-also><ref type="configOption">atxferdropcall</ref></see-also>
				</configOption>
				<configOption name="atxfercallbackretries" default="2">
					<synopsis>Number of times to re-attempt dialing a transfer destination</synopsis>
					<see-also><ref type="configOption">atxferdropcall</ref></see-also>
				</configOption>
				<configOption name="xfersound" default="beep">
					<synopsis>Sound to play to during transfer and transfer-like operations.</synopsis>
					<description>
						<para>This sound will play to the transferrer and transfer target channels when
						an attended transfer completes. This sound is also played to channels when performing
						an AMI <literal>Bridge</literal> action.</para>
					</description>
				</configOption>
				<configOption name="xferfailsound" default="beeperr">
					<synopsis>Sound to play to a transferee when a transfer fails</synopsis>
				</configOption>
				<configOption name="atxferabort" default="*1">
					<synopsis>Digits to dial to abort an attended transfer attempt</synopsis>
					<description>
						<para>This option is only available to the transferrer during an attended
						transfer operation. Aborting a transfer results in the transfer being cancelled and
						the original parties in the call being re-bridged.</para>
					</description>
				</configOption>
				<configOption name="atxfercomplete" default="*2">
					<synopsis>Digits to dial to complete an attended transfer</synopsis>
					<description>
						<para>This option is only available to the transferrer during an attended
						transfer operation. Completing the transfer with a DTMF sequence is functionally
						equivalent to hanging up the transferrer channel during an attended transfer. The
						result is that the transfer target and transferees are bridged.</para>
					</description>
				</configOption>
				<configOption name="atxferthreeway" default="*3">
					<synopsis>Digits to dial to change an attended transfer into a three-way call</synopsis>
					<description>
						<para>This option is only available to the transferrer during an attended
						transfer operation. Pressing this DTMF sequence will result in the transferrer,
						the transferees, and the transfer target all being in a single bridge together.</para>
					</description>
				</configOption>
				<configOption name="atxferswap" default="*4">
					<synopsis>Digits to dial to toggle who the transferrer is currently bridged to during an attended transfer</synopsis>
					<description>
						<para>This option is only available to the transferrer during an attended
						transfer operation. Pressing this DTMF sequence will result in the transferrer swapping
						which party he is bridged with. For instance, if the transferrer is currently bridged with
						the transfer target, then pressing this DTMF sequence will cause the transferrer to be
						bridged with the transferees.</para>
					</description>
				</configOption>
				<configOption name="pickupexten" default="*8">
					<synopsis>Digits used for picking up ringing calls</synopsis>
					<description>
						<para>In order for the pickup attempt to be successful, the party attempting to
						pick up the call must either have a <replaceable>namedpickupgroup</replaceable> in
						common with a ringing party's <replaceable>namedcallgroup</replaceable> or must
						have a <replaceable>pickupgroup</replaceable> in common with a ringing party's
						<replaceable>callgroup</replaceable>.</para>
					</description>
				</configOption>
				<configOption name="pickupsound">
					<synopsis>Sound to play to picker when a call is picked up</synopsis>
				</configOption>
				<configOption name="pickupfailsound">
					<synopsis>Sound to play to picker when a call cannot be picked up</synopsis>
				</configOption>
				<configOption name="transferdialattempts" default="3">
					<synopsis>Number of dial attempts allowed when attempting a transfer</synopsis>
				</configOption>
				<configOption name="transferretrysound" default="pbx-invalid">
					<synopsis>Sound that is played when an incorrect extension is dialed and the transferer should try again.</synopsis>
				</configOption>
				<configOption name="transferinvalidsound" default="privacy-incorrect">
					<synopsis>Sound that is played when an incorrect extension is dialed and the transferer has no attempts remaining.</synopsis>
				</configOption>
			</configObject>
			<configObject name="featuremap">
				<synopsis>DTMF options that can be triggered during bridged calls</synopsis>
				<configOption name="atxfer">
					<synopsis>DTMF sequence to initiate an attended transfer</synopsis>
					<description>
						<para>The transferee parties will be placed on hold and the
						transferrer may dial an extension to reach a transfer target. During an
						attended transfer, the transferrer may consult with the transfer target
						before completing the transfer. Once the transferrer has hung up or pressed
						the <replaceable>atxfercomplete</replaceable> DTMF sequence, then the transferees
						and transfer target will be bridged.</para>
					</description>
				</configOption>
				<configOption name="blindxfer" default="#">
					<synopsis>DTMF sequence to initiate a blind transfer</synopsis>
					<description>
						<para>The transferee parties will be placed on hold and the
						transferrer may dial an extension to reach a transfer target. During a
						blind transfer, as soon as the transfer target is dialed, the transferrer
						is hung up.</para>
					</description>
				</configOption>
				<configOption name="disconnect" default="*">
					<synopsis>DTMF sequence to disconnect the current call</synopsis>
					<description>
						<para>Entering this DTMF sequence will cause the bridge to end, no
						matter the number of parties present</para>
					</description>
				</configOption>
				<configOption name="parkcall">
					<synopsis>DTMF sequence to park a call</synopsis>
					<description>
						<para>The parking lot used to park the call is determined by using either the
						<replaceable>PARKINGLOT</replaceable> channel variable or a configured value on
						the channel (provided by the channel driver) if the variable is not present. If
						no configured value on the channel is present, then <literal>"default"</literal>
						is used. The call is parked in the next available space in the parking lot.</para>
					</description>
				</configOption>
				<configOption name="automon">
					<synopsis>DTMF sequence to start or stop monitoring a call</synopsis>
					<description>
						<para>This will cause the channel that pressed the DTMF sequence
						to be monitored by the <literal>Monitor</literal> application. The
						format for the recording is determined by the <replaceable>TOUCH_MONITOR_FORMAT</replaceable>
						channel variable. If this variable is not specified, then <literal>wav</literal> is the
						default. The filename is constructed in the following manner:</para>

						<para>    prefix-timestamp-filename</para>

						<para>where prefix is either the value of the <replaceable>TOUCH_MONITOR_PREFIX</replaceable>
						channel variable or <literal>auto</literal> if the variable is not set. The timestamp
						is a UNIX timestamp. The filename is either the value of the <replaceable>TOUCH_MONITOR</replaceable>
						channel variable or the callerID of the channels if the variable is not set.</para>
					</description>
				</configOption>
				<configOption name="automixmon">
					<synopsis>DTMF sequence to start or stop mixmonitoring a call </synopsis>
					<description>
						<para>Operation of the automixmon is similar to the <literal> automon </literal>
						feature, with the following exceptions:
							<replaceable>TOUCH_MIXMONITOR</replaceable> is used in place of <replaceable>TOUCH_MONITOR</replaceable>
							<replaceable>TOUCH_MIXMONITOR_FORMAT</replaceable> is used in place of <replaceable>TOUCH_MIXMONITOR</replaceable>
							There is no equivalent for <replaceable>TOUCH_MONITOR_PREFIX</replaceable>. <literal>"auto"</literal> is always how the filename begins.</para>
					</description>
					<see-also><ref type="configOption">automon</ref></see-also>
				</configOption>
			</configObject>
			<configObject name="applicationmap">
				<synopsis>Section for defining custom feature invocations during a call</synopsis>
				<description>
					<para>The applicationmap is an area where new custom features can be created. Items
					defined in the applicationmap are not automatically accessible to bridged parties. Access
					to the individual items is controled using the <replaceable>DYNAMIC_FEATURES</replaceable> channel variable.
					The <replaceable>DYNAMIC_FEATURES</replaceable> is a <literal>#</literal> separated list of
					either applicationmap item names or featuregroup names.</para>
				</description>
				<configOption name="">
					<synopsis>A custom feature to invoke during a bridged call</synopsis>
					<description>
						<para>Each item listed here is a comma-separated list of parameters that determine
						how a feature may be invoked during a call</para>
						<para>    Example:</para>
						<para>    eggs = *5,self,Playback(hello-world),default</para>
						<para>This would create a feature called <literal>eggs</literal> that could be invoked
						during a call by pressing the <literal>*5</literal>. The party that presses the DTMF
						sequence would then trigger the <literal>Playback</literal> application to play the
						<literal>hello-world</literal> file. The application invocation would happen on the
						party that pressed the DTMF sequence since <literal>self</literal> is specified. The
						other parties in the bridge would hear the <literal>default</literal> music on hold
						class during the playback.</para>
						<para>In addition to the syntax outlined in this documentation, a backwards-compatible alternative
						is also allowed. The following applicationmap lines are functionally identical:</para>
						<para>    eggs = *5,self,Playback(hello-world),default</para>
						<para>    eggs = *5,self,Playback,hello-world,default</para>
						<para>    eggs = *5,self,Playback,"hello-world",default</para>
					</description>
					<syntax argsep=",">
						<parameter name="dtmf" required="true">
							<para>The DTMF sequence used to trigger the option</para>
						</parameter>
						<parameter name="activate_on" required="true">
							<para>The party that the feature will be invoked on</para>
							<optionlist>
								<option name="self"><para>Feature is invoked on party that presses the DTMF sequence</para></option>
								<option name="peer"><para>Feature is invoked on other parties in the bridge</para></option>
							</optionlist>
						</parameter>
						<parameter name="app" required="true">
							<para>The dialplan application to run when the DTMF sequence is pressed</para>
							<argument name="app_args" required="false">
								<para>The arguments to the dialplan application to run</para>
							</argument>
						</parameter>
						<parameter name="moh_class" required="false">
							<para>Music on hold class to play to bridge participants that are not the target of the application invocation</para>
						</parameter>
					</syntax>
				</configOption>
			</configObject>
			<configObject name="featuregroup">
				<synopsis>Groupings of items from the applicationmap</synopsis>
				<description>
					<para>Feature groups allow for multiple applicationmap items to be
					grouped together. Like with individual applicationmap items, feature groups
					can be part of the <replaceable>DYNAMIC_FEATURES</replaceable> channel variable.
					In addition to creating groupings, the feature group section allows for the
					DTMF sequence used to invoke an applicationmap item to be overridden with
					a different sequence.</para>
				</description>
				<configOption name="">
					<synopsis>Applicationmap item to place in the feature group</synopsis>
					<description>
						<para>Each item here must be a name of an item in the applicationmap. The
						argument may either be a new DTMF sequence to use for the item or it
						may be left blank in order to use the DTMF sequence specified in the
						applicationmap. For example:</para>
						<para>	eggs => *1</para>
						<para>	bacon =></para>
						<para>would result in the applicationmap items <literal>eggs</literal> and
						<literal>bacon</literal> being in the featuregroup. The former would have its
						default DTMF trigger overridden with <literal>*1</literal> and the latter would
						have the DTMF value specified in the applicationmap.</para>
					</description>
				</configOption>
			</configObject>
		</configFile>
	</configInfo>
	<function name="FEATURE" language="en_US">
		<synopsis>
			Get or set a feature option on a channel.
		</synopsis>
		<syntax>
			<parameter name="option_name" required="true">
				<para>The allowed values are:</para>
				<enumlist>
					<enum name="inherit"><para>Inherit feature settings made in FEATURE or FEATUREMAP to child channels.</para></enum>
					<enum name="featuredigittimeout"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='featuredigittimeout']/synopsis/text())" /></para></enum>
					<enum name="transferdigittimeout"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='transferdigittimeout']/synopsis/text())" /></para></enum>
					<enum name="atxfernoanswertimeout"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='atxfernoanswertimeout']/synopsis/text())" /></para></enum>
					<enum name="atxferdropcall"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='atxferdropcall']/synopsis/text())" /></para></enum>
					<enum name="atxferloopdelay"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='atxferloopdelay']/synopsis/text())" /></para></enum>
					<enum name="atxfercallbackretries"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='atxfercallbackretries']/synopsis/text())" /></para></enum>
					<enum name="xfersound"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='xfersound']/synopsis/text())" /></para></enum>
					<enum name="xferfailsound"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='xferfailsound']/synopsis/text())" /></para></enum>
					<enum name="atxferabort"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='atxferabort']/synopsis/text())" /></para></enum>
					<enum name="atxfercomplete"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='atxfercomplete']/synopsis/text())" /></para></enum>
					<enum name="atxferthreeway"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='atxferthreeway']/synopsis/text())" /></para></enum>
					<enum name="pickupexten"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='pickupexten']/synopsis/text())" /></para></enum>
					<enum name="pickupsound"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='pickupsound']/synopsis/text())" /></para></enum>
					<enum name="pickupfailsound"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='pickupfailsound']/synopsis/text())" /></para></enum>
					<enum name="courtesytone"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='courtesytone']/synopsis/text())" /></para></enum>
					<enum name="recordingfailsound"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='recordingfailsound']/synopsis/text())" /></para></enum>
					<enum name="transferdialattempts"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='transferdialattempts']/synopsis/text())" /></para></enum>
					<enum name="transferretrysound"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='transferretrysound']/synopsis/text())" /></para></enum>
					<enum name="transferinvalidsound"><para><xi:include xpointer="xpointer(/docs/configInfo[@name='features']/configFile[@name='features.conf']/configObject[@name='globals']/configOption[@name='transferinvalidsound']/synopsis/text())" /></para></enum>
				</enumlist>
			</parameter>
		</syntax>
		<description>
			<para>When this function is used as a read, it will get the current
			value of the specified feature option for this channel.  It will be
			the value of this option configured in features.conf if a channel specific
			value has not been set.  This function can also be used to set a channel
			specific value for the supported feature options.</para>
		</description>
		<see-also>
			<ref type="function">FEATUREMAP</ref>
		</see-also>
	</function>
	<function name="FEATUREMAP" language="en_US">
		<synopsis>
			Get or set a feature map to a given value on a specific channel.
		</synopsis>
		<syntax>
			<parameter name="feature_name" required="true">
				<para>The allowed values are:</para>
				<enumlist>
					<enum name="atxfer"><para>Attended Transfer</para></enum>
					<enum name="blindxfer"><para>Blind Transfer</para></enum>
					<enum name="automon"><para>Auto Monitor</para></enum>
					<enum name="disconnect"><para>Call Disconnect</para></enum>
					<enum name="parkcall"><para>Park Call</para></enum>
					<enum name="automixmon"><para>Auto MixMonitor</para></enum>
				</enumlist>
			</parameter>
		</syntax>
		<description>
			<para>When this function is used as a read, it will get the current
			digit sequence mapped to the specified feature for this channel.  This
			value will be the one configured in features.conf if a channel specific
			value has not been set.  This function can also be used to set a channel
			specific value for a feature mapping.</para>
		</description>
		<see-also>
			<ref type="function">FEATURE</ref>
		</see-also>
	</function>
 ***/
/*! Default general options */
#define DEFAULT_FEATURE_DIGIT_TIMEOUT               1000
#define DEFAULT_COURTESY_TONE                       ""
#define DEFAULT_RECORDING_FAIL_SOUND                ""

/*! Default xfer options */
#define DEFAULT_TRANSFER_DIGIT_TIMEOUT              3
#define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER  15
#define DEFAULT_ATXFER_DROP_CALL                    0
#define DEFAULT_ATXFER_LOOP_DELAY                   10
#define DEFAULT_ATXFER_CALLBACK_RETRIES             2
#define DEFAULT_XFERSOUND                           "beep"
#define DEFAULT_XFERFAILSOUND                       "beeperr"
#define DEFAULT_ATXFER_ABORT                        "*1"
#define DEFAULT_ATXFER_COMPLETE                     "*2"
#define DEFAULT_ATXFER_THREEWAY                     "*3"
#define DEFAULT_ATXFER_SWAP                         "*4"
#define DEFAULT_TRANSFER_DIAL_ATTEMPTS              3
#define DEFAULT_TRANSFER_RETRY_SOUND                "pbx-invalid"
#define DEFAULT_TRANSFER_INVALID_SOUND              "privacy-incorrect"

/*! Default pickup options */
#define DEFAULT_PICKUPEXTEN                         "*8"
#define DEFAULT_PICKUPSOUND                         ""
#define DEFAULT_PICKUPFAILSOUND                     ""

/*! Default featuremap options */
#define DEFAULT_FEATUREMAP_BLINDXFER                "#"
#define DEFAULT_FEATUREMAP_DISCONNECT               "*"
#define DEFAULT_FEATUREMAP_AUTOMON                  ""
#define DEFAULT_FEATUREMAP_ATXFER                   ""
#define DEFAULT_FEATUREMAP_PARKCALL                 ""
#define DEFAULT_FEATUREMAP_AUTOMIXMON               ""

/*!
 * \brief Configuration from the "general" section of features.conf
 */
struct features_global_config {
	struct ast_features_general_config *general;
	struct ast_features_xfer_config *xfer;
	struct ast_features_pickup_config *pickup;
};

static void ast_applicationmap_item_destructor(void *obj)
{
	struct ast_applicationmap_item *item = obj;

	ast_string_field_free_memory(item);
}

static int applicationmap_sort(const void *obj, const void *arg, int flags)
{
	const struct ast_applicationmap_item *item1 = obj;
	const struct ast_applicationmap_item *item2;
	const char *key2;

	switch(flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
	case OBJ_KEY:
		key2 = arg;
		return strcasecmp(item1->name, key2);
	case OBJ_PARTIAL_KEY:
		key2 = arg;
		return strncasecmp(item1->name, key2, strlen(key2));
	default:
	case OBJ_POINTER:
		item2 = arg;
		return strcasecmp(item1->name, item2->name);
	}
}

/*!
 * \brief Entry in the container of featuregroups
 */
struct featuregroup_item {
	AST_DECLARE_STRING_FIELDS(
		/*! The name of the applicationmap item that we are referring to */
		AST_STRING_FIELD(appmap_item_name);
		/*! Custom DTMF override to use instead of the default for the applicationmap item */
		AST_STRING_FIELD(dtmf_override);
	);
	/*! The applicationmap item that is being referred to */
	struct ast_applicationmap_item *appmap_item;
};

static void featuregroup_item_destructor(void *obj)
{
	struct featuregroup_item *item = obj;

	ast_string_field_free_memory(item);
	ao2_cleanup(item->appmap_item);
}

static int group_item_sort(const void *obj, const void *arg, int flags)
{
	const struct featuregroup_item *item1 = obj;
	const struct featuregroup_item *item2;
	const char *key2;

	switch(flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
	case OBJ_KEY:
		key2 = arg;
		return strcasecmp(item1->appmap_item_name, key2);
	case OBJ_PARTIAL_KEY:
		key2 = arg;
		return strncasecmp(item1->appmap_item_name, key2, strlen(key2));
	case OBJ_POINTER:
		item2 = arg;
		return strcasecmp(item1->appmap_item_name, item2->appmap_item_name);
	default:
		return CMP_STOP;
	}
}

/*!
 * \brief Featuregroup representation
 */
struct featuregroup {
	/*! The name of the featuregroup */
	const char *name;
	/*! A container of featuregroup_items */
	struct ao2_container *items;
};

static int featuregroup_hash(const void *obj, int flags)
{
	const struct featuregroup *group;
	const char *key;

	switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
	case OBJ_KEY:
		key = obj;
		return ast_str_case_hash(key);
	case OBJ_PARTIAL_KEY:
		ast_assert(0);
		return 0;
	case OBJ_POINTER:
	default:
		group = obj;
		return ast_str_case_hash(group->name);
	}
}

static int featuregroup_cmp(void *obj, void *arg, int flags)
{
	struct featuregroup *group1 = obj;
	struct featuregroup *group2;
	const char *key2;

	switch(flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
	case OBJ_KEY:
		key2 = arg;
		return strcasecmp(group1->name, key2) ? 0 : CMP_MATCH;
	case OBJ_PARTIAL_KEY:
		key2 = arg;
		return strncasecmp(group1->name, key2, strlen(key2)) ? 0 : CMP_MATCH;
	case OBJ_POINTER:
		group2 = arg;
		return strcasecmp(group1->name, group2->name) ? 0 : CMP_MATCH;
	default:
		return CMP_STOP;
	}
}

static void *featuregroup_find(struct ao2_container *group_container, const char *category)
{
	return ao2_find(group_container, category, OBJ_KEY);
}

static void featuregroup_destructor(void *obj)
{
	struct featuregroup *group = obj;

	ast_free((char *) group->name);
	ao2_cleanup(group->items);
}

static void *featuregroup_alloc(const char *cat)
{
	struct featuregroup *group;

	group = ao2_alloc(sizeof(*group), featuregroup_destructor);
	if (!group) {
		return NULL;
	}

	group->name = ast_strdup(cat);
	if (!group->name) {
		ao2_cleanup(group);
		return NULL;
	}

	group->items = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK,
		AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, group_item_sort, NULL);
	if (!group->items) {
		ao2_cleanup(group);
		return NULL;
	}

	return group;
}

/* Used for deprecated parking configuration */
struct dummy_config {
	char dummy;
};

struct features_config {
	struct features_global_config *global;
	struct ast_featuremap_config *featuremap;
	struct dummy_config *parkinglots;
	struct ao2_container *applicationmap;
	struct ao2_container *featuregroups;
};

static struct aco_type global_option = {
	.type = ACO_GLOBAL,
	.name = "globals",
	.category_match = ACO_WHITELIST_EXACT,
	.category = "general",
	.item_offset = offsetof(struct features_config, global),
};

static struct aco_type featuremap_option = {
	.type = ACO_GLOBAL,
	.name = "featuremap",
	.category_match = ACO_WHITELIST_EXACT,
	.category = "featuremap",
	.item_offset = offsetof(struct features_config, featuremap),
};

static struct aco_type applicationmap_option = {
	.type = ACO_GLOBAL,
	.name = "applicationmap",
	.category_match = ACO_WHITELIST_EXACT,
	.category = "applicationmap",
	.item_offset = offsetof(struct features_config, applicationmap),
};

static struct aco_type featuregroup_option = {
	.type = ACO_ITEM,
	.name = "featuregroup",
	.category_match = ACO_BLACKLIST,
	.category = "^(general|featuremap|applicationmap|parkinglot_.*)$",
	.item_offset = offsetof(struct features_config, featuregroups),
	.item_alloc = featuregroup_alloc,
	.item_find = featuregroup_find,
};

static struct aco_type parkinglot_option = {
	.type = ACO_GLOBAL,
	.name = "parkinglot",
	.category_match = ACO_WHITELIST,
	.category = "^parkinglot_.*$",
	.item_offset = offsetof(struct features_config, parkinglots),
	.hidden = 1,
};

static struct aco_type *global_options[] = ACO_TYPES(&global_option);
static struct aco_type *featuremap_options[] = ACO_TYPES(&featuremap_option);
static struct aco_type *applicationmap_options[] = ACO_TYPES(&applicationmap_option);
static struct aco_type *featuregroup_options[] = ACO_TYPES(&featuregroup_option);
static struct aco_type *parkinglot_options[] = ACO_TYPES(&parkinglot_option);

static struct aco_file features_conf = {
	.filename = "features.conf",
	.types = ACO_TYPES(&global_option, &featuremap_option, &applicationmap_option, &featuregroup_option, &parkinglot_option),
};

AO2_GLOBAL_OBJ_STATIC(globals);

static void features_config_destructor(void *obj)
{
	struct features_config *cfg = obj;

	ao2_cleanup(cfg->global);
	ao2_cleanup(cfg->featuremap);
	ao2_cleanup(cfg->parkinglots);
	ao2_cleanup(cfg->applicationmap);
	ao2_cleanup(cfg->featuregroups);
}

static void featuremap_config_destructor(void *obj)
{
	struct ast_featuremap_config *cfg = obj;

	ast_string_field_free_memory(cfg);
}

static void global_config_destructor(void *obj)
{
	struct features_global_config *cfg = obj;

	ao2_cleanup(cfg->general);
	ao2_cleanup(cfg->xfer);
	ao2_cleanup(cfg->pickup);
}

static void general_destructor(void *obj)
{
	struct ast_features_general_config *cfg = obj;

	ast_string_field_free_memory(cfg);
}

static void xfer_destructor(void *obj)
{
	struct ast_features_xfer_config *cfg = obj;

	ast_string_field_free_memory(cfg);
}

static void pickup_destructor(void *obj)
{
	struct ast_features_pickup_config *cfg = obj;

	ast_string_field_free_memory(cfg);
}

static struct features_global_config *global_config_alloc(void)
{
	RAII_VAR(struct features_global_config *, cfg, NULL, ao2_cleanup);

	cfg = ao2_alloc(sizeof(*cfg), global_config_destructor);
	if (!cfg) {
		return NULL;
	}

	cfg->general = ao2_alloc(sizeof(*cfg->general), general_destructor);
	if (!cfg->general || ast_string_field_init(cfg->general, 32)) {
		return NULL;
	}

	cfg->xfer = ao2_alloc(sizeof(*cfg->xfer), xfer_destructor);
	if (!cfg->xfer || ast_string_field_init(cfg->xfer, 32)) {
		return NULL;
	}

	cfg->pickup = ao2_alloc(sizeof(*cfg->pickup), pickup_destructor);
	if (!cfg->pickup || ast_string_field_init(cfg->pickup, 32)) {
		return NULL;
	}

	ao2_ref(cfg, +1);
	return cfg;
}

static struct ao2_container *applicationmap_alloc(int replace_duplicates)
{
	return ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK,
		replace_duplicates ? AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE : AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW,
		applicationmap_sort, NULL);
}

/*!
 * \internal
 * \brief Allocate the major configuration structure
 *
 * The parameter is used to determine if the applicationmap and featuregroup
 * structures should be allocated. We only want to allocate these structures for
 * the global features_config structure. For the datastores on channels, we don't
 * need to allocate these structures because they are not used.
 *
 * \param allocate_applicationmap See previous explanation
 * \retval NULL Failed to alloate configuration
 * \retval non-NULL Allocated configuration
 */
static struct features_config *__features_config_alloc(int allocate_applicationmap)
{
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);

	cfg = ao2_alloc(sizeof(*cfg), features_config_destructor);
	if (!cfg) {
		return NULL;
	}

	cfg->global = global_config_alloc();
	if (!cfg->global) {
		return NULL;
	}

	cfg->featuremap = ao2_alloc(sizeof(*cfg->featuremap), featuremap_config_destructor);
	if (!cfg->featuremap || ast_string_field_init(cfg->featuremap, 32)) {
		return NULL;
	}

	cfg->parkinglots = ao2_alloc(sizeof(*cfg->parkinglots), NULL);
	if (!cfg->parkinglots) {
		return NULL;
	}

	if (allocate_applicationmap) {
		cfg->applicationmap = applicationmap_alloc(1);
		if (!cfg->applicationmap) {
			return NULL;
		}

		cfg->featuregroups = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, 11,
			featuregroup_hash, NULL, featuregroup_cmp);
		if (!cfg->featuregroups) {
			return NULL;
		}
	}

	ao2_ref(cfg, +1);
	return cfg;

}

static void *features_config_alloc(void)
{
	return __features_config_alloc(1);
}

static void general_copy(struct ast_features_general_config *dest, const struct ast_features_general_config *src)
{
	ast_string_fields_copy(dest, src);
	dest->featuredigittimeout = src->featuredigittimeout;
}

static void xfer_copy(struct ast_features_xfer_config *dest, const struct ast_features_xfer_config *src)
{
	ast_string_fields_copy(dest, src);
	dest->transferdigittimeout = src->transferdigittimeout;
	dest->atxfernoanswertimeout = src->atxfernoanswertimeout;
	dest->atxferloopdelay = src->atxferloopdelay;
	dest->atxfercallbackretries = src->atxfercallbackretries;
	dest->atxferdropcall = src->atxferdropcall;
	dest->transferdialattempts = src->transferdialattempts;
}

static void pickup_copy(struct ast_features_pickup_config *dest, const struct ast_features_pickup_config *src)
{
	ast_string_fields_copy(dest, src);
}

static void global_copy(struct features_global_config *dest, const struct features_global_config *src)
{
	general_copy(dest->general, src->general);
	xfer_copy(dest->xfer, src->xfer);
	pickup_copy(dest->pickup, src->pickup);
}

static void featuremap_copy(struct ast_featuremap_config *dest, const struct ast_featuremap_config *src)
{
	ast_string_fields_copy(dest, src);
}

static void features_copy(struct features_config *dest, const struct features_config *src)
{
	global_copy(dest->global, src->global);
	featuremap_copy(dest->featuremap, src->featuremap);

	/* applicationmap and featuregroups are purposely not copied. A channel's applicationmap
	 * is produced on the fly when ast_get_chan_applicationmap() is called
	 * NOTE: This does not apply to the global cfg->applicationmap and cfg->featuresgroups
	 */
}

static struct features_config *features_config_dup(const struct features_config *orig)
{
	struct features_config *dup;

	dup = __features_config_alloc(0);
	if (!dup) {
		return NULL;
	}

	features_copy(dup, orig);

	return dup;
}

static int general_set(struct ast_features_general_config *general, const char *name,
		const char *value)
{
	int res = 0;

	if (!strcasecmp(name, "featuredigittimeout")) {
		res = ast_parse_arg(value, PARSE_INT32, &general->featuredigittimeout);
	} else if (!strcasecmp(name, "courtesytone")) {
		ast_string_field_set(general, courtesytone, value);
	} else if (!strcasecmp(name, "recordingfailsound")) {
		ast_string_field_set(general, recordingfailsound, value);
	} else {
		/* Unrecognized option */
		res = -1;
	}

	return res;
}

static int general_get(struct ast_features_general_config *general, const char *field,
		char *buf, size_t len)
{
	int res = 0;

	if (!strcasecmp(field, "featuredigittimeout")) {
		snprintf(buf, len, "%u", general->featuredigittimeout);
	} else if (!strcasecmp(field, "courtesytone")) {
		ast_copy_string(buf, general->courtesytone, len);
	} else if (!strcasecmp(field, "recordingfailsound")) {
		ast_copy_string(buf, general->recordingfailsound, len);
	} else {
		/* Unrecognized option */
		res = -1;
	}

	return res;
}

static int xfer_set(struct ast_features_xfer_config *xfer, const char *name,
		const char *value)
{
	int res = 0;

	if (!strcasecmp(name, "transferdigittimeout")) {
		res = ast_parse_arg(value, PARSE_INT32, &xfer->transferdigittimeout);
	} else if (!strcasecmp(name, "atxfernoanswertimeout")) {
		res = ast_parse_arg(value, PARSE_INT32, &xfer->atxfernoanswertimeout);
	} else if (!strcasecmp(name, "atxferloopdelay")) {
		res = ast_parse_arg(value, PARSE_INT32, &xfer->atxferloopdelay);
	} else if (!strcasecmp(name, "atxfercallbackretries")) {
		res = ast_parse_arg(value, PARSE_INT32, &xfer->atxfercallbackretries);
	} else if (!strcasecmp(name, "atxferdropcall")) {
		xfer->atxferdropcall = ast_true(value);
	} else if (!strcasecmp(name, "xfersound")) {
		ast_string_field_set(xfer, xfersound, value);
	} else if (!strcasecmp(name, "xferfailsound")) {
		ast_string_field_set(xfer, xferfailsound, value);
	} else if (!strcasecmp(name, "atxferabort")) {
		ast_string_field_set(xfer, atxferabort, value);
	} else if (!strcasecmp(name, "atxfercomplete")) {
		ast_string_field_set(xfer, atxfercomplete, value);
	} else if (!strcasecmp(name, "atxferthreeway")) {
		ast_string_field_set(xfer, atxferthreeway, value);
	} else if (!strcasecmp(name, "atxferswap")) {
		ast_string_field_set(xfer, atxferswap, value);
	} else if (!strcasecmp(name, "transferdialattempts")) {
		res = ast_parse_arg(value, PARSE_INT32, &xfer->transferdialattempts);
	} else if (!strcasecmp(name, "transferretrysound")) {
		ast_string_field_set(xfer, transferretrysound, value);
	} else if (!strcasecmp(name, "transferinvalidsound")) {
		ast_string_field_set(xfer, transferinvalidsound, value);
	} else {
		/* Unrecognized option */
		res = -1;
	}

	return res;
}

static int xfer_get(struct ast_features_xfer_config *xfer, const char *field,
		char *buf, size_t len)
{
	int res = 0;

	if (!strcasecmp(field, "transferdigittimeout")) {
		snprintf(buf, len, "%u", xfer->transferdigittimeout);
	} else if (!strcasecmp(field, "atxfernoanswertimeout")) {
		snprintf(buf, len, "%u", xfer->atxfernoanswertimeout);
	} else if (!strcasecmp(field, "atxferloopdelay")) {
		snprintf(buf, len, "%u", xfer->atxferloopdelay);
	} else if (!strcasecmp(field, "atxfercallbackretries")) {
		snprintf(buf, len, "%u", xfer->atxfercallbackretries);
	} else if (!strcasecmp(field, "atxferdropcall")) {
		snprintf(buf, len, "%u", xfer->atxferdropcall);
	} else if (!strcasecmp(field, "xfersound")) {
		ast_copy_string(buf, xfer->xfersound, len);
	} else if (!strcasecmp(field, "xferfailsound")) {
		ast_copy_string(buf, xfer->xferfailsound, len);
	} else if (!strcasecmp(field, "atxferabort")) {
		ast_copy_string(buf, xfer->atxferabort, len);
	} else if (!strcasecmp(field, "atxfercomplete")) {
		ast_copy_string(buf, xfer->atxfercomplete, len);
	} else if (!strcasecmp(field, "atxferthreeway")) {
		ast_copy_string(buf, xfer->atxferthreeway, len);
	} else if (!strcasecmp(field, "atxferswap")) {
		ast_copy_string(buf, xfer->atxferswap, len);
	} else if (!strcasecmp(field, "transferdialattempts")) {
		snprintf(buf, len, "%u", xfer->transferdialattempts);
	} else if (!strcasecmp(field, "transferretrysound")) {
		ast_copy_string(buf, xfer->transferretrysound, len);
	} else if (!strcasecmp(field, "transferinvalidsound")) {
		ast_copy_string(buf, xfer->transferinvalidsound, len);
	} else {
		/* Unrecognized option */
		res = -1;
	}

	return res;
}

static int pickup_set(struct ast_features_pickup_config *pickup, const char *name,
		const char *value)
{
	int res = 0;

	if (!strcasecmp(name, "pickupsound")) {
		ast_string_field_set(pickup, pickupsound, value);
	} else if (!strcasecmp(name, "pickupfailsound")) {
		ast_string_field_set(pickup, pickupfailsound, value);
	} else if (!strcasecmp(name, "pickupexten")) {
		ast_string_field_set(pickup, pickupexten, value);
	} else {
		/* Unrecognized option */
		res = -1;
	}

	return res;
}

static int pickup_get(struct ast_features_pickup_config *pickup, const char *field,
		char *buf, size_t len)
{
	int res = 0;

	if (!strcasecmp(field, "pickupsound")) {
		ast_copy_string(buf, pickup->pickupsound, len);
	} else if (!strcasecmp(field, "pickupfailsound")) {
		ast_copy_string(buf, pickup->pickupfailsound, len);
	} else if (!strcasecmp(field, "pickupexten")) {
		ast_copy_string(buf, pickup->pickupexten, len);
	} else {
		/* Unrecognized option */
		res = -1;
	}

	return res;
}

static int featuremap_set(struct ast_featuremap_config *featuremap, const char *name,
		const char *value)
{
	int res = 0;

	if (!strcasecmp(name, "blindxfer")) {
		ast_string_field_set(featuremap, blindxfer, value);
	} else if (!strcasecmp(name, "disconnect")) {
		ast_string_field_set(featuremap, disconnect, value);
	} else if (!strcasecmp(name, "automon")) {
		ast_string_field_set(featuremap, automon, value);
	} else if (!strcasecmp(name, "atxfer")) {
		ast_string_field_set(featuremap, atxfer, value);
	} else if (!strcasecmp(name, "automixmon")) {
		ast_string_field_set(featuremap, automixmon, value);
	} else if (!strcasecmp(name, "parkcall")) {
		ast_string_field_set(featuremap, parkcall, value);
	} else {
		/* Unrecognized option */
		res = -1;
	}

	return res;
}

static int featuremap_get(struct ast_featuremap_config *featuremap, const char *field,
		char *buf, size_t len)
{
	int res = 0;

	if (!strcasecmp(field, "blindxfer")) {
		ast_copy_string(buf, featuremap->blindxfer, len);
	} else if (!strcasecmp(field, "disconnect")) {
		ast_copy_string(buf, featuremap->disconnect, len);
	} else if (!strcasecmp(field, "automon")) {
		ast_copy_string(buf, featuremap->automon, len);
	} else if (!strcasecmp(field, "atxfer")) {
		ast_copy_string(buf, featuremap->atxfer, len);
	} else if (!strcasecmp(field, "automixmon")) {
		ast_copy_string(buf, featuremap->automixmon, len);
	} else if (!strcasecmp(field, "parkcall")) {
		ast_copy_string(buf, featuremap->parkcall, len);
	} else {
		/* Unrecognized option */
		res = -1;
	}

	return res;
}

static void feature_ds_destroy(void *data)
{
	struct features_config *cfg = data;
	ao2_cleanup(cfg);
}

static void *feature_ds_duplicate(void *data)
{
	struct features_config *old_cfg = data;

	return features_config_dup(old_cfg);
}

static const struct ast_datastore_info feature_ds_info = {
	.type = "FEATURE",
	.destroy = feature_ds_destroy,
	.duplicate = feature_ds_duplicate,
};

/*!
 * \internal
 * \brief Find or create feature datastore on a channel
 *
 * \pre chan is locked
 *
 * \return the data on the FEATURE datastore, or NULL on error
 */
static struct features_config *get_feature_ds(struct ast_channel *chan)
{
	RAII_VAR(struct features_config *, orig, NULL, ao2_cleanup);
	struct features_config *cfg;
	struct ast_datastore *ds;

	if ((ds = ast_channel_datastore_find(chan, &feature_ds_info, NULL))) {
		cfg = ds->data;
		ao2_ref(cfg, +1);
		return cfg;
	}

	orig = ao2_global_obj_ref(globals);
	if (!orig) {
		return NULL;
	}

	cfg = features_config_dup(orig);
	if (!cfg) {
		return NULL;
	}

	if (!(ds = ast_datastore_alloc(&feature_ds_info, NULL))) {
		ao2_cleanup(cfg);
		return NULL;
	}

	/* Give the datastore a reference to the config */
	ao2_ref(cfg, +1);
	ds->data = cfg;

	ast_channel_datastore_add(chan, ds);

	return cfg;
}

static struct ast_datastore *get_feature_chan_ds(struct ast_channel *chan)
{
	struct ast_datastore *ds;

	if (!(ds = ast_channel_datastore_find(chan, &feature_ds_info, NULL))) {
		/* Hasn't been created yet.  Trigger creation. */
		ao2_cleanup(get_feature_ds(chan));

		ds = ast_channel_datastore_find(chan, &feature_ds_info, NULL);
	}

	return ds;
}

struct ast_features_general_config *ast_get_chan_features_general_config(struct ast_channel *chan)
{
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);

	if (chan) {
		cfg = get_feature_ds(chan);
	} else {
		cfg = ao2_global_obj_ref(globals);
	}

	if (!cfg) {
		return NULL;
	}

	ast_assert(cfg->global && cfg->global->general);

	ao2_ref(cfg->global->general, +1);
	return cfg->global->general;
}

struct ast_features_xfer_config *ast_get_chan_features_xfer_config(struct ast_channel *chan)
{
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);

	if (chan) {
		cfg = get_feature_ds(chan);
	} else {
		cfg = ao2_global_obj_ref(globals);
	}

	if (!cfg) {
		return NULL;
	}

	ast_assert(cfg->global && cfg->global->xfer);

	ao2_ref(cfg->global->xfer, +1);
	return cfg->global->xfer;
}

char *ast_get_chan_features_xferfailsound(struct ast_channel *chan)
{
	char *res;
	struct ast_features_xfer_config *cfg = ast_get_chan_features_xfer_config(chan);

	if (!cfg) {
		return NULL;
	}

	res = ast_strdup(cfg->xferfailsound);
	ao2_ref(cfg, -1);

	return res;
}

char *ast_get_chan_features_atxferabort(struct ast_channel *chan)
{
	char *res;
	struct ast_features_xfer_config *cfg = ast_get_chan_features_xfer_config(chan);

	if (!cfg) {
		return NULL;
	}

	res = ast_strdup(cfg->atxferabort);
	ao2_ref(cfg, -1);

	return res;
}

struct ast_features_pickup_config *ast_get_chan_features_pickup_config(struct ast_channel *chan)
{
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);

	if (chan) {
		cfg = get_feature_ds(chan);
	} else {
		cfg = ao2_global_obj_ref(globals);
	}

	if (!cfg) {
		return NULL;
	}

	ast_assert(cfg->global && cfg->global->pickup);

	ao2_ref(cfg->global->pickup, +1);
	return cfg->global->pickup;
}

struct ast_featuremap_config *ast_get_chan_featuremap_config(struct ast_channel *chan)
{
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);

	if (chan) {
		cfg = get_feature_ds(chan);
	} else {
		cfg = ao2_global_obj_ref(globals);
	}

	if (!cfg) {
		return NULL;
	}

	ast_assert(cfg->featuremap != NULL);

	ao2_ref(cfg->featuremap, +1);
	return cfg->featuremap;
}

int ast_get_builtin_feature(struct ast_channel *chan, const char *feature, char *buf, size_t len)
{
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);

	if (chan) {
		cfg = get_feature_ds(chan);
	} else {
		cfg = ao2_global_obj_ref(globals);
	}

	if (!cfg) {
		return -1;
	}

	return featuremap_get(cfg->featuremap, feature, buf, len);
}

int ast_get_feature(struct ast_channel *chan, const char *feature, char *buf, size_t len)
{
	RAII_VAR(struct ao2_container *, applicationmap, NULL, ao2_cleanup);
	RAII_VAR(struct ast_applicationmap_item *, item, NULL, ao2_cleanup);

	if (!ast_get_builtin_feature(chan, feature, buf, len)) {
		return 0;
	}

	/* Dang, must be in the application map */
	applicationmap = ast_get_chan_applicationmap(chan);
	if (!applicationmap) {
		return -1;
	}

	item = ao2_find(applicationmap, feature, OBJ_KEY);
	if (!item) {
		return -1;
	}

	ast_copy_string(buf, item->dtmf, len);
	return 0;
}

static struct ast_applicationmap_item *applicationmap_item_alloc(const char *name,
		const char *app, const char *app_data, const char *moh_class, const char *dtmf,
		unsigned int activate_on_self)
{
	struct ast_applicationmap_item *item;

	item = ao2_alloc(sizeof(*item), ast_applicationmap_item_destructor);

	if (!item || ast_string_field_init(item, 64)) {
		return NULL;
	}

	ast_string_field_set(item, name, name);
	ast_string_field_set(item, app, app);
	ast_string_field_set(item, app_data, app_data);
	ast_string_field_set(item, moh_class, moh_class);
	ast_copy_string(item->dtmf, dtmf, sizeof(item->dtmf));
	item->activate_on_self = activate_on_self;

	return item;
}

static int add_item(void *obj, void *arg, int flags)
{
	struct featuregroup_item *fg_item = obj;
	struct ao2_container *applicationmap = arg;
	RAII_VAR(struct ast_applicationmap_item *, appmap_item, NULL, ao2_cleanup);

	/* If there's no DTMF override, then we can just link
	 * the applicationmap item directly. Otherwise, we need
	 * to create a copy with the DTMF override in place and
	 * link that instead
	 */
	if (ast_strlen_zero(fg_item->dtmf_override)) {
		ao2_ref(fg_item->appmap_item, +1);
		appmap_item = fg_item->appmap_item;
	} else {
		appmap_item = applicationmap_item_alloc(fg_item->appmap_item_name,
				fg_item->appmap_item->app, fg_item->appmap_item->app_data,
				fg_item->appmap_item->moh_class, fg_item->dtmf_override,
				fg_item->appmap_item->activate_on_self);
	}

	if (!appmap_item) {
		return 0;
	}

	ao2_link(applicationmap, appmap_item);
	return 0;
}

struct ao2_container *ast_get_chan_applicationmap(struct ast_channel *chan)
{
	RAII_VAR(struct features_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
	struct ao2_container *applicationmap;
	char *group_names;
	char *name;

	if (!cfg) {
		return NULL;
	}

	if (!chan) {
		if (!cfg->applicationmap || ao2_container_count(cfg->applicationmap) == 0) {
			return NULL;
		}
		ao2_ref(cfg->applicationmap, +1);
		return cfg->applicationmap;
	}

	group_names = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"), ""));
	if (ast_strlen_zero(group_names)) {
		return NULL;
	}

	applicationmap = applicationmap_alloc(0);
	if (!applicationmap) {
		return NULL;
	}

	/* global config must be initialized */
	ast_assert(cfg->featuregroups != NULL);
	ast_assert(cfg->applicationmap != NULL);
	while ((name = strsep(&group_names, "#"))) {
		RAII_VAR(struct featuregroup *, group, ao2_find(cfg->featuregroups, name, OBJ_KEY), ao2_cleanup);

		if (!group) {
			RAII_VAR(struct ast_applicationmap_item *, item, ao2_find(cfg->applicationmap, name, OBJ_KEY), ao2_cleanup);

			if (item) {
				ao2_link(applicationmap, item);
			} else {
				ast_log(LOG_WARNING, "Unknown DYNAMIC_FEATURES item '%s' on channel %s.\n",
					name, ast_channel_name(chan));
			}
		} else {
			ao2_callback(group->items, 0, add_item, applicationmap);
		}
	}

	if (ao2_container_count(applicationmap) == 0) {
		ao2_cleanup(applicationmap);
		return NULL;
	}

	return applicationmap;
}

static int applicationmap_handler(const struct aco_option *opt,
		struct ast_variable *var, void *obj)
{
	RAII_VAR(struct ast_applicationmap_item *, item, NULL, ao2_cleanup);
	struct ao2_container *applicationmap = obj;
	AST_DECLARE_APP_ARGS(args,
		AST_APP_ARG(dtmf);
		AST_APP_ARG(activate_on);
		AST_APP_ARG(app);
		AST_APP_ARG(app_data);
		AST_APP_ARG(moh_class);
	);
	char *parse = ast_strdupa(var->value);
	char *slash;
	char *paren;
	unsigned int activate_on_self;

	AST_STANDARD_APP_ARGS(args, parse);

	if (ast_strlen_zero(args.dtmf) ||
			ast_strlen_zero(args.activate_on) ||
			ast_strlen_zero(args.app)) {
		ast_log(LOG_WARNING, "Invalid applicationmap syntax for '%s'. Missing required argument\n", var->name);
		return -1;
	}

	/* features.conf used to have an "activated_by" portion
	 * in addition to activate_on. Get rid of whatever may be
	 * there
	 */
	slash = strchr(args.activate_on, '/');
	if (slash) {
		*slash = '\0';
	}

	/* Some applications do not require arguments. */
	if (!args.app_data) {
		args.app_data = "";
	}

	/* Two syntaxes allowed for applicationmap:
	 * Old: foo = *1,self,NoOp,Boo!,default
	 * New: foo = *1,self,NoOp(Boo!),default
	 *
	 * We need to handle both
	 */
	paren = strchr(args.app, '(');
	if (paren) {
		/* New syntax */
		char *close_paren;

		args.moh_class = args.app_data;
		*paren++ = '\0';
		close_paren = strrchr(paren, ')');
		if (close_paren) {
			*close_paren = '\0';
		}
		args.app_data = paren;

		/* Re-check that the application is not empty */
		if (ast_strlen_zero(args.app)) {
			ast_log(LOG_WARNING, "Applicationmap item '%s' does not contain an application name.\n", var->name);
			return -1;
		}
	} else if (strchr(args.app_data, '"')) {
		args.app_data = ast_strip_quoted(args.app_data, "\"", "\"");
	}

	/* Allow caller and callee to be specified for backwards compatibility */
	if (!strcasecmp(args.activate_on, "self") || !strcasecmp(args.activate_on, "caller")) {
		activate_on_self = 1;
	} else if (!strcasecmp(args.activate_on, "peer") || !strcasecmp(args.activate_on, "callee")) {
		activate_on_self = 0;
	} else {
		ast_log(LOG_WARNING, "Invalid 'activate_on' value %s for applicationmap item %s\n",
			args.activate_on, var->name);
		return -1;
	}

	ast_debug(1, "Allocating applicationmap item: dtmf = %s, app = %s, app_data = %s, moh_class = %s\n",
			args.dtmf, args.app, args.app_data, args.moh_class);

	item = applicationmap_item_alloc(var->name, args.app, args.app_data,
			args.moh_class, args.dtmf, activate_on_self);

	if (!item) {
		return -1;
	}

	if (!ao2_link(applicationmap, item)) {
		return -1;
	}

	return 0;
}

static int featuregroup_handler(const struct aco_option *opt,
		struct ast_variable *var, void *obj)
{
	RAII_VAR(struct featuregroup_item *, item, NULL, ao2_cleanup);
	struct featuregroup *group = obj;

	item = ao2_alloc(sizeof(*item), featuregroup_item_destructor);
	if (!item || ast_string_field_init(item, 32)) {
		return -1;
	}

	ast_string_field_set(item, appmap_item_name, var->name);
	ast_string_field_set(item, dtmf_override, var->value);

	if (!ao2_link(group->items, item)) {
		return -1;
	}

	/* We wait to look up the application map item in the preapply callback */

	return 0;
}

static int general_handler(const struct aco_option *opt,
		struct ast_variable *var, void *obj)
{
	struct features_global_config *global = obj;
	struct ast_features_general_config *general = global->general;

	return general_set(general, var->name, var->value);
}

static int xfer_handler(const struct aco_option *opt,
		struct ast_variable *var, void *obj)
{
	struct features_global_config *global = obj;
	struct ast_features_xfer_config *xfer = global->xfer;

	return xfer_set(xfer, var->name, var->value);
}

static int pickup_handler(const struct aco_option *opt,
		struct ast_variable *var, void *obj)
{
	struct features_global_config *global = obj;
	struct ast_features_pickup_config *pickup = global->pickup;

	return pickup_set(pickup, var->name, var->value);
}

static int parking_warning = 0;
static int unsupported_handler(const struct aco_option *opt,
		struct ast_variable *var, void *obj)
{
	if (!parking_warning) {
		ast_log(LOG_WARNING, "Parkinglots are no longer configurable in features.conf; "
			"parking is now handled by res_parking.conf\n");
		parking_warning = 1;
	}
	ast_log(LOG_WARNING, "The option '%s' is no longer configurable in features.conf.\n", var->name);
	return 0;
}

static int featuremap_handler(const struct aco_option *opt,
		struct ast_variable *var, void *obj)
{
	struct ast_featuremap_config *featuremap = obj;

	return featuremap_set(featuremap, var->name, var->value);
}

static int check_featuregroup_item(void *obj, void *arg, void *data, int flags)
{
	struct ast_applicationmap_item *appmap_item;
	struct featuregroup_item *fg_item = obj;
	int *err = arg;
	struct ao2_container *applicationmap = data;

	appmap_item = ao2_find(applicationmap, fg_item->appmap_item_name, OBJ_KEY);
	if (!appmap_item) {
		*err = 1;
		return CMP_STOP;
	}

	fg_item->appmap_item = appmap_item;

	return 0;
}

static int check_featuregroup(void *obj, void *arg, void *data, int flags)
{
	struct featuregroup *group = obj;
	int *err = arg;

	ao2_callback_data(group->items, 0, check_featuregroup_item, arg, data);

	if (*err) {
		ast_log(LOG_WARNING, "Featuregroup %s refers to non-existent applicationmap item\n",
				group->name);
	}

	return *err ? CMP_STOP : 0;
}

static int features_pre_apply_config(void);

CONFIG_INFO_CORE("features", cfg_info, globals, features_config_alloc,
	.files = ACO_FILES(&features_conf),
	.pre_apply_config = features_pre_apply_config,
);

static int features_pre_apply_config(void)
{
	struct features_config *cfg = aco_pending_config(&cfg_info);
	int err = 0;

	/* Now that the entire config has been processed, we can check that the featuregroup
	 * items refer to actual applicationmap items.
	 */

	/* global config must be initialized */
	ast_assert(cfg->featuregroups != NULL);
	ast_assert(cfg->applicationmap != NULL);
	ao2_callback_data(cfg->featuregroups, 0, check_featuregroup, &err, cfg->applicationmap);

	return err;
}

static int internal_feature_read(struct ast_channel *chan, const char *cmd, char *data,
	       char *buf, size_t len)
{
	int res;
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);
	SCOPED_CHANNELLOCK(lock, chan);

	if (!strcasecmp(data, "inherit")) {
		struct ast_datastore *ds = get_feature_chan_ds(chan);
		unsigned int inherit = ds ? ds->inheritance : 0;

		snprintf(buf, len, "%s", inherit ? "yes" : "no");
		return 0;
	}

	cfg = get_feature_ds(chan);
	if (!cfg) {
		return -1;
	}

	res = general_get(cfg->global->general, data, buf, len) &&
		xfer_get(cfg->global->xfer, data, buf, len) &&
		pickup_get(cfg->global->pickup, data, buf, len);

	if (res) {
		ast_log(LOG_WARNING, "Invalid argument '%s' to FEATURE()\n", data);
	}

	return res;
}

static int internal_feature_write(struct ast_channel *chan, const char *cmd, char *data,
		const char *value)
{
	int res;
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);
	SCOPED_CHANNELLOCK(lock, chan);

	if (!strcasecmp(data, "inherit")) {
		struct ast_datastore *ds = get_feature_chan_ds(chan);
		if (ds) {
			ds->inheritance = ast_true(value) ? DATASTORE_INHERIT_FOREVER : 0;
		}
		return 0;
	}

	if (!(cfg = get_feature_ds(chan))) {
		return -1;
	}

	res = general_set(cfg->global->general, data, value) &&
		xfer_set(cfg->global->xfer, data, value) &&
		pickup_set(cfg->global->pickup, data, value);

	if (res) {
		ast_log(LOG_WARNING, "Invalid argument '%s' to FEATURE()\n", data);
	}

	return res;
}

static int internal_featuremap_read(struct ast_channel *chan, const char *cmd, char *data,
	       char *buf, size_t len)
{
	int res;
	SCOPED_CHANNELLOCK(lock, chan);

	res = ast_get_builtin_feature(chan, data, buf, len);

	if (res) {
		ast_log(LOG_WARNING, "Invalid argument '%s' to FEATUREMAP()\n", data);
	}

	return res;
}

static int internal_featuremap_write(struct ast_channel *chan, const char *cmd, char *data,
		const char *value)
{
	int res;
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);
	SCOPED_CHANNELLOCK(lock, chan);

	if (!(cfg = get_feature_ds(chan))) {
		return -1;
	}

	res = featuremap_set(cfg->featuremap, data, value);
	if (res) {
		ast_log(LOG_WARNING, "Invalid argument '%s' to FEATUREMAP()\n", data);
		return -1;
	}

	return 0;
}

static int feature_read(struct ast_channel *chan, const char *cmd, char *data,
		char *buf, size_t len)
{
	if (!chan) {
		ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
		return -1;
	}

	return internal_feature_read(chan, cmd, data, buf, len);
}

static int feature_write(struct ast_channel *chan, const char *cmd, char *data,
		const char *value)
{
	if (!chan) {
		ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
		return -1;
	}

	return internal_feature_write(chan, cmd, data, value);
}

static int featuremap_read(struct ast_channel *chan, const char *cmd, char *data,
		char *buf, size_t len)
{
	if (!chan) {
		ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
		return -1;
	}

	return internal_featuremap_read(chan, cmd, data, buf, len);
}

static int featuremap_write(struct ast_channel *chan, const char *cmd, char *data,
		const char *value)
{
	if (!chan) {
		ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
		return -1;
	}

	return internal_featuremap_write(chan, cmd, data, value);
}

static struct ast_custom_function feature_function = {
	.name = "FEATURE",
	.read = feature_read,
	.write = feature_write
};

static struct ast_custom_function featuremap_function = {
	.name = "FEATUREMAP",
	.read = featuremap_read,
	.write = featuremap_write
};

static int load_config(void)
{
	if (aco_info_init(&cfg_info)) {
		ast_log(LOG_ERROR, "Unable to initialize configuration info for features\n");
		return -1;
	}

	aco_option_register_custom(&cfg_info, "featuredigittimeout", ACO_EXACT, global_options,
			__stringify(DEFAULT_FEATURE_DIGIT_TIMEOUT), general_handler, 0);
	aco_option_register_custom(&cfg_info, "recordingfailsound", ACO_EXACT, global_options,
			DEFAULT_RECORDING_FAIL_SOUND, general_handler, 0);
	aco_option_register_custom(&cfg_info, "courtesytone", ACO_EXACT, global_options,
			DEFAULT_COURTESY_TONE, general_handler, 0);

	aco_option_register_custom(&cfg_info, "transferdigittimeout", ACO_EXACT, global_options,
			__stringify(DEFAULT_TRANSFER_DIGIT_TIMEOUT), xfer_handler, 0)
	aco_option_register_custom(&cfg_info, "atxfernoanswertimeout", ACO_EXACT, global_options,
			__stringify(DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER), xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "atxferdropcall", ACO_EXACT, global_options,
			__stringify(DEFAULT_ATXFER_DROP_CALL), xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "atxferloopdelay", ACO_EXACT, global_options,
			__stringify(DEFAULT_ATXFER_LOOP_DELAY), xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "atxfercallbackretries", ACO_EXACT, global_options,
			__stringify(DEFAULT_ATXFER_CALLBACK_RETRIES), xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "xfersound", ACO_EXACT, global_options,
			DEFAULT_XFERSOUND, xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "xferfailsound", ACO_EXACT, global_options,
			DEFAULT_XFERFAILSOUND, xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "atxferabort", ACO_EXACT, global_options,
			DEFAULT_ATXFER_ABORT, xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "atxfercomplete", ACO_EXACT, global_options,
			DEFAULT_ATXFER_COMPLETE, xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "atxferthreeway", ACO_EXACT, global_options,
			DEFAULT_ATXFER_THREEWAY, xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "atxferswap", ACO_EXACT, global_options,
			DEFAULT_ATXFER_SWAP, xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "transferdialattempts", ACO_EXACT, global_options,
			__stringify(DEFAULT_TRANSFER_DIAL_ATTEMPTS), xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "transferretrysound", ACO_EXACT, global_options,
			DEFAULT_TRANSFER_RETRY_SOUND, xfer_handler, 0);
	aco_option_register_custom(&cfg_info, "transferinvalidsound", ACO_EXACT, global_options,
			DEFAULT_TRANSFER_INVALID_SOUND, xfer_handler, 0);

	aco_option_register_custom(&cfg_info, "pickupexten", ACO_EXACT, global_options,
			DEFAULT_PICKUPEXTEN, pickup_handler, 0);
	aco_option_register_custom(&cfg_info, "pickupsound", ACO_EXACT, global_options,
			DEFAULT_PICKUPSOUND, pickup_handler, 0);
	aco_option_register_custom(&cfg_info, "pickupfailsound", ACO_EXACT, global_options,
			DEFAULT_PICKUPFAILSOUND, pickup_handler, 0);

	aco_option_register_custom_nodoc(&cfg_info, "context", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkext", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkext_exclusive", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkinghints", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkedmusicclass", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkingtime", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkpos", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "findslot", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkedcalltransfers", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkedcallreparking", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkedcallhangup", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkedcallrecording", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "comebackcontext", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "comebacktoorigin", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "comebackdialtime", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "parkeddynamic", ACO_EXACT, global_options,
			"", unsupported_handler, 0);
	aco_option_register_custom_nodoc(&cfg_info, "adsipark", ACO_EXACT, global_options,
			"", unsupported_handler, 0);

	aco_option_register_custom(&cfg_info, "blindxfer", ACO_EXACT, featuremap_options,
			DEFAULT_FEATUREMAP_BLINDXFER, featuremap_handler, 0);
	aco_option_register_custom(&cfg_info, "disconnect", ACO_EXACT, featuremap_options,
			DEFAULT_FEATUREMAP_DISCONNECT, featuremap_handler, 0);
	aco_option_register_custom(&cfg_info, "automon", ACO_EXACT, featuremap_options,
			DEFAULT_FEATUREMAP_AUTOMON, featuremap_handler, 0);
	aco_option_register_custom(&cfg_info, "atxfer", ACO_EXACT, featuremap_options,
			DEFAULT_FEATUREMAP_ATXFER, featuremap_handler, 0);
	aco_option_register_custom(&cfg_info, "parkcall", ACO_EXACT, featuremap_options,
			DEFAULT_FEATUREMAP_PARKCALL, featuremap_handler, 0);
	aco_option_register_custom(&cfg_info, "automixmon", ACO_EXACT, featuremap_options,
			DEFAULT_FEATUREMAP_AUTOMIXMON, featuremap_handler, 0);

	aco_option_register_custom(&cfg_info, "", ACO_PREFIX, applicationmap_options,
			"", applicationmap_handler, 0);

	aco_option_register_custom(&cfg_info, "", ACO_PREFIX, featuregroup_options,
			"", featuregroup_handler, 0);

	aco_option_register_custom_nodoc(&cfg_info, "", ACO_PREFIX, parkinglot_options,
			"", unsupported_handler, 0);

	if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
		RAII_VAR(struct features_config *, features_cfg, features_config_alloc(), ao2_cleanup);

		if (aco_set_defaults(&global_option, "general", features_cfg->global) ||
			aco_set_defaults(&featuremap_option, "featuremap", features_cfg->featuremap)) {
			ast_log(LOG_ERROR, "Failed to load features.conf and failed to initialize defaults.\n");
			return -1;
		}

		ast_log(LOG_NOTICE, "Could not load features config; using defaults\n");
		ao2_global_obj_replace_unref(globals, features_cfg);
	}

	return 0;
}

static int print_featuregroup(void *obj, void *arg, int flags)
{
	struct featuregroup_item *item = obj;
	struct ast_cli_args *a = arg;

	ast_cli(a->fd, "===> --> %s (%s)\n", item->appmap_item_name,
			S_OR(item->dtmf_override, item->appmap_item->dtmf));

	return 0;
}

static int print_featuregroups(void *obj, void *arg, int flags)
{
	struct featuregroup *group = obj;
	struct ast_cli_args *a = arg;

	ast_cli(a->fd, "===> Group: %s\n", group->name);

	ao2_callback(group->items, 0, print_featuregroup, a);
	return 0;
}

#define HFS_FORMAT "%-25s %-7s %-7s\n"

static int print_applicationmap(void *obj, void *arg, int flags)
{
	struct ast_applicationmap_item *item = obj;
	struct ast_cli_args *a = arg;

	ast_cli(a->fd, HFS_FORMAT, item->name, "no def", item->dtmf);
	return 0;
}

/*!
 * \brief CLI command to list configured features
 * \param e
 * \param cmd
 * \param a
 *
 * \retval CLI_SUCCESS on success.
 * \retval NULL when tab completion is used.
 */
static char *handle_feature_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	RAII_VAR(struct features_config *, cfg, NULL, ao2_cleanup);

	switch (cmd) {

	case CLI_INIT:
		e->command = "features show";
		e->usage =
			"Usage: features show\n"
			"       Lists configured features\n";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	cfg = ao2_global_obj_ref(globals);

	ast_cli(a->fd, HFS_FORMAT, "Builtin Feature", "Default", "Current");
	ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");

	ast_cli(a->fd, HFS_FORMAT, "Pickup", DEFAULT_PICKUPEXTEN, cfg->global->pickup->pickupexten);
	ast_cli(a->fd, HFS_FORMAT, "Blind Transfer", DEFAULT_FEATUREMAP_BLINDXFER, cfg->featuremap->blindxfer);
	ast_cli(a->fd, HFS_FORMAT, "Attended Transfer", DEFAULT_FEATUREMAP_ATXFER, cfg->featuremap->atxfer);
	ast_cli(a->fd, HFS_FORMAT, "One Touch Monitor", DEFAULT_FEATUREMAP_AUTOMON, cfg->featuremap->automon);
	ast_cli(a->fd, HFS_FORMAT, "Disconnect Call", DEFAULT_FEATUREMAP_DISCONNECT, cfg->featuremap->disconnect);
	ast_cli(a->fd, HFS_FORMAT, "Park Call", DEFAULT_FEATUREMAP_PARKCALL, cfg->featuremap->parkcall);
	ast_cli(a->fd, HFS_FORMAT, "One Touch MixMonitor", DEFAULT_FEATUREMAP_AUTOMIXMON, cfg->featuremap->automixmon);

	ast_cli(a->fd, "\n");
	ast_cli(a->fd, HFS_FORMAT, "Dynamic Feature", "Default", "Current");
	ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
	if (!cfg->applicationmap || ao2_container_count(cfg->applicationmap) == 0) {
		ast_cli(a->fd, "(none)\n");
	} else {
		ao2_callback(cfg->applicationmap, 0, print_applicationmap, a);
	}

	ast_cli(a->fd, "\nFeature Groups:\n");
	ast_cli(a->fd, "---------------\n");
	if (!cfg->featuregroups || ao2_container_count(cfg->featuregroups) == 0) {
		ast_cli(a->fd, "(none)\n");
	} else {
		ao2_callback(cfg->featuregroups, 0, print_featuregroups, a);
	}

	return CLI_SUCCESS;
}

static struct ast_cli_entry cli_features_config[] = {
	AST_CLI_DEFINE(handle_feature_show, "Lists configured features"),
};

void unload_features_config(void)
{
	ast_custom_function_unregister(&featuremap_function);
	ast_custom_function_unregister(&feature_function);
	ast_cli_unregister_multiple(cli_features_config, ARRAY_LEN(cli_features_config));
	aco_info_destroy(&cfg_info);
	ao2_global_obj_release(globals);
}

int reload_features_config(void)
{
	/* Rearm the parking config options have moved warning. */
	parking_warning = 0;

	if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
		return -1;
	}
	return 0;
}

int load_features_config(void)
{
	int res;

	res = load_config();
	res |= __ast_custom_function_register(&feature_function, NULL);
	res |= __ast_custom_function_register(&featuremap_function, NULL);
	res |= ast_cli_register_multiple(cli_features_config, ARRAY_LEN(cli_features_config));

	return res;
}