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