Doxygen documentation update from oej (issue #5505)
[asterisk/asterisk.git] / apps / app_chanspy.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005 Anthony Minessale II (anthmct@yahoo.com)
5  *
6  * Disclaimed to Digium
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  * \brief ChanSpy: Listen in on any channel.
21  * 
22  */
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/features.h"
37 #include "asterisk/options.h"
38 #include "asterisk/slinfactory.h"
39 #include "asterisk/app.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/say.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/module.h"
45 #include "asterisk/lock.h"
46
47 AST_MUTEX_DEFINE_STATIC(modlock);
48
49 #define AST_NAME_STRLEN 256
50 #define ALL_DONE(u, ret) LOCAL_USER_REMOVE(u); return ret;
51 #define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0
52
53 static const char *synopsis = "Tap into any type of asterisk channel and listen to audio";
54 static const char *app = "ChanSpy";
55 static const char *desc = "   Chanspy([<scanspec>][|<options>])\n\n"
56 "Valid Options:\n"
57 " - q: quiet, don't announce channels beep, etc.\n"
58 " - b: bridged, only spy on channels involved in a bridged call.\n"
59 " - v([-4..4]): adjust the initial volume. (negative is quieter)\n"
60 " - g(grp): enforce group.  Match only calls where their ${SPYGROUP} is 'grp'.\n"
61 " - r[(basename)]: Record session to monitor spool dir (with optional basename, default is 'chanspy')\n\n"
62 "If <scanspec> is specified, only channel names *beginning* with that string will be scanned.\n"
63 "('all' or an empty string are also both valid <scanspec>)\n\n"
64 "While Spying:\n\n"
65 "Dialing # cycles the volume level.\n"
66 "Dialing * will stop spying and look for another channel to spy on.\n"
67 "Dialing a series of digits followed by # builds a channel name to append to <scanspec>\n"
68 "(e.g. run Chanspy(Agent) and dial 1234# while spying to jump to channel Agent/1234)\n\n"
69 "";
70
71 #define OPTION_QUIET     (1 << 0)       /* Quiet, no announcement */
72 #define OPTION_BRIDGED   (1 << 1)       /* Only look at bridged calls */
73 #define OPTION_VOLUME    (1 << 2)       /* Specify initial volume */
74 #define OPTION_GROUP     (1 << 3)   /* Only look at channels in group */
75 #define OPTION_RECORD    (1 << 4)   /* Record */
76
77 AST_DECLARE_OPTIONS(chanspy_opts,{
78         ['q'] = { OPTION_QUIET },
79         ['b'] = { OPTION_BRIDGED },
80         ['v'] = { OPTION_VOLUME, 1 },
81         ['g'] = { OPTION_GROUP, 2 },
82         ['r'] = { OPTION_RECORD, 3 },
83 });
84
85 STANDARD_LOCAL_USER;
86 LOCAL_USER_DECL;
87
88 struct chanspy_translation_helper {
89         /* spy data */
90         struct ast_channel_spy spy;
91         int volfactor;
92         int fd;
93         struct ast_slinfactory slinfactory[2];
94 };
95
96 /* Prototypes */
97 static struct ast_channel *local_get_channel_begin_name(char *name);
98 static struct ast_channel *local_channel_walk(struct ast_channel *chan);
99 static void spy_release(struct ast_channel *chan, void *data);
100 static void *spy_alloc(struct ast_channel *chan, void *params);
101 static struct ast_frame *spy_queue_shift(struct ast_channel_spy *spy, int qnum);
102 static void ast_flush_spy_queue(struct ast_channel_spy *spy);
103 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples);
104 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy);
105 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy);
106 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd);
107 static int chanspy_exec(struct ast_channel *chan, void *data);
108
109
110 #if 0
111 static struct ast_channel *local_get_channel_by_name(char *name) 
112 {
113         struct ast_channel *ret;
114         ast_mutex_lock(&modlock);
115         if ((ret = ast_get_channel_by_name_locked(name))) {
116                 ast_mutex_unlock(&ret->lock);
117         }
118         ast_mutex_unlock(&modlock);
119
120         return ret;
121 }
122 #endif
123
124 static struct ast_channel *local_channel_walk(struct ast_channel *chan) 
125 {
126         struct ast_channel *ret;
127         ast_mutex_lock(&modlock);       
128         if ((ret = ast_channel_walk_locked(chan))) {
129                 ast_mutex_unlock(&ret->lock);
130         }
131         ast_mutex_unlock(&modlock);                     
132         return ret;
133 }
134
135 static struct ast_channel *local_get_channel_begin_name(char *name) 
136 {
137         struct ast_channel *chan, *ret = NULL;
138         ast_mutex_lock(&modlock);
139         chan = local_channel_walk(NULL);
140         while (chan) {
141                 if (!strncmp(chan->name, name, strlen(name))) {
142                         ret = chan;
143                         break;
144                 }
145                 chan = local_channel_walk(chan);
146         }
147         ast_mutex_unlock(&modlock);
148         
149         return ret;
150 }
151
152
153 static void spy_release(struct ast_channel *chan, void *data) 
154 {
155         struct chanspy_translation_helper *csth = data;
156
157         ast_slinfactory_destroy(&csth->slinfactory[0]);
158         ast_slinfactory_destroy(&csth->slinfactory[1]);
159
160         return;
161 }
162
163 static void *spy_alloc(struct ast_channel *chan, void *params) 
164 {
165         struct chanspy_translation_helper *csth = params;
166         ast_slinfactory_init(&csth->slinfactory[0]);
167         ast_slinfactory_init(&csth->slinfactory[1]);
168         return params;
169 }
170
171 static struct ast_frame *spy_queue_shift(struct ast_channel_spy *spy, int qnum) 
172 {
173         struct ast_frame *f;
174         
175         if (qnum < 0 || qnum > 1)
176                 return NULL;
177
178         f = spy->queue[qnum];
179         if (f) {
180                 spy->queue[qnum] = f->next;
181                 return f;
182         }
183         return NULL;
184 }
185
186
187 static void ast_flush_spy_queue(struct ast_channel_spy *spy) 
188 {
189         struct ast_frame *f=NULL;
190         int x = 0;
191         ast_mutex_lock(&spy->lock);
192         for(x=0;x<2;x++) {
193                 f = NULL;
194                 while((f = spy_queue_shift(spy, x))) 
195                         ast_frfree(f);
196         }
197         ast_mutex_unlock(&spy->lock);
198 }
199
200
201 #if 0
202 static int extract_audio(short *buf, size_t len, struct ast_trans_pvt *trans, struct ast_frame *fr, int *maxsamp)
203 {
204         struct ast_frame *f;
205         int size, retlen = 0;
206         
207         if (trans) {
208                 if ((f = ast_translate(trans, fr, 0))) {
209                         size = (f->datalen > len) ? len : f->datalen;
210                         memcpy(buf, f->data, size);
211                         retlen = f->datalen;
212                         ast_frfree(f);
213                 } else {
214                         /* your guess is as good as mine why this will happen but it seems to only happen on iax and appears harmless */
215                         ast_log(LOG_DEBUG, "Failed to translate frame from %s\n", ast_getformatname(fr->subclass));
216                 }
217         } else {
218                 size = (fr->datalen > len) ? len : fr->datalen;
219                 memcpy(buf, fr->data, size);
220                 retlen = fr->datalen;
221         }
222
223         if (retlen > 0 && (size = retlen / 2)) {
224                 if (size > *maxsamp) {
225                         *maxsamp = size;
226                 }
227         }
228         
229         return retlen;
230 }
231
232
233 static int spy_queue_ready(struct ast_channel_spy *spy)
234 {
235         int res = 0;
236
237         ast_mutex_lock(&spy->lock);
238         if (spy->status == CHANSPY_RUNNING) {
239                 res = (spy->queue[0] && spy->queue[1]) ? 1 : 0;
240         } else {
241                 res = (spy->queue[0] || spy->queue[1]) ? 1 : -1;
242         }
243         ast_mutex_unlock(&spy->lock);
244         return res;
245 }
246 #endif
247
248 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples) 
249 {
250
251         struct chanspy_translation_helper *csth = data;
252         struct ast_frame frame, *f;
253         int len0 = 0, len1 = 0, samp0 = 0, samp1 = 0, x, vf, maxsamp;
254         short buf0[1280], buf1[1280], buf[1280];
255                 
256         if (csth->spy.status == CHANSPY_DONE) {
257                 /* Channel is already gone more than likely */
258                 return -1;
259         }
260
261         ast_mutex_lock(&csth->spy.lock);
262         while((f = csth->spy.queue[0])) {
263                 csth->spy.queue[0] = f->next;
264                 ast_slinfactory_feed(&csth->slinfactory[0], f);
265                 ast_frfree(f);
266         }
267         ast_mutex_unlock(&csth->spy.lock);
268         ast_mutex_lock(&csth->spy.lock);
269         while((f = csth->spy.queue[1])) {
270                 csth->spy.queue[1] = f->next;
271                 ast_slinfactory_feed(&csth->slinfactory[1], f);
272                 ast_frfree(f);
273         }
274         ast_mutex_unlock(&csth->spy.lock);
275                 
276         if (csth->slinfactory[0].size < len || csth->slinfactory[1].size < len) {
277                 return 0;
278         }
279                 
280         if ((len0 = ast_slinfactory_read(&csth->slinfactory[0], buf0, len))) {
281                 samp0 = len0 / 2;
282         } 
283         if ((len1 = ast_slinfactory_read(&csth->slinfactory[1], buf1, len))) {
284                 samp1 = len1 / 2;
285         }
286
287         maxsamp = (samp0 > samp1) ? samp0 : samp1;
288         vf = get_volfactor(csth->volfactor);
289                 
290         for(x=0; x < maxsamp; x++) {
291                 if (vf < 0) {
292                         if (samp0) {
293                                 buf0[x] /= abs(vf);
294                         }
295                         if (samp1) {
296                                 buf1[x] /= abs(vf);
297                         }
298                 } else if (vf > 0) {
299                         if (samp0) {
300                                 buf0[x] *= vf;
301                         }
302                         if (samp1) {
303                                 buf1[x] *= vf;
304                         }
305                 }
306                 if (samp0 && samp1) {
307                         if (x < samp0 && x < samp1) {
308                                 buf[x] = buf0[x] + buf1[x];
309                         } else if (x < samp0) {
310                                 buf[x] = buf0[x];
311                         } else if (x < samp1) {
312                                 buf[x] = buf1[x];
313                         }
314                 } else if (x < samp0) {
315                         buf[x] = buf0[x];
316                 } else if (x < samp1) {
317                         buf[x] = buf1[x];
318                 }
319         }
320                 
321         memset(&frame, 0, sizeof(frame));
322         frame.frametype = AST_FRAME_VOICE;
323         frame.subclass = AST_FORMAT_SLINEAR;
324         frame.data = buf;
325         frame.samples = x;
326         frame.datalen = x * 2;
327
328         if (ast_write(chan, &frame)) {
329                 return -1;
330         }
331
332         if (csth->fd) {
333                 write(csth->fd, buf1, len1);
334         }
335
336         return 0;
337 }
338
339
340 static struct ast_generator spygen = {
341         alloc: spy_alloc, 
342         release: spy_release, 
343         generate: spy_generate, 
344 };
345
346 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy) 
347 {
348
349         struct ast_channel_spy *cptr=NULL;
350         struct ast_channel *peer;
351
352
353         ast_log(LOG_WARNING, "Attaching %s to %s\n", spychan->name, chan->name);
354
355
356         ast_mutex_lock(&chan->lock);
357         if (chan->spiers) {
358                 for(cptr=chan->spiers;cptr && cptr->next;cptr=cptr->next);
359                 cptr->next = spy;
360         } else {
361                 chan->spiers = spy;
362         }
363         ast_mutex_unlock(&chan->lock);
364         if ( ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan))) {
365                 ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);  
366         }
367
368 }
369
370 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy) 
371 {
372         struct ast_channel_spy *cptr=NULL, *prev=NULL;
373
374         /* If our status has changed, then the channel we're spying on is gone....
375            DON'T TOUCH IT!!!  RUN AWAY!!! */
376         if (spy->status != CHANSPY_RUNNING)
377                 return;
378
379         ast_mutex_lock(&chan->lock);
380         for(cptr=chan->spiers; cptr; cptr=cptr->next) {
381                 if (cptr == spy) {
382                         if (prev) {
383                                 prev->next = cptr->next;
384                                 cptr->next = NULL;
385                         } else
386                                 chan->spiers = NULL;
387                 }
388                 prev = cptr;
389         }
390         ast_mutex_unlock(&chan->lock);
391
392 }
393
394 /* Map 'volume' levels from -4 through +4 into
395    decibel (dB) settings for channel drivers
396 */
397 static signed char volfactor_map[] = {
398         -24,
399         -18,
400         -12,
401         -6,
402         0,
403         6,
404         12,
405         18,
406         24,
407 };
408
409 /* attempt to set the desired gain adjustment via the channel driver;
410    if successful, clear it out of the csth structure so the
411    generator will not attempt to do the adjustment itself
412 */
413 static void set_volume(struct ast_channel *chan, struct chanspy_translation_helper *csth)
414 {
415         signed char volume_adjust = volfactor_map[csth->volfactor + 4];
416
417         if (!ast_channel_setoption(chan, AST_OPTION_TXGAIN, &volume_adjust, sizeof(volume_adjust), 0)) {
418                 csth->volfactor = 0;
419         }
420 }
421
422 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd) 
423 {
424         struct chanspy_translation_helper csth;
425         int running = 1, res = 0, x = 0;
426         char inp[24];
427         char *name=NULL;
428         struct ast_frame *f;
429
430         if (chan && !ast_check_hangup(chan) && spyee && !ast_check_hangup(spyee)) {
431                 memset(inp, 0, sizeof(inp));
432                 name = ast_strdupa(spyee->name);
433                 if (option_verbose >= 2)
434                         ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name);
435
436                 memset(&csth, 0, sizeof(csth));
437                 csth.spy.status = CHANSPY_RUNNING;
438                 ast_mutex_init(&csth.spy.lock);
439                 csth.volfactor = *volfactor;
440                 set_volume(chan, &csth);
441                 
442                 if (fd) {
443                         csth.fd = fd;
444                 }
445                 start_spying(spyee, chan, &csth.spy);
446                 ast_activate_generator(chan, &spygen, &csth);
447
448                 while (csth.spy.status == CHANSPY_RUNNING &&
449                        chan && !ast_check_hangup(chan) &&
450                        spyee &&
451                        !ast_check_hangup(spyee) &&
452                        running == 1 &&
453                        (res = ast_waitfor(chan, -1) > -1)) {
454                         if ((f = ast_read(chan))) {
455                                 res = 0;
456                                 if (f->frametype == AST_FRAME_DTMF) {
457                                         res = f->subclass;
458                                 }
459                                 ast_frfree(f);
460                                 if (!res) {
461                                         continue;
462                                 }
463                         } else {
464                                 break;
465                         }
466                         if (x == sizeof(inp)) {
467                                 x = 0;
468                         }
469                         if (res < 0) {
470                                 running = -1;
471                         }
472                         if (res == 0) {
473                                 continue;
474                         } else if (res == '*') {
475                                 running = 0; 
476                         } else if (res == '#') {
477                                 if (!ast_strlen_zero(inp)) {
478                                         running = x ? atoi(inp) : -1;
479                                         break;
480                                 } else {
481                                         (*volfactor)++;
482                                         if (*volfactor > 4) {
483                                                 *volfactor = -4;
484                                         }
485                                         if (option_verbose > 2) {
486                                                 ast_verbose(VERBOSE_PREFIX_3 "Setting spy volume on %s to %d\n", chan->name, *volfactor);
487                                         }
488                                         csth.volfactor = *volfactor;
489                                         set_volume(chan, &csth);
490                                 }
491                         } else if (res >= 48 && res <= 57) {
492                                 inp[x++] = res;
493                         }
494                 }
495                 ast_deactivate_generator(chan);
496                 stop_spying(spyee, &csth.spy);
497
498                 if (option_verbose >= 2) {
499                         ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name);
500                 }
501                 ast_flush_spy_queue(&csth.spy);
502         } else {
503                 running = 0;
504         }
505         ast_mutex_destroy(&csth.spy.lock);
506         return running;
507 }
508
509 static int chanspy_exec(struct ast_channel *chan, void *data)
510 {
511         struct localuser *u;
512         struct ast_channel *peer=NULL, *prev=NULL;
513         char name[AST_NAME_STRLEN],
514                 peer_name[AST_NAME_STRLEN + 5],
515                 *args,
516                 *ptr = NULL,
517                 *options = NULL,
518                 *spec = NULL,
519                 *argv[5],
520                 *mygroup = NULL,
521                 *recbase = NULL;
522         int res = -1,
523                 volfactor = 0,
524                 silent = 0,
525                 argc = 0,
526                 bronly = 0,
527                 chosen = 0,
528                 count=0,
529                 waitms = 100,
530                 num = 0,
531                 oldrf = 0,
532                 oldwf = 0,
533                 fd = 0;
534         struct ast_flags flags;
535         signed char zero_volume = 0;
536
537         if (!(args = ast_strdupa((char *)data))) {
538                 ast_log(LOG_ERROR, "Out of memory!\n");
539                 return -1;
540         }
541
542         LOCAL_USER_ADD(u);
543
544         oldrf = chan->readformat;
545         oldwf = chan->writeformat;
546         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
547                 ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
548                 LOCAL_USER_REMOVE(u);
549                 return -1;
550         }
551         
552         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
553                 ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
554                 LOCAL_USER_REMOVE(u);
555                 return -1;
556         }
557
558         ast_answer(chan);
559
560         ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */
561
562         if ((argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(argv[0])))) {
563                 spec = argv[0];
564                 if ( argc > 1) {
565                         options = argv[1];
566                 }
567                 if (ast_strlen_zero(spec) || !strcmp(spec, "all")) {
568                         spec = NULL;
569                 }
570         }
571         
572         if (options) {
573                 char *opts[3];
574                 ast_parseoptions(chanspy_opts, &flags, opts, options);
575                 if (ast_test_flag(&flags, OPTION_GROUP)) {
576                         mygroup = opts[1];
577                 }
578                 if (ast_test_flag(&flags, OPTION_RECORD)) {
579                         if (!(recbase = opts[2])) {
580                                 recbase = "chanspy";
581                         }
582                 }
583                 silent = ast_test_flag(&flags, OPTION_QUIET);
584                 bronly = ast_test_flag(&flags, OPTION_BRIDGED);
585                 if (ast_test_flag(&flags, OPTION_VOLUME) && opts[1]) {
586                         int vol;
587
588                         if ((sscanf(opts[0], "%d", &vol) != 1) || (vol > 4) || (vol < -4))
589                                 ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n");
590                         else
591                                 volfactor = vol;
592                         }
593         }
594
595         if (recbase) {
596                 char filename[512];
597                 snprintf(filename,sizeof(filename),"%s/%s.%ld.raw",ast_config_AST_MONITOR_DIR, recbase, time(NULL));
598                 if ((fd = open(filename, O_CREAT | O_WRONLY, O_TRUNC)) <= 0) {
599                         ast_log(LOG_WARNING, "Cannot open %s for recording\n", filename);
600                         fd = 0;
601                 }
602         }
603
604         for(;;) {
605                 if (!silent) {
606                         res = ast_streamfile(chan, "beep", chan->language);
607                         if (!res)
608                                 res = ast_waitstream(chan, "");
609                         if (res < 0) {
610                                 ast_clear_flag(chan, AST_FLAG_SPYING);
611                                 break;
612                         }
613                 }
614
615                 count = 0;
616                 res = ast_waitfordigit(chan, waitms);
617                 if (res < 0) {
618                         ast_clear_flag(chan, AST_FLAG_SPYING);
619                         break;
620                 }
621                                 
622                 peer = local_channel_walk(NULL);
623                 prev=NULL;
624                 while(peer) {
625                         if (peer != chan) {
626                                 char *group = NULL;
627                                 int igrp = 1;
628
629                                 if (peer == prev && !chosen) {
630                                         break;
631                                 }
632                                 chosen = 0;
633                                 group = pbx_builtin_getvar_helper(peer, "SPYGROUP");
634                                 if (mygroup) {
635                                         if (!group || strcmp(mygroup, group)) {
636                                                 igrp = 0;
637                                         }
638                                 }
639                                 
640                                 if (igrp && (!spec || ((strlen(spec) < strlen(peer->name) &&
641                                                         !strncasecmp(peer->name, spec, strlen(spec)))))) {
642                                         if (peer && (!bronly || ast_bridged_channel(peer)) &&
643                                             !ast_check_hangup(peer) && !ast_test_flag(peer, AST_FLAG_SPYING)) {
644                                                 int x = 0;
645                                                 strncpy(peer_name, "spy-", 5);
646                                                 strncpy(peer_name + strlen(peer_name), peer->name, AST_NAME_STRLEN);
647                                                 ptr = strchr(peer_name, '/');
648                                                 *ptr = '\0';
649                                                 ptr++;
650                                                 for (x = 0 ; x < strlen(peer_name) ; x++) {
651                                                         if (peer_name[x] == '/') {
652                                                                 break;
653                                                         }
654                                                         peer_name[x] = tolower(peer_name[x]);
655                                                 }
656
657                                                 if (!silent) {
658                                                         if (ast_fileexists(peer_name, NULL, NULL) != -1) {
659                                                                 res = ast_streamfile(chan, peer_name, chan->language);
660                                                                 if (!res)
661                                                                         res = ast_waitstream(chan, "");
662                                                                 if (res)
663                                                                         break;
664                                                         } else
665                                                                 res = ast_say_character_str(chan, peer_name, "", chan->language);
666                                                         if ((num=atoi(ptr))) 
667                                                                 ast_say_digits(chan, atoi(ptr), "", chan->language);
668                                                 }
669                                                 count++;
670                                                 prev = peer;
671                                                 res = channel_spy(chan, peer, &volfactor, fd);
672                                                 if (res == -1) {
673                                                         break;
674                                                 } else if (res > 1 && spec) {
675                                                         snprintf(name, AST_NAME_STRLEN, "%s/%d", spec, res);
676                                                         if ((peer = local_get_channel_begin_name(name))) {
677                                                                 chosen = 1;
678                                                         }
679                                                         continue;
680                                                 }
681                                         }
682                                 }
683                         }
684                         if ((peer = local_channel_walk(peer)) == NULL) {
685                                 break;
686                         }
687                 }
688                 waitms = count ? 100 : 5000;
689         }
690         
691
692         if (fd > 0) {
693                 close(fd);
694         }
695
696         if (oldrf && ast_set_read_format(chan, oldrf) < 0) {
697                 ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
698         }
699         
700         if (oldwf && ast_set_write_format(chan, oldwf) < 0) {
701                 ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
702         }
703
704         ast_clear_flag(chan, AST_FLAG_SPYING);
705
706         ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
707
708         ALL_DONE(u, res);
709 }
710
711 int unload_module(void)
712 {
713         int res;
714
715         res = ast_unregister_application(app);
716
717         STANDARD_HANGUP_LOCALUSERS;
718
719         return res;
720 }
721
722 int load_module(void)
723 {
724         return ast_register_application(app, chanspy_exec, synopsis, desc);
725 }
726
727 char *description(void)
728 {
729         return (char *) synopsis;
730 }
731
732 int usecount(void)
733 {
734         int res;
735         STANDARD_USECOUNT(res);
736         return res;
737 }
738
739 char *key()
740 {
741         return ASTERISK_GPL_KEY;
742 }