Finishing touches and addition of record flag to ChanSpy <pay up jets\!>
[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 #define AST_MONITOR_DIR AST_SPOOL_DIR "/monitor"
15
16 #include <../asterisk.h>
17 #include <asterisk/file.h>
18 #include <asterisk/logger.h>
19 #include <asterisk/channel.h>
20 #include <asterisk/features.h>
21 #include <asterisk/options.h>
22 #include <asterisk/app.h>
23 #include <asterisk/utils.h>
24 #include <asterisk/say.h>
25 #include <asterisk/pbx.h>
26 #include <asterisk/translate.h>
27 #include <asterisk/module.h>
28 #include <asterisk/lock.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 AST_MUTEX_DEFINE_STATIC(modlock);
35
36 #define ast_fit_in_short(in) (in < -32768 ? -32768 : in > 32767 ? 32767 : in)
37 #define AST_NAME_STRLEN 256
38 #define ALL_DONE(u, ret) LOCAL_USER_REMOVE(u); return ret;
39 #define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0
40 #define minmax(x,y) x ? (x > y) ? y : ((x < (y * -1)) ? (y * -1) : x) : 0
41
42
43 static char *synopsis = "Tap into any type of asterisk channel and listen to audio";
44 static char *app = "ChanSpy";
45 static 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
82         /* read frame */
83         int fmt0;
84         short *buf0;
85         int len0;
86         struct ast_trans_pvt *trans0;
87
88         /* write frame */
89         int fmt1;
90         struct ast_trans_pvt *trans1;
91         short *buf1;
92         int len1;
93
94         /* muxed frame */
95         struct ast_frame frame;
96         short *buf;
97         int len;
98         
99         int samples;
100         int rsamples;
101         int volfactor;
102         int fd;
103 };
104
105 /* Prototypes */
106 static struct ast_channel *local_get_channel_by_name(char *name);
107 static struct ast_channel *local_get_channel_begin_name(char *name);
108 static struct ast_channel *local_channel_walk(struct ast_channel *chan);
109 static void spy_release(struct ast_channel *chan, void *data);
110 static void *spy_alloc(struct ast_channel *chan, void *params);
111 static struct ast_frame *spy_queue_shift(struct ast_channel_spy *spy, int qnum);
112 static void ast_flush_spy_queue(struct ast_channel_spy *spy);
113 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples);
114 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy);
115 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy);
116 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd);
117 static int chanspy_exec(struct ast_channel *chan, void *data);
118
119
120 static struct ast_channel *local_get_channel_by_name(char *name) 
121 {
122         struct ast_channel *ret;
123         ast_mutex_lock(&modlock);
124         if ((ret = ast_get_channel_by_name_locked(name))) {
125                 ast_mutex_unlock(&ret->lock);
126         }
127         ast_mutex_unlock(&modlock);
128
129         return ret;
130 }
131
132 static struct ast_channel *local_channel_walk(struct ast_channel *chan) 
133 {
134         struct ast_channel *ret;
135         ast_mutex_lock(&modlock);       
136         if ((ret = ast_channel_walk_locked(chan))) {
137                 ast_mutex_unlock(&ret->lock);
138         }
139         ast_mutex_unlock(&modlock);                     
140         return ret;
141 }
142
143 static struct ast_channel *local_get_channel_begin_name(char *name) 
144 {
145         struct ast_channel *chan, *ret = NULL;
146         ast_mutex_lock(&modlock);
147         chan = local_channel_walk(NULL);
148         while (chan) {
149                 if (!strncmp(chan->name, name, strlen(name))) {
150                         ret = chan;
151                         break;
152                 }
153                 chan = local_channel_walk(chan);
154         }
155         ast_mutex_unlock(&modlock);
156         
157         return ret;
158 }
159
160
161 static void spy_release(struct ast_channel *chan, void *data) 
162 {
163         struct chanspy_translation_helper *csth = data;
164
165
166         if (csth->trans0) {
167                 ast_translator_free_path(csth->trans0);
168                 csth->trans0 = NULL;
169         }
170         if (csth->trans1) {
171                 ast_translator_free_path(csth->trans1);
172                 csth->trans1 = NULL;
173         }
174
175         if (csth->buf0) {
176                 free(csth->buf0);
177                 csth->buf0 = NULL;
178         }
179         if (csth->buf1) {
180                 free(csth->buf1);
181                 csth->buf1 = NULL;
182         }
183         if (csth->buf) {
184                 free(csth->buf);
185                 csth->buf = NULL;
186         }
187         return;
188 }
189
190 static void *spy_alloc(struct ast_channel *chan, void *params) 
191 {
192         return params;
193 }
194
195 static struct ast_frame *spy_queue_shift(struct ast_channel_spy *spy, int qnum) 
196 {
197         struct ast_frame *f;
198         
199         if (qnum < 0 || qnum > 1)
200                 return NULL;
201
202         f = spy->queue[qnum];
203         if (f) {
204                 spy->queue[qnum] = f->next;
205                 return f;
206         }
207         return NULL;
208 }
209
210
211 static void ast_flush_spy_queue(struct ast_channel_spy *spy) 
212 {
213         struct ast_frame *f=NULL;
214         int x = 0;
215         ast_mutex_lock(&spy->lock);
216         for(x=0;x<2;x++) {
217                 f = NULL;
218                 while((f = spy_queue_shift(spy, x))) 
219                         ast_frfree(f);
220         }
221         ast_mutex_unlock(&spy->lock);
222 }
223
224 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples) 
225 {
226         struct ast_frame *f, *f0, *f1;
227         int x, vf, dlen, maxsamp, loops;
228         struct chanspy_translation_helper *csth = data;
229
230         if (csth->rsamples < csth->samples) {
231                 csth->rsamples += samples;
232                 return 0;
233         } 
234         csth->rsamples += samples;
235         loops = 0;
236         do {
237                 loops++;
238                 f = f0 = f1 = NULL;
239                 x = vf = dlen = maxsamp = 0;
240                 if (csth->rsamples == csth->samples) {
241                         csth->rsamples = csth->samples = 0;
242                 }
243
244                 ast_mutex_lock(&csth->spy.lock);
245                 f0 = spy_queue_shift(&csth->spy, 0);
246                 f1 = spy_queue_shift(&csth->spy, 1);
247                 ast_mutex_unlock(&csth->spy.lock);
248
249                 if (csth->spy.status == CHANSPY_DONE) {
250                         return -1;
251                 }
252         
253                 if (!f0 && !f1) {
254                         return 0;
255                 }
256
257                 if (f0 && csth->fmt0 && csth->fmt0 != f0->subclass) {
258                         ast_translator_free_path(csth->trans0);
259                         csth->trans0 = NULL;
260                         csth->fmt0 = f0->subclass;
261                 }
262
263                 if (f1 && csth->fmt1 && csth->fmt1 != f1->subclass) {
264                         ast_translator_free_path(csth->trans1);
265                         csth->trans1 = NULL;
266                         csth->fmt1 = f1->subclass;
267                 }
268         
269                 if (!csth->fmt0 && f0) {
270                         csth->fmt0 = f0->subclass;
271                 }
272
273                 if (!csth->fmt1 && f1) {
274                         csth->fmt1 = f1->subclass;
275                 }
276
277                 if (csth->fmt0 && csth->fmt0 != AST_FORMAT_SLINEAR && !csth->trans0) {
278                         if ((csth->trans0 = ast_translator_build_path(AST_FORMAT_SLINEAR, csth->fmt0)) == NULL) {
279                                 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(csth->fmt0));
280                                 csth->spy.status = CHANSPY_DONE;
281                                 return -1;
282                         }
283                 }
284                 if (csth->fmt1 && csth->fmt1 != AST_FORMAT_SLINEAR && !csth->trans1) {
285                         if ((csth->trans1 = ast_translator_build_path(AST_FORMAT_SLINEAR, csth->fmt1)) == NULL) {
286                                 ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(csth->fmt1));
287                                 csth->spy.status = CHANSPY_DONE;
288                                 return -1;
289                         }
290                 }
291         
292                 if (f0) {
293                         if (csth->trans0) {
294                                 if ((f = ast_translate(csth->trans0, f0, 0))) {
295                                         if (csth->len0 < f->datalen) {
296                                                 if (!csth->len0) {
297                                                         if (!(csth->buf0 = malloc(f->datalen * 2))) {
298                                                                 csth->spy.status = CHANSPY_DONE;
299                                                                 return -1;
300                                                         }
301                                                 } else {
302                                                         if (!realloc(csth->buf0, f->datalen * 2)) {
303                                                                 csth->spy.status = CHANSPY_DONE;
304                                                                 return -1;
305                                                         }
306                                                 }
307                                                 csth->len0 = f->datalen;
308                                         }
309                                         memcpy(csth->buf0, f->data, f->datalen);
310                                         maxsamp = f->samples;
311                                         ast_frfree(f);
312                                 } else {
313                                         return 0;
314                                 }
315                         } else {
316                                 memcpy(csth->buf0, f0->data, f0->datalen);
317                                 maxsamp = f0->samples;
318                         }
319                 }
320         
321                 if (f1) {
322                         if (csth->trans1) {
323                                 if ((f = ast_translate(csth->trans1, f1, 0))) {
324                                         if (csth->len1 < f->datalen) {
325                                                 if (!csth->len1) {
326                                                         if (!(csth->buf1 = malloc(f->datalen))) {
327                                                                 csth->spy.status = CHANSPY_DONE;
328                                                                 return -1;
329                                                         }
330                                                 } else {
331                                                         if (!realloc(csth->buf1, f->datalen)) {
332                                                                 csth->spy.status = CHANSPY_DONE;
333                                                                 return -1;
334                                                         }
335                                                 }
336                                                 csth->len1 = f->datalen;
337                                         }
338                                         memcpy(csth->buf1, f->data, f->datalen);
339                                         if (f->samples > maxsamp) {
340                                                 maxsamp = f->samples;
341                                         }
342                                         ast_frfree(f);
343                                 
344                                 } else {
345                                         return 0;
346                                 }
347                         } else {
348                                 memcpy(csth->buf1, f1->data, f1->datalen);
349                                 if (f1->samples > maxsamp) {
350                                         maxsamp = f1->samples;
351                                 }
352                         }
353                 }
354
355                 vf = get_volfactor(csth->volfactor);
356                 vf = minmax(vf, 16);
357
358                 dlen = (csth->len0 > csth->len1) ? csth->len0 : csth->len1;
359
360                 if (csth->len < dlen) {
361                         if (!csth->len) {
362                                 if (!(csth->buf = malloc(dlen*2))) {
363                                         csth->spy.status = CHANSPY_DONE;
364                                         return -1;
365                                 }
366                         } else {
367                                 if (!realloc(csth->buf, dlen * 2)) {
368                                         csth->spy.status = CHANSPY_DONE;
369                                         return -1;
370                                 }
371                         }
372                         csth->len = dlen;
373                 }
374
375                 for(x=0; x < maxsamp; x++) {
376                         if (vf < 0) {
377                                 if (f0) {
378                                         csth->buf0[x] /= abs(vf);
379                                 }
380                                 if (f1) {
381                                         csth->buf1[x] /= abs(vf);
382                                 }
383                         } else if (vf > 0) {
384                                 if (f0) {
385                                         csth->buf0[x] *= vf;
386                                 }
387                                 if (f1) {
388                                         csth->buf1[x] *= vf;
389                                 }
390                         }
391                         if (f0 && f1) {
392                                 if (x < csth->len0 && x < csth->len1) {
393                                         csth->buf[x] = ast_fit_in_short(csth->buf0[x] + csth->buf1[x]);
394                                 } else if (x < csth->len0) {
395                                         csth->buf[x] = csth->buf0[x];
396                                 } else if (x < csth->len1) {
397                                         csth->buf[x] = csth->buf1[x];
398                                 }
399                         } else if (f0 && x < csth->len0) {
400                                 csth->buf[x] = csth->buf0[x];
401                         } else if (f1 && x < csth->len1) {
402                                 csth->buf[x] = csth->buf1[x];
403                         }
404                 }
405
406                 csth->frame.data = csth->buf;
407                 csth->frame.samples = maxsamp;
408                 csth->frame.datalen = csth->frame.samples * 2;
409                 csth->samples += csth->frame.samples;
410                 
411                 if (ast_write(chan, &csth->frame)) {
412                         csth->spy.status = CHANSPY_DONE;
413                         return -1;
414                 }
415                 if (csth->fd) {
416                         write(csth->fd, csth->frame.data, csth->frame.datalen);
417                 }
418
419                 if (f0) {
420                         ast_frfree(f0);
421                 }
422                 if (f1) {
423                         ast_frfree(f1);
424                 }
425
426                 if (loops > 10) {
427                         ast_log(LOG_WARNING, "Too Many Loops Bailing Out....");
428                         break;
429                 }
430         } while (csth->samples <  csth->rsamples);
431
432         return 0;
433 }
434
435 static struct ast_generator spygen = {
436     alloc: spy_alloc, 
437     release: spy_release, 
438     generate: spy_generate, 
439 };
440
441 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy) 
442 {
443
444         struct ast_channel_spy *cptr=NULL;
445         struct ast_channel *peer;
446
447
448         ast_log(LOG_WARNING, "Attaching %s to %s\n", spychan->name, chan->name);
449
450
451         ast_mutex_lock(&chan->lock);
452         if (chan->spiers) {
453                 for(cptr=chan->spiers;cptr && cptr->next;cptr=cptr->next);
454                 cptr->next = spy;
455         } else {
456                 chan->spiers = spy;
457         }
458         ast_mutex_unlock(&chan->lock);
459         if ( ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan))) {
460                 ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);  
461         }
462
463 }
464
465 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy) 
466 {
467         struct ast_channel_spy *cptr=NULL, *prev=NULL;
468         int count = 0;
469
470         while(ast_mutex_trylock(&chan->lock)) {
471                 /* if its locked already it's almost surely hanging up and we are too late 
472                    we can safely remove the head pointer if it points at us without needing a lock.
473                    since everybody spying will be in the same boat whomever is pointing at the head
474                    will surely erase it which is all we really need since it's a linked list of
475                    staticly declared structs that belong to each spy.
476                 */
477                 if (chan->spiers == spy) {
478                         chan->spiers = NULL;
479                         return;
480                 }
481                 count++;
482                 if (count > 10) {
483                         return;
484                 }
485                 sched_yield();
486         }
487
488         for(cptr=chan->spiers; cptr; cptr=cptr->next) {
489                 if (cptr == spy) {
490                         if (prev) {
491                                 prev->next = cptr->next;
492                                 cptr->next = NULL;
493                         } else
494                                 chan->spiers = NULL;
495                 }
496                 prev = cptr;
497         }
498         ast_mutex_unlock(&chan->lock);
499
500 }
501
502 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd) 
503 {
504         struct chanspy_translation_helper csth;
505         int running = 1, res = 0, x = 0;
506         char inp[24];
507         char *name=NULL;
508         struct ast_frame *f;
509
510         if (chan && !ast_check_hangup(chan) && spyee && !ast_check_hangup(spyee)) {
511                 memset(inp, 0, sizeof(inp));
512                 name = ast_strdupa(spyee->name);
513                 if (option_verbose >= 2)
514                         ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name);
515
516                 memset(&csth, 0, sizeof(csth));
517                 csth.spy.status = CHANSPY_RUNNING;
518                 ast_mutex_init(&csth.spy.lock);
519                 csth.volfactor = *volfactor;
520                 csth.frame.frametype = AST_FRAME_VOICE;
521                 csth.frame.subclass = AST_FORMAT_SLINEAR;
522                 csth.frame.datalen = 320;
523                 csth.frame.samples = 160;
524                 if (fd) {
525                         csth.fd = fd;
526                 }
527                 start_spying(spyee, chan, &csth.spy);
528                 ast_activate_generator(chan, &spygen, &csth);
529
530                 while(csth.spy.status == CHANSPY_RUNNING && 
531                           chan && !ast_check_hangup(chan) && 
532                           spyee && 
533                           !ast_check_hangup(spyee) 
534                           && running == 1 && 
535                           (res = ast_waitfor(chan, -1) > -1)) {
536                         if ((f = ast_read(chan))) {
537                                 res = 0;
538                                 if (f->frametype == AST_FRAME_DTMF) {
539                                         res = f->subclass;
540                                 }
541                                 ast_frfree(f);
542                                 if (!res) {
543                                         continue;
544                                 }
545                         } else {
546                                 break;
547                         }
548                         if (x == sizeof(inp)) {
549                                 x = 0;
550                         }
551                         if (res < 0) {
552                                 running = -1;
553                         }
554                         if (res == 0) {
555                                 continue;
556                         } else if (res == '*') {
557                                 running = 0; 
558                         } else if (res == '#') {
559                                 if (!ast_strlen_zero(inp)) {
560                                         running = x ? atoi(inp) : -1;
561                                         break;
562                                 } else {
563                                         csth.volfactor++;
564                                         if (csth.volfactor > 4) {
565                                                 csth.volfactor = -4;
566                                         }
567                                         if (option_verbose > 2) {
568                                                 ast_verbose(VERBOSE_PREFIX_3"Setting spy volume on %s to %d\n", chan->name, csth.volfactor);
569                                         }
570                                         *volfactor = csth.volfactor;
571                                 }
572                         } else if (res >= 48 && res <= 57) {
573                                 inp[x++] = res;
574                         }
575                 }
576                 ast_deactivate_generator(chan);
577                 stop_spying(spyee, &csth.spy);
578
579                 if (option_verbose >= 2) {
580                         ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name);
581                 }
582                 ast_flush_spy_queue(&csth.spy);
583         } else {
584                 running = 0;
585         }
586         ast_mutex_destroy(&csth.spy.lock);
587         return running;
588 }
589
590
591
592 static int chanspy_exec(struct ast_channel *chan, void *data)
593 {
594         struct localuser *u;
595         struct ast_channel *peer=NULL, *prev=NULL;
596         char name[AST_NAME_STRLEN],
597                 peer_name[AST_NAME_STRLEN],
598                 *args,
599                 *ptr = NULL,
600                 *options = NULL,
601                 *spec = NULL,
602                 *argv[5],
603                 *mygroup = NULL,
604                 *recbase = NULL;
605         int res = -1,
606                 volfactor = 0,
607                 silent = 0,
608                 argc = 0,
609                 bronly = 0,
610                 chosen = 0,
611                 count=0,
612                 waitms = 100,
613                 num = 0,
614                 oldrf = 0,
615                 oldwf = 0,
616                 fd = 0;
617         struct ast_flags flags;
618
619
620         if (!(args = ast_strdupa((char *)data))) {
621                 ast_log(LOG_ERROR, "Out of memory!\n");
622                 return -1;
623         }
624
625         oldrf = chan->readformat;
626         oldwf = chan->writeformat;
627         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
628                 ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
629                 return -1;
630         }
631         
632         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
633                 ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
634                 return -1;
635         }
636
637         LOCAL_USER_ADD(u);
638         ast_answer(chan);
639
640         ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */
641
642
643         if ((argc = ast_separate_app_args(args, '|', argv, sizeof(argv) / sizeof(argv[0])))) {
644                 spec = argv[0];
645                 if ( argc > 1) {
646                         options = argv[1];
647                 }
648                 if (ast_strlen_zero(spec) || !strcmp(spec, "all")) {
649                         spec = NULL;
650                 }
651         }
652         
653         if (options) {
654                 char *opts[3];
655                 ast_parseoptions(chanspy_opts, &flags, opts, options);
656                 if (ast_test_flag(&flags, OPTION_GROUP)) {
657                         mygroup = opts[1];
658                 }
659                 if (ast_test_flag(&flags, OPTION_RECORD)) {
660                         if (!(recbase = opts[2])) {
661                                 recbase = "chanspy";
662                         }
663                 }
664                 silent = ast_test_flag(&flags, OPTION_QUIET);
665                 bronly = ast_test_flag(&flags, OPTION_BRIDGED);
666                 if (ast_test_flag(&flags, OPTION_VOLUME) && opts[1]) {
667                         if (sscanf(opts[0], "%d", &volfactor) != 1)
668                                 ast_log(LOG_NOTICE, "volfactor must be a number between -4 and 4\n");
669                         else {
670                                 volfactor = minmax(volfactor, 4);
671                         }
672                 }
673         }
674
675         if (recbase) {
676                 char filename[512];
677                 snprintf(filename,sizeof(filename),"%s/%s.%ld.raw",AST_MONITOR_DIR, recbase, time(NULL));
678                 if ((fd = open(filename, O_CREAT | O_WRONLY, O_TRUNC)) <= 0) {
679                         ast_log(LOG_WARNING, "Cannot open %s for recording\n", filename);
680                         fd = 0;
681                 }
682         }
683
684         for(;;) {
685                 res = ast_streamfile(chan, "beep", chan->language);
686                 if (!res)
687                         res = ast_waitstream(chan, "");
688                 if (res < 0) {
689                         ast_clear_flag(chan, AST_FLAG_SPYING);
690                         break;
691                 }                       
692
693                 count = 0;
694                 res = ast_waitfordigit(chan, waitms);
695                 if (res < 0) {
696                         ast_clear_flag(chan, AST_FLAG_SPYING);
697                         break;
698                 }
699                                 
700                 peer = local_channel_walk(NULL);
701                 prev=NULL;
702                 while(peer) {
703                         if (peer != chan) {
704                                 char *group = NULL;
705                                 int igrp = 1;
706
707                                 if (peer == prev && !chosen) {
708                                         break;
709                                 }
710                                 chosen = 0;
711                                 group = pbx_builtin_getvar_helper(peer, "SPYGROUP");
712                                 if (mygroup) {
713                                         if (!group || strcmp(mygroup, group)) {
714                                                 igrp = 0;
715                                         }
716                                 }
717                                 
718                                 if (igrp && (!spec || ((strlen(spec) < strlen(peer->name) &&
719                                                                            !strncasecmp(peer->name, spec, strlen(spec)))))) {
720                                         if (peer && (!bronly || ast_bridged_channel(peer)) &&
721                                                 !ast_check_hangup(peer) && !ast_test_flag(peer, AST_FLAG_SPYING)) {
722                                                 int x = 0;
723
724                                                 strncpy(peer_name, peer->name, AST_NAME_STRLEN);
725                                                 ptr = strchr(peer_name, '/');
726                                                 *ptr = '\0';
727                                                 ptr++;
728                                                 for (x = 0 ; x < strlen(peer_name) ; x++) {
729                                                         if (peer_name[x] == '/') {
730                                                                 break;
731                                                         }
732                                                         peer_name[x] = tolower(peer_name[x]);
733                                                 }
734
735                                                 if (!silent) {
736                                                         if (ast_fileexists(peer_name, NULL, NULL) != -1) {
737                                                                 res = ast_streamfile(chan, peer_name, chan->language);
738                                                                 if (!res)
739                                                                         res = ast_waitstream(chan, "");
740                                                                 if (res)
741                                                                         break;
742                                                         } else
743                                                                 res = ast_say_character_str(chan, peer_name, "", chan->language);
744                                                         if ((num=atoi(ptr))) 
745                                                                 ast_say_digits(chan, atoi(ptr), "", chan->language);
746                                                 }
747                                                 count++;
748                                                 prev = peer;
749                                                 res = channel_spy(chan, peer, &volfactor, fd);
750                                                 if (res == -1) {
751                                                         break;
752                                                 } else if (res > 1 && spec) {
753                                                         snprintf(name, AST_NAME_STRLEN, "%s/%d", spec, res);
754                                                         if ((peer = local_get_channel_begin_name(name))) {
755                                                                 chosen = 1;
756                                                         }
757                                                         continue;
758                                                 }
759                                         }
760                                 }
761                         }
762                         if ((peer = local_channel_walk(peer)) == NULL) {
763                                 break;
764                         }
765                 }
766                 waitms = count ? 100 : 5000;
767         }
768         
769
770         if (fd > 0) {
771                 close(fd);
772         }
773
774         if (oldrf && ast_set_read_format(chan, oldrf) < 0) {
775                 ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
776         }
777         
778         if (oldwf && ast_set_write_format(chan, oldwf) < 0) {
779                 ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
780         }
781
782         ast_clear_flag(chan, AST_FLAG_SPYING);
783         ALL_DONE(u, res);
784 }
785
786 int unload_module(void)
787 {
788         STANDARD_HANGUP_LOCALUSERS;
789         return ast_unregister_application(app);
790 }
791
792 int load_module(void)
793 {
794         return ast_register_application(app, chanspy_exec, synopsis, desc);
795 }
796
797 char *description(void)
798 {
799         return synopsis;
800 }
801
802 int usecount(void)
803 {
804         int res;
805         STANDARD_USECOUNT(res);
806         return res;
807 }
808
809 char *key()
810 {
811         return ASTERISK_GPL_KEY;
812 }