fb0956721dfea311b51e98f81d55bc8696910ab8
[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, "H323")) {
77                         c = dest;
78                         if (!strncmp(c, "h323:", 5))
79                                 c += 5;
80                         snprintf(tmp, sizeof(tmp), "H323/%s", c);
81                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
82                 } else if (!strcasecmp(tech, "IAX")) {
83                         c = dest;
84                         if (!strncmp(c, "iax:", 4))
85                                 c += 4;
86                         snprintf(tmp, sizeof(tmp), "IAX/%s", c);
87                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
88                 } else if (!strcasecmp(tech, "IAX2")) {
89                         c = dest;
90                         if (!strncmp(c, "iax2:", 5))
91                                 c += 5;
92                         snprintf(tmp, sizeof(tmp), "IAX2/%s", c);
93                         pbx_builtin_setvar_helper(chan, "ENUM", tmp);
94                 } else if (!strcasecmp(tech, "tel")) {
95                         c = dest;
96                         if (!strncmp(c, "tel:", 4))
97                                 c += 4;
98
99                         if (c[0] != '+') {
100                                 ast_log(LOG_NOTICE, "tel: uri must start with a \"+\" (got '%s')\n", c);
101                                 res = 0;
102                         } else {
103 /* now copy over the number, skipping all non-digits and stop at ; or NULL */
104                                 t = tmp;        
105                                 while( *c && (*c != ';') && (t - tmp < (sizeof(tmp) - 1))) {
106                                         if (isdigit(*c))
107                                                 *t++ = *c;
108                                         c++;
109                                 }
110                                 *t = 0;
111                                 pbx_builtin_setvar_helper(chan, "ENUM", tmp);
112                                 ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp);
113                                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 51, chan->callerid))
114                                         chan->priority += 50;
115                                 else
116                                         res = 0;
117                         }
118                 } else if (strlen(tech)) {
119                         ast_log(LOG_NOTICE, "Don't know how to handle technology '%s'\n", tech);
120                         res = 0;
121                 }
122         }
123         if (!res) {
124                 /* Look for a "busy" place */
125                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
126                         chan->priority += 100;
127         } else if (res > 0)
128                 res = 0;
129         return res;
130 }
131
132 int unload_module(void)
133 {
134         STANDARD_HANGUP_LOCALUSERS;
135         return ast_unregister_application(app);
136 }
137
138 int load_module(void)
139 {
140         return ast_register_application(app, enumlookup_exec, synopsis, descrip);
141 }
142
143 char *description(void)
144 {
145         return tdesc;
146 }
147
148 int usecount(void)
149 {
150         int res;
151         STANDARD_USECOUNT(res);
152         return res;
153 }
154
155 char *key()
156 {
157         return ASTERISK_GPL_KEY;
158 }