1 # Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Each instance of this represents a NNTP client socket
5 package PublicInbox::NNTP;
8 use base qw(PublicInbox::DS);
9 use fields qw(nntpd article rbuf ng long_res);
10 use PublicInbox::Search;
11 use PublicInbox::Msgmap;
12 use PublicInbox::MID qw(mid_escape);
14 require PublicInbox::EvCleanup;
16 use POSIX qw(strftime);
17 PublicInbox::DS->import(qw(now msg_more));
18 use Digest::SHA qw(sha1_hex);
19 use Time::Local qw(timegm timelocal);
21 r501 => '501 command syntax error',
22 r221 => '221 Header follows',
23 r224 => '224 Overview information follows (multi-line)',
24 r225 => '225 Headers follow (multi-line)',
25 r430 => '430 No article with that message-id',
27 use PublicInbox::Syscall qw(EPOLLOUT EPOLLONESHOT);
30 my @OVERVIEW = qw(Subject From Date Message-ID References Xref);
31 my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines)) . ":\r\n";
32 my $LIST_HEADERS = join("\r\n", @OVERVIEW,
33 qw(:bytes :lines Xref To Cc)) . "\r\n";
35 # disable commands with easy DoS potential:
36 my %DISABLED; # = map { $_ => 1 } qw(xover list_overview_fmt newnews xhdr);
38 my $EXPMAP; # fd -> [ idle_time, $self ]
40 our $EXPTIME = 180; # 3 minutes
48 foreach my $nntp (@$q) {
49 # for request && response protocols, always finish writing
50 # before finishing reading:
51 if (my $long_cb = $nntp->{long_res}) {
52 $nntp->write($long_cb);
54 # pipelined request, we bypassed socket-readiness
63 $nextt ||= PublicInbox::EvCleanup::asap(*next_tick);
66 sub update_idle_time ($) {
68 my $sock = $self->{sock} or return;
69 $EXPMAP->{fileno($sock)} = [ now(), $self ];
75 my $old = $now - $exp;
78 while (my ($fd, $v) = each %$EXPMAP) {
79 my ($idle_time, $nntp) = @$v;
80 if ($idle_time < $old) {
81 $nntp->close; # idempotent
89 $expt = PublicInbox::EvCleanup::later(*expire_old);
92 # noop to kick outselves out of the loop ASAP so descriptors
94 PublicInbox::EvCleanup::asap(sub {});
99 my ($class, $sock, $nntpd) = @_;
100 my $self = fields::new($class);
101 my $ev = EPOLLOUT | EPOLLONESHOT;
103 if (ref($sock) eq 'IO::Socket::SSL' && !$sock->accept_SSL) {
104 $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
106 $wbuf->[0] = \&PublicInbox::DS::accept_tls_step;
108 $self->SUPER::new($sock, $ev);
109 $self->{nntpd} = $nntpd;
110 my $greet = "201 $nntpd->{servername} ready - post via email\r\n";
111 open my $fh, '<:scalar', \$greet or die "open :scalar: $!";
113 $self->{wbuf} = $wbuf;
115 update_idle_time($self);
116 $expt ||= PublicInbox::EvCleanup::later(*expire_old);
121 my ($cb, $argc) = @_;
122 my $tot = prototype $cb;
123 my ($nreq, undef) = split(';', $tot);
124 $nreq = ($nreq =~ tr/$//) - 1;
125 $tot = ($tot =~ tr/$//) - 1;
126 ($argc <= $tot && $argc >= $nreq);
129 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
130 sub process_line ($$) {
132 my ($req, @args) = split(/[ \t]/, $l);
133 return 1 unless defined($req); # skip blank line
137 $req = $DISABLED{$req} ? undef : *{'cmd_'.$req}{CODE};
139 return res($self, '500 command not recognized') unless $req;
140 return res($self, r501) unless args_ok($req, scalar @args);
142 my $res = eval { $req->($self, @args) };
144 if ($err && $self->{sock}) {
147 err($self, 'error from: %s (%s)', $l, $err);
148 $res = '503 program fault - command not performed';
150 return 0 unless defined $res;
155 my ($self, $arg) = @_;
157 return r501 unless $arg eq 'READER';
158 '201 Posting prohibited';
161 sub cmd_slave ($) { '202 slave status noted' }
163 sub cmd_xgtitle ($;$) {
164 my ($self, $wildmat) = @_;
165 more($self, '282 list of groups and descriptions follows');
166 list_newsgroups($self, $wildmat);
170 sub list_overview_fmt ($) {
172 msg_more($self, $OVERVIEW_FMT);
175 sub list_headers ($;$) {
177 msg_more($self, $LIST_HEADERS);
180 sub list_active ($;$) {
181 my ($self, $wildmat) = @_;
182 wildmat2re($wildmat);
183 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
184 $ng->{newsgroup} =~ $wildmat or next;
185 group_line($self, $ng);
189 sub list_active_times ($;$) {
190 my ($self, $wildmat) = @_;
191 wildmat2re($wildmat);
192 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
193 $ng->{newsgroup} =~ $wildmat or next;
194 my $c = eval { $ng->mm->created_at } || time;
195 more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
199 sub list_newsgroups ($;$) {
200 my ($self, $wildmat) = @_;
201 wildmat2re($wildmat);
202 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
203 $ng->{newsgroup} =~ $wildmat or next;
204 my $d = $ng->description;
205 more($self, "$ng->{newsgroup} $d");
209 # LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
210 sub cmd_list ($;$$) {
211 my ($self, @args) = @_;
213 my $arg = shift @args;
214 $arg =~ tr/A-Z./a-z_/;
216 return r501 if $DISABLED{$arg};
222 return r501 unless $arg && args_ok($arg, scalar @args);
223 more($self, '215 information follows');
224 $arg->($self, @args);
226 more($self, '215 list of newsgroups follows');
227 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
228 group_line($self, $ng);
234 sub cmd_listgroup ($;$) {
235 my ($self, $group) = @_;
236 if (defined $group) {
237 my $res = cmd_group($self, $group);
238 return $res if ($res !~ /\A211 /);
242 $self->{ng} or return '412 no newsgroup selected';
244 long_response($self, sub {
245 my $ary = $self->{ng}->mm->ids_after(\$n);
246 scalar @$ary or return;
247 more($self, join("\r\n", @$ary));
252 sub parse_time ($$;$) {
253 my ($date, $time, $gmt) = @_;
254 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
256 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
259 my @now = $gmt ? gmtime : localtime;
260 my ($YYYY, $MM, $DD);
261 if (bytes::length($date) == 8) { # RFC 3977 allows YYYYMMDD
262 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
263 } else { # legacy clients send YYMMDD
264 ($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
265 my $cur_year = $now[5] + 1900;
266 if ($YYYY > $cur_year) {
267 $YYYY += int($cur_year / 1000) * 1000 - 100;
271 timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
273 timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
277 sub group_line ($$) {
278 my ($self, $ng) = @_;
279 my ($min, $max) = $ng->mm->minmax;
280 more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
283 sub cmd_newgroups ($$$;$$) {
284 my ($self, $date, $time, $gmt, $dists) = @_;
285 my $ts = eval { parse_time($date, $time, $gmt) };
289 more($self, '231 list of new newsgroups follows');
290 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
291 my $c = eval { $ng->mm->created_at } || 0;
292 next unless $c > $ts;
293 group_line($self, $ng);
298 sub wildmat2re (;$) {
299 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
304 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
306 my $key = sha1_hex($orig . $salt);
307 $orig =~ s/([^\w\-])+/\Q$1/g;
311 my %map = ('*' => '.*', '?' => '.' );
312 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
314 $tmp =~ s#([a-f0-9]{40})#
315 my $orig = $keep{$1};
316 defined $orig ? $orig : $1;
319 $_[0] = qr/\A$tmp\z/;
323 return $_[0] = qr/\A\z/ unless defined $_[0];
324 my %map = ('*' => '.*', ',' => '|');
325 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
326 $_[0] = qr/\A(?:$_[0])\z/;
329 sub cmd_newnews ($$$$;$$) {
330 my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
331 my $ts = eval { parse_time($date, $time, $gmt) };
333 more($self, '230 list of new articles by message-id follows');
334 my ($keep, $skip) = split('!', $newsgroups, 2);
338 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
339 $ng->{newsgroup} =~ $keep or next;
340 $ng->{newsgroup} =~ $skip and next;
341 my $over = $ng->over or next;
344 return '.' unless @over;
347 long_response($self, sub {
349 my $msgs = $over->query_ts($ts, $prev);
352 join(">\r\n<", map { $_->mid } @$msgs ).
354 $prev = $msgs->[-1]->{num};
357 if (@over) { # continue onto next newsgroup
360 } else { # break out of the long response.
368 my ($self, $group) = @_;
369 my $no_such = '411 no such news group';
370 my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
373 my ($min, $max) = $ng->mm->minmax;
376 $self->{article} = $min;
377 my $est_size = $max - $min;
378 "211 $est_size $min $max $group";
381 sub article_adj ($$) {
382 my ($self, $off) = @_;
383 my $ng = $self->{ng} or return '412 no newsgroup selected';
385 my $n = $self->{article};
386 defined $n or return '420 no current article has been selected';
389 my $mid = $ng->mm->mid_for($n);
391 $n = $off > 0 ? 'next' : 'previous';
392 return "421 no $n article in this group";
394 $self->{article} = $n;
395 "223 $n <$mid> article retrieved - request text separately";
398 sub cmd_next ($) { article_adj($_[0], 1) }
399 sub cmd_last ($) { article_adj($_[0], -1) }
401 # We want to encourage using email and CC-ing everybody involved to avoid
402 # the single-point-of-failure a single server provides.
405 my $ng = $self->{ng};
406 $ng ? "440 mailto:$ng->{-primary_address} to post"
407 : '440 posting not allowed'
412 res($self, '205 closing connection - goodbye!');
417 sub header_append ($$$) {
418 my ($hdr, $k, $v) = @_;
419 my @v = $hdr->header($k);
423 $hdr->header_set($k, @v, $v);
427 my ($self, $ng, $n, $mid) = @_;
428 my $ret = $self->{nntpd}->{servername} . " $ng->{newsgroup}:$n";
430 # num_for is pretty cheap and sometimes we'll lookup the existence
431 # of an article without getting even the OVER info. In other words,
432 # I'm not sure if its worth optimizing by scanning To:/Cc: and
433 # PublicInbox::ExtMsg on the PSGI end is just as expensive
434 foreach my $other (@{$self->{nntpd}->{grouplist}}) {
435 next if $ng eq $other;
436 my $num = eval { $other->mm->num_for($mid) } or next;
437 $ret .= " $other->{newsgroup}:$num";
442 sub set_nntp_headers ($$$$$) {
443 my ($self, $hdr, $ng, $n, $mid) = @_;
445 # why? leafnode requires a Path: header for some inexplicable
446 # reason. We'll fake the shortest one possible.
447 $hdr->header_set('Path', 'y');
449 # leafnode (and maybe other NNTP clients) have trouble dealing
450 # with v2 messages which have multiple Message-IDs (either due
451 # to our own content-based dedupe or buggy git-send-email versions).
452 my @mids = $hdr->header('Message-ID');
453 if (scalar(@mids) > 1) {
455 $hdr->header_set('Message-ID', $mid0);
456 my @alt = $hdr->header('X-Alt-Message-ID');
457 my %seen = map { $_ => 1 } (@alt, $mid0);
458 foreach my $m (@mids) {
462 $hdr->header_set('X-Alt-Message-ID', @alt);
466 my $xref = xref($self, $ng, $n, $mid);
467 $hdr->header_set('Xref', $xref);
468 $xref =~ s/:[0-9]+//g;
469 $hdr->header_set('Newsgroups', (split(/ /, $xref, 2))[1]);
470 header_append($hdr, 'List-Post', "<mailto:$ng->{-primary_address}>");
471 if (my $url = $ng->base_url) {
472 $mid = mid_escape($mid);
473 header_append($hdr, 'Archived-At', "<$url$mid/>");
474 header_append($hdr, 'List-Archive', "<$url>");
478 sub art_lookup ($$$) {
479 my ($self, $art, $set_headers) = @_;
480 my $ng = $self->{ng};
484 if ($art =~ /\A[0-9]+\z/) {
485 $err = '423 no such article number in this group';
488 } elsif ($art =~ /\A<([^>]+)>\z/) {
491 $n = $ng->mm->num_for($mid) if $ng;
492 goto found if defined $n;
493 foreach my $g (values %{$self->{nntpd}->{groups}}) {
494 $n = $g->mm->num_for($mid);
505 $err = '420 no current article has been selected';
506 $n = $self->{article};
507 defined $n or return $err;
509 $ng or return '412 no newsgroup has been selected';
510 $mid = $ng->mm->mid_for($n);
511 defined $mid or return $err;
514 my $smsg = $ng->over->get_art($n) or return $err;
515 my $msg = $ng->msg_by_smsg($smsg) or return $err;
516 my $s = Email::Simple->new($msg);
518 set_nntp_headers($self, $s->header_obj, $ng, $n, $mid);
521 $s->body_set('') if ($set_headers == 2);
523 [ $n, $mid, $s, $smsg->bytes, $smsg->lines, $ng ];
526 sub simple_body_write ($$) {
530 $body =~ s/^\./../smg;
531 $body =~ s/(?<!\r)\n/\r\n/sg;
532 msg_more($self, $body);
533 msg_more($self, "\r\n") unless $body =~ /\r\n\z/s;
538 my ($self, $art) = @_;
539 $self->{article} = $art if defined $art && $art =~ /\A[0-9]+\z/;
543 my $hdr = $_[0]->header_obj->as_string;
545 $hdr =~ s/(?<!\r)\n/\r\n/sg;
547 # for leafnode compatibility, we need to ensure Message-ID headers
548 # are only a single line. We can't subclass Email::Simple::Header
549 # and override _default_fold_at in here, either; since that won't
550 # affect messages already in the archive.
551 $hdr =~ s/^(Message-ID:)[ \t]*\r\n[ \t]+([^\r]+)\r\n/$1 $2\r\n/igsm;
556 sub cmd_article ($;$) {
557 my ($self, $art) = @_;
558 my $r = art_lookup($self, $art, 1);
559 return $r unless ref $r;
560 my ($n, $mid, $s) = @$r;
561 set_art($self, $art);
562 more($self, "220 $n <$mid> article retrieved - head and body follow");
563 msg_more($self, _header($s));
564 msg_more($self, "\r\n");
565 simple_body_write($self, $s);
569 my ($self, $art) = @_;
570 my $r = art_lookup($self, $art, 2);
571 return $r unless ref $r;
572 my ($n, $mid, $s) = @$r;
573 set_art($self, $art);
574 more($self, "221 $n <$mid> article retrieved - head follows");
575 msg_more($self, _header($s));
580 my ($self, $art) = @_;
581 my $r = art_lookup($self, $art, 0);
582 return $r unless ref $r;
583 my ($n, $mid, $s) = @$r;
584 set_art($self, $art);
585 more($self, "222 $n <$mid> article retrieved - body follows");
586 simple_body_write($self, $s);
590 my ($self, $art) = @_;
591 my $r = art_lookup($self, $art, 0);
592 return $r unless ref $r;
593 my ($n, $mid, undef) = @$r;
594 set_art($self, $art);
595 "223 $n <$mid> article retrieved - request text separately";
598 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
600 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
604 more($self, '100 help text follows');
609 my ($self, $range) = @_;
610 my $ng = $self->{ng} or return '412 no news group has been selected';
611 defined $range or return '420 No article(s) selected';
613 my ($min, $max) = $ng->mm->minmax;
614 if ($range =~ /\A([0-9]+)\z/) {
616 } elsif ($range =~ /\A([0-9]+)-\z/) {
617 ($beg, $end) = ($1, $max);
618 } elsif ($range =~ /\A([0-9]+)-([0-9]+)\z/) {
619 ($beg, $end) = ($1, $2);
623 $beg = $min if ($beg < $min);
624 $end = $max if ($end > $max);
625 return '420 No article(s) selected' if ($beg > $end);
629 sub long_response ($$) {
630 my ($self, $cb) = @_;
631 die "BUG: nested long response" if $self->{long_res};
633 my $fd = fileno($self->{sock});
634 defined $fd or return;
635 # make sure we disable reading during a long response,
636 # clients should not be sending us stuff and making us do more
637 # work while we are stream a response to them
639 $self->{long_res} = sub {
640 my $more = eval { $cb->() };
641 if ($@ || !$self->{sock}) { # something bad happened...
642 delete $self->{long_res};
646 "%s during long response[$fd] - %0.6f",
650 update_idle_time($self);
653 out($self, " deferred[$fd] aborted - %0.6f",
656 } elsif ($more) { # $self->{wbuf}:
657 # no recursion, schedule another call ASAP
658 # but only after all pending writes are done
659 update_idle_time($self);
662 delete $self->{long_res};
664 out($self, " deferred[$fd] done - %0.6f", now() - $t0);
668 $self->{long_res}->(); # kick off!
672 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
673 my ($self, $xhdr, $range) = @_;
675 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
676 my ($ng, $n) = mid_lookup($self, $1);
677 return r430 unless $n;
678 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
679 } else { # numeric range
680 $range = $self->{article} unless defined $range;
681 my $r = get_range($self, $range);
682 return $r unless ref $r;
683 my $mm = $self->{ng}->mm;
684 my ($beg, $end) = @$r;
685 more($self, $xhdr ? r221 : r225);
686 long_response($self, sub {
687 my $r = $mm->msg_range(\$beg, $end);
689 more($self, join("\r\n", map {
697 sub mid_lookup ($$) {
698 my ($self, $mid) = @_;
699 my $self_ng = $self->{ng};
701 my $n = $self_ng->mm->num_for($mid);
702 return ($self_ng, $n) if defined $n;
704 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
705 next if defined $self_ng && $ng eq $self_ng;
706 my $n = $ng->mm->num_for($mid);
707 return ($ng, $n) if defined $n;
712 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
713 my ($self, $xhdr, $range) = @_;
715 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
717 my ($ng, $n) = mid_lookup($self, $mid);
718 return r430 unless $n;
719 hdr_mid_response($self, $xhdr, $ng, $n, $range,
720 xref($self, $ng, $n, $mid));
721 } else { # numeric range
722 $range = $self->{article} unless defined $range;
723 my $r = get_range($self, $range);
724 return $r unless ref $r;
725 my $ng = $self->{ng};
727 my ($beg, $end) = @$r;
728 more($self, $xhdr ? r221 : r225);
729 long_response($self, sub {
730 my $r = $mm->msg_range(\$beg, $end);
732 more($self, join("\r\n", map {
734 "$num ".xref($self, $ng, $num, $_->[1]);
741 sub over_header_for {
742 my ($over, $num, $field) = @_;
743 my $smsg = $over->get_art($num) or return;
744 return PublicInbox::SearchMsg::date($smsg) if $field eq 'date';
748 sub hdr_searchmsg ($$$$) {
749 my ($self, $xhdr, $field, $range) = @_;
750 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
751 my ($ng, $n) = mid_lookup($self, $1);
752 return r430 unless defined $n;
753 my $v = over_header_for($ng->over, $n, $field);
754 hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
755 } else { # numeric range
756 $range = $self->{article} unless defined $range;
757 my $over = $self->{ng}->over;
758 my $mm = $self->{ng}->mm;
759 my $r = get_range($self, $range);
760 return $r unless ref $r;
761 my ($beg, $end) = @$r;
762 more($self, $xhdr ? r221 : r225);
764 long_response($self, sub {
765 my $msgs = $over->query_xover($cur, $end);
766 my $nr = scalar @$msgs or return;
768 foreach my $s (@$msgs) {
769 $tmp .= $s->{num} . ' ' . $s->$field . "\r\n";
772 msg_more($self, $tmp);
773 $cur = $msgs->[-1]->{num} + 1;
779 my ($self, $xhdr, $header, $range) = @_;
780 my $sub = lc $header;
781 if ($sub eq 'message-id') {
782 hdr_message_id($self, $xhdr, $range);
783 } elsif ($sub eq 'xref') {
784 hdr_xref($self, $xhdr, $range);
785 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
787 hdr_searchmsg($self, $xhdr, $sub, $range);
788 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
789 hdr_searchmsg($self, $xhdr, $1, $range);
791 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
797 my ($self, $header, $range) = @_;
798 do_hdr($self, 0, $header, $range);
802 sub cmd_xhdr ($$;$) {
803 my ($self, $header, $range) = @_;
804 do_hdr($self, 1, $header, $range);
807 sub hdr_mid_prefix ($$$$$) {
808 my ($self, $xhdr, $ng, $n, $mid) = @_;
809 return $mid if $xhdr;
811 # HDR for RFC 3977 users
812 if (my $self_ng = $self->{ng}) {
813 ($self_ng eq $ng) ? $n : '0';
819 sub hdr_mid_response ($$$$$$) {
820 my ($self, $xhdr, $ng, $n, $mid, $v) = @_; # r: art_lookup result
823 $res .= r221 . "\r\n";
824 $res .= "$mid $v\r\n";
826 $res .= r225 . "\r\n";
827 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
828 $res .= "$pfx $v\r\n";
830 res($self, $res .= '.');
834 sub cmd_xrover ($;$) {
835 my ($self, $range) = @_;
836 my $ng = $self->{ng} or return '412 no newsgroup selected';
837 (defined $range && $range =~ /[<>]/) and
838 return '420 No article(s) selected'; # no message IDs
840 $range = $self->{article} unless defined $range;
841 my $r = get_range($self, $range);
842 return $r unless ref $r;
843 my ($beg, $end) = @$r;
845 my $over = $ng->over;
846 more($self, '224 Overview information follows');
848 long_response($self, sub {
849 my $h = over_header_for($over, $beg, 'references');
850 more($self, "$beg $h") if defined($h);
855 sub over_line ($$$$) {
856 my ($self, $ng, $num, $smsg) = @_;
857 # n.b. field access and procedural calls can be
858 # 10%-15% faster than OO method calls:
859 my $s = join("\t", $num,
862 PublicInbox::SearchMsg::date($smsg),
867 "Xref: " . xref($self, $ng, $num, $smsg->{mid}));
873 my ($self, $range) = @_;
874 if ($range && $range =~ /\A<(.+)>\z/) {
875 my ($ng, $n) = mid_lookup($self, $1);
876 defined $n or return r430;
877 my $smsg = $ng->over->get_art($n) or return r430;
878 more($self, '224 Overview information follows (multi-line)');
880 # Only set article number column if it's the current group
881 my $self_ng = $self->{ng};
882 $n = 0 if (!$self_ng || $self_ng ne $ng);
883 more($self, over_line($self, $ng, $n, $smsg));
886 cmd_xover($self, $range);
890 sub cmd_xover ($;$) {
891 my ($self, $range) = @_;
892 $range = $self->{article} unless defined $range;
893 my $r = get_range($self, $range);
894 return $r unless ref $r;
895 my ($beg, $end) = @$r;
896 more($self, "224 Overview information follows for $beg to $end");
897 my $over = $self->{ng}->over;
899 long_response($self, sub {
900 my $msgs = $over->query_xover($cur, $end);
901 my $nr = scalar @$msgs or return;
904 more($self, join("\r\n", map {
905 over_line($self, $self->{ng}, $_->{num}, $_);
907 $cur = $msgs->[-1]->{num} + 1;
911 sub cmd_starttls ($) {
913 my $sock = $self->{sock} or return;
915 (ref($sock) eq 'IO::Socket::SSL') and return '502 Command unavailable';
916 my $opt = $self->{nntpd}->{accept_tls} or
917 return '580 can not initiate TLS negotiation';
918 res($self, '382 Continue with TLS negotiation');
919 $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
920 requeue($self) if PublicInbox::DS::accept_tls_step($self);
925 my ($self, $mid) = @_;
926 return r501 unless $mid =~ /\A<(.+)>\z/;
929 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
930 my $n = $ng->mm->num_for($mid);
931 push @paths, "$ng->{newsgroup}/$n" if defined $n;
933 return '430 no such article on server' unless @paths;
934 '223 '.join(' ', @paths);
937 sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
939 sub more ($$) { msg_more($_[0], $_[1] . "\r\n") }
943 my $done = $self->write(\($_[1]));
944 return 0 unless $self->{sock};
950 my ($self, $fmt, @args) = @_;
951 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
955 my ($self, $fmt, @args) = @_;
956 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
959 # callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
963 return unless $self->flush_write && $self->{sock};
965 update_idle_time($self);
966 # only read more requests if we've drained the write buffer,
967 # otherwise we can be buffering infinitely w/o backpressure
969 use constant LINE_MAX => 512; # RFC 977 section 2.3
970 my $rbuf = \($self->{rbuf});
973 if (index($$rbuf, "\n") < 0) {
974 my $off = bytes::length($$rbuf);
975 $r = $self->do_read($rbuf, LINE_MAX, $off) or return;
977 while ($r > 0 && $$rbuf =~ s/\A[ \t\r\n]*([^\r\n]*)\r?\n//) {
979 return $self->close if $line =~ /[[:cntrl:]]/s;
981 my $fd = fileno($self->{sock});
982 $r = eval { process_line($self, $line) };
983 my $d = $self->{long_res} ?
984 " deferred[$fd]" : '';
985 out($self, "[$fd] %s - %0.6f$d", $line, now() - $t0);
988 return $self->close if $r < 0;
989 my $len = bytes::length($$rbuf);
990 return $self->close if ($len >= LINE_MAX);
991 update_idle_time($self);
993 # maybe there's more pipelined data, or we'll have
994 # to register it for socket-readiness notifications
995 requeue($self) unless ($self->{long_res} || $self->{wbuf});
998 sub not_idle_long ($$) {
999 my ($self, $now) = @_;
1000 my $sock = $self->{sock} or return;
1001 my $ary = $EXPMAP->{fileno($sock)} or return;
1002 my $exp_at = $ary->[0] + $EXPTIME;
1006 # for graceful shutdown in PublicInbox::Daemon:
1008 my ($self, $now) = @_;
1009 ($self->{rbuf} ne '' || $self->{long_res} ||
1010 $self->{wbuf} || not_idle_long($self, $now));