]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NetNNTPSocks.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / NetNNTPSocks.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # wrap Net::NNTP client with SOCKS support
5 package PublicInbox::NetNNTPSocks;
6 use strict;
7 use v5.10.1;
8 use Net::NNTP;
9 our %OPT;
10 our @ISA = qw(IO::Socket::Socks);
11 my @SOCKS_KEYS = qw(ProxyAddr ProxyPort SocksVersion SocksDebug SocksResolve);
12
13 # use this instead of Net::NNTP->new if using Proxy*
14 sub new_socks {
15         my (undef, %opt) = @_;
16         require IO::Socket::Socks;
17         local @Net::NNTP::ISA = (qw(Net::Cmd), __PACKAGE__);
18         local %OPT = map {;
19                 defined($opt{$_}) ? ($_ => $opt{$_}) : ()
20         } @SOCKS_KEYS;
21         Net::NNTP->new(%opt); # this calls our new() below:
22 }
23
24 # called by Net::NNTP->new
25 sub new {
26         my ($self, %opt) = @_;
27         @OPT{qw(ConnectAddr ConnectPort)} = @opt{qw(PeerAddr PeerPort)};
28         my $ret = $self->SUPER::new(%OPT) or
29                 die 'SOCKS error: '.eval('$IO::Socket::Socks::SOCKS_ERROR');
30         $ret;
31 }
32
33 1;