-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-dup-mails-from-maildir.plx
executable file
·121 lines (104 loc) · 3.66 KB
/
remove-dup-mails-from-maildir.plx
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
#!/usr/bin/perl
use strict;
use warnings;
use Mail::Header;
use File::Copy;
if (@ARGV < 2) {
print STDERR "usage: $0 <TYPE> <SOURCE_MAILDIR_FOLDER_PATH> [<MALDIRS_LOOK_FOR_DUPS_IN> ...]\n";
exit 1;
}
my($TYPE, $MAILDIR_FOLDER) = ($ARGV[0], $ARGV[1]);
my (@dupFolders) = @ARGV[2..$#ARGV];
my %msgs; # indexed by Message-Id
foreach my $folder (@dupFolders) {
my @msgDirs = ("$folder/cur", "$folder/new");
foreach my $dir (@msgDirs) {
die "$MAILDIR_FOLDER must not be a maildir folder (or is unreadable by you), since $dir isn't a readable directory: $!"
unless (-d $dir);
}
foreach my $dir (@msgDirs) {
opendir(MAILDIR, $dir) or die "Unable to open directory $dir for reading: $!";
while (my $file = readdir MAILDIR) {
next if -d $file; # skip directories
my $existing_file = "$dir/$file";
open(MAIL_MESSAGE, "<", $existing_file) or
die "unable to open $existing_file for reading: $!";
my $header = new Mail::Header(\*MAIL_MESSAGE);
my $fields = $header->header_hashref;
my $id = $fields->{'Message-ID'}[0];
chomp $id;
if ($id !~ s/^\s*\<?\s*(\S+)\s*\>?.*$/$1/) {
$id = $fields->{'Resent-Message-ID'}[0];
chomp $id;
unless ($id =~ s/^\s*\<?\s*(\S+)\s*\>?.*$/$1/) {
warn "weirdly formatted, or missing Message-ID (or Resent-Message-ID), \"$id\" in $dir/$file";
next;
}
}
if (not defined $id or $id =~ /^\s*$/) {
warn "$dir/$file has no message ID";
next;
}
# die "Duplicate message ID's $id\n" if defined $msgs{$id};
$msgs{$id} = $fields;
}
close MAIL_MESSAGE;
}
close MAILDIR;
}
# This code shouldn't be all cut-and-pasty from the above, but I was in a hurry.
my @msgDirs = ("$MAILDIR_FOLDER/cur", "$MAILDIR_FOLDER/new");
foreach my $dir (@msgDirs) {
die "$MAILDIR_FOLDER must not be a maildir folder (or is unreadable by you), since $dir isn't a readable directory: $!"
unless (-d $dir);
}
foreach my $dir (@msgDirs) {
opendir(MAILDIR, $dir) or die "Unable to open directory $dir for reading: $!";
while (my $file = readdir MAILDIR) {
next if -d $file; # skip directories
my $existing_file = "$dir/$file";
open(MAIL_MESSAGE, "<", $existing_file) or
die "unable to open $existing_file for reading: $!";
my $header = new Mail::Header(\*MAIL_MESSAGE);
my $fields = $header->header_hashref;
my $id = $fields->{'Message-ID'}[0];
chomp $id;
if ($id !~ s/^\s*\<?\s*(\S+)\s*\>?.*$/$1/) {
$id = $fields->{'Resent-Message-ID'}[0];
chomp $id;
unless ($id =~ s/^\s*\<?\s*(\S+)\s*\>?.*$/$1/) {
warn "weirdly formatted, or missing Message-ID (or Resent-Message-ID), \"$id\" in $dir/$file";
next;
}
}
if (not defined $id or $id =~ /^\s*$/) {
warn "$dir/$file has no message ID";
next;
}
# If we already have this message elsehwere, then we simply remove it
# from this folder here. Otherwise, we note that we have it by adding
# it to %msgs.
if (defined $msgs{$id}) {
if ($TYPE eq "print") {
print "$id\n";
} elsif ($TYPE eq "svn") {
system("svn rm \"$existing_file\"");
die "Unable to unlink file $existing_file: $!"
unless $? == 0;
} else {
print STDERR "Removing $existing_file\n";
die "Unable to unlink $existing_file: $!"
unless unlink($existing_file) == 1;
}
} else {
$msgs{$id} = $fields;
}
close MAIL_MESSAGE;
}
close MAILDIR;
}
###############################################################################
#
# Local variables:
# compile-command: "perl -c remove-dup-mails-from-maildir.plx"
# End: