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