#!/usr/bin/perl -w
##
#
#    [ voids passim ]
#      
#    A blast from the past ...
#    relive those heady days when men 
#    were men, posts were long and 
#    there were people called Candace on 
#    the mailing list.
#
#    This script will take a standard mbox
#    of void archived mail, in the form 
#       void-YYYY-MM
#    and mail them out, exactly 1 year later.
# 
#    How cool is that?
#     
#    It's like google groups only more embarassing.
#
#    (c)opyright 2002, simon wistow <simon@thegestalt.org>
#    Distributed under the same terms as Perl itself
#
#
#    This software is under no warranty and will probably 
#    destroy your computer, kill your friends, burn your 
#    house and bring about the apocalypse.
#
##


# import lots and lots and lots of stuff
use strict;
use Mail::MboxParser;
use Date::Handler;
use Date::Handler::Delta;
use Date::Format;
use Date::Parse;
use MIME::Lite;


## 
#
# Variables
#
##

# the window of time the mail must be in 
# this should be the same amount of time as the cron 
# interval you're running this script at
my $DELAY = 1; 

# who gets the mail
my @RECIPIENTS = qw(passim@thegestalt.org);

# where your mail archives are kept
my $ARCHIVES = 'mail/';

# how to send your mail
my $SENDMAIL = "/usr/lib/sendmail -oi -oem";

# what we should munge the subject line from
my $SUBJECT_FROM = '\(void\)';  # this needs to be Regexp meaningful characters quoted 
my $SUBJECT_TO   = '(passim)';


##
#
# Code
#
##

# work out the date now
my $date = new Date::Handler (date => time, timezone=>'GMT');
# .. and subtract a year
my $delta = new Date::Handler::Delta({
                                      years => 1,
                                      months => 0,
                                      days => 0,
                                      hours => 0,
                                      minutes => 0,
                                      seconds => 0,
                                     });

$date -= $delta;


# work out which mail folder to use
my $folder = sprintf "%s/void-%s-%s",$ARCHIVES, $date->Year, $date->Month;
my $parser = new Mail::MboxParser($folder) || die "Couldn't open $folder\n";

# and loop through every mail
while (my $msg = $parser->next_message)
{
	# work out the date this was sent
	my $msgdate  = new Date::Handler(date => str2time($msg->header->{date}));

	# and how much difference there is between the two of them
	my $msgdelta = $date - $msgdate;
	my $seconds  = $msgdelta->Seconds();

	# bug out if we've gone passed
        # WARNING! This will fail if mailboxes aren't sorted in order
	last if ($seconds<0);

	# if it's in the window then, ermm, handle it
	handle($msg) if ($seconds <= $DELAY*60 && $seconds>0);

}


# do whatever we want to do
sub handle
{
	my ($msg) = @_;

	# sort out the headers
	my $from    =  $msg->header->{from};
	my $to      =  'passim@thegestalt.org';
	my $date    = new Date::Handler(date => str2time($msg->header->{date}));
	my $subject = $msg->header->{subject};

	# munge the subject line
	$subject =~ s!$SUBJECT_FROM!$SUBJECT_TO!g;

	# Debug ....
	# print "$subject\n";

	# create a new message
	my $newmsg = MIME::Lite->new(
			  Date    => $date,
                          From    => $from,
                          To      => $to,
                          Subject => $subject,
			  Data    => $msg->body()->as_string(),
			  );


	# and send it
        open SENDMAIL, "| $SENDMAIL @RECIPIENTS" or die "couldn't pipe to sendmail: $!";
        $newmsg->print(\*SENDMAIL);
        close SENDMAIL;



}




syntax highlighted by Code2HTML, v. 0.9