Fix crash in chanspy (bug #5332)
[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         int count = 0;
375
376         /* If our status has changed, then the channel we're spying on is gone....
377            DON'T TOUCH IT!!!  RUN AWAY!!! */
378         if (spy->status != CHANSPY_RUNNING)
379                 return;
380
381         ast_mutex_lock(&chan->lock);
382         for(cptr=chan->spiers; cptr; cptr=cptr->next) {
383                 if (cptr == spy) {
384                         if (prev) {
385                                 prev->next = cptr->next;
386                                 cptr->next = NULL;
387                         } else
388                                 chan->spiers = NULL;
389                 }
390                 prev = cptr;
391         }
392         ast_mutex_unlock(&chan->lock);
393
394 }
395
396 /* Map 'volume' levels from -4 through +4 into
397    decibel (dB) settings for channel drivers
398 */
399 static signed char volfactor_map[] = {
400         -24,
401         -18,
402         -12,
403         -6,
404         0,
405         6,
406         12,
407         18,
408         24,
409 };
410
411 /* attempt to set the desired gain adjustment via the channel driver;
412    if successful, clear it out of the csth structure so the
413    generator will not attempt to do the adjustment itself
414 */
415 static void set_volume(struct ast_channel *chan, struct chanspy_translation_helper *csth)
416 {
417         signed char volume_adjust = volfactor_map[csth->volfactor + 4];
418
419         if (!ast_channel_setoption(chan, AST_OPTION_TXGAIN, &volume_adjust, sizeof(volume_adjust), 0)) {
420                 csth->volfactor = 0;
421         }
422 }
423
424 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd) 
425 {
426         struct chanspy_translation_helper csth;
427         int running = 1, res = 0, x = 0;
428         char inp[24];
429         char *name=NULL;
430         struct ast_frame *f;
431
432         if (chan && !ast_check_hangup(chan) && spyee && !ast_check_hangup(spyee)) {
433                 memset(inp, 0, sizeof(inp));
434                 name = ast_strdupa(spyee->name);
435                 if (option_verbose >= 2)
436                         ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name);
437
438                 memset(&csth, 0, sizeof(csth));
439                 csth.spy.status = CHANSPY_RUNNING;
440                 ast_mutex_init(&csth.spy.lock);
441                 csth.volfactor = *volfactor;
442                 set_volume(chan, &csth);
443                 
444                 if (fd) {
445                         csth.fd = fd;
446                 }
447                 start_spying(spyee, chan, &csth.spy);
448                 ast_activate_generator(chan, &spygen, &csth);
449
450                 while (csth.spy.status == CHANSPY_RUNNING &&
451                        chan && !ast_check_hangup(chan) &&
452                        spyee &&
453                        !ast_check_hangup(spyee) &&
454                        running == 1 &&
455                        (res = ast_waitfor(chan, -1) > -1)) {
456                         if ((f = ast_read(chan))) {
457                                 res = 0;
458                                 if (f->frametype == AST_FRAME_DTMF) {
459                                         res = f->subclass;
460                                 }
461                                 ast_frfree(f);
462                                 if (!res) {
463                                         continue;
464                                 }
465                         } else {
466                                 break;
467                         }
468                         if (x == sizeof(inp)) {
469                                 x = 0;
470                         }
471                         if (res < 0) {
472                                 running = -1;
473                         }
474                         if (res == 0) {
475                                 continue;
476                         } else if (res == '*') {
477                                 running = 0; 
478                         } else if (res == '#') {
479                                 if (!ast_strlen_zero(inp)) {
480                                         running = x ? atoi(inp) : -1;
481                                         break;
482                                 } else {
483                                         (*volfactor)++;
484                                         if (*volfactor > 4) {
485                                                 *volfactor = -4;
486                                         }
487                                         if (option_verbose > 2) {
488                                                 ast_verbose(VERBOSE_PREFIX_3 "Setting spy volume on %s to %d\n", chan->name, *volfactor);
489                                         }
490                                         csth.volfactor = *volfactor;
491                                         set_volume(chan, &csth);
492                                 }
493                         } else if (res >= 48 && res <= 57) {
494                                 inp[x++] = res;
495                         }
496                 }
497                 ast_deactivate_generator(chan);
498                 stop_spying(spyee, &csth.spy);
499
500                 if (option_verbose >= 2) {
501                         ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name);
502                 }
503                 ast_flush_spy_queue(&csth.spy);
504         } else {
505                 running = 0;
506         }
507         ast_mutex_destroy(&csth.spy.lock);
508         return running;
509 }
510
511 static int chanspy_exec(struct ast_channel *chan, void *data)
512 {
513         struct localuser *u;
514         struct ast_channel *peer=NULL, *prev=NULL;
515         char name[AST_NAME_STRLEN],
516                 peer_name[AST_NAME_STRLEN + 5],
517                 *args,
518                 *ptr = NULL,
519                 *options = NULL,
520                 *spec = NULL,
521                 *argv[5],
522                 *mygroup = NULL,
523                 *recbase = NULL;
524         int res = -1,
525                 volfactor = 0,
526                 silent = 0,
527                 argc = 0,
528                 bronly = 0,
529                 chosen = 0,
530                 count=0,
531                 waitms = 100,
532                 num = 0,
533                 oldrf = 0,
534                 oldwf = 0,
535                 fd = 0;
536         struct ast_flags flags;
537         signed char zero_volume = 0;
538
539         if (!(args = ast_strdupa((char *)data))) {
540                 ast_log(LOG_ERROR, "Out of memory!\n");
541                 return -1;
542         }
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                 return -1;
549         }
550         
551         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
552                 ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
553                 return -1;
554         }
555
556         LOCAL_USER_ADD(u);
557         ast_answer(chan);
558
559         ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */
560
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         STANDARD_HANGUP_LOCALUSERS;
714         return ast_unregister_application(app);
715 }
716
717 int load_module(void)
718 {
719         return ast_register_application(app, chanspy_exec, synopsis, desc);
720 }
721
722 char *description(void)
723 {
724         return (char *) synopsis;
725 }
726
727 int usecount(void)
728 {
729         int res;
730         STANDARD_USECOUNT(res);
731         return res;
732 }
733
734 char *key()
735 {
736         return ASTERISK_GPL_KEY;
737 }