Let's also include aclocal.m4
[asterisk-bristuff.git] / funcs / func_callerid.c
blob19b8f413f49046df04d69317d4de30d8238f8535
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 int res = -1;
45 char *opt = data;
47 if (!chan)
48 return -1;
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);
58 res = 0;
59 } else if (!strncasecmp("name", data, 4)) {
60 ast_copy_string(buf, name, len);
61 res = 0;
62 } else if (!strncasecmp("num", data, 3) ||
63 !strncasecmp("number", data, 6)) {
65 ast_copy_string(buf, num, len);
66 res = 0;
67 } else {
68 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
70 } else {
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, ""));
77 res = 0;
78 } else if (!strncasecmp("name", data, 4)) {
79 if (chan->cid.cid_name) {
80 ast_copy_string(buf, chan->cid.cid_name, len);
81 res = 0;
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);
87 res = 0;
89 } else if (!strncasecmp("ani", data, 3)) {
90 if (chan->cid.cid_ani) {
91 ast_copy_string(buf, chan->cid.cid_ani, len);
92 res = 0;
94 } else if (!strncasecmp("dnid", data, 4)) {
95 if (chan->cid.cid_dnid) {
96 ast_copy_string(buf, chan->cid.cid_dnid, len);
97 res = 0;
99 } else if (!strncasecmp("rdnis", data, 5)) {
100 if (chan->cid.cid_rdnis) {
101 ast_copy_string(buf, chan->cid.cid_rdnis, len);
102 res = 0;
104 } else {
105 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
108 ast_channel_unlock(chan);
111 return res;
114 static int callerid_write(struct ast_channel *chan, char *cmd, char *data,
115 const char *value)
117 if (!value || !chan)
118 return -1;
120 if (!strncasecmp("all", data, 3)) {
121 char name[256];
122 char num[256];
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);
145 } else {
146 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
149 return 0;
152 static struct ast_custom_function callerid_function = {
153 .name = "CALLERID",
154 .synopsis = "Gets or sets Caller*ID data on the channel.",
155 .syntax = "CALLERID(datatype[,<optional-CID>])",
156 .desc =
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");