1c22ddb8666ec67a568090c3ac07a0a2662d0e1d
[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                 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                 csth->spy.status = CHANSPY_DONE;
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         while(ast_mutex_trylock(&chan->lock)) {
377                 /* if its locked already it's almost surely hanging up and we are too late 
378                    we can safely remove the head pointer if it points at us without needing a lock.
379                    since everybody spying will be in the same boat whomever is pointing at the head
380                    will surely erase it which is all we really need since it's a linked list of
381                    staticly declared structs that belong to each spy.
382                 */
383                 if (chan->spiers == spy) {
384                         chan->spiers = NULL;
385                         return;
386                 }
387                 count++;
388                 if (count > 10) {
389                         return;
390                 }
391                 sched_yield();
392         }
393
394         for(cptr=chan->spiers; cptr; cptr=cptr->next) {
395                 if (cptr == spy) {
396                         if (prev) {
397                                 prev->next = cptr->next;
398                                 cptr->next = NULL;
399                         } else
400                                 chan->spiers = NULL;
401                 }
402                 prev = cptr;
403         }
404         ast_mutex_unlock(&chan->lock);
405
406 }
407
408 /* Map 'volume' levels from -4 through +4 into
409    decibel (dB) settings for channel drivers
410 */
411 static signed char volfactor_map[] = {
412         -24,
413         -18,
414         -12,
415         -6,
416         0,
417         6,
418         12,
419         18,
420         24,
421 };
422
423 /* attempt to set the desired gain adjustment via the channel driver;
424    if successful, clear it out of the csth structure so the
425    generator will not attempt to do the adjustment itself
426 */
427 static void set_volume(struct ast_channel *chan, struct chanspy_translation_helper *csth)
428 {
429         signed char volume_adjust = volfactor_map[csth->volfactor + 4];
430
431         if (!ast_channel_setoption(chan, AST_OPTION_TXGAIN, &volume_adjust, sizeof(volume_adjust), 0)) {
432                 csth->volfactor = 0;
433         }
434 }
435
436 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd) 
437 {
438         struct chanspy_translation_helper csth;
439         int running = 1, res = 0, x = 0;
440         char inp[24];
441         char *name=NULL;
442         struct ast_frame *f;
443
444         if (chan && !ast_check_hangup(chan) && spyee && !ast_check_hangup(spyee)) {
445                 memset(inp, 0, sizeof(inp));
446                 name = ast_strdupa(spyee->name);
447                 if (option_verbose >= 2)
448                         ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name);
449
450                 memset(&csth, 0, sizeof(csth));
451                 csth.spy.status = CHANSPY_RUNNING;
452                 ast_mutex_init(&csth.spy.lock);
453                 csth.volfactor = *volfactor;
454                 set_volume(chan, &csth);
455                 
456                 if (fd) {
457                         csth.fd = fd;
458                 }
459                 start_spying(spyee, chan, &csth.spy);
460                 ast_activate_generator(chan, &spygen, &csth);
461
462                 while (csth.spy.status == CHANSPY_RUNNING &&
463                        chan && !ast_check_hangup(chan) &&
464                        spyee &&
465                        !ast_check_hangup(spyee) &&
466                        running == 1 &&
467                        (res = ast_waitfor(chan, -1) > -1)) {
468                         if ((f = ast_read(chan))) {
469                                 res = 0;
470                                 if (f->frametype == AST_FRAME_DTMF) {
471                                         res = f->subclass;
472                                 }
473                                 ast_frfree(f);
474                                 if (!res) {
475                                         continue;
476                                 }
477                         } else {
478                                 break;
479                         }
480                         if (x == sizeof(inp)) {
481                                 x = 0;
482                         }
483                         if (res < 0) {
484                                 running = -1;
485                         }
486                         if (res == 0) {
487                                 continue;
488                         } else if (res == '*') {
489                                 running = 0; 
490                         } else if (res == '#') {
491                                 if (!ast_strlen_zero(inp)) {
492                                         running = x ? atoi(inp) : -1;
493                                         break;
494                                 } else {
495                                         (*volfactor)++;
496                                         if (*volfactor > 4) {
497                                                 *volfactor = -4;
498                                         }
499                                         if (option_verbose > 2) {
500                                                 ast_verbose(VERBOSE_PREFIX_3 "Setting spy volume on %s to %d\n", chan->name, *volfactor);
501                                         }
502                                         csth.volfactor = *volfactor;
503                                         set_volume(chan, &csth);
504                                 }
505                         } else if (res >= 48 && res <= 57) {
506                                 inp[x++] = res;
507                         }
508                 }
509                 ast_deactivate_generator(chan);
510                 stop_spying(spyee, &csth.spy);
511
512                 if (option_verbose >= 2) {
513                         ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name);
514                 }
515                 ast_flush_spy_queue(&csth.spy);
516         } else {
517                 running = 0;
518         }
519         ast_mutex_destroy(&csth.spy.lock);
520         return running;
521 }
522
523 static int chanspy_exec(struct ast_channel *chan, void *data)
524 {
525         struct localuser *u;
526         struct ast_channel *peer=NULL, *prev=NULL;
527         char name[AST_NAME_STRLEN],
528                 peer_name[AST_NAME_STRLEN + 5],
529                 *args,
530                 *ptr = NULL,
531                 *options = NULL,
532                 *spec = NULL,
533                 *argv[5],
534                 *mygroup = NULL,
535                 *recbase = NULL;
536         int res = -1,
537                 volfactor = 0,
538                 silent = 0,
539                 argc = 0,
540                 bronly = 0,
541                 chosen = 0,
542                 count=0,
543                 waitms = 100,
544                 num = 0,
545                 oldrf = 0,
546                 oldwf = 0,
547                 fd = 0;
548         struct ast_flags flags;
549         signed char zero_volume = 0;
550
551         if (!(args = ast_strdupa((char *)data))) {
552                 ast_log(LOG_ERROR, "Out of memory!\n");
553                 return -1;
554         }
555
556         oldrf = chan->readformat;
557         oldwf = chan->writeformat;
558         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
559                 ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
560                 return -1;
561         }
562         
563         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
564                 ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
565                 return -1;
566         }
567
568         LOCAL_USER_ADD(u);
569         ast_answer(chan);
570
571         ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */
572
573
574         if ((argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(argv[0])))) {
575                 spec = argv[0];
576                 if ( argc > 1) {
577                         options = argv[1];
578                 }
579                 if (ast_strlen_zero(spec) || !strcmp(spec, "all")) {
580                         spec = NULL;
581                 }
582         }
583         
584         if (options) {
585                 char *opts[3];
586                 ast_parseoptions(chanspy_opts, &flags, opts, options);
587                 if (ast_test_flag(&flags, OPTION_GROUP)) {
588                         mygroup = opts[1];
589                 }
590                 if (ast_test_flag(&flags, OPTION_RECORD)) {
591                         if (!(recbase = opts[2])) {
592                                 recbase = "chanspy";
593                         }
594                 }
595                 silent = ast_test_flag(&flags, OPTION_QUIET);
596                 bronly = ast_test_flag(&flags, OPTION_BRIDGED);
597                 if (ast_test_flag(&flags, OPTION_VOLUME) && opts[1]) {
598                         int vol;
599
600                         if ((sscanf(opts[0], "%d", &vol) != 1) || (vol > 4) || (vol < -4))
601                                 ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n");
602                         else
603                                 volfactor = vol;
604                         }
605         }
606
607         if (recbase) {
608                 char filename[512];
609                 snprintf(filename,sizeof(filename),"%s/%s.%ld.raw",ast_config_AST_MONITOR_DIR, recbase, time(NULL));
610                 if ((fd = open(filename, O_CREAT | O_WRONLY, O_TRUNC)) <= 0) {
611                         ast_log(LOG_WARNING, "Cannot open %s for recording\n", filename);
612                         fd = 0;
613                 }
614         }
615
616         for(;;) {
617                 if (!silent) {
618                         res = ast_streamfile(chan, "beep", chan->language);
619                         if (!res)
620                                 res = ast_waitstream(chan, "");
621                         if (res < 0) {
622                                 ast_clear_flag(chan, AST_FLAG_SPYING);
623                                 break;
624                         }
625                 }
626
627                 count = 0;
628                 res = ast_waitfordigit(chan, waitms);
629                 if (res < 0) {
630                         ast_clear_flag(chan, AST_FLAG_SPYING);
631                         break;
632                 }
633                                 
634                 peer = local_channel_walk(NULL);
635                 prev=NULL;
636                 while(peer) {
637                         if (peer != chan) {
638                                 char *group = NULL;
639                                 int igrp = 1;
640
641                                 if (peer == prev && !chosen) {
642                                         break;
643                                 }
644                                 chosen = 0;
645                                 group = pbx_builtin_getvar_helper(peer, "SPYGROUP");
646                                 if (mygroup) {
647                                         if (!group || strcmp(mygroup, group)) {
648                                                 igrp = 0;
649                                         }
650                                 }
651                                 
652                                 if (igrp && (!spec || ((strlen(spec) < strlen(peer->name) &&
653                                                         !strncasecmp(peer->name, spec, strlen(spec)))))) {
654                                         if (peer && (!bronly || ast_bridged_channel(peer)) &&
655                                             !ast_check_hangup(peer) && !ast_test_flag(peer, AST_FLAG_SPYING)) {
656                                                 int x = 0;
657                                                 strncpy(peer_name, "spy-", 5);
658                                                 strncpy(peer_name + strlen(peer_name), peer->name, AST_NAME_STRLEN);
659                                                 ptr = strchr(peer_name, '/');
660                                                 *ptr = '\0';
661                                                 ptr++;
662                                                 for (x = 0 ; x < strlen(peer_name) ; x++) {
663                                                         if (peer_name[x] == '/') {
664                                                                 break;
665                                                         }
666                                                         peer_name[x] = tolower(peer_name[x]);
667                                                 }
668
669                                                 if (!silent) {
670                                                         if (ast_fileexists(peer_name, NULL, NULL) != -1) {
671                                                                 res = ast_streamfile(chan, peer_name, chan->language);
672                                                                 if (!res)
673                                                                         res = ast_waitstream(chan, "");
674                                                                 if (res)
675                                                                         break;
676                                                         } else
677                                                                 res = ast_say_character_str(chan, peer_name, "", chan->language);
678                                                         if ((num=atoi(ptr))) 
679                                                                 ast_say_digits(chan, atoi(ptr), "", chan->language);
680                                                 }
681                                                 count++;
682                                                 prev = peer;
683                                                 res = channel_spy(chan, peer, &volfactor, fd);
684                                                 if (res == -1) {
685                                                         break;
686                                                 } else if (res > 1 && spec) {
687                                                         snprintf(name, AST_NAME_STRLEN, "%s/%d", spec, res);
688                                                         if ((peer = local_get_channel_begin_name(name))) {
689                                                                 chosen = 1;
690                                                         }
691                                                         continue;
692                                                 }
693                                         }
694                                 }
695                         }
696                         if ((peer = local_channel_walk(peer)) == NULL) {
697                                 break;
698                         }
699                 }
700                 waitms = count ? 100 : 5000;
701         }
702         
703
704         if (fd > 0) {
705                 close(fd);
706         }
707
708         if (oldrf && ast_set_read_format(chan, oldrf) < 0) {
709                 ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
710         }
711         
712         if (oldwf && ast_set_write_format(chan, oldwf) < 0) {
713                 ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
714         }
715
716         ast_clear_flag(chan, AST_FLAG_SPYING);
717
718         ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
719
720         ALL_DONE(u, res);
721 }
722
723 int unload_module(void)
724 {
725         STANDARD_HANGUP_LOCALUSERS;
726         return ast_unregister_application(app);
727 }
728
729 int load_module(void)
730 {
731         return ast_register_application(app, chanspy_exec, synopsis, desc);
732 }
733
734 char *description(void)
735 {
736         return (char *) synopsis;
737 }
738
739 int usecount(void)
740 {
741         int res;
742         STANDARD_USECOUNT(res);
743         return res;
744 }
745
746 char *key()
747 {
748         return ASTERISK_GPL_KEY;
749 }