Bug 5327 - new function FILTER and optional argument to CALLERID
[asterisk/asterisk.git] / funcs / func_callerid.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, 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
32 #ifndef BUILTIN_FUNC
33 #include "asterisk/module.h"
34 #endif /* BUILTIN_FUNC */
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/app.h"
40 #include "asterisk/options.h"
41 #include "asterisk/callerid.h"
42
43 static char *callerid_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
44 {
45         char *opt = data;
46
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) || !strncasecmp("number", data, 6)) {
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>", chan->cid.cid_name ? chan->cid.cid_name : "", chan->cid.cid_num ? chan->cid.cid_num : "");    
65                 } else if (!strncasecmp("name", data, 4)) {
66                         if (chan->cid.cid_name) {
67                                 ast_copy_string(buf, chan->cid.cid_name, len);
68                         }
69                 } else if (!strncasecmp("num", data, 3) || !strncasecmp("number", data, 6)) {
70                         if (chan->cid.cid_num) {
71                                 ast_copy_string(buf, chan->cid.cid_num, len);
72                         }
73                 } else if (!strncasecmp("ani", data, 3)) {
74                         if (chan->cid.cid_ani) {
75                                 ast_copy_string(buf, chan->cid.cid_ani, len);
76                         }
77                 } else if (!strncasecmp("dnid", data, 4)) {
78                         if (chan->cid.cid_dnid) {
79                                 ast_copy_string(buf, chan->cid.cid_dnid, len);
80                         }
81                 } else if (!strncasecmp("rdnis", data, 5)) {
82                         if (chan->cid.cid_rdnis) {
83                                 ast_copy_string(buf, chan->cid.cid_rdnis, len);
84                         }
85                 } else {
86                         ast_log(LOG_ERROR, "Unknown callerid data type.\n");
87                 }
88         }
89         return buf;
90 }
91
92 static void callerid_write(struct ast_channel *chan, char *cmd, char *data, const char *value) 
93 {
94         if (!value)
95                 return;
96         
97         if (!strncasecmp("all", data, 3)) {
98                 char name[256];
99                 char num[256];
100                 if (!ast_callerid_split(value, name, sizeof(name), num, sizeof(num)))
101                         ast_set_callerid(chan, num, name, num); 
102         } else if (!strncasecmp("name", data, 4)) {
103                 ast_set_callerid(chan, NULL, value, NULL);
104         } else if (!strncasecmp("num", data, 3) || !strncasecmp("number", data, 6)) {
105                 ast_set_callerid(chan, value, NULL, NULL);
106         } else if (!strncasecmp("ani", data, 3)) {
107                 ast_set_callerid(chan, NULL, NULL, value);
108         } else if (!strncasecmp("dnid", data, 4)) {
109                 /* do we need to lock chan here? */
110                 if (chan->cid.cid_dnid)
111                         free(chan->cid.cid_dnid);
112                 chan->cid.cid_dnid = ast_strlen_zero(value) ? NULL : strdup(value);
113         } else if (!strncasecmp("rdnis", data, 5)) {
114                 /* do we need to lock chan here? */
115                 if (chan->cid.cid_rdnis)
116                         free(chan->cid.cid_rdnis);
117                 chan->cid.cid_rdnis = ast_strlen_zero(value) ? NULL : strdup(value);
118         } else {
119                 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
120         }
121 }
122
123 #ifndef BUILTIN_FUNC
124 static
125 #endif /* BUILTIN_FUNC */
126 struct ast_custom_function callerid_function = {
127         .name = "CALLERID",
128         .synopsis = "Gets or sets Caller*ID data on the channel.",
129         .syntax = "CALLERID(datatype[,<optional-CID>])",
130         .desc = "Gets or sets Caller*ID data on the channel.  The allowable datatypes\n"
131         "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n"
132         "Uses channel callerid by default or optional callerid, if specified.\n",
133         .read = callerid_read,
134         .write = callerid_write,
135 };
136
137 #ifndef BUILTIN_FUNC
138 static char *tdesc = "Caller ID related dialplan function";
139
140 int unload_module(void)
141 {
142         return ast_custom_function_unregister(&callerid_function);
143 }
144
145 int load_module(void)
146 {
147         return ast_custom_function_register(&callerid_function);
148 }
149
150 char *description(void)
151 {
152         return tdesc;
153 }
154
155 int usecount(void)
156 {
157         return 0;
158 }
159
160 char *key()
161 {
162         return ASTERISK_GPL_KEY;
163 }
164 #endif /* BUILTIN_FUNC */
165
166 /*
167 Local Variables:
168 mode: C
169 c-file-style: "linux"
170 indent-tabs-mode: nil
171 End:
172 */