Created
November 27, 2015 13:27
-
-
Save andrewalker/36d58c19ff2a8a5fb41c to your computer and use it in GitHub Desktop.
Deduplicating my mailbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Playing with mbsync, I ended up duplicating a few hundred messages when tweaking config | |
# This is the fix :) | |
use utf8; | |
use warnings; | |
use strict; | |
use Mail::Box::Manager; | |
use List::Util qw/sum0/; | |
my $mgr = Mail::Box::Manager->new(access => 'rw'); | |
my $folder = $mgr->open('/path/to/maildir'); | |
dedup_rec($folder, 0); | |
sub dedup_rec { | |
my ($folder, $level) = @_; | |
my %messages; | |
my @messages = $folder->messages; | |
for my $subfolder ($folder->listSubFolders) { | |
dedup_rec($folder->openSubFolder($subfolder, access => 'rw'), $level+1); | |
} | |
info($level => "Going to process folder @{[ $folder->name ]}"); | |
if (scalar @messages <= 1) { | |
return; | |
} | |
for my $msg (@messages) { | |
my $subj = $msg->subject; | |
my $from = join ', ', map { $_->format } $msg->from; | |
my $date = $msg->get('Date'); | |
if ($messages{$subj . $from . $date}++) { | |
$msg->delete; | |
} | |
} | |
my $sum = sum0 map { $_ - 1 } values %messages; | |
info($level => "Deduped $sum messages in @{[ $folder->name ]}"; | |
} | |
sub info { | |
my ($level, $msg) = @_; | |
print "\t" x $level; | |
print "$msg\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment