; originally from http://www.loligo.com/asterisk/current/extensions.conf
; ===========================================================
;[globals]  (all globals are in extensions.conf)
;MONITORDIR=

[macro-monitor-on]
; ARG1 <- user number to turn on monitoring
exten = s,1,DBput(MONITOR/${ARG1}=YES)
exten = s,2,Playback(beep)
exten = s,3,Macro(return_to_top)

[macro-monitor-once]
; ARG1 <- user number to turn on monitoring
exten = s,1,DBput(MONITOR/${ARG1}=ONCE)
exten = s,2,Playback(beep)
exten = s,3,Macro(return_to_top)

[macro-monitor-off]
; ARG1 <- user number to turn off monitoring
exten = s,1,DBdel(MONITOR/${ARG1})
exten = s,2,Playback(beep)
exten = s,3,Macro(return_to_top)

; ------------------------------------------------------------------
[macro-monitor-begin]
; ARG1 -> user number
; ARG1 -> from caller id
; ARG2 -> to caller id
;
; ----
; Contents of the file /usr/share/asterisk/agi-bin/set-timestamp.agi:
;
; #!/bin/sh
; longtime=`date +%Y%m%d-%H%M%S`
; echo SET VARIABLE timestamp $longtime 
; ----
exten = s,1,DBget(monitor=MONITOR/${ARG1})
exten = s,2,NoOp(Beginning monitoring for user ${ARG1} from ${ARG2} to ${ARG3})
exten = s,3,AGI(set-timestamp.agi)
exten = s,4,SetVar(monitor_infile=${timestamp}-${ARG1}-${ARG2}-${ARG3})
exten = s,5,Monitor(wav,${monitor_infile})    
exten = s,6,Goto(102)    
exten = s,102,NoOp()

; ------------------------------------------------------------------
[macro-monitor-end]
; If we have recorded a call, it is to our advantage to change the
;  format of the call from a two-file system (blah-in.wav blah-out.wav)
;  into a single file that contains both legs of the call, and then
;  compress the call into some reasonably small filesize using ogg
;  compression.  This routine should be called out of the "h" priority
;  in a context.  If the call was not recorded, this macro will
;  not cause any harm, so calling it on each hangup isn't a problem.
;
; ARG1 -> user number

exten = s,1,GotoIf($["${monitor_infile}foo" = "foo"]?9:2)
exten = s,2,NoOp(Ending monitoring at ${monitor_infile})

; If monitor mode is ONCE, turn it off now
exten = s,3,DBget(monitor_mode=MONITOR/${ARG1})
exten = s,4,GotoIf($["${monitor_mode}" = "ONCE"]?5:6)
exten = s,5,DBdel(MONITOR/${ARG1})

; This part of the routine mixes the in and out .wav files into one .wav, and then
;  cleans up the original files (removes them)
;
; wmix is part of Wavetools-1.0 (http://tph.tuwien.ac.at/~oemer/tgz/wavetools-1.0.tgz)
;
; Turn the two in/out .wav files into a single .wav file with both channels
exten = s,6,System(/usr/bin/soxmix ${MONITORDIR}/${monitor_infile}-in.wav ${MONITORDIR}/${monitor_infile}-out.wav ${MONITORDIR}/${monitor_infile}.wav)

; Remove the old .wav files - we don't need them anymore.
exten = s,7,System(/bin/rm -f ${MONITORDIR}/${monitor_infile}-in.wav ${MONITORDIR}/${monitor_infile}-out.wav)

; Compress combined wav file to ogg and discard the wav file
exten = s,8,System(/usr/bin/nice -20 /usr/bin/oggenc -q 2 ${MONITORDIR}/${monitor_infile}.wav && /bin/rm -f ${MONITORDIR}/${monitor_infile}.wav &)

exten = s,9,NoOp
exten = s,104,Goto(s,6)

; ===========================================================
; Record a series of files each time this is called
; ===========================================================
[record-series]
;exten = s,1,Playback(user_sounds/record/record_instructions)
exten = s,1,NoOp
exten = s,2,AGI(set-timestamp.agi)  ; see monitor.conf for how this works
exten = s,3,Wait(2) ; give yourself 2 secs to take a breath and wait for beep
exten = s,4,SetVar(file=${MONITORDIR}/series/${timestamp})
exten = s,5,Record(${file}:gsm)
exten = s,6,Wait(1)
exten = s,7,Macro(return_to_top)

; Compress wav file to ogg and discard the wav file
;exten = h,1,System(/usr/bin/nice -20 /usr/bin/oggenc -q 2 ${file}.wav && /bin/rm -f ${file}.wav &)
; Copy the file to a temp directory.  From here, a script run every minute by cron
; catches the file and creates a voicemail from it.  This could be done through the voicemail
; menus, but I wanted a really quick and easy way to get recordings and then forward
; them to a voicemail later.
exten = h,1,System(/bin/cp ${file}.gsm ${MONITORDIR}/series/temp/${timestamp}.gsm)
exten = h,2,Hangup

