remove lots of useless checks of the result of ast_strdupa
[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  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
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"
51
52 static char *tdesc = "OSP Lookup";
53
54 static char *app = "OSPLookup";
55 static char *app2 = "OSPNext";
56 static char *app3 = "OSPFinish";
57
58 static char *synopsis = "Lookup number in OSP";
59 static char *synopsis2 = "Lookup next OSP entry";
60 static char *synopsis3 = "Record OSP entry";
61
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"
70 "\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";
76
77
78 static char *descrip2 = 
79 "  OSPNext(cause[|options]):  Looks up the next OSP Destination for ${OSPHANDLE}\n"
80 "See OSPLookup for more information\n"
81 "\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";
87
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"
92 "\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";
98
99 STANDARD_LOCAL_USER;
100
101 LOCAL_USER_DECL;
102
103 static int str2cause(char *cause)
104 {
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;
119 }
120
121 static int osplookup_exec(struct ast_channel *chan, void *data)
122 {
123         int res=0;
124         struct localuser *u;
125         char *temp;
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);
132         );
133         
134         if (ast_strlen_zero(data)) {
135                 ast_log(LOG_WARNING, "OSPLookup requires an argument OSPLookup(exten[|provider[|options]])\n");
136                 return -1;
137         }
138
139         LOCAL_USER_ADD(u);
140
141         temp = ast_strdupa(data);
142
143         AST_STANDARD_APP_ARGS(args, temp);
144
145         if (args.options) {
146                 if (strchr(args.options, 'j'))
147                         priority_jump = 1;
148         }
149
150         ast_log(LOG_DEBUG, "Whoo hoo, looking up OSP on '%s' via '%s'\n", args.extension, args.provider ? args.provider : "<default>");
151         if ((res = ast_osp_lookup(chan, args.provider, args.extension, chan->cid.cid_num, &result)) > 0) {
152                 char tmp[80];
153                 snprintf(tmp, sizeof(tmp), "%d", result.handle);
154                 pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
155                 pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
156                 pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
157                 pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
158                 snprintf(tmp, sizeof(tmp), "%d", result.numresults);
159                 pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
160                 pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", "SUCCESS");
161
162         } else {
163                 if (!res) {
164                         ast_log(LOG_NOTICE, "OSP Lookup failed for '%s' (provider '%s')\n", args.extension, args.provider ? args.provider : "<default>");
165                         pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", "FAILED");
166                 } else
167                         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>" );
168         }
169         if (!res) {
170                 /* Look for a "busy" place */
171                 if (priority_jump || ast_opt_priority_jumping)
172                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
173         } else if (res > 0)
174                 res = 0;
175         LOCAL_USER_REMOVE(u);
176         return res;
177 }
178
179 static int ospnext_exec(struct ast_channel *chan, void *data)
180 {
181         int res=0;
182         struct localuser *u;
183         char *temp;
184         int cause;
185         struct ast_osp_result result;
186         int priority_jump = 0;
187         AST_DECLARE_APP_ARGS(args,
188                 AST_APP_ARG(cause);
189                 AST_APP_ARG(options);
190         );
191         
192         if (ast_strlen_zero(data)) {
193                 ast_log(LOG_WARNING, "OSPNext should have an argument (cause[|options])\n");
194                 return -1;
195         }
196
197         LOCAL_USER_ADD(u);
198
199         temp = ast_strdupa(data);
200
201         AST_STANDARD_APP_ARGS(args, temp);
202
203         if (args.options) {
204                 if (strchr(args.options, 'j'))
205                         priority_jump = 1;
206         }
207
208         cause = str2cause(args.cause);
209         temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
210         result.handle = -1;
211         if (!ast_strlen_zero(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
212                 temp = pbx_builtin_getvar_helper(chan, "OSPRESULTS");
213                 if (ast_strlen_zero(temp) || (sscanf(temp, "%d", &result.numresults) != 1)) {
214                         result.numresults = 0;
215                 }
216                 if ((res = ast_osp_next(&result, cause)) > 0) {
217                         char tmp[80];
218                         snprintf(tmp, sizeof(tmp), "%d", result.handle);
219                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", tmp);
220                         pbx_builtin_setvar_helper(chan, "_OSPTECH", result.tech);
221                         pbx_builtin_setvar_helper(chan, "_OSPDEST", result.dest);
222                         pbx_builtin_setvar_helper(chan, "_OSPTOKEN", result.token);
223                         snprintf(tmp, sizeof(tmp), "%d", result.numresults);
224                         pbx_builtin_setvar_helper(chan, "_OSPRESULTS", tmp);
225                         pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "SUCCESS");
226                 }
227         } else {
228                 if (!res) {
229                         if (result.handle < 0)
230                                 ast_log(LOG_NOTICE, "OSP Lookup Next failed for handle '%d'\n", result.handle);
231                         else
232                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
233                         pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", "FAILED");     
234                 } else
235                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Next!\n", chan->name);
236         }
237         if (!res) {
238                 /* Look for a "busy" place */
239                 if (priority_jump || ast_opt_priority_jumping)
240                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
241         } else if (res > 0)
242                 res = 0;
243         LOCAL_USER_REMOVE(u);
244         return res;
245 }
246
247 static int ospfinished_exec(struct ast_channel *chan, void *data)
248 {
249         int res=0;
250         struct localuser *u;
251         char *temp;
252         int cause;
253         time_t start=0, duration=0;
254         struct ast_osp_result result;
255         int priority_jump = 0;
256         AST_DECLARE_APP_ARGS(args,
257                 AST_APP_ARG(status);
258                 AST_APP_ARG(options);
259         );
260         
261         if (ast_strlen_zero(data)) {
262                 ast_log(LOG_WARNING, "OSPFinish should have an argument (status[|options])\n");
263                 return -1;
264         }
265
266         LOCAL_USER_ADD(u);
267
268         temp = ast_strdupa(data);
269
270         AST_STANDARD_APP_ARGS(args, temp);
271
272         if (args.options) {
273                 if (strchr(args.options, 'j'))
274                         priority_jump = 1;
275         }
276
277         if (chan->cdr) {
278                 start = chan->cdr->answer.tv_sec;
279                 if (start)
280                         duration = time(NULL) - start;
281                 else
282                         duration = 0;
283         } else
284                 ast_log(LOG_WARNING, "OSPFinish called on channel '%s' with no CDR!\n", chan->name);
285         
286         cause = str2cause(args.status);
287         temp = pbx_builtin_getvar_helper(chan, "OSPHANDLE");
288         result.handle = -1;
289         if (!ast_strlen_zero(temp) && (sscanf(temp, "%d", &result.handle) == 1) && (result.handle > -1)) {
290                 if (!ast_osp_terminate(result.handle, cause, start, duration)) {
291                         pbx_builtin_setvar_helper(chan, "_OSPHANDLE", "");
292                         pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "SUCCESS");
293                         res = 1;
294                 }
295         } else {
296                 if (!res) {
297                         if (result.handle > -1)
298                                 ast_log(LOG_NOTICE, "OSP Finish failed for handle '%d'\n", result.handle);
299                         else
300                                 ast_log(LOG_DEBUG, "No OSP handle specified\n");
301                         pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", "FAILED");
302                 } else
303                         ast_log(LOG_DEBUG, "Got hangup on '%s' while doing OSP Terminate!\n", chan->name);
304         }
305         if (!res) {
306                 /* Look for a "busy" place */
307                 if (priority_jump || ast_opt_priority_jumping)
308                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
309         } else if (res > 0)
310                 res = 0;
311         LOCAL_USER_REMOVE(u);
312         return res;
313 }
314
315
316 int unload_module(void)
317 {
318         int res;
319         
320         res = ast_unregister_application(app3);
321         res |= ast_unregister_application(app2);
322         res |= ast_unregister_application(app);
323
324         STANDARD_HANGUP_LOCALUSERS;
325
326         return res;
327 }
328
329 int load_module(void)
330 {
331         int res;
332         
333         res = ast_register_application(app, osplookup_exec, synopsis, descrip);
334         res |= ast_register_application(app2, ospnext_exec, synopsis2, descrip2);
335         res |= ast_register_application(app3, ospfinished_exec, synopsis3, descrip3);
336         
337         return res;
338 }
339
340 int reload(void)
341 {
342         return 0;
343 }
344
345
346 char *description(void)
347 {
348         return tdesc;
349 }
350
351 int usecount(void)
352 {
353         int res;
354         STANDARD_USECOUNT(res);
355         return res;
356 }
357
358 char *key()
359 {
360         return ASTERISK_GPL_KEY;
361 }
362