aboutsummaryrefslogtreecommitdiff
path: root/lib/Sietima.pm
blob: 85468bb3483ed6cf9772880ae7301804b4d977fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package Sietima; 
use 5.020;
use Moo;
use Types::Standard qw(ArrayRef Object FileHandle Maybe);
use Type::Params qw(compile);
use Sietima::Types qw(Address EmailMIME Message Subscriber Transport);
use Sietima::Message;
use Email::Sender::Simple qw(sendmail);
use namespace::clean;
 
has return_path => (
    isa => Address,
    is => 'ro',
    required => 1,
);
 
has subscribers => (
    isa => ArrayRef[Subscriber],
    is => 'lazy',
);
sub _build_subscribers { +[] }
 
has transport => (
    isa => Maybe[Transport],
    is => 'lazy',
);
sub _build_transport { }
 
sub handle_message {
    state $check = compile(Obect,EmailMIME);
    my ($self,$incoming_message) = $check->(@_);
 
    my (@outgoing_messages) = $self->munge_message($incoming_message);
    for my $outgoing_message (@outgoing_messages) {
        $self->send_message($outgoing_message);
    }
    return;
}
 
sub munge_message {
    state $check = compile(Obect,EmailMIME);
    my ($self,$incoming_message) = $check->(@_);
 
    return Sietima::Message->new({
        mail => $incoming_message,
        from => $self->return_path,
        to => [
            map { $_->address } @{$self->subscribers}
        ],
    });
}
 
sub send_message {
    state $check = compile(Obect,Message);
    my ($self,$outgoing_message) = $check->(@_);
 
    sendmail(
        $outgoing_message->mail,
        {
            %{$outgoing_message->envelope},
            transport => $self->transport,
        },
    );
 
    return;
}
 
1;