2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, 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 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
52 static char *tdesc = "OSP Lookup";
54 static char *app = "OSPLookup";
55 static char *app2 = "OSPNext";
56 static char *app3 = "OSPFinish";
58 static char *synopsis = "Lookup number in OSP";
59 static char *synopsis2 = "Lookup next OSP entry";
60 static char *synopsis3 = "Record OSP entry";
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"
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";
78 static char *descrip2 =
79 " OSPNext(cause[|options]): Looks up the next OSP Destination for ${OSPHANDLE}\n"
80 "See OSPLookup for more information\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";
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"
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";
103 static int str2cause(char *cause)
105 if (!strcasecmp(cause, "BUSY"))
106 return AST_CAUSE_BUSY;
107 if (!strcasecmp(cause, "CONGESTION"))
108 return AST_CAUSE_CONGESTION;
109 if (!strcasecmp(cause, "ANSWER"))
110 return AST_CAUSE_NORMAL;
111 if (!strcasecmp(cause, "CANCEL"))
112 return AST_CAUSE_NORMAL;
113 if (!strcasecmp(cause, "NOANSWER"))
114 return AST_CAUSE_NOANSWER;
115 if (!strcasecmp(cause, "NOCHANAVAIL"))
116 return AST_CAUSE_CONGESTION;
117 ast_log(LOG_WARNING, "Unknown cause '%s', using NORMAL\n", cause);
118 return AST_CAUSE_NORMAL;
121 static int osplookup_exec(struct ast_channel *chan, void *data)
126 struct ast_osp_result result;
127 int priority_jump = 0;
128 AST_DECLARE_APP_ARGS(args,
129 AST_APP_ARG(extension);
130 AST_APP_ARG(provider);
131 AST_APP_ARG(options);
134 if (ast_strlen_zero(data)) {
135 ast_log(LOG_WARNING, "OSPLookup requires an argument OSPLookup(exten[|provider[|options]])\n");
141 if (!(temp = ast_strdupa(data))) {
142 LOCAL_USER_REMOVE(u);
146 AST_STANDARD_APP_ARGS(args, temp);
149 if (strchr(args.options, 'j'))
153 ast_log(LOG_DEBUG, "Whoo hoo, looking up OSP on '%s' via '%s'\n", args.extension, args.provider ? args.provider : "<default>");
154 if ((res = ast_osp_lookup(chan, args.provider, args.extension, chan->cid.cid_num, &result)) > 0) {
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 pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", "SUCCESS");
167 ast_log(LOG_NOTICE, "OSP Lookup failed for '%s' (provider '%s')\n", args.extension, args.provider ? args.provider : "<default>");
168 pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", "FAILED");
170 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>" );
173 /* Look for a "busy" place */
174 if (priority_jump || ast_opt_priority_jumping)
175 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
178 LOCAL_USER_REMOVE(u);
182 static int ospnext_exec(struct ast_channel *chan, void *data)
189 struct ast_osp_result result;
190 int priority_jump = 0;
191 AST_DECLARE_APP_ARGS(args,
193 AST_APP_ARG(options);
196 if (ast_strlen_zero(data)) {
197 ast_log(LOG_WARNING, "OSPNext should have an argument (cause[|options])\n");
203 if (!(temp = ast_strdupa(data))) {
204 LOCAL_USER_REMOVE(u);
208 AST_STANDARD_APP_ARGS(args, temp);
211 if (strchr(args.options, 'j'))
215 cause = str2cause(args.cause);
216 val = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
218 if (!ast_strlen_zero(val) && (sscanf(val, "%d", &result.handle) == 1) && (result.handle > -1)) {
219 val = pbx_builtin_getvar_helper(chan, "OSPRESULTS");
220 if (ast_strlen_zero(val) || (sscanf(val, "%d", &result.numresults) != 1)) {
221 result.numresults = 0;
223 if ((res = ast_osp_next(&result, cause)) > 0) {
225 snprintf(tmp, sizeof(tmp), "%d", result.handle);
226 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
227 pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
228 pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
229 pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
230 snprintf(tmp, sizeof(tmp), "%d", result.numresults);
231 pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
232 pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "SUCCESS");
236 if (result.handle < 0)
237 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
239 ast_log(LOG_DEBUG, "No OSP handle specified\n");
240 pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "FAILED");
242 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
245 /* Look for a "busy" place */
246 if (priority_jump || ast_opt_priority_jumping)
247 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
250 LOCAL_USER_REMOVE(u);
254 static int ospfinished_exec(struct ast_channel *chan, void *data)
261 time_t start=0, duration=0;
262 struct ast_osp_result result;
263 int priority_jump = 0;
264 AST_DECLARE_APP_ARGS(args,
266 AST_APP_ARG(options);
269 if (ast_strlen_zero(data)) {
270 ast_log(LOG_WARNING, "OSPFinish should have an argument (status[|options])\n");
276 if (!(temp = ast_strdupa(data))) {
277 LOCAL_USER_REMOVE(u);
281 AST_STANDARD_APP_ARGS(args, temp);
284 if (strchr(args.options, 'j'))
289 start = chan->cdr->answer.tv_sec;
291 duration = time(NULL) - start;
295 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
297 cause = str2cause(args.status);
298 val = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
300 if (!ast_strlen_zero(val) && (sscanf(val, "%d", &result.handle) == 1) && (result.handle > -1)) {
301 if (!ast_osp_terminate(result.handle, cause, start, duration)) {
302 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", "");
303 pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "SUCCESS");
308 if (result.handle > -1)
309 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
311 ast_log(LOG_DEBUG, "No OSP handle specified\n");
312 pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "FAILED");
314 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
317 /* Look for a "busy" place */
318 if (priority_jump || ast_opt_priority_jumping)
319 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
322 LOCAL_USER_REMOVE(u);
327 int unload_module(void)
331 res = ast_unregister_application(app3);
332 res |= ast_unregister_application(app2);
333 res |= ast_unregister_application(app);
335 STANDARD_HANGUP_LOCALUSERS;
340 int load_module(void)
344 res = ast_register_application(app, osplookup_exec, synopsis, descrip);
345 res |= ast_register_application(app2, ospnext_exec, synopsis2, descrip2);
346 res |= ast_register_application(app3, ospfinished_exec, synopsis3, descrip3);
357 char *description(void)
365 STANDARD_USECOUNT(res);
371 return ASTERISK_GPL_KEY;