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);
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(EPOLLIN 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 event_step($_) for @$q;
53 $nextt ||= PublicInbox::EvCleanup::asap(*next_tick);
56 sub update_idle_time ($) {
58 my $sock = $self->{sock} or return;
59 $EXPMAP->{fileno($sock)} = [ now(), $self ];
65 my $old = $now - $exp;
69 while (my ($fd, $v) = each %$EXPMAP) {
70 my ($idle_time, $nntp) = @$v;
71 if ($idle_time < $old) {
85 $expt = PublicInbox::EvCleanup::later(*expire_old);
88 # noop to kick outselves out of the loop ASAP so descriptors
90 PublicInbox::EvCleanup::asap(sub {}) if $closed;
94 sub greet ($) { $_[0]->write($_[0]->{nntpd}->{greet}) };
97 my ($class, $sock, $nntpd) = @_;
98 my $self = fields::new($class);
101 if (ref($sock) eq 'IO::Socket::SSL' && !$sock->accept_SSL) {
102 $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
103 $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
105 $self->SUPER::new($sock, $ev | EPOLLONESHOT);
106 $self->{nntpd} = $nntpd;
108 $self->{wbuf} = $wbuf;
112 update_idle_time($self);
113 $expt ||= PublicInbox::EvCleanup::later(*expire_old);
118 my ($cb, $argc) = @_;
119 my $tot = prototype $cb;
120 my ($nreq, undef) = split(';', $tot);
121 $nreq = ($nreq =~ tr/$//) - 1;
122 $tot = ($tot =~ tr/$//) - 1;
123 ($argc <= $tot && $argc >= $nreq);
126 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
127 sub process_line ($$) {
129 my ($req, @args) = split(/[ \t]/, $l);
130 return 1 unless defined($req); # skip blank line
134 $req = $DISABLED{$req} ? undef : *{'cmd_'.$req}{CODE};
136 return res($self, '500 command not recognized') unless $req;
137 return res($self, r501) unless args_ok($req, scalar @args);
139 my $res = eval { $req->($self, @args) };
141 if ($err && $self->{sock}) {
144 err($self, 'error from: %s (%s)', $l, $err);
145 $res = '503 program fault - command not performed';
147 return 0 unless defined $res;
152 my ($self, $arg) = @_;
154 return r501 unless $arg eq 'READER';
155 '201 Posting prohibited';
158 sub cmd_slave ($) { '202 slave status noted' }
160 sub cmd_xgtitle ($;$) {
161 my ($self, $wildmat) = @_;
162 more($self, '282 list of groups and descriptions follows');
163 list_newsgroups($self, $wildmat);
167 sub list_overview_fmt ($) {
169 msg_more($self, $OVERVIEW_FMT);
172 sub list_headers ($;$) {
174 msg_more($self, $LIST_HEADERS);
177 sub list_active ($;$) {
178 my ($self, $wildmat) = @_;
179 wildmat2re($wildmat);
180 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
181 $ng->{newsgroup} =~ $wildmat or next;
182 group_line($self, $ng);
186 sub list_active_times ($;$) {
187 my ($self, $wildmat) = @_;
188 wildmat2re($wildmat);
189 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
190 $ng->{newsgroup} =~ $wildmat or next;
191 my $c = eval { $ng->mm->created_at } || time;
192 more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
196 sub list_newsgroups ($;$) {
197 my ($self, $wildmat) = @_;
198 wildmat2re($wildmat);
199 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
200 $ng->{newsgroup} =~ $wildmat or next;
201 my $d = $ng->description;
202 more($self, "$ng->{newsgroup} $d");
206 # LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
207 sub cmd_list ($;$$) {
208 my ($self, @args) = @_;
210 my $arg = shift @args;
211 $arg =~ tr/A-Z./a-z_/;
213 return r501 if $DISABLED{$arg};
219 return r501 unless $arg && args_ok($arg, scalar @args);
220 more($self, '215 information follows');
221 $arg->($self, @args);
223 more($self, '215 list of newsgroups follows');
224 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
225 group_line($self, $ng);
231 sub cmd_listgroup ($;$) {
232 my ($self, $group) = @_;
233 if (defined $group) {
234 my $res = cmd_group($self, $group);
235 return $res if ($res !~ /\A211 /);
239 $self->{ng} or return '412 no newsgroup selected';
241 long_response($self, sub {
242 my $ary = $self->{ng}->mm->ids_after(\$n);
243 scalar @$ary or return;
244 more($self, join("\r\n", @$ary));
249 sub parse_time ($$;$) {
250 my ($date, $time, $gmt) = @_;
251 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
253 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
256 my @now = $gmt ? gmtime : localtime;
257 my ($YYYY, $MM, $DD);
258 if (bytes::length($date) == 8) { # RFC 3977 allows YYYYMMDD
259 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
260 } else { # legacy clients send YYMMDD
261 ($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
262 my $cur_year = $now[5] + 1900;
263 if ($YYYY > $cur_year) {
264 $YYYY += int($cur_year / 1000) * 1000 - 100;
268 timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
270 timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
274 sub group_line ($$) {
275 my ($self, $ng) = @_;
276 my ($min, $max) = $ng->mm->minmax;
277 more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
280 sub cmd_newgroups ($$$;$$) {
281 my ($self, $date, $time, $gmt, $dists) = @_;
282 my $ts = eval { parse_time($date, $time, $gmt) };
286 more($self, '231 list of new newsgroups follows');
287 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
288 my $c = eval { $ng->mm->created_at } || 0;
289 next unless $c > $ts;
290 group_line($self, $ng);
295 sub wildmat2re (;$) {
296 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
301 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
303 my $key = sha1_hex($orig . $salt);
304 $orig =~ s/([^\w\-])+/\Q$1/g;
308 my %map = ('*' => '.*', '?' => '.' );
309 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
311 $tmp =~ s#([a-f0-9]{40})#
312 my $orig = $keep{$1};
313 defined $orig ? $orig : $1;
316 $_[0] = qr/\A$tmp\z/;
320 return $_[0] = qr/\A\z/ unless defined $_[0];
321 my %map = ('*' => '.*', ',' => '|');
322 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
323 $_[0] = qr/\A(?:$_[0])\z/;
326 sub cmd_newnews ($$$$;$$) {
327 my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
328 my $ts = eval { parse_time($date, $time, $gmt) };
330 more($self, '230 list of new articles by message-id follows');
331 my ($keep, $skip) = split('!', $newsgroups, 2);
335 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
336 $ng->{newsgroup} =~ $keep or next;
337 $ng->{newsgroup} =~ $skip and next;
338 my $over = $ng->over or next;
341 return '.' unless @over;
344 long_response($self, sub {
346 my $msgs = $over->query_ts($ts, $prev);
349 join(">\r\n<", map { $_->mid } @$msgs ).
351 $prev = $msgs->[-1]->{num};
354 if (@over) { # continue onto next newsgroup
357 } else { # break out of the long response.
365 my ($self, $group) = @_;
366 my $no_such = '411 no such news group';
367 my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
370 my ($min, $max) = $ng->mm->minmax;
373 $self->{article} = $min;
374 my $est_size = $max - $min;
375 "211 $est_size $min $max $group";
378 sub article_adj ($$) {
379 my ($self, $off) = @_;
380 my $ng = $self->{ng} or return '412 no newsgroup selected';
382 my $n = $self->{article};
383 defined $n or return '420 no current article has been selected';
386 my $mid = $ng->mm->mid_for($n);
388 $n = $off > 0 ? 'next' : 'previous';
389 return "421 no $n article in this group";
391 $self->{article} = $n;
392 "223 $n <$mid> article retrieved - request text separately";
395 sub cmd_next ($) { article_adj($_[0], 1) }
396 sub cmd_last ($) { article_adj($_[0], -1) }
398 # We want to encourage using email and CC-ing everybody involved to avoid
399 # the single-point-of-failure a single server provides.
402 my $ng = $self->{ng};
403 $ng ? "440 mailto:$ng->{-primary_address} to post"
404 : '440 posting not allowed'
409 res($self, '205 closing connection - goodbye!');
414 sub header_append ($$$) {
415 my ($hdr, $k, $v) = @_;
416 my @v = $hdr->header($k);
420 $hdr->header_set($k, @v, $v);
424 my ($self, $ng, $n, $mid) = @_;
425 my $ret = $self->{nntpd}->{servername} . " $ng->{newsgroup}:$n";
427 # num_for is pretty cheap and sometimes we'll lookup the existence
428 # of an article without getting even the OVER info. In other words,
429 # I'm not sure if its worth optimizing by scanning To:/Cc: and
430 # PublicInbox::ExtMsg on the PSGI end is just as expensive
431 foreach my $other (@{$self->{nntpd}->{grouplist}}) {
432 next if $ng eq $other;
433 my $num = eval { $other->mm->num_for($mid) } or next;
434 $ret .= " $other->{newsgroup}:$num";
439 sub set_nntp_headers ($$$$$) {
440 my ($self, $hdr, $ng, $n, $mid) = @_;
442 # why? leafnode requires a Path: header for some inexplicable
443 # reason. We'll fake the shortest one possible.
444 $hdr->header_set('Path', 'y');
446 # leafnode (and maybe other NNTP clients) have trouble dealing
447 # with v2 messages which have multiple Message-IDs (either due
448 # to our own content-based dedupe or buggy git-send-email versions).
449 my @mids = $hdr->header('Message-ID');
450 if (scalar(@mids) > 1) {
452 $hdr->header_set('Message-ID', $mid0);
453 my @alt = $hdr->header('X-Alt-Message-ID');
454 my %seen = map { $_ => 1 } (@alt, $mid0);
455 foreach my $m (@mids) {
459 $hdr->header_set('X-Alt-Message-ID', @alt);
463 my $xref = xref($self, $ng, $n, $mid);
464 $hdr->header_set('Xref', $xref);
465 $xref =~ s/:[0-9]+//g;
466 $hdr->header_set('Newsgroups', (split(/ /, $xref, 2))[1]);
467 header_append($hdr, 'List-Post', "<mailto:$ng->{-primary_address}>");
468 if (my $url = $ng->base_url) {
469 $mid = mid_escape($mid);
470 header_append($hdr, 'Archived-At', "<$url$mid/>");
471 header_append($hdr, 'List-Archive', "<$url>");
475 sub art_lookup ($$$) {
476 my ($self, $art, $set_headers) = @_;
477 my $ng = $self->{ng};
481 if ($art =~ /\A[0-9]+\z/) {
482 $err = '423 no such article number in this group';
485 } elsif ($art =~ /\A<([^>]+)>\z/) {
488 $n = $ng->mm->num_for($mid) if $ng;
489 goto found if defined $n;
490 foreach my $g (values %{$self->{nntpd}->{groups}}) {
491 $n = $g->mm->num_for($mid);
502 $err = '420 no current article has been selected';
503 $n = $self->{article};
504 defined $n or return $err;
506 $ng or return '412 no newsgroup has been selected';
507 $mid = $ng->mm->mid_for($n);
508 defined $mid or return $err;
511 my $smsg = $ng->over->get_art($n) or return $err;
512 my $msg = $ng->msg_by_smsg($smsg) or return $err;
513 my $s = Email::Simple->new($msg);
515 set_nntp_headers($self, $s->header_obj, $ng, $n, $mid);
518 $s->body_set('') if ($set_headers == 2);
520 [ $n, $mid, $s, $smsg->bytes, $smsg->lines, $ng ];
523 sub simple_body_write ($$) {
527 $body =~ s/^\./../smg;
528 $body =~ s/(?<!\r)\n/\r\n/sg;
529 msg_more($self, $body);
530 msg_more($self, "\r\n") unless $body =~ /\r\n\z/s;
535 my ($self, $art) = @_;
536 $self->{article} = $art if defined $art && $art =~ /\A[0-9]+\z/;
540 my $hdr = $_[0]->header_obj->as_string;
542 $hdr =~ s/(?<!\r)\n/\r\n/sg;
544 # for leafnode compatibility, we need to ensure Message-ID headers
545 # are only a single line. We can't subclass Email::Simple::Header
546 # and override _default_fold_at in here, either; since that won't
547 # affect messages already in the archive.
548 $hdr =~ s/^(Message-ID:)[ \t]*\r\n[ \t]+([^\r]+)\r\n/$1 $2\r\n/igsm;
553 sub cmd_article ($;$) {
554 my ($self, $art) = @_;
555 my $r = art_lookup($self, $art, 1);
556 return $r unless ref $r;
557 my ($n, $mid, $s) = @$r;
558 set_art($self, $art);
559 more($self, "220 $n <$mid> article retrieved - head and body follow");
560 msg_more($self, _header($s));
561 msg_more($self, "\r\n");
562 simple_body_write($self, $s);
566 my ($self, $art) = @_;
567 my $r = art_lookup($self, $art, 2);
568 return $r unless ref $r;
569 my ($n, $mid, $s) = @$r;
570 set_art($self, $art);
571 more($self, "221 $n <$mid> article retrieved - head follows");
572 msg_more($self, _header($s));
577 my ($self, $art) = @_;
578 my $r = art_lookup($self, $art, 0);
579 return $r unless ref $r;
580 my ($n, $mid, $s) = @$r;
581 set_art($self, $art);
582 more($self, "222 $n <$mid> article retrieved - body follows");
583 simple_body_write($self, $s);
587 my ($self, $art) = @_;
588 my $r = art_lookup($self, $art, 0);
589 return $r unless ref $r;
590 my ($n, $mid, undef) = @$r;
591 set_art($self, $art);
592 "223 $n <$mid> article retrieved - request text separately";
595 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
597 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
601 more($self, '100 help text follows');
606 my ($self, $range) = @_;
607 my $ng = $self->{ng} or return '412 no news group has been selected';
608 defined $range or return '420 No article(s) selected';
610 my ($min, $max) = $ng->mm->minmax;
611 if ($range =~ /\A([0-9]+)\z/) {
613 } elsif ($range =~ /\A([0-9]+)-\z/) {
614 ($beg, $end) = ($1, $max);
615 } elsif ($range =~ /\A([0-9]+)-([0-9]+)\z/) {
616 ($beg, $end) = ($1, $2);
620 $beg = $min if ($beg < $min);
621 $end = $max if ($end > $max);
622 return '420 No article(s) selected' if ($beg > $end);
626 sub long_response ($$) {
627 my ($self, $cb) = @_; # cb returns true if more, false if done
629 my $fd = fileno($self->{sock});
630 defined $fd or return;
631 # make sure we disable reading during a long response,
632 # clients should not be sending us stuff and making us do more
633 # work while we are stream a response to them
635 my $long_cb; # DANGER: self-referential
637 # wbuf is unset or empty, here; $cb may add to it
638 my $more = eval { $cb->() };
639 if ($@ || !$self->{sock}) { # something bad happened...
641 my $diff = now() - $t0;
644 "%s during long response[$fd] - %0.6f",
647 out($self, " deferred[$fd] aborted - %0.6f", $diff);
649 } elsif ($more) { # $self->{wbuf}:
650 update_idle_time($self);
652 # no recursion, schedule another call ASAP
653 # but only after all pending writes are done
654 my $wbuf = $self->{wbuf} ||= [];
655 push @$wbuf, $long_cb;
657 # wbuf may be populated by $cb, no need to rearm if so:
658 requeue($self) if scalar(@$wbuf) == 1;
662 out($self, " deferred[$fd] done - %0.6f", now() - $t0);
663 requeue($self) unless $self->{wbuf};
666 $self->write($long_cb); # kick off!
670 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
671 my ($self, $xhdr, $range) = @_;
673 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
674 my ($ng, $n) = mid_lookup($self, $1);
675 return r430 unless $n;
676 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
677 } else { # numeric range
678 $range = $self->{article} unless defined $range;
679 my $r = get_range($self, $range);
680 return $r unless ref $r;
681 my $mm = $self->{ng}->mm;
682 my ($beg, $end) = @$r;
683 more($self, $xhdr ? r221 : r225);
684 long_response($self, sub {
685 my $r = $mm->msg_range(\$beg, $end);
687 more($self, join("\r\n", map {
695 sub mid_lookup ($$) {
696 my ($self, $mid) = @_;
697 my $self_ng = $self->{ng};
699 my $n = $self_ng->mm->num_for($mid);
700 return ($self_ng, $n) if defined $n;
702 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
703 next if defined $self_ng && $ng eq $self_ng;
704 my $n = $ng->mm->num_for($mid);
705 return ($ng, $n) if defined $n;
710 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
711 my ($self, $xhdr, $range) = @_;
713 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
715 my ($ng, $n) = mid_lookup($self, $mid);
716 return r430 unless $n;
717 hdr_mid_response($self, $xhdr, $ng, $n, $range,
718 xref($self, $ng, $n, $mid));
719 } else { # numeric range
720 $range = $self->{article} unless defined $range;
721 my $r = get_range($self, $range);
722 return $r unless ref $r;
723 my $ng = $self->{ng};
725 my ($beg, $end) = @$r;
726 more($self, $xhdr ? r221 : r225);
727 long_response($self, sub {
728 my $r = $mm->msg_range(\$beg, $end);
730 more($self, join("\r\n", map {
732 "$num ".xref($self, $ng, $num, $_->[1]);
739 sub over_header_for {
740 my ($over, $num, $field) = @_;
741 my $smsg = $over->get_art($num) or return;
742 return PublicInbox::SearchMsg::date($smsg) if $field eq 'date';
746 sub hdr_searchmsg ($$$$) {
747 my ($self, $xhdr, $field, $range) = @_;
748 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
749 my ($ng, $n) = mid_lookup($self, $1);
750 return r430 unless defined $n;
751 my $v = over_header_for($ng->over, $n, $field);
752 hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
753 } else { # numeric range
754 $range = $self->{article} unless defined $range;
755 my $over = $self->{ng}->over;
756 my $mm = $self->{ng}->mm;
757 my $r = get_range($self, $range);
758 return $r unless ref $r;
759 my ($beg, $end) = @$r;
760 more($self, $xhdr ? r221 : r225);
762 long_response($self, sub {
763 my $msgs = $over->query_xover($cur, $end);
764 my $nr = scalar @$msgs or return;
766 foreach my $s (@$msgs) {
767 $tmp .= $s->{num} . ' ' . $s->$field . "\r\n";
770 msg_more($self, $tmp);
771 $cur = $msgs->[-1]->{num} + 1;
777 my ($self, $xhdr, $header, $range) = @_;
778 my $sub = lc $header;
779 if ($sub eq 'message-id') {
780 hdr_message_id($self, $xhdr, $range);
781 } elsif ($sub eq 'xref') {
782 hdr_xref($self, $xhdr, $range);
783 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
785 hdr_searchmsg($self, $xhdr, $sub, $range);
786 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
787 hdr_searchmsg($self, $xhdr, $1, $range);
789 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
795 my ($self, $header, $range) = @_;
796 do_hdr($self, 0, $header, $range);
800 sub cmd_xhdr ($$;$) {
801 my ($self, $header, $range) = @_;
802 do_hdr($self, 1, $header, $range);
805 sub hdr_mid_prefix ($$$$$) {
806 my ($self, $xhdr, $ng, $n, $mid) = @_;
807 return $mid if $xhdr;
809 # HDR for RFC 3977 users
810 if (my $self_ng = $self->{ng}) {
811 ($self_ng eq $ng) ? $n : '0';
817 sub hdr_mid_response ($$$$$$) {
818 my ($self, $xhdr, $ng, $n, $mid, $v) = @_; # r: art_lookup result
821 $res .= r221 . "\r\n";
822 $res .= "$mid $v\r\n";
824 $res .= r225 . "\r\n";
825 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
826 $res .= "$pfx $v\r\n";
828 res($self, $res .= '.');
832 sub cmd_xrover ($;$) {
833 my ($self, $range) = @_;
834 my $ng = $self->{ng} or return '412 no newsgroup selected';
835 (defined $range && $range =~ /[<>]/) and
836 return '420 No article(s) selected'; # no message IDs
838 $range = $self->{article} unless defined $range;
839 my $r = get_range($self, $range);
840 return $r unless ref $r;
841 my ($beg, $end) = @$r;
843 my $over = $ng->over;
844 more($self, '224 Overview information follows');
846 long_response($self, sub {
847 my $h = over_header_for($over, $beg, 'references');
848 more($self, "$beg $h") if defined($h);
853 sub over_line ($$$$) {
854 my ($self, $ng, $num, $smsg) = @_;
855 # n.b. field access and procedural calls can be
856 # 10%-15% faster than OO method calls:
857 my $s = join("\t", $num,
860 PublicInbox::SearchMsg::date($smsg),
865 "Xref: " . xref($self, $ng, $num, $smsg->{mid}));
871 my ($self, $range) = @_;
872 if ($range && $range =~ /\A<(.+)>\z/) {
873 my ($ng, $n) = mid_lookup($self, $1);
874 defined $n or return r430;
875 my $smsg = $ng->over->get_art($n) or return r430;
876 more($self, '224 Overview information follows (multi-line)');
878 # Only set article number column if it's the current group
879 my $self_ng = $self->{ng};
880 $n = 0 if (!$self_ng || $self_ng ne $ng);
881 more($self, over_line($self, $ng, $n, $smsg));
884 cmd_xover($self, $range);
888 sub cmd_xover ($;$) {
889 my ($self, $range) = @_;
890 $range = $self->{article} unless defined $range;
891 my $r = get_range($self, $range);
892 return $r unless ref $r;
893 my ($beg, $end) = @$r;
894 more($self, "224 Overview information follows for $beg to $end");
895 my $over = $self->{ng}->over;
897 long_response($self, sub {
898 my $msgs = $over->query_xover($cur, $end);
899 my $nr = scalar @$msgs or return;
902 more($self, join("\r\n", map {
903 over_line($self, $self->{ng}, $_->{num}, $_);
905 $cur = $msgs->[-1]->{num} + 1;
909 sub cmd_starttls ($) {
911 my $sock = $self->{sock} or return;
913 (ref($sock) eq 'IO::Socket::SSL') and return '502 Command unavailable';
914 my $opt = $self->{nntpd}->{accept_tls} or
915 return '580 can not initiate TLS negotiation';
916 res($self, '382 Continue with TLS negotiation');
917 $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
918 requeue($self) if PublicInbox::DS::accept_tls_step($self);
923 my ($self, $mid) = @_;
924 return r501 unless $mid =~ /\A<(.+)>\z/;
927 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
928 my $n = $ng->mm->num_for($mid);
929 push @paths, "$ng->{newsgroup}/$n" if defined $n;
931 return '430 no such article on server' unless @paths;
932 '223 '.join(' ', @paths);
935 sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
937 sub more ($$) { msg_more($_[0], $_[1] . "\r\n") }
941 my $done = $self->write(\($_[1]));
942 return 0 unless $self->{sock};
948 my ($self, $fmt, @args) = @_;
949 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
953 my ($self, $fmt, @args) = @_;
954 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
957 # callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
961 return unless $self->flush_write && $self->{sock};
963 update_idle_time($self);
964 # only read more requests if we've drained the write buffer,
965 # otherwise we can be buffering infinitely w/o backpressure
967 use constant LINE_MAX => 512; # RFC 977 section 2.3
968 my $rbuf = $self->{rbuf} // (\(my $x = ''));
971 if (index($$rbuf, "\n") < 0) {
972 my $off = bytes::length($$rbuf);
973 $r = $self->do_read($rbuf, LINE_MAX, $off) or return;
975 while ($r > 0 && $$rbuf =~ s/\A[ \t\r\n]*([^\r\n]*)\r?\n//) {
977 return $self->close if $line =~ /[[:cntrl:]]/s;
979 my $fd = fileno($self->{sock});
980 $r = eval { process_line($self, $line) };
981 my $pending = $self->{wbuf} ? ' pending' : '';
982 out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
985 return $self->close if $r < 0;
986 my $len = bytes::length($$rbuf);
987 return $self->close if ($len >= LINE_MAX);
989 $self->{rbuf} = $rbuf;
991 delete $self->{rbuf};
993 update_idle_time($self);
995 # maybe there's more pipelined data, or we'll have
996 # to register it for socket-readiness notifications
997 requeue($self) unless $self->{wbuf};
1000 sub not_idle_long ($$) {
1001 my ($self, $now) = @_;
1002 my $sock = $self->{sock} or return;
1003 my $ary = $EXPMAP->{fileno($sock)} or return;
1004 my $exp_at = $ary->[0] + $EXPTIME;
1008 # for graceful shutdown in PublicInbox::Daemon:
1010 my ($self, $now) = @_;
1011 ($self->{rbuf} || $self->{wbuf} || not_idle_long($self, $now));