# Category = MisterHouse

#@ Ed Guy's hook to asterisk
# GPL  Copyright (C) 2005 Ed guy , edguy@eguy.org

# file: $MHROOT/mh/code/test/speak_proxy.pl
#
=begin comment

Use this code to speak via asterisk

requires 'asterisk_phones' to be defined, e.g,

mh/bin/mh.private.ini:asterisk_phones=default=>i202, kitchen=>i202, office=>i201, stu=>207, edcell=>16316457292@vonage

=cut

# Log hook is not muted like speak hook is
&Speak_pre_add_hook(\&etg_proxy_speak_play, 0, 'speak') if $Reload;
&Play_pre_add_hook (\&etg_proxy_speak_play, 0, 'play')  if $Reload;

sub etg_proxy_speak_play {
    my ($mode) = pop @_;
    my (%parms) = @_;

    print "etg_proxy_play mode=$mode parms: @_\n" ;

    if ($mode eq 'speak')
    {
        # Drop extra blanks and newlines
        $parms{text} =~ s/[\n\r ]+/ /gm;
        %parms = map {$_, $parms{$_}} grep $parms{$_} =~ /\S+/, keys %parms;

        system( " echo $parms{text} | /usr/bin/text2wave -eval '(voice_cmu_us_slt_arctic_hts)' -scale 1.5 -F 8000  -o /tmp/message.wav");
    }
    elsif ($mode eq 'play')
    {
        system( " cp $parms{file} /tmp/message.wav");
    }

    my %ast_exts;
    &main::read_parm_hash(\%ast_exts, $main::config_parms{asterisk_phones});

    my $SIPLINE=$ast_exts{default};
    $SIPLINE=$ast_exts{$parms{location}} if $parms{location};

    #todo ... create file elsewhere and move/copy when complete.
    open (FH,'>/var/spool/asterisk/outgoing/message')
        or die "oops";


    print FH "Channel: SIP/$SIPLINE\n";
    print FH "MaxRetries: 0\n";
    # note.. 999999999 must be a valid destination in internal-origination context.
    print FH "Callerid: Mr House <999999999>\n";
    print FH "RetryTime: 60\n";
    print FH "WaitTime: 30\n";
    print FH "Context: internal-origination\n";
    print FH "Extension:  999999999\n";
    print FH "Priority: 1\n";
    print FH ";$config_parms{'asterisk_phones'}  \n";
    close FH;


}

