messages-expire.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/perl
  2. #
  3. # Script to expire voicemail after a specified number of days
  4. # by Steve Creel <screel@turbs.com>
  5. #
  6. # Directory housing the voicemail spool for asterisk
  7. $dir = "/var/spool/asterisk/voicemail";
  8. # Context for which the script should be running
  9. $context = "default";
  10. # Age (Delete files older than $age days old)
  11. $age = 31;
  12. # Age for unheard messages (Defaults to same age for all messages)
  13. # Set to 0 to not delete unheard messages
  14. $unheardage = $age;
  15. # Delete all files older than $age and $unheardage
  16. # (named msg????.??? to get the audio and txt files,
  17. # but we don't delete greetings or the user's name)
  18. if($age==$unheardage) {
  19. # Save time by doing one find if we're treating everything the same
  20. system('find '.$dir.'/'.$context.' -name msg????.??? -mtime +'.$age.' -exec rm {} \; -exec echo Deleted {} \;');
  21. } else {
  22. # Find everything not in a folder called 'INBOX' and delete it after $age days
  23. system('find '.$dir.'/'.$context.' -path \'*INBOX*\' -prune -o -name msg????.??? -mtime +'.$age.' -exec rm {} \; -exec echo Deleted {} \;');
  24. # If unheardage is set to 0, we won't delete any unheard messages
  25. if($unheardage > 0) {
  26. # Delete things that are in a folder called INBOX after $unheardage days
  27. system('find '.$dir.'/'.$context.' -path \'*INBOX*\' -name msg????.??? -mtime +'.$unheardage.' -exec rm {} \; -exec echo Deleted {} \;');
  28. }
  29. }
  30. # For testing - what number to we start when we renumber?
  31. $start = "0";
  32. # Rename to msg and a 4 digit number, 0 padded.
  33. $fnbase = sprintf "msg%04d", $start;
  34. # Make $dir include the context too
  35. $dir.="/".$context;
  36. ( -d $dir ) || die "Can't read list of mailboxes ($dir): $!\n";
  37. @mailboxes = `ls -A1 $dir`;
  38. chomp(@mailboxes);
  39. $save_fnbase = $fnbase;
  40. foreach $mailbox (@mailboxes) {
  41. ( -d $dir."/".$mailbox) || die "Can't read list of folders (".$dir."/".$mailbox."): $!\n";
  42. @folders = `ls -A1 $dir/$mailbox`;
  43. chomp(@folders);
  44. foreach $folder (@folders) {
  45. if (-d $dir."/".$mailbox."/".$folder) {
  46. ( -d $dir."/".$mailbox."/".$folder) || die "Can't read list of messages (".$dir."/".$mailbox."/".$folder.") $!\n";
  47. @files = `ls -A1 $dir/$mailbox/$folder/`;
  48. # Sort so everything is in proper order.
  49. @files = sort @files;
  50. chomp(@files);
  51. # If there is still (after deleting old files earlier in the
  52. # script) a msg0000.txt, we don't need to shuffle anything
  53. # in this folder.
  54. if (-f $dir."/".$mailbox."/".$folder."/msg0000.txt") { next; }
  55. foreach $ext (("WAV", "wav", "gsm", "txt")) {
  56. # Reset the fnbase for each file type
  57. $fnbase = $save_fnbase;
  58. foreach $file (@files) {
  59. if ( $file =~ /$ext/ ) {
  60. chdir($dir."/".$mailbox."/".$folder."/") || die "Can't change folder: $!";
  61. print "Renaming: ".$dir."/".$mailbox."/".$folder."/".$file." to ".$fnbase.".".$ext."\n";
  62. rename($file, $fnbase.".".$ext) || die "Cannot rename: $!";
  63. $fnbase++;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. __END__