aea6b9c035f2103bfbe9e864f845efbf5762c37c
[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         struct localuser *u;
75
76         if (!data || ast_strlen_zero(data)) {
77                 ast_log(LOG_WARNING, "EnumLookup requires an argument (extension)\n");
78                 res = 0;
79         }
80         LOCAL_USER_ADD(u);
81         if (!res) {
82                 res = ast_get_enum(chan, data, dest, sizeof(dest), tech, sizeof(tech));
83                 printf("ENUM got '%d'\n", res);
84         }
85         LOCAL_USER_REMOVE(u);
86         if (!res) {     /* Failed to do a lookup */
87                 /* Look for a "busy" place */
88                 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
89                 pbx_builtin_setvar_helper(chan, "ENUMSTATUS", "ERROR");
90                 return 0;
91         }
92         pbx_builtin_setvar_helper(chan, "ENUMSTATUS", tech);
93         /* Parse it out */
94         if (res > 0) {
95                 if (!strcasecmp(tech, "SIP")) {
96                         c = dest;
97                         if (!strncmp(c, "sip:", 4))
98                                 c += 4;
99                         snprintf(tmp, sizeof(tmp), "SIP/%s", c);
100                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
101                 } else if (!strcasecmp(tech, "h323")) {
102                         c = dest;
103                         if (!strncmp(c, "h323:", 5))
104                                 c += 5;
105                         snprintf(tmp, sizeof(tmp), "%s/%s", h323driver, c);
106 /* do a s!;.*!! on the H323 URI */
107                         t = strchr(c,';');
108                         if (t) 
109                                 *t = 0;
110                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
111                 } else if (!strcasecmp(tech, "iax")) {
112                         c = dest;
113                         if (!strncmp(c, "iax:", 4))
114                                 c += 4;
115                         snprintf(tmp, sizeof(tmp), "IAX/%s", c);
116                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
117                 } else if (!strcasecmp(tech, "iax2")) {
118                         c = dest;
119                         if (!strncmp(c, "iax2:", 5))
120                                 c += 5;
121                         snprintf(tmp, sizeof(tmp), "IAX2/%s", c);
122                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
123                 } else if (!strcasecmp(tech, "tel")) {
124                         c = dest;
125                         if (!strncmp(c, "tel:", 4))
126                                 c += 4;
127
128                         if (c[0] != '+') {
129                                 ast_log(LOG_NOTICE, "tel: uri must start with a \"+\" (got '%s')\n", c);
130                                 res = 0;
131                         } else {
132 /* now copy over the number, skipping all non-digits and stop at ; or NULL */
133                                 t = tmp;        
134                                 while( *c && (*c != ';') && (t - tmp < (sizeof(tmp) - 1))) {
135                                         if (isdigit(*c))
136                                                 *t++ = *c;
137                                         c++;
138                                 }
139                                 *t = 0;
140                                 pbx_builtin_setvar_helper(chan, "ENUM", tmp);
141                                 ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp);
142                                 if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 51))
143                                         res = 0;
144                         }
145                 } else if (!ast_strlen_zero(tech)) {
146                         ast_log(LOG_NOTICE, "Don't know how to handle technology '%s'\n", tech);
147                         pbx_builtin_setvar_helper(chan, "ENUMSTATUS", "BADURI");
148                         res = 0;
149                 }
150         }
151         return 0;
152 }
153
154 /*--- load_config: Load enum.conf and find out how to handle H.323 */
155 static int load_config(void)
156 {
157         struct ast_config *cfg;
158         char *s;
159
160         cfg = ast_config_load(ENUM_CONFIG);
161         if (cfg) {
162                 if (!(s=ast_variable_retrieve(cfg, "general", "h323driver"))) {
163                         strncpy(h323driver, H323DRIVERDEFAULT, sizeof(h323driver) - 1);
164                 } else {
165                         strncpy(h323driver, s, sizeof(h323driver) - 1);
166                 }
167                 ast_config_destroy(cfg);
168                 return 0;
169         }
170         ast_log(LOG_NOTICE, "No ENUM Config file, using defaults\n");
171         return 0;
172 }
173
174
175 /*--- unload_module: Unload this application from PBX */
176 int unload_module(void)
177 {
178         STANDARD_HANGUP_LOCALUSERS;
179         return ast_unregister_application(app);
180 }
181
182 /*--- load_module: Load this application into PBX */
183 int load_module(void)
184 {
185         int res;
186         res = ast_register_application(app, enumlookup_exec, synopsis, descrip);
187         if (res)
188                 return(res);
189         if ((res=load_config())) {
190                 return(res);
191         }
192         return(0);
193 }
194
195 /*--- reload: Reload configuration file */
196 int reload(void)
197 {
198         return(load_config());
199 }
200
201
202 /*--- description: Describe module */
203 char *description(void)
204 {
205         return tdesc;
206 }
207
208 int usecount(void)
209 {
210         int res;
211         STANDARD_USECOUNT(res);
212         return res;
213 }
214
215 char *key()
216 {
217         return ASTERISK_GPL_KEY;
218 }
219