266c66dda4812ab271c9bd04810c020787b20cff
[asterisk/asterisk.git] / apps / app_osplookup.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Open Settlement Protocol Lookup
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/lock.h"
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/options.h"
44 #include "asterisk/config.h"
45 #include "asterisk/module.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/causes.h"
48 #include "asterisk/astosp.h"
49 #include "asterisk/app.h"
50 #include "asterisk/options.h"
51
52 static char *tdesc = "OSP Lookup";
53
54 static char *app = "OSPLookup";
55 static char *app2 = "OSPNext";
56 static char *app3 = "OSPFinish";
57
58 static char *synopsis = "Lookup number in OSP";
59 static char *synopsis2 = "Lookup next OSP entry";
60 static char *synopsis3 = "Record OSP entry";
61
62 static char *descrip = 
63 "  OSPLookup(exten[|provider[|options]]):  Looks up an extension via OSP and sets\n"
64 "the variables, where 'n' is the number of the result beginning with 1:\n"
65 " ${OSPTECH}:   The technology to use for the call\n"
66 " ${OSPDEST}:   The destination to use for the call\n"
67 " ${OSPTOKEN}:  The actual OSP token as a string\n"
68 " ${OSPHANDLE}: The OSP Handle for anything remaining\n"
69 " ${OSPRESULTS}: The number of OSP results total remaining\n"
70 "\n"
71 "The option string may contain the following character:\n"
72 "       'j' -- jump to n+101 priority if the lookup was NOT successful\n"
73 "This application sets the following channel variable upon completion:\n"
74 "       OSPLOOKUPSTATUS The status of the OSP Lookup attempt as a text string, one of\n"
75 "               SUCCESS | FAILED \n";
76
77
78 static char *descrip2 = 
79 "  OSPNext(cause[|options]):  Looks up the next OSP Destination for ${OSPHANDLE}\n"
80 "See OSPLookup for more information\n"
81 "\n"
82 "The option string may contain the following character:\n"
83 "       'j' -- jump to n+101 priority if the lookup was NOT successful\n"
84 "This application sets the following channel variable upon completion:\n"
85 "       OSPNEXTSTATUS   The status of the OSP Next attempt as a text string, one of\n"
86 "               SUCCESS | FAILED \n";
87
88 static char *descrip3 = 
89 "  OSPFinish(status[|options]):  Records call state for ${OSPHANDLE}, according to\n"
90 "status, which should be one of BUSY, CONGESTION, ANSWER, NOANSWER, or CHANUNAVAIL\n"
91 "or coincidentally, just what the Dial application stores in its ${DIALSTATUS}.\n"
92 "\n"
93 "The option string may contain the following character:\n"
94 "       'j' -- jump to n+101 priority if the finish attempt was NOT successful\n"
95 "This application sets the following channel variable upon completion:\n"
96 "       OSPFINISHSTATUS The status of the OSP Finish attempt as a text string, one of\n"
97 "               SUCCESS | FAILED \n";
98
99 LOCAL_USER_DECL;
100
101 static int str2cause(char *cause)
102 {
103         if (!strcasecmp(cause, "BUSY"))
104                 return AST_CAUSE_BUSY;
105         if (!strcasecmp(cause, "CONGESTION"))
106                 return AST_CAUSE_CONGESTION;
107         if (!strcasecmp(cause, "ANSWER"))
108                 return AST_CAUSE_NORMAL;
109         if (!strcasecmp(cause, "CANCEL"))
110                 return AST_CAUSE_NORMAL;
111         if (!strcasecmp(cause, "NOANSWER"))
112                 return AST_CAUSE_NOANSWER;
113         if (!strcasecmp(cause, "NOCHANAVAIL"))
114                 return AST_CAUSE_CONGESTION;
115         ast_log(LOG_WARNING, "Unknown cause '%s', using NORMAL\n", cause);
116         return AST_CAUSE_NORMAL;
117 }
118
119 static int osplookup_exec(struct ast_channel *chan, void *data)
120 {
121         int res=0;
122         struct localuser *u;
123         char *temp;
124         struct ast_osp_result result;
125         int priority_jump = 0;
126         AST_DECLARE_APP_ARGS(args,
127                 AST_APP_ARG(extension);
128                 AST_APP_ARG(provider);
129                 AST_APP_ARG(options);
130         );
131         
132         if (ast_strlen_zero(data)) {
133                 ast_log(LOG_WARNING, "OSPLookup requires an argument OSPLookup(exten[|provider[|options]])\n");
134                 return -1;
135         }
136
137         LOCAL_USER_ADD(u);
138
139         if (!(temp = ast_strdupa(data))) {
140                 LOCAL_USER_REMOVE(u);
141                 return -1;
142         }
143
144         AST_STANDARD_APP_ARGS(args, temp);
145
146         if (args.options) {
147                 if (strchr(args.options, 'j'))
148                         priority_jump = 1;
149         }
150
151         ast_log(LOG_DEBUG, "Whoo hoo, looking up OSP on '%s' via '%s'\n", args.extension, args.provider ? args.provider : "<default>");
152         if ((res = ast_osp_lookup(chan, args.provider, args.extension, chan->cid.cid_num, &result)) > 0) {
153                 char tmp[80];
154                 snprintf(tmp, sizeof(tmp), "%d", result.handle);
155                 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
156                 pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
157                 pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
158                 pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
159                 snprintf(tmp, sizeof(tmp), "%d", result.numresults);
160                 pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
161                 pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", "SUCCESS");
162
163         } else {
164                 if (!res) {
165                         ast_log(LOG_NOTICE, "OSP Lookup failed for '%s' (provider '%s')\n", args.extension, args.provider ? args.provider : "<default>");
166                         pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", "FAILED");
167                 } else
168                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Lookup for '%s' (provider '%s')!\n", chan->name, args.extension, args.provider ? args.provider : "<default>" );
169         }
170         if (!res) {
171                 /* Look for a "busy" place */
172                 if (priority_jump || ast_opt_priority_jumping)
173                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
174         } else if (res > 0)
175                 res = 0;
176         LOCAL_USER_REMOVE(u);
177         return res;
178 }
179
180 static int ospnext_exec(struct ast_channel *chan, void *data)
181 {
182         int res=0;
183         struct localuser *u;
184         char *temp;
185         const char *val;
186         int cause;
187         struct ast_osp_result result;
188         int priority_jump = 0;
189         AST_DECLARE_APP_ARGS(args,
190                 AST_APP_ARG(cause);
191                 AST_APP_ARG(options);
192         );
193         
194         if (ast_strlen_zero(data)) {
195                 ast_log(LOG_WARNING, "OSPNext should have an argument (cause[|options])\n");
196                 return -1;
197         }
198
199         LOCAL_USER_ADD(u);
200
201         if (!(temp = ast_strdupa(data))) {
202                 LOCAL_USER_REMOVE(u);
203                 return -1;
204         }
205
206         AST_STANDARD_APP_ARGS(args, temp);
207
208         if (args.options) {
209                 if (strchr(args.options, 'j'))
210                         priority_jump = 1;
211         }
212
213         cause = str2cause(args.cause);
214         val = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
215         result.handle = -1;
216         if (!ast_strlen_zero(val) && (sscanf(val, "%d", &result.handle) == 1) && (result.handle > -1)) {
217                 val = pbx_builtin_getvar_helper(chan, "OSPRESULTS");
218                 if (ast_strlen_zero(val) || (sscanf(val, "%d", &result.numresults) != 1)) {
219                         result.numresults = 0;
220                 }
221                 if ((res = ast_osp_next(&result, cause)) > 0) {
222                         char tmp[80];
223                         snprintf(tmp, sizeof(tmp), "%d", result.handle);
224                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
225                         pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
226                         pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
227                         pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
228                         snprintf(tmp, sizeof(tmp), "%d", result.numresults);
229                         pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
230                         pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "SUCCESS");
231                 }
232         } else {
233                 if (!res) {
234                         if (result.handle < 0)
235                                 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
236                         else
237                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
238                         pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "FAILED");     
239                 } else
240                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
241         }
242         if (!res) {
243                 /* Look for a "busy" place */
244                 if (priority_jump || ast_opt_priority_jumping)
245                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
246         } else if (res > 0)
247                 res = 0;
248         LOCAL_USER_REMOVE(u);
249         return res;
250 }
251
252 static int ospfinished_exec(struct ast_channel *chan, void *data)
253 {
254         int res=0;
255         struct localuser *u;
256         char *temp;
257         const char *val;
258         int cause;
259         time_t start=0, duration=0;
260         struct ast_osp_result result;
261         int priority_jump = 0;
262         AST_DECLARE_APP_ARGS(args,
263                 AST_APP_ARG(status);
264                 AST_APP_ARG(options);
265         );
266         
267         if (ast_strlen_zero(data)) {
268                 ast_log(LOG_WARNING, "OSPFinish should have an argument (status[|options])\n");
269                 return -1;
270         }
271
272         LOCAL_USER_ADD(u);
273
274         if (!(temp = ast_strdupa(data))) {
275                 LOCAL_USER_REMOVE(u);
276                 return -1;
277         }
278
279         AST_STANDARD_APP_ARGS(args, temp);
280
281         if (args.options) {
282                 if (strchr(args.options, 'j'))
283                         priority_jump = 1;
284         }
285
286         if (chan->cdr) {
287                 start = chan->cdr->answer.tv_sec;
288                 if (start)
289                         duration = time(NULL) - start;
290                 else
291                         duration = 0;
292         } else
293                 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
294         
295         cause = str2cause(args.status);
296         val = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
297         result.handle = -1;
298         if (!ast_strlen_zero(val) && (sscanf(val, "%d", &result.handle) == 1) && (result.handle > -1)) {
299                 if (!ast_osp_terminate(result.handle, cause, start, duration)) {
300                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", "");
301                         pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "SUCCESS");
302                         res = 1;
303                 }
304         } else {
305                 if (!res) {
306                         if (result.handle > -1)
307                                 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
308                         else
309                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
310                         pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "FAILED");
311                 } else
312                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
313         }
314         if (!res) {
315                 /* Look for a "busy" place */
316                 if (priority_jump || ast_opt_priority_jumping)
317                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
318         } else if (res > 0)
319                 res = 0;
320         LOCAL_USER_REMOVE(u);
321         return res;
322 }
323
324
325 int unload_module(void)
326 {
327         int res;
328         
329         res = ast_unregister_application(app3);
330         res |= ast_unregister_application(app2);
331         res |= ast_unregister_application(app);
332
333         STANDARD_HANGUP_LOCALUSERS;
334
335         return res;
336 }
337
338 int load_module(void)
339 {
340         int res;
341         
342         res = ast_register_application(app, osplookup_exec, synopsis, descrip);
343         res |= ast_register_application(app2, ospnext_exec, synopsis2, descrip2);
344         res |= ast_register_application(app3, ospfinished_exec, synopsis3, descrip3);
345         
346         return res;
347 }
348
349 int reload(void)
350 {
351         return 0;
352 }
353
354
355 char *description(void)
356 {
357         return tdesc;
358 }
359
360 int usecount(void)
361 {
362         int res;
363         STANDARD_USECOUNT(res);
364         return res;
365 }
366
367 char *key()
368 {
369         return ASTERISK_GPL_KEY;
370 }
371