#!/usr/bin/perl # # vmnotify.pl v0.1, Copyright (C) Mike Cathey # # ChangeLog # # Use this at your own risk! # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # my $DEBUG = 0; open(OUT, ">>/tmp/asterisk.log"); my $callFile = "Channel: Zap/25/CALLTHISNUMBER MaxRetries: 6 RetryTime: 60 WaitTime: 5 Context: vmnotify Extension: s Callerid: VM Notification <(XXX) XXX-XXXX> Priority: 2"; my $notifyFileP = "Channel: Zap/g1/9CALLTHISNUMBER,,,,EXTENSION# MaxRetries: 0 RetryTime: 300 WaitTime:30 Context: default Extension: s Callerid: VM Notification <(XXX) XXX-XXXX> Priority: 2"; my $notifyFileM = "Channel: Zap/g1/9CALLTHISNUMBER MaxRetries: 0 RetryTime: 300 WaitTime:30 Context: default Application: VoicemailMain Data: sEXTENSION Callerid: VM Notification <(XXX) XXX-XXXX> Priority: 2"; my $currentMessageDir = "/var/spool/asterisk/mwi/"; my $currentNotifyDir = "/var/spool/asterisk/notify/"; my $rand = $$; my $callFileTEMP = "/var/tmp/asterisk.callfile." . $rand; $rand2 = $rand + 1; my $callFileTEMP2 = "/var/tmp/asterisk.callfile." . $rand2; my $date = `date`; print OUT $date; if ($DEBUG > 0) { print OUT "\$callFileTEMP = $callFileTEMP\n"; print OUT "\$callFileTEMP2 = $callFileTEMP2\n"; for my $arg (@ARGV) { print OUT "\t$arg\n"; } } shift; my $mailboxTEMP = shift; my $messages = shift; chomp $mailbox; chomp $messages; my ($mailbox,$context) = split(/\@/, $mailboxTEMP); # testing #$mailbox = 1723; #$context = "default"; #$messages = 0; $CMDPath = $currentMessageDir . $context . "/" . $mailbox; my $currentMessages = 0; if ( -e $CMDPath ) { open(CURRENT, "<$CMDPath"); $currentMessages = ; close CURRENT; chomp $currentMessages; print OUT "currentMessages = \"$currentMessages\"\n" if $DEBUG > 0; } else { $currentMessages = 0; } # create the call file open(CF, ">$callFileTEMP"); if ( $messages > $currentMessages ) { # they have messages $callFile =~ s/CALLTHISNUMBER/\*1$mailbox/; # print "Turning ON MWI for $mailbox\@$context\n" if $DEBUG > 0; print OUT "Turning ON MWI for $mailbox\@$context\n"; print OUT $callFile if $DEBUG > 0; open(CF2, ">$callFileTEMP2"); my $notifyNumber = getNotifyNumber($mailbox); my $notifyMethod = $notifyNumber; $notifyMethod = substr($notifyNumber,0,1); $notifyNumber = substr($notifyNumber,1,7); my $notifyFile = ""; my $qFile = "/var/spool/asterisk/outgoing/asterisk." . $rand2; print OUT "\$qfile = $qFile\n" if $DEBUG > 0; if ($notifyMethod eq 'm') { $notifyFile = $notifyFileM; } elsif ( $notifyMethod eq 'p' ) { $notifyFile = $notifyFileP; } if ( $notifyNumber != 0 ) { $notifyFile =~ s/CALLTHISNUMBER/$notifyNumber/; if ($notifyMethod eq 'p' ) { $notifyFile =~ s/EXTENSION/$mailbox/; } else { $notifyFile =~ s/EXTENSION/$mailbox\@$context/; } print CF2 $notifyFile; if ($DEBUG > 0 ) { print OUT "---\n"; print OUT $notifyFile; print OUT "---\n"; } close CF2; rename($callFileTEMP2, $qFile) or print(OUT "ERROR: Can't rename callfile($callFileTEMP2): $!\n"); } } else { $callFile =~ s/CALLTHISNUMBER/\#1$mailbox/; #print "Turning OFF MWI for $mailbox\@$context\n" if $DEBUG > 0; print OUT "Turning OFF MWI for $mailbox\@$context\n"; print OUT $callFile if $DEBUG > 0; } print CF $callFile; close CF; # put it in the outgoing queue rename($callFileTEMP, "/var/spool/asterisk/outgoing/asterisk.$$") or print(OUT "ERROR: Can't rename callfile($callFileTEMP): $!\n"); # update the new message count open(UPDATECMD, ">$CMDPath"); print UPDATECMD $messages; close UPDATECMD; print OUT "-"x80 . "\n"; close OUT; sub getNotifyNumber { my $NotifyPath = $currentNotifyDir . $context . "/" . $mailbox; if ( -e $NotifyPath ) { open(CURRENT, "<$NotifyPath"); $notifyNumber = ; close CURRENT; chomp $notifyNumber; print OUT "notifyNumber = \"$notifyNumber\"\n" if $DEBUG > 0; return $notifyNumber; } else { return 0; } }