2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Open Settlement Protocol Lookup
23 * \ingroup applications
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
49 static char *tdesc = "OSP Lookup";
51 static char *app = "OSPLookup";
52 static char *app2 = "OSPNext";
53 static char *app3 = "OSPFinish";
55 static char *synopsis = "Lookup number in OSP";
56 static char *synopsis2 = "Lookup next OSP entry";
57 static char *synopsis3 = "Record OSP entry";
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"
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";
75 static char *descrip2 =
76 " OSPNext(cause[|options]): Looks up the next OSP Destination for ${OSPHANDLE}\n"
77 "See OSPLookup for more information\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";
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"
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";
100 static int str2cause(char *cause)
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;
118 static int osplookup_exec(struct ast_channel *chan, void *data)
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);
131 if (ast_strlen_zero(data)) {
132 ast_log(LOG_WARNING, "OSPLookup requires an argument OSPLookup(exten[|provider[|options]])\n");
138 temp = ast_strdupa(data);
140 ast_log(LOG_ERROR, "Out of memory!\n");
141 LOCAL_USER_REMOVE(u);
145 AST_STANDARD_APP_ARGS(args, temp);
148 if (strchr(args.options, 'j'))
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) {
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");
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");
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>" );
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);
177 LOCAL_USER_REMOVE(u);
181 static int ospnext_exec(struct ast_channel *chan, void *data)
187 struct ast_osp_result result;
188 int priority_jump = 0;
189 AST_DECLARE_APP_ARGS(args,
191 AST_APP_ARG(options);
194 if (ast_strlen_zero(data)) {
195 ast_log(LOG_WARNING, "OSPNext should have an argument (cause[|options])\n");
201 temp = ast_strdupa(data);
203 ast_log(LOG_ERROR, "Out of memory!\n");
204 LOCAL_USER_REMOVE(u);
208 AST_STANDARD_APP_ARGS(args, temp);
211 if (strchr(args.options, 'j'))
215 cause = str2cause(args.cause);
216 temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
218 if (!ast_strlen_zero(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
219 if ((res = ast_osp_next(&result, cause)) > 0) {
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");
232 if (result.handle < 0)
233 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
235 ast_log(LOG_DEBUG, "No OSP handle specified\n");
236 pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "FAILED");
238 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
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);
246 LOCAL_USER_REMOVE(u);
250 static int ospfinished_exec(struct ast_channel *chan, void *data)
256 time_t start=0, duration=0;
257 struct ast_osp_result result;
258 int priority_jump = 0;
259 AST_DECLARE_APP_ARGS(args,
261 AST_APP_ARG(options);
264 if (ast_strlen_zero(data)) {
265 ast_log(LOG_WARNING, "OSPFinish should have an argument (status[|options])\n");
271 temp = ast_strdupa(data);
273 ast_log(LOG_ERROR, "Out of memory!\n");
274 LOCAL_USER_REMOVE(u);
278 AST_STANDARD_APP_ARGS(args, temp);
281 if (strchr(args.options, 'j'))
286 start = chan->cdr->answer.tv_sec;
288 duration = time(NULL) - start;
292 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
294 cause = str2cause(args.status);
295 temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
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");
305 if (result.handle > -1)
306 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
308 ast_log(LOG_DEBUG, "No OSP handle specified\n");
309 pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "FAILED");
311 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
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);
319 LOCAL_USER_REMOVE(u);
324 int unload_module(void)
328 res = ast_unregister_application(app3);
329 res |= ast_unregister_application(app2);
330 res |= ast_unregister_application(app);
332 STANDARD_HANGUP_LOCALUSERS;
337 int load_module(void)
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);
354 char *description(void)
362 STANDARD_USECOUNT(res);
368 return ASTERISK_GPL_KEY;