6068739d44b6f6867af4681b765e681c1ef92559
[asterisk/asterisk.git] / funcs / func_callerid.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999-2006, Digium, Inc.
5  *
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.
11  *
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.
15  */
16
17 /*! \file
18  *
19  * \brief Caller ID related dialplan functions
20  * 
21  */
22
23 #include "asterisk.h"
24
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/types.h>
31
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"
40
41 static int callerid_read(struct ast_channel *chan, char *cmd, char *data,
42                          char *buf, size_t len)
43 {
44         char *opt = data;
45
46         /* XXX we are not always clearing the buffer. Is this correct ? */
47         if (strchr(opt, '|')) {
48                 char name[80], num[80];
49
50                 data = strsep(&opt, "|");
51                 ast_callerid_split(opt, name, sizeof(name), num, sizeof(num));
52
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)) {
59
60                         ast_copy_string(buf, num, len);
61                 } else {
62                         ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
63                 }
64         } else {
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);
72                         }
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);
77                         }
78                 } else if (!strncasecmp("ani", data, 3)) {
79                         if (chan->cid.cid_ani) {
80                                 ast_copy_string(buf, chan->cid.cid_ani, len);
81                         }
82                 } else if (!strncasecmp("dnid", data, 4)) {
83                         if (chan->cid.cid_dnid) {
84                                 ast_copy_string(buf, chan->cid.cid_dnid, len);
85                         }
86                 } else if (!strncasecmp("rdnis", data, 5)) {
87                         if (chan->cid.cid_rdnis) {
88                                 ast_copy_string(buf, chan->cid.cid_rdnis, len);
89                         }
90                 } else {
91                         ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
92                 }
93         }
94
95         return 0;
96 }
97
98 static int callerid_write(struct ast_channel *chan, char *cmd, char *data,
99                           const char *value)
100 {
101         if (!value)
102                 return -1;
103
104         if (!strncasecmp("all", data, 3)) {
105                 char name[256];
106                 char num[256];
107
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);
127         } else {
128                 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
129         }
130
131         return 0;
132 }
133
134 static struct ast_custom_function callerid_function = {
135         .name = "CALLERID",
136         .synopsis = "Gets or sets Caller*ID data on the channel.",
137         .syntax = "CALLERID(datatype[,<optional-CID>])",
138         .desc =
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,
144 };
145
146 static int unload_module(void)
147 {
148         return ast_custom_function_unregister(&callerid_function);
149 }
150
151 static int load_module(void)
152 {
153         return ast_custom_function_register(&callerid_function);
154 }
155
156 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Caller ID related dialplan function");