[asterisk-users] Removing voicemail messages

Dana Harding dharding at nucleus.com
Fri Jul 4 11:34:05 CDT 2008


> I want to create an script which remove all the old voicemail messages.
> I make a simple Bash script to delete all the new messages for the
> extension 100. Something like,
>
> rm /var/spool/asterisk/voicemail/defaul/100/INBOX
>
> Should I update any index file or something after reemove them?

In my solution,  I archive messages that haven't been touched in <x> number 
days and send the compilation of messages being archived to the 
voicemailbox's owner.  You could modify to delete them and leave it at that. 
Since I'm leaving newer messages in the directory, and Asterisk doesn't like 
nonsequential filenames - I renumber all the remaining files.

I also had an issue with abandoned voicemail - where the caller would hangup 
at the exact right moment leaving a zero length message.  The  .txt file 
would be created but none of the audio formats.   It was a rare occurrence - 
but the user was unable to fix it from the phone interface meaning I had to 
manually correct it every time,  so I automated it with a script and a 
cronjob.

My crack at these is below,  these are a work in progress - use at your own 
risk, suggested improvements are welcome.

--- age_move_voicemail.sh ---
#!/bin/bash
# Script to automatically archive old voicemail messages
# Based on modified date of files, when messages are listened to the date is 
updated.
# Not an issue in this implementation as users mostly listen to e-mail 
attachment and don't
# touch the phone.
# Archived messages are also e-mailed to the voicemailbox owner.
# Possible future iteration: Read the date of the messages directly from the 
.txt file
# Future Improvement: Read options from config file to allow defaults and 
mailbox
# specific modifications.
# Possible future improvement: Use AGI or other interface to allow users to 
modify archive
# options via the telephone. If this proves too difficult, perhaps an e-mail
# or web interface would work.

archivefolder=/home/asterisk/oldvoicemail
voicemailroot=/var/spool/asterisk/voicemail/default
process=yes
logging=yes
defaultdaysold=60
sendemail=yes
function logit() {
if [ "$logging" = "yes" ]; then
echo $1
fi
}
function processold() {
if ( test -d /tmp/$maindir ); then
logit "/tmp/$maindir Already exists. Deleting contents"
rm /tmp/$maindir/*
else
mkdir /tmp/$maindir
fi
find . -type f -name 'msg*' -daystart -mtime +$daysold -exec mv {} 
/tmp/$maindir \;
# Let's make one .txt file for an e-mail that we will send out.
# first a message explaining what's going on
cd /tmp/$maindir
echo -e "Messages older then $daysold days in voicemail box $maindir folder 
$subdir are being archived and removed from the phone system.\n\nThese have 
been previously sent to your e-mail when they were received, and are 
attached here for reference" >> email.txt
echo -e "\n\n" >>email.txt
for txtname in msg*.txt; do
echo -n "`echo $txtname|awk -F . '{print $1}'` " >> email.txt
echo -n `grep -e "callerid=" -e "origdate=" -e "duration=" $txtname` >> 
email.txt
echo " seconds" >> email.txt
done
echo -e "\n\n" >> email.txt
# now the fun stuff - uuencode all the .WAV files that were previously 
e-mailed
for filename in msg*.WAV; do
uuencode $filename $filename >> email.txt
done
# who do we e-mail this to?
sendtoemail=`grep "$maindir => " /etc/asterisk/voicemail.conf| awk -F , 
'{print $3}'`
if [ "$sendemail" = "yes" ]; then
logit "Sending e-mail to $sendtoemail"
todaydate=`date +%Y%m%d`
/usr/bin/mail -s "Archived Voicemail Messages $todaydate" -a 'From: Asterisk 
PBX <asterisk at phoneserver>' $sendtoemail < email.txt
fi


logit "Tarballing the old messages."
tarfile=$maindir\_$todaydate\_$subdir.tar.gz
tar -czf $tarfile --no-recursion --remove-files ./msg*
chmod 600 $tarfile
mv ./$tarfile $archivefolder
rm /tmp/$maindir/*
rmdir /tmp/$maindir
}

#for maindir in `find $voicemailroot -type d -maxdepth 1|awk -F / '{print 
$7}'`; do
# Currently only processing specific mailboxes:
for maindir in 1234 204 250 205 214 220; do
processme=$process
daysold=$defaultdaysold
case $maindir in
1234) processme=no;;
200) processme=no;;
250) let daysold=7;;
205) let daysold=14;;
204) let daysold=5;;
esac
if [ "$processme" = "no" ]; then
echo skipping $maindir
continue
fi
cd $voicemailroot/$maindir
for subdir in `find . -type d|awk -F / '{print $2}'`; do
# to only process specific directories:
#for subdir in INBOX; do
cd $voicemailroot/$maindir/$subdir
if ( test -e msg0000.txt ); then
messages=`ls *.WAV|grep -c msg`
oldmessages=`find . -type f -name '*.WAV' -daystart -mtime +$daysold|grep -c 
msg`
if [ $oldmessages = 0 ]; then logit "$maindir $subdir $oldmessages/$messages 
No old messages to process";continue;fi
logit "$maindir $subdir $oldmessages/$messages Processing old messages"
processold
else
logit "$maindir $subdir 0/0 No messages to process"
fi
done
done
/home/asterisk/renumber.sh

--- renumber.sh ---
#!/bin/sh
# Asterisk doesn't handle out of order messages very well.
# Script written assuming worst case scenario that patches of messages
# Have been removed, instead of a solid block.
# Future improvement: Read in configuration information from a config file
# to allow skipping or otherwise modified behaviour on a per-mailbox basis.

voicemailroot=/var/spool/asterisk/voicemail/default
process=yes
logging=yes
function logit() {
if [ "$logging" = "yes" ]; then
echo $1
fi
}
padit() {
case $1 in
"in")
case ${#in} in
1) instr=000$in;;
2) instr=00$in;;
3) instr=0$in;;
4) instr=$in;;
esac;;
"out")
case ${#out} in
1) outstr=000$out;;
2) outstr=00$out;;
3) outstr=0$out;;
4) outstr=$out;;
esac;;
esac
}
function renumberfiles() {
out=0
padit out
numberfiles=`find . -type f -name "msg*.txt"|wc -l`
for (( in=0 ; in<=9999 ; in+=1 )); do
padit in
if ( test -e msg$instr.txt ); then
if ( test -e msg$outstr.txt );
then
# Infile number is already lowest possible no renaming necessary
[ 1 ]
else
for ext in txt WAV wav gsm; do mv msg$instr.$ext msg$outstr.$ext; done
fi
let out+=1
padit out
if (( $out == $numberfiles )); then break; fi
fi
done
}
echo STARTING RENUMBERING `date`
for maindir in `find $voicemailroot -type d -maxdepth 1|awk -F / '{print 
$7}'`; do
processme=$process
if [ "$maindir" = "1234" ]; then
processme=no
elif [ "$maindir" = "200" ]; then
processme=no
fi
if [ "$processme" = "no" ]; then
echo skipping $maindir
continue
fi
cd $voicemailroot/$maindir
for subdir in `find . -type d|awk -F / '{print $2}'`; do
cd $voicemailroot/$maindir/$subdir
if [ "$(ls -A ./)" ]; then
logit "Renumbering $maindir $subdir"
renumberfiles
else
logit "No messages in $maindir $subdir to renumber"
continue
fi
done
done
echo FINISHED RENUMBERING `date`


--- abandoned_voicemail ---
#!/bin/bash
# Sometimes a zero length voicemail message is recorded,
# The message leaves a .txt file but none of the audio formats
# When the user attempts to listen to this message, there is no audio file
# so the message is blank. The user cannot delete this message.

# This version is expecting to see four files per voicemail message:
# .txt .WAV .wav .gsm    if four files don't exist, delete.
# Future iteration to read in options from config file.

voicemailbasedir=/var/spool/asterisk/voicemail/default
renumberexecute=/home/asterisk/renumber.sh
renumber=0

cd $voicemailbasedir
for str in `find . -type f -name "msg*.txt"`; do
 maindir=`echo "$str"|awk -F / '{print $2}'`
 subdir=`echo "$str"|awk -F / '{print $3}'`
 file=`echo "$str"|awk -F / '{print $4}'`
 filebase=`echo "$file"|awk -F . '{print $1}'`

 cd $voicemailbasedir/$maindir/$subdir
 if (( `find . -type f -name "$filebase.*"|wc -l` == 4 )); then
  [ 1 ]   # file is okay
 else
  echo $maindir $subdir $file BAD - Deleting
  rm $file
  let renumber=1
 fi
done
if (( renumber == 1 )); then
 echo Starting Renumbering: $renumberexecute
 exec $renumberexecute
fi




More information about the asterisk-users mailing list