(closes issue #13493)
[asterisk-bristuff.git] / funcs / func_callerid.c
blobef3c447bb521b587dba6c27ff28ef145cf2ff0b0
1 /*
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.
17 /*! \file
19 * \brief Caller ID related dialplan functions
23 #include "asterisk.h"
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
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)
44 char *opt = data;
46 if (!chan)
47 return -1;
49 if (strchr(opt, '|')) {
50 char name[80], num[80];
52 data = strsep(&opt, "|");
53 ast_callerid_split(opt, name, sizeof(name), num, sizeof(num));
55 if (!strncasecmp("all", data, 3)) {
56 snprintf(buf, len, "\"%s\" <%s>", name, num);
57 } else if (!strncasecmp("name", data, 4)) {
58 ast_copy_string(buf, name, len);
59 } else if (!strncasecmp("num", data, 3) ||
60 !strncasecmp("number", data, 6)) {
62 ast_copy_string(buf, num, len);
63 } else {
64 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
66 } else {
67 ast_channel_lock(chan);
69 if (!strncasecmp("all", data, 3)) {
70 snprintf(buf, len, "\"%s\" <%s>",
71 S_OR(chan->cid.cid_name, ""),
72 S_OR(chan->cid.cid_num, ""));
73 } else if (!strncasecmp("name", data, 4)) {
74 if (chan->cid.cid_name) {
75 ast_copy_string(buf, chan->cid.cid_name, len);
77 } else if (!strncasecmp("num", data, 3)
78 || !strncasecmp("number", data, 6)) {
79 if (chan->cid.cid_num) {
80 ast_copy_string(buf, chan->cid.cid_num, len);
82 } else if (!strncasecmp("ani", data, 3)) {
83 if (chan->cid.cid_ani) {
84 ast_copy_string(buf, chan->cid.cid_ani, len);
86 } else if (!strncasecmp("dnid", data, 4)) {
87 if (chan->cid.cid_dnid) {
88 ast_copy_string(buf, chan->cid.cid_dnid, len);
90 } else if (!strncasecmp("rdnis", data, 5)) {
91 if (chan->cid.cid_rdnis) {
92 ast_copy_string(buf, chan->cid.cid_rdnis, len);
94 } else {
95 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
98 ast_channel_unlock(chan);
101 return 0;
104 static int callerid_write(struct ast_channel *chan, char *cmd, char *data,
105 const char *value)
107 if (!value || !chan)
108 return -1;
110 if (!strncasecmp("all", data, 3)) {
111 char name[256];
112 char num[256];
114 if (!ast_callerid_split(value, name, sizeof(name), num, sizeof(num)))
115 ast_set_callerid(chan, num, name, num);
116 } else if (!strncasecmp("name", data, 4)) {
117 ast_set_callerid(chan, NULL, value, NULL);
118 } else if (!strncasecmp("num", data, 3) ||
119 !strncasecmp("number", data, 6)) {
120 ast_set_callerid(chan, value, NULL, NULL);
121 } else if (!strncasecmp("ani", data, 3)) {
122 ast_set_callerid(chan, NULL, NULL, value);
123 } else if (!strncasecmp("dnid", data, 4)) {
124 ast_channel_lock(chan);
125 if (chan->cid.cid_dnid)
126 free(chan->cid.cid_dnid);
127 chan->cid.cid_dnid = ast_strdup(value);
128 ast_channel_unlock(chan);
129 } else if (!strncasecmp("rdnis", data, 5)) {
130 ast_channel_lock(chan);
131 if (chan->cid.cid_rdnis)
132 free(chan->cid.cid_rdnis);
133 chan->cid.cid_rdnis = ast_strdup(value);
134 ast_channel_unlock(chan);
135 } else {
136 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
139 return 0;
142 static struct ast_custom_function callerid_function = {
143 .name = "CALLERID",
144 .synopsis = "Gets or sets Caller*ID data on the channel.",
145 .syntax = "CALLERID(datatype[,<optional-CID>])",
146 .desc =
147 "Gets or sets Caller*ID data on the channel. The allowable datatypes\n"
148 "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n"
149 "Uses channel callerid by default or optional callerid, if specified.\n",
150 .read = callerid_read,
151 .write = callerid_write,
154 static int unload_module(void)
156 return ast_custom_function_unregister(&callerid_function);
159 static int load_module(void)
161 return ast_custom_function_register(&callerid_function);
164 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Caller ID related dialplan function");