aboutsummaryrefslogtreecommitdiffstats
path: root/funcs/func_callerid.c
blob: bb631fe4ead9b4e882d1ceee4515056711db49fd (plain)
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999-2010, Digium, Inc.
 *
 * 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.
 */

/*! \file
 *
 * \brief Party ID related dialplan functions (Caller-ID, Connected-line, Redirecting)
 *
 * \ingroup functions
 *
 * See Also:
 * \arg \ref AstCREDITS
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/callerid.h"

/*
 * Do not document the CALLERID(pres) datatype.
 * The name and number now have their own presentation value.  The pres
 * option will simply live on as a historical relic with as best
 * as can be managed backward compatible meaning.
 *
 * Do not document the CALLERID(ton) datatype.
 * It is an alias for num-plan.
 *
 * Do not document the CALLERID(ANI-subaddr-...) datatype.
 * This is not used.
 *
 * Do not document the CONNECTEDLINE(source) datatype.
 * It has turned out to not be needed.  The source value is really
 * only useful as a possible tracing aid.
 *
 * Do not document the CONNECTEDLINE(pres) datatype.
 * The name and number now have their own presentation value.  The pres
 * option will simply live on as a historical relic with as best
 * as can be managed backward compatible meaning.
 *
 * Do not document the CONNECTEDLINE(ton) datatype.
 * It is an alias for num-plan.
 *
 * Do not document the REDIRECTING(pres) datatype.
 * It has turned out that the from-pres and to-pres values must be kept
 * separate.  They represent two different parties and there is a case when
 * they are active at the same time.  The plain pres option will simply
 * live on as a historical relic.
 *
 * Do not document the REDIRECTING(from-pres) or REDIRECTING(to-pres) datatypes.
 * The name and number now have their own presentation value.  The from-pres
 * and to-pres options will simply live on as a historical relic with as best
 * as can be managed backward compatible meaning.
 *
 * Do not document the REDIRECTING(from-ton) or REDIRECTING(to-ton) datatypes.
 * They are aliases for from-num-plan and to-num-plan respectively.
 */
/*** DOCUMENTATION
	<function name="CALLERID" language="en_US">
		<synopsis>
			Gets or sets Caller*ID data on the channel.
		</synopsis>
		<syntax>
			<parameter name="datatype" required="true">
				<para>The allowable datatypes are:</para>
				<enumlist>
					<enum name = "all" />
					<enum name = "name" />
					<enum name = "name-valid" />
					<enum name = "name-charset" />
					<enum name = "name-pres" />
					<enum name = "num" />
					<enum name = "num-valid" />
					<enum name = "num-plan" />
					<enum name = "num-pres" />
					<enum name = "subaddr" />
					<enum name = "subaddr-valid" />
					<enum name = "subaddr-type" />
					<enum name = "subaddr-odd" />
					<enum name = "tag" />
					<enum name = "ANI-all" />
					<enum name = "ANI-name" />
					<enum name = "ANI-name-valid" />
					<enum name = "ANI-name-charset" />
					<enum name = "ANI-name-pres" />
					<enum name = "ANI-num" />
					<enum name = "ANI-num-valid" />
					<enum name = "ANI-num-plan" />
					<enum name = "ANI-num-pres" />
					<enum name = "ANI-tag" />
					<enum name = "RDNIS" />
					<enum name = "DNID" />
					<enum name = "dnid-num-plan" />
					<enum name = "dnid-subaddr" />
					<enum name = "dnid-subaddr-valid" />
					<enum name = "dnid-subaddr-type" />
					<enum name = "dnid-subaddr-odd" />
				</enumlist>
			</parameter>
			<parameter name="CID">
				<para>Optional Caller*ID</para>
			</parameter>
		</syntax>
		<description>
			<para>Gets or sets Caller*ID data on the channel. Uses channel callerid by
			default or optional callerid, if specified.</para>
			<para>The allowable values for the <replaceable>name-charset</replaceable>
			field are the following:</para>
			<enumlist>
				<enum name = "unknown"><para>Unknown</para></enum>
				<enum name = "iso8859-1"><para>ISO8859-1</para></enum>
				<enum name = "withdrawn"><para>Withdrawn</para></enum>
				<enum name = "iso8859-2"><para>ISO8859-2</para></enum>
				<enum name = "iso8859-3"><para>ISO8859-3</para></enum>
				<enum name = "iso8859-4"><para>ISO8859-4</para></enum>
				<enum name = "iso8859-5"><para>ISO8859-5</para></enum>
				<enum name = "iso8859-7"><para>ISO8859-7</para></enum>
				<enum name = "bmp"><para>ISO10646 Bmp String</para></enum>
				<enum name = "utf8"><para>ISO10646 UTF-8 String</para></enum>
			</enumlist>
		</description>
	</function>
	<function name="CALLERPRES" language="en_US">
		<synopsis>
			Gets or sets Caller*ID presentation on the channel.
		</synopsis>
		<syntax />
		<description>
			<para>Gets or sets Caller*ID presentation on the channel.
			This function is deprecated in favor of CALLERID(num-pres)
			and CALLERID(name-pres).
			The following values are valid:</para>
			<enumlist>
				<enum name="allowed_not_screened">
					<para>Presentation Allowed, Not Screened.</para>
				</enum>
				<enum name="allowed_passed_screen">
					<para>Presentation Allowed, Passed Screen.</para>
				</enum>
				<enum name="allowed_failed_screen">
					<para>Presentation Allowed, Failed Screen.</para>
				</enum>
				<enum name="allowed">
					<para>Presentation Allowed, Network Number.</para>
				</enum>
				<enum name="prohib_not_screened">
					<para>Presentation Prohibited, Not Screened.</para>
				</enum>
				<enum name="prohib_passed_screen">
					<para>Presentation Prohibited, Passed Screen.</para>
				</enum>
				<enum name="prohib_failed_screen">
					<para>Presentation Prohibited, Failed Screen.</para>
				</enum>
				<enum name="prohib">
					<para>Presentation Prohibited, Network Number.</para>
				</enum>
				<enum name="unavailable">
					<para>Number Unavailable.</para>
				</enum>
			</enumlist>
		</description>
	</function>
	<function name="CONNECTEDLINE" language="en_US">
		<synopsis>
			Gets or sets Connected Line data on the channel.
		</synopsis>
		<syntax>
			<parameter name="datatype" required="true">
				<para>The allowable datatypes are:</para>
				<enumlist>
					<enum name = "all" />
					<enum name = "name" />
					<enum name = "name-valid" />
					<enum name = "name-charset" />
					<enum name = "name-pres" />
					<enum name = "num" />
					<enum name = "num-valid" />
					<enum name = "num-plan" />
					<enum name = "num-pres" />
					<enum name = "subaddr" />
					<enum name = "subaddr-valid" />
					<enum name = "subaddr-type" />
					<enum name = "subaddr-odd" />
					<enum name = "tag" />
				</enumlist>
			</parameter>
			<parameter name="i">
				<para>If set, this will prevent the channel from sending out protocol
				messages because of the value being set</para>
			</parameter>
		</syntax>
		<description>
			<para>Gets or sets Connected Line data on the channel.</para>
			<para>The allowable values for the <replaceable>name-charset</replaceable>
			field are the following:</para>
			<enumlist>
				<enum name = "unknown"><para>Unknown</para></enum>
				<enum name = "iso8859-1"><para>ISO8859-1</para></enum>
				<enum name = "withdrawn"><para>Withdrawn</para></enum>
				<enum name = "iso8859-2"><para>ISO8859-2</para></enum>
				<enum name = "iso8859-3"><para>ISO8859-3</para></enum>
				<enum name = "iso8859-4"><para>ISO8859-4</para></enum>
				<enum name = "iso8859-5"><para>ISO8859-5</para></enum>
				<enum name = "iso8859-7"><para>ISO8859-7</para></enum>
				<enum name = "bmp"><para>ISO10646 Bmp String</para></enum>
				<enum name = "utf8"><para>ISO10646 UTF-8 String</para></enum>
			</enumlist>
		</description>
	</function>
	<function name="REDIRECTING" language="en_US">
		<synopsis>
			Gets or sets Redirecting data on the channel.
		</synopsis>
		<syntax>
			<parameter name="datatype" required="true">
				<para>The allowable datatypes are:</para>
				<enumlist>
					<enum name = "from-all" />
					<enum name = "from-name" />
					<enum name = "from-name-valid" />
					<enum name = "from-name-charset" />
					<enum name = "from-name-pres" />
					<enum name = "from-num" />
					<enum name = "from-num-valid" />
					<enum name = "from-num-plan" />
					<enum name = "from-num-pres" />
					<enum name = "from-subaddr" />
					<enum name = "from-subaddr-valid" />
					<enum name = "from-subaddr-type" />
					<enum name = "from-subaddr-odd" />
					<enum name = "from-tag" />
					<enum name = "to-all" />
					<enum name = "to-name" />
					<enum name = "to-name-valid" />
					<enum name = "to-name-charset" />
					<enum name = "to-name-pres" />
					<enum name = "to-num" />
					<enum name = "to-num-valid" />
					<enum name = "to-num-plan" />
					<enum name = "to-num-pres" />
					<enum name = "to-subaddr" />
					<enum name = "to-subaddr-valid" />
					<enum name = "to-subaddr-type" />
					<enum name = "to-subaddr-odd" />
					<enum name = "to-tag" />
					<enum name = "reason" />
					<enum name = "count" />
				</enumlist>
			</parameter>
			<parameter name="i">
				<para>If set, this will prevent the channel from sending out protocol
				messages because of the value being set</para>
			</parameter>
		</syntax>
		<description>
			<para>Gets or sets Redirecting data on the channel.</para>
			<para>The allowable values for the <replaceable>reason</replaceable>
			field are the following:</para>
			<enumlist>
				<enum name = "unknown"><para>Unknown</para></enum>
				<enum name = "cfb"><para>Call Forwarding Busy</para></enum>
				<enum name = "cfnr"><para>Call Forwarding No Reply</para></enum>
				<enum name = "unavailable"><para>Callee is Unavailable</para></enum>
				<enum name = "time_of_day"><para>Time of Day</para></enum>
				<enum name = "dnd"><para>Do Not Disturb</para></enum>
				<enum name = "deflection"><para>Call Deflection</para></enum>
				<enum name = "follow_me"><para>Follow Me</para></enum>
				<enum name = "out_of_order"><para>Called DTE Out-Of-Order</para></enum>
				<enum name = "away"><para>Callee is Away</para></enum>
				<enum name = "cf_dte"><para>Call Forwarding By The Called DTE</para></enum>
				<enum name = "cfu"><para>Call Forwarding Unconditional</para></enum>
			</enumlist>
			<para>The allowable values for the <replaceable>xxx-name-charset</replaceable>
			field are the following:</para>
			<enumlist>
				<enum name = "unknown"><para>Unknown</para></enum>
				<enum name = "iso8859-1"><para>ISO8859-1</para></enum>
				<enum name = "withdrawn"><para>Withdrawn</para></enum>
				<enum name = "iso8859-2"><para>ISO8859-2</para></enum>
				<enum name = "iso8859-3"><para>ISO8859-3</para></enum>
				<enum name = "iso8859-4"><para>ISO8859-4</para></enum>
				<enum name = "iso8859-5"><para>ISO8859-5</para></enum>
				<enum name = "iso8859-7"><para>ISO8859-7</para></enum>
				<enum name = "bmp"><para>ISO10646 Bmp String</para></enum>
				<enum name = "utf8"><para>ISO10646 UTF-8 String</para></enum>
			</enumlist>
		</description>
	</function>
 ***/

enum ID_FIELD_STATUS {
	ID_FIELD_VALID,
	ID_FIELD_INVALID,
	ID_FIELD_UNKNOWN
};

AST_DEFINE_APP_ARGS_TYPE(ast_party_func_args,
	AST_APP_ARG(member);	/*!< Member name */
	AST_APP_ARG(opts);		/*!< Options token */
	AST_APP_ARG(other);		/*!< Any remining unused arguments */
	);

AST_DEFINE_APP_ARGS_TYPE(ast_party_members,
	AST_APP_ARG(subnames[10]);	/*!< Option member subnames */
	);

enum CONNECTED_LINE_OPT_FLAGS {
	CONNECTED_LINE_OPT_INHIBIT = (1 << 0),
};
enum CONNECTED_LINE_OPT_ARGS {
	CONNECTED_LINE_OPT_DUMMY,	/*!< Delete this if CONNECTED_LINE ever gets an option with parameters. */

	/*! \note This entry _MUST_ be the last one in the enum */
	CONNECTED_LINE_OPT_ARG_ARRAY_SIZE
};

AST_APP_OPTIONS(connectedline_opts, BEGIN_OPTIONS
	AST_APP_OPTION('i', CONNECTED_LINE_OPT_INHIBIT),
END_OPTIONS);

enum REDIRECTING_OPT_FLAGS {
	REDIRECTING_OPT_INHIBIT = (1 << 0),
};
enum REDIRECTING_OPT_ARGS {
	REDIRECTING_OPT_DUMMY,	/*!< Delete this if REDIRECTING ever gets an option with parameters. */

	/*! \note This entry _MUST_ be the last one in the enum */
	REDIRECTING_OPT_ARG_ARRAY_SIZE
};

AST_APP_OPTIONS(redirecting_opts, BEGIN_OPTIONS
	AST_APP_OPTION('i', REDIRECTING_OPT_INHIBIT),
END_OPTIONS);

/*!
 * \internal
 * \brief Read values from the party name struct.
 * \since 1.8
 *
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer.
 * \param argc Number of party member subnames.
 * \param argv Party member subnames given.
 * \param name Party name to get values from.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS party_name_read(char *buf, size_t len, int argc, char *argv[], const struct ast_party_name *name)
{
	enum ID_FIELD_STATUS status;

	status = ID_FIELD_VALID;

	if (argc == 0) {
		/* We want the name string */
		if (name->valid && name->str) {
			ast_copy_string(buf, name->str, len);
		}
	} else if (argc == 1 && !strcasecmp("valid", argv[0])) {
		snprintf(buf, len, "%d", name->valid);
	} else if (argc == 1 && !strcasecmp("charset", argv[0])) {
		ast_copy_string(buf, ast_party_name_charset_str(name->char_set), len);
	} else if (argc == 1 && !strncasecmp("pres", argv[0], 4)) {
		/* Accept pres[entation] */
		ast_copy_string(buf, ast_named_caller_presentation(name->presentation), len);
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}

/*!
 * \internal
 * \brief Read values from the party number struct.
 * \since 1.8
 *
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer.
 * \param argc Number of party member subnames.
 * \param argv Party member subnames given.
 * \param number Party number to get values from.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS party_number_read(char *buf, size_t len, int argc, char *argv[], const struct ast_party_number *number)
{
	enum ID_FIELD_STATUS status;

	status = ID_FIELD_VALID;

	if (argc == 0) {
		/* We want the number string */
		if (number->valid && number->str) {
			ast_copy_string(buf, number->str, len);
		}
	} else if (argc == 1 && !strcasecmp("valid", argv[0])) {
		snprintf(buf, len, "%d", number->valid);
	} else if (argc == 1 && !strcasecmp("plan", argv[0])) {
		snprintf(buf, len, "%d", number->plan);
	} else if (argc == 1 && !strncasecmp("pres", argv[0], 4)) {
		/* Accept pres[entation] */
		ast_copy_string(buf, ast_named_caller_presentation(number->presentation), len);
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}

/*!
 * \internal
 * \brief Read values from the party subaddress struct.
 * \since 1.8
 *
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer.
 * \param argc Number of party member subnames.
 * \param argv Party member subnames given.
 * \param subaddress Party subaddress to get values from.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS party_subaddress_read(char *buf, size_t len, int argc, char *argv[], const struct ast_party_subaddress *subaddress)
{
	enum ID_FIELD_STATUS status;

	status = ID_FIELD_VALID;

	if (argc == 0) {
		/* We want the subaddress string */
		if (subaddress->str) {
			ast_copy_string(buf, subaddress->str, len);
		}
	} else if (argc == 1 && !strcasecmp("valid", argv[0])) {
		snprintf(buf, len, "%d", subaddress->valid);
	} else if (argc == 1 && !strcasecmp("type", argv[0])) {
		snprintf(buf, len, "%d", subaddress->type);
	} else if (argc == 1 && !strcasecmp("odd", argv[0])) {
		snprintf(buf, len, "%d", subaddress->odd_even_indicator);
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}

/*!
 * \internal
 * \brief Read values from the party id struct.
 * \since 1.8
 *
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer.
 * \param argc Number of party member subnames.
 * \param argv Party member subnames given.
 * \param id Party id to get values from.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS party_id_read(char *buf, size_t len, int argc, char *argv[], const struct ast_party_id *id)
{
	enum ID_FIELD_STATUS status;

	if (argc == 0) {
		/* Must have at least one subname. */
		return ID_FIELD_UNKNOWN;
	}

	status = ID_FIELD_VALID;

	if (argc == 1 && !strcasecmp("all", argv[0])) {
		snprintf(buf, len, "\"%s\" <%s>",
			 S_COR(id->name.valid, id->name.str, ""),
			 S_COR(id->number.valid, id->number.str, ""));
	} else if (!strcasecmp("name", argv[0])) {
		status = party_name_read(buf, len, argc - 1, argv + 1, &id->name);
	} else if (!strncasecmp("num", argv[0], 3)) {
		/* Accept num[ber] */
		status = party_number_read(buf, len, argc - 1, argv + 1, &id->number);
	} else if (!strncasecmp("subaddr", argv[0], 7)) {
		/* Accept subaddr[ess] */
		status = party_subaddress_read(buf, len, argc - 1, argv + 1, &id->subaddress);
	} else if (argc == 1 && !strcasecmp("tag", argv[0])) {
		if (id->tag) {
			ast_copy_string(buf, id->tag, len);
		}
	} else if (argc == 1 && !strcasecmp("ton", argv[0])) {
		/* ton is an alias for num-plan */
		snprintf(buf, len, "%d", id->number.plan);
	} else if (argc == 1 && !strncasecmp("pres", argv[0], 4)) {
		/*
		 * Accept pres[entation]
		 * This is the combined name/number presentation.
		 */
		ast_copy_string(buf,
			ast_named_caller_presentation(ast_party_id_presentation(id)), len);
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}

/*!
 * \internal
 * \brief Write new values to the party name struct
 * \since 1.8
 *
 * \param name Party name struct to write values
 * \param argc Number of party member subnames.
 * \param argv Party member subnames given.
 * \param value Value to assign to the party name.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_INVALID on error with field value.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS party_name_write(struct ast_party_name *name, int argc, char *argv[], const char *value)
{
	char *val;
	enum ID_FIELD_STATUS status;

	status = ID_FIELD_VALID;

	if (argc == 0) {
		/* We are setting the name string */
		name->valid = 1;
		name->str = ast_strdup(value);
		ast_trim_blanks(name->str);
	} else if (argc == 1 && !strcasecmp("valid", argv[0])) {
		name->valid = atoi(value) ? 1 : 0;
	} else if (argc == 1 && !strcasecmp("charset", argv[0])) {
		int char_set;

		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			char_set = atoi(val);
		} else {
			char_set = ast_party_name_charset_parse(val);
		}

		if (char_set < 0) {
			ast_log(LOG_ERROR,
				"Unknown name char-set '%s', value unchanged\n", val);
			status = ID_FIELD_INVALID;
		} else {
			name->char_set = char_set;
		}
	} else if (argc == 1 && !strncasecmp("pres", argv[0], 4)) {
		int pres;

		/* Accept pres[entation] */
		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			pres = atoi(val);
		} else {
			pres = ast_parse_caller_presentation(val);
		}

		if (pres < 0) {
			ast_log(LOG_ERROR,
				"Unknown name presentation '%s', value unchanged\n", val);
			status = ID_FIELD_INVALID;
		} else {
			name->presentation = pres;
		}
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}

/*!
 * \internal
 * \brief Write new values to the party number struct
 * \since 1.8
 *
 * \param number Party number struct to write values
 * \param argc Number of party member subnames.
 * \param argv Party member subnames given.
 * \param value Value to assign to the party number.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_INVALID on error with field value.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS party_number_write(struct ast_party_number *number, int argc, char *argv[], const char *value)
{
	char *val;
	enum ID_FIELD_STATUS status;

	status = ID_FIELD_VALID;

	if (argc == 0) {
		/* We are setting the number string */
		number->valid = 1;
		number->str = ast_strdup(value);
		ast_trim_blanks(number->str);
	} else if (argc == 1 && !strcasecmp("valid", argv[0])) {
		number->valid = atoi(value) ? 1 : 0;
	} else if (argc == 1 && !strcasecmp("plan", argv[0])) {
		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			number->plan = atoi(val);
		} else {
			ast_log(LOG_ERROR,
				"Unknown type-of-number/numbering-plan '%s', value unchanged\n", val);
			status = ID_FIELD_INVALID;
		}
	} else if (argc == 1 && !strncasecmp("pres", argv[0], 4)) {
		int pres;

		/* Accept pres[entation] */
		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			pres = atoi(val);
		} else {
			pres = ast_parse_caller_presentation(val);
		}

		if (pres < 0) {
			ast_log(LOG_ERROR,
				"Unknown number presentation '%s', value unchanged\n", val);
			status = ID_FIELD_INVALID;
		} else {
			number->presentation = pres;
		}
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}

/*!
 * \internal
 * \brief Write new values to the party subaddress struct
 * \since 1.8
 *
 * \param subaddress Party subaddress struct to write values
 * \param argc Number of party member subnames.
 * \param argv Party member subnames given.
 * \param value Value to assign to the party subaddress.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_INVALID on error with field value.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS party_subaddress_write(struct ast_party_subaddress *subaddress, int argc, char *argv[], const char *value)
{
	enum ID_FIELD_STATUS status;

	status = ID_FIELD_VALID;

	if (argc == 0) {
		/* We are setting the subaddress string */
		subaddress->str = ast_strdup(value);
		ast_trim_blanks(subaddress->str);
	} else if (argc == 1 && !strcasecmp("valid", argv[0])) {
		subaddress->valid = atoi(value) ? 1 : 0;
	} else if (argc == 1 && !strcasecmp("type", argv[0])) {
		subaddress->type = atoi(value) ? 2 : 0;
	} else if (argc == 1 && !strcasecmp("odd", argv[0])) {
		subaddress->odd_even_indicator = atoi(value) ? 1 : 0;
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}

/*!
 * \internal
 * \brief Write new values to the party id struct
 * \since 1.8
 *
 * \param id Party ID struct to write values
 * \param argc Number of party member subnames.
 * \param argv Party member subnames given.
 * \param value Value to assign to the party id.
 *
 * \retval ID_FIELD_VALID on success.
 * \retval ID_FIELD_INVALID on error with field value.
 * \retval ID_FIELD_UNKNOWN on unknown field name.
 */
static enum ID_FIELD_STATUS party_id_write(struct ast_party_id *id, int argc, char *argv[], const char *value)
{
	char *val;
	enum ID_FIELD_STATUS status;

	if (argc == 0) {
		/* Must have at least one subname. */
		return ID_FIELD_UNKNOWN;
	}

	status = ID_FIELD_VALID;

	if (argc == 1 && !strcasecmp("all", argv[0])) {
		char name[256];
		char num[256];

		ast_callerid_split(value, name, sizeof(name), num, sizeof(num));
		id->name.valid = 1;
		id->name.str = ast_strdup(name);
		if (!id->name.str) {
			return ID_FIELD_INVALID;
		}
		id->number.valid = 1;
		id->number.str = ast_strdup(num);
		if (!id->number.str) {
			return ID_FIELD_INVALID;
		}
	} else if (!strcasecmp("name", argv[0])) {
		status = party_name_write(&id->name, argc - 1, argv + 1, value);
	} else if (!strncasecmp("num", argv[0], 3)) {
		/* Accept num[ber] */
		status = party_number_write(&id->number, argc - 1, argv + 1, value);
	} else if (!strncasecmp("subaddr", argv[0], 7)) {
		/* Accept subaddr[ess] */
		status = party_subaddress_write(&id->subaddress, argc - 1, argv + 1, value);
	} else if (argc == 1 && !strcasecmp("tag", argv[0])) {
		id->tag = ast_strdup(value);
		ast_trim_blanks(id->tag);
	} else if (argc == 1 && !strcasecmp("ton", argv[0])) {
		/* ton is an alias for num-plan */
		argv[0] = "plan";
		status = party_number_write(&id->number, argc, argv, value);
	} else if (argc == 1 && !strncasecmp("pres", argv[0], 4)) {
		int pres;

		/*
		 * Accept pres[entation]
		 * This is the combined name/number presentation.
		 */
		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			pres = atoi(val);
		} else {
			pres = ast_parse_caller_presentation(val);
		}

		if (pres < 0) {
			ast_log(LOG_ERROR,
				"Unknown combined presentation '%s', value unchanged\n", val);
			status = ID_FIELD_INVALID;
		} else {
			id->name.presentation = pres;
			id->number.presentation = pres;
		}
	} else {
		status = ID_FIELD_UNKNOWN;
	}

	return status;
}

/*! TRUE if we have already notified about CALLERPRES being deprecated. */
static int callerpres_deprecate_notify;

/*!
 * \internal
 * \brief Read values from the caller-id presentation information struct.
 *
 * \param chan Asterisk channel to read
 * \param cmd Not used
 * \param data Caller-id presentation function datatype string
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int callerpres_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	if (!callerpres_deprecate_notify) {
		callerpres_deprecate_notify = 1;
		ast_log(LOG_WARNING, "CALLERPRES is deprecated."
			"  Use CALLERID(name-pres) or CALLERID(num-pres) instead.\n");
	}
	ast_copy_string(buf,
		ast_named_caller_presentation(ast_party_id_presentation(&chan->caller.id)), len);
	return 0;
}

/*!
 * \internal
 * \brief Write new values to the caller-id presentation information struct.
 *
 * \param chan Asterisk channel to update
 * \param cmd Not used
 * \param data Caller-id presentation function datatype string
 * \param value Value to assign to the caller-id presentation information struct.
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int callerpres_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
	int pres;

	if (!callerpres_deprecate_notify) {
		callerpres_deprecate_notify = 1;
		ast_log(LOG_WARNING, "CALLERPRES is deprecated."
			"  Use CALLERID(name-pres) or CALLERID(num-pres) instead.\n");
	}

	pres = ast_parse_caller_presentation(value);
	if (pres < 0) {
		ast_log(LOG_WARNING, "'%s' is not a valid presentation (see 'show function CALLERPRES')\n", value);
	} else {
		chan->caller.id.name.presentation = pres;
		chan->caller.id.number.presentation = pres;
	}
	return 0;
}

/*!
 * \internal
 * \brief Read values from the caller-id information struct.
 *
 * \param chan Asterisk channel to read
 * \param cmd Not used
 * \param data Caller-id function datatype string
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int callerid_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	enum ID_FIELD_STATUS status;
	char *parms;
	struct ast_party_members member;
	AST_DECLARE_APP_ARGS(args,
		AST_APP_ARG(member);	/*!< Member name */
		AST_APP_ARG(cid);		/*!< Optional caller id to parse instead of from the channel. */
		);

	/* Ensure that the buffer is empty */
	*buf = 0;

	if (!chan) {
		return -1;
	}

	parms = ast_strdupa(data);
	AST_STANDARD_APP_ARGS(args, parms);
	if (args.argc == 0) {
		/* Must have at least one argument. */
		return -1;
	}

	AST_NONSTANDARD_APP_ARGS(member, args.member, '-');
	if (member.argc == 0 || ARRAY_LEN(member.subnames) <= member.argc) {
		/* Too few or too many subnames */
		return -1;
	}

	if (args.argc == 2) {
		char name[80];
		char num[80];

		ast_callerid_split(args.cid, name, sizeof(name), num, sizeof(num));

		if (member.argc == 1 && !strcasecmp("all", member.argv[0])) {
			snprintf(buf, len, "\"%s\" <%s>", name, num);
		} else if (member.argc == 1 && !strcasecmp("name", member.argv[0])) {
			ast_copy_string(buf, name, len);
		} else if (member.argc == 1 && !strncasecmp("num", member.argv[0], 3)) {
			/* Accept num[ber] */
			ast_copy_string(buf, num, len);
		} else {
			ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
		}
	} else {
		ast_channel_lock(chan);

		if (member.argc == 1 && !strcasecmp("rdnis", member.argv[0])) {
			if (chan->redirecting.from.number.valid
				&& chan->redirecting.from.number.str) {
				ast_copy_string(buf, chan->redirecting.from.number.str, len);
			}
		} else if (!strcasecmp("dnid", member.argv[0])) {
			if (member.argc == 1) {
				/* Setup as if user had given dnid-num instead. */
				member.argc = 2;
				member.argv[1] = "num";
			}
			if (!strncasecmp("num", member.argv[1], 3)) {
				/*
				 * Accept num[ber]
				 * dnid-num...
				 */
				if (member.argc == 2) {
					/* dnid-num */
					if (chan->dialed.number.str) {
						ast_copy_string(buf, chan->dialed.number.str, len);
					}
				} else if (member.argc == 3 && !strcasecmp("plan", member.argv[2])) {
					/* dnid-num-plan */
					snprintf(buf, len, "%d", chan->dialed.number.plan);
				} else {
					ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
				}
			} else if (!strncasecmp("subaddr", member.argv[1], 7)) {
				/*
				 * Accept subaddr[ess]
				 * dnid-subaddr...
				 */
				status = party_subaddress_read(buf, len, member.argc - 2, member.argv + 2,
					&chan->dialed.subaddress);
				switch (status) {
				case ID_FIELD_VALID:
				case ID_FIELD_INVALID:
					break;
				default:
					ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
					break;
				}
			} else {
				ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
			}
		} else if (member.argc == 1 && !strcasecmp("ani2", member.argv[0])) {
			snprintf(buf, len, "%d", chan->caller.ani2);
		} else if (!strcasecmp("ani", member.argv[0])) {
			if (member.argc == 1) {
				/* Setup as if user had given ani-num instead. */
				member.argc = 2;
				member.argv[1] = "num";
			}
			status = party_id_read(buf, len, member.argc - 1, member.argv + 1,
				&chan->caller.ani);
			switch (status) {
			case ID_FIELD_VALID:
			case ID_FIELD_INVALID:
				break;
			default:
				ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
				break;
			}
		} else {
			status = party_id_read(buf, len, member.argc, member.argv, &chan->caller.id);
			switch (status) {
			case ID_FIELD_VALID:
			case ID_FIELD_INVALID:
				break;
			default:
				ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
				break;
			}
		}

		ast_channel_unlock(chan);
	}

	return 0;
}

/*!
 * \internal
 * \brief Write new values to the caller-id information struct.
 *
 * \param chan Asterisk channel to update
 * \param cmd Not used
 * \param data Caller-id function datatype string
 * \param value Value to assign to the caller-id information struct.
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int callerid_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
	struct ast_party_caller caller;
	struct ast_party_dialed dialed;
	enum ID_FIELD_STATUS status;
	char *val;
	char *parms;
	struct ast_party_func_args args;
	struct ast_party_members member;

	if (!value || !chan) {
		return -1;
	}

	parms = ast_strdupa(data);
	AST_STANDARD_APP_ARGS(args, parms);
	if (args.argc == 0) {
		/* Must have at least one argument. */
		return -1;
	}

	AST_NONSTANDARD_APP_ARGS(member, args.member, '-');
	if (member.argc == 0 || ARRAY_LEN(member.subnames) <= member.argc) {
		/* Too few or too many subnames */
		return -1;
	}

	value = ast_skip_blanks(value);

	ast_channel_lock(chan);
	if (member.argc == 1 && !strcasecmp("rdnis", member.argv[0])) {
		chan->redirecting.from.number.valid = 1;
		ast_free(chan->redirecting.from.number.str);
		chan->redirecting.from.number.str = ast_strdup(value);
		if (chan->cdr) {
			ast_cdr_setcid(chan->cdr, chan);
		}
	} else if (!strcasecmp("dnid", member.argv[0])) {
		ast_party_dialed_set_init(&dialed, &chan->dialed);
		if (member.argc == 1) {
			/* Setup as if user had given dnid-num instead. */
			member.argc = 2;
			member.argv[1] = "num";
		}
		if (!strncasecmp("num", member.argv[1], 3)) {
			/*
			 * Accept num[ber]
			 * dnid-num...
			 */
			if (member.argc == 2) {
				/* dnid-num */
				dialed.number.str = ast_strdup(value);
				ast_trim_blanks(dialed.number.str);
				ast_party_dialed_set(&chan->dialed, &dialed);
				if (chan->cdr) {
					ast_cdr_setcid(chan->cdr, chan);
				}
			} else if (member.argc == 3 && !strcasecmp("plan", member.argv[2])) {
				/* dnid-num-plan */
				val = ast_strdupa(value);
				ast_trim_blanks(val);

				if (('0' <= val[0]) && (val[0] <= '9')) {
					chan->dialed.number.plan = atoi(val);
					if (chan->cdr) {
						ast_cdr_setcid(chan->cdr, chan);
					}
				} else {
					ast_log(LOG_ERROR,
						"Unknown type-of-number/numbering-plan '%s', value unchanged\n", val);
				}
			} else {
				ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
			}
		} else if (!strncasecmp("subaddr", member.argv[1], 7)) {
			/*
			 * Accept subaddr[ess]
			 * dnid-subaddr...
			 */
			status = party_subaddress_write(&dialed.subaddress, member.argc - 2,
				member.argv + 2, value);
			switch (status) {
			case ID_FIELD_VALID:
				ast_party_dialed_set(&chan->dialed, &dialed);
				if (chan->cdr) {
					ast_cdr_setcid(chan->cdr, chan);
				}
				break;
			case ID_FIELD_INVALID:
				break;
			default:
				ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
				break;
			}
		} else {
			ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
		}
		ast_party_dialed_free(&dialed);
	} else if (member.argc == 1 && !strcasecmp("ani2", member.argv[0])) {
		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			chan->caller.ani2 = atoi(val);
			if (chan->cdr) {
				ast_cdr_setcid(chan->cdr, chan);
			}
		} else {
			ast_log(LOG_ERROR, "Unknown callerid ani2 '%s', value unchanged\n", val);
		}
	} else if (!strcasecmp("ani", member.argv[0])) {
		ast_party_caller_set_init(&caller, &chan->caller);
		if (member.argc == 1) {
			/* Setup as if user had given ani-num instead. */
			member.argc = 2;
			member.argv[1] = "num";
		}
		status = party_id_write(&caller.ani, member.argc - 1, member.argv + 1, value);
		switch (status) {
		case ID_FIELD_VALID:
			ast_party_caller_set(&chan->caller, &caller, NULL);
			if (chan->cdr) {
				ast_cdr_setcid(chan->cdr, chan);
			}
			break;
		case ID_FIELD_INVALID:
			break;
		default:
			ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
			break;
		}
		ast_party_caller_free(&caller);
	} else {
		ast_party_caller_set_init(&caller, &chan->caller);
		status = party_id_write(&caller.id, member.argc, member.argv, value);
		switch (status) {
		case ID_FIELD_VALID:
			ast_channel_set_caller_event(chan, &caller, NULL);
			if (chan->cdr) {
				ast_cdr_setcid(chan->cdr, chan);
			}
			break;
		case ID_FIELD_INVALID:
			break;
		default:
			ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
			break;
		}
		ast_party_caller_free(&caller);
	}
	ast_channel_unlock(chan);

	return 0;
}

/*!
 * \internal
 * \brief Read values from the connected line information struct.
 *
 * \param chan Asterisk channel to read
 * \param cmd Not used
 * \param data Connected line function datatype string
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int connectedline_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	struct ast_party_members member;
	char *read_what;
	enum ID_FIELD_STATUS status;

	/* Ensure that the buffer is empty */
	*buf = 0;

	if (!chan) {
		return -1;
	}

	read_what = ast_strdupa(data);
	AST_NONSTANDARD_APP_ARGS(member, read_what, '-');
	if (member.argc == 0 || ARRAY_LEN(member.subnames) <= member.argc) {
		/* Too few or too many subnames */
		return -1;
	}

	ast_channel_lock(chan);

	if (member.argc == 1 && !strcasecmp("source", member.argv[0])) {
		ast_copy_string(buf, ast_connected_line_source_name(chan->connected.source), len);
	} else {
		status = party_id_read(buf, len, member.argc, member.argv, &chan->connected.id);
		switch (status) {
		case ID_FIELD_VALID:
		case ID_FIELD_INVALID:
			break;
		default:
			ast_log(LOG_ERROR, "Unknown connectedline data type '%s'.\n", data);
			break;
		}
	}

	ast_channel_unlock(chan);

	return 0;
}

/*!
 * \internal
 * \brief Write new values to the connected line information struct.
 *
 * \param chan Asterisk channel to update
 * \param cmd Not used
 * \param data Connected line function datatype string
 * \param value Value to assign to the connected line information struct.
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int connectedline_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
	struct ast_party_connected_line connected;
	enum ID_FIELD_STATUS status;
	char *val;
	char *parms;
	void (*set_it)(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update);
	struct ast_party_func_args args;
	struct ast_party_members member;
	struct ast_flags opts;
	char *opt_args[CONNECTED_LINE_OPT_ARG_ARRAY_SIZE];

	if (!value || !chan) {
		return -1;
	}

	parms = ast_strdupa(data);
	AST_STANDARD_APP_ARGS(args, parms);
	if (args.argc == 0) {
		/* Must have at least one argument. */
		return -1;
	}

	AST_NONSTANDARD_APP_ARGS(member, args.member, '-');
	if (member.argc == 0 || ARRAY_LEN(member.subnames) <= member.argc) {
		/* Too few or too many subnames */
		return -1;
	}

	if (ast_app_parse_options(connectedline_opts, &opts, opt_args, args.opts)) {
		/* General invalid option syntax. */
		return -1;
	}

	/* Determine if the update indication inhibit option is present */
	if (ast_test_flag(&opts, CONNECTED_LINE_OPT_INHIBIT)) {
		set_it = ast_channel_set_connected_line;
	} else {
		set_it = ast_channel_update_connected_line;
	}

	ast_channel_lock(chan);
	ast_party_connected_line_set_init(&connected, &chan->connected);
	ast_channel_unlock(chan);

	value = ast_skip_blanks(value);

	if (member.argc == 1 && !strcasecmp("source", member.argv[0])) {
		int source;

		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			source = atoi(val);
		} else {
			source = ast_connected_line_source_parse(val);
		}

		if (source < 0) {
			ast_log(LOG_ERROR, "Unknown connectedline source '%s', value unchanged\n", val);
		} else {
			connected.source = source;
			set_it(chan, &connected, NULL);
		}
	} else {
		status = party_id_write(&connected.id, member.argc, member.argv, value);
		switch (status) {
		case ID_FIELD_VALID:
			set_it(chan, &connected, NULL);
			break;
		case ID_FIELD_INVALID:
			break;
		default:
			ast_log(LOG_ERROR, "Unknown connectedline data type '%s'.\n", data);
			break;
		}
		ast_party_connected_line_free(&connected);
	}

	return 0;
}

/*!
 * \internal
 * \brief Read values from the redirecting information struct.
 *
 * \param chan Asterisk channel to read
 * \param cmd Not used
 * \param data Redirecting function datatype string
 * \param buf Buffer to fill with read value.
 * \param len Length of the buffer
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int redirecting_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
	struct ast_party_members member;
	char *read_what;
	enum ID_FIELD_STATUS status;

	/* Ensure that the buffer is empty */
	*buf = 0;

	if (!chan) {
		return -1;
	}

	read_what = ast_strdupa(data);
	AST_NONSTANDARD_APP_ARGS(member, read_what, '-');
	if (member.argc == 0 || ARRAY_LEN(member.subnames) <= member.argc) {
		/* Too few or too many subnames */
		return -1;
	}

	ast_channel_lock(chan);

	if (!strcasecmp("from", member.argv[0])) {
		status = party_id_read(buf, len, member.argc - 1, member.argv + 1,
			&chan->redirecting.from);
		switch (status) {
		case ID_FIELD_VALID:
		case ID_FIELD_INVALID:
			break;
		default:
			ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
			break;
		}
	} else if (!strcasecmp("to", member.argv[0])) {
		status = party_id_read(buf, len, member.argc - 1, member.argv + 1,
			&chan->redirecting.to);
		switch (status) {
		case ID_FIELD_VALID:
		case ID_FIELD_INVALID:
			break;
		default:
			ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
			break;
		}
	} else if (member.argc == 1 && !strncasecmp("pres", member.argv[0], 4)) {
		/*
		 * Accept pres[entation]
		 * This is the combined from name/number presentation.
		 */
		ast_copy_string(buf,
			ast_named_caller_presentation(
				ast_party_id_presentation(&chan->redirecting.from)), len);
	} else if (member.argc == 1 && !strcasecmp("reason", member.argv[0])) {
		ast_copy_string(buf, ast_redirecting_reason_name(chan->redirecting.reason), len);
	} else if (member.argc == 1 && !strcasecmp("count", member.argv[0])) {
		snprintf(buf, len, "%d", chan->redirecting.count);
	} else {
		ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
	}

	ast_channel_unlock(chan);

	return 0;
}

/*!
 * \internal
 * \brief Write new values to the redirecting information struct.
 *
 * \param chan Asterisk channel to update
 * \param cmd Not used
 * \param data Redirecting function datatype string
 * \param value Value to assign to the redirecting information struct.
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int redirecting_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
	struct ast_party_redirecting redirecting;
	enum ID_FIELD_STATUS status;
	char *val;
	char *parms;
	void (*set_it)(struct ast_channel *chan, const struct ast_party_redirecting *redirecting, const struct ast_set_party_redirecting *update);
	struct ast_party_func_args args;
	struct ast_party_members member;
	struct ast_flags opts;
	char *opt_args[REDIRECTING_OPT_ARG_ARRAY_SIZE];

	if (!value || !chan) {
		return -1;
	}

	parms = ast_strdupa(data);
	AST_STANDARD_APP_ARGS(args, parms);
	if (args.argc == 0) {
		/* Must have at least one argument. */
		return -1;
	}

	AST_NONSTANDARD_APP_ARGS(member, args.member, '-');
	if (member.argc == 0 || ARRAY_LEN(member.subnames) <= member.argc) {
		/* Too few or too many subnames */
		return -1;
	}

	if (ast_app_parse_options(redirecting_opts, &opts, opt_args, args.opts)) {
		/* General invalid option syntax. */
		return -1;
	}

	/* Determine if the update indication inhibit option is present */
	if (ast_test_flag(&opts, REDIRECTING_OPT_INHIBIT)) {
		set_it = ast_channel_set_redirecting;
	} else {
		set_it = ast_channel_update_redirecting;
	}

	ast_channel_lock(chan);
	ast_party_redirecting_set_init(&redirecting, &chan->redirecting);
	ast_channel_unlock(chan);

	value = ast_skip_blanks(value);

	if (!strcasecmp("from", member.argv[0])) {
		status = party_id_write(&redirecting.from, member.argc - 1, member.argv + 1,
			value);
		switch (status) {
		case ID_FIELD_VALID:
			set_it(chan, &redirecting, NULL);
			break;
		case ID_FIELD_INVALID:
			break;
		default:
			ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
			break;
		}
		ast_party_redirecting_free(&redirecting);
	} else if (!strcasecmp("to", member.argv[0])) {
		status = party_id_write(&redirecting.to, member.argc - 1, member.argv + 1, value);
		switch (status) {
		case ID_FIELD_VALID:
			set_it(chan, &redirecting, NULL);
			break;
		case ID_FIELD_INVALID:
			break;
		default:
			ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
			break;
		}
		ast_party_redirecting_free(&redirecting);
	} else if (member.argc == 1 && !strncasecmp("pres", member.argv[0], 4)) {
		int pres;

		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			pres = atoi(val);
		} else {
			pres = ast_parse_caller_presentation(val);
		}

		if (pres < 0) {
			ast_log(LOG_ERROR,
				"Unknown redirecting combined presentation '%s', value unchanged\n", val);
		} else {
			redirecting.from.name.presentation = pres;
			redirecting.from.number.presentation = pres;
			redirecting.to.name.presentation = pres;
			redirecting.to.number.presentation = pres;
			set_it(chan, &redirecting, NULL);
		}
	} else if (member.argc == 1 && !strcasecmp("reason", member.argv[0])) {
		int reason;

		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			reason = atoi(val);
		} else {
			reason = ast_redirecting_reason_parse(val);
		}

		if (reason < 0) {
			ast_log(LOG_ERROR, "Unknown redirecting reason '%s', value unchanged\n", val);
		} else {
			redirecting.reason = reason;
			set_it(chan, &redirecting, NULL);
		}
	} else if (member.argc == 1 && !strcasecmp("count", member.argv[0])) {
		val = ast_strdupa(value);
		ast_trim_blanks(val);

		if (('0' <= val[0]) && (val[0] <= '9')) {
			redirecting.count = atoi(val);
			set_it(chan, &redirecting, NULL);
		} else {
			ast_log(LOG_ERROR, "Unknown redirecting count '%s', value unchanged\n", val);
		}
	} else {
		ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
	}

	return 0;
}

static struct ast_custom_function callerid_function = {
	.name = "CALLERID",
	.read = callerid_read,
	.read_max = 256,
	.write = callerid_write,
};

static struct ast_custom_function callerpres_function = {
	.name = "CALLERPRES",
	.read = callerpres_read,
	.read_max = 50,
	.write = callerpres_write,
};

static struct ast_custom_function connectedline_function = {
	.name = "CONNECTEDLINE",
	.read = connectedline_read,
	.write = connectedline_write,
};

static struct ast_custom_function redirecting_function = {
	.name = "REDIRECTING",
	.read = redirecting_read,
	.write = redirecting_write,
};

/*!
 * \internal
 * \brief Unload the function module
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int unload_module(void)
{
	int res;

	res = ast_custom_function_unregister(&callerpres_function);
	res |= ast_custom_function_unregister(&callerid_function);
	res |= ast_custom_function_unregister(&connectedline_function);
	res |= ast_custom_function_unregister(&redirecting_function);
	return res;
}

/*!
 * \internal
 * \brief Load and initialize the function module.
 *
 * \retval AST_MODULE_LOAD_SUCCESS on success.
 * \retval AST_MODULE_LOAD_DECLINE on error.
 */
static int load_module(void)
{
	int res;

	res = ast_custom_function_register(&callerpres_function);
	res |= ast_custom_function_register(&callerid_function);
	res |= ast_custom_function_register(&connectedline_function);
	res |= ast_custom_function_register(&redirecting_function);
	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
}

/* Do not wrap the following line. */
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Party ID related dialplan functions (Caller-ID, Connected-line, Redirecting)");