don't use '%i' at all, since we have no current use cases that need non base-10 parsi...
[asterisk/asterisk.git] / apps / app_osplookup.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Open Settlement Protocol Lookup
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 "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/options.h"
20 #include "asterisk/config.h"
21 #include "asterisk/module.h"
22 #include "asterisk/utils.h"
23 #include "asterisk/causes.h"
24 #include "asterisk/astosp.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 static char *tdesc = "OSP Lookup";
32
33 static char *app = "OSPLookup";
34 static char *app2 = "OSPNext";
35 static char *app3 = "OSPFinish";
36
37 static char *synopsis = "Lookup number in OSP";
38 static char *synopsis2 = "Lookup next OSP entry";
39 static char *synopsis3 = "Record OSP entry";
40
41 static char *descrip = 
42 "  OSPLookup(exten[|provider[|options]]):  Looks up an extension via OSP and sets\n"
43 "the variables, where 'n' is the number of the result beginning with 1:\n"
44 " ${OSPTECH}:   The technology to use for the call\n"
45 " ${OSPDEST}:   The destination to use for the call\n"
46 " ${OSPTOKEN}:  The actual OSP token as a string\n"
47 " ${OSPHANDLE}: The OSP Handle for anything remaining\n"
48 " ${OSPRESULTS}: The number of OSP results total remaining\n"
49 "\n"
50 "If the lookup was *not* successful and there exists a priority n + 101,\n"
51 "then that priority will be taken next.\n" ;
52
53 static char *descrip2 = 
54 "  OSPNext:  Looks up the next OSP Destination for ${OSPHANDLE}\n"
55 "See OSPLookup for more information\n"
56 "\n"
57 "If the lookup was *not* successful and there exists a priority n + 101,\n"
58 "then that priority will be taken next.\n" ;
59
60 static char *descrip3 = 
61 "  OSPFinish(status):  Records call state for ${OSPHANDLE}, according to\n"
62 "status, which should be one of BUSY, CONGESTION, ANSWER, NOANSWER, or NOCHANAVAIL\n"
63 "or coincidentally, just what the Dial application stores in its ${DIALSTATUS}\n"
64 "\n"
65 "If the finishing was *not* successful and there exists a priority n + 101,\n"
66 "then that priority will be taken next.\n" ;
67
68 STANDARD_LOCAL_USER;
69
70 LOCAL_USER_DECL;
71
72 static int str2cause(char *cause)
73 {
74         if (!strcasecmp(cause, "BUSY"))
75                 return AST_CAUSE_BUSY;
76         if (!strcasecmp(cause, "CONGESTION"))
77                 return AST_CAUSE_CONGESTION;
78         if (!strcasecmp(cause, "ANSWER"))
79                 return AST_CAUSE_NORMAL;
80         if (!strcasecmp(cause, "CANCEL"))
81                 return AST_CAUSE_NORMAL;
82         if (!strcasecmp(cause, "NOANSWER"))
83                 return AST_CAUSE_NOANSWER;
84         if (!strcasecmp(cause, "NOCHANAVAIL"))
85                 return AST_CAUSE_CONGESTION;
86         ast_log(LOG_WARNING, "Unknown cause '%s', using NORMAL\n", cause);
87         return AST_CAUSE_NORMAL;
88 }
89
90 static int osplookup_exec(struct ast_channel *chan, void *data)
91 {
92         int res=0;
93         struct localuser *u;
94         char *temp;
95         char *provider, *opts=NULL;
96         struct ast_osp_result result;
97         if (!data || ast_strlen_zero(data) || !(temp = ast_strdupa(data))) {
98                 ast_log(LOG_WARNING, "OSPLookup requires an argument (extension)\n");
99                 return -1;
100         }
101         provider = strchr(temp, '|');
102         if (provider) {
103                 *provider = '\0';
104                 provider++;
105                 opts = strchr(provider, '|');
106                 if (opts) {
107                         *opts = '\0';
108                         opts++;
109                 }
110         }
111         LOCAL_USER_ADD(u);
112         ast_log(LOG_DEBUG, "Whoo hoo, looking up OSP on '%s' via '%s'\n", temp, provider ? provider : "<default>");
113         if ((res = ast_osp_lookup(chan, provider, temp, chan->cid.cid_num, &result)) > 0) {
114                 char tmp[80];
115                 snprintf(tmp, sizeof(tmp), "%d", result.handle);
116                 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
117                 pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
118                 pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
119                 pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
120                 snprintf(tmp, sizeof(tmp), "%d", result.numresults);
121                 pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
122
123         } else {
124                 if (!res)
125                         ast_log(LOG_NOTICE, "OSP Lookup failed for '%s' (provider '%s')\n", temp, provider ? provider : "<default>");
126                 else
127                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Lookup for '%s' (provider '%s')!\n", chan->name, temp, provider ? provider : "<default>" );
128         }
129         if (!res) {
130                 /* Look for a "busy" place */
131                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
132                         chan->priority += 100;
133         } else if (res > 0)
134                 res = 0;
135         LOCAL_USER_REMOVE(u);
136         return res;
137 }
138
139 static int ospnext_exec(struct ast_channel *chan, void *data)
140 {
141         int res=0;
142         struct localuser *u;
143         char *temp;
144         int cause;
145         struct ast_osp_result result;
146         if (!data || ast_strlen_zero(data)) {
147                 ast_log(LOG_WARNING, "OSPNext should have an argument (cause)\n");
148         }
149         LOCAL_USER_ADD(u);
150         cause = str2cause((char *)data);
151         temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
152         result.handle = -1;
153         if (temp && strlen(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
154                 if ((res = ast_osp_next(&result, cause)) > 0) {
155                         char tmp[80];
156                         snprintf(tmp, sizeof(tmp), "%d", result.handle);
157                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
158                         pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
159                         pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
160                         pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
161                         snprintf(tmp, sizeof(tmp), "%d", result.numresults);
162                         pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
163                 }
164         } else {
165                 if (!res) {
166                         if (result.handle < 0)
167                                 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
168                         else
169                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
170                 } else
171                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
172         }
173         if (!res) {
174                 /* Look for a "busy" place */
175                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
176                         chan->priority += 100;
177         } else if (res > 0)
178                 res = 0;
179         LOCAL_USER_REMOVE(u);
180         return res;
181 }
182
183 static int ospfinished_exec(struct ast_channel *chan, void *data)
184 {
185         int res=0;
186         struct localuser *u;
187         char *temp;
188         int cause;
189         time_t start=0, duration=0;
190         struct ast_osp_result result;
191         if (!data || ast_strlen_zero(data)) {
192                 ast_log(LOG_WARNING, "OSPFinish should have an argument (cause)\n");
193         }
194         if (chan->cdr) {
195                 start = chan->cdr->answer.tv_sec;
196                 if (start)
197                         duration = time(NULL) - start;
198                 else
199                         duration = 0;
200         } else
201                 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
202         LOCAL_USER_ADD(u);
203         cause = str2cause((char *)data);
204         temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
205         result.handle = -1;
206         if (temp && strlen(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
207                 if (!ast_osp_terminate(result.handle, cause, start, duration)) {
208                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", "");
209                         res = 1;
210                 }
211         } else {
212                 if (!res) {
213                         if (result.handle > -1)
214                                 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
215                         else
216                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
217                 } else
218                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
219         }
220         if (!res) {
221                 /* Look for a "busy" place */
222                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
223                         chan->priority += 100;
224         } else if (res > 0)
225                 res = 0;
226         LOCAL_USER_REMOVE(u);
227         return res;
228 }
229
230
231 int unload_module(void)
232 {
233         int res;
234         STANDARD_HANGUP_LOCALUSERS;
235         res = ast_unregister_application(app3);
236         res |= ast_unregister_application(app2);
237         res |= ast_unregister_application(app);
238         return res;
239 }
240
241 int load_module(void)
242 {
243         int res;
244         res = ast_register_application(app, osplookup_exec, synopsis, descrip);
245         if (res)
246                 return(res);
247         res = ast_register_application(app2, ospnext_exec, synopsis2, descrip2);
248         if (res)
249                 return(res);
250         res = ast_register_application(app3, ospfinished_exec, synopsis3, descrip3);
251         if (res)
252                 return(res);
253         return(0);
254 }
255
256 int reload(void)
257 {
258         return 0;
259 }
260
261
262 char *description(void)
263 {
264         return tdesc;
265 }
266
267 int usecount(void)
268 {
269         int res;
270         STANDARD_USECOUNT(res);
271         return res;
272 }
273
274 char *key()
275 {
276         return ASTERISK_GPL_KEY;
277 }
278