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