Skip to content

Instantly share code, notes, and snippets.

@schubev
Created September 5, 2018 14:51
Show Gist options
  • Save schubev/774be6c5199ffca9215cb917ab1c5f97 to your computer and use it in GitHub Desktop.
Save schubev/774be6c5199ffca9215cb917ab1c5f97 to your computer and use it in GitHub Desktop.
Null-separated list of old mails from maildir mailboxes.
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say);
use File::Spec::Functions 'catfile';
use Date::Parse;
my $keep_for_seconds = 60 * 60 * 24 * 60;
my $mail_root = '/home/schube/Mail';
my @mail_dir_names = ('Inbox', 'Jira', 'Github', 'Sentry/Assigns', 'Newrelic', 'Build');
sub maildate {
open MAIL, "<", shift;
while (<MAIL>) {
if (s/^Date: //) {
close MAIL;
return str2time($_);
}
}
close MAIL;
}
sub maildates {
my $maildirpath = shift;
my %mail_dates;
opendir(my $dirhandle, $maildirpath) or die;
while (readdir $dirhandle) {
next if /^\./;
my $mail_path = "$maildirpath/$_";
my $date = maildate $mail_path;;
$mail_dates{$mail_path} = $date;
}
closedir $dirhandle;
return %mail_dates;
}
sub main {
my $now = time;
my $cutoff = $now - $keep_for_seconds;
chdir $mail_root;
foreach(@mail_dir_names) {
my $mail_dir_path = catfile '.', $_, 'cur';
my %mail_dates = maildates $mail_dir_path;
my @expired_mail_paths;
foreach my $key (keys %mail_dates) {
print "$key\0" if $mail_dates{$key} < $cutoff;
}
}
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment