oko1 has asked for the wisdom of the Perl Monks concerning the following question:
I just wrote a script that does what I want - i.e., trimming a "catch-all" mailbox to just the last N days worth of emails. However, I'm a little uncomfortable with the basic design of the program - it just seems like there should be a smarter way to do this, somehow. Any suggestions for improvement would be appreciated.
(Incidentally, the reason that I'm using 'date --date=""' is that a) it's very smart about figuring out the variety of dates one runs into in email headers, and b) the various modules I've tried are either not smart enough or slower than 'date'.)
#!/usr/bin/perl use warnings; use strict; die "Usage: ", $0 =~ /([^\/]+)$/, " <mbox> [days]\n" unless -f $ARGV[0] && @ARGV >= 1 && @ARGV <= 2; my $days = $ARGV[1] || 3; pop if $ARGV[1]; my $cutoff = time - $days * 60 * 60 * 24; my ($found, $content); while (<>){ print && next if $found; if (/^From /../^$/){ $content .= $_; if (/^Date: ([-+:,)(\w ]+)$/){ my $mdate = `date --date="$1" "+%s"`; if ($mdate >= $cutoff){ $found++; print $content; } } } else { $content = ""; } }
Update: Thanks to the advice from [thargas], the date is now validated. Despite the jocular tone in my response to his post, it was a serious issue - a 'Date: $(rm -rf ~/*)' would indeed have done some serious damage if it somehow got through the mail filters.
-- I hate storms, but calms undermine my spirits. -- Bernard Moitessier, "The Long Way"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trimming a mailbox
by thargas (Deacon) on Feb 13, 2012 at 12:01 UTC | |
by oko1 (Deacon) on Feb 13, 2012 at 18:02 UTC | |
|
Re: Trimming a mailbox
by chrestomanci (Priest) on Feb 13, 2012 at 13:25 UTC | |
by oko1 (Deacon) on Feb 13, 2012 at 18:16 UTC |