From 2d0c6762436f4050155bfae15a5d65db9a4f29e1 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 30 May 2025 09:10:41 -0400 Subject: [PATCH] MT#62544 add vmemailnotify wrapper Change-Id: Id6e4b091889538a086fbd2e6709137c819b5c984 --- Makefile | 1 + vmemailnotify | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100755 vmemailnotify diff --git a/Makefile b/Makefile index c64317f..0aff4ef 100644 --- a/Makefile +++ b/Makefile @@ -13,3 +13,4 @@ install: $(INSTALL_PROGRAM) vmnotify $(DESTDIR)/usr/bin/ngcp-vmnotify $(INSTALL_PROGRAM) vmsmsnotify $(DESTDIR)/usr/bin/ngcp-vmsmsnotify $(INSTALL_PROGRAM) recnotify $(DESTDIR)/usr/bin/ngcp-recnotify + $(INSTALL_PROGRAM) vmemailnotify $(DESTDIR)/usr/bin/ngcp-vmemailnotify diff --git a/vmemailnotify b/vmemailnotify new file mode 100755 index 0000000..64eb932 --- /dev/null +++ b/vmemailnotify @@ -0,0 +1,68 @@ +#!/usr/bin/perl -w + +use strict; +use warnings; +use Config::Any; +use DBI; + +my $CONF_FILE = '/etc/ngcp-vmnotify/vmnotify.conf'; + +my %CONFIG = %{ + Config::Any->load_files({ + files => [ $CONF_FILE ], + use_ext => 1 + })->[0]->{$CONF_FILE} + or die("$0 error: Cannot load config $CONF_FILE: $!"); +}; + +# pass-through mode +if (($CONFIG{TRANSCRIBE} // 'no') ne 'yes') { + exec(@ARGV); + die("$0 error: exec '@ARGV' failed: $!"); +} + +# read in email body +my $email; +{ + local $/ = undef; + $email = ; +} + +my ($uuid) = $email =~ /^X-Asterisk-VM-Extension: (\S+)\r?$/m; +my ($msgnum) = $email =~ /^X-Asterisk-VM-Message-Num: (\d+)\r?$/m; + +# transcript replacement wanted? +if ($email =~ /^(.*)\@\@\@TRANSCRIPT\@\@\@(.*)$/s) { + my ($head, $tail) = ($1, $2); + my $transcript = '(unavailable)'; + + my $dsn = "DBI:mysql:database=kamailio;host=$CONFIG{DB_HOST};port=$CONFIG{DB_PORT}"; + my $dbh = DBI->connect($dsn, $CONFIG{DB_USER}, $CONFIG{DB_PASS}); + + if ($dbh && $uuid && defined($msgnum)) { + my $sth = $dbh->prepare('select transcript, transcript_status from voicemail_spool where mailboxuser=? and msgnum=?'); + + while (1) { + $sth->execute($uuid, $msgnum - 1); + my $row = $sth->fetchrow_arrayref(); + $sth->finish(); + if ($row && $row->[1] eq 'done') { + $transcript = $row->[0]; + last; + } + sleep($CONFIG{TRANSCRIPT_POLL_INTERVAL} || 5); + } + } + + $email = $head . $transcript . $tail; +} + +# spawn process and feed in email +my $fh; +if (!open($fh, '|-', @ARGV)) { + die("$0 error: exec '@ARGV' failed: $!"); +} + +print $fh $email; +close($fh); +exit($?);