#!/usr/bin/perl # # emaildir # # version 0.1, August 2000 # # This script replaces e-mails in fields `From: ' and `Return-Path: ' of e-mail # header. It's usefull when you have different local and remote e-mail. # Input parameters are paths to two maildirs. This script reads messages from # first maildir and writes to second. # # Replacements must be set in /etc/emaildir.loc2rem. There must be TWO e-mails # on each line separated by TABULATOR. Comments lines must begin with `#' char. # Empty lines are supported. # # Script is distributed under the GNU General Public Licence. # # author: # Jiri VERUNEK # http://verunek.host.sk # http://verunek.oceany.cz # sub createfilelist { local($a); push(@dirlist, ""); while (@dirlist."" > 0) { $curdirpath = pop(@dirlist); opendir(dir, @ARGV[0].$curdirpath); @curdir = readdir(dir); closedir(dir); for ($a = 0; $a < @curdir.""; ++$a) { if (-f @ARGV[0].$curdirpath.$curdir[$a]) { push(@filelist, $curdirpath.$curdir[$a]); } elsif ((-d @ARGV[0].$curdirpath.$curdir[$a]) && ($curdir[$a] ne '.') && ($curdir[$a] ne '..')) { push(@dirlist, $curdirpath.$curdir[$a]."/"); } } } } sub loademailfile { open(emailfile, "/etc/emaildir.loc2rem"); @email = ; close(email); local($a); do { if (($email[$a] !~ /^#/) && ($email[$a] =~ /.\t./)) { push(@emailrem, substr($email[$a], index($email[$a], "\t")+1)); push(@emailloc, substr($email[$a], 0, index($email[$a], "\t"))); } ++$a } until($a >= @email.""); chop(@emailrem); undef @email; } sub replaceemail { local($a); for ($a = 0; $a < @emailloc.""; ++$a) { $message[$_[0]] =~ s/$emailloc[$a]/$emailrem[$a]/; } } sub savemessage { if (-f @ARGV[1].$messagepath) { print "ERROR: File @ARGV[1]$messagepath exists yet!\n" } elsif (open(file, ">@ARGV[1]$messagepath") == 1) { print file @message; close(file); unlink(@ARGV[0].$messagepath); } else { print "ERROR: File @ARGV[1]$messagepath cannot be created.\n" } } sub processthefiles { while (@filelist."" > 0) { $messagepath = pop(@filelist); open(file, @ARGV[0].$messagepath); @message = ; close(file); $returnpath_ok = 0; $from_ok = 0; $messagelinenumber = 0; do { if ($message[$messagelinenumber] =~ /.@./) { if (($returnpath_ok == 0) && ($message[$messagelinenumber] =~ /^Return-Path: /)) { $returnpath_ok = 1; &replaceemail($messagelinenumber); } elsif (($from_ok == 0) && ($message[$messagelinenumber] =~ /^From: /)) { $from_ok = 1; &replaceemail($messagelinenumber); } } ++$messagelinenumber; } until ($messagelinenumber >= @message."") || (($returnpath_ok == 1) && ($from_ok == 1)); &savemessage; } } # ===== MAIN PROGRAM ===== if ((@ARGV."" == 1) && (@ARGV[0] == "--help")) { print "emaildir\n"; print "version 0.1, August 2000\n\n"; print "This script replaces e-mails in fields `From: ' and `Return-Path: ' of e-mail header. It's usefull when you have different local and remote e-mail. Input parameters are paths to two maildirs. This script reads messages from first maildir and writes to second.\n"; print "Replacements must be set in /etc/emaildir.loc2rem. There must be TWO e-mails on each line separated by TABULATOR. Comments lines must begin with `#' char. Empty lines are supported.\n\n"; print "license: GNU GPL\n\n"; print "author:\n"; print ' Jiri VERUNEK '."\n"; print ' http://www.oceany.cz/verunek'."\n"; } elsif ((@ARGV."" != 2) || !(-d @ARGV[0]) || !(-d @ARGV[1])) { print "Usage: emaildir DIR1 DIR2\n"; print " \"emaildir --help\" for help\n" } else { if (@ARGV[0] !~ /.\/$/) { @ARGV[0] = @ARGV[0]."/"; } if (@ARGV[1] !~ /.\/$/) { @ARGV[1] = @ARGV[1]."/"; } &createfilelist; if (@filelist."" > 0) { &loademailfile; &processthefiles; } }