-# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
#
# Each instance of this represents a NNTP client socket
+# fields:
+# nntpd: PublicInbox::NNTPD ref
+# article: per-session current article number
+# ng: PublicInbox::Inbox ref
+# long_cb: long_response private data
package PublicInbox::NNTP;
use strict;
-use warnings;
-use base qw(Danga::Socket);
-use fields qw(nntpd article rbuf ng long_res);
-use PublicInbox::Search;
-use PublicInbox::Msgmap;
-use PublicInbox::MID qw(mid_escape);
-use PublicInbox::Git;
-require PublicInbox::EvCleanup;
-use Email::Simple;
+use parent qw(PublicInbox::DS);
+use PublicInbox::MID qw(mid_escape $MID_EXTRACT);
+use PublicInbox::Eml;
use POSIX qw(strftime);
-use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
+use PublicInbox::DS qw(now);
use Digest::SHA qw(sha1_hex);
use Time::Local qw(timegm timelocal);
+use PublicInbox::GitAsyncCat;
+use PublicInbox::Address;
+
use constant {
+ LINE_MAX => 512, # RFC 977 section 2.3
r501 => '501 command syntax error',
+ r502 => '502 Command unavailable',
r221 => '221 Header follows',
r224 => '224 Overview information follows (multi-line)',
r225 => '225 Headers follow (multi-line)',
r430 => '430 No article with that message-id',
- long_response_limit => 0xffffffff,
};
-
-sub now () { clock_gettime(CLOCK_MONOTONIC) };
-
+use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
+use Errno qw(EAGAIN);
+my $ONE_MSGID = qr/\A$MID_EXTRACT\z/;
my @OVERVIEW = qw(Subject From Date Message-ID References);
-my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines)) . ":\r\n";
+my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines), '') .
+ "Xref:full\r\n.";
my $LIST_HEADERS = join("\r\n", @OVERVIEW,
- qw(:bytes :lines Xref To Cc)) . "\r\n";
-
-# disable commands with easy DoS potential:
-# LISTGROUP could get pretty bad, too...
-my %DISABLED; # = map { $_ => 1 } qw(xover list_overview_fmt newnews xhdr);
-
-my $EXPMAP; # fd -> [ idle_time, $self ]
-my $expt;
-our $EXPTIME = 180; # 3 minutes
-my $nextt;
-
-my $nextq = [];
-sub next_tick () {
- $nextt = undef;
- my $q = $nextq;
- $nextq = [];
- foreach my $nntp (@$q) {
- # for request && response protocols, always finish writing
- # before finishing reading:
- if (my $long_cb = $nntp->{long_res}) {
- $nntp->write($long_cb);
- } elsif (&Danga::Socket::POLLIN & $nntp->{event_watch}) {
- event_read($nntp);
- }
- }
-}
+ qw(:bytes :lines Xref To Cc)) . "\r\n.";
+my $CAPABILITIES = <<"";
+101 Capability list:\r
+VERSION 2\r
+READER\r
+NEWNEWS\r
+LIST ACTIVE ACTIVE.TIMES NEWSGROUPS OVERVIEW.FMT\r
+HDR\r
+OVER\r
+COMPRESS DEFLATE\r
+
+sub greet ($) { $_[0]->write($_[0]->{nntpd}->{greet}) };
-sub update_idle_time ($) {
- my ($self) = @_;
- my $fd = $self->{fd};
- defined $fd and $EXPMAP->{$fd} = [ now(), $self ];
-}
-
-sub expire_old () {
- my $now = now();
- my $exp = $EXPTIME;
- my $old = $now - $exp;
- my $nr = 0;
- my %new;
- while (my ($fd, $v) = each %$EXPMAP) {
- my ($idle_time, $nntp) = @$v;
- if ($idle_time < $old) {
- $nntp->close; # idempotent
- } else {
- ++$nr;
- $new{$fd} = $v;
- }
+sub new ($$$) {
+ my ($class, $sock, $nntpd) = @_;
+ my $self = bless { nntpd => $nntpd }, $class;
+ my $ev = EPOLLIN;
+ my $wbuf;
+ if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
+ return CORE::close($sock) if $! != EAGAIN;
+ $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
+ $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
}
- $EXPMAP = \%new;
- if ($nr) {
- $expt = PublicInbox::EvCleanup::later(*expire_old);
+ $self->SUPER::new($sock, $ev | EPOLLONESHOT);
+ if ($wbuf) {
+ $self->{wbuf} = $wbuf;
} else {
- $expt = undef;
- # noop to kick outselves out of the loop ASAP so descriptors
- # really get closed
- PublicInbox::EvCleanup::asap(sub {});
+ greet($self);
}
-}
-
-sub new ($$$) {
- my ($class, $sock, $nntpd) = @_;
- my $self = fields::new($class);
- $self->SUPER::new($sock);
- $self->{nntpd} = $nntpd;
- res($self, '201 server ready - post via email');
- $self->{rbuf} = '';
- $self->watch_read(1);
- update_idle_time($self);
- $expt ||= PublicInbox::EvCleanup::later(*expire_old);
+ $self->update_idle_time;
$self;
}
# returns 1 if we can continue, 0 if not due to buffered writes or disconnect
sub process_line ($$) {
my ($self, $l) = @_;
- my ($req, @args) = split(/\s+/, $l);
- return unless defined($req);
- $req = lc($req);
- $req = eval {
- no strict 'refs';
- $req = $DISABLED{$req} ? undef : *{'cmd_'.$req}{CODE};
- };
+ my ($req, @args) = split(/[ \t]+/, $l);
+ return 1 unless defined($req); # skip blank line
+ $req = $self->can('cmd_'.lc($req));
return res($self, '500 command not recognized') unless $req;
return res($self, r501) unless args_ok($req, scalar @args);
my $res = eval { $req->($self, @args) };
my $err = $@;
- if ($err && !$self->{closed}) {
+ if ($err && $self->{sock}) {
local $/ = "\n";
chomp($l);
err($self, 'error from: %s (%s)', $l, $err);
$res = '503 program fault - command not performed';
}
- return 0 unless defined $res;
- res($self, $res);
+ defined($res) ? res($self, $res) : 0;
+}
+
+# The keyword argument is not used (rfc3977 5.2.2)
+sub cmd_capabilities ($;$) {
+ my ($self, undef) = @_;
+ my $res = $CAPABILITIES;
+ if (!$self->{sock}->can('accept_SSL') &&
+ $self->{nntpd}->{accept_tls}) {
+ $res .= "STARTTLS\r\n";
+ }
+ $res .= '.';
}
sub cmd_mode ($$) {
my ($self, $arg) = @_;
- $arg = uc $arg;
- return r501 unless $arg eq 'READER';
- '201 Posting prohibited';
+ uc($arg) eq 'READER' ? '201 Posting prohibited' : r501;
}
sub cmd_slave ($) { '202 slave status noted' }
my ($self, $wildmat) = @_;
more($self, '282 list of groups and descriptions follows');
list_newsgroups($self, $wildmat);
- '.'
}
-sub list_overview_fmt ($) {
- my ($self) = @_;
- do_more($self, $OVERVIEW_FMT);
-}
+sub list_overview_fmt ($) { $OVERVIEW_FMT }
-sub list_headers ($;$) {
- my ($self) = @_;
- do_more($self, $LIST_HEADERS);
+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;
+ my $groups = $self->{nntpd}->{pi_config}->{-by_newsgroup};
+ for my $ngname (@window) {
+ $ibx = $groups->{$ngname} and group_line($self, $ibx);
+ }
+ scalar(@$groupnames); # continue if there's more
}
-sub list_active ($;$) {
+sub list_active ($;$) { # called by cmd_list
my ($self, $wildmat) = @_;
wildmat2re($wildmat);
- foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
- $ng->{newsgroup} =~ $wildmat or next;
- group_line($self, $ng);
+ long_response($self, \&list_active_i, [
+ grep(/$wildmat/, @{$self->{nntpd}->{groupnames}}) ]);
+}
+
+sub list_active_times_i {
+ my ($self, $groupnames) = @_;
+ my @window = splice(@$groupnames, 0, 100) or return 0;
+ my $groups = $self->{nntpd}->{pi_config}->{-by_newsgroup};
+ for my $ngname (@window) {
+ my $ibx = $groups->{$ngname} or next;
+ my $c = eval { $ibx->uidvalidity } // time;
+ more($self, "$ngname $c <$ibx->{-primary_address}>");
}
+ scalar(@$groupnames); # continue if there's more
}
-sub list_active_times ($;$) {
+sub list_active_times ($;$) { # called by cmd_list
my ($self, $wildmat) = @_;
wildmat2re($wildmat);
- foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
- $ng->{newsgroup} =~ $wildmat or next;
- my $c = eval { $ng->mm->created_at } || time;
- more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
+ long_response($self, \&list_active_times_i, [
+ grep(/$wildmat/, @{$self->{nntpd}->{groupnames}}) ]);
+}
+
+sub list_newsgroups_i {
+ my ($self, $groupnames) = @_;
+ my @window = splice(@$groupnames, 0, 100) or return 0;
+ my $groups = $self->{nntpd}->{pi_config}->{-by_newsgroup};
+ my $ibx;
+ for my $ngname (@window) {
+ $ibx = $groups->{$ngname} and
+ more($self, "$ngname ".$ibx->description);
}
+ scalar(@$groupnames); # continue if there's more
}
-sub list_newsgroups ($;$) {
+sub list_newsgroups ($;$) { # called by cmd_list
my ($self, $wildmat) = @_;
wildmat2re($wildmat);
- foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
- $ng->{newsgroup} =~ $wildmat or next;
- my $d = $ng->description;
- more($self, "$ng->{newsgroup} $d");
- }
+ long_response($self, \&list_newsgroups_i, [
+ grep(/$wildmat/, @{$self->{nntpd}->{groupnames}}) ]);
}
# LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
if (scalar @args) {
my $arg = shift @args;
$arg =~ tr/A-Z./a-z_/;
+ my $ret = $arg eq 'active';
$arg = "list_$arg";
- return r501 if $DISABLED{$arg};
-
- $arg = eval {
- no strict 'refs';
- *{$arg}{CODE};
- };
+ $arg = $self->can($arg);
return r501 unless $arg && args_ok($arg, scalar @args);
more($self, '215 information follows');
$arg->($self, @args);
} else {
more($self, '215 list of newsgroups follows');
- foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
- group_line($self, $ng);
- }
+ long_response($self, \&list_active_i, [ # copy array
+ @{$self->{nntpd}->{groupnames}} ]);
}
- '.'
}
-sub cmd_listgroup ($;$) {
- my ($self, $group) = @_;
+sub listgroup_range_i {
+ my ($self, $beg, $end) = @_;
+ my $r = $self->{ng}->mm->msg_range($beg, $end, 'num');
+ scalar(@$r) or return;
+ more($self, join("\r\n", map { $_->[0] } @$r));
+ 1;
+}
+
+sub listgroup_all_i {
+ my ($self, $num) = @_;
+ my $ary = $self->{ng}->mm->ids_after($num);
+ scalar(@$ary) or return;
+ more($self, join("\r\n", @$ary));
+ 1;
+}
+
+sub cmd_listgroup ($;$$) {
+ my ($self, $group, $range) = @_;
if (defined $group) {
my $res = cmd_group($self, $group);
return $res if ($res !~ /\A211 /);
more($self, $res);
}
-
$self->{ng} or return '412 no newsgroup selected';
- long_response($self, 0, long_response_limit, sub {
- my ($i) = @_;
- my $nr = $self->{ng}->mm->id_batch($$i, sub {
- my ($ary) = @_;
- more($self, join("\r\n", @$ary));
- });
-
- # -1 to adjust for implicit increment in long_response
- $$i = $nr ? $$i + $nr - 1 : long_response_limit;
- });
+ if (defined $range) {
+ my $r = get_range($self, $range);
+ return $r unless ref $r;
+ long_response($self, \&listgroup_range_i, @$r);
+ } else { # grab every article number
+ long_response($self, \&listgroup_all_i, \(my $num = 0));
+ }
}
sub parse_time ($$;$) {
$gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
$gmt = 1;
}
- my @now = $gmt ? gmtime : localtime;
my ($YYYY, $MM, $DD);
- if (length($date) == 8) { # RFC 3977 allows YYYYMMDD
+ if (bytes::length($date) == 8) { # RFC 3977 allows YYYYMMDD
($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
} else { # legacy clients send YYMMDD
- ($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
+ my $YY;
+ ($YY, $MM, $DD) = unpack('A2A2A2', $date);
+ my @now = $gmt ? gmtime : localtime;
my $cur_year = $now[5] + 1900;
- if ($YYYY > $cur_year) {
- $YYYY += int($cur_year / 1000) * 1000 - 100;
- }
+ my $cur_cent = int($cur_year / 100) * 100;
+ $YYYY = (($YY + $cur_cent) > $cur_year) ?
+ ($YY + 1900) : ($YY + $cur_cent);
}
if ($gmt) {
timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
sub group_line ($$) {
my ($self, $ng) = @_;
my ($min, $max) = $ng->mm->minmax;
- more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
+ more($self, "$ng->{newsgroup} $max $min n");
+}
+
+sub newgroups_i {
+ my ($self, $ts, $i, $groupnames) = @_;
+ my $end = $$i + 100;
+ my $groups = $self->{nntpd}->{pi_config}->{-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;
}
sub cmd_newgroups ($$$;$$) {
# TODO dists
more($self, '231 list of new newsgroups follows');
- foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
- my $c = eval { $ng->mm->created_at } || 0;
- next unless $c > $ts;
- group_line($self, $ng);
- }
- '.'
+ long_response($self, \&newgroups_i, $ts, \(my $i = 0),
+ $self->{nntpd}->{groupnames});
}
sub wildmat2re (;$) {
$_[0] = qr/\A(?:$_[0])\z/;
}
+sub newnews_i {
+ my ($self, $names, $ts, $prev) = @_;
+ my $ngname = $names->[0];
+ if (my $ibx = $self->{nntpd}->{pi_config}->{-by_newsgroup}->{$ngname}) {
+ if (my $over = $ibx->over) {
+ my $msgs = $over->query_ts($ts, $$prev);
+ if (scalar @$msgs) {
+ more($self, '<' .
+ join(">\r\n<",
+ map { $_->{mid} } @$msgs ) .
+ '>');
+ $$prev = $msgs->[-1]->{num};
+ return 1; # continue on current group
+ }
+ }
+ }
+ shift @$names;
+ if (@$names) { # continue onto next newsgroup
+ $$prev = 0;
+ 1;
+ } else { # all done, break out of the long_response
+ undef;
+ }
+}
+
sub cmd_newnews ($$$$;$$) {
my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
my $ts = eval { parse_time($date, $time, $gmt) };
my ($keep, $skip) = split('!', $newsgroups, 2);
ngpat2re($keep);
ngpat2re($skip);
- my @srch;
- foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
- $ng->{newsgroup} =~ $keep or next;
- $ng->{newsgroup} =~ $skip and next;
- my $srch = $ng->search or next;
- push @srch, $srch;
- };
- return '.' unless @srch;
-
- $ts .= '..';
- my $opts = { asc => 1, limit => 1000, offset => 0 };
- long_response($self, 0, long_response_limit, sub {
- my ($i) = @_;
- my $srch = $srch[0];
- my $res = $srch->query_ts($ts, $opts);
- my $msgs = $res->{msgs};
- if (my $nr = scalar @$msgs) {
- more($self, '<' .
- join(">\r\n<", map { $_->mid } @$msgs ).
- '>');
- $opts->{offset} += $nr;
- } else {
- shift @srch;
- if (@srch) { # continue onto next newsgroup
- $opts->{offset} = 0;
- } else { # break out of the long response.
- $$i = long_response_limit;
- }
- }
- });
+ my @names = grep(!/$skip/, grep(/$keep/,
+ @{$self->{nntpd}->{groupnames}}));
+ return '.' unless scalar(@names);
+ my $prev = 0;
+ long_response($self, \&newnews_i, \@names, $ts, \$prev);
}
sub cmd_group ($$) {
my ($self, $group) = @_;
- my $no_such = '411 no such news group';
- my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
+ my $nntpd = $self->{nntpd};
+ my $ibx = $nntpd->{pi_config}->{-by_newsgroup}->{$group} or
+ return '411 no such news group';
+ $nntpd->idler_start;
- $self->{ng} = $ng;
- my ($min, $max) = $ng->mm->minmax;
- $min ||= 0;
- $max ||= 0;
+ $self->{ng} = $ibx;
+ my ($min, $max) = $ibx->mm->minmax;
$self->{article} = $min;
my $est_size = $max - $min;
"211 $est_size $min $max $group";
sub cmd_quit ($) {
my ($self) = @_;
res($self, '205 closing connection - goodbye!');
- $self->close;
+ $self->shutdn;
undef;
}
sub header_append ($$$) {
my ($hdr, $k, $v) = @_;
- my @v = $hdr->header($k);
+ my @v = $hdr->header_raw($k);
foreach (@v) {
return if $v eq $_;
}
$hdr->header_set($k, @v, $v);
}
-sub set_nntp_headers {
- my ($hdr, $ng, $n, $mid) = @_;
+sub xref_by_tc ($$$) {
+ my ($xref, $pi_cfg, $smsg) = @_;
+ my $by_addr = $pi_cfg->{-by_addr};
+ my $groups = $pi_cfg->{-by_newsgroup};
+ my $mid = $smsg->{mid};
+ for my $f (qw(to cc)) {
+ my @ibxs = map {
+ $by_addr->{lc($_)} // ()
+ } (PublicInbox::Address::emails($smsg->{$f} // ''));
+ for my $ibx (@ibxs) {
+ $groups->{my $ngname = $ibx->{newsgroup}} or next;
+ next if defined $xref->{$ngname};
+ $xref->{$ngname} = eval { $ibx->mm->num_for($mid) };
+ }
+ }
+}
+
+sub xref ($$$) {
+ my ($self, $cur_ibx, $smsg) = @_;
+ my $nntpd = $self->{nntpd};
+ my $cur_ng = $cur_ibx->{newsgroup};
+ my $xref;
+ if (my $ALL = $nntpd->{pi_config}->ALL) {
+ $xref = $ALL->nntp_xref_for($cur_ibx, $smsg);
+ xref_by_tc($xref, $nntpd->{pi_config}, $smsg);
+ } else { # slow path
+ $xref = { $cur_ng => $smsg->{num} };
+ my $mid = $smsg->{mid};
+ for my $ibx (values %{$nntpd->{pi_config}->{-by_newsgroup}}) {
+ next if defined($xref->{$ibx->{newsgroup}});
+ my $num = eval { $ibx->mm->num_for($mid) } // next;
+ $xref->{$ibx->{newsgroup}} = $num;
+ }
+ }
+ my $ret = "$nntpd->{servername} $cur_ng:".delete($xref->{$cur_ng});
+ $ret .= " $_:$xref->{$_}" for (sort keys %$xref);
+ $ret;
+}
+
+sub set_nntp_headers ($$) {
+ my ($hdr, $smsg) = @_;
+ my ($mid) = $smsg->{mid};
+
+ # leafnode (and maybe other NNTP clients) have trouble dealing
+ # with v2 messages which have multiple Message-IDs (either due
+ # to our own content-based dedupe or buggy git-send-email versions).
+ my @mids = $hdr->header_raw('Message-ID');
+ if (scalar(@mids) > 1) {
+ my $mid0 = "<$mid>";
+ $hdr->header_set('Message-ID', $mid0);
+ my @alt = $hdr->header_raw('X-Alt-Message-ID');
+ my %seen = map { $_ => 1 } (@alt, $mid0);
+ push(@alt, grep { !$seen{$_}++ } @mids);
+ $hdr->header_set('X-Alt-Message-ID', @alt);
+ }
+
+ # clobber some existing headers
+ my $ibx = $smsg->{-ibx};
+ my $xref = xref($smsg->{nntp}, $ibx, $smsg);
+ $hdr->header_set('Xref', $xref);
+
+ # RFC 5536 3.1.4
+ my ($server_name, $newsgroups) = split(/ /, $xref, 2);
+ $newsgroups =~ s/:[0-9]+\b//g; # drop NNTP article numbers
+ $newsgroups =~ tr/ /,/;
+ $hdr->header_set('Newsgroups', $newsgroups);
- # clobber some
- $hdr->header_set('Newsgroups', $ng->{newsgroup});
- $hdr->header_set('Xref', xref($ng, $n));
- header_append($hdr, 'List-Post', "<mailto:$ng->{-primary_address}>");
- if (my $url = $ng->base_url) {
+ # *something* here is required for leafnode, try to follow
+ # RFC 5536 3.1.5...
+ $hdr->header_set('Path', $server_name . '!not-for-mail');
+
+ header_append($hdr, 'List-Post', "<mailto:$ibx->{-primary_address}>");
+ if (my $url = $ibx->base_url) {
$mid = mid_escape($mid);
header_append($hdr, 'Archived-At', "<$url$mid/>");
header_append($hdr, 'List-Archive', "<$url>");
}
sub art_lookup ($$$) {
- my ($self, $art, $set_headers) = @_;
- my $ng = $self->{ng};
- my ($n, $mid);
+ my ($self, $art, $code) = @_;
+ my ($ibx, $n);
my $err;
if (defined $art) {
- if ($art =~ /\A\d+\z/o) {
+ if ($art =~ /\A[0-9]+\z/) {
$err = '423 no such article number in this group';
$n = int($art);
- goto find_mid;
- } elsif ($art =~ /\A<([^>]+)>\z/) {
- $mid = $1;
- $err = r430;
- $n = $ng->mm->num_for($mid) if $ng;
- goto found if defined $n;
- foreach my $g (values %{$self->{nntpd}->{groups}}) {
- $n = $g->mm->num_for($mid);
- if (defined $n) {
- $ng = $g;
- goto found;
- }
- }
- return $err;
+ goto find_ibx;
+ } elsif ($art =~ $ONE_MSGID) {
+ ($ibx, $n) = mid_lookup($self, $1);
+ goto found if $ibx;
+ return r430;
} else {
return r501;
}
} else {
$err = '420 no current article has been selected';
- $n = $self->{article};
- defined $n or return $err;
-find_mid:
- $ng or return '412 no newsgroup has been selected';
- $mid = $ng->mm->mid_for($n);
- defined $mid or return $err;
+ $n = $self->{article} // return $err;
+find_ibx:
+ $ibx = $self->{ng} or
+ return '412 no newsgroup has been selected';
}
found:
- my $bytes;
- my $s = eval { $ng->msg_by_mid($mid, \$bytes) } or return $err;
- $s = Email::Simple->new($s);
- my $lines;
- if ($set_headers) {
- set_nntp_headers($s->header_obj, $ng, $n, $mid);
- $lines = $s->body =~ tr!\n!\n!;
-
- # must be last
- $s->body_set('') if ($set_headers == 2);
- }
- [ $n, $mid, $s, $bytes, $lines, $ng ];
-}
-
-sub simple_body_write ($$) {
- my ($self, $s) = @_;
- my $body = $s->body;
- $s->body_set('');
- $body =~ s/^\./../smg;
- $body =~ s/(?<!\r)\n/\r\n/sg;
- do_more($self, $body);
- do_more($self, "\r\n") unless $body =~ /\r\n\z/s;
- '.'
+ my $smsg = $ibx->over->get_art($n) or return $err;
+ $smsg->{-ibx} = $ibx;
+ if ($code == 223) { # STAT
+ set_art($self, $n);
+ "223 $n <$smsg->{mid}> article retrieved - " .
+ "request text separately";
+ } else { # HEAD | BODY | ARTICLE
+ $smsg->{nntp} = $self;
+ $smsg->{nntp_code} = $code;
+ set_art($self, $art);
+ # this dereferences to `undef'
+ ${git_async_cat($ibx->git, $smsg->{blob}, \&blob_cb, $smsg)};
+ }
}
-sub set_art {
- my ($self, $art) = @_;
- $self->{article} = $art if defined $art && $art =~ /\A\d+\z/;
+sub msg_body_write ($$) {
+ my ($self, $msg) = @_;
+
+ # 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;
+ $self->msg_more($$msg);
}
-sub _header ($) {
- my $hdr = $_[0]->header_obj->as_string;
- utf8::encode($hdr);
- $hdr
+sub set_art {
+ my ($self, $art) = @_;
+ $self->{article} = $art if defined $art && $art =~ /\A[0-9]+\z/;
+}
+
+sub msg_hdr_write ($$) {
+ my ($eml, $smsg) = @_;
+ set_nntp_headers($eml, $smsg);
+
+ my $hdr = $eml->{hdr} // \(my $x = '');
+ # fixup old bug from import (pre-a0c07cba0e5d8b6a)
+ $$hdr =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+ $$hdr =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
+
+ # for leafnode compatibility, we need to ensure Message-ID headers
+ # are only a single line.
+ $$hdr =~ s/^(Message-ID:)[ \t]*\r\n[ \t]+([^\r]+)\r\n/$1 $2\r\n/igsm;
+ $smsg->{nntp}->msg_more($$hdr);
+}
+
+sub blob_cb { # called by git->cat_async via git_async_cat
+ my ($bref, $oid, $type, $size, $smsg) = @_;
+ my $self = $smsg->{nntp};
+ my $code = $smsg->{nntp_code};
+ if (!defined($oid)) {
+ # it's possible to have TOCTOU if an admin runs
+ # public-inbox-(edit|purge), just move onto the next message
+ warn "E: $smsg->{blob} missing in $smsg->{-ibx}->{inboxdir}\n";
+ return $self->requeue;
+ } elsif ($smsg->{blob} ne $oid) {
+ $self->close;
+ die "BUG: $smsg->{blob} != $oid";
+ }
+ my $r = "$code $smsg->{num} <$smsg->{mid}> article retrieved - ";
+ my $eml = PublicInbox::Eml->new($bref);
+ if ($code == 220) {
+ more($self, $r .= 'head and body follow');
+ msg_hdr_write($eml, $smsg);
+ $self->msg_more("\r\n");
+ msg_body_write($self, $bref);
+ } elsif ($code == 221) {
+ more($self, $r .= 'head follows');
+ msg_hdr_write($eml, $smsg);
+ } elsif ($code == 222) {
+ more($self, $r .= 'body follows');
+ msg_body_write($self, $bref);
+ } else {
+ $self->close;
+ die "BUG: bad code: $r";
+ }
+ $self->write(\".\r\n"); # flushes (includes ->zflush)
+ $self->requeue;
}
sub cmd_article ($;$) {
my ($self, $art) = @_;
- my $r = art_lookup($self, $art, 1);
- return $r unless ref $r;
- my ($n, $mid, $s) = @$r;
- set_art($self, $art);
- more($self, "220 $n <$mid> article retrieved - head and body follow");
- do_more($self, _header($s));
- do_more($self, "\r\n");
- simple_body_write($self, $s);
+ art_lookup($self, $art, 220);
}
sub cmd_head ($;$) {
my ($self, $art) = @_;
- my $r = art_lookup($self, $art, 2);
- return $r unless ref $r;
- my ($n, $mid, $s) = @$r;
- set_art($self, $art);
- more($self, "221 $n <$mid> article retrieved - head follows");
- do_more($self, _header($s));
- '.'
+ art_lookup($self, $art, 221);
}
sub cmd_body ($;$) {
my ($self, $art) = @_;
- my $r = art_lookup($self, $art, 0);
- return $r unless ref $r;
- my ($n, $mid, $s) = @$r;
- set_art($self, $art);
- more($self, "222 $n <$mid> article retrieved - body follows");
- simple_body_write($self, $s);
+ art_lookup($self, $art, 222);
}
sub cmd_stat ($;$) {
my ($self, $art) = @_;
- my $r = art_lookup($self, $art, 0);
- return $r unless ref $r;
- my ($n, $mid, undef) = @$r;
- set_art($self, $art);
- "223 $n <$mid> article retrieved - request text separately";
+ art_lookup($self, $art, 223); # art may be msgid
}
sub cmd_ihave ($) { '435 article not wanted - do not send it' }
defined $range or return '420 No article(s) selected';
my ($beg, $end);
my ($min, $max) = $ng->mm->minmax;
- if ($range =~ /\A(\d+)\z/) {
+ if ($range =~ /\A([0-9]+)\z/) {
$beg = $end = $1;
- } elsif ($range =~ /\A(\d+)-\z/) {
+ } elsif ($range =~ /\A([0-9]+)-\z/) {
($beg, $end) = ($1, $max);
- } elsif ($range =~ /\A(\d+)-(\d+)\z/) {
+ } elsif ($range =~ /\A([0-9]+)-([0-9]+)\z/) {
($beg, $end) = ($1, $2);
} else {
return r501;
$beg = $min if ($beg < $min);
$end = $max if ($end > $max);
return '420 No article(s) selected' if ($beg > $end);
- [ $beg, $end ];
+ [ \$beg, $end ];
+}
+
+sub long_step {
+ my ($self) = @_;
+ # wbuf is unset or empty, here; {long} may add to it
+ my ($fd, $cb, $t0, @args) = @{$self->{long_cb}};
+ my $more = eval { $cb->($self, @args) };
+ if ($@ || !$self->{sock}) { # something bad happened...
+ delete $self->{long_cb};
+ my $elapsed = now() - $t0;
+ if ($@) {
+ err($self,
+ "%s during long response[$fd] - %0.6f",
+ $@, $elapsed);
+ }
+ out($self, " deferred[$fd] aborted - %0.6f", $elapsed);
+ $self->close;
+ } elsif ($more) { # $self->{wbuf}:
+ $self->update_idle_time;
+
+ # COMPRESS users all share the same DEFLATE context.
+ # Flush it here to ensure clients don't see
+ # each other's data
+ $self->zflush;
+
+ # no recursion, schedule another call ASAP, but only after
+ # all pending writes are done. autovivify wbuf:
+ my $new_size = push(@{$self->{wbuf}}, \&long_step);
+
+ # wbuf may be populated by $cb, no need to rearm if so:
+ $self->requeue if $new_size == 1;
+ } else { # all done!
+ delete $self->{long_cb};
+ res($self, '.');
+ my $elapsed = now() - $t0;
+ my $fd = fileno($self->{sock});
+ out($self, " deferred[$fd] done - %0.6f", $elapsed);
+ my $wbuf = $self->{wbuf}; # do NOT autovivify
+ $self->requeue unless $wbuf && @$wbuf;
+ }
}
-sub long_response ($$$$) {
- my ($self, $beg, $end, $cb) = @_;
- die "BUG: nested long response" if $self->{long_res};
+sub long_response ($$;@) {
+ my ($self, $cb, @args) = @_; # cb returns true if more, false if done
- my $fd = $self->{fd};
- defined $fd or return;
+ my $sock = $self->{sock} or return;
# make sure we disable reading during a long response,
# clients should not be sending us stuff and making us do more
# work while we are stream a response to them
- $self->watch_read(0);
- my $t0 = now();
- $self->{long_res} = sub {
- # limit our own running time for fairness with other
- # clients and to avoid buffering too much:
- my $lim = 100;
-
- my $err;
- do {
- eval { $cb->(\$beg, \$lim) };
- } until (($err = $@) || $self->{closed} ||
- ++$beg > $end || --$lim < 0 ||
- $self->{write_buf_size});
-
- if ($err || $self->{closed}) {
- $self->{long_res} = undef;
-
- if ($err) {
- err($self,
- "%s during long response[$fd] - %0.6f",
- $err, now() - $t0);
- }
- if ($self->{closed}) {
- out($self, " deferred[$fd] aborted - %0.6f",
- now() - $t0);
- } else {
- update_idle_time($self);
- $self->watch_read(1);
- }
- } elsif ($lim < 0 || $self->{write_buf_size}) {
- # no recursion, schedule another call ASAP
- # but only after all pending writes are done
- update_idle_time($self);
-
- push @$nextq, $self;
- $nextt ||= PublicInbox::EvCleanup::asap(*next_tick);
- } else { # all done!
- $self->{long_res} = undef;
- $self->watch_read(1);
- res($self, '.');
- out($self, " deferred[$fd] done - %0.6f", now() - $t0);
- }
- };
- $self->{long_res}->(); # kick off!
+ $self->{long_cb} = [ fileno($sock), $cb, now(), @args ];
+ long_step($self); # kick off!
undef;
}
+sub hdr_msgid_range_i {
+ my ($self, $beg, $end) = @_;
+ my $r = $self->{ng}->mm->msg_range($beg, $end);
+ @$r or return;
+ more($self, join("\r\n", map { "$_->[0] <$_->[1]>" } @$r));
+ 1;
+}
+
sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
my ($self, $xhdr, $range) = @_;
- if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
+ if (defined $range && $range =~ $ONE_MSGID) {
my ($ng, $n) = mid_lookup($self, $1);
return r430 unless $n;
hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
$range = $self->{article} unless defined $range;
my $r = get_range($self, $range);
return $r unless ref $r;
- my $mm = $self->{ng}->mm;
- my ($beg, $end) = @$r;
more($self, $xhdr ? r221 : r225);
- long_response($self, $beg, $end, sub {
- my ($i) = @_;
- my $mid = $mm->mid_for($$i);
- more($self, "$$i <$mid>") if defined $mid;
- });
+ long_response($self, \&hdr_msgid_range_i, @$r);
}
}
-sub xref ($$) {
- my ($ng, $n) = @_;
- "$ng->{domain} $ng->{newsgroup}:$n"
-}
-
sub mid_lookup ($$) {
my ($self, $mid) = @_;
my $self_ng = $self->{ng};
my $n = $self_ng->mm->num_for($mid);
return ($self_ng, $n) if defined $n;
}
- foreach my $ng (values %{$self->{nntpd}->{groups}}) {
- next if defined $self_ng && $ng eq $self_ng;
- my $n = $ng->mm->num_for($mid);
- return ($ng, $n) if defined $n;
+ my $pi_cfg = $self->{nntpd}->{pi_config};
+ if (my $ALL = $pi_cfg->ALL) {
+ my ($id, $prev);
+ while (my $smsg = $ALL->over->next_by_mid($mid, \$id, \$prev)) {
+ my $xr3 = $ALL->over->get_xref3($smsg->{num});
+ if (my @x = grep(/:$smsg->{blob}\z/, @$xr3)) {
+ my ($ngname, $xnum) = split(/:/, $x[0]);
+ my $ibx = $pi_cfg->{-by_newsgroup}->{$ngname};
+ return ($ibx, $xnum) if $ibx;
+ # fall through to trying all xref3s
+ } else {
+ warn <<EOF;
+W: xref3 missing for <$mid> ($smsg->{blob}) in $ALL->{topdir}, -extindex bug?
+EOF
+ }
+ # try all xref3s
+ for my $x (@$xr3) {
+ my ($ngname, $xnum) = split(/:/, $x);
+ my $ibx = $pi_cfg->{-by_newsgroup}->{$ngname};
+ return ($ibx, $xnum) if $ibx;
+ warn "W: `$ngname' does not exist for #$xnum\n";
+ }
+ }
+ # no warning here, $mid is just invalid
+ } else { # slow path for non-ALL users
+ for my $ibx (values %{$pi_cfg->{-by_newsgroup}}) {
+ next if defined $self_ng && $ibx eq $self_ng;
+ my $n = $ibx->mm->num_for($mid);
+ return ($ibx, $n) if defined $n;
+ }
}
(undef, undef);
}
+sub xref_range_i {
+ my ($self, $beg, $end) = @_;
+ my $ng = $self->{ng};
+ my $msgs = $ng->over->query_xover($$beg, $end);
+ scalar(@$msgs) or return;
+ $$beg = $msgs->[-1]->{num} + 1;
+ more($self, join("\r\n", map {
+ "$_->{num} ".xref($self, $ng, $_);
+ } @$msgs));
+ 1;
+}
+
sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
my ($self, $xhdr, $range) = @_;
- if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
- my ($ng, $n) = mid_lookup($self, $1);
+ if (defined $range && $range =~ $ONE_MSGID) {
+ my $mid = $1;
+ my ($ng, $n) = mid_lookup($self, $mid);
return r430 unless $n;
- hdr_mid_response($self, $xhdr, $ng, $n, $range, xref($ng, $n));
+ my $smsg = $ng->over->get_art($n) or return;
+ hdr_mid_response($self, $xhdr, $ng, $n, $range,
+ xref($self, $ng, $smsg));
} else { # numeric range
$range = $self->{article} unless defined $range;
my $r = get_range($self, $range);
return $r unless ref $r;
- my $ng = $self->{ng};
- my $mm = $ng->mm;
- my ($beg, $end) = @$r;
more($self, $xhdr ? r221 : r225);
- long_response($self, $beg, $end, sub {
- my ($i) = @_;
- my $mid = $mm->mid_for($$i);
- more($self, "$$i ".xref($ng, $$i)) if defined $mid;
- });
+ long_response($self, \&xref_range_i, @$r);
}
}
-sub search_header_for {
- my ($srch, $mid, $field) = @_;
- my $smsg = $srch->lookup_mail($mid) or return;
- $smsg->$field;
+sub over_header_for {
+ my ($over, $num, $field) = @_;
+ my $smsg = $over->get_art($num) or return;
+ return PublicInbox::Smsg::date($smsg) if $field eq 'date';
+ $smsg->{$field};
}
-sub hdr_searchmsg ($$$$) {
+sub smsg_range_i {
+ my ($self, $beg, $end, $field) = @_;
+ my $over = $self->{ng}->over;
+ my $msgs = $over->query_xover($$beg, $end);
+ scalar(@$msgs) or return;
+ my $tmp = '';
+
+ # ->{$field} is faster than ->$field invocations, so favor that.
+ if ($field eq 'date') {
+ for my $s (@$msgs) {
+ $tmp .= "$s->{num} ".PublicInbox::Smsg::date($s)."\r\n"
+ }
+ } else {
+ for my $s (@$msgs) {
+ $tmp .= "$s->{num} $s->{$field}\r\n";
+ }
+ }
+ utf8::encode($tmp);
+ $self->msg_more($tmp);
+ $$beg = $msgs->[-1]->{num} + 1;
+}
+
+sub hdr_smsg ($$$$) {
my ($self, $xhdr, $field, $range) = @_;
- if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
+ if (defined $range && $range =~ $ONE_MSGID) {
my ($ng, $n) = mid_lookup($self, $1);
- return r430 unless $n;
- my $v = search_header_for($ng->search, $range, $field);
+ return r430 unless defined $n;
+ my $v = over_header_for($ng->over, $n, $field);
hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
} else { # numeric range
$range = $self->{article} unless defined $range;
- my $srch = $self->{ng}->search;
- my $mm = $self->{ng}->mm;
my $r = get_range($self, $range);
return $r unless ref $r;
- my ($beg, $end) = @$r;
more($self, $xhdr ? r221 : r225);
- my $off = 0;
- long_response($self, $beg, $end, sub {
- my ($i, $lim) = @_;
- my $res = $srch->query_xover($beg, $end, $off);
- my $msgs = $res->{msgs};
- my $nr = scalar @$msgs or return;
- $off += $nr;
- $$lim -= $nr;
- my $tmp = '';
- foreach my $s (@$msgs) {
- $tmp .= $s->num . ' ' . $s->$field . "\r\n";
- }
- utf8::encode($tmp);
- do_more($self, $tmp);
- # -1 to adjust for implicit increment in long_response
- $$i = $nr ? $$i + $nr - 1 : long_response_limit;
- });
+ long_response($self, \&smsg_range_i, @$r, $field);
}
}
hdr_xref($self, $xhdr, $range);
} elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
bytes|lines)\z/x) {
- hdr_searchmsg($self, $xhdr, $sub, $range);
+ hdr_smsg($self, $xhdr, $sub, $range);
} elsif ($sub =~ /\A:(bytes|lines)\z/) {
- hdr_searchmsg($self, $xhdr, $1, $range);
+ hdr_smsg($self, $xhdr, $1, $range);
} else {
$xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
}
}
sub hdr_mid_response ($$$$$$) {
- my ($self, $xhdr, $ng, $n, $mid, $v) = @_; # r: art_lookup result
+ my ($self, $xhdr, $ng, $n, $mid, $v) = @_;
my $res = '';
if ($xhdr) {
$res .= r221 . "\r\n";
undef;
}
+sub xrover_i {
+ my ($self, $beg, $end) = @_;
+ my $h = over_header_for($self->{ng}->over, $$beg, 'references');
+ more($self, "$$beg $h") if defined($h);
+ $$beg++ < $end;
+}
+
sub cmd_xrover ($;$) {
my ($self, $range) = @_;
my $ng = $self->{ng} or return '412 no newsgroup selected';
$range = $self->{article} unless defined $range;
my $r = get_range($self, $range);
return $r unless ref $r;
- my ($beg, $end) = @$r;
- my $mm = $ng->mm;
- my $srch = $ng->search;
more($self, '224 Overview information follows');
- long_response($self, $beg, $end, sub {
- my ($i) = @_;
- my $mid = $mm->mid_for($$i) or return;
- my $h = search_header_for($srch, $mid, 'references');
- more($self, "$$i $h");
- });
+ long_response($self, \&xrover_i, @$r);
}
-sub over_line ($$) {
- my ($num, $smsg) = @_;
+sub over_line ($$$) {
+ my ($self, $ng, $smsg) = @_;
# n.b. field access and procedural calls can be
# 10%-15% faster than OO method calls:
- my $s = join("\t", $num,
+ my $s = join("\t", $smsg->{num},
$smsg->{subject},
$smsg->{from},
- PublicInbox::SearchMsg::date($smsg),
- '<'.PublicInbox::SearchMsg::mid($smsg).'>',
+ PublicInbox::Smsg::date($smsg),
+ "<$smsg->{mid}>",
$smsg->{references},
- PublicInbox::SearchMsg::bytes($smsg),
- PublicInbox::SearchMsg::lines($smsg));
+ $smsg->{bytes},
+ $smsg->{lines},
+ "Xref: " . xref($self, $ng, $smsg));
utf8::encode($s);
$s
}
sub cmd_over ($;$) {
my ($self, $range) = @_;
- if ($range && $range =~ /\A<(.+)>\z/) {
+ if ($range && $range =~ $ONE_MSGID) {
my ($ng, $n) = mid_lookup($self, $1);
- my $smsg = $ng->search->lookup_mail($range) or
- return '430 No article with that message-id';
+ defined $n or return r430;
+ my $smsg = $ng->over->get_art($n) or return r430;
more($self, '224 Overview information follows (multi-line)');
# Only set article number column if it's the current group
+ # (RFC 3977 8.3.2)
my $self_ng = $self->{ng};
- $n = 0 if (!$self_ng || $self_ng ne $ng);
- more($self, over_line($n, $smsg));
+ if (!$self_ng || $self_ng ne $ng) {
+ # set {-orig_num} for nntp_xref_for
+ $smsg->{-orig_num} = $smsg->{num};
+ $smsg->{num} = 0;
+ }
+ more($self, over_line($self, $ng, $smsg));
'.';
} else {
cmd_xover($self, $range);
}
}
+sub xover_i {
+ my ($self, $beg, $end) = @_;
+ my $ng = $self->{ng};
+ my $msgs = $ng->over->query_xover($$beg, $end);
+ my $nr = scalar @$msgs or return;
+
+ # OVERVIEW.FMT
+ more($self, join("\r\n", map {
+ over_line($self, $ng, $_);
+ } @$msgs));
+ $$beg = $msgs->[-1]->{num} + 1;
+}
+
sub cmd_xover ($;$) {
my ($self, $range) = @_;
$range = $self->{article} unless defined $range;
my $r = get_range($self, $range);
return $r unless ref $r;
my ($beg, $end) = @$r;
- more($self, "224 Overview information follows for $beg to $end");
- my $srch = $self->{ng}->search;
- my $off = 0;
- long_response($self, $beg, $end, sub {
- my ($i, $lim) = @_;
- my $res = $srch->query_xover($beg, $end, $off);
- my $msgs = $res->{msgs};
- my $nr = scalar @$msgs or return;
- $off += $nr;
- $$lim -= $nr;
-
- # OVERVIEW.FMT
- more($self, join("\r\n", map {
- over_line(PublicInbox::SearchMsg::num($_), $_);
- } @$msgs));
-
- # -1 to adjust for implicit increment in long_response
- $$i = $nr ? $$i + $nr - 1 : long_response_limit;
- });
+ more($self, "224 Overview information follows for $$beg to $end");
+ long_response($self, \&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
+ return '580 can not initiate TLS negotiation';
+ res($self, '382 Continue with TLS negotiation');
+ $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
+ $self->requeue if PublicInbox::DS::accept_tls_step($self);
+ undef;
}
+# RFC 8054
+sub cmd_compress ($$) {
+ my ($self, $alg) = @_;
+ return '503 Only DEFLATE is supported' if uc($alg) ne 'DEFLATE';
+ return r502 if $self->compressed;
+ PublicInbox::NNTPdeflate->enable($self);
+ $self->requeue;
+ undef
+}
+
+sub zflush {} # overridden by NNTPdeflate
+
sub cmd_xpath ($$) {
my ($self, $mid) = @_;
- return r501 unless $mid =~ /\A<(.+)>\z/;
+ return r501 unless $mid =~ $ONE_MSGID;
$mid = $1;
my @paths;
- foreach my $ng (values %{$self->{nntpd}->{groups}}) {
- my $n = $ng->mm->num_for($mid);
- push @paths, "$ng->{newsgroup}/$n" if defined $n;
+ my $pi_cfg = $self->{nntpd}->{pi_config};
+ my $groups = $pi_cfg->{-by_newsgroup};
+ if (my $ALL = $pi_cfg->ALL) {
+ my ($id, $prev, %seen);
+ while (my $smsg = $ALL->over->next_by_mid($mid, \$id, \$prev)) {
+ my $xr3 = $ALL->over->get_xref3($smsg->{num});
+ for my $x (@$xr3) {
+ my ($ngname, $n) = split(/:/, $x);
+ $x = "$ngname/$n";
+ if ($groups->{$ngname} && !$seen{$x}++) {
+ push(@paths, $x);
+ }
+ }
+ }
+ } else { # slow path, no point in using long_response
+ for my $ibx (values %$groups) {
+ my $n = $ibx->mm->num_for($mid) // next;
+ push @paths, "$ibx->{newsgroup}/$n";
+ }
}
return '430 no such article on server' unless @paths;
- '223 '.join(' ', @paths);
+ '223 '.join(' ', sort(@paths));
}
-sub res ($$) {
- my ($self, $line) = @_;
- do_write($self, $line . "\r\n");
-}
+sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
-sub more ($$) {
- my ($self, $line) = @_;
- do_more($self, $line . "\r\n");
-}
+sub more ($$) { $_[0]->msg_more($_[1] . "\r\n") }
sub do_write ($$) {
- my ($self, $data) = @_;
- my $done = $self->write($data);
- return if $self->{closed};
-
- # Do not watch for readability if we have data in the queue,
- # instead re-enable watching for readability when we can
- $self->watch_read(0) if (!$done || $self->{long_res});
+ my $self = $_[0];
+ my $done = $self->write(\($_[1]));
+ return 0 unless $self->{sock};
$done;
}
printf { $self->{nntpd}->{out} } $fmt."\n", @args;
}
-use constant MSG_MORE => ($^O eq 'linux') ? 0x8000 : 0;
-
-sub do_more ($$) {
- my ($self, $data) = @_;
- if (MSG_MORE && !$self->{write_buf_size}) {
- my $n = send($self->{sock}, $data, MSG_MORE);
- if (defined $n) {
- my $dlen = length($data);
- return 1 if $n == $dlen; # all done!
- $data = substr($data, $n, $dlen - $n);
- }
- }
- do_write($self, $data);
-}
-
-# callbacks for Danga::Socket
-
-sub event_hup { $_[0]->close }
-sub event_err { $_[0]->close }
-
-sub event_write {
+# callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
+sub event_step {
my ($self) = @_;
- update_idle_time($self);
- # only continue watching for readability when we are done writing:
- if ($self->write(undef) == 1 && !$self->{long_res}) {
- $self->watch_read(1);
- }
-}
-sub event_read {
- my ($self) = @_;
- use constant LINE_MAX => 512; # RFC 977 section 2.3
+ return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
- if (index($self->{rbuf}, "\n") < 0) {
- my $buf = $self->read(LINE_MAX) or return $self->close;
- $self->{rbuf} .= $$buf;
- }
- my $r = 1;
- while ($r > 0 && $self->{rbuf} =~ s/\A\s*([^\r\n]*)\r?\n//) {
- my $line = $1;
- return $self->close if $line =~ /[[:cntrl:]]/s;
- my $t0 = now();
- my $fd = $self->{fd};
- $r = eval { process_line($self, $line) };
- my $d = $self->{long_res} ?
- " deferred[$fd]" : '';
- out($self, "[$fd] %s - %0.6f$d", $line, now() - $t0);
+ $self->update_idle_time;
+ # only read more requests if we've drained the write buffer,
+ # otherwise we can be buffering infinitely w/o backpressure
+
+ my $rbuf = $self->{rbuf} // \(my $x = '');
+ my $line = index($$rbuf, "\n");
+ while ($line < 0) {
+ return $self->close if length($$rbuf) >= LINE_MAX;
+ $self->do_read($rbuf, LINE_MAX, length($$rbuf)) or return;
+ $line = index($$rbuf, "\n");
}
+ $line = substr($$rbuf, 0, $line + 1, '');
+ $line =~ s/\r?\n\z//s;
+ return $self->close if $line =~ /[[:cntrl:]]/s;
+ my $t0 = now();
+ my $fd = fileno($self->{sock});
+ my $r = eval { process_line($self, $line) };
+ my $pending = $self->{wbuf} ? ' pending' : '';
+ out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
return $self->close if $r < 0;
- my $len = length($self->{rbuf});
- return $self->close if ($len >= LINE_MAX);
- update_idle_time($self);
-}
+ $self->rbuf_idle($rbuf);
+ $self->update_idle_time;
-sub watch_read {
- my ($self, $bool) = @_;
- my $rv = $self->SUPER::watch_read($bool);
- if ($bool && index($self->{rbuf}, "\n") >= 0) {
- # Force another read if there is a pipelined request.
- # We don't know if the socket has anything for us to read,
- # and we must double-check again by the time the timer fires
- # in case we really did dispatch a read event and started
- # another long response.
- push @$nextq, $self;
- $nextt ||= PublicInbox::EvCleanup::asap(*next_tick);
- }
- $rv;
-}
-
-sub not_idle_long ($$) {
- my ($self, $now) = @_;
- defined(my $fd = $self->{fd}) or return;
- my $ary = $EXPMAP->{$fd} or return;
- my $exp_at = $ary->[0] + $EXPTIME;
- $exp_at > $now;
+ # maybe there's more pipelined data, or we'll have
+ # to register it for socket-readiness notifications
+ $self->requeue unless $pending;
}
# for graceful shutdown in PublicInbox::Daemon:
sub busy {
my ($self, $now) = @_;
- ($self->{rbuf} ne '' || $self->{long_res} || $self->{write_buf_size} ||
- not_idle_long($self, $now));
+ ($self->{rbuf} || $self->{wbuf} || $self->not_idle_long($now));
}
1;