1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
|
#!/usr/bin/perl -w
use strict;
use Mail::Box::Manager;
my $VERSION = '2.107';
Mail::Box->VERSION($VERSION);
# file-globals.
my ($manager, @folderpage, @messagepage);
sub create_folder(@);
sub create_message(@);
sub clean_dir($);
sub default_folderpage();
sub default_messagepage();
=head1 NAME
mailbox2html - EXPERIMENTAL: convert mail folders into an HTML structure
=head1 SYNOPSIS
mailbox2html [-src folder] [-dest directory] [-norecurse]
=cut
sub usage($)
{ my $rc = shift;
warn <<USAGE;
Usage: $0 [options]
options:
-cleanup clean the destination directory before producing
-dest directory output location of translated
-folderpage filename template for folder-pages
-help -? show this help
-index filename name of the directory-index
-messagepage filename template for message-pages
-norecurse descend into sub-folders
-src folder folder(-directory) to be translated into html
-template the page-template file
-verbose verbose messages
USAGE
exit $rc;
}
my %option =
( cleanup => 0
, dest => exists $ENV{TMPDIR} ? "$ENV{TMPDIR}/mail2html"
: -d '/var/tmp' ? '/var/tmp/mail2html'
: -d '/tmp' ? '/tmp/mail2html'
: '.'
, folderpage => undef
, help => 0
, index => 'index.html'
, messagepage => undef
, recurse => 1
, src => exists $ENV{MAIL} ? $ENV{MAIL}
: exists $ENV{mail} ? $ENV{mail}
: exists $ENV{HOME} && -d "$ENV{HOME}/Mail" ? "$ENV{HOME}/Mail"
: exists $ENV{home} && -d "$ENV{home}/Mail" ? "$ENV{home}/Mail"
: '.'
, verbose => 0
);
sub get_options()
{ use Getopt::Long;
GetOptions \%option
, 'src=s'
, 'dest=s'
, 'folderpage=s'
, 'index=s'
, 'messagepage=s'
, 'recurse!'
, 'verbose!'
, 'cleanup!'
, 'help|?!';
}
sub trace(@) { warn @_,"\n" if $option{verbose} }
sub clean_dir($)
{ my $dir = shift;
trace "cleaning $dir.\n";
opendir DIR, $dir or return;
my (@files, @directories);
while(my $entry = readdir DIR)
{ next if $entry =~ m/^\.\.?$/;
if(-d $entry) {push @directories, $entry}
else {push @files, $entry}
}
closedir DIR;
unlink "$dir/$_" for @files;
clean_dir "$dir/$_" for @directories;
unlink $dir;
}
sub create_uplinks(@)
{ return () unless @_;
my @links = ('<B>'.(pop).'</B>');
my $href = '..';
while(@_)
{ unshift @links, "<A HREF=$href>".(pop)."</A>";
$href .= '/..';
}
@links;
}
sub create_folder(@)
{ my %args = @_;
my $folder = $args{folder};
my $name = $folder->name;
#
# Output location
#
my $dirname = "$args{dest}/$name";
for($dirname) # some cleanups.
{ s!/\./!/!g;
s!/\=(/|$)!/!g;
}
unless(-d $dirname)
{ trace "Create directory $dirname.";
mkdir $dirname, 0700
or die "Cannot create directory $dirname: $!\n";
}
my $filename = "$dirname/$option{index}";
#
# Sub-directory preparations.
#
my @subfolders;
if($option{recurse})
{ my @subs = sort $folder->listSubFolders;
my $prev;
for(my $i=0; $i<@subs; $i++)
{
my @path = $args{path} ? @{$args{path}} : ();
my $sub = $folder->openSubFolder($subs[$i]);
my $sum = create_folder
( folder => $sub
, dest => $args{dest}
, path => [ @path , $name]
, previous => ($i <= 0 ? ''
: "Previous: <A HREF=\"../$subs[$i-1]\">$subs[$i-1]</A>")
, next => ($i >= @subs-1 ? ''
: "Next: <A HREF=\"../$subs[$i+1]\">$subs[$i+1]</A>")
);
push @subfolders, $sum;
}
}
#
# Summerize data about this folder.
#
trace "processing folder $name";
my @uplinks = create_uplinks @{$args{path}};
my @messages = $folder->messages;
my %sum = # collect info for super-folder, so no place
( foldername => $name # for sub-folder stuff.
, foldersize => 0
, seen_messages => 0
, nr_messages => scalar @messages
, nr_subfolders => scalar @subfolders
);
$sum{foldersize} += $_->{foldersize} foreach @subfolders;
$sum{newest} = $sum{oldest} = $messages[0]->timestamp if @messages;
foreach my $message (@messages)
{ $sum{deleted}++ if $message->deleted;
$sum{seen_messages}++ if $message->label('seen');
$sum{foldersize} += $message->size;
if(my $timestamp = $message->timestamp)
{ $sum{newest_message} = $timestamp if $timestamp > $sum{newest};
$sum{oldest_message} = $timestamp if $timestamp < $sum{oldest};
}
create_message
( message => $message
, folder => $folder
, manager => $manager
, dirname => $dirname
);
}
$sum{new_messages} = @messages - $sum{seen_messages};
#
# Page output.
#
local *HTML;
open HTML, '>', $filename
or die "Cannot create $filename: $!\n";
my $oldout = select HTML;
use_template
( template => \@folderpage
, folder => $folder
, subfolders => \@subfolders
, messages => \@messages
, manager => $manager
, %sum
);
select $oldout;
close HTML;
\%sum;
}
sub create_message(@)
{ my %args = @_;
my $message = $args{message};
# f.i. usable for constructing links, because all specials are stripped.
my $clean_id = $message->messageID;
for($clean_id)
{ s/^\s*\<(.*?)\>\s*$/$1/g;
tr/a-zA-Z0-9_/-/cs;
}
my $filename = "$args{dirname}/${clean_id}.html";
my $subject = $message->head->get('subject') || '<no subject>';
chomp $subject;
$message->label
( clean_id => $clean_id
, filename => $filename
, subject => $subject
);
local *HTML;
open HTML, '>', $filename
or die "Cannot create $filename: $!\n";
my $oldout = select HTML;
use_template
( template => \@messagepage
, message => $message
, filename => $filename
, folder => $args{folder}
, subject => $subject
, manager => $manager
, indexfile=> $option{index}
);
select $oldout;
close HTML;
}
sub parse_template($@)
{ my $name = shift;
my (@parts, $rest);
while(@_)
{ $rest .= shift;
while($rest =~ m/\<\#perl\b/)
{ push @parts, $`;
$rest = $';
$rest .= shift while @_ && $rest !~ m/\#\>/;
$rest =~ m/\#\>/
or die "Perl part not closed in $name.\n";
(my $code, $rest) = split /\s*\#\>\s*/, $rest, 2;
if($code =~ s/^\s*BEGIN\s+//)
{ WEBPAGE::run_code($code);
$rest = (pop @parts).$rest;
}
else
{ push @parts, $code;
}
}
}
push @parts, $rest;
@parts;
}
sub use_template(@)
{ my %args = @_;
foreach (keys %args)
{ no strict 'refs';
${"WEBPAGE::$_"} = $args{$_};
}
my $template = $args{template};
my $lines = @$template;
for(my $t=0; $t<$lines; $t++)
{ print $template->[$t++];
WEBPAGE::run_code($template->[$t]) if $t<$lines;
}
}
package WEBPAGE;
no strict;
sub run_code($)
{ eval shift;
die $@ if $@;
}
package main;
use strict;
#####
##### MAIN
#####
usage 22 unless get_options;
usage 0 if $option{help};
# Prepare destination directory.
clean_dir $option{dest}
if $option{cleanup} && -d $option{dest};
unless(-d $option{dest})
{ trace "Creating $option{dest}.";
mkdir $option{dest}, 0700
or die "Cannot create destination directory $option{dest}: $!\n";
}
#
# Get the configuration
#
# Parse folderpage.
if(my $foldtempl = $option{folderpage})
{ trace "Parsing folderpage-template from $foldtempl.";
open TEMPLATE, $foldtempl
or die "Cannot read $foldtempl: $!\n";
@folderpage = parse_template 'folderpage', <TEMPLATE>;
close TEMPLATE;
}
else
{ trace "Taking default folderpage template.";
@folderpage = parse_template 'default folderpage', default_folderpage;
}
# Parse messagepage.
if(my $msgtempl = $option{messagepage})
{ trace "Parsing messagepage-template from $msgtempl.";
open TEMPLATE, $msgtempl
or die "Cannot read $msgtempl: $!\n";
@messagepage = parse_template 'messagepage', <TEMPLATE>;
close TEMPLATE;
}
else
{ trace "Taking default messagepage template.";
@messagepage = parse_template 'default messagepage', default_messagepage;
}
#
# Start handling the folder
#
trace "Start folder manager.";
$manager = Mail::Box::Manager->new;
trace "Opening folder `$option{src}'.";
my $folder = $manager->open
( folderdir => $option{src}
, folder => '=' # open folderdir
, extract => 'ALWAYS' # need all messages anyway.
);
die "Cannot open folder `$option{src}'.\n"
unless $folder;
trace "Folder-type is ",ref $folder, ".";
create_folder
( dest => $option{dest}
, folder => $folder
);
#-------------------------------------------
sub default_folderpage()
{ <<'DEFAULT_FOLDERPAGE';
<#perl BEGIN
use File::Basename;
sub nicemessage($)
{ my $msg = shift;
'<A HREF="'.(basename $msg->label('filename')).'">'
. $msg->label('subject') . '</A>';
}
sub polish($)
{ my @lines = split /\n/, shift;
foreach (@lines)
{ if( my ($layout,$rest) = m/^(.*?)(\<.*)$/ )
{ $layout =~ s/ / /g;
$_ = "<TT>$layout</TT>$rest";
}
s/$/<BR>\n/;
}
@lines;
}
#>
<HTML>
<HEAD><TITLE><#perl print $foldername#></TITLE></HEAD>
<BODY BGCOLOR=#FFFFFF TEXT=#000000>
See the templates as included in the Mail::Box-package for nicer output
and more detailed examples.
<#perl
print "SUB: $nr_subfolders ".@$subfolders."\n";
print "NR=$nr_subfolders, NR=".@$subfolders."\n";
if($nr_subfolders)
{ print "<H4>$nr_subfolders subfolders:</H4>\n";
foreach (@$subfolders)
{ my $subname = basename $_->{foldername};
print "<A HREF=\"$subname/$indexfile\">$subname</A> "
, "($_->{nr_messages} messages)<BR>\n";
}
}
if($nr_messages)
{ print "\n<H4>$nr_messages messages:</H4>\n";
print polish($_->threadToString(\&nicemessage))
foreach $manager->threads(folder => $folder)->sortedAll;
}
#>
</BODY></HTML>
DEFAULT_FOLDERPAGE
}
sub default_messagepage()
{ <<'DEFAULT_MESSAGEPAGE';
<HTML><HEAD><TITLE><#perl print $subject#></TITLE></HEAD>
<BODY BGCOLOR=#FFFFFF TEXT=#000000>
<PRE>
<#perl if($message) {$message->print(select)} else {print '<undef>'} #>
</PRE>
</BODY></HTML>
DEFAULT_MESSAGEPAGE
}
#-------------------------------------------
__END__
=head1 DESCRIPTION
Convert various kinds of mail folders to HTML, permitting them to be
read by a web browser.
Options:
=over 4
=item -cleanup =E<gt> BOOLEAN
(or C<-cleanup> or C<-nocleanup>) removes the directory before generating
the data.
=item -dest =E<gt> DIRECTORY
The output directory. When this option is not specified, the program will
try to create a sub-directory named C<mail2html> in the directory pointed
to by the environment variable TMPDIR or tmpdir, otherwise the output will
be in C</var/tmp> or C</tmp>.
=item -folderpage =E<gt> FILENAME
# DWC Hm... 2nd sentence doesn't make sense
The template which is used to create folder pages. See the examples with
the distribution of this code for examples.
=item -help =E<gt> BOOLEAN
(or C<-help> or C<-?>) Print a brief help
=item -index =E<gt> FILENAME
The filename which the web server loads when the user specifies only the
name of the directory in a URL. This option is set to C<index.html> by
default.
=item -messagepage =E<gt> FILENAME
# DWC Hm... 2nd sentence doesn't make sense
The template which is used to create one page per message.
See the examples with the distribution of this code for examples.
=item -recurse =E<gt> BOOLEAN
(or C<-recurse> or <-norecurse>) Recurse through subfolders. By default,
recursive production is enabled.
=item -src =E<gt> FILE | DIRECTORY
Start with the folder(-directory) which can be found at the specified
location. The type of folder is auto-detected. By default, the contents
of the directory specified by the C<MAIL> or C<mail> environment variable
are used. If these variables are not defined, C<HOME> and C<home> are
checked for a sub-directory named C<Mail>. If these are unsuccessful as
well, the current directory is used.
=item -verbose =E<gt> BOOLEAN
(or C<-verbose> or C<-noverbose>) Be verbose about the progress of the
program. This is especially useful for debugging purposes.
=back
=head1 AUTHOR
Mark Overmeer (F<Mark@Overmeer.net>).
All rights reserved. This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
=head1 VERSION
This code is beta, version 2.107
=cut
1;
|