« lib/Monitoring/Spooler/Cmd/Command/email.pm
↑ Summary
lib/Monitoring/Spooler/Cmd/Command/list.pm »
| 1 | package Monitoring::Spooler::Transport::Pjsua; | ||
| 2 | $Monitoring::Spooler::Transport::Pjsua::VERSION = '0.05'; | ||
| 3 | BEGIN { | ||
| 4 | $Monitoring::Spooler::Transport::Pjsua::AUTHORITY = 'cpan:TEX'; | ||
| 5 | } | ||
| 6 | # ABSTRACT: a monitoring spooler voice transport using pjsua | ||
| 7 | |||
| 8 | use 5.010_000; | ||
| 9 | use mro 'c3'; | ||
| 10 | use feature ':5.10'; | ||
| 11 | |||
| 12 | use Moose; | ||
| 13 | use namespace::autoclean; | ||
| 14 | |||
| 15 | # use IO::Handle; | ||
| 16 | # use autodie; | ||
| 17 | # use MooseX::Params::Validate; | ||
| 18 | # use Carp; | ||
| 19 | # use English qw( -no_match_vars ); | ||
| 20 | # use Try::Tiny; | ||
| 21 | use IPC::Open2; | ||
| 22 | use File::Temp qw(); | ||
| 23 | |||
| 24 | # extends ... | ||
| 25 | extends 'Monitoring::Spooler::Transport'; | ||
| 26 | # has ... | ||
| 27 | has 'sipid' => ( | ||
| 28 | 'is' => 'ro', | ||
| 29 | 'isa' => 'Str', | ||
| 30 | 'required' => 1, | ||
| 31 | ); | ||
| 32 | has 'registrar' => ( | ||
| 33 | 'is' => 'ro', | ||
| 34 | 'isa' => 'Str', | ||
| 35 | 'required' => 1, | ||
| 36 | ); | ||
| 37 | has 'realm' => ( | ||
| 38 | 'is' => 'ro', | ||
| 39 | 'isa' => 'Str', | ||
| 40 | 'required' => 1, | ||
| 41 | ); | ||
| 42 | has 'username' => ( | ||
| 43 | 'is' => 'ro', | ||
| 44 | 'isa' => 'Str', | ||
| 45 | 'required' => 1, | ||
| 46 | ); | ||
| 47 | has 'password' => ( | ||
| 48 | 'is' => 'ro', | ||
| 49 | 'isa' => 'Str', | ||
| 50 | 'required' => 1, | ||
| 51 | ); | ||
| 52 | has 'outbound' => ( | ||
| 53 | 'is' => 'ro', | ||
| 54 | 'isa' => 'Str', | ||
| 55 | 'required' => 1, | ||
| 56 | ); | ||
| 57 | has 'stunsrv' => ( | ||
| 58 | 'is' => 'ro', | ||
| 59 | 'isa' => 'Str', | ||
| 60 | 'required' => 1, | ||
| 61 | ); | ||
| 62 | has 'sipdest' => ( | ||
| 63 | 'is' => 'ro', | ||
| 64 | 'isa' => 'Str', | ||
| 65 | 'required' => 1, | ||
| 66 | ); | ||
| 67 | # with ... | ||
| 68 | # initializers ... | ||
| 69 | |||
| 70 | # your code here ... | ||
| 71 | sub provides { | ||
| 72 | my $self = shift; | ||
| 73 | my $type = shift; | ||
| 74 | |||
| 75 | if($type =~ m/^phone/i) { | ||
| 76 | return 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub run { | ||
| 83 | my $self = shift; | ||
| 84 | my $number = shift; | ||
| 85 | my $voicefile = shift; | ||
| 86 | |||
| 87 | # use IPC::open2 or open3 (or even just open ...) and hand-crafted eval-timeouts in favor of Expect | ||
| 88 | |||
| 89 | # run pjsua, call number, play voicefile, record DTMF | ||
| 90 | # return (nothing) on error or no DTMF data | ||
| 91 | # return value of DTMF on success: return wantarray ? (key1, key2, ...) : key1; | ||
| 92 | # write temp config file | ||
| 93 | my $config_file = $self->_write_config(); | ||
| 94 | my $pjsua_cmd = '/usr/bin/pjsua --config-file '.$config_file; | ||
| 95 | $pjsua_cmd .= ' --play-file '.$voicefile; | ||
| 96 | $pjsua_cmd .= ' --log-level 5 --app-log-level 3 --auto-play --duration 90'; | ||
| 97 | $pjsua_cmd .= ' --max-calls 1 '; | ||
| 98 | $pjsua_cmd .= ' --log-file /var/log/mon-spooler/pjsua_logs/log-'.$number.'pjsua_out_'.time(); | ||
| 99 | $pjsua_cmd .= ' sip:'.$number.'@'.$self->sipdest(); | ||
| 100 | $pjsua_cmd .= ' 2>/tmp/pj.error'; | ||
| 101 | |||
| 102 | my ($child_in, $child_out); | ||
| 103 | my $pid = open2($child_out, $child_in, $pjsua_cmd); | ||
| 104 | |||
| 105 | if($pid) { | ||
| 106 | if($self->_wait_for($child_out,qr/Got answer/i,90)) { | ||
| 107 | my $match_re = qr/Incoming DTMF on call 0: (\d+)/i; | ||
| 108 | my $dtmf = $self->_wait_for($child_out,$match_re,90); | ||
| 109 | if($dtmf && $dtmf =~ m/$match_re/) { | ||
| 110 | my $dmtf_digit = $1; | ||
| 111 | |||
| 112 | print $child_in "h\n"; # send hangup command | ||
| 113 | sleep 2; | ||
| 114 | print $child_in "q\n"; # quit pjsua | ||
| 115 | sleep 2; | ||
| 116 | |||
| 117 | close($child_out); | ||
| 118 | close($child_in); | ||
| 119 | unlink($config_file); | ||
| 120 | return $dmtf_digit; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | unlink($config_file); | ||
| 124 | close($child_out); | ||
| 125 | close($child_in); | ||
| 126 | } | ||
| 127 | |||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _write_config { | ||
| 132 | my $self = shift; | ||
| 133 | |||
| 134 | my ($fh, $filename) = File::Temp::tempfile(); | ||
| 135 | print $fh '--id '.$self->sipid()."\n"; | ||
| 136 | print $fh '--registrar '.$self->registrar()."\n"; | ||
| 137 | print $fh '--realm '.$self->realm()."\n"; | ||
| 138 | print $fh '--username '.$self->username()."\n"; | ||
| 139 | print $fh '--password '.$self->password()."\n"; | ||
| 140 | print $fh '--outboudn '.$self->outbound()."\n"; | ||
| 141 | print $fh '--stun-srv '.$self->stunsrv()."\n"; | ||
| 142 | print $fh '--null-audio'."\n"; | ||
| 143 | close($fh); | ||
| 144 | |||
| 145 | return $filename; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub _wait_for { | ||
| 149 | my $self = shift; | ||
| 150 | my $fh = shift; | ||
| 151 | my $re = shift; | ||
| 152 | my $timeout = shift; | ||
| 153 | |||
| 154 | my $subject; | ||
| 155 | my $prev_timeout; | ||
| 156 | my $eval_status = eval { | ||
| 157 | local $SIG{ALRM} = sub { die "alarm-monitoring-spooler-transport-pjsua\n"; }; | ||
| 158 | $prev_timeout = alarm $timeout; | ||
| 159 | while(my $line = <$fh>) { | ||
| 160 | if($line =~ m/$re/) { | ||
| 161 | $subject = $line; | ||
| 162 | last; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | 1; | ||
| 166 | }; | ||
| 167 | alarm $prev_timeout; | ||
| 168 | if($@ && $@ eq "alarm-monitoring-spooler-transport-pjsua\n") { | ||
| 169 | # Timeout ... | ||
| 170 | return; | ||
| 171 | } elsif($@ || !$eval_status || !$subject) { | ||
| 172 | # Other eval error | ||
| 173 | return; | ||
| 174 | } else { | ||
| 175 | return $subject; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | no Moose; | ||
| 180 | __PACKAGE__->meta->make_immutable; | ||
| 181 | |||
| 182 | 1; | ||
| 183 | |||
| 184 | __END__ |