aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Email.pm
blob: 2e3507031f4378675205fd56e0dca89fd59f570a (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package Fripost::Email;

use 5.010_000;
use strict;
use warnings;
use utf8;

=head1 NAME

Email.pm - Send emails

=cut

our @EXPORT = qw/new_welcome_message
                 new_user_info_message
                 new_alias_info_message
                 subscribe
                 security_status/;
our @ISA = qw(Exporter);

use FindBin qw($Bin);
use lib "$Bin/lib";

use MIME::Entity;
use MIME::QuotedPrint;
use File::Spec qw/devnull/;
use Encode qw/encode/;
use Template;
use Mail::GnuPG;
use GnuPG::Interface;



# Create and send an email.
sub new {
    my ($conf, $h) = @_;

    my $msg = MIME::Entity->build(
        From     => encode('MIME-Q', 'Friposts administratörer')
                    . $conf->{admin_email},
        To       => $h->{To},
        Subject  => $h->{Subject},
        Encoding => 'quoted-printable',
        Charset  => 'utf-8',
        Data     => $h->{Data}
    );
    my $encrypt_to = $conf->{encrypt_to};
    $encrypt_to  //= $h->{To};

    my $encrypt = 0;
    $encrypt = 1 unless $conf->{encrypt} eq 'never';

    if ($h->{Data} ne '' and (defined $conf->{sign} or $encrypt)) {

        # To encrypt, the recipient's key has to be in the public
        # keyring.
        if ($encrypt) {
            my $gnupg = GnuPG::Interface->new();
            my $res;
            {
              # The only way to supress the warning is to desactivate
              # STDERR.
              local *STDERR;
              open *STDERR, '>', File::Spec->devnull()
                or die "Can't open ".File::Spec->devnull().": $!";
              $res = $gnupg->get_public_keys( $encrypt_to );
              close *STDERR;
            }
            unless ($res) {
                die "Error: Public key not found for $encrypt_to.\n"
                    if $conf->{encrypt} eq 'secure';
                warn "WARN: Public key not found for $encrypt_to. The e-mail will be sent clear.\n";
                $encrypt = 0;
            }
        }

        my %gpg;
        $gpg{use_agent} = 1 if defined $conf->{sign};
        $gpg{always_trust} = 1 if $encrypt;
        $gpg{key} = $conf->{sign} if defined $conf->{sign} and
                                     $conf->{sign} ne '';
        my $gpg = new Mail::GnuPG( %gpg );

        my $ret;
        if ($encrypt) {
            if (defined $conf->{sign}) {
                $ret = $gpg->mime_signencrypt( $msg, $encrypt_to );
            }
            else {
                $ret = $gpg->mime_encrypt( $msg, $encrypt_to );
            }
        }
        elsif (defined $conf->{sign}) {
            $ret = $gpg->mime_sign( $msg );
        }

        map { warn "WARN: $_" } @{$gpg->{last_message}}
            if $ret;
    }

    &debug($msg) if $conf->{debug};
    return $msg;
}


sub debug {
    say STDERR "------------------------------------------------------------------------";
    say STDERR decode_qp($_[0]->as_string);
    say STDERR "------------------------------------------------------------------------";
}


# Create a template
sub template_create {
    my ($file, $vars) = @_;

    my $tt = Template->new({
        INCLUDE_PATH => "$Bin/templ",
        INTERPOLATE  => 1,
    }) || die "$Template::ERROR\n";

    my $data;
    $tt->process($file, $vars, \$data)
        || die $tt->error(), '\n';
    return $data;
}


sub new_welcome_message {
    my ($conf, $username) = @_;

    my $data = &template_create( 'new_user_mail.tt', {} );

    return &new ( $conf,
                { To => $username
                , Subject => encode('MIME-Q', 'Välkommen till Fripost!')
                , Data => $data
                } );

}

sub new_user_info_message {
    my ($conf, $username, $password, $to) = @_;

    my $data = &template_create( 'user_info.tt'
                               , { user => $username,
                                   pass => $password } );
    return &new ( $conf,
                { To => $to
                , Subject => encode('MIME-Q', 'Välkommen till Fripost!')
                , Data => $data
                } );
}

sub new_alias_info_message {
    my ($conf, $goto, $addrs) = @_;

    my $data = &template_create( 'new_alias.tt'
                               , { addrs => $addrs } );
    return &new ( $conf,
                { To => $goto
                , Subject => encode('MIME-Q', 'Nya alias till din inkorg'),
                , Data => $data
                } );
}

# Subscribe the user to the given list eg, 'announce@lists.fripost.org'
sub subscribe {
    my ($conf, $user, $list) = @_;

    my ($name, $domain) = split /\@/, $list, 2;
    $list = $name .'-subscribe@' . $domain;
    
    my $msg = MIME::Entity->build(
        From     => $user,
        To       => $list,
        Subject  => '',
        Data     => ''
    );
    &debug($msg) if $conf->{debug};
    $msg->send();
}


# Return the security status of the given MIME entity. Note that this
# check is done *after* the possible encryption, hence it cannot detect
# Encrypted+Signed emails (they are detected as encrypted only).
sub security_status {
    my $msg = $_[0];
    my $gpg = new Mail::GnuPG( );
    if ($gpg->is_encrypted ( $msg )) {
        return 'Encrypted'
    }
    else {
        if ($gpg->is_signed ( $msg )) {
            return 'Signed'
        }
        else {
            return 'Plain'
        }
    }
}


=head1 AUTHOR

Stefan Kangas C<< <skangas at skangas.se> >>

Guilhem Moulin C<< <guilhem at fripost.org> >>

=head1 COPYRIGHT

Copyright 2010,2011 Stefan Kangas.

Copyright 2012 Guilhem Moulin.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as perl itself.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut

1; # End of Email.pm

__END__