#!/usr/bin/perl -w # # agent2mbox # version 1.0 11/01/2003 # # this is a converter for Forte Agent files to plain mbox files # this will not convert filters or attachs, just the messages # i dont use forte agent and i only have some .dat files to # test but it seens that is working fine... # if you find any bug, please email me to the email below # # agent2mbox@caravela.homelinux.net # # it will converte one .dat file at the time, so to convert all # files do something like: # for i in *.DAT ; do agent2mbox $i ; done # if all in just one dir or # find . -name \*.dat -exec agent2mbox {} \; # to search and convert in several folders # # this converter was based in my kmx2mbox converter script that # converts Mail Warrior .kmx mailboxes, so probably there are things # in this script that arent really used to convert from agent # if i have more free time i will hunt then down and remove then # but they shouldnt do any harm too, just making the script more # complex # # this was made to help Luis Ferreira from pt.comp.so.linux to # export his old agent archive messages to any format usable by linux # # by: Daniel Mota Leite higuita@Gmx.net - Portugal # tmp website: http://caravela.homelinux.net/~higuita/agent2mbox # (dynamic DNS site, maybe offline sometimes) # # Good luck and enjoy # #---------------------------------------------------------------------------- #This program is protected by the GNU General Public License. Please see #http://www.gnu.org for information regarding the GNU General Public License. #---------------------------------------------------------------------------- # $nome=shift; unless ($nome) {$nome=""}; $mbox_nova=$nome; $mbox_nova=~ s/.dat/.mbox/i; $mbox_nova=~ s/.DAT/.mbox/i; $ok=0; $n=0; $i=0; @email=""; $from=""; $date=""; #$subject=""; #$to=""; $from_ok=0; $date_ok=0; $date="Sun, 1 Jan 1998 00:00:00 -0000"; $i_old=0; # protection against screw up files with no .dat extension # without this the script will overwrite all files without .dat unless ( ( $nome=~ /.DAT/i) || ( $nome=~ /.dat/i) ) { print " $nome No .dat extension... Aborting\n"; exit; } print "Reading $nome\n\n"; # read the mailbox $nome open(DATA, "< $nome") || die("ERROR - can't open datafile: $!"); @msg=; close(DATA); # save the new mailbox with the correct translated name if (($nome=~ /.DAT/i) || ( $nome=~ /.dat/i) ){ open(DATA , ">$mbox_nova ") || die("ERROR - cant open datafile: $!"); } foreach $linha(@msg) { $i_old++; #i_old is used to find the from and date from old, imported kmx #print "$n $i_old $i $linha"; #debug # escape first junk lines if ( $linha =~ /\t0\r$/ ){ unless ( $n == 0 ) { print "$n -> $from $date\n"; &save; } $i=0; $from_ok=0; $from=""; @email=""; $ok=1; $date_ok=0; $i_old=0; $n++; $headers=0; } elsif ( $ok==1 ) { #verifica as headers unless ($headers == 1) { &check_headers; if ( $linha =~ /^X-Agent-Group: / ) { #fim das headers $headers=0; } } # ignore the multipart header, but not any possible message # also ignore any line with a NULL character unless ( $linha =~ /\x00multipart\/single\x00/ || $linha =~ /\x00/){ $linha =~ s/\r//g; $email[$i]=$linha; $i++; } } } &save; &exit; ####################### ###### Modules ######## ####################### sub save{ # the mbox format need a "\nFrom name date" at start of each email # the "converted" header is just to fix some incomplete headers # i had only one very old kmx mailbox that had this problem # this helps this emails and doesnt do anything for the others # ignore if there is no email (ie: the last field in the new MW format) unless ( $email[0] eq "" ){ $head=""; # # one old hack for ancient kmx mailbox, should be needed by agent2mbox # # if ( $from_ok==0 && $email[0] ne "" ) { # $from= $from_old; # $from=~ s/\n//; # $from=~ s/\r//; # $subject=~ s/\n//; # $to=~ s/\n//; # # #fake missing header for the old imported kmx # $head= "From: $from\nDate: $date\nSubject: $subject\nX-Mailer: Mail Warrior 2\nTo: $to\n"; # $head=~ s/\r/ /g; # } print DATA "\nFrom $from $date\nConverted: agent2mbox\n"; print DATA $head; print DATA @email; print DATA "\n"; #just as precaution if its missing the newline in the last line of the email } } #closes files, display sumary, have a nice day... 8) sub exit{ close(DATA); print "\nConverted $n emails to mbox $mbox_nova\n\n"; exit; } # check the From, Date and Content-Type: multipart (there is no attach, # this can screw some email clients) sub check_headers{ if ($linha =~ /^From: /i && $from_ok==0) { $from= $linha; unless ( $from =~ /\@/ ) { $from=" bogus\@email.none"; } $from=~ s/[<>]//ig; $from=~ s/^From:.*\s+([^\s]+@[^\s]+\.[\w\d]+)(.*)?/$1/i; $from=~ s/\n//; $from_ok=1; } elsif ($linha =~ /^Date: /i && $date_ok==0) { $date= $linha; $date=~ s/^Date: //i; $date=~ s/\n//; $date=~ s/,//; $date_ok=1; } # hide multipart # shouldnt be needed in agent converter elsif ( $linha=~ /^Content-Type: multipart/i) { $linha=~ s/^Content-Type: multipart/encode-/i; } }