major dialplan functions update
[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 <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include "asterisk.h"
29
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"
39
40 static int callerid_read(struct ast_channel *chan, char *cmd, char *data,
41                          char *buf, size_t len)
42 {
43         char *opt = data;
44
45         if (strchr(opt, '|')) {
46                 char name[80], num[80];
47
48                 data = strsep(&opt, "|");
49                 ast_callerid_split(opt, name, sizeof(name), num, sizeof(num));
50
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)) {
57
58                         ast_copy_string(buf, num, len);
59                 } else {
60                         ast_log(LOG_ERROR, "Unknown callerid data type.\n");
61                 }
62         } else {
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);
70                         }
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);
75                         }
76                 } else if (!strncasecmp("ani", data, 3)) {
77                         if (chan->cid.cid_ani) {
78                                 ast_copy_string(buf, chan->cid.cid_ani, len);
79                         }
80                 } else if (!strncasecmp("dnid", data, 4)) {
81                         if (chan->cid.cid_dnid) {
82                                 ast_copy_string(buf, chan->cid.cid_dnid, len);
83                         }
84                 } else if (!strncasecmp("rdnis", data, 5)) {
85                         if (chan->cid.cid_rdnis) {
86                                 ast_copy_string(buf, chan->cid.cid_rdnis, len);
87                         }
88                 } else {
89                         ast_log(LOG_ERROR, "Unknown callerid data type.\n");
90                 }
91         }
92
93         return 0;
94 }
95
96 static int callerid_write(struct ast_channel *chan, char *cmd, char *data,
97                           const char *value)
98 {
99         if (!value)
100                 return -1;
101
102         if (!strncasecmp("all", data, 3)) {
103                 char name[256];
104                 char num[256];
105
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);
125         } else {
126                 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
127         }
128
129         return 0;
130 }
131
132 static struct ast_custom_function callerid_function = {
133         .name = "CALLERID",
134         .synopsis = "Gets or sets Caller*ID data on the channel.",
135         .syntax = "CALLERID(datatype[,<optional-CID>])",
136         .desc =
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,
142 };
143
144 static char *tdesc = "Caller ID related dialplan function";
145
146 int unload_module(void)
147 {
148         return ast_custom_function_unregister(&callerid_function);
149 }
150
151 int load_module(void)
152 {
153         return ast_custom_function_register(&callerid_function);
154 }
155
156 char *description(void)
157 {
158         return tdesc;
159 }
160
161 int usecount(void)
162 {
163         return 0;
164 }
165
166 char *key()
167 {
168         return ASTERISK_GPL_KEY;
169 }