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
26 #include <sys/types.h>
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include "asterisk/module.h"
32 #include "asterisk/channel.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/app.h"
37 #include "asterisk/options.h"
38 #include "asterisk/callerid.h"
40 static int callerid_read(struct ast_channel *chan, char *cmd, char *data,
41 char *buf, size_t len)
45 if (strchr(opt, '|')) {
46 char name[80], num[80];
48 data = strsep(&opt, "|");
49 ast_callerid_split(opt, name, sizeof(name), num, sizeof(num));
51 if (!strncasecmp("all", data, 3)) {
52 snprintf(buf, len, "\"%s\" <%s>", name, num);
53 } else if (!strncasecmp("name", data, 4)) {
54 ast_copy_string(buf, name, len);
55 } else if (!strncasecmp("num", data, 3) ||
56 !strncasecmp("number", data, 6)) {
58 ast_copy_string(buf, num, len);
60 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
63 if (!strncasecmp("all", data, 3)) {
64 snprintf(buf, len, "\"%s\" <%s>",
65 chan->cid.cid_name ? chan->cid.cid_name : "",
66 chan->cid.cid_num ? chan->cid.cid_num : "");
67 } else if (!strncasecmp("name", data, 4)) {
68 if (chan->cid.cid_name) {
69 ast_copy_string(buf, chan->cid.cid_name, len);
71 } else if (!strncasecmp("num", data, 3)
72 || !strncasecmp("number", data, 6)) {
73 if (chan->cid.cid_num) {
74 ast_copy_string(buf, chan->cid.cid_num, len);
76 } else if (!strncasecmp("ani", data, 3)) {
77 if (chan->cid.cid_ani) {
78 ast_copy_string(buf, chan->cid.cid_ani, len);
80 } else if (!strncasecmp("dnid", data, 4)) {
81 if (chan->cid.cid_dnid) {
82 ast_copy_string(buf, chan->cid.cid_dnid, len);
84 } else if (!strncasecmp("rdnis", data, 5)) {
85 if (chan->cid.cid_rdnis) {
86 ast_copy_string(buf, chan->cid.cid_rdnis, len);
89 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
96 static int callerid_write(struct ast_channel *chan, char *cmd, char *data,
102 if (!strncasecmp("all", data, 3)) {
106 if (!ast_callerid_split(value, name, sizeof(name), num, sizeof(num)))
107 ast_set_callerid(chan, num, name, num);
108 } else if (!strncasecmp("name", data, 4)) {
109 ast_set_callerid(chan, NULL, value, NULL);
110 } else if (!strncasecmp("num", data, 3) ||
111 !strncasecmp("number", data, 6)) {
112 ast_set_callerid(chan, value, NULL, NULL);
113 } else if (!strncasecmp("ani", data, 3)) {
114 ast_set_callerid(chan, NULL, NULL, value);
115 } else if (!strncasecmp("dnid", data, 4)) {
116 /* do we need to lock chan here? */
117 if (chan->cid.cid_dnid)
118 free(chan->cid.cid_dnid);
119 chan->cid.cid_dnid = ast_strlen_zero(value) ? NULL : strdup(value);
120 } else if (!strncasecmp("rdnis", data, 5)) {
121 /* do we need to lock chan here? */
122 if (chan->cid.cid_rdnis)
123 free(chan->cid.cid_rdnis);
124 chan->cid.cid_rdnis = ast_strlen_zero(value) ? NULL : strdup(value);
126 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
132 static struct ast_custom_function callerid_function = {
134 .synopsis = "Gets or sets Caller*ID data on the channel.",
135 .syntax = "CALLERID(datatype[,<optional-CID>])",
137 "Gets or sets Caller*ID data on the channel. The allowable datatypes\n"
138 "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n"
139 "Uses channel callerid by default or optional callerid, if specified.\n",
140 .read = callerid_read,
141 .write = callerid_write,
144 static char *tdesc = "Caller ID related dialplan function";
146 int unload_module(void)
148 return ast_custom_function_unregister(&callerid_function);
151 int load_module(void)
153 return ast_custom_function_register(&callerid_function);
156 char *description(void)
168 return ASTERISK_GPL_KEY;