c907d96a9e8b8cfed119ba11ec184d94fcce4126
[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 #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 char *callerid_read(struct ast_channel *chan, char *cmd, char *data, 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) || !strncasecmp("number", data, 6)) {
56                         ast_copy_string(buf, num, len);
57                 } else {
58                         ast_log(LOG_ERROR, "Unknown callerid data type.\n");
59                 }
60         } else {
61                 if (!strncasecmp("all", data, 3)) {
62                         snprintf(buf, len, "\"%s\" <%s>", chan->cid.cid_name ? chan->cid.cid_name : "", chan->cid.cid_num ? chan->cid.cid_num : "");    
63                 } else if (!strncasecmp("name", data, 4)) {
64                         if (chan->cid.cid_name) {
65                                 ast_copy_string(buf, chan->cid.cid_name, len);
66                         }
67                 } else if (!strncasecmp("num", data, 3) || !strncasecmp("number", data, 6)) {
68                         if (chan->cid.cid_num) {
69                                 ast_copy_string(buf, chan->cid.cid_num, len);
70                         }
71                 } else if (!strncasecmp("ani", data, 3)) {
72                         if (chan->cid.cid_ani) {
73                                 ast_copy_string(buf, chan->cid.cid_ani, len);
74                         }
75                 } else if (!strncasecmp("dnid", data, 4)) {
76                         if (chan->cid.cid_dnid) {
77                                 ast_copy_string(buf, chan->cid.cid_dnid, len);
78                         }
79                 } else if (!strncasecmp("rdnis", data, 5)) {
80                         if (chan->cid.cid_rdnis) {
81                                 ast_copy_string(buf, chan->cid.cid_rdnis, len);
82                         }
83                 } else {
84                         ast_log(LOG_ERROR, "Unknown callerid data type.\n");
85                 }
86         }
87         return buf;
88 }
89
90 static void callerid_write(struct ast_channel *chan, char *cmd, char *data, const char *value) 
91 {
92         if (!value)
93                 return;
94         
95         if (!strncasecmp("all", data, 3)) {
96                 char name[256];
97                 char num[256];
98                 if (!ast_callerid_split(value, name, sizeof(name), num, sizeof(num)))
99                         ast_set_callerid(chan, num, name, num); 
100         } else if (!strncasecmp("name", data, 4)) {
101                 ast_set_callerid(chan, NULL, value, NULL);
102         } else if (!strncasecmp("num", data, 3) || !strncasecmp("number", data, 6)) {
103                 ast_set_callerid(chan, value, NULL, NULL);
104         } else if (!strncasecmp("ani", data, 3)) {
105                 ast_set_callerid(chan, NULL, NULL, value);
106         } else if (!strncasecmp("dnid", data, 4)) {
107                 /* do we need to lock chan here? */
108                 if (chan->cid.cid_dnid)
109                         free(chan->cid.cid_dnid);
110                 chan->cid.cid_dnid = ast_strlen_zero(value) ? NULL : strdup(value);
111         } else if (!strncasecmp("rdnis", data, 5)) {
112                 /* do we need to lock chan here? */
113                 if (chan->cid.cid_rdnis)
114                         free(chan->cid.cid_rdnis);
115                 chan->cid.cid_rdnis = ast_strlen_zero(value) ? NULL : strdup(value);
116         } else {
117                 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
118         }
119 }
120
121 static struct ast_custom_function callerid_function = {
122         .name = "CALLERID",
123         .synopsis = "Gets or sets Caller*ID data on the channel.",
124         .syntax = "CALLERID(datatype[,<optional-CID>])",
125         .desc = "Gets or sets Caller*ID data on the channel.  The allowable datatypes\n"
126         "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n"
127         "Uses channel callerid by default or optional callerid, if specified.\n",
128         .read = callerid_read,
129         .write = callerid_write,
130 };
131
132 static char *tdesc = "Caller ID related dialplan function";
133
134 int unload_module(void)
135 {
136         return ast_custom_function_unregister(&callerid_function);
137 }
138
139 int load_module(void)
140 {
141         return ast_custom_function_register(&callerid_function);
142 }
143
144 char *description(void)
145 {
146         return tdesc;
147 }
148
149 int usecount(void)
150 {
151         return 0;
152 }
153
154 char *key()
155 {
156         return ASTERISK_GPL_KEY;
157 }
158
159 /*
160 Local Variables:
161 mode: C
162 c-file-style: "linux"
163 indent-tabs-mode: nil
164 End:
165 */