summaryrefslogtreecommitdiff
path: root/lib/Config/ClawsMail/Account.pm
blob: 248725059e0e8aad29e09d4750f9528f6ec807d4 (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
package Config::ClawsMail::Account; 
use v5.26;
use Moo;
# VERSION 
use Types::Standard qw(Str InstanceOf);
use Config::ClawsMail::Server;
use namespace::clean;
 
# ABSTRACT: Claws-Mail account 
 
has [qw(account_name name address organization)] => (
    is => 'ro',
    required => 1,
    isa => Str,
);
 
has [qw(imap smtp)] => (
    is => 'ro',
    isa => InstanceOf['Config::ClawsMail::Server'],
);
 
my @ssl_string=qw(no ssl starttls);
# these are from src/imap.h 
my %imap_auth_method = (
    => 'plaintext',
    => 'CRAM_MD5',
    => 'ANONYMOUS',
    => 'GSSAPI',
    16 => 'DIGEST_MD5',
    32 => 'SCRAM_SHA1',
    64 => 'PLAIN',
    128 => 'LOGIN',
    256 => 'XOAUTH2',
    512 => 'SCRAM_SHA224',
    1024 => 'SCRAM_SHA256',
    2048 => 'SCRAM_SHA384',
    4096 => 'SCRAM_SHA512',
);
# these are from src/common/smtp.h 
my %smtp_auth_method = (
    => 'LOGIN',
    => 'CRAM_MD5',
    => 'DIGEST_MD5',
    => 'tls_available'# ?? what does this mean? 
    16 => 'PLAIN',
    32 => 'XOAUTH2',
);
 
sub _expand_bitfield {
    my ($bits$map) = @_;
 
    my @result;
    for my $key (sort { $a <=> $b } keys %$map) {
        if ($bits & $key) {
            push @result$map->{$key};
        }
    }
 
    return \@result;
}
 
sub new_from_config {
    my ($class,$args) = @_;
 
    my $config = $args->{account_config};
    return unless $config->{protocoleq '1';
 
    my $section = $args->{account_section};
    (my $password_section = $section)=~ s{Account: }{account:};
 
    my $password_store = $args->{password_store};
 
    my $imap_server = Config::ClawsMail::Server->new({
        host => $config->{receive_server},
        port => (
            $config->{set_imapport}
                $config->{imap_port}
                $config->{ssl_imap} == 1
                scalar getservbyname('imaps','tcp')
                scalar getservbyname('imap','tcp')
            ),
        ssl => $ssl_string[$config->{ssl_imap}],
        %{$config}{qw(user_id)},
        password => $password_store->password_for($password_section,'recv'),
        auth_methods => _expand_bitfield($config->{imap_auth_method},\%imap_auth_method),
    });
 
    my $smtp_server = Config::ClawsMail::Server->new({
        host => $config->{smtp_server}||$config->{receive_server},
        port => (
            $config->{set_smtpport}
                $config->{smtp_port}
                $config->{ssl_smtp} == 1
                scalar getservbyname('smtps','tcp')
                scalar getservbyname('smtp','tcp')
            ),
        ssl => $ssl_string[$config->{ssl_smtp}],
        $config->{use_smtp_auth} ? (
            user_id => $config->{smtp_user_id} || $config->{user_id},
            password => $password_store->password_for($password_section,'send') || $password_store->password_for($password_section,'recv'),
            auth_methods => _expand_bitfield($config->{smtp_auth_method},\%smtp_auth_method),
        ) : () ),
    });
 
    return $class->new({
        %{$config}{qw(account_name name address organization)},
        imap => $imap_server,
        smtp => $smtp_server,
    });
}
 
sub email_transport {
    my ($self) = @_;
 
    # once https://github.com/rjbs/Email-Sender/pull/77 gets merged, 
    # we can use `$smtp->auth_methods` to build a `Authen::SASL` and 
    # pass it as `sasl_authenticator`, to restrict which mechanism(s) 
    # should be used; at the same time, we should remove the `sub 
    # _order` from `Authen::SASL::Perl::XOAUTH2` 
    require Email::Sender::Transport::SMTP;
    my $smtp = $self->smtp;
    return Email::Sender::Transport::SMTP->new(
        host => $smtp->host,
        port => $smtp->port,
        ssl => $smtp->ssl,
        $smtp->user_id ? (
            sasl_username => $smtp->user_id,
            sasl_password => $smtp->password,
        ) : () )
    );
}
 
1;