use PublicInbox::Eml;
use POSIX qw(strftime);
use PublicInbox::DS qw(now);
-use Digest::SHA qw(sha1_hex);
+use PublicInbox::SHA qw(sha1_hex);
use Time::Local qw(timegm timelocal);
use PublicInbox::GitAsyncCat;
use PublicInbox::Address;
my $res = eval { $req->($self, @args) };
my $err = $@;
if ($err && $self->{sock}) {
- local $/ = "\n";
- chomp($l);
- err($self, 'error from: %s (%s)', $l, $err);
+ $l =~ s/\r?\n//s;
+ warn("error from: $l ($err)\n");
$res = \"503 program fault - command not performed\r\n";
}
defined($res) ? $self->write($res) : 0;
my ($self, undef) = @_;
my $res = $CAPABILITIES;
if (!$self->{sock}->can('accept_SSL') &&
- $self->{nntpd}->{accept_tls}) {
+ $self->{nntpd}->{ssl_ctx_opt}) {
$res .= "STARTTLS\r\n";
}
$res .= ".\r\n";
sub list_headers ($;$) { $LIST_HEADERS }
-sub list_active_i { # "LIST ACTIVE" and also just "LIST" (no args)
- my ($self, $groupnames) = @_;
- my @window = splice(@$groupnames, 0, 100) or return 0;
- my $ibx;
+sub names2ibx ($;$) {
+ my ($self, $names) = @_;
my $groups = $self->{nntpd}->{pi_cfg}->{-by_newsgroup};
- for my $ngname (@window) {
- $ibx = $groups->{$ngname} and group_line($self, $ibx);
+ if ($names) { # modify arrayref in-place
+ $_ = $groups->{$_} for @$names;
+ $names; # now an arrayref of ibx
+ } else {
+ my @ret = map { $groups->{$_} } @{$self->{nntpd}->{groupnames}};
+ \@ret;
}
- scalar(@$groupnames); # continue if there's more
+}
+
+sub list_active_i { # "LIST ACTIVE" and also just "LIST" (no args)
+ my ($self, $ibxs) = @_;
+ my @window = splice(@$ibxs, 0, 1000);
+ emit_group_lines($self, \@window);
+ scalar @$ibxs; # continue if there's more
}
sub list_active ($;$) { # called by cmd_list
my ($self, $wildmat) = @_;
wildmat2re($wildmat);
- $self->long_response(\&list_active_i, [
- grep(/$wildmat/, @{$self->{nntpd}->{groupnames}}) ]);
+ my @names = grep(/$wildmat/, @{$self->{nntpd}->{groupnames}});
+ $self->long_response(\&list_active_i, names2ibx($self, \@names));
}
sub list_active_times_i {
- my ($self, $groupnames) = @_;
- my @window = splice(@$groupnames, 0, 100) or return 0;
- my $groups = $self->{nntpd}->{pi_cfg}->{-by_newsgroup};
- for my $ngname (@window) {
- my $ibx = $groups->{$ngname} or next;
- my $c = eval { $ibx->uidvalidity } // time;
- $self->msg_more("$ngname $c <$ibx->{-primary_address}>\r\n");
- }
- scalar(@$groupnames); # continue if there's more
+ my ($self, $ibxs) = @_;
+ my @window = splice(@$ibxs, 0, 1000);
+ $self->msg_more(join('', map {
+ my $c = eval { $_->uidvalidity } // time;
+ "$_->{newsgroup} $c <$_->{-primary_address}>\r\n";
+ } @window));
+ scalar @$ibxs; # continue if there's more
}
sub list_active_times ($;$) { # called by cmd_list
my ($self, $wildmat) = @_;
wildmat2re($wildmat);
- $self->long_response(\&list_active_times_i, [
- grep(/$wildmat/, @{$self->{nntpd}->{groupnames}}) ]);
+ my @names = grep(/$wildmat/, @{$self->{nntpd}->{groupnames}});
+ $self->long_response(\&list_active_times_i, names2ibx($self, \@names));
}
sub list_newsgroups_i {
- my ($self, $groupnames) = @_;
- my @window = splice(@$groupnames, 0, 100) or return 0;
- my $groups = $self->{nntpd}->{pi_cfg}->{-by_newsgroup};
- my $ibx;
- for my $ngname (@window) {
- $ibx = $groups->{$ngname} and
- $self->msg_more("$ngname ".$ibx->description."\r\n");
- }
- scalar(@$groupnames); # continue if there's more
+ my ($self, $ibxs) = @_;
+ my @window = splice(@$ibxs, 0, 1000);
+ $self->msg_more(join('', map {
+ "$_->{newsgroup} ".$_->description."\r\n"
+ } @window));
+ scalar @$ibxs; # continue if there's more
}
sub list_newsgroups ($;$) { # called by cmd_list
my ($self, $wildmat) = @_;
wildmat2re($wildmat);
- $self->long_response(\&list_newsgroups_i, [
- grep(/$wildmat/, @{$self->{nntpd}->{groupnames}}) ]);
+ my @names = grep(/$wildmat/, @{$self->{nntpd}->{groupnames}});
+ $self->long_response(\&list_newsgroups_i, names2ibx($self, \@names));
}
# LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
$arg->($self, @args);
} else {
$self->msg_more("215 list of newsgroups follows\r\n");
- $self->long_response(\&list_active_i, [ # copy array
- @{$self->{nntpd}->{groupnames}} ]);
+ $self->long_response(\&list_active_i, names2ibx($self));
}
}
my ($self, $beg, $end) = @_;
my $r = $self->{ibx}->mm(1)->msg_range($beg, $end, 'num');
scalar(@$r) or return;
- $self->msg_more(join("\r\n", @$r, ''));
+ $self->msg_more(join('', map { "$_->[0]\r\n" } @$r));
1;
}
}
}
-sub group_line ($$) {
- my ($self, $ibx) = @_;
- my ($min, $max) = $ibx->mm(1)->minmax;
- $self->msg_more("$ibx->{newsgroup} $max $min n\r\n");
+sub emit_group_lines {
+ my ($self, $ibxs) = @_;
+ my ($min, $max);
+ my $ALL = $self->{nntpd}->{pi_cfg}->ALL;
+ my $misc = $ALL->misc if $ALL;
+ my $buf = '';
+ for my $ibx (@$ibxs) {
+ $misc ? $misc->inbox_data($ibx) :
+ delete(@$ibx{qw(-art_min -art_max)});
+ ($min, $max) = ($ibx->art_min, $ibx->art_max);
+ $buf .= "$ibx->{newsgroup} $max $min n\r\n";
+ }
+ $self->msg_more($buf);
}
sub newgroups_i {
- my ($self, $ts, $i, $groupnames) = @_;
- my $end = $$i + 100;
- my $groups = $self->{nntpd}->{pi_cfg}->{-by_newsgroup};
- while ($$i < $end) {
- my $ngname = $groupnames->[$$i++] // return;
- my $ibx = $groups->{$ngname} or next; # expired on reload
- next unless (eval { $ibx->uidvalidity } // 0) > $ts;
- group_line($self, $ibx);
- }
- 1;
+ my ($self, $ts, $ibxs) = @_;
+ my @window = splice(@$ibxs, 0, 1000);
+ @window = grep { (eval { $_->uidvalidity } // 0) > $ts } @window;
+ emit_group_lines($self, \@window);
+ scalar @$ibxs; # any more?
}
sub cmd_newgroups ($$$;$$) {
# TODO dists
$self->msg_more("231 list of new newsgroups follows\r\n");
- $self->long_response(\&newgroups_i, $ts, \(my $i = 0),
- $self->{nntpd}->{groupnames});
+ $self->long_response(\&newgroups_i, $ts, names2ibx($self));
}
sub wildmat2re (;$) {
}
sub newnews_i {
- my ($self, $names, $ts, $prev) = @_;
- my $ngname = $names->[0];
- if (my $ibx = $self->{nntpd}->{pi_cfg}->{-by_newsgroup}->{$ngname}) {
- if (my $over = $ibx->over) {
- my $msgs = $over->query_ts($ts, $$prev);
- if (scalar @$msgs) {
- $self->msg_more(join('', map {
- "<$_->{mid}>\r\n";
- } @$msgs));
- $$prev = $msgs->[-1]->{num};
- return 1; # continue on current group
- }
+ my ($self, $ibxs, $ts, $prev) = @_;
+ if (my $over = $ibxs->[0]->over) {
+ my $msgs = $over->query_ts($ts, $$prev);
+ if (scalar @$msgs) {
+ $self->msg_more(join('', map {
+ "<$_->{mid}>\r\n";
+ } @$msgs));
+ $$prev = $msgs->[-1]->{num};
+ return 1; # continue on current group
}
}
- shift @$names;
- if (@$names) { # continue onto next newsgroup
+ shift @$ibxs;
+ if (@$ibxs) { # continue onto next newsgroup
$$prev = 0;
1;
} else { # all done, break out of the long_response
my ($keep, $skip) = split(/!/, $newsgroups, 2);
ngpat2re($keep);
ngpat2re($skip);
- my @names = grep(!/$skip/, grep(/$keep/,
- @{$self->{nntpd}->{groupnames}}));
- return ".\r\n" unless scalar(@names);
+ my @names = grep(/$keep/, @{$self->{nntpd}->{groupnames}});
+ @names = grep(!/$skip/, @names);
+ return \".\r\n" unless scalar(@names);
my $prev = 0;
- $self->long_response(\&newnews_i, \@names, $ts, \$prev);
+ $self->long_response(\&newnews_i, names2ibx($self, \@names),
+ $ts, \$prev);
}
sub cmd_group ($$) {
# these can momentarily double the memory consumption :<
$$msg =~ s/^\./../smg;
$$msg =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
- $$msg .= "\r\n" unless $$msg =~ /\r\n\z/s;
+ $$msg .= "\r\n" unless substr($$msg, -2, 2) eq "\r\n";
$self->msg_more($$msg);
}
$self->close;
die "BUG: bad code: $r";
}
- $self->write(\".\r\n"); # flushes (includes ->zflush)
+ $self->write(\".\r\n"); # flushes (includes ->dflush)
$self->requeue;
}
$self->long_response(\&xover_i, @$r);
}
-sub compressed { undef }
-
sub cmd_starttls ($) {
my ($self) = @_;
- my $sock = $self->{sock} or return;
# RFC 4642 2.2.1
- return r502 if ($sock->can('accept_SSL') || $self->compressed);
- my $opt = $self->{nntpd}->{accept_tls} or
+ (($self->{sock} // return)->can('stop_SSL') || $self->compressed) and
+ return r502;
+ $self->{nntpd}->{ssl_ctx_opt} or
return \"580 can not initiate TLS negotiation\r\n";
$self->write(\"382 Continue with TLS negotiation\r\n");
- $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
+ PublicInbox::TLS::start($self->{sock}, $self->{nntpd});
$self->requeue if PublicInbox::DS::accept_tls_step($self);
undef;
}
my ($self, $alg) = @_;
return "503 Only DEFLATE is supported\r\n" if uc($alg) ne 'DEFLATE';
return r502 if $self->compressed;
- PublicInbox::NNTPdeflate->enable($self);
+ PublicInbox::NNTPdeflate->enable($self) or return
+ \"403 Unable to activate compression\r\n";
+ PublicInbox::DS::write($self, \"206 Compression active\r\n");
$self->requeue;
undef
}
'223 '.join(' ', sort(@paths))."\r\n";
}
-sub err ($$;@) {
- my ($self, $fmt, @args) = @_;
- printf { $self->{nntpd}->{err} } $fmt."\n", @args;
-}
-
sub out ($$;@) {
my ($self, $fmt, @args) = @_;
printf { $self->{nntpd}->{out} } $fmt."\n", @args;
# callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
sub event_step {
my ($self) = @_;
-
+ local $SIG{__WARN__} = $self->{nntpd}->{warn_cb};
return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
# only read more requests if we've drained the write buffer,
defined($self->{rbuf}) || defined($self->{wbuf})
}
+package PublicInbox::NNTPdeflate;
+use PublicInbox::DSdeflate;
+our @ISA = qw(PublicInbox::DSdeflate PublicInbox::NNTP);
+
1;