44a2a23b3ece75d70710da12b0454b0d914c90f2
[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, Digium
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 #include <pthread.h>
31
32 static char *tdesc = "Check if channel is available";
33
34 static char *app = "ChanIsAvail";
35
36 static char *synopsis = "Check if channel is available";
37
38 static char *descrip = 
39 "  ChanIsAvail(Technology/resource[&Technology2/resource2...]): \n"
40 "Checks is any of the requested channels are available.  If none\n"
41 "of the requested channels are available the new priority will\n"
42 "be n+101 (unless such a priority does not exist, in which case\n"
43 "ChanIsAvail will return -1.  If any of the requested channels\n"
44 "are available, the next priority will be n+1, the channel variable\n"
45 "${AVAILCHAN} will be set to the name of the available channel and\n"
46 "the ChanIsAvail app will return 0.\n";
47
48 STANDARD_LOCAL_USER;
49
50 LOCAL_USER_DECL;
51
52 static int chanavail_exec(struct ast_channel *chan, void *data)
53 {
54         int res=-1;
55         struct localuser *u;
56         char info[256], *peers, *tech, *number, *rest, *cur;
57         struct ast_channel *tempchan;
58
59         if (!data) {
60                 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
61                 return -1;
62         }
63         LOCAL_USER_ADD(u);
64
65         strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
66         peers = info;
67         if (peers) {
68                 cur = peers;
69                 do {
70                         /* remember where to start next time */
71                         rest = strchr(cur, '&');
72                         if (rest) {
73                                 *rest = 0;
74                                 rest++;
75                         }
76                         tech = cur;
77                         number = strchr(tech, '/');
78                         if (!number) {
79                                 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
80                                 return -1;
81                         }
82                         *number = '\0';
83                         number++;
84                         if ((tempchan = ast_request(tech, chan->nativeformats, number))) {
85                                         pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
86                                         ast_hangup(tempchan);
87                                         tempchan = NULL;
88                                         res = 1;
89                                         break;
90                         }
91                         cur = rest;
92                 } while (cur);
93         }
94
95         if (res < 1) {
96                 pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
97                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
98                         chan->priority+=100;
99                 else
100                         return -1;
101         }
102
103         LOCAL_USER_REMOVE(u);
104         return 0;
105 }
106
107 int unload_module(void)
108 {
109         STANDARD_HANGUP_LOCALUSERS;
110         return ast_unregister_application(app);
111 }
112
113 int load_module(void)
114 {
115         return ast_register_application(app, chanavail_exec, synopsis, descrip);
116 }
117
118 char *description(void)
119 {
120         return tdesc;
121 }
122
123 int usecount(void)
124 {
125         int res;
126         STANDARD_USECOUNT(res);
127         return res;
128 }
129
130 char *key()
131 {
132         return ASTERISK_GPL_KEY;
133 }