use the zone and options set by the arg parsing macros
[asterisk/asterisk.git] / funcs / func_enum.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006
5  *
6  * Mark Spencer <markster@digium.com>
7  * Oleksiy Krivoshey <oleksiyk@gmail.com>
8  * Russell Bryant <russelb@clemson.edu>
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief ENUM Functions
24  *
25  * \author Mark Spencer <markster@digium.com>
26  * \author Oleksiy Krivoshey <oleksiyk@gmail.com>
27  * \author Russell Bryant <russelb@clemson.edu>
28  *
29  * \arg See also AstENUM
30  */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include "asterisk/module.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/file.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/options.h"
48 #include "asterisk/enum.h"
49 #include "asterisk/app.h"
50
51  static char *synopsis = "Syntax: ENUMLOOKUP(number[|Method-type[|options[|record#[|zone-suffix]]]])\n";
52
53 STANDARD_LOCAL_USER;
54
55 LOCAL_USER_DECL;
56
57 static int function_enum(struct ast_channel *chan, char *cmd, char *data,
58                          char *buf, size_t len)
59 {
60         AST_DECLARE_APP_ARGS(args,
61                 AST_APP_ARG(number);
62                 AST_APP_ARG(tech);
63                 AST_APP_ARG(options);
64                 AST_APP_ARG(record);
65                 AST_APP_ARG(zone);
66         );
67         int res = 0;
68         char tech[80];
69         char dest[80] = "";
70         struct localuser *u;
71         char *s, *p;
72
73         buf[0] = '\0';
74
75         if (ast_strlen_zero(data)) {
76                 ast_log(LOG_WARNING, synopsis);
77                 return -1;
78         }
79
80         AST_STANDARD_APP_ARGS(args, data);
81
82         if (args.argc < 1) {
83                 ast_log(LOG_WARNING, synopsis);
84                 return -1;
85         }
86
87         ast_copy_string(tech, args.tech ? args.tech : "sip", sizeof(tech));
88
89         if (!args.zone)
90                 args.zone = "e164.arpa";
91
92         if (!args.options)
93                 args.options = "1";
94
95         /* strip any '-' signs from number */
96         for (p = args.number, s = p; *s; *s++) {
97                 if (*s != '-')
98                         *p++ = *s;
99         }
100         *p = '\0';
101
102         LOCAL_USER_ACF_ADD(u);
103
104         res = ast_get_enum(chan, p, dest, sizeof(dest), tech, sizeof(tech), args.zone,
105                            args.options);
106
107         LOCAL_USER_REMOVE(u);
108
109         p = strchr(dest, ':');
110         if (p && strcasecmp(tech, "ALL"))
111                 ast_copy_string(buf, p + 1, len);
112         else
113                 ast_copy_string(buf, dest, len);
114
115         return 0;
116 }
117
118 static struct ast_custom_function enum_function = {
119         .name = "ENUMLOOKUP",
120         .synopsis =
121                 "ENUMLOOKUP allows for general or specific querying of NAPTR records"
122                 " or counts of NAPTR types for ENUM or ENUM-like DNS pointers",
123         .syntax =
124                 "ENUMLOOKUP(number[|Method-type[|options[|record#[|zone-suffix]]]])",
125         .desc =
126                 "Option 'c' returns an integer count of the number of NAPTRs of a certain RR type.\n"
127                 "Combination of 'c' and Method-type of 'ALL' will return a count of all NAPTRs for the record.\n"
128                 "Defaults are: Method-type=sip, no options, record=1, zone-suffix=e164.arpa\n\n"
129                 "For more information, see README.enum",
130         .read = function_enum,
131 };
132
133 static int function_txtcidname(struct ast_channel *chan, char *cmd,
134                                char *data, char *buf, size_t len)
135 {
136         int res;
137         char tech[80];
138         char txt[256] = "";
139         char dest[80];
140         struct localuser *u;
141
142         buf[0] = '\0';
143
144         LOCAL_USER_ACF_ADD(u);
145
146         if (ast_strlen_zero(data)) {
147                 ast_log(LOG_WARNING, "TXTCIDNAME requires an argument (number)\n");
148                 LOCAL_USER_REMOVE(u);
149                 return -1;
150         }
151
152         res = ast_get_txt(chan, data, dest, sizeof(dest), tech, sizeof(tech), txt,
153                           sizeof(txt));
154
155         if (!ast_strlen_zero(txt))
156                 ast_copy_string(buf, txt, len);
157
158         LOCAL_USER_REMOVE(u);
159
160         return 0;
161 }
162
163 static struct ast_custom_function txtcidname_function = {
164         .name = "TXTCIDNAME",
165         .synopsis = "TXTCIDNAME looks up a caller name via DNS",
166         .syntax = "TXTCIDNAME(<number>)",
167         .desc =
168                 "This function looks up the given phone number in DNS to retrieve\n"
169                 "the caller id name.  The result will either be blank or be the value\n"
170                 "found in the TXT record in DNS.\n",
171         .read = function_txtcidname,
172 };
173
174 static char *tdesc = "ENUM related dialplan functions";
175
176 int unload_module(void)
177 {
178         int res = 0;
179
180         res |= ast_custom_function_unregister(&enum_function);
181         res |= ast_custom_function_unregister(&txtcidname_function);
182
183         STANDARD_HANGUP_LOCALUSERS;
184
185         return res;
186 }
187
188 int load_module(void)
189 {
190         int res = 0;
191
192         res |= ast_custom_function_register(&enum_function);
193         res |= ast_custom_function_register(&txtcidname_function);
194
195         return res;
196 }
197
198 char *description(void)
199 {
200         return tdesc;
201 }
202
203 int usecount(void)
204 {
205         int res;
206
207         STANDARD_USECOUNT(res);
208
209         return res;
210 }
211
212 char *key()
213 {
214         return ASTERISK_GPL_KEY;
215 }