1 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # IMAP delivery module, used by Ssoma::Extractor if Email::LocalDelivery
5 # is not available. Since we are dependent on git, we use the same config
6 # settings as those used by git-imap-send(1)
11 use Net::IMAP::Simple;
14 my ($class, $git) = @_;
15 my $file = "$git->{git_dir}/config";
16 my $cfg = $git->config_list($file);
18 my $self = bless { opts => \%opts }, $class;
19 foreach my $k (qw/folder host user pass port tunnel/) {
20 $self->{$k} = $cfg->{"imap.$k"};
23 check_unsupported($git, $cfg);
26 if ((my $host = $self->{host})) {
28 $host =~ s!imaps://!! and $opts{use_ssl} = 1;
29 my $port = $self->{port};
30 $host .= ":$port" if defined $port;
31 $self->get_pass($host);
32 $imap = Net::IMAP::Simple->new($host, %opts) or conn_fail();
33 $imap->login($self->{user}, $self->{pass}) or
34 die "Login failed: " . $imap->errstr . "\n";
35 } elsif ((my $tunnel = $self->{tunnel})) {
37 $host = "cmd:$tunnel";
38 $imap = Net::IMAP::Simple->new($host, %opts) or conn_fail();
40 die "neither imap.host nor imap.tunnel set in $file\n";
42 $self->{imap} = $imap;
47 my ($self, $msg) = @_;
48 $self->{imap}->put($self->{folder}, $msg);
51 sub check_unsupported {
54 if ((my $sslverify = $cfg->{"imap.sslverify"})) {
55 local $ENV{GIT_CONFIG} = "$git->{git_dir}/config";
56 $sslverify = `git config --bool imap.sslverify`;
58 if ($sslverify eq "false") {
59 die "imap.sslverify=false not supported\n";
63 if (defined $cfg->{"imap.authmethod"}) {
64 die "imap.authMethod not supported by Net::IMAP::Simple\n";
69 my ($self, $host) = @_;
71 return if defined $self->{pass};
74 print STDERR "$self->{user}\@$host password:";
78 require Term::ReadKey;
79 Term::ReadKey::ReadMode('noecho');
82 my $cmd = 'stty -echo';
83 print STDERR "Term::ReadKey not available, using `$cmd'\n";
84 system($cmd) and die "$cmd failed: $?\n";
87 system($cmd) and die "$cmd failed: $?\n";
91 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
92 last if $key =~ /[\012\015]/; # [\r\n]
95 Term::ReadKey::ReadMode('restore');
100 $self->{pass} = $pass;
104 die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";