make app_queue 1.2 jump compliant (issue #5580)
[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/options.h"
47
48 static char *tdesc = "ENUM Lookup";
49
50 static char *app = "EnumLookup";
51
52 static char *synopsis = "Lookup number in ENUM";
53
54 static char *descrip =
55 "  EnumLookup(exten[|option]):  Looks up an extension via ENUM and sets\n"
56 "the variable 'ENUM'. For VoIP URIs this variable will \n"
57 "look like 'TECHNOLOGY/URI' with the appropriate technology.\n"
58 "Currently, the enumservices SIP, H323, IAX, IAX2 and TEL are recognized. \n"
59 "\nReturns status in the ENUMSTATUS channel variable:\n"
60 "    ERROR      Failed to do a lookup\n"
61 "    <tech>     Technology of the successful lookup: SIP, H323, IAX, IAX2 or TEL\n"
62 "    BADURI     Got URI Asterisk does not understand.\n"
63 "  The option string may contain zero or the following character:\n"
64 "       'j' -- jump to +101 priority if the lookup isn't successful.\n"
65 "              and jump to +51 priority on a TEL entry.\n";
66
67 #define ENUM_CONFIG "enum.conf"
68
69 static char h323driver[80] = "";
70 #define H323DRIVERDEFAULT "H323"
71
72 STANDARD_LOCAL_USER;
73
74 LOCAL_USER_DECL;
75
76 /*--- enumlookup_exec: Look up number in ENUM and return result */
77 static int enumlookup_exec(struct ast_channel *chan, void *data)
78 {
79         int res=0,priority_jump=0;
80         char tech[80];
81         char dest[80];
82         char tmp[256];
83         char *c,*t,*d,*o = NULL;
84         static int dep_warning=0;
85         struct localuser *u;
86
87         if (ast_strlen_zero(data)) {
88                 ast_log(LOG_WARNING, "EnumLookup requires an argument (extension)\n");
89                 return -1;
90         }
91                 
92         if (!dep_warning) {
93                 ast_log(LOG_WARNING, "The application EnumLookup is deprecated.  Please use the ENUMLOOKUP() function instead.\n");
94                 dep_warning = 1;
95         }
96
97         LOCAL_USER_ADD(u);
98
99         tech[0] = '\0';
100
101         if (strchr(data, '|')) {
102                 d = strsep(data, "|");
103                 o = strsep(data, "\0");
104         } else
105                 d = strsep(data, "\0"); 
106                 
107         if (o) {
108                 if (strchr(o, 'j'))
109                         priority_jump = 1;
110         }
111
112         res = ast_get_enum(chan, d, dest, sizeof(dest), tech, sizeof(tech), NULL, NULL);
113         
114         if (!res) {     /* Failed to do a lookup */
115                 if (priority_jump || option_priority_jumping) {
116                         /* Look for a "busy" place */
117                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
118                 }
119                 pbx_builtin_setvar_helper(chan, "ENUMSTATUS", "ERROR");
120                 LOCAL_USER_REMOVE(u);
121                 return 0;
122         }
123         pbx_builtin_setvar_helper(chan, "ENUMSTATUS", tech);
124         /* Parse it out */
125         if (res > 0) {
126                 if (!strcasecmp(tech, "SIP")) {
127                         c = dest;
128                         if (!strncmp(c, "sip:", 4))
129                                 c += 4;
130                         snprintf(tmp, sizeof(tmp), "SIP/%s", c);
131                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
132                 } else if (!strcasecmp(tech, "h323")) {
133                         c = dest;
134                         if (!strncmp(c, "h323:", 5))
135                                 c += 5;
136                         snprintf(tmp, sizeof(tmp), "%s/%s", h323driver, c);
137 /* do a s!;.*!! on the H323 URI */
138                         t = strchr(c,';');
139                        if (t)
140                                 *t = 0;
141                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
142                 } else if (!strcasecmp(tech, "iax")) {
143                         c = dest;
144                         if (!strncmp(c, "iax:", 4))
145                                 c += 4;
146                         snprintf(tmp, sizeof(tmp), "IAX/%s", c);
147                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
148                 } else if (!strcasecmp(tech, "iax2")) {
149                         c = dest;
150                         if (!strncmp(c, "iax2:", 5))
151                                 c += 5;
152                         snprintf(tmp, sizeof(tmp), "IAX2/%s", c);
153                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
154                 } else if (!strcasecmp(tech, "tel")) {
155                         c = dest;
156                         if (!strncmp(c, "tel:", 4))
157                                 c += 4;
158
159                         if (c[0] != '+') {
160                                 ast_log(LOG_NOTICE, "tel: uri must start with a \"+\" (got '%s')\n", c);
161                                 res = 0;
162                         } else {
163 /* now copy over the number, skipping all non-digits and stop at ; or NULL */
164                                t = tmp;
165                                 while( *c && (*c != ';') && (t - tmp < (sizeof(tmp) - 1))) {
166                                         if (isdigit(*c))
167                                                 *t++ = *c;
168                                         c++;
169                                 }
170                                 *t = 0;
171                                 pbx_builtin_setvar_helper(chan, "ENUM", tmp);
172                                 ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp);
173                                 if (priority_jump || option_priority_jumping) {
174                                         if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 51))
175                                                 res = 0;
176                                 }
177                         }
178                 } else if (!ast_strlen_zero(tech)) {
179                         ast_log(LOG_NOTICE, "Don't know how to handle technology '%s'\n", tech);
180                         pbx_builtin_setvar_helper(chan, "ENUMSTATUS", "BADURI");
181                         res = 0;
182                 }
183         }
184
185         LOCAL_USER_REMOVE(u);
186
187         return 0;
188 }
189
190 /*--- load_config: Load enum.conf and find out how to handle H.323 */
191 static int load_config(void)
192 {
193         struct ast_config *cfg;
194         char *s;
195
196         cfg = ast_config_load(ENUM_CONFIG);
197         if (cfg) {
198                 if (!(s=ast_variable_retrieve(cfg, "general", "h323driver"))) {
199                         strncpy(h323driver, H323DRIVERDEFAULT, sizeof(h323driver) - 1);
200                 } else {
201                         strncpy(h323driver, s, sizeof(h323driver) - 1);
202                 }
203                 ast_config_destroy(cfg);
204                 return 0;
205         }
206         ast_log(LOG_NOTICE, "No ENUM Config file, using defaults\n");
207         return 0;
208 }
209
210
211 /*--- unload_module: Unload this application from PBX */
212 int unload_module(void)
213 {
214         int res;
215
216         res = ast_unregister_application(app);
217
218         STANDARD_HANGUP_LOCALUSERS;
219
220         return res;
221 }
222
223 /*--- load_module: Load this application into PBX */
224 int load_module(void)
225 {
226         int res;
227         
228         res = ast_register_application(app, enumlookup_exec, synopsis, descrip);
229         
230         if (!res)
231                 res = load_config();
232         
233         return res;
234 }
235
236 /*--- reload: Reload configuration file */
237 int reload(void)
238 {
239         return load_config();
240 }
241
242
243 /*--- description: Describe module */
244 char *description(void)
245 {
246         return tdesc;
247 }
248
249 int usecount(void)
250 {
251         int res;
252         STANDARD_USECOUNT(res);
253         return res;
254 }
255
256 char *key()
257 {
258         return ASTERISK_GPL_KEY;
259 }
260