3 # Author: Thorsten Maerz <info@netztorte.de>
5 # Dependencies: MIME::Parser, LWP::MediaTypes from www.cpan.org
6 # Converts mbox files as exported from Calypso to MH format. Regenerates
7 # Calypso's folder structure and optionally includes the attachments.
11 our $mboxdir = '' || showhelp
(); # enter path to exported mbox
12 our $mboxfile = '' || showhelp
(); # enter name of exported mbox
13 our $outdir = '' || showhelp
(); # enter destination path
15 my $incl_attach = 1 ; # include attachments (needs CPAN modules)
16 my $verbose = 1 ; # show some headers of processed mail
17 my $testonly = 0 ; # dont create any files
19 ################################################################################
20 # no user servicable parts below :)
24 use LWP
::MediaTypes
qw(guess_media_type);
27 my $mbox = "$mboxdir/$mboxfile";
28 my $calypso_hdr = 'From \?\?\?@\?\?\? '; #Mon Apr 17 00:37:38 2000
29 my $hdr_Folder = 'X-CalypsoFolder:';
30 my $hdr_HTML = 'X-CalypsoHtmlBody:';
31 my $hdr_Account = 'X-CalypsoAccount:';
32 my $hdr_Attach = 'X-Attachment:';
34 my $create_dirs = 1 ; # create dirs from "X-Calypso-Folder:" header
36 ################################################################################
38 die ( "You have not yet configured this script.\n"
39 . "Please provide the correct path and file names, e.g\n"
40 . "\tour \$mboxdir = 'Archive'\n"
41 . "\tour \$mboxfile = 'mail.txt'\n"
42 . "\tour \$outdir = 'Calypso'\n"
43 . "at the top of $0\n"
47 ################################################################################
49 # MAIN : scan $mbox linewise
50 # Create a separate message for each $calypso_hdr found (MH format)
51 # $attach_full = filename with path, $attach_short = original attachment name
52 # $folder = Calypso folder
54 ################################################################################
55 my ($folder, $html, $html_full, $html_short,
56 $account, $attach, $attach_short, $attach_full);
59 open (INBOX
, "<".$mbox);
64 if (m/^$calypso_hdr/) {
67 shift @lines ; # remove blank line
71 $folder = $html = $html_full = $html_short = $account
72 = $attach = $attach_short = $attach_full = "";
77 if (/^$hdr_Folder /) { $folder = $' ;
79 $folder =~ tr#\\#\/# ;
81 if (/^$hdr_HTML /) { $html = $' ;
89 if (/^$hdr_Account /) { $account = $' ;
92 if (/^$hdr_Attach /) { $attach = $' ;
94 $attach =~ tr#\\#\/# ;
95 if ($attach =~ /; /) {
106 ################################################################################
109 # Saves mail in @lines to $outdir/$folder/$mail_nr
110 # Folder is created unless $testonly or (not $create_dirs) is set
112 ################################################################################
114 my $mailname = $mail_nr{$folder};
125 m/^$/ and ($ishead="");
128 ($hdr,$cnt) = ($`,$');
135 print "MAIL : $mailname\n";
136 print "FOLDER : $folder\n" if ($folder);
137 print "HTML : $html_short ($html_full)\n" if ($html);
138 print "ACCOUNT : $account\n" if ($account);
139 print "ATTACH : $attach_short ($attach_full)\n" if ($attach);
142 # write mail to folder
145 $targetdir = $outdir.'/'.$folder ;
147 foreach (split('/',$targetdir)) {
149 ( -d
$curdir) || mkdir $curdir;
153 open (OUTFILE
, ">".$targetdir.'/'.$mailname);
154 foreach (@lines) { print OUTFILE
"$_\n" ; }
158 include_attachment
($targetdir.'/'.$mailname);
163 ################################################################################
164 # make inline attachment from external file
165 # uses MIME::Parser, LWP::MediaTypes from www.cpan.org
166 # (Currently leaves a blank attachment in converted mails. Feel free to
167 # improve this script)
168 sub include_attachment
() {
169 my $mailname = shift ;
170 my $parser = new MIME
::Parser
;
176 $parser->output_to_core(1); # dont save to harddisk
177 $entity = $parser->parse_open($mailname);
179 # look for external attachments
180 foreach ($entity->head->get('X-Attachment')) {
181 if (m
/["']? # 1. start with " or ' (or none)
182 ([^"';]+) # word till quote or separator
184 \s?;\s? # separator ; (opt. spaces)
185 ["']? # 2. start (s.a.)
188 /x ) { $attachments{$1} = $2 ;
191 foreach ($entity->head->get('X-CalypsoHtmlBody')) {
192 if (m/["']? # 1. start with " or ' (or none
)
193 ([^"';]+) # word till quote or separator
195 \s?;\s? # separator ; (opt. spaces)
196 ["']?
# 2. start (s.o.)
199 /x ) { $attachments{$1} = $2 ;
202 foreach ($entity->head->get('X
-CalypsoHtmlImg
')) {
203 if (m/["']?
# 1. start with " or ' (or none)
204 ([^"';]+) # word till quote or separator
206 \s?;\s? # separator ; (opt. spaces)
207 ["']?
# 2. start (s.a.)
210 \s?;\s? # separator ; (opt. spaces)
211 ["']?
# 3. start (s.a.)
214 /x ) { $attachments{$1} = $3 ;
221 foreach my $key (keys (%attachments)) {
226 $fnam =~ tr#\\#/# if -d '/' ; # correct path names on unix like OS
227 $fnam = $mboxdir .'/'. $fnam ;
228 $type = guess_media_type($fnam);
230 if ( $type =~ m/text/i ) { $enc = "8bit" }
231 else { $enc = "base64" }
233 $entity->attach(Path => $fnam,
236 Filename => $attachments{$key}
240 my $lines = $entity->as_string ;
241 # correct images names in html messages
242 foreach (keys (%CID)) {
243 $lines =~ s/CID:$CID{$_}/$attachments{$_}/eg;
246 print $mailname."\n";
247 # qx(mv $mailname $mailname.bak);
248 open ( MAIL, ">".$mailname );
249 print( MAIL $lines );