Add ListAllVoicemailUsers manager command. (issue #8112 reported by Tony Zhao)
[asterisk/asterisk.git] / apps / app_zapbarge.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  * Special thanks to comphealth.com for sponsoring this
9  * GPL application.
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2. See the LICENSE file
19  * at the top of the source tree.
20  */
21
22 /*! \file
23  *
24  * \brief Zap Barge support
25  *
26  * \author Mark Spencer <markster@digium.com>
27  *
28  * \note Special thanks to comphealth.com for sponsoring this
29  * GPL application.
30  * 
31  * \ingroup applications
32  */
33
34 /*** MODULEINFO
35         <depend>zaptel</depend>
36  ***/
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include "asterisk/zapata.h"
48
49 #include "asterisk/lock.h"
50 #include "asterisk/file.h"
51 #include "asterisk/logger.h"
52 #include "asterisk/channel.h"
53 #include "asterisk/pbx.h"
54 #include "asterisk/module.h"
55 #include "asterisk/config.h"
56 #include "asterisk/app.h"
57 #include "asterisk/options.h"
58 #include "asterisk/cli.h"
59 #include "asterisk/say.h"
60 #include "asterisk/utils.h"
61
62 static char *app = "ZapBarge";
63
64 static char *synopsis = "Barge in (monitor) Zap channel";
65
66 static char *descrip = 
67 "  ZapBarge([channel]): Barges in on a specified zap\n"
68 "channel or prompts if one is not specified.  Returns\n"
69 "-1 when caller user hangs up and is independent of the\n"
70 "state of the channel being monitored.";
71
72
73 #define CONF_SIZE 160
74
75 static int careful_write(int fd, unsigned char *data, int len)
76 {
77         int res;
78         while(len) {
79                 res = write(fd, data, len);
80                 if (res < 1) {
81                         if (errno != EAGAIN) {
82                                 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
83                                 return -1;
84                         } else
85                                 return 0;
86                 }
87                 len -= res;
88                 data += res;
89         }
90         return 0;
91 }
92
93 static int conf_run(struct ast_channel *chan, int confno, int confflags)
94 {
95         int fd;
96         struct zt_confinfo ztc;
97         struct ast_frame *f;
98         struct ast_channel *c;
99         struct ast_frame fr;
100         int outfd;
101         int ms;
102         int nfds;
103         int res;
104         int flags;
105         int retryzap;
106         int origfd;
107         int ret = -1;
108
109         ZT_BUFFERINFO bi;
110         char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
111         char *buf = __buf + AST_FRIENDLY_OFFSET;
112
113         /* Set it into U-law mode (write) */
114         if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
115                 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
116                 goto outrun;
117         }
118
119         /* Set it into U-law mode (read) */
120         if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
121                 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
122                 goto outrun;
123         }
124         ast_indicate(chan, -1);
125         retryzap = strcasecmp(chan->tech->type, "Zap");
126 zapretry:
127         origfd = chan->fds[0];
128         if (retryzap) {
129                 fd = open("/dev/zap/pseudo", O_RDWR);
130                 if (fd < 0) {
131                         ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
132                         goto outrun;
133                 }
134                 /* Make non-blocking */
135                 flags = fcntl(fd, F_GETFL);
136                 if (flags < 0) {
137                         ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
138                         close(fd);
139                         goto outrun;
140                 }
141                 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
142                         ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
143                         close(fd);
144                         goto outrun;
145                 }
146                 /* Setup buffering information */
147                 memset(&bi, 0, sizeof(bi));
148                 bi.bufsize = CONF_SIZE;
149                 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
150                 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
151                 bi.numbufs = 4;
152                 if (ioctl(fd, ZT_SET_BUFINFO, &bi)) {
153                         ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
154                         close(fd);
155                         goto outrun;
156                 }
157                 nfds = 1;
158         } else {
159                 /* XXX Make sure we're not running on a pseudo channel XXX */
160                 fd = chan->fds[0];
161                 nfds = 0;
162         }
163         memset(&ztc, 0, sizeof(ztc));
164         /* Check to see if we're in a conference... */
165         ztc.chan = 0;   
166         if (ioctl(fd, ZT_GETCONF, &ztc)) {
167                 ast_log(LOG_WARNING, "Error getting conference\n");
168                 close(fd);
169                 goto outrun;
170         }
171         if (ztc.confmode) {
172                 /* Whoa, already in a conference...  Retry... */
173                 if (!retryzap) {
174                         if (option_debug)
175                                 ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
176                         retryzap = 1;
177                         goto zapretry;
178                 }
179         }
180         memset(&ztc, 0, sizeof(ztc));
181         /* Add us to the conference */
182         ztc.chan = 0;   
183         ztc.confno = confno;
184         ztc.confmode = ZT_CONF_MONITORBOTH;
185
186         if (ioctl(fd, ZT_SETCONF, &ztc)) {
187                 ast_log(LOG_WARNING, "Error setting conference\n");
188                 close(fd);
189                 goto outrun;
190         }
191         if (option_debug)
192                 ast_log(LOG_DEBUG, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
193
194         for(;;) {
195                 outfd = -1;
196                 ms = -1;
197                 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
198                 if (c) {
199                         if (c->fds[0] != origfd) {
200                                 if (retryzap) {
201                                         /* Kill old pseudo */
202                                         close(fd);
203                                 }
204                                 if (option_debug)
205                                         ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
206                                 retryzap = 0;
207                                 goto zapretry;
208                         }
209                         f = ast_read(c);
210                         if (!f) 
211                                 break;
212                         if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
213                                 ret = 0;
214                                 ast_frfree(f);
215                                 break;
216                         } else if (fd != chan->fds[0]) {
217                                 if (f->frametype == AST_FRAME_VOICE) {
218                                         if (f->subclass == AST_FORMAT_ULAW) {
219                                                 /* Carefully write */
220                                                 careful_write(fd, f->data, f->datalen);
221                                         } else
222                                                 ast_log(LOG_WARNING, "Huh?  Got a non-ulaw (%d) frame in the conference\n", f->subclass);
223                                 }
224                         }
225                         ast_frfree(f);
226                 } else if (outfd > -1) {
227                         res = read(outfd, buf, CONF_SIZE);
228                         if (res > 0) {
229                                 memset(&fr, 0, sizeof(fr));
230                                 fr.frametype = AST_FRAME_VOICE;
231                                 fr.subclass = AST_FORMAT_ULAW;
232                                 fr.datalen = res;
233                                 fr.samples = res;
234                                 fr.data = buf;
235                                 fr.offset = AST_FRIENDLY_OFFSET;
236                                 if (ast_write(chan, &fr) < 0) {
237                                         ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
238                                         /* break; */
239                                 }
240                         } else 
241                                 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
242                 }
243         }
244         if (fd != chan->fds[0])
245                 close(fd);
246         else {
247                 /* Take out of conference */
248                 /* Add us to the conference */
249                 ztc.chan = 0;   
250                 ztc.confno = 0;
251                 ztc.confmode = 0;
252                 if (ioctl(fd, ZT_SETCONF, &ztc)) {
253                         ast_log(LOG_WARNING, "Error setting conference\n");
254                 }
255         }
256
257 outrun:
258
259         return ret;
260 }
261
262 static int conf_exec(struct ast_channel *chan, void *data)
263 {
264         int res=-1;
265         struct ast_module_user *u;
266         int retrycnt = 0;
267         int confflags = 0;
268         int confno = 0;
269         char confstr[80] = "";
270
271         u = ast_module_user_add(chan);
272         
273         if (!ast_strlen_zero(data)) {
274                 if ((sscanf(data, "Zap/%d", &confno) != 1) &&
275                     (sscanf(data, "%d", &confno) != 1)) {
276                         ast_log(LOG_WARNING, "ZapBarge Argument (if specified) must be a channel number, not '%s'\n", (char *)data);
277                         ast_module_user_remove(u);
278                         return 0;
279                 }
280         }
281         
282         if (chan->_state != AST_STATE_UP)
283                 ast_answer(chan);
284
285         while(!confno && (++retrycnt < 4)) {
286                 /* Prompt user for conference number */
287                 confstr[0] = '\0';
288                 res = ast_app_getdata(chan, "conf-getchannel",confstr, sizeof(confstr) - 1, 0);
289                 if (res <0) goto out;
290                 if (sscanf(confstr, "%d", &confno) != 1)
291                         confno = 0;
292         }
293         if (confno) {
294                 /* XXX Should prompt user for pin if pin is required XXX */
295                 /* Run the conference */
296                 res = conf_run(chan, confno, confflags);
297         }
298 out:
299         /* Do the conference */
300         ast_module_user_remove(u);
301         return res;
302 }
303
304 static int unload_module(void)
305 {
306         int res;
307         
308         res = ast_unregister_application(app);
309         
310         ast_module_user_hangup_all();
311
312         return res;     
313 }
314
315 static int load_module(void)
316 {
317         return ast_register_application(app, conf_exec, synopsis, descrip);
318 }
319
320 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Barge in on Zap channel application");