Can't use the same buffer to snprintf that we're grabbing
[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 be\n"
42 "n+101 (unless such a priority does not exist or on error, in which\n"
43 "case 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.  ${AVAILORIGCHAN} is\n"
47 "the canonical channel name that was used to create the channel.\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;
56         struct localuser *u;
57         char info[512], tmp[512], *peers, *tech, *number, *rest, *cur;
58         struct ast_channel *tempchan;
59
60         if (!data) {
61                 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
62                 return -1;
63         }
64         LOCAL_USER_ADD(u);
65
66         strncpy(info, (char *)data, sizeof(info)-1);
67         peers = info;
68         if (peers) {
69                 cur = peers;
70                 do {
71                         /* remember where to start next time */
72                         rest = strchr(cur, '&');
73                         if (rest) {
74                                 *rest = 0;
75                                 rest++;
76                         }
77                         tech = cur;
78                         number = strchr(tech, '/');
79                         if (!number) {
80                                 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
81                                 return -1;
82                         }
83                         *number = '\0';
84                         number++;
85                         if ((tempchan = ast_request(tech, chan->nativeformats, number))) {
86                                         pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
87                                         /* Store the originally used channel too */
88                                         snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
89                                         pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
90                                         ast_hangup(tempchan);
91                                         tempchan = NULL;
92                                         res = 1;
93                                         break;
94                         }
95                         cur = rest;
96                 } while (cur);
97         }
98
99         if (res < 1) {
100                 pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
101                 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
102                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
103                         chan->priority+=100;
104                 else
105                         return -1;
106         }
107
108         LOCAL_USER_REMOVE(u);
109         return 0;
110 }
111
112 int unload_module(void)
113 {
114         STANDARD_HANGUP_LOCALUSERS;
115         return ast_unregister_application(app);
116 }
117
118 int load_module(void)
119 {
120         return ast_register_application(app, chanavail_exec, synopsis, descrip);
121 }
122
123 char *description(void)
124 {
125         return tdesc;
126 }
127
128 int usecount(void)
129 {
130         int res;
131         STANDARD_USECOUNT(res);
132         return res;
133 }
134
135 char *key()
136 {
137         return ASTERISK_GPL_KEY;
138 }