2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999-2006, Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief Caller ID related dialplan functions
25 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
30 #include <sys/types.h>
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/app.h"
38 #include "asterisk/options.h"
39 #include "asterisk/callerid.h"
41 static int callerid_read(struct ast_channel
*chan
, char *cmd
, char *data
,
42 char *buf
, size_t len
)
46 /* XXX we are not always clearing the buffer. Is this correct ? */
47 if (strchr(opt
, '|')) {
48 char name
[80], num
[80];
50 data
= strsep(&opt
, "|");
51 ast_callerid_split(opt
, name
, sizeof(name
), num
, sizeof(num
));
53 if (!strncasecmp("all", data
, 3)) {
54 snprintf(buf
, len
, "\"%s\" <%s>", name
, num
);
55 } else if (!strncasecmp("name", data
, 4)) {
56 ast_copy_string(buf
, name
, len
);
57 } else if (!strncasecmp("num", data
, 3) ||
58 !strncasecmp("number", data
, 6)) {
60 ast_copy_string(buf
, num
, len
);
62 ast_log(LOG_ERROR
, "Unknown callerid data type '%s'.\n", data
);
65 if (!strncasecmp("all", data
, 3)) {
66 snprintf(buf
, len
, "\"%s\" <%s>",
67 S_OR(chan
->cid
.cid_name
, ""),
68 S_OR(chan
->cid
.cid_num
, ""));
69 } else if (!strncasecmp("name", data
, 4)) {
70 if (chan
->cid
.cid_name
) {
71 ast_copy_string(buf
, chan
->cid
.cid_name
, len
);
73 } else if (!strncasecmp("num", data
, 3)
74 || !strncasecmp("number", data
, 6)) {
75 if (chan
->cid
.cid_num
) {
76 ast_copy_string(buf
, chan
->cid
.cid_num
, len
);
78 } else if (!strncasecmp("ani", data
, 3)) {
79 if (chan
->cid
.cid_ani
) {
80 ast_copy_string(buf
, chan
->cid
.cid_ani
, len
);
82 } else if (!strncasecmp("dnid", data
, 4)) {
83 if (chan
->cid
.cid_dnid
) {
84 ast_copy_string(buf
, chan
->cid
.cid_dnid
, len
);
86 } else if (!strncasecmp("rdnis", data
, 5)) {
87 if (chan
->cid
.cid_rdnis
) {
88 ast_copy_string(buf
, chan
->cid
.cid_rdnis
, len
);
91 ast_log(LOG_ERROR
, "Unknown callerid data type '%s'.\n", data
);
98 static int callerid_write(struct ast_channel
*chan
, char *cmd
, char *data
,
104 if (!strncasecmp("all", data
, 3)) {
108 if (!ast_callerid_split(value
, name
, sizeof(name
), num
, sizeof(num
)))
109 ast_set_callerid(chan
, num
, name
, num
);
110 } else if (!strncasecmp("name", data
, 4)) {
111 ast_set_callerid(chan
, NULL
, value
, NULL
);
112 } else if (!strncasecmp("num", data
, 3) ||
113 !strncasecmp("number", data
, 6)) {
114 ast_set_callerid(chan
, value
, NULL
, NULL
);
115 } else if (!strncasecmp("ani", data
, 3)) {
116 ast_set_callerid(chan
, NULL
, NULL
, value
);
117 } else if (!strncasecmp("dnid", data
, 4)) {
118 /* do we need to lock chan here? */
119 if (chan
->cid
.cid_dnid
)
120 free(chan
->cid
.cid_dnid
);
121 chan
->cid
.cid_dnid
= ast_strdup(value
);
122 } else if (!strncasecmp("rdnis", data
, 5)) {
123 /* do we need to lock chan here? */
124 if (chan
->cid
.cid_rdnis
)
125 free(chan
->cid
.cid_rdnis
);
126 chan
->cid
.cid_rdnis
= ast_strdup(value
);
128 ast_log(LOG_ERROR
, "Unknown callerid data type '%s'.\n", data
);
134 static struct ast_custom_function callerid_function
= {
136 .synopsis
= "Gets or sets Caller*ID data on the channel.",
137 .syntax
= "CALLERID(datatype[,<optional-CID>])",
139 "Gets or sets Caller*ID data on the channel. The allowable datatypes\n"
140 "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n"
141 "Uses channel callerid by default or optional callerid, if specified.\n",
142 .read
= callerid_read
,
143 .write
= callerid_write
,
146 static int unload_module(void)
148 return ast_custom_function_unregister(&callerid_function
);
151 static int load_module(void)
153 return ast_custom_function_register(&callerid_function
);
156 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Caller ID related dialplan function");