Fix enum lookup (bug 5674)
[asterisk/asterisk.git] / apps / app_enumlookup.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Enumlookup - lookup entry in ENUM
22  *
23  * \ingroup applications
24  */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <ctype.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/options.h"
42 #include "asterisk/config.h"
43 #include "asterisk/module.h"
44 #include "asterisk/enum.h"
45 #include "asterisk/utils.h"
46 #include "asterisk/app.h"
47 #include "asterisk/options.h"
48
49 static char *tdesc = "ENUM Lookup";
50
51 static char *app = "EnumLookup";
52
53 static char *synopsis = "Lookup number in ENUM";
54
55 static char *descrip =
56 "  EnumLookup(exten[|option]):  Looks up an extension via ENUM and sets\n"
57 "the variable 'ENUM'. For VoIP URIs this variable will \n"
58 "look like 'TECHNOLOGY/URI' with the appropriate technology.\n"
59 "Currently, the enumservices SIP, H323, IAX, IAX2 and TEL are recognized. \n"
60 "\nReturns status in the ENUMSTATUS channel variable:\n"
61 "    ERROR      Failed to do a lookup\n"
62 "    <tech>     Technology of the successful lookup: SIP, H323, IAX, IAX2 or TEL\n"
63 "    BADURI     Got URI Asterisk does not understand.\n"
64 "  The option string may contain zero or the following character:\n"
65 "       'j' -- jump to +101 priority if the lookup isn't successful.\n"
66 "              and jump to +51 priority on a TEL entry.\n";
67
68 #define ENUM_CONFIG "enum.conf"
69
70 static char h323driver[80] = "";
71 #define H323DRIVERDEFAULT "H323"
72
73 STANDARD_LOCAL_USER;
74
75 LOCAL_USER_DECL;
76
77 /*--- enumlookup_exec: Look up number in ENUM and return result */
78 static int enumlookup_exec(struct ast_channel *chan, void *data)
79 {
80         int res=0,priority_jump=0;
81         char tech[80];
82         char dest[80];
83         char tmp[256];
84         char *c,*t = NULL;
85         static int dep_warning=0;
86         struct localuser *u;
87         char *parse;
88         AST_DECLARE_APP_ARGS(args,
89                 AST_APP_ARG(d);
90                 AST_APP_ARG(o);
91         );
92
93         if (ast_strlen_zero(data)) {
94                 ast_log(LOG_WARNING, "EnumLookup requires an argument (extension)\n");
95                 return -1;
96         }
97                 
98         if (!dep_warning) {
99                 ast_log(LOG_WARNING, "The application EnumLookup is deprecated.  Please use the ENUMLOOKUP() function instead.\n");
100                 dep_warning = 1;
101         }
102
103         LOCAL_USER_ADD(u);
104
105         parse = ast_strdupa(data);
106         if (!parse) {
107                 ast_log(LOG_ERROR, "Out of memory!\n");
108                 LOCAL_USER_REMOVE(u);
109                 return -1;
110         }
111
112         AST_STANDARD_APP_ARGS(args, parse);
113
114         tech[0] = '\0';
115         dest[0] = '\0';
116
117         if (args.o) {
118                 if (strchr(args.o, 'j'))
119                         priority_jump = 1;
120         }
121
122         res = ast_get_enum(chan, args.d, dest, sizeof(dest), tech, sizeof(tech), NULL, NULL);
123         
124         if (!res) {     /* Failed to do a lookup */
125                 if (priority_jump || option_priority_jumping) {
126                         /* Look for a "busy" place */
127                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
128                 }
129                 pbx_builtin_setvar_helper(chan, "ENUMSTATUS", "ERROR");
130                 LOCAL_USER_REMOVE(u);
131                 return 0;
132         }
133         pbx_builtin_setvar_helper(chan, "ENUMSTATUS", tech);
134         /* Parse it out */
135         if (res > 0) {
136                 if (!strcasecmp(tech, "SIP")) {
137                         c = dest;
138                         if (!strncmp(c, "sip:", 4))
139                                 c += 4;
140                         snprintf(tmp, sizeof(tmp), "SIP/%s", c);
141                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
142                 } else if (!strcasecmp(tech, "h323")) {
143                         c = dest;
144                         if (!strncmp(c, "h323:", 5))
145                                 c += 5;
146                         snprintf(tmp, sizeof(tmp), "%s/%s", h323driver, c);
147 /* do a s!;.*!! on the H323 URI */
148                         t = strchr(c,';');
149                        if (t)
150                                 *t = 0;
151                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
152                 } else if (!strcasecmp(tech, "iax")) {
153                         c = dest;
154                         if (!strncmp(c, "iax:", 4))
155                                 c += 4;
156                         snprintf(tmp, sizeof(tmp), "IAX/%s", c);
157                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
158                 } else if (!strcasecmp(tech, "iax2")) {
159                         c = dest;
160                         if (!strncmp(c, "iax2:", 5))
161                                 c += 5;
162                         snprintf(tmp, sizeof(tmp), "IAX2/%s", c);
163                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
164                 } else if (!strcasecmp(tech, "tel")) {
165                         c = dest;
166                         if (!strncmp(c, "tel:", 4))
167                                 c += 4;
168
169                         if (c[0] != '+') {
170                                 ast_log(LOG_NOTICE, "tel: uri must start with a \"+\" (got '%s')\n", c);
171                                 res = 0;
172                         } else {
173 /* now copy over the number, skipping all non-digits and stop at ; or NULL */
174                                t = tmp;
175                                 while( *c && (*c != ';') && (t - tmp < (sizeof(tmp) - 1))) {
176                                         if (isdigit(*c))
177                                                 *t++ = *c;
178                                         c++;
179                                 }
180                                 *t = 0;
181                                 pbx_builtin_setvar_helper(chan, "ENUM", tmp);
182                                 ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp);
183                                 if (priority_jump || option_priority_jumping) {
184                                         if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 51))
185                                                 res = 0;
186                                 }
187                         }
188                 } else if (!ast_strlen_zero(tech)) {
189                         ast_log(LOG_NOTICE, "Don't know how to handle technology '%s'\n", tech);
190                         pbx_builtin_setvar_helper(chan, "ENUMSTATUS", "BADURI");
191                         res = 0;
192                 }
193         }
194
195         LOCAL_USER_REMOVE(u);
196
197         return 0;
198 }
199
200 /*--- load_config: Load enum.conf and find out how to handle H.323 */
201 static int load_config(void)
202 {
203         struct ast_config *cfg;
204         char *s;
205
206         cfg = ast_config_load(ENUM_CONFIG);
207         if (cfg) {
208                 if (!(s=ast_variable_retrieve(cfg, "general", "h323driver"))) {
209                         strncpy(h323driver, H323DRIVERDEFAULT, sizeof(h323driver) - 1);
210                 } else {
211                         strncpy(h323driver, s, sizeof(h323driver) - 1);
212                 }
213                 ast_config_destroy(cfg);
214                 return 0;
215         }
216         ast_log(LOG_NOTICE, "No ENUM Config file, using defaults\n");
217         return 0;
218 }
219
220
221 /*--- unload_module: Unload this application from PBX */
222 int unload_module(void)
223 {
224         int res;
225
226         res = ast_unregister_application(app);
227
228         STANDARD_HANGUP_LOCALUSERS;
229
230         return res;
231 }
232
233 /*--- load_module: Load this application into PBX */
234 int load_module(void)
235 {
236         int res;
237         
238         res = ast_register_application(app, enumlookup_exec, synopsis, descrip);
239         
240         if (!res)
241                 res = load_config();
242         
243         return res;
244 }
245
246 /*--- reload: Reload configuration file */
247 int reload(void)
248 {
249         return load_config();
250 }
251
252
253 /*--- description: Describe module */
254 char *description(void)
255 {
256         return tdesc;
257 }
258
259 int usecount(void)
260 {
261         int res;
262         STANDARD_USECOUNT(res);
263         return res;
264 }
265
266 char *key()
267 {
268         return ASTERISK_GPL_KEY;
269 }
270