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
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/lock.h"
35 #include "asterisk/file.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/options.h"
40 #include "asterisk/config.h"
41 #include "asterisk/module.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/causes.h"
44 #include "asterisk/astosp.h"
46 static char *tdesc = "OSP Lookup";
48 static char *app = "OSPLookup";
49 static char *app2 = "OSPNext";
50 static char *app3 = "OSPFinish";
52 static char *synopsis = "Lookup number in OSP";
53 static char *synopsis2 = "Lookup next OSP entry";
54 static char *synopsis3 = "Record OSP entry";
56 static char *descrip =
57 " OSPLookup(exten[|provider[|options]]): Looks up an extension via OSP and sets\n"
58 "the variables, where 'n' is the number of the result beginning with 1:\n"
59 " ${OSPTECH}: The technology to use for the call\n"
60 " ${OSPDEST}: The destination to use for the call\n"
61 " ${OSPTOKEN}: The actual OSP token as a string\n"
62 " ${OSPHANDLE}: The OSP Handle for anything remaining\n"
63 " ${OSPRESULTS}: The number of OSP results total remaining\n"
65 "If the lookup was *not* successful and there exists a priority n + 101,\n"
66 "then that priority will be taken next.\n" ;
68 static char *descrip2 =
69 " OSPNext: Looks up the next OSP Destination for ${OSPHANDLE}\n"
70 "See OSPLookup for more information\n"
72 "If the lookup was *not* successful and there exists a priority n + 101,\n"
73 "then that priority will be taken next.\n" ;
75 static char *descrip3 =
76 " OSPFinish(status): Records call state for ${OSPHANDLE}, according to\n"
77 "status, which should be one of BUSY, CONGESTION, ANSWER, NOANSWER, or NOCHANAVAIL\n"
78 "or coincidentally, just what the Dial application stores in its ${DIALSTATUS}\n"
80 "If the finishing was *not* successful and there exists a priority n + 101,\n"
81 "then that priority will be taken next.\n" ;
87 static int str2cause(char *cause)
89 if (!strcasecmp(cause, "BUSY"))
90 return AST_CAUSE_BUSY;
91 if (!strcasecmp(cause, "CONGESTION"))
92 return AST_CAUSE_CONGESTION;
93 if (!strcasecmp(cause, "ANSWER"))
94 return AST_CAUSE_NORMAL;
95 if (!strcasecmp(cause, "CANCEL"))
96 return AST_CAUSE_NORMAL;
97 if (!strcasecmp(cause, "NOANSWER"))
98 return AST_CAUSE_NOANSWER;
99 if (!strcasecmp(cause, "NOCHANAVAIL"))
100 return AST_CAUSE_CONGESTION;
101 ast_log(LOG_WARNING, "Unknown cause '%s', using NORMAL\n", cause);
102 return AST_CAUSE_NORMAL;
105 static int osplookup_exec(struct ast_channel *chan, void *data)
110 char *provider, *opts=NULL;
111 struct ast_osp_result result;
113 if (ast_strlen_zero(data)) {
114 ast_log(LOG_WARNING, "OSPLookup requires an argument (extension)\n");
120 temp = ast_strdupa(data);
122 ast_log(LOG_ERROR, "Out of memory!\n");
123 LOCAL_USER_REMOVE(u);
127 provider = strchr(temp, '|');
131 opts = strchr(provider, '|');
138 ast_log(LOG_DEBUG, "Whoo hoo, looking up OSP on '%s' via '%s'\n", temp, provider ? provider : "<default>");
139 if ((res = ast_osp_lookup(chan, provider, temp, chan->cid.cid_num, &result)) > 0) {
141 snprintf(tmp, sizeof(tmp), "%d", result.handle);
142 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
143 pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
144 pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
145 pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
146 snprintf(tmp, sizeof(tmp), "%d", result.numresults);
147 pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
151 ast_log(LOG_NOTICE, "OSP Lookup failed for '%s' (provider '%s')\n", temp, provider ? provider : "<default>");
153 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Lookup for '%s' (provider '%s')!\n", chan->name, temp, provider ? provider : "<default>" );
156 /* Look for a "busy" place */
157 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
160 LOCAL_USER_REMOVE(u);
164 static int ospnext_exec(struct ast_channel *chan, void *data)
170 struct ast_osp_result result;
172 if (ast_strlen_zero(data)) {
173 ast_log(LOG_WARNING, "OSPNext should have an argument (cause)\n");
179 cause = str2cause((char *)data);
180 temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
182 if (!ast_strlen_zero(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
183 if ((res = ast_osp_next(&result, cause)) > 0) {
185 snprintf(tmp, sizeof(tmp), "%d", result.handle);
186 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
187 pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
188 pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
189 pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
190 snprintf(tmp, sizeof(tmp), "%d", result.numresults);
191 pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
195 if (result.handle < 0)
196 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
198 ast_log(LOG_DEBUG, "No OSP handle specified\n");
200 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
203 /* Look for a "busy" place */
204 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
207 LOCAL_USER_REMOVE(u);
211 static int ospfinished_exec(struct ast_channel *chan, void *data)
217 time_t start=0, duration=0;
218 struct ast_osp_result result;
220 if (ast_strlen_zero(data)) {
221 ast_log(LOG_WARNING, "OSPFinish should have an argument (cause)\n");
228 start = chan->cdr->answer.tv_sec;
230 duration = time(NULL) - start;
234 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
236 cause = str2cause((char *)data);
237 temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
239 if (!ast_strlen_zero(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
240 if (!ast_osp_terminate(result.handle, cause, start, duration)) {
241 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", "");
246 if (result.handle > -1)
247 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
249 ast_log(LOG_DEBUG, "No OSP handle specified\n");
251 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
254 /* Look for a "busy" place */
255 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
258 LOCAL_USER_REMOVE(u);
263 int unload_module(void)
267 res = ast_unregister_application(app3);
268 res |= ast_unregister_application(app2);
269 res |= ast_unregister_application(app);
271 STANDARD_HANGUP_LOCALUSERS;
276 int load_module(void)
280 res = ast_register_application(app, osplookup_exec, synopsis, descrip);
281 res |= ast_register_application(app2, ospnext_exec, synopsis2, descrip2);
282 res |= ast_register_application(app3, ospfinished_exec, synopsis3, descrip3);
293 char *description(void)
301 STANDARD_USECOUNT(res);
307 return ASTERISK_GPL_KEY;