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