don't play any sounds when using the quiet option (bug #4348)
[asterisk/asterisk.git] / apps / app_chanspy.c
index a1bf656..292a151 100755 (executable)
  * the GNU General Public License
  */
 
  * the GNU General Public License
  */
 
-#include <asterisk/file.h>
-#include <asterisk/logger.h>
-#include <asterisk/channel.h>
-#include <asterisk/features.h>
-#include <asterisk/options.h>
-#include <asterisk/app.h>
-#include <asterisk/utils.h>
-#include <asterisk/say.h>
-#include <asterisk/pbx.h>
-#include <asterisk/translate.h>
-#include <asterisk/module.h>
-#include <asterisk/lock.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <ctype.h>
 
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <ctype.h>
 
+#include "asterisk.h"
+#include "asterisk/file.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/features.h"
+#include "asterisk/options.h"
+#include "asterisk/app.h"
+#include "asterisk/utils.h"
+#include "asterisk/say.h"
+#include "asterisk/pbx.h"
+#include "asterisk/translate.h"
+#include "asterisk/module.h"
+#include "asterisk/lock.h"
+
+#define AST_MONITOR_DIR AST_SPOOL_DIR "/monitor"
+
 AST_MUTEX_DEFINE_STATIC(modlock);
 
 #define ast_fit_in_short(in) (in < -32768 ? -32768 : in > 32767 ? 32767 : in)
 AST_MUTEX_DEFINE_STATIC(modlock);
 
 #define ast_fit_in_short(in) (in < -32768 ? -32768 : in > 32767 ? 32767 : in)
@@ -35,7 +39,7 @@ AST_MUTEX_DEFINE_STATIC(modlock);
 #define ALL_DONE(u, ret) LOCAL_USER_REMOVE(u); return ret;
 #define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0
 #define minmax(x,y) x ? (x > y) ? y : ((x < (y * -1)) ? (y * -1) : x) : 0
 #define ALL_DONE(u, ret) LOCAL_USER_REMOVE(u); return ret;
 #define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0
 #define minmax(x,y) x ? (x > y) ? y : ((x < (y * -1)) ? (y * -1) : x) : 0
-#define CS_BUFLEN 1024
+
 
 static char *synopsis = "Tap into any type of asterisk channel and listen to audio";
 static char *app = "ChanSpy";
 
 static char *synopsis = "Tap into any type of asterisk channel and listen to audio";
 static char *app = "ChanSpy";
@@ -44,7 +48,8 @@ static char *desc = "   Chanspy([<scanspec>][|<options>])\n\n"
 " - q: quiet, don't announce channels beep, etc.\n"
 " - b: bridged, only spy on channels involved in a bridged call.\n"
 " - v([-4..4]): adjust the initial volume. (negative is quieter)\n"
 " - q: quiet, don't announce channels beep, etc.\n"
 " - b: bridged, only spy on channels involved in a bridged call.\n"
 " - v([-4..4]): adjust the initial volume. (negative is quieter)\n"
-" - g(grp): enforce group.  Match only calls where their ${SPYGROUP} is 'grp'.\n\n"
+" - g(grp): enforce group.  Match only calls where their ${SPYGROUP} is 'grp'.\n"
+" - r[(basename)]: Record session to monitor spool dir (with optional basename, default is 'chanspy')\n\n"
 "If <scanspec> is specified, only channel names *beginning* with that string will be scanned.\n"
 "('all' or an empty string are also both valid <scanspec>)\n\n"
 "While Spying:\n\n"
 "If <scanspec> is specified, only channel names *beginning* with that string will be scanned.\n"
 "('all' or an empty string are also both valid <scanspec>)\n\n"
 "While Spying:\n\n"
@@ -58,12 +63,14 @@ static char *desc = "   Chanspy([<scanspec>][|<options>])\n\n"
 #define OPTION_BRIDGED   (1 << 1)      /* Only look at bridged calls */
 #define OPTION_VOLUME    (1 << 2)      /* Specify initial volume */
 #define OPTION_GROUP     (1 << 3)   /* Only look at channels in group */
 #define OPTION_BRIDGED   (1 << 1)      /* Only look at bridged calls */
 #define OPTION_VOLUME    (1 << 2)      /* Specify initial volume */
 #define OPTION_GROUP     (1 << 3)   /* Only look at channels in group */
+#define OPTION_RECORD    (1 << 4)   /* Record */
 
 AST_DECLARE_OPTIONS(chanspy_opts,{
        ['q'] = { OPTION_QUIET },
        ['b'] = { OPTION_BRIDGED },
        ['v'] = { OPTION_VOLUME, 1 },
        ['g'] = { OPTION_GROUP, 2 },
 
 AST_DECLARE_OPTIONS(chanspy_opts,{
        ['q'] = { OPTION_QUIET },
        ['b'] = { OPTION_BRIDGED },
        ['v'] = { OPTION_VOLUME, 1 },
        ['g'] = { OPTION_GROUP, 2 },
+       ['r'] = { OPTION_RECORD, 3 },
 });
 
 STANDARD_LOCAL_USER;
 });
 
 STANDARD_LOCAL_USER;
@@ -75,24 +82,28 @@ struct chanspy_translation_helper {
 
        /* read frame */
        int fmt0;
 
        /* read frame */
        int fmt0;
-       short buf0[CS_BUFLEN];
+       short *buf0;
+       int len0;
        struct ast_trans_pvt *trans0;
 
        /* write frame */
        int fmt1;
        struct ast_trans_pvt *trans1;
        struct ast_trans_pvt *trans0;
 
        /* write frame */
        int fmt1;
        struct ast_trans_pvt *trans1;
-       short buf1[CS_BUFLEN];
-       
+       short *buf1;
+       int len1;
+
        /* muxed frame */
        struct ast_frame frame;
        /* muxed frame */
        struct ast_frame frame;
-       short buf[CS_BUFLEN];
-
+       short *buf;
+       int len;
+       
+       int samples;
+       int rsamples;
        int volfactor;
        int volfactor;
-
+       int fd;
 };
 
 /* Prototypes */
 };
 
 /* Prototypes */
-static struct ast_channel *local_get_channel_by_name(char *name);
 static struct ast_channel *local_get_channel_begin_name(char *name);
 static struct ast_channel *local_channel_walk(struct ast_channel *chan);
 static void spy_release(struct ast_channel *chan, void *data);
 static struct ast_channel *local_get_channel_begin_name(char *name);
 static struct ast_channel *local_channel_walk(struct ast_channel *chan);
 static void spy_release(struct ast_channel *chan, void *data);
@@ -102,10 +113,11 @@ static void ast_flush_spy_queue(struct ast_channel_spy *spy);
 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples);
 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy);
 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy);
 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples);
 static void start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy);
 static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy);
-static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor);
+static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd);
 static int chanspy_exec(struct ast_channel *chan, void *data);
 
 
 static int chanspy_exec(struct ast_channel *chan, void *data);
 
 
+#if 0
 static struct ast_channel *local_get_channel_by_name(char *name) 
 {
        struct ast_channel *ret;
 static struct ast_channel *local_get_channel_by_name(char *name) 
 {
        struct ast_channel *ret;
@@ -117,6 +129,7 @@ static struct ast_channel *local_get_channel_by_name(char *name)
 
        return ret;
 }
 
        return ret;
 }
+#endif
 
 static struct ast_channel *local_channel_walk(struct ast_channel *chan) 
 {
 
 static struct ast_channel *local_channel_walk(struct ast_channel *chan) 
 {
@@ -150,20 +163,29 @@ static struct ast_channel *local_get_channel_begin_name(char *name)
 static void spy_release(struct ast_channel *chan, void *data) 
 {
        struct chanspy_translation_helper *csth = data;
 static void spy_release(struct ast_channel *chan, void *data) 
 {
        struct chanspy_translation_helper *csth = data;
-       int same=0;
 
 
-       same = (csth->trans0 == csth->trans1) ? 1 : 0;
 
        if (csth->trans0) {
                ast_translator_free_path(csth->trans0);
                csth->trans0 = NULL;
        }
 
        if (csth->trans0) {
                ast_translator_free_path(csth->trans0);
                csth->trans0 = NULL;
        }
-       if (same)
-               return;
        if (csth->trans1) {
                ast_translator_free_path(csth->trans1);
                csth->trans1 = NULL;
        }
        if (csth->trans1) {
                ast_translator_free_path(csth->trans1);
                csth->trans1 = NULL;
        }
+
+       if (csth->buf0) {
+               free(csth->buf0);
+               csth->buf0 = NULL;
+       }
+       if (csth->buf1) {
+               free(csth->buf1);
+               csth->buf1 = NULL;
+       }
+       if (csth->buf) {
+               free(csth->buf);
+               csth->buf = NULL;
+       }
        return;
 }
 
        return;
 }
 
@@ -204,119 +226,238 @@ static void ast_flush_spy_queue(struct ast_channel_spy *spy)
 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples) 
 {
        struct ast_frame *f, *f0, *f1;
 static int spy_generate(struct ast_channel *chan, void *data, int len, int samples) 
 {
        struct ast_frame *f, *f0, *f1;
-       int x = 0, vf = 0;
-
+       int x, vf, dlen, maxsamp, loops;
        struct chanspy_translation_helper *csth = data;
 
        struct chanspy_translation_helper *csth = data;
 
-       ast_mutex_lock(&csth->spy.lock);
-       f0 = spy_queue_shift(&csth->spy, 0);
-       f1 = spy_queue_shift(&csth->spy, 1);
-       ast_mutex_unlock(&csth->spy.lock);
-       
-       if (!f0 && !f1) {
+       if (csth->rsamples < csth->samples) {
+               csth->rsamples += samples;
                return 0;
                return 0;
-       }
+       } 
+       csth->rsamples += samples;
+       loops = 0;
+       do {
+               loops++;
+               f = f0 = f1 = NULL;
+               x = vf = dlen = maxsamp = 0;
+               if (csth->rsamples == csth->samples) {
+                       csth->rsamples = csth->samples = 0;
+               }
 
 
-       if (f0 && csth->fmt0 && csth->fmt0 != f0->subclass) {
-               ast_translator_free_path(csth->trans0);
-        csth->trans0 = NULL;
-               csth->fmt0 = csth->fmt0;
-       }
+               ast_mutex_lock(&csth->spy.lock);
+               f0 = spy_queue_shift(&csth->spy, 0);
+               f1 = spy_queue_shift(&csth->spy, 1);
+               ast_mutex_unlock(&csth->spy.lock);
 
 
-       if (f1 && csth->fmt1 && csth->fmt1 != f1->subclass) {
-               ast_translator_free_path(csth->trans1);
-        csth->trans1 = NULL;
-               csth->fmt1 = csth->fmt1;
-       }
+               if (csth->spy.status == CHANSPY_DONE) {
+                       return -1;
+               }
        
        
-       if (!csth->fmt0 && f0) {
-               csth->fmt0 = f0->subclass;
-       }
+               if (!f0 && !f1) {
+                       return 0;
+               }
 
 
-       if (!csth->fmt1 && f1) {
-               csth->fmt1 = f1->subclass;
-       }
+               if (f0 && csth->fmt0 && csth->fmt0 != f0->subclass) {
+                       ast_translator_free_path(csth->trans0);
+                       csth->trans0 = NULL;
+                       csth->fmt0 = f0->subclass;
+               }
 
 
-       if (csth->fmt0 && csth->fmt0 != AST_FORMAT_SLINEAR && !csth->trans0) {
-               if (csth->fmt0 == csth->fmt1 && csth->trans1) {
-                       csth->trans0 = csth->trans1;
-               } else if ((csth->trans0 = ast_translator_build_path(AST_FORMAT_SLINEAR, csth->fmt0)) == NULL) {
-                       ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(csth->fmt0));
-                       return -1;
+               if (f1 && csth->fmt1 && csth->fmt1 != f1->subclass) {
+                       ast_translator_free_path(csth->trans1);
+                       csth->trans1 = NULL;
+                       csth->fmt1 = f1->subclass;
                }
                }
-       }
        
        
-       if (csth->fmt1 && csth->fmt1 != AST_FORMAT_SLINEAR && !csth->trans1) {
-               if (csth->fmt1 == csth->fmt0 && csth->trans0) {
-                       csth->trans1 = csth->trans0;
-               } else if ((csth->trans1 = ast_translator_build_path(AST_FORMAT_SLINEAR, csth->fmt1)) == NULL) {
-                       ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(csth->fmt1));
-                       return -1;
+               if (!csth->fmt0 && f0) {
+                       csth->fmt0 = f0->subclass;
+               }
+
+               if (!csth->fmt1 && f1) {
+                       csth->fmt1 = f1->subclass;
+               }
+
+               if (csth->fmt0 && csth->fmt0 != AST_FORMAT_SLINEAR && !csth->trans0) {
+                       if ((csth->trans0 = ast_translator_build_path(AST_FORMAT_SLINEAR, csth->fmt0)) == NULL) {
+                               ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(csth->fmt0));
+                               csth->spy.status = CHANSPY_DONE;
+                               return -1;
+                       }
+               }
+               if (csth->fmt1 && csth->fmt1 != AST_FORMAT_SLINEAR && !csth->trans1) {
+                       if ((csth->trans1 = ast_translator_build_path(AST_FORMAT_SLINEAR, csth->fmt1)) == NULL) {
+                               ast_log(LOG_WARNING, "Cannot build a path from %s to slin\n", ast_getformatname(csth->fmt1));
+                               csth->spy.status = CHANSPY_DONE;
+                               return -1;
+                       }
                }
                }
-       }
        
        
-       if (f0) {
-               if (csth->trans0) {
-                       if ((f = ast_translate(csth->trans0, f0, 0))) {
-                               memcpy(csth->buf0, f->data, f->datalen * sizeof(short));
-                               ast_frfree(f);
+               if (f0) {
+                       if (csth->trans0) {
+                               if ((f = ast_translate(csth->trans0, f0, 0))) {
+                                       if (csth->len0 < f->datalen) {
+                                               if (!csth->len0) {
+                                                       if (!(csth->buf0 = malloc(f->datalen * 2))) {
+                                                               csth->spy.status = CHANSPY_DONE;
+                                                               return -1;
+                                                       }
+                                               } else {
+                                                       if (!realloc(csth->buf0, f->datalen * 2)) {
+                                                               csth->spy.status = CHANSPY_DONE;
+                                                               return -1;
+                                                       }
+                                               }
+                                               csth->len0 = f->datalen;
+                                       }
+                                       memcpy(csth->buf0, f->data, f->datalen);
+                                       maxsamp = f->samples;
+                                       ast_frfree(f);
+                               } else {
+                                       return 0;
+                               }
                        } else {
                        } else {
-                               return 0;
+                               if (csth->len0 < f0->datalen) {
+                                       if (!csth->len0) {
+                                               if (!(csth->buf0 = malloc(f0->datalen * 2))) {
+                                                       csth->spy.status = CHANSPY_DONE;
+                                                       return -1;
+                                               }
+                                       } else {
+                                               if (!realloc(csth->buf0, f0->datalen * 2)) {
+                                                       csth->spy.status = CHANSPY_DONE;
+                                                       return -1;
+                                               }
+                                       }
+                                       csth->len0 = f0->datalen;
+                               }
+                               memcpy(csth->buf0, f0->data, f0->datalen);
+                               maxsamp = f0->samples;
                        }
                        }
-               } else {
-                       memcpy(csth->buf0, f0->data, f0->datalen * sizeof(short));
                }
                }
-       }
        
        
-       if (f1) {
-               if (csth->trans1) {
-                       if ((f = ast_translate(csth->trans1, f1, 0))) {
-                               memcpy(csth->buf1, f->data, f->datalen * sizeof(short));
-                               ast_frfree(f);
+               if (f1) {
+                       if (csth->trans1) {
+                               if ((f = ast_translate(csth->trans1, f1, 0))) {
+                                       if (csth->len1 < f->datalen) {
+                                               if (!csth->len1) {
+                                                       if (!(csth->buf1 = malloc(f->datalen * 2))) {
+                                                               csth->spy.status = CHANSPY_DONE;
+                                                               return -1;
+                                                       }
+                                               } else {
+                                                       if (!realloc(csth->buf1, f->datalen * 2)) {
+                                                               csth->spy.status = CHANSPY_DONE;
+                                                               return -1;
+                                                       }
+                                               }
+                                               csth->len1 = f->datalen;
+                                       }
+                                       memcpy(csth->buf1, f->data, f->datalen);
+                                       if (f->samples > maxsamp) {
+                                               maxsamp = f->samples;
+                                       }
+                                       ast_frfree(f);
+                               
+                               } else {
+                                       return 0;
+                               }
                        } else {
                        } else {
-                               return 0;
+                               if (csth->len1 < f1->datalen) {
+                                       if (!csth->len1) {
+                                               if (!(csth->buf1 = malloc(f1->datalen * 2))) {
+                                                       csth->spy.status = CHANSPY_DONE;
+                                                       return -1;
+                                               }
+                                       } else {
+                                               if (!realloc(csth->buf1, f1->datalen * 2)) {
+                                                       csth->spy.status = CHANSPY_DONE;
+                                                       return -1;
+                                               }
+                                       }
+                                       csth->len1 = f1->datalen;
+                               }
+                               memcpy(csth->buf1, f1->data, f1->datalen);
+                               if (f1->samples > maxsamp) {
+                                       maxsamp = f1->samples;
+                               }
                        }
                        }
-               } else {
-                       memcpy(csth->buf1, f1->data, f1->datalen * sizeof(short));
                }
                }
-       }
 
 
-       vf = get_volfactor(csth->volfactor);
-       vf = minmax(vf, 16);
-       for(x=0; x < len; x++) {
-               if (vf < 0) {
-                       if (f0) {
-                               csth->buf0[x] /= abs(vf);
-                       }
-                       if (f1) {
-                               csth->buf1[x] /= abs(vf);
+               vf = get_volfactor(csth->volfactor);
+               vf = minmax(vf, 16);
+
+               dlen = (csth->len0 > csth->len1) ? csth->len0 : csth->len1;
+
+               if (csth->len < dlen) {
+                       if (!csth->len) {
+                               if (!(csth->buf = malloc(dlen*2))) {
+                                       csth->spy.status = CHANSPY_DONE;
+                                       return -1;
+                               }
+                       } else {
+                               if (!realloc(csth->buf, dlen * 2)) {
+                                       csth->spy.status = CHANSPY_DONE;
+                                       return -1;
+                               }
                        }
                        }
-               } else if (vf > 0) {
-                       if (f0) {
-                               csth->buf0[x] *= vf;
+                       csth->len = dlen;
+               }
+
+               for(x=0; x < maxsamp; x++) {
+                       if (vf < 0) {
+                               if (f0) {
+                                       csth->buf0[x] /= abs(vf);
+                               }
+                               if (f1) {
+                                       csth->buf1[x] /= abs(vf);
+                               }
+                       } else if (vf > 0) {
+                               if (f0) {
+                                       csth->buf0[x] *= vf;
+                               }
+                               if (f1) {
+                                       csth->buf1[x] *= vf;
+                               }
                        }
                        }
-                       if (f1) {
-                               csth->buf1[x] *= vf;
+                       if (f0 && f1) {
+                               if (x < csth->len0 && x < csth->len1) {
+                                       csth->buf[x] = ast_fit_in_short(csth->buf0[x] + csth->buf1[x]);
+                               } else if (x < csth->len0) {
+                                       csth->buf[x] = csth->buf0[x];
+                               } else if (x < csth->len1) {
+                                       csth->buf[x] = csth->buf1[x];
+                               }
+                       } else if (f0 && x < csth->len0) {
+                               csth->buf[x] = csth->buf0[x];
+                       } else if (f1 && x < csth->len1) {
+                               csth->buf[x] = csth->buf1[x];
                        }
                }
                        }
                }
-               if (f0 && f1) {
-                       csth->buf[x] = ast_fit_in_short(csth->buf0[x] + csth->buf1[x]);
-               } else if (f0) {
-                       csth->buf[x] = csth->buf0[x];
-               } else if (f1) {
-                       csth->buf[x] = csth->buf1[x];
+
+               csth->frame.data = csth->buf;
+               csth->frame.samples = maxsamp;
+               csth->frame.datalen = csth->frame.samples * 2;
+               csth->samples += csth->frame.samples;
+               
+               if (ast_write(chan, &csth->frame)) {
+                       csth->spy.status = CHANSPY_DONE;
+                       return -1;
+               }
+               if (csth->fd) {
+                       write(csth->fd, csth->frame.data, csth->frame.datalen);
                }
                }
-       }
 
 
-       csth->frame.data = csth->buf;
-       ast_write(chan, &csth->frame);
+               if (f0) {
+                       ast_frfree(f0);
+               }
+               if (f1) {
+                       ast_frfree(f1);
+               }
 
 
-       if (f0) {
-               ast_frfree(f0);
-       }
-       if (f1) {
-               ast_frfree(f1);
-       }
+               if (loops > 10) {
+                       ast_log(LOG_WARNING, "Too Many Loops Bailing Out....");
+                       break;
+               }
+       } while (csth->samples <  csth->rsamples);
 
        return 0;
 }
 
        return 0;
 }
@@ -368,7 +509,7 @@ static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy)
                        return;
                }
                count++;
                        return;
                }
                count++;
-               if(count > 10) {
+               if (count > 10) {
                        return;
                }
                sched_yield();
                        return;
                }
                sched_yield();
@@ -388,7 +529,7 @@ static void stop_spying(struct ast_channel *chan, struct ast_channel_spy *spy)
 
 }
 
 
 }
 
-static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor) 
+static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd) 
 {
        struct chanspy_translation_helper csth;
        int running = 1, res = 0, x = 0;
 {
        struct chanspy_translation_helper csth;
        int running = 1, res = 0, x = 0;
@@ -410,6 +551,9 @@ static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int
                csth.frame.subclass = AST_FORMAT_SLINEAR;
                csth.frame.datalen = 320;
                csth.frame.samples = 160;
                csth.frame.subclass = AST_FORMAT_SLINEAR;
                csth.frame.datalen = 320;
                csth.frame.samples = 160;
+               if (fd) {
+                       csth.fd = fd;
+               }
                start_spying(spyee, chan, &csth.spy);
                ast_activate_generator(chan, &spygen, &csth);
 
                start_spying(spyee, chan, &csth.spy);
                ast_activate_generator(chan, &spygen, &csth);
 
@@ -421,13 +565,15 @@ static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int
                          (res = ast_waitfor(chan, -1) > -1)) {
                        if ((f = ast_read(chan))) {
                                res = 0;
                          (res = ast_waitfor(chan, -1) > -1)) {
                        if ((f = ast_read(chan))) {
                                res = 0;
-                               if(f->frametype == AST_FRAME_DTMF) {
+                               if (f->frametype == AST_FRAME_DTMF) {
                                        res = f->subclass;
                                }
                                ast_frfree(f);
                                        res = f->subclass;
                                }
                                ast_frfree(f);
-                               if(!res) {
+                               if (!res) {
                                        continue;
                                }
                                        continue;
                                }
+                       } else {
+                               break;
                        }
                        if (x == sizeof(inp)) {
                                x = 0;
                        }
                        if (x == sizeof(inp)) {
                                x = 0;
@@ -484,7 +630,8 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                *options = NULL,
                *spec = NULL,
                *argv[5],
                *options = NULL,
                *spec = NULL,
                *argv[5],
-               *mygroup = NULL;
+               *mygroup = NULL,
+               *recbase = NULL;
        int res = -1,
                volfactor = 0,
                silent = 0,
        int res = -1,
                volfactor = 0,
                silent = 0,
@@ -493,7 +640,10 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                chosen = 0,
                count=0,
                waitms = 100,
                chosen = 0,
                count=0,
                waitms = 100,
-               num = 0;
+               num = 0,
+               oldrf = 0,
+               oldwf = 0,
+               fd = 0;
        struct ast_flags flags;
 
 
        struct ast_flags flags;
 
 
@@ -502,6 +652,8 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                return -1;
        }
 
                return -1;
        }
 
+       oldrf = chan->readformat;
+       oldwf = chan->writeformat;
        if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
                ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
                return -1;
        if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
                ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
                return -1;
@@ -529,11 +681,16 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
        }
        
        if (options) {
        }
        
        if (options) {
-               char *opts[2];
+               char *opts[3];
                ast_parseoptions(chanspy_opts, &flags, opts, options);
                if (ast_test_flag(&flags, OPTION_GROUP)) {
                        mygroup = opts[1];
                }
                ast_parseoptions(chanspy_opts, &flags, opts, options);
                if (ast_test_flag(&flags, OPTION_GROUP)) {
                        mygroup = opts[1];
                }
+               if (ast_test_flag(&flags, OPTION_RECORD)) {
+                       if (!(recbase = opts[2])) {
+                               recbase = "chanspy";
+                       }
+               }
                silent = ast_test_flag(&flags, OPTION_QUIET);
                bronly = ast_test_flag(&flags, OPTION_BRIDGED);
                if (ast_test_flag(&flags, OPTION_VOLUME) && opts[1]) {
                silent = ast_test_flag(&flags, OPTION_QUIET);
                bronly = ast_test_flag(&flags, OPTION_BRIDGED);
                if (ast_test_flag(&flags, OPTION_VOLUME) && opts[1]) {
@@ -545,21 +702,31 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                }
        }
 
                }
        }
 
+       if (recbase) {
+               char filename[512];
+               snprintf(filename,sizeof(filename),"%s/%s.%ld.raw",AST_MONITOR_DIR, recbase, time(NULL));
+               if ((fd = open(filename, O_CREAT | O_WRONLY, O_TRUNC)) <= 0) {
+                       ast_log(LOG_WARNING, "Cannot open %s for recording\n", filename);
+                       fd = 0;
+               }
+       }
 
        for(;;) {
 
        for(;;) {
-               res = ast_streamfile(chan, "beep", chan->language);
-               if (!res)
-                       res = ast_waitstream(chan, "");
-               if (res < 0) {
-                       ast_clear_flag(chan, AST_FLAG_SPYING);
-                       ALL_DONE(u, -1);
-               }                       
+               if (!silent) {
+                       res = ast_streamfile(chan, "beep", chan->language);
+                       if (!res)
+                               res = ast_waitstream(chan, "");
+                       if (res < 0) {
+                               ast_clear_flag(chan, AST_FLAG_SPYING);
+                               break;
+                       }
+               }
 
                count = 0;
                res = ast_waitfordigit(chan, waitms);
                if (res < 0) {
                        ast_clear_flag(chan, AST_FLAG_SPYING);
 
                count = 0;
                res = ast_waitfordigit(chan, waitms);
                if (res < 0) {
                        ast_clear_flag(chan, AST_FLAG_SPYING);
-                       ALL_DONE(u, -1);
+                       break;
                }
                                
                peer = local_channel_walk(NULL);
                }
                                
                peer = local_channel_walk(NULL);
@@ -591,7 +758,7 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                                                *ptr = '\0';
                                                ptr++;
                                                for (x = 0 ; x < strlen(peer_name) ; x++) {
                                                *ptr = '\0';
                                                ptr++;
                                                for (x = 0 ; x < strlen(peer_name) ; x++) {
-                                                       if(peer_name[x] == '/') {
+                                                       if (peer_name[x] == '/') {
                                                                break;
                                                        }
                                                        peer_name[x] = tolower(peer_name[x]);
                                                                break;
                                                        }
                                                        peer_name[x] = tolower(peer_name[x]);
@@ -611,10 +778,9 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
                                                }
                                                count++;
                                                prev = peer;
                                                }
                                                count++;
                                                prev = peer;
-                                               res = channel_spy(chan, peer, &volfactor);
+                                               res = channel_spy(chan, peer, &volfactor, fd);
                                                if (res == -1) {
                                                if (res == -1) {
-                                                       ast_clear_flag(chan, AST_FLAG_SPYING);
-                                                       ALL_DONE(u, -1);
+                                                       break;
                                                } else if (res > 1 && spec) {
                                                        snprintf(name, AST_NAME_STRLEN, "%s/%d", spec, res);
                                                        if ((peer = local_get_channel_begin_name(name))) {
                                                } else if (res > 1 && spec) {
                                                        snprintf(name, AST_NAME_STRLEN, "%s/%d", spec, res);
                                                        if ((peer = local_get_channel_begin_name(name))) {
@@ -633,6 +799,18 @@ static int chanspy_exec(struct ast_channel *chan, void *data)
        }
        
 
        }
        
 
+       if (fd > 0) {
+               close(fd);
+       }
+
+       if (oldrf && ast_set_read_format(chan, oldrf) < 0) {
+               ast_log(LOG_ERROR, "Could Not Set Read Format.\n");
+       }
+       
+       if (oldwf && ast_set_write_format(chan, oldwf) < 0) {
+               ast_log(LOG_ERROR, "Could Not Set Write Format.\n");
+       }
+
        ast_clear_flag(chan, AST_FLAG_SPYING);
        ALL_DONE(u, res);
 }
        ast_clear_flag(chan, AST_FLAG_SPYING);
        ALL_DONE(u, res);
 }