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