package folder; BEGIN { *slog = \&::slog }; use Digest::SHA1; use constant MDIFVERSION => 1; sub new { my $class = shift; my %arg = @_; bless { path => "$::PREFIX/$arg{name}", %arg, }, $class; } sub dirty { $_[0]{dirty} = 1; } sub DESTROY { $_[0]->write_mdif; } # parse_mbox(mbox-file-path, callback) # callback gets called with \$header and \$body, # $header includes the mbox From_ line without # the leading From_ itself. sub parse_mbox { my ($path, $cb) = @_; open my $fh, "<", $path or die "$path: $!"; local $/ = "\n\n"; my ($head, $body, $offs); 5 == read $fh, $head, 5 or return; $head eq "From " or return; $offs = 0; while (defined ($head = <$fh>)) { $head =~ /^.*? [A-Z][a-z][a-z] [A-Z][a-z][a-z] [ 0-9][0-9] \d\d:\d\d:\d\d(?: [+-]\d\d\d\d)? \d\d(?:\d\d)\n/ or die "$path: not standard mbox format header:\n$head\n"; local $/ = "\nFrom "; # NEVER enable this. content-length simply is broken by design #if ($head =~ /^Content-Length:\s+(\d+)$/im) { # $1 <= read $fh, $body, $1 + 5 # or die "$path: partial message in mbox"; #} else { $body = <$fh>; #} chomp $body; $cb->($offs, \$head, \$body); $offs = (tell $fh) - 5; ::give unless ++$ecnt & 1023; } 1; } sub conf_path { (my $conf = $_[0]{path}) =~ s%([^/]+$)%.$1.mdif%; $conf; } sub read_mdif { my $self = shift; my $path = $self->conf_path; return if $self->{idx}; open my $fh, "<", $path or return; defined ($_ = <$fh>) or die "$path: empty mdif file\n"; do { if ($_ eq "[SYNCMAIL]\n") { while (<$fh>) { last unless /^([a-z]+)\s*=\s*(.*)\n$/; $self->{$1} = $2; } } elsif ($_ eq "[HOSTS]\n") { while (<$fh>) { last unless /^([^[].*)=(.*)\n$/; $self->{host}{$1} = $2; } } elsif (/^\[DIFF (\d+)\]\n$/) { my $mtime = $1; my (@add, @del); while (<$fh>) { last unless /^([+-])(.*)\n$/; if ($1 eq "+") { push @add, $2; } else { push @del, $2; } } unshift @{$self->{diff}}, [$mtime, \@add, \@del]; } elsif ($_ eq "[INDEX]\n") { my @idx; while (<$fh>) { last unless /^(\d+)=(.*)\n$/; push @idx, [$1, $2]; } $self->{idx} = \@idx; } elsif (/^#/) { $_ = <$fh>; # nop } else { die "$path: unparseable section '$_'\n"; } } while defined $_; $self->{version} <= MDIFVERSION or die "$path: version mismatch ($self->{version} found, <".MDIFVERSION." expected)\n"; } sub write_mdif { my $self = shift; my $path = $self->conf_path; return unless $self->{dirty}; open my $fh, ">", "$path~" or die "$path~: $!"; print $fh "# automatically generated, do NOT edit\n"; print $fh "[SYNCMAIL]\n"; print $fh "$_=$self->{$_}\n" for (qw(fsize mtime version)); print $fh "[HOSTS]\n"; while (my ($k,$v) = each %{$self->{host}}) { print $fh "$k=$v\n"; } print $fh "[INDEX]\n"; print $fh "$_->[0]=$_->[1]\n" for @{$self->{idx}}; for (reverse @{$self->{diff}}) { print $fh "[DIFF $_->[0]]\n"; print $fh "+$_\n" for @{$_->[1]}; print $fh "-$_\n" for @{$_->[2]}; } close $fh or die "$path~: unable to create updated .mdif: $!"; rename "$path~", $path; delete $self->{dirty}; } if (1) { use OpenSSL (); *hash = \&OpenSSL::Digest::sha1_hex; } elsif (0) { # use Digest::SHA1; my $digest = new Digest::SHA1; *hash = sub { $digest->reset; $digest->add(@_); $mid = $digest->hexdigest; }; } sub gendiff { my ($d1, $d2) = @_; my (@add, @del); my (%d1, %d2); for (@$d2) { undef $d2{$_->[1]}; } # delete msgs in d1 but not in d2 for (@$d1) { undef $d1{$_->[1]}; push @del, $_->[1] unless exists $d2{$_->[1]}; } %d2 = (); # conserve memory # add msgs in d2 but not in d1 for (@$d2) { push @add, $_->[1] unless exists $d1{$_->[1]}; } (\@add, \@del); } sub check { my $self = shift; my $path = $self->{path}; my $conf = $self->conf_path; my $guard = $::lockdisk->guard; slog 3, "checking $path\n"; if (stat $path) { my ($fsize, $mtime) = (stat _)[7, 9]; if (open my $fh, "<", $conf) { my %conf; <$fh>; # skip initial comment <$fh> eq "[SYNCMAIL]\n" or die "$conf: format error"; while (<$fh> =~ /^([a-z]+)\s*=\s*(.*)$/) { $conf{$1} = $2; } return 1 if $fsize == $conf{fsize} && $mtime == $conf{mtime}; $conf{mtime} <= $mtime or die "$path: folder older than mdif"; } slog 2, "updating $path\n"; my @idx; parse_mbox $path, sub { my ($offs, $head, $body) = @_; push @idx, [$offs, hash($$head, "\0", $$body)]; } or return (); $self->read_mdif; $self->{version} ||= MDIFVERSION; my ($add, $del) = gendiff $self->{idx}, \@idx; push @{$self->{diff}}, [ $mtime, $add, $del, ] if @$add || @$del; $self->{fsize} = $fsize; $self->{mtime} = $mtime; $self->{idx} = \@idx; $self->dirty; return 2; } else { slog 2, "$path: no longer exists\n"; unlink $conf; return (); } } 1;