package Debbugs::MIME;
+=head1 NAME
+
+Debbugs::MIME -- Mime handling routines for debbugs
+
+=head1 SYNOPSIS
+
+ use Debbugs::MIME qw(parse decode_rfc1522);
+
+=head1 DESCRIPTION
+
+
+=head1 BUGS
+
+None known.
+
+=cut
+
+use warnings;
use strict;
use base qw(Exporter);
}
use File::Path;
+use File::Temp qw();
use MIME::Parser;
+use POSIX qw(strftime);
+use List::MoreUtils qw(apply);
+
# for decode_rfc1522
use MIME::WordDecoder qw();
use Encode qw(decode encode encode_utf8 decode_utf8 is_utf8);
my (@headerlines, @bodylines);
my $parser = MIME::Parser->new();
- mkdir "mime.tmp.$$", 0777;
- $parser->output_under("mime.tmp.$$");
+ my $tempdir = File::Temp::tempdir();
+ $parser->output_under($tempdir);
my $entity = eval { $parser->parse_data($_[0]) };
if ($entity and $entity->head->tags) {
@bodylines = @msg[$i .. $#msg];
}
- rmtree "mime.tmp.$$", 0, 1;
+ rmtree $tempdir, 0, 1;
# Remove blank lines.
shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
=head2 create_mime_message
- create_mime_message([To=>'don@debian.org'],$body,[$attach1, $attach2]);
+ create_mime_message([To=>'don@debian.org'],$body,[$attach1, $attach2],$include_date);
Creates a MIME encoded message with headers given by the first
argument, and a message given by the second.
Optional attachments can be specified in the third arrayref argument.
+Whether to include the date in the header is the final argument; it
+defaults to true, setting the Date header if one is not already
+present.
+
Headers are passed directly to MIME::Entity::build, the message is the
first attachment.
=cut
sub create_mime_message{
- my ($headers,$body,$attachments) = @_;
+ my ($headers,$body,$attachments,$include_date) = @_;
$attachments = [] if not defined $attachments;
+ $include_date = 1 if not defined $include_date;
die "The first argument to create_mime_message must be an arrayref" unless ref($headers) eq 'ARRAY';
die "The third argument to create_mime_message must be an arrayref" unless ref($attachments) eq 'ARRAY';
+ if ($include_date) {
+ my %headers = apply {lc($_)} @{$headers};
+ if (not exists $headers{date}) {
+ push @{$headers},
+ ('Date',
+ strftime("%a, %d %b %Y %H:%M:%S +0000",gmtime)
+ );
+ }
+ }
+
# Build the message
# MIME::Entity is stupid, and doesn't rfc1522 encode its headers, so we do it for it.
my $msg = MIME::Entity->build('Content-Type' => 'text/plain; charset=utf-8',