diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2022-07-15 14:35:44 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2022-07-15 15:34:47 -0700 |
commit | 29c37dba62d23eafb021e9ae16112294d8651af7 (patch) | |
tree | 7774e1a16f44ae13c2c1d4506c03bd66fd6065fc /perl5 | |
parent | 7e2153ccac98df619ecde41b29878606b10c82b2 (diff) | |
download | dotfiles-29c37dba62d23eafb021e9ae16112294d8651af7.tar.gz |
tidy up to-mbox and from-mbox scripts
Additionally, archive-fmail-to-annex creates ~/.fmail/annex if missing, and
expand-annex-to-fmail creates ~/.fmail/annex/.expanded before trying to create
files under that directory.
Diffstat (limited to 'perl5')
-rw-r--r-- | perl5/Local/Homedir/Mail.pm | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/perl5/Local/Homedir/Mail.pm b/perl5/Local/Homedir/Mail.pm deleted file mode 100644 index 68fc6036..00000000 --- a/perl5/Local/Homedir/Mail.pm +++ /dev/null @@ -1,140 +0,0 @@ -package Local::Homedir::Mail; - -# Copyright (C) 2019 Sean Whitton -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or (at -# your option) any later version. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -use 5.028; -use strict; -use warnings; -use constant THIRTYONE => 31 * 24 * 60 * 60; - -use File::Spec::Functions qw(catfile splitpath); -use File::Temp qw(tempdir); -use File::Path qw(remove_tree); -use IO::Compress::Gzip qw(gzip $GzipError); -use IO::Uncompress::Gunzip qw(gunzip $GunzipError); -use Mail::Box::Manager; -use Exporter 'import'; - -our @EXPORT_OK = qw( archive_to_mbox expand_mbox ); - -sub archive_to_mbox { - my ($source_path, $mbox_path, $expanded_path) = @_; - - # bail out if compressed mbox exists: that means this month's - # archive has already happened. it's okay to append to a - # not-yet-compressed mbox, as that probably means that the - # archival process was interrupted - die -"wanted to archive $source_path to $mbox_path but $mbox_path.gz already exists" - if -e "$mbox_path.gz"; - - # each invocation of the archive subroutine has its own - # Mail::Box::Manager because we want to call closeAllFolders and - # thereby perform all the moves and copies - my $mgr = Mail::Box::Manager->new(); - - my $source - = $mgr->open($source_path, access => 'rw', create => 0, keep_dups => 1); - my $mbox = $mgr->open( - $mbox_path, - access => 'a', - create => 1, - keep_dups => 1, - type => 'mbox' - ); - my $expanded = $mgr->open( - $expanded_path, - access => 'a', - create => 1, - type => 'maildir', - ); - - my $now = time(); - foreach my $message ($source->messages()) { - next unless $now - $message->timestamp > THIRTYONE; - next unless $message->label('seen'); - next if $message->label('flagged'); - - $mgr->copyMessage($expanded, $message); - $mgr->moveMessage($mbox, $message); - } - - $expanded->acceptMessages; - $mgr->closeAllFolders; - open my $touch_fh, ">", - catfile($expanded_path, ".expanded", (splitpath $mbox_path)[2]); - gzip($mbox_path, "$mbox_path.gz") - or die "gzip failed: $GzipError\n"; - unlink $mbox_path if -e "$mbox_path.gz"; -} - -sub expand_mbox { - my ($source_path, $target) = @_; - (my $source_name = (splitpath $source_path)[2]) =~ s/\.gz$//; - my $expanded_path = catfile $target, ".$source_name"; - my $donefile = catfile $target, ".expanded", $source_name; - - # Check whether we finished, or got partway there. - return if -e $donefile; - remove_tree($expanded_path) if -e $expanded_path; - - -e or mkdir for $target, map catfile($target, $_), "cur", "new", "tmp"; - - # unzip it to (what is hopefully a) tmpfs, since Mail::Box can - # only accept a path to an unzipped mbox - my $dir = tempdir(CLEANUP => 1, DIR => "/tmp"); - chmod 0700, $dir; - my $unzipped = catfile($dir, "unzip.mbox"); - gunzip($source_path, $unzipped) or die "gunzip failed: $GunzipError\n"; - - my $mgr = Mail::Box::Manager->new(); - my $source = $mgr->open( - $unzipped, - access => 'r', - type => 'mbox' - ); - my $expanded = $mgr->open( - $expanded_path, - access => 'a', - create => 1, - type => 'maildir' - ); - - foreach my $message ($source->messages()) { - $message->label(flagged => 0, seen => 1); - $mgr->copyMessage($expanded, $message); - } - $source->close(write => "NEVER"); - $expanded->acceptMessages; - $expanded->close; - - # Now we move all the files the old fashioned way, so that it's fast. - # It's safe because the filenames begin with a timestamp, so there really - # oughtn't be any collisions, and in any case this whole thing is a cache. - opendir my $dirh, catfile $expanded_path, "cur"; - while (readdir $dirh) { - my $s = catfile $expanded_path, "cur", $_; - -f $s and rename $s, catfile $target, "cur", $_; - } - open my $touch_fh, ">", $donefile; - - # nuke the tempdir now to avoid running out of ramdisk if we're - # expanding a lot of mboxes - remove_tree($dir); - remove_tree($expanded_path); -} - -1; |