2 # delay_mail delays mail and requeus it, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2006 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 495 2006-08-10 08:02:01Z don $
17 delay_mail - Delay mail to a specific time and send it back
21 delay_mail [options] < mail_message
24 --enqueue enqueues a message for sending out later
25 --delay length of delay (suitable for passing to at)
26 --email pull delay out from email address
27 --list list emails in queue
28 --queue directory to use as a queue
29 --process sends out a specific message that was enqueued
30 --debug, -d debugging level (Default 0)
31 --help, -h display this help
32 --man, -m display manual
40 Enqueue a message which should be sent out later
44 Length of delay (man at for details of specification)
48 The delay option is actually an email address; apply the following
51 $delay =~ m/[+-]d(?:ela?y?)?[-+]([^\@]+)/;
56 Thus, foo-delay-now+5_min@bar.baz becomes now+5 min and
57 foo-del+00=30=00@bar.baz becomes 00:30:00
61 List entries which are in the queue
65 Delete an entry from the queue
69 Who to mail the delayed mail to
73 The queue directory to use; defaults to ~/.delay_mail_queue
77 Process a specific entry in the queue (this is called by at at the
78 appropriate time; you shouldn't need call it manually.)
82 Debug verbosity. (Default 0)
86 Display brief useage information.
102 my %options = (debug => 0,
108 GetOptions(\%options,'debug|d+','help|h|?','man|m',
109 'list|l','dequeue=s','process|p=s','enqueue|e',
110 'delay|D=s','email|E',
111 'mailto|mail-to|M=s',
114 pod2usage() if $options{help};
115 pod2usage({verbose=>2}) if $options{man};
117 $DEBUG = $options{debug};
119 use List::Util qw(sum);
120 use MIME::WordDecoder;
125 if (1 < grep {exists $options{$_}} qw(enqueue list process dequeue)) {
126 $ERROR .= "Exactly one of --enque, --list, --process, or --dequeue must be specified\n";
128 if (not $options{enqueue} and ($options{email} or exists $options{delay})) {
129 $ERROR .= "Setting email or delay is nonsensical unless enqueuing\n";
132 pod2usage($ERROR) if length $ERROR;
134 # create queue directory if it doesn't already exist
135 if (not exists $options{queue}) {
136 $options{queue} = "$ENV{HOME}/.delay_mail_queue";
138 if (not -d $options{queue}) {
139 mkdir($options{queue}) or
140 die "Unable to create queue directory $options{queue}: $!";
143 if (not exists $options{mailto}) {
144 if (exists $ENV{EMAIL}) {
145 $options{mailto} = $ENV{EMAIL};
147 elsif (exists $ENV{USER}) {
148 $options{mailto} = $ENV{USER};
151 $options{mailto} = qx(id -nu);
154 $options{mailto} =~ s/\n//g;
156 if (exists $options{enqueue}) {
158 my $delay = $options{delay};
160 if ($options{email}) {
161 $delay =~ m/[+-]d(?:ela?y?)?[-+]([^\@]+)/;
169 # rip subject out of email
170 # we cheat for now; this isn't correct at all.
171 my ($subject) = $email =~ /^Subject:\s*(.+)/mi;
172 $subject = decode_rfc1522($subject);
175 # create a queue entry
176 my $queue_fn = $time . $$;
177 my $q_fh = IO::File->new("$options{queue}/$queue_fn",'w') or
178 die "Unable to open $options{queue}/$queue_fn for writing";
179 print {$q_fh} "delay: $delay\n";
180 print {$q_fh} "time: $time\n";
181 print {$q_fh} "mailto: $options{mailto}\n";
182 print {$q_fh} "entry: $queue_fn\n";
183 print {$q_fh} "subject: $subject\n";
184 print {$q_fh} "#####\n";
185 print {$q_fh} $email;
187 my $pid = open($at_fh,'|-','at',$delay) or exit 1;
188 print {$at_fh} "$0 '--queue' '$options{queue}' '--process' '$queue_fn';\n";
189 close $at_fh or exit $?;
192 elsif ($options{list}) {
193 my $dir = IO::Dir->new($options{queue}) or
194 die "Unable to list contents of $options{queue}: $!";
197 while (defined($entry = $dir->read)) {
198 #valid queue entries are entirely numeric
199 print STDERR "Dealing with $entry\n" if $DEBUG;
200 last if not defined $entry;
201 next if $entry !~ /^\d+$/;
202 # they're also just readable files
203 next if not -f "$options{queue}/$entry" or not -r "$options{queue}/$entry";
204 print STDERR "Still dealing with $entry\n" if $DEBUG;
205 push @queue,parse_queue_entry($entry);
207 for my $q_e (@queue) {
209 print "$q_e->{entry}: send $q_e->{subject} to $q_e->{mailto} at $q_e->{delay} ($q_e->{time})\n";
212 elsif ($options{dequeue}) {
213 if (-e "$options{queue}/$options{dequeue}") {
214 unlink("$options{queue}/$options{dequeue}") or
215 die "Unable to unlink $options{queue}/$options{dequeue}";
218 print STDERR "$options{queue}/$options{dequeue} doesn't exist\n"
221 elsif ($options{process}) {
222 if (-e "$options{queue}/$options{process}") {
223 my $q_e = parse_queue_entry($options{process});
224 if (not defined $q_e) {
225 die "Unable to parse $options{process}";
227 # munge the message id
228 my ($message_id) = $q_e->{email} =~ m/^Message-Id:\s*(.+)/;
229 if (not $message_id =~ s/\@/delay$q_e->{entry}@/){
230 $message_id =~ s/(\w)/delay$q_e->{entry}$1/;
232 $q_e->{email} =~ s/^(Message-Id:\s*)(.+)/$1$message_id/;
235 open($sendmail_fh,'|-','/usr/sbin/sendmail',$q_e->{mailto}) or
236 die "Unable to open sendmail to send message";
237 print {$sendmail_fh} $q_e->{email};
241 print STDERR "Sendmail failed with $?\n";
244 unlink("$options{queue}/$options{process}");
247 print STDERR "$options{queue}/$options{process} doesn't exist\n"
252 sub parse_queue_entry{
255 my $entry_fh = IO::File->new("$options{queue}/$entry",'r') or
257 my $queue_entry = {};
258 while (<$entry_fh>) {
262 my ($key,$value) = split /: /, $line,2;
263 $queue_entry->{$key} = $value;
266 $queue_entry->{email} = <$entry_fh>;
271 # These functions below I've jacked from Debbugs::MIME which I also
272 # wrote; probably should put them somewhere else eventually.
274 sub convert_to_utf8 {
275 my ($data, $charset) = @_;
276 # raw data just gets returned (that's the charset WordDecorder
277 # uses when it doesn't know what to do)
278 return $data if $charset eq 'raw' or is_utf8($data,1);
281 # this encode/decode madness is to make sure that the data
282 # really is valid utf8 and that the is_utf8 flag is off.
283 $result = encode("utf8",decode($charset,$data))
286 warn "Unable to decode charset; '$charset' and '$data': $@";
294 # Set up the default RFC1522 decoder, which turns all charsets that
295 # are supported into the appropriate UTF-8 charset.
296 MIME::WordDecoder->default(new MIME::WordDecoder(
297 ['*' => \&convert_to_utf8,
305 # this is craptacular, but leading space is hacked off by unmime.
307 my $leading_space = '';
308 $leading_space = $1 if $string =~ s/^(\s+)//;
309 # unmime calls the default MIME::WordDecoder handler set up at
310 # initialization time.
311 return $leading_space . MIME::WordDecoder::unmime($string);