Channel availability updates (bug #2829 with mods)
[asterisk/asterisk.git] / apps / app_chanisavail.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Check if Channel is Available
5  * 
6  * Copyright (C) 2003-2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  * James Golovich <james@gnuinter.net>
10  *
11  * This program is free software, distributed under the terms of
12  * the GNU General Public License
13  *
14  */
15
16 #include <asterisk/lock.h>
17 #include <asterisk/file.h>
18 #include <asterisk/logger.h>
19 #include <asterisk/channel.h>
20 #include <asterisk/pbx.h>
21 #include <asterisk/module.h>
22 #include <asterisk/app.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <sys/ioctl.h>
29
30 static char *tdesc = "Check if channel is available";
31
32 static char *app = "ChanIsAvail";
33
34 static char *synopsis = "Check if channel is available";
35
36 static char *descrip = 
37 "  ChanIsAvail(Technology/resource[&Technology2/resource2...][|option]): \n"
38 "Checks is any of the requested channels are available.  If none\n"
39 "of the requested channels are available the new priority will be\n"
40 "n+101 (unless such a priority does not exist or on error, in which\n"
41 "case ChanIsAvail will return -1).  If any of the requested channels\n"
42 "are available, the next priority will be n+1, the channel variable\n"
43 "${AVAILCHAN} will be set to the name of the available channel and\n"
44 "the ChanIsAvail app will return 0.  ${AVAILORIGCHAN} is\n"
45 "the canonical channel name that was used to create the channel.\n"
46 "If the option 's' is specified (state), will consider channel unavailable\n"
47 "when the channel is in use at all, even if it can take another call.\n";
48
49 STANDARD_LOCAL_USER;
50
51 LOCAL_USER_DECL;
52
53 static int chanavail_exec(struct ast_channel *chan, void *data)
54 {
55         int res=-1, inuse=-1, option_state=0;
56         int status;
57         struct localuser *u;
58         char info[512], tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur, *options, *stringp;
59         struct ast_channel *tempchan;
60
61         if (!data) {
62                 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
63                 return -1;
64         }
65         LOCAL_USER_ADD(u);
66
67         strncpy(info, (char *)data, sizeof(info)-1);
68         stringp = info;
69         strsep(&stringp, "|");
70         options = strsep(&stringp, "|");
71         if (options && *options == 's');
72                 option_state = 1;
73         peers = info;
74         if (peers) {
75                 cur = peers;
76                 do {
77                         /* remember where to start next time */
78                         rest = strchr(cur, '&');
79                         if (rest) {
80                                 *rest = 0;
81                                 rest++;
82                         }
83                         tech = cur;
84                         number = strchr(tech, '/');
85                         if (!number) {
86                                 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
87                                 return -1;
88                         }
89                         *number = '\0';
90                         number++;
91                         
92                         if (option_state) {
93                                 /* If the pbx says in use then don't bother trying further.
94                                    This is to permit testing if someone's on a call, even if the 
95                                    channel can permit more calls (ie callwaiting, sip calls, etc).  */
96                                
97                                 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
98                                 status = inuse = ast_device_state(trychan);
99                         }
100                         if ((inuse < 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
101                                         pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
102                                         /* Store the originally used channel too */
103                                         snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
104                                         pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
105                                         snprintf(tmp, sizeof(tmp), "%d", status);
106                                         pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
107                                         ast_hangup(tempchan);
108                                         tempchan = NULL;
109                                         res = 1;
110                                         break;
111                         } else {
112                                 snprintf(tmp, sizeof(tmp), "%d", status);
113                                 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
114                         }
115                         cur = rest;
116                 } while (cur);
117         }
118         if (res < 1) {
119                 pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
120                 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
121                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
122                         chan->priority+=100;
123                 else
124                         return -1;
125         }
126
127         LOCAL_USER_REMOVE(u);
128         return 0;
129 }
130
131 int unload_module(void)
132 {
133         STANDARD_HANGUP_LOCALUSERS;
134         return ast_unregister_application(app);
135 }
136
137 int load_module(void)
138 {
139         return ast_register_application(app, chanavail_exec, synopsis, descrip);
140 }
141
142 char *description(void)
143 {
144         return tdesc;
145 }
146
147 int usecount(void)
148 {
149         int res;
150         STANDARD_USECOUNT(res);
151         return res;
152 }
153
154 char *key()
155 {
156         return ASTERISK_GPL_KEY;
157 }