summaryrefslogtreecommitdiff
path: root/awm
blob: 37792013e048b5955dbb77a1322bed4a9e0f3473 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/perl -w

# awm - Archive Weasel's Mail
#
#    Copyright (C) 2002,2013 Peter Palfrader <peter@palfrader.org>
#
#    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 2 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, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

=pod

=head1 NAME

awm - Archive Weasel's Mail

=head1 SYNOPSIS

=over

=item B<awm> [B<--help>]

=item B<awm> [B<--quiet>] [B<--verbose> [B<--verbose>]] [B<--older=>I<date>] B<--base=>I<base> [I<folder> [I<folder> ...]]

=back

=head1 DESCRIPTION

B<awm> is a simple mail archiver for MailDir style mail folders. It moves mails
older than the specified time to I<folder>.I<yyyy.mm> folders where I<yyyy.mm>
is the date when the mail was received (last Received header) or sent (Date
header) if no Received header can be found.

It archives the Maildir folder I<base> as well as all subfolders (Maildirs in the
same directory as I<base> that name starts with a period (B<.>)).  If you list
one of more folders it will only archive them.

=head1 OPTIONS

=over

=item B<--verbose>

Verbose mode. Causes B<awm> to print debugging messages about its progress. May
be used more than once to increase verbosity.

=item B<--quiet>

Quiet mode. Be even more quient than normally (supress some warnings).

=item B<--help>

Print a short help and exit sucessfully.

=item B<--version>

Print version number and exit sucessfully.

=item B<--base>=I<base>

Specify the base of B<awm>'s operation. Example: C<--base=$(HOME)/Maildir>.

=item B<--older>=I<date>

Archive mails older than the specified date. Example: C<--older='-2 months'>
or C<--older='2002-01-01'>. Defaults to two months ago. See
C<Time::ParseDate(3)> for reognised formats.

=back

=head1 AUTHOR

Peter Palfrader E<lt>peter@palfrader.orgE<gt>

=head1 BUGS

Please report them to the author.

=cut

use strict;
use FindBin qw{ $Bin };
use Getopt::Long;
use Time::ParseDate;
use Mail::Internet;
use English;

my $VERBOSE = 0;
my $QUIET = 0;
my $VERSION = '0.0.1';
my $ARCHIVE_OLDER = '-2 months';
my $BASE;

sub make_target($$$) {
	my ($folder, $dir, $time) = @_;

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($time);
	$folder .= sprintf(".%04d.%02d", $year+1900, $mon+1);
	$folder =~ s/^\.\././;
	unless (-d $folder) {
		for my $d ($folder, $folder.'/cur', $folder.'/new', $folder.'/tmp') {
			mkdir ($d) or
				warn ("$PROGRAM_NAME: Cannot mkdir $d: $!"),
				return undef;
		};
	};
	return $folder.'/'.$dir;
};

sub readfile($) {
	my ($file) = @_;
	open (F, $file) or
		warn("$PROGRAM_NAME: Cannot open $file: $!\n"),
		return undef;
	my @result = <F>;
	close (F) or
		warn("$PROGRAM_NAME: Cannot read $file: $!\n"),
		return undef;
	return \@result;
};

sub gettime($) {
	my ($file) = @_;
	my $maillines = readfile($file) or
		warn("$PROGRAM_NAME: Cannot read $file\n"),
		return undef;
	
	my $mail = new Mail::Internet($maillines);
	my $header = $mail->head();
	my $received = $header->get('Received', 0);
	my ($date, $time);
	($date) = $received =~ /;([^;]+)$/ if defined $received;
	$time = parsedate($date, GMT => 1) if defined $date;
	unless (defined $time) {
		$QUIET or warn ("$PROGRAM_NAME: Falling back to date header in $file\n");
		$date = $header->get('Date');
		$time = parsedate($date, GMT => 1) if defined $date;
		warn ("$PROGRAM_NAME: Cannot get time for mail $file\n"),
			return undef
			unless defined $time;
	};
	return $time;
};

sub archive_MailDir($$) {
	my ($older, $folder) = @_;
	for my $dir (qw{cur new}) {
		opendir(DIR, $folder.'/'.$dir);
		my @files = grep { ! /^\./ } readdir(DIR);
		closedir(DIR);

		for my $file (@files) {
			my $time = gettime($folder.'/'.$dir.'/'.$file) or
				warn("$PROGRAM_NAME: Cannot get time from $folder/$dir/$file\n"),
				next;
			my $date = gmtime($time);
			if ($time < $older) {
				my $target = make_target($folder, $dir, $time);
				warn ("$PROGRAM_NAME: Target not defined\n"), next unless defined $target;
				print "  Moving $folder/$dir/$file to $target/$file ($date)\n"
					if ($VERBOSE >= 2);
				link($folder.'/'.$dir.'/'.$file, $target.'/'.$file) or 
					warn ("$PROGRAM_NAME: Cannot move $folder/$dir/$file to $target/$file\n"),
					next;
				unlink($folder.'/'.$dir.'/'.$file) or
					warn ("$PROGRAM_NAME: Cannot unlink $folder/$dir/$file\n"),
					next;
			} else {
				print "  Not moving $folder/$dir/$file ($date)\n"
					if ($VERBOSE >= 2);
			};
		};
	};
};


my $HELP = 0;
my $PRINTVERSION = 0;


my $USAGE = "Usage: $PROGRAM_NAME [--help] [--quiet] [--verbose [--verbose]] [--older=<date>] --base=<base> [folder [folder]]\n";
Getopt::Long::config('bundling');
GetOptions (
	'--verbose+' => \$VERBOSE,
	'--quiet' => \$QUIET,
	'--base=s' => \$BASE,
	'--older=s' => \$ARCHIVE_OLDER,
	'--help' => \$HELP,
	'--version' => \$PRINTVERSION,
) &&
(defined $BASE) or
	die ($USAGE);
$HELP and
	print $USAGE,
	exit(0);
$PRINTVERSION and
	print "$PROGRAM_NAME version $VERSION",
	exit(0);
if ($ARCHIVE_OLDER =~ /[^0-9]/) {
	$ARCHIVE_OLDER = parsedate($ARCHIVE_OLDER, PREFER_PAST => 1, WHOLE => 1, GMT => 1);
	die ("Cannot parse timestamp\n") unless defined $ARCHIVE_OLDER;
};


my @files;

chdir($BASE);
umask(0007);
if (scalar @ARGV) {
	@files = @ARGV
} else {
	opendir(DIR, '.');
	@files = grep { $_ ne '..' && $_ ne 'cur' && $_ ne 'new' && $_ ne 'tmp' && -d $_ && (! m/\.\d\d$/) } readdir(DIR);
	closedir(DIR);
};

for my $dir (@files) {
	print "Doing $dir\n"
		if ($VERBOSE >= 1);
	archive_MailDir($ARCHIVE_OLDER, $dir);
};


# vim:set ts=2:
# vim:set shiftwidth=2: