2 * Asterisk -- A telephony toolkit for Linux.
4 * Open Settlement Protocol Lookup
6 * Copyright (C) 1999 - 2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
21 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
23 #include "asterisk/lock.h"
24 #include "asterisk/file.h"
25 #include "asterisk/logger.h"
26 #include "asterisk/channel.h"
27 #include "asterisk/pbx.h"
28 #include "asterisk/options.h"
29 #include "asterisk/config.h"
30 #include "asterisk/module.h"
31 #include "asterisk/utils.h"
32 #include "asterisk/causes.h"
33 #include "asterisk/astosp.h"
35 static char *tdesc = "OSP Lookup";
37 static char *app = "OSPLookup";
38 static char *app2 = "OSPNext";
39 static char *app3 = "OSPFinish";
41 static char *synopsis = "Lookup number in OSP";
42 static char *synopsis2 = "Lookup next OSP entry";
43 static char *synopsis3 = "Record OSP entry";
45 static char *descrip =
46 " OSPLookup(exten[|provider[|options]]): Looks up an extension via OSP and sets\n"
47 "the variables, where 'n' is the number of the result beginning with 1:\n"
48 " ${OSPTECH}: The technology to use for the call\n"
49 " ${OSPDEST}: The destination to use for the call\n"
50 " ${OSPTOKEN}: The actual OSP token as a string\n"
51 " ${OSPHANDLE}: The OSP Handle for anything remaining\n"
52 " ${OSPRESULTS}: The number of OSP results total remaining\n"
54 "If the lookup was *not* successful and there exists a priority n + 101,\n"
55 "then that priority will be taken next.\n" ;
57 static char *descrip2 =
58 " OSPNext: Looks up the next OSP Destination for ${OSPHANDLE}\n"
59 "See OSPLookup for more information\n"
61 "If the lookup was *not* successful and there exists a priority n + 101,\n"
62 "then that priority will be taken next.\n" ;
64 static char *descrip3 =
65 " OSPFinish(status): Records call state for ${OSPHANDLE}, according to\n"
66 "status, which should be one of BUSY, CONGESTION, ANSWER, NOANSWER, or NOCHANAVAIL\n"
67 "or coincidentally, just what the Dial application stores in its ${DIALSTATUS}\n"
69 "If the finishing was *not* successful and there exists a priority n + 101,\n"
70 "then that priority will be taken next.\n" ;
76 static int str2cause(char *cause)
78 if (!strcasecmp(cause, "BUSY"))
79 return AST_CAUSE_BUSY;
80 if (!strcasecmp(cause, "CONGESTION"))
81 return AST_CAUSE_CONGESTION;
82 if (!strcasecmp(cause, "ANSWER"))
83 return AST_CAUSE_NORMAL;
84 if (!strcasecmp(cause, "CANCEL"))
85 return AST_CAUSE_NORMAL;
86 if (!strcasecmp(cause, "NOANSWER"))
87 return AST_CAUSE_NOANSWER;
88 if (!strcasecmp(cause, "NOCHANAVAIL"))
89 return AST_CAUSE_CONGESTION;
90 ast_log(LOG_WARNING, "Unknown cause '%s', using NORMAL\n", cause);
91 return AST_CAUSE_NORMAL;
94 static int osplookup_exec(struct ast_channel *chan, void *data)
99 char *provider, *opts=NULL;
100 struct ast_osp_result result;
101 if (!data || ast_strlen_zero(data) || !(temp = ast_strdupa(data))) {
102 ast_log(LOG_WARNING, "OSPLookup requires an argument (extension)\n");
105 provider = strchr(temp, '|');
109 opts = strchr(provider, '|');
116 ast_log(LOG_DEBUG, "Whoo hoo, looking up OSP on '%s' via '%s'\n", temp, provider ? provider : "<default>");
117 if ((res = ast_osp_lookup(chan, provider, temp, chan->cid.cid_num, &result)) > 0) {
119 snprintf(tmp, sizeof(tmp), "%d", result.handle);
120 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
121 pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
122 pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
123 pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
124 snprintf(tmp, sizeof(tmp), "%d", result.numresults);
125 pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
129 ast_log(LOG_NOTICE, "OSP Lookup failed for '%s' (provider '%s')\n", temp, provider ? provider : "<default>");
131 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Lookup for '%s' (provider '%s')!\n", chan->name, temp, provider ? provider : "<default>" );
134 /* Look for a "busy" place */
135 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
138 LOCAL_USER_REMOVE(u);
142 static int ospnext_exec(struct ast_channel *chan, void *data)
148 struct ast_osp_result result;
149 if (!data || ast_strlen_zero(data)) {
150 ast_log(LOG_WARNING, "OSPNext should have an argument (cause)\n");
153 cause = str2cause((char *)data);
154 temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
156 if (temp && strlen(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
157 if ((res = ast_osp_next(&result, cause)) > 0) {
159 snprintf(tmp, sizeof(tmp), "%d", result.handle);
160 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
161 pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
162 pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
163 pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
164 snprintf(tmp, sizeof(tmp), "%d", result.numresults);
165 pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
169 if (result.handle < 0)
170 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
172 ast_log(LOG_DEBUG, "No OSP handle specified\n");
174 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
177 /* Look for a "busy" place */
178 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
181 LOCAL_USER_REMOVE(u);
185 static int ospfinished_exec(struct ast_channel *chan, void *data)
191 time_t start=0, duration=0;
192 struct ast_osp_result result;
193 if (!data || ast_strlen_zero(data)) {
194 ast_log(LOG_WARNING, "OSPFinish should have an argument (cause)\n");
197 start = chan->cdr->answer.tv_sec;
199 duration = time(NULL) - start;
203 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
205 cause = str2cause((char *)data);
206 temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
208 if (temp && strlen(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
209 if (!ast_osp_terminate(result.handle, cause, start, duration)) {
210 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", "");
215 if (result.handle > -1)
216 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
218 ast_log(LOG_DEBUG, "No OSP handle specified\n");
220 ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
223 /* Look for a "busy" place */
224 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
227 LOCAL_USER_REMOVE(u);
232 int unload_module(void)
235 STANDARD_HANGUP_LOCALUSERS;
236 res = ast_unregister_application(app3);
237 res |= ast_unregister_application(app2);
238 res |= ast_unregister_application(app);
242 int load_module(void)
245 res = ast_register_application(app, osplookup_exec, synopsis, descrip);
248 res = ast_register_application(app2, ospnext_exec, synopsis2, descrip2);
251 res = ast_register_application(app3, ospfinished_exec, synopsis3, descrip3);
263 char *description(void)
271 STANDARD_USECOUNT(res);
277 return ASTERISK_GPL_KEY;