extensive ENUM support update, including ENUMLOOKUP() dialplan function (issue #5201...
[asterisk/asterisk.git] / apps / app_enumlookup.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Enumlookup - lookup entry in ENUM
5  *
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <ctype.h>
19
20 #include "asterisk.h"
21
22 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
23
24 #include "asterisk/lock.h"
25 #include "asterisk/file.h"
26 #include "asterisk/logger.h"
27 #include "asterisk/channel.h"
28 #include "asterisk/pbx.h"
29 #include "asterisk/options.h"
30 #include "asterisk/config.h"
31 #include "asterisk/module.h"
32 #include "asterisk/enum.h"
33 #include "asterisk/utils.h"
34
35 static char *tdesc = "ENUM Lookup";
36
37 static char *app = "EnumLookup";
38
39 static char *synopsis = "Lookup number in ENUM";
40
41 static char *descrip =
42 "  EnumLookup(exten):  Looks up an extension via ENUM and sets\n"
43 "the variable 'ENUM'. For VoIP URIs this variable will \n"
44 "look like 'TECHNOLOGY/URI' with the appropriate technology.\n"
45 "Returns -1 on hangup, or 0 on completion\n"
46 "Currently, the enumservices SIP, H323, IAX, IAX2 and TEL are recognized. \n"
47 "\nReturns status in the ENUMSTATUS channel variable:\n"
48 "    ERROR      Failed to do a lookup\n"
49 "    <tech>     Technology of the successful lookup: SIP, H323, IAX, IAX2 or TEL\n"
50 "    BADURI     Got URI Asterisk does not understand.\n"
51 "\nOld, depreciated, behaviour:\n"
52 "\nA SIP, H323, IAX or IAX2 entry will result in normal priority handling, \n"
53 "whereas a TEL entry will increase the priority by 51 (if existing).\n"
54 "If the lookup was *not* successful and there exists a priority n + 101,\n"
55 "then that priority will be taken next.\n" ;
56
57 #define ENUM_CONFIG "enum.conf"
58
59 static char h323driver[80] = "";
60 #define H323DRIVERDEFAULT "H323"
61
62 STANDARD_LOCAL_USER;
63
64 LOCAL_USER_DECL;
65
66 /*--- enumlookup_exec: Look up number in ENUM and return result */
67 static int enumlookup_exec(struct ast_channel *chan, void *data)
68 {
69         int res=0;
70         char tech[80];
71         char dest[80];
72         char tmp[256];
73         char *c,*t;
74
75        tech[0] = '\0';
76
77         struct localuser *u;
78
79         if (!data || ast_strlen_zero(data)) {
80                 ast_log(LOG_WARNING, "EnumLookup requires an argument (extension)\n");
81                 res = 0;
82         }
83         LOCAL_USER_ADD(u);
84         if (!res) {
85                res = ast_get_enum(chan, data, dest, sizeof(dest), tech, sizeof(tech), NULL, NULL);
86                 printf("ENUM got '%d'\n", res);
87         }
88         LOCAL_USER_REMOVE(u);
89         if (!res) {     /* Failed to do a lookup */
90                 /* Look for a "busy" place */
91                 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
92                 pbx_builtin_setvar_helper(chan, "ENUMSTATUS", "ERROR");
93                 return 0;
94         }
95         pbx_builtin_setvar_helper(chan, "ENUMSTATUS", tech);
96         /* Parse it out */
97         if (res > 0) {
98                 if (!strcasecmp(tech, "SIP")) {
99                         c = dest;
100                         if (!strncmp(c, "sip:", 4))
101                                 c += 4;
102                         snprintf(tmp, sizeof(tmp), "SIP/%s", c);
103                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
104                 } else if (!strcasecmp(tech, "h323")) {
105                         c = dest;
106                         if (!strncmp(c, "h323:", 5))
107                                 c += 5;
108                         snprintf(tmp, sizeof(tmp), "%s/%s", h323driver, c);
109 /* do a s!;.*!! on the H323 URI */
110                         t = strchr(c,';');
111                        if (t)
112                                 *t = 0;
113                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
114                 } else if (!strcasecmp(tech, "iax")) {
115                         c = dest;
116                         if (!strncmp(c, "iax:", 4))
117                                 c += 4;
118                         snprintf(tmp, sizeof(tmp), "IAX/%s", c);
119                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
120                 } else if (!strcasecmp(tech, "iax2")) {
121                         c = dest;
122                         if (!strncmp(c, "iax2:", 5))
123                                 c += 5;
124                         snprintf(tmp, sizeof(tmp), "IAX2/%s", c);
125                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
126                 } else if (!strcasecmp(tech, "tel")) {
127                         c = dest;
128                         if (!strncmp(c, "tel:", 4))
129                                 c += 4;
130
131                         if (c[0] != '+') {
132                                 ast_log(LOG_NOTICE, "tel: uri must start with a \"+\" (got '%s')\n", c);
133                                 res = 0;
134                         } else {
135 /* now copy over the number, skipping all non-digits and stop at ; or NULL */
136                                t = tmp;
137                                 while( *c && (*c != ';') && (t - tmp < (sizeof(tmp) - 1))) {
138                                         if (isdigit(*c))
139                                                 *t++ = *c;
140                                         c++;
141                                 }
142                                 *t = 0;
143                                 pbx_builtin_setvar_helper(chan, "ENUM", tmp);
144                                 ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp);
145                                 if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 51))
146                                         res = 0;
147                         }
148                 } else if (!ast_strlen_zero(tech)) {
149                         ast_log(LOG_NOTICE, "Don't know how to handle technology '%s'\n", tech);
150                         pbx_builtin_setvar_helper(chan, "ENUMSTATUS", "BADURI");
151                         res = 0;
152                 }
153         }
154         return 0;
155 }
156
157 /*--- load_config: Load enum.conf and find out how to handle H.323 */
158 static int load_config(void)
159 {
160         struct ast_config *cfg;
161         char *s;
162
163         cfg = ast_config_load(ENUM_CONFIG);
164         if (cfg) {
165                 if (!(s=ast_variable_retrieve(cfg, "general", "h323driver"))) {
166                         strncpy(h323driver, H323DRIVERDEFAULT, sizeof(h323driver) - 1);
167                 } else {
168                         strncpy(h323driver, s, sizeof(h323driver) - 1);
169                 }
170                 ast_config_destroy(cfg);
171                 return 0;
172         }
173         ast_log(LOG_NOTICE, "No ENUM Config file, using defaults\n");
174         return 0;
175 }
176
177
178 /*--- unload_module: Unload this application from PBX */
179 int unload_module(void)
180 {
181         STANDARD_HANGUP_LOCALUSERS;
182         return ast_unregister_application(app);
183 }
184
185 /*--- load_module: Load this application into PBX */
186 int load_module(void)
187 {
188         int res;
189         res = ast_register_application(app, enumlookup_exec, synopsis, descrip);
190         if (res)
191                 return(res);
192         if ((res=load_config())) {
193                 return(res);
194         }
195         return(0);
196 }
197
198 /*--- reload: Reload configuration file */
199 int reload(void)
200 {
201         return(load_config());
202 }
203
204
205 /*--- description: Describe module */
206 char *description(void)
207 {
208         return tdesc;
209 }
210
211 int usecount(void)
212 {
213         int res;
214         STANDARD_USECOUNT(res);
215         return res;
216 }
217
218 char *key()
219 {
220         return ASTERISK_GPL_KEY;
221 }
222