Mariachi::Message - representation of a mail message


Mariachi documentation Contained in the Mariachi distribution.

Index


Code Index:

NAME

Top

Mariachi::Message - representation of a mail message

METHODS

Top

->new($message)

$message is a rfc2822 compliant message body

your standard constructor

->body

->header

->header_set

body, header, and header_set are provided for interface compatibility with Email::Simple

->first_lines

->first_paragraph

->first_sentence

See Text::Original

->body_sigless

Returns the body with the signature (defined as anything after "\n-- \n") removed.

->sig

Returns the stripped sig.

->from

A privacy repecting version of the From: header.

->subject

->date

the Subject and Date headers

->filename

the name of the output file

->epoch_date

The date header pared into epoch seconds

->ymd

->day

->month

->year

epoch_date formatted in useful ways

->linked

hashref of indexes that link to us. key is the type of index, value is the filename

->next

the next message in the archive, thread-wise

->prev

the previous message in the archive, thread-wise

->root

the root of the thread you live in


Mariachi documentation Contained in the Mariachi distribution.
use strict;
package Mariachi::Message;
use Email::Simple;
use Digest::MD5 qw(md5_hex);
use Date::Parse qw(str2time);
use Text::Original ();
use Memoize;

use base qw(Class::Accessor::Fast);
__PACKAGE__->mk_accessors(qw( body _header next prev root
                              epoch_date day month year ymd linked
                            ));

sub new {
    my $class = shift;
    my $source = shift;

    my $self = $class->SUPER::new;
    my $mail = Email::Simple->new($source) or return;

    $self->linked({});
    $self->_header({});
    $self->header_set( $_, $mail->header($_) ) for
      qw( message-id from subject date references in-reply-to );
    $self->body( $mail->body );

    $self->header_set('message-id', $self->_make_fake_id)
      unless $self->header('message-id');

    # this is a bit ugly to be here but much quicker than making it a
    # memoized lookup
    my @date = localtime $self->epoch_date(str2time( $self->header('date') )
                                             || 0);
    my @ymd = ( $date[5] + 1900, $date[4] + 1, $date[3] );
    $self->ymd(\@ymd);
    $self->day(   sprintf "%04d/%02d/%02d", @ymd );
    $self->month( sprintf "%04d/%02d", @ymd );
    $self->year(  sprintf "%04d", @ymd );

    return $self;
}


sub _make_fake_id {
    my $self = shift;
    my $hash = substr( md5_hex( $self->header('from').$self->date ), 0, 8 );
    return "$hash\@made_up";
}

sub header {
    my $self = shift;
    $self->_header->{ lc shift() };
}

sub header_set {
    my $self = shift;
    my $hdr = shift;
    $self->_header->{ lc $hdr } = shift;
}

*first_line = \&first_lines;
sub first_lines {
    my $self = shift;
    return Text::Original::first_lines( $self->body, @_ );
}

sub first_paragraph {
    my $self = shift;
    return Text::Original::first_paragraph( $self->body );
}

sub first_sentence {
    my $self = shift;
    return Text::Original::first_sentence( $self->body );
}

sub body_sigless {
    my $self = shift;
    my ($body, undef) = split /^-- $/m, $self->body, 2;

    return $body;
}

sub sig {
    my $self = shift;
    my (undef, $sig) = split /^-- $/m, $self->body, 2;
    $sig =~ s/^\n// if $sig;
    return $sig;
}



sub from {
    my $self = shift;

    my $from = $self->header('from');
    $from =~ s/<.*>//;
    $from =~ s/\@\S+//;
    $from =~ s/\s+\z//;
    $from =~ s/"(.*?)"/$1/;
    return $from;
}
memoize('from');

sub subject { $_[0]->header('subject') }
sub date    { $_[0]->header('date') }


sub filename {
    my $self = shift;

    my $msgid = $self->header('message-id');

    my $filename = substr( md5_hex( $msgid ), 0, 8 ).".html";
    return $self->day."/".$filename;
}
memoize('filename');

1;

__END__