Merge ENUM fixes (bug #99)
[asterisk/asterisk.git] / apps / app_enumlookup.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Time of day - Report the time of day
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/enum.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <pthread.h>
27
28
29 static char *tdesc = "ENUM Lookup";
30
31 static char *app = "EnumLookup";
32
33 static char *synopsis = "Lookup number in ENUM";
34
35 static char *descrip = 
36 "  EnumLookup(exten):  Looks up an extension via ENUM and sets\n"
37 "the variable 'ENUM'.  Returns -1 on hangup, or 0 on completion\n"
38 "regardless of whether the lookup was successful. Currently, the\n"
39 "enumservices SIP and TEL are recognized. A good SIP entry\n"
40 "will result in normal priority handling, whereas a good TEL entry\n"
41 "will increase the priority by 51 (if existing)\n"
42 "If the lookup was *not* successful and there exists a priority n + 101,\n"
43 "then that priority will be taken next.\n" ;
44
45 STANDARD_LOCAL_USER;
46
47 LOCAL_USER_DECL;
48
49 static int enumlookup_exec(struct ast_channel *chan, void *data)
50 {
51         int res=0;
52         char tech[80];
53         char dest[80];
54         char tmp[256];
55         char *c,*t;
56         struct localuser *u;
57         if (!data || !strlen(data)) {
58                 ast_log(LOG_WARNING, "EnumLookup requires an argument (extension)\n");
59                 res = 1;
60         }
61         LOCAL_USER_ADD(u);
62         if (!res) {
63                 res = ast_get_enum(chan, data, dest, sizeof(dest), tech, sizeof(tech));
64                 printf("ENUM got '%d'\n", res);
65         }
66         LOCAL_USER_REMOVE(u);
67         /* Parse it out */
68         if (res > 0) {
69                 if (!strcasecmp(tech, "SIP")) {
70                         c = dest;
71                         if (!strncmp(c, "sip:", 4))
72                                 c += 4;
73                         snprintf(tmp, sizeof(tmp), "SIP/%s", c);
74                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
75                 } else if (!strcasecmp(tech, "tel")) {
76                         c = dest;
77                         if (!strncmp(c, "tel:", 4))
78                                 c += 4;
79
80                         if (c[0] != '+') {
81                                 ast_log(LOG_NOTICE, "tel: uri must start with a \"+\" (got '%s')\n", c);
82                                 res = 0;
83                         } else {
84 /* now copy over the number, skipping all non-digits and stop at ; or NULL */
85                                 t = tmp;        
86                                 while( *c && (*c != ';') && (t - tmp < (sizeof(tmp) - 1))) {
87                                         if (isdigit(*c))
88                                                 *t++ = *c;
89                                         c++;
90                                 }
91                                 *t = 0;
92                                 pbx_builtin_setvar_helper(chan, "ENUM", tmp);
93                                 ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp);
94                                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 51, chan->callerid))
95                                         chan->priority += 50;
96                                 else
97                                         res = 0;
98                         }
99                 } else if (strlen(tech)) {
100                         ast_log(LOG_NOTICE, "Don't know how to handle technology '%s'\n", tech);
101                         res = 0;
102                 }
103         }
104         if (!res) {
105                 /* Look for a "busy" place */
106                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
107                         chan->priority += 100;
108         } else if (res > 0)
109                 res = 0;
110         return res;
111 }
112
113 int unload_module(void)
114 {
115         STANDARD_HANGUP_LOCALUSERS;
116         return ast_unregister_application(app);
117 }
118
119 int load_module(void)
120 {
121         return ast_register_application(app, enumlookup_exec, synopsis, descrip);
122 }
123
124 char *description(void)
125 {
126         return tdesc;
127 }
128
129 int usecount(void)
130 {
131         int res;
132         STANDARD_USECOUNT(res);
133         return res;
134 }
135
136 char *key()
137 {
138         return ASTERISK_GPL_KEY;
139 }