use v5.10.1;
use parent qw(Exporter PublicInbox::IPC);
use PublicInbox::Eml;
+use PublicInbox::Config;
our %IMAPflags2kw = map {; "\\\u$_" => $_ } qw(seen answered flagged draft);
$IMAPflags2kw{'$Forwarded'} = 'forwarded'; # RFC 5550
$uri->scheme . '://' . $uri->authority;
}
+sub socks_args ($) {
+ my ($val) = @_;
+ return if ($val // '') eq '';
+ if ($val =~ m!\Asocks5h:// (?: \[ ([^\]]+) \] | ([^:/]+) )
+ (?::([0-9]+))?/*\z!ix) {
+ my ($h, $p) = ($1 // $2, $3 + 0);
+ $h = '127.0.0.1' if $h eq '0';
+ eval { require IO::Socket::Socks } or die <<EOM;
+IO::Socket::Socks missing for socks5h://$h:$p
+EOM
+ return { ProxyAddr => $h, ProxyPort => $p };
+ }
+ die "$val not understood (only socks5h:// is supported)\n";
+}
+
+sub mic_new ($$$$) {
+ my ($self, $mic_arg, $sec, $uri) = @_;
+ my %socks;
+ my $sa = $self->{imap_opt}->{$sec}->{-proxy_cfg} || $self->{-proxy_cli};
+ if ($sa) {
+ my %opt = %$sa;
+ $opt{ConnectAddr} = delete $mic_arg->{Server};
+ $opt{ConnectPort} = delete $mic_arg->{Port};
+ $socks{Socket} = IO::Socket::Socks->new(%opt) or die
+ "E: <$$uri> ".eval('$IO::Socket::Socks::SOCKS_ERROR');
+ }
+ PublicInbox::IMAPClient->new(%$mic_arg, %socks);
+}
+
sub auth_anon_cb { '' }; # for Mail::IMAPClient::Authcallback
# mic_for may prompt the user and store auth info, prepares mic_get
-sub mic_for { # mic = Mail::IMAPClient
+sub mic_for ($$$$) { # mic = Mail::IMAPClient
my ($self, $url, $mic_args, $lei) = @_;
require PublicInbox::URIimap;
my $uri = PublicInbox::URIimap->new($url);
username => $uri->user,
password => $uri->password,
}, 'PublicInbox::GitCredential';
- my $common = $mic_args->{uri_section($uri)} // {};
+ my $sec = uri_section($uri);
+ my $common = $mic_args->{$sec} // {};
# IMAPClient and Net::Netrc both mishandles `0', so we pass `127.0.0.1'
my $host = $cred->{host};
$host = '127.0.0.1' if $host eq '0';
%$common, # may set Starttls, Compress, Debug ....
};
require PublicInbox::IMAPClient;
- my $mic = PublicInbox::IMAPClient->new(%$mic_arg) or
- die "E: <$url> new: $@\n";
-
+ my $mic = mic_new($self, $mic_arg, $sec, $uri) or
+ die "E: <$url> new: $@\n";
# default to using STARTTLS if it's available, but allow
# it to be disabled since I usually connect to localhost
if (!$mic_arg->{Ssl} && !defined($mic_arg->{Starttls}) &&
my $err;
if ($mic->login && $mic->IsAuthenticated) {
# success! keep IMAPClient->new arg in case we get disconnected
- $self->{mic_arg}->{uri_section($uri)} = $mic_arg;
+ $self->{mic_arg}->{$sec} = $mic_arg;
} else {
$err = "E: <$url> LOGIN: $@\n";
if ($cred && defined($cred->{password})) {
sub nn_new ($$$) {
my ($nn_arg, $nntp_opt, $uri) = @_;
- my $nn = Net::NNTP->new(%$nn_arg) or die "E: <$uri> new: $!\n";
+ my $nn;
+ if (defined $nn_arg->{ProxyAddr}) {
+ require PublicInbox::NetNNTPSocks;
+ eval { $nn = PublicInbox::NetNNTPSocks->new_socks(%$nn_arg) };
+ die "E: <$uri> $@\n" if $@;
+ } else {
+ $nn = Net::NNTP->new(%$nn_arg) or die "E: <$uri> new: $!\n";
+ }
# default to using STARTTLS if it's available, but allow
# it to be disabled for localhost/VPN users
$nn;
}
-sub nn_for ($$$;$) { # nn = Net::NNTP
+sub nn_for ($$$$) { # nn = Net::NNTP
my ($self, $uri, $nn_args, $lei) = @_;
my $sec = uri_section($uri);
my $nntp_opt = $self->{nntp_opt}->{$sec} //= {};
SSL => $uri->secure, # snews == nntps
%$common, # may Debug ....
};
+ my $sa = $self->{-proxy_cli};
+ %$nn_arg = (%$nn_arg, %$sa) if $sa;
my $nn = nn_new($nn_arg, $nntp_opt, $uri);
if ($cred) {
$cred->fill($lei); # may prompt user here
}
my $to = cfg_intvl($cfg, 'imap.timeout', $$uri);
$mic_args->{$sec}->{Timeout} = $to if $to;
+ my $sa = socks_args($cfg->urlmatch('imap.Proxy', $$uri));
+ $self->{imap_opt}->{$sec}->{-proxy_cfg} = $sa if $sa;
for my $k (qw(pollInterval idleInterval)) {
$to = cfg_intvl($cfg, "imap.$k", $$uri) // next;
$self->{imap_opt}->{$sec}->{$k} = $to;
my $nn_args = {}; # scheme://authority => Net::NNTP->new arg
for my $uri (@{$self->{nntp_order}}) {
my $sec = uri_section($uri);
+ my $args = $nn_args->{$sec} //= {};
# Debug and Timeout are passed to Net::NNTP->new
my $v = cfg_bool($cfg, 'nntp.Debug', $$uri);
- $nn_args->{$sec}->{Debug} = $v if defined $v;
+ $args->{Debug} = $v if defined $v;
my $to = cfg_intvl($cfg, 'nntp.Timeout', $$uri);
- $nn_args->{$sec}->{Timeout} = $to if $to;
+ $args->{Timeout} = $to if $to;
+ my $sa = socks_args($cfg->urlmatch('nntp.Proxy', $$uri));
+ %$args = (%$args, %$sa) if $sa;
# Net::NNTP post-connect commands
for my $k (qw(starttls compress)) {
$self->{nntp_opt}->{$sec}->{$k} = $v;
}
- # internal option
+ # -watch internal option
for my $k (qw(pollInterval)) {
$to = cfg_intvl($cfg, "nntp.$k", $$uri) // next;
$self->{nntp_opt}->{$sec}->{$k} = $to;
}
sub errors {
- my ($self) = @_;
+ my ($self, $lei) = @_;
if (my $u = $self->{unsupported_url}) {
return "Unsupported URL(s): @$u";
}
eval { require Net::NNTP } or
die "Net::NNTP is required for NNTP:\n$@\n";
}
+ my $sa = socks_args($lei ? $lei->{opt}->{proxy} : undef);
+ $self->{-proxy_cli} = $sa if $sa;
undef;
}
return "E: $orig_uri cannot get UIDVALIDITY";
$r_uidnext //= $mic->uidnext($mbx) //
return "E: $orig_uri cannot get UIDNEXT";
+ my $expect = $orig_uri->uidvalidity // $r_uidval;
+ return <<EOF if $expect != $r_uidval;
+E: $orig_uri UIDVALIDITY mismatch (got $r_uidval)
+EOF
+
my $uri = $orig_uri->clone;
my ($itrk, $l_uid, $l_uidval) = _itrk_last($self, $uri, $r_uidval);
return <<EOF if $l_uidval != $r_uidval;
$mic_arg->{Authcallback} = $self->can($cb_name);
}
}
- my $mic = PublicInbox::IMAPClient->new(%$mic_arg);
+ my $mic = mic_new($self, $mic_arg, $sec, $uri);
$cached //= {}; # invalid placeholder if no cache enabled
$mic && $mic->IsConnected ? ($cached->{$sec} = $mic) : undef;
}
} else {
$err = "E: <$uri> not connected: $!";
}
+ die $err if $err && $self->{-can_die};
warn $err if $err;
$mic;
}
warn "# $uri fetching ARTICLE $beg..$end\n";
}
my $n = $self->{max_batch};
+ my $url = $$uri;
for ($beg..$end) {
last if $self->{quit};
$art = $_;
$raw = join('', @$raw);
$raw =~ s/\r\n/\n/sg;
my ($eml_cb, @args) = @{$self->{eml_each}};
- $eml_cb->($uri, $art, $kw, PublicInbox::Eml->new(\$raw), @args);
+ $eml_cb->($url, $art, $kw, PublicInbox::Eml->new(\$raw), @args);
$last_art = $art;
}
run_commit_cb($self);
} else {
$err = "E: <$uri> not connected: $!";
}
+ die $err if $err && $self->{-can_die};
warn $err if $err;
$nn;
}