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
)
50 if (strchr(opt
, '|')) {
51 char name
[80], num
[80];
53 data
= strsep(&opt
, "|");
54 ast_callerid_split(opt
, name
, sizeof(name
), num
, sizeof(num
));
56 if (!strncasecmp("all", data
, 3)) {
57 snprintf(buf
, len
, "\"%s\" <%s>", name
, num
);
59 } else if (!strncasecmp("name", data
, 4)) {
60 ast_copy_string(buf
, name
, len
);
62 } else if (!strncasecmp("num", data
, 3) ||
63 !strncasecmp("number", data
, 6)) {
65 ast_copy_string(buf
, num
, len
);
68 ast_log(LOG_ERROR
, "Unknown callerid data type '%s'.\n", data
);
71 ast_channel_lock(chan
);
73 if (!strncasecmp("all", data
, 3)) {
74 snprintf(buf
, len
, "\"%s\" <%s>",
75 S_OR(chan
->cid
.cid_name
, ""),
76 S_OR(chan
->cid
.cid_num
, ""));
78 } else if (!strncasecmp("name", data
, 4)) {
79 if (chan
->cid
.cid_name
) {
80 ast_copy_string(buf
, chan
->cid
.cid_name
, len
);
83 } else if (!strncasecmp("num", data
, 3)
84 || !strncasecmp("number", data
, 6)) {
85 if (chan
->cid
.cid_num
) {
86 ast_copy_string(buf
, chan
->cid
.cid_num
, len
);
89 } else if (!strncasecmp("ani", data
, 3)) {
90 if (chan
->cid
.cid_ani
) {
91 ast_copy_string(buf
, chan
->cid
.cid_ani
, len
);
94 } else if (!strncasecmp("dnid", data
, 4)) {
95 if (chan
->cid
.cid_dnid
) {
96 ast_copy_string(buf
, chan
->cid
.cid_dnid
, len
);
99 } else if (!strncasecmp("rdnis", data
, 5)) {
100 if (chan
->cid
.cid_rdnis
) {
101 ast_copy_string(buf
, chan
->cid
.cid_rdnis
, len
);
105 ast_log(LOG_ERROR
, "Unknown callerid data type '%s'.\n", data
);
108 ast_channel_unlock(chan
);
114 static int callerid_write(struct ast_channel
*chan
, char *cmd
, char *data
,
120 if (!strncasecmp("all", data
, 3)) {
124 if (!ast_callerid_split(value
, name
, sizeof(name
), num
, sizeof(num
)))
125 ast_set_callerid(chan
, num
, name
, num
);
126 } else if (!strncasecmp("name", data
, 4)) {
127 ast_set_callerid(chan
, NULL
, value
, NULL
);
128 } else if (!strncasecmp("num", data
, 3) ||
129 !strncasecmp("number", data
, 6)) {
130 ast_set_callerid(chan
, value
, NULL
, NULL
);
131 } else if (!strncasecmp("ani", data
, 3)) {
132 ast_set_callerid(chan
, NULL
, NULL
, value
);
133 } else if (!strncasecmp("dnid", data
, 4)) {
134 ast_channel_lock(chan
);
135 if (chan
->cid
.cid_dnid
)
136 free(chan
->cid
.cid_dnid
);
137 chan
->cid
.cid_dnid
= ast_strdup(value
);
138 ast_channel_unlock(chan
);
139 } else if (!strncasecmp("rdnis", data
, 5)) {
140 ast_channel_lock(chan
);
141 if (chan
->cid
.cid_rdnis
)
142 free(chan
->cid
.cid_rdnis
);
143 chan
->cid
.cid_rdnis
= ast_strdup(value
);
144 ast_channel_unlock(chan
);
146 ast_log(LOG_ERROR
, "Unknown callerid data type '%s'.\n", data
);
152 static struct ast_custom_function callerid_function
= {
154 .synopsis
= "Gets or sets Caller*ID data on the channel.",
155 .syntax
= "CALLERID(datatype[,<optional-CID>])",
157 "Gets or sets Caller*ID data on the channel. The allowable datatypes\n"
158 "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n"
159 "Uses channel callerid by default or optional callerid, if specified.\n",
160 .read
= callerid_read
,
161 .write
= callerid_write
,
164 static int unload_module(void)
166 return ast_custom_function_unregister(&callerid_function
);
169 static int load_module(void)
171 return ast_custom_function_register(&callerid_function
);
174 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Caller ID related dialplan function");