the last round of file version tags
[asterisk/asterisk.git] / apps / app_osplookup.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Open Settlement Protocol Lookup
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <ctype.h>
18
19 #include "asterisk.h"
20
21 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
22
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"
34
35 static char *tdesc = "OSP Lookup";
36
37 static char *app = "OSPLookup";
38 static char *app2 = "OSPNext";
39 static char *app3 = "OSPFinish";
40
41 static char *synopsis = "Lookup number in OSP";
42 static char *synopsis2 = "Lookup next OSP entry";
43 static char *synopsis3 = "Record OSP entry";
44
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"
53 "\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" ;
56
57 static char *descrip2 = 
58 "  OSPNext:  Looks up the next OSP Destination for ${OSPHANDLE}\n"
59 "See OSPLookup for more information\n"
60 "\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" ;
63
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"
68 "\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" ;
71
72 STANDARD_LOCAL_USER;
73
74 LOCAL_USER_DECL;
75
76 static int str2cause(char *cause)
77 {
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;
92 }
93
94 static int osplookup_exec(struct ast_channel *chan, void *data)
95 {
96         int res=0;
97         struct localuser *u;
98         char *temp;
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");
103                 return -1;
104         }
105         provider = strchr(temp, '|');
106         if (provider) {
107                 *provider = '\0';
108                 provider++;
109                 opts = strchr(provider, '|');
110                 if (opts) {
111                         *opts = '\0';
112                         opts++;
113                 }
114         }
115         LOCAL_USER_ADD(u);
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) {
118                 char tmp[80];
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);
126
127         } else {
128                 if (!res)
129                         ast_log(LOG_NOTICE, "OSP Lookup failed for '%s' (provider '%s')\n", temp, provider ? provider : "<default>");
130                 else
131                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Lookup for '%s' (provider '%s')!\n", chan->name, temp, provider ? provider : "<default>" );
132         }
133         if (!res) {
134                 /* Look for a "busy" place */
135                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
136                         chan->priority += 100;
137         } else if (res > 0)
138                 res = 0;
139         LOCAL_USER_REMOVE(u);
140         return res;
141 }
142
143 static int ospnext_exec(struct ast_channel *chan, void *data)
144 {
145         int res=0;
146         struct localuser *u;
147         char *temp;
148         int cause;
149         struct ast_osp_result result;
150         if (!data || ast_strlen_zero(data)) {
151                 ast_log(LOG_WARNING, "OSPNext should have an argument (cause)\n");
152         }
153         LOCAL_USER_ADD(u);
154         cause = str2cause((char *)data);
155         temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
156         result.handle = -1;
157         if (temp && strlen(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
158                 if ((res = ast_osp_next(&result, cause)) > 0) {
159                         char tmp[80];
160                         snprintf(tmp, sizeof(tmp), "%d", result.handle);
161                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
162                         pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
163                         pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
164                         pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
165                         snprintf(tmp, sizeof(tmp), "%d", result.numresults);
166                         pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
167                 }
168         } else {
169                 if (!res) {
170                         if (result.handle < 0)
171                                 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
172                         else
173                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
174                 } else
175                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
176         }
177         if (!res) {
178                 /* Look for a "busy" place */
179                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
180                         chan->priority += 100;
181         } else if (res > 0)
182                 res = 0;
183         LOCAL_USER_REMOVE(u);
184         return res;
185 }
186
187 static int ospfinished_exec(struct ast_channel *chan, void *data)
188 {
189         int res=0;
190         struct localuser *u;
191         char *temp;
192         int cause;
193         time_t start=0, duration=0;
194         struct ast_osp_result result;
195         if (!data || ast_strlen_zero(data)) {
196                 ast_log(LOG_WARNING, "OSPFinish should have an argument (cause)\n");
197         }
198         if (chan->cdr) {
199                 start = chan->cdr->answer.tv_sec;
200                 if (start)
201                         duration = time(NULL) - start;
202                 else
203                         duration = 0;
204         } else
205                 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
206         LOCAL_USER_ADD(u);
207         cause = str2cause((char *)data);
208         temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
209         result.handle = -1;
210         if (temp && strlen(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
211                 if (!ast_osp_terminate(result.handle, cause, start, duration)) {
212                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", "");
213                         res = 1;
214                 }
215         } else {
216                 if (!res) {
217                         if (result.handle > -1)
218                                 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
219                         else
220                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
221                 } else
222                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
223         }
224         if (!res) {
225                 /* Look for a "busy" place */
226                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
227                         chan->priority += 100;
228         } else if (res > 0)
229                 res = 0;
230         LOCAL_USER_REMOVE(u);
231         return res;
232 }
233
234
235 int unload_module(void)
236 {
237         int res;
238         STANDARD_HANGUP_LOCALUSERS;
239         res = ast_unregister_application(app3);
240         res |= ast_unregister_application(app2);
241         res |= ast_unregister_application(app);
242         return res;
243 }
244
245 int load_module(void)
246 {
247         int res;
248         res = ast_register_application(app, osplookup_exec, synopsis, descrip);
249         if (res)
250                 return(res);
251         res = ast_register_application(app2, ospnext_exec, synopsis2, descrip2);
252         if (res)
253                 return(res);
254         res = ast_register_application(app3, ospfinished_exec, synopsis3, descrip3);
255         if (res)
256                 return(res);
257         return(0);
258 }
259
260 int reload(void)
261 {
262         return 0;
263 }
264
265
266 char *description(void)
267 {
268         return tdesc;
269 }
270
271 int usecount(void)
272 {
273         int res;
274         STANDARD_USECOUNT(res);
275         return res;
276 }
277
278 char *key()
279 {
280         return ASTERISK_GPL_KEY;
281 }
282