convert most of the option_*'s to a single ast_flags structure. Also, fix some
[asterisk/asterisk.git] / apps / app_osplookup.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Open Settlement Protocol Lookup
22  * 
23  * \ingroup applications
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/options.h"
42 #include "asterisk/config.h"
43 #include "asterisk/module.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/astosp.h"
47 #include "asterisk/app.h"
48 #include "asterisk/options.h"
49
50 static char *tdesc = "OSP Lookup";
51
52 static char *app = "OSPLookup";
53 static char *app2 = "OSPNext";
54 static char *app3 = "OSPFinish";
55
56 static char *synopsis = "Lookup number in OSP";
57 static char *synopsis2 = "Lookup next OSP entry";
58 static char *synopsis3 = "Record OSP entry";
59
60 static char *descrip = 
61 "  OSPLookup(exten[|provider[|options]]):  Looks up an extension via OSP and sets\n"
62 "the variables, where 'n' is the number of the result beginning with 1:\n"
63 " ${OSPTECH}:   The technology to use for the call\n"
64 " ${OSPDEST}:   The destination to use for the call\n"
65 " ${OSPTOKEN}:  The actual OSP token as a string\n"
66 " ${OSPHANDLE}: The OSP Handle for anything remaining\n"
67 " ${OSPRESULTS}: The number of OSP results total remaining\n"
68 "\n"
69 "The option string may contain the following character:\n"
70 "       'j' -- jump to n+101 priority if the lookup was NOT successful\n"
71 "This application sets the following channel variable upon completion:\n"
72 "       OSPLOOKUPSTATUS The status of the OSP Lookup attempt as a text string, one of\n"
73 "               SUCCESS | FAILED \n";
74
75
76 static char *descrip2 = 
77 "  OSPNext(cause[|options]):  Looks up the next OSP Destination for ${OSPHANDLE}\n"
78 "See OSPLookup for more information\n"
79 "\n"
80 "The option string may contain the following character:\n"
81 "       'j' -- jump to n+101 priority if the lookup was NOT successful\n"
82 "This application sets the following channel variable upon completion:\n"
83 "       OSPNEXTSTATUS   The status of the OSP Next attempt as a text string, one of\n"
84 "               SUCCESS | FAILED \n";
85
86 static char *descrip3 = 
87 "  OSPFinish(status[|options]):  Records call state for ${OSPHANDLE}, according to\n"
88 "status, which should be one of BUSY, CONGESTION, ANSWER, NOANSWER, or CHANUNAVAIL\n"
89 "or coincidentally, just what the Dial application stores in its ${DIALSTATUS}.\n"
90 "\n"
91 "The option string may contain the following character:\n"
92 "       'j' -- jump to n+101 priority if the finish attempt was NOT successful\n"
93 "This application sets the following channel variable upon completion:\n"
94 "       OSPFINISHSTATUS The status of the OSP Finish attempt as a text string, one of\n"
95 "               SUCCESS | FAILED \n";
96
97 STANDARD_LOCAL_USER;
98
99 LOCAL_USER_DECL;
100
101 static int str2cause(char *cause)
102 {
103         if (!strcasecmp(cause, "BUSY"))
104                 return AST_CAUSE_BUSY;
105         if (!strcasecmp(cause, "CONGESTION"))
106                 return AST_CAUSE_CONGESTION;
107         if (!strcasecmp(cause, "ANSWER"))
108                 return AST_CAUSE_NORMAL;
109         if (!strcasecmp(cause, "CANCEL"))
110                 return AST_CAUSE_NORMAL;
111         if (!strcasecmp(cause, "NOANSWER"))
112                 return AST_CAUSE_NOANSWER;
113         if (!strcasecmp(cause, "NOCHANAVAIL"))
114                 return AST_CAUSE_CONGESTION;
115         ast_log(LOG_WARNING, "Unknown cause '%s', using NORMAL\n", cause);
116         return AST_CAUSE_NORMAL;
117 }
118
119 static int osplookup_exec(struct ast_channel *chan, void *data)
120 {
121         int res=0;
122         struct localuser *u;
123         char *temp;
124         struct ast_osp_result result;
125         int priority_jump = 0;
126         AST_DECLARE_APP_ARGS(args,
127                 AST_APP_ARG(extension);
128                 AST_APP_ARG(provider);
129                 AST_APP_ARG(options);
130         );
131         
132         if (ast_strlen_zero(data)) {
133                 ast_log(LOG_WARNING, "OSPLookup requires an argument OSPLookup(exten[|provider[|options]])\n");
134                 return -1;
135         }
136
137         LOCAL_USER_ADD(u);
138
139         temp = ast_strdupa(data);
140         if (!temp) {
141                 ast_log(LOG_ERROR, "Out of memory!\n");
142                 LOCAL_USER_REMOVE(u);
143                 return -1;
144         }
145
146         AST_STANDARD_APP_ARGS(args, temp);
147
148         if (args.options) {
149                 if (strchr(args.options, 'j'))
150                         priority_jump = 1;
151         }
152
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) {
155                 char tmp[80];
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");
164
165         } else {
166                 if (!res) {
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");
169                 } else
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>" );
171         }
172         if (!res) {
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);
176         } else if (res > 0)
177                 res = 0;
178         LOCAL_USER_REMOVE(u);
179         return res;
180 }
181
182 static int ospnext_exec(struct ast_channel *chan, void *data)
183 {
184         int res=0;
185         struct localuser *u;
186         char *temp;
187         int cause;
188         struct ast_osp_result result;
189         int priority_jump = 0;
190         AST_DECLARE_APP_ARGS(args,
191                 AST_APP_ARG(cause);
192                 AST_APP_ARG(options);
193         );
194         
195         if (ast_strlen_zero(data)) {
196                 ast_log(LOG_WARNING, "OSPNext should have an argument (cause[|options])\n");
197                 return -1;
198         }
199
200         LOCAL_USER_ADD(u);
201
202         temp = ast_strdupa(data);
203         if (!temp) {
204                 ast_log(LOG_ERROR, "Out of memory!\n");
205                 LOCAL_USER_REMOVE(u);
206                 return -1;
207         }
208
209         AST_STANDARD_APP_ARGS(args, temp);
210
211         if (args.options) {
212                 if (strchr(args.options, 'j'))
213                         priority_jump = 1;
214         }
215
216         cause = str2cause(args.cause);
217         temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
218         result.handle = -1;
219         if (!ast_strlen_zero(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
220                 temp = pbx_builtin_getvar_helper(chan, "OSPRESULTS");
221                 if (ast_strlen_zero(temp) || (sscanf(temp, "%d", &result.numresults) != 1)) {
222                         result.numresults = 0;
223                 }
224                 if ((res = ast_osp_next(&result, cause)) > 0) {
225                         char tmp[80];
226                         snprintf(tmp, sizeof(tmp), "%d", result.handle);
227                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
228                         pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
229                         pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
230                         pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
231                         snprintf(tmp, sizeof(tmp), "%d", result.numresults);
232                         pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
233                         pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "SUCCESS");
234                 }
235         } else {
236                 if (!res) {
237                         if (result.handle < 0)
238                                 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
239                         else
240                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
241                         pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "FAILED");     
242                 } else
243                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
244         }
245         if (!res) {
246                 /* Look for a "busy" place */
247                 if (priority_jump || ast_opt_priority_jumping)
248                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
249         } else if (res > 0)
250                 res = 0;
251         LOCAL_USER_REMOVE(u);
252         return res;
253 }
254
255 static int ospfinished_exec(struct ast_channel *chan, void *data)
256 {
257         int res=0;
258         struct localuser *u;
259         char *temp;
260         int cause;
261         time_t start=0, duration=0;
262         struct ast_osp_result result;
263         int priority_jump = 0;
264         AST_DECLARE_APP_ARGS(args,
265                 AST_APP_ARG(status);
266                 AST_APP_ARG(options);
267         );
268         
269         if (ast_strlen_zero(data)) {
270                 ast_log(LOG_WARNING, "OSPFinish should have an argument (status[|options])\n");
271                 return -1;
272         }
273
274         LOCAL_USER_ADD(u);
275
276         temp = ast_strdupa(data);
277         if (!temp) {
278                 ast_log(LOG_ERROR, "Out of memory!\n");
279                 LOCAL_USER_REMOVE(u);
280                 return -1;
281         }
282
283         AST_STANDARD_APP_ARGS(args, temp);
284
285         if (args.options) {
286                 if (strchr(args.options, 'j'))
287                         priority_jump = 1;
288         }
289
290         if (chan->cdr) {
291                 start = chan->cdr->answer.tv_sec;
292                 if (start)
293                         duration = time(NULL) - start;
294                 else
295                         duration = 0;
296         } else
297                 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
298         
299         cause = str2cause(args.status);
300         temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
301         result.handle = -1;
302         if (!ast_strlen_zero(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
303                 if (!ast_osp_terminate(result.handle, cause, start, duration)) {
304                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", "");
305                         pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "SUCCESS");
306                         res = 1;
307                 }
308         } else {
309                 if (!res) {
310                         if (result.handle > -1)
311                                 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
312                         else
313                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
314                         pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "FAILED");
315                 } else
316                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
317         }
318         if (!res) {
319                 /* Look for a "busy" place */
320                 if (priority_jump || ast_opt_priority_jumping)
321                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
322         } else if (res > 0)
323                 res = 0;
324         LOCAL_USER_REMOVE(u);
325         return res;
326 }
327
328
329 int unload_module(void)
330 {
331         int res;
332         
333         res = ast_unregister_application(app3);
334         res |= ast_unregister_application(app2);
335         res |= ast_unregister_application(app);
336
337         STANDARD_HANGUP_LOCALUSERS;
338
339         return res;
340 }
341
342 int load_module(void)
343 {
344         int res;
345         
346         res = ast_register_application(app, osplookup_exec, synopsis, descrip);
347         res |= ast_register_application(app2, ospnext_exec, synopsis2, descrip2);
348         res |= ast_register_application(app3, ospfinished_exec, synopsis3, descrip3);
349         
350         return res;
351 }
352
353 int reload(void)
354 {
355         return 0;
356 }
357
358
359 char *description(void)
360 {
361         return tdesc;
362 }
363
364 int usecount(void)
365 {
366         int res;
367         STANDARD_USECOUNT(res);
368         return res;
369 }
370
371 char *key()
372 {
373         return ASTERISK_GPL_KEY;
374 }
375