aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Schema/Mail.pm
blob: c07c6d1fc90dc2e0446f5d67763b1e0073ff69d4 (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
package Fripost::Schema::Mail;

=head1 NAME

Mail.pm - Send clear, signed or encrypted e-mails.

=head1 DESCRIPTION

This module is adds GnuPG signing on top of MIME::Lite. Most of the
code comes from Mail::GnuPG.

=cut

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

use MIME::Entity;
use Mail::GnuPG;
use Encode 'encode';

my $DEBUG = 0;

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    my %msg = @_;

    $msg{To}       //= $ENV{USER}.'@localhost';
    $msg{Encoding} //= 'quoted-printable';
    $msg{Charset}  //= 'utf-8';

    $msg{From}    = Encode::encode( 'MIME-Q', $msg{From})    if $msg{From};
    $msg{To}      = Encode::encode( 'MIME-Q', $msg{To})      if $msg{To};
    $msg{Subject} = Encode::encode( 'MIME-Q', $msg{Subject}) if $msg{Subject};

    my $msg = MIME::Entity::->build( %msg );
    $msg->sign( Signature => $msg{Signature} ) if $msg{Signature};

    $self->{_msg} = $msg;
    return $self;
}

sub sign {
    my $self = shift;
    my $sign_as = shift;

    my $msg = $self->{_msg};
    # TODO: use a config option.
    my $gpg = Mail::GnuPG::->new( use_agent => 1 );
    my $ret = $gpg->mime_sign( $msg );
    map { warn "WARN: $_" } @{$gpg->{last_message}} if $ret;

    return $self;
}

sub send {
    my $self = shift;
    my $msg = $self->{_msg};
    print STDERR $msg->as_string if $DEBUG;
#    $msg->send;
}


=back

=head1 AUTHOR

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

=head1 COPYRIGHT

Copyright 2013 Guilhem Moulin.

=head1 LICENSE

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

=cut

1;

__END__