Merged revisions 68922 via svnmerge from
[asterisk/asterisk.git] / apps / app_chanisavail.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 * James Golovich <james@gnuinter.net>
8 *
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20 /*! \file
21  * 
22  * \brief Check if Channel is Available
23  * 
24  * \author Mark Spencer <markster@digium.com>
25  * \author James Golovich <james@gnuinter.net>
26
27  * \ingroup applications
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <sys/ioctl.h>
40
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/app.h"
48 #include "asterisk/devicestate.h"
49 #include "asterisk/options.h"
50
51 static char *app = "ChanIsAvail";
52
53 static char *synopsis = "Check channel availability";
54
55 static char *descrip = 
56 "  ChanIsAvail(Technology/resource[&Technology2/resource2...][|options]): \n"
57 "This application will check to see if any of the specified channels are\n"
58 "available. The following variables will be set by this application:\n"
59 "  ${AVAILCHAN}     - the name of the available channel, if one exists\n"
60 "  ${AVAILORIGCHAN} - the canonical channel name that was used to create the channel\n"
61 "  ${AVAILSTATUS}   - the status code for the available channel\n"
62 "  Options:\n"
63 "    s - Consider the channel unavailable if the channel is in use at all\n"
64 "    t - Simply checks if specified channels exist in the channel list\n"
65 "        (implies option s) \n"
66 "    j - Support jumping to priority n+101 if no channel is available\n";
67
68
69 static int chanavail_exec(struct ast_channel *chan, void *data)
70 {
71         int res=-1, inuse=-1, option_state=0, priority_jump=0, string_compare=0;
72         int status;
73         struct ast_module_user *u;
74         char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
75         struct ast_channel *tempchan;
76         AST_DECLARE_APP_ARGS(args,
77                 AST_APP_ARG(reqchans);
78                 AST_APP_ARG(options);
79         );
80
81         if (ast_strlen_zero(data)) {
82                 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
83                 return -1;
84         }
85
86         u = ast_module_user_add(chan);
87
88         info = ast_strdupa(data); 
89
90         AST_STANDARD_APP_ARGS(args, info);
91
92         if (args.options) {
93                 if (strchr(args.options, 's'))
94                         option_state = 1;
95                 if (strchr(args.options, 't'))
96                         string_compare = 1;
97                 if (strchr(args.options, 'j'))
98                         priority_jump = 1;
99         }
100         peers = args.reqchans;
101         if (peers) {
102                 cur = peers;
103                 do {
104                         /* remember where to start next time */
105                         rest = strchr(cur, '&');
106                         if (rest) {
107                                 *rest = 0;
108                                 rest++;
109                         }
110                         tech = cur;
111                         number = strchr(tech, '/');
112                         if (!number) {
113                                 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
114                                 ast_module_user_remove(u);
115                                 return -1;
116                         }
117                         *number = '\0';
118                         number++;
119                         
120                         if (string_compare) {
121                                 /* ast_parse_device_state checks for "SIP/1234" as a channel name. 
122                                    ast_device_state will ask the SIP driver for the channel state. */
123
124                                 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
125                                 status = inuse = ast_parse_device_state(trychan);
126                         } else if (option_state) {
127                                 /* If the pbx says in use then don't bother trying further.
128                                    This is to permit testing if someone's on a call, even if the 
129                                    channel can permit more calls (ie callwaiting, sip calls, etc).  */
130                                
131                                 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
132                                 status = inuse = ast_device_state(trychan);
133                         }
134                         if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
135                                         pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
136                                         /* Store the originally used channel too */
137                                         snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
138                                         pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
139                                         snprintf(tmp, sizeof(tmp), "%d", status);
140                                         pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
141                                         ast_hangup(tempchan);
142                                         tempchan = NULL;
143                                         res = 1;
144                                         break;
145                         } else {
146                                 snprintf(tmp, sizeof(tmp), "%d", status);
147                                 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
148                         }
149                         cur = rest;
150                 } while (cur);
151         }
152         if (res < 1) {
153                 pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
154                 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
155                 if (priority_jump || ast_opt_priority_jumping) {
156                         if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
157                                 ast_module_user_remove(u);
158                                 return -1;
159                         }
160                 }
161         }
162
163         ast_module_user_remove(u);
164         return 0;
165 }
166
167 static int unload_module(void)
168 {
169         int res = 0;
170
171         res = ast_unregister_application(app);
172
173         ast_module_user_hangup_all();
174         
175         return res;
176 }
177
178 static int load_module(void)
179 {
180         return ast_register_application(app, chanavail_exec, synopsis, descrip);
181 }
182
183 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");