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 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 use PublicInbox::DS qw(now);
18 use Digest::SHA qw(sha1_hex);
19 use Time::Local qw(timegm timelocal);
21 LINE_MAX => 512, # RFC 977 section 2.3
22 r501 => '501 command syntax error',
23 r502 => '502 Command unavailable',
24 r221 => '221 Header follows',
25 r224 => '224 Overview information follows (multi-line)',
26 r225 => '225 Headers follow (multi-line)',
27 r430 => '430 No article with that message-id',
29 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
32 my @OVERVIEW = qw(Subject From Date Message-ID References);
33 my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines), '') .
35 my $LIST_HEADERS = join("\r\n", @OVERVIEW,
36 qw(:bytes :lines Xref To Cc)) . "\r\n";
37 my $CAPABILITIES = <<"";
38 101 Capability list:\r
42 LIST ACTIVE ACTIVE.TIMES NEWSGROUPS OVERVIEW.FMT\r
47 my $EXPMAP; # fd -> [ idle_time, $self ]
49 our $EXPTIME = 180; # 3 minutes
51 sub update_idle_time ($) {
53 my $sock = $self->{sock} or return;
54 $EXPMAP->{fileno($sock)} = [ now(), $self ];
60 my $old = $now - $exp;
62 while (my ($fd, $v) = each %$EXPMAP) {
63 my ($idle_time, $nntp) = @$v;
64 if ($idle_time < $old) {
73 $expt = scalar(keys %new) ? PublicInbox::EvCleanup::later(*expire_old)
77 sub greet ($) { $_[0]->write($_[0]->{nntpd}->{greet}) };
80 my ($class, $sock, $nntpd) = @_;
81 my $self = fields::new($class);
84 if (ref($sock) eq 'IO::Socket::SSL' && !$sock->accept_SSL) {
85 return CORE::close($sock) if $! != EAGAIN;
86 $ev = PublicInbox::TLS::epollbit();
87 $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
89 $self->SUPER::new($sock, $ev | EPOLLONESHOT);
90 $self->{nntpd} = $nntpd;
92 $self->{wbuf} = $wbuf;
96 update_idle_time($self);
97 $expt ||= PublicInbox::EvCleanup::later(*expire_old);
102 my ($cb, $argc) = @_;
103 my $tot = prototype $cb;
104 my ($nreq, undef) = split(';', $tot);
105 $nreq = ($nreq =~ tr/$//) - 1;
106 $tot = ($tot =~ tr/$//) - 1;
107 ($argc <= $tot && $argc >= $nreq);
110 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
111 sub process_line ($$) {
113 my ($req, @args) = split(/[ \t]/, $l);
114 return 1 unless defined($req); # skip blank line
117 *{'cmd_'.lc($req)}{CODE};
119 return res($self, '500 command not recognized') unless $req;
120 return res($self, r501) unless args_ok($req, scalar @args);
122 my $res = eval { $req->($self, @args) };
124 if ($err && $self->{sock}) {
127 err($self, 'error from: %s (%s)', $l, $err);
128 $res = '503 program fault - command not performed';
130 return 0 unless defined $res;
134 # The keyword argument is not used (rfc3977 5.2.2)
135 sub cmd_capabilities ($;$) {
136 my ($self, undef) = @_;
137 my $res = $CAPABILITIES;
138 if (ref($self->{sock}) ne 'IO::Socket::SSL' &&
139 $self->{nntpd}->{accept_tls}) {
140 $res .= "STARTTLS\r\n";
146 my ($self, $arg) = @_;
148 return r501 unless $arg eq 'READER';
149 '201 Posting prohibited';
152 sub cmd_slave ($) { '202 slave status noted' }
154 sub cmd_xgtitle ($;$) {
155 my ($self, $wildmat) = @_;
156 more($self, '282 list of groups and descriptions follows');
157 list_newsgroups($self, $wildmat);
161 sub list_overview_fmt ($) {
163 $self->msg_more($OVERVIEW_FMT);
166 sub list_headers ($;$) {
168 $self->msg_more($LIST_HEADERS);
171 sub list_active ($;$) {
172 my ($self, $wildmat) = @_;
173 wildmat2re($wildmat);
174 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
175 $ng->{newsgroup} =~ $wildmat or next;
176 group_line($self, $ng);
180 sub list_active_times ($;$) {
181 my ($self, $wildmat) = @_;
182 wildmat2re($wildmat);
183 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
184 $ng->{newsgroup} =~ $wildmat or next;
185 my $c = eval { $ng->mm->created_at } || time;
186 more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
190 sub list_newsgroups ($;$) {
191 my ($self, $wildmat) = @_;
192 wildmat2re($wildmat);
193 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
194 $ng->{newsgroup} =~ $wildmat or next;
195 my $d = $ng->description;
196 more($self, "$ng->{newsgroup} $d");
200 # LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
201 sub cmd_list ($;$$) {
202 my ($self, @args) = @_;
204 my $arg = shift @args;
205 $arg =~ tr/A-Z./a-z_/;
212 return r501 unless $arg && args_ok($arg, scalar @args);
213 more($self, '215 information follows');
214 $arg->($self, @args);
216 more($self, '215 list of newsgroups follows');
217 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
218 group_line($self, $ng);
224 sub cmd_listgroup ($;$$) {
225 my ($self, $group, $range) = @_;
226 if (defined $group) {
227 my $res = cmd_group($self, $group);
228 return $res if ($res !~ /\A211 /);
231 my $ng = $self->{ng} or return '412 no newsgroup selected';
233 if (defined $range) {
234 my $r = get_range($self, $range);
235 return $r unless ref $r;
236 my ($beg, $end) = @$r;
237 long_response($self, sub {
238 $r = $mm->msg_range(\$beg, $end, 'num');
239 scalar(@$r) or return;
240 more($self, join("\r\n", map { $_->[0] } @$r));
243 } else { # grab every article number
245 long_response($self, sub {
246 my $ary = $mm->ids_after(\$n);
247 scalar(@$ary) or return;
248 more($self, join("\r\n", @$ary));
254 sub parse_time ($$;$) {
255 my ($date, $time, $gmt) = @_;
256 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
258 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
261 my @now = $gmt ? gmtime : localtime;
262 my ($YYYY, $MM, $DD);
263 if (bytes::length($date) == 8) { # RFC 3977 allows YYYYMMDD
264 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
265 } else { # legacy clients send YYMMDD
266 ($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
267 my $cur_year = $now[5] + 1900;
268 if ($YYYY > $cur_year) {
269 $YYYY += int($cur_year / 1000) * 1000 - 100;
273 timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
275 timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
279 sub group_line ($$) {
280 my ($self, $ng) = @_;
281 my ($min, $max) = $ng->mm->minmax;
282 more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
285 sub cmd_newgroups ($$$;$$) {
286 my ($self, $date, $time, $gmt, $dists) = @_;
287 my $ts = eval { parse_time($date, $time, $gmt) };
291 more($self, '231 list of new newsgroups follows');
292 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
293 my $c = eval { $ng->mm->created_at } || 0;
294 next unless $c > $ts;
295 group_line($self, $ng);
300 sub wildmat2re (;$) {
301 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
306 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
308 my $key = sha1_hex($orig . $salt);
309 $orig =~ s/([^\w\-])+/\Q$1/g;
313 my %map = ('*' => '.*', '?' => '.' );
314 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
316 $tmp =~ s#([a-f0-9]{40})#
317 my $orig = $keep{$1};
318 defined $orig ? $orig : $1;
321 $_[0] = qr/\A$tmp\z/;
325 return $_[0] = qr/\A\z/ unless defined $_[0];
326 my %map = ('*' => '.*', ',' => '|');
327 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
328 $_[0] = qr/\A(?:$_[0])\z/;
331 sub cmd_newnews ($$$$;$$) {
332 my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
333 my $ts = eval { parse_time($date, $time, $gmt) };
335 more($self, '230 list of new articles by message-id follows');
336 my ($keep, $skip) = split('!', $newsgroups, 2);
340 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
341 $ng->{newsgroup} =~ $keep or next;
342 $ng->{newsgroup} =~ $skip and next;
343 my $over = $ng->over or next;
346 return '.' unless @over;
349 long_response($self, sub {
351 my $msgs = $over->query_ts($ts, $prev);
354 join(">\r\n<", map { $_->mid } @$msgs ).
356 $prev = $msgs->[-1]->{num};
359 if (@over) { # continue onto next newsgroup
362 } else { # break out of the long response.
370 my ($self, $group) = @_;
371 my $no_such = '411 no such news group';
372 my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
375 my ($min, $max) = $ng->mm->minmax;
378 $self->{article} = $min;
379 my $est_size = $max - $min;
380 "211 $est_size $min $max $group";
383 sub article_adj ($$) {
384 my ($self, $off) = @_;
385 my $ng = $self->{ng} or return '412 no newsgroup selected';
387 my $n = $self->{article};
388 defined $n or return '420 no current article has been selected';
391 my $mid = $ng->mm->mid_for($n);
393 $n = $off > 0 ? 'next' : 'previous';
394 return "421 no $n article in this group";
396 $self->{article} = $n;
397 "223 $n <$mid> article retrieved - request text separately";
400 sub cmd_next ($) { article_adj($_[0], 1) }
401 sub cmd_last ($) { article_adj($_[0], -1) }
403 # We want to encourage using email and CC-ing everybody involved to avoid
404 # the single-point-of-failure a single server provides.
407 my $ng = $self->{ng};
408 $ng ? "440 mailto:$ng->{-primary_address} to post"
409 : '440 posting not allowed'
414 res($self, '205 closing connection - goodbye!');
419 sub header_append ($$$) {
420 my ($hdr, $k, $v) = @_;
421 my @v = $hdr->header($k);
425 $hdr->header_set($k, @v, $v);
429 my ($self, $ng, $n, $mid) = @_;
430 my $ret = $self->{nntpd}->{servername} . " $ng->{newsgroup}:$n";
432 # num_for is pretty cheap and sometimes we'll lookup the existence
433 # of an article without getting even the OVER info. In other words,
434 # I'm not sure if its worth optimizing by scanning To:/Cc: and
435 # PublicInbox::ExtMsg on the PSGI end is just as expensive
436 foreach my $other (@{$self->{nntpd}->{grouplist}}) {
437 next if $ng eq $other;
438 my $num = eval { $other->mm->num_for($mid) } or next;
439 $ret .= " $other->{newsgroup}:$num";
444 sub set_nntp_headers ($$$$$) {
445 my ($self, $hdr, $ng, $n, $mid) = @_;
447 # why? leafnode requires a Path: header for some inexplicable
448 # reason. We'll fake the shortest one possible.
449 $hdr->header_set('Path', 'y');
451 # leafnode (and maybe other NNTP clients) have trouble dealing
452 # with v2 messages which have multiple Message-IDs (either due
453 # to our own content-based dedupe or buggy git-send-email versions).
454 my @mids = $hdr->header('Message-ID');
455 if (scalar(@mids) > 1) {
457 $hdr->header_set('Message-ID', $mid0);
458 my @alt = $hdr->header('X-Alt-Message-ID');
459 my %seen = map { $_ => 1 } (@alt, $mid0);
460 foreach my $m (@mids) {
464 $hdr->header_set('X-Alt-Message-ID', @alt);
468 my $xref = xref($self, $ng, $n, $mid);
469 $hdr->header_set('Xref', $xref);
470 $xref =~ s/:[0-9]+//g;
471 $hdr->header_set('Newsgroups', (split(/ /, $xref, 2))[1]);
472 header_append($hdr, 'List-Post', "<mailto:$ng->{-primary_address}>");
473 if (my $url = $ng->base_url) {
474 $mid = mid_escape($mid);
475 header_append($hdr, 'Archived-At', "<$url$mid/>");
476 header_append($hdr, 'List-Archive', "<$url>");
480 sub art_lookup ($$$) {
481 my ($self, $art, $set_headers) = @_;
482 my $ng = $self->{ng};
486 if ($art =~ /\A[0-9]+\z/) {
487 $err = '423 no such article number in this group';
490 } elsif ($art =~ /\A<([^>]+)>\z/) {
493 $n = $ng->mm->num_for($mid) if $ng;
494 goto found if defined $n;
495 foreach my $g (values %{$self->{nntpd}->{groups}}) {
496 $n = $g->mm->num_for($mid);
507 $err = '420 no current article has been selected';
508 $n = $self->{article};
509 defined $n or return $err;
511 $ng or return '412 no newsgroup has been selected';
512 $mid = $ng->mm->mid_for($n);
513 defined $mid or return $err;
516 my $smsg = $ng->over->get_art($n) or return $err;
517 my $msg = $ng->msg_by_smsg($smsg) or return $err;
519 # Email::Simple->new will modify $msg in-place as documented
520 # in its manpage, so what's left is the body and we won't need
521 # to call Email::Simple::body(), later
522 my $hdr = Email::Simple->new($msg)->header_obj;
523 set_nntp_headers($self, $hdr, $ng, $n, $mid) if $set_headers;
524 [ $n, $mid, $msg, $hdr ];
527 sub msg_body_write ($$) {
528 my ($self, $msg) = @_;
530 # these can momentarily double the memory consumption :<
531 $$msg =~ s/^\./../smg;
532 $$msg =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
533 $$msg .= "\r\n" unless $$msg =~ /\r\n\z/s;
534 $self->msg_more($$msg);
539 my ($self, $art) = @_;
540 $self->{article} = $art if defined $art && $art =~ /\A[0-9]+\z/;
543 sub msg_hdr_write ($$$) {
544 my ($self, $hdr, $body_follows) = @_;
545 $hdr = $hdr->as_string;
547 $hdr =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
549 # for leafnode compatibility, we need to ensure Message-ID headers
550 # are only a single line. We can't subclass Email::Simple::Header
551 # and override _default_fold_at in here, either; since that won't
552 # affect messages already in the archive.
553 $hdr =~ s/^(Message-ID:)[ \t]*\r\n[ \t]+([^\r]+)\r\n/$1 $2\r\n/igsm;
554 $hdr .= "\r\n" if $body_follows;
555 $self->msg_more($hdr);
558 sub cmd_article ($;$) {
559 my ($self, $art) = @_;
560 my $r = art_lookup($self, $art, 1);
561 return $r unless ref $r;
562 my ($n, $mid, $msg, $hdr) = @$r;
563 set_art($self, $art);
564 more($self, "220 $n <$mid> article retrieved - head and body follow");
565 msg_hdr_write($self, $hdr, 1);
566 msg_body_write($self, $msg);
570 my ($self, $art) = @_;
571 my $r = art_lookup($self, $art, 2);
572 return $r unless ref $r;
573 my ($n, $mid, undef, $hdr) = @$r;
574 set_art($self, $art);
575 more($self, "221 $n <$mid> article retrieved - head follows");
576 msg_hdr_write($self, $hdr, 0);
581 my ($self, $art) = @_;
582 my $r = art_lookup($self, $art, 0);
583 return $r unless ref $r;
584 my ($n, $mid, $msg) = @$r;
585 set_art($self, $art);
586 more($self, "222 $n <$mid> article retrieved - body follows");
587 msg_body_write($self, $msg);
591 my ($self, $art) = @_;
592 my $r = art_lookup($self, $art, 0);
593 return $r unless ref $r;
595 set_art($self, $art);
596 "223 $n <$mid> article retrieved - request text separately";
599 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
601 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
605 more($self, '100 help text follows');
610 my ($self, $range) = @_;
611 my $ng = $self->{ng} or return '412 no news group has been selected';
612 defined $range or return '420 No article(s) selected';
614 my ($min, $max) = $ng->mm->minmax;
615 if ($range =~ /\A([0-9]+)\z/) {
617 } elsif ($range =~ /\A([0-9]+)-\z/) {
618 ($beg, $end) = ($1, $max);
619 } elsif ($range =~ /\A([0-9]+)-([0-9]+)\z/) {
620 ($beg, $end) = ($1, $2);
624 $beg = $min if ($beg < $min);
625 $end = $max if ($end > $max);
626 return '420 No article(s) selected' if ($beg > $end);
630 sub long_response ($$) {
631 my ($self, $cb) = @_; # cb returns true if more, false if done
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 my $long_cb; # DANGER: self-referential
641 # wbuf is unset or empty, here; $cb may add to it
642 my $more = eval { $cb->() };
643 if ($@ || !$self->{sock}) { # something bad happened...
645 my $diff = now() - $t0;
648 "%s during long response[$fd] - %0.6f",
651 out($self, " deferred[$fd] aborted - %0.6f", $diff);
653 } elsif ($more) { # $self->{wbuf}:
654 update_idle_time($self);
656 # COMPRESS users all share the same DEFLATE context.
657 # Flush it here to ensure clients don't see
661 # no recursion, schedule another call ASAP
662 # but only after all pending writes are done
663 my $wbuf = $self->{wbuf} ||= [];
664 push @$wbuf, $long_cb;
666 # wbuf may be populated by $cb, no need to rearm if so:
667 $self->requeue if scalar(@$wbuf) == 1;
671 out($self, " deferred[$fd] done - %0.6f", now() - $t0);
672 my $wbuf = $self->{wbuf};
673 $self->requeue unless $wbuf && @$wbuf;
676 $self->write($long_cb); # kick off!
680 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
681 my ($self, $xhdr, $range) = @_;
683 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
684 my ($ng, $n) = mid_lookup($self, $1);
685 return r430 unless $n;
686 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
687 } else { # numeric range
688 $range = $self->{article} unless defined $range;
689 my $r = get_range($self, $range);
690 return $r unless ref $r;
691 my $mm = $self->{ng}->mm;
692 my ($beg, $end) = @$r;
693 more($self, $xhdr ? r221 : r225);
694 long_response($self, sub {
695 my $r = $mm->msg_range(\$beg, $end);
697 more($self, join("\r\n", map {
705 sub mid_lookup ($$) {
706 my ($self, $mid) = @_;
707 my $self_ng = $self->{ng};
709 my $n = $self_ng->mm->num_for($mid);
710 return ($self_ng, $n) if defined $n;
712 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
713 next if defined $self_ng && $ng eq $self_ng;
714 my $n = $ng->mm->num_for($mid);
715 return ($ng, $n) if defined $n;
720 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
721 my ($self, $xhdr, $range) = @_;
723 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
725 my ($ng, $n) = mid_lookup($self, $mid);
726 return r430 unless $n;
727 hdr_mid_response($self, $xhdr, $ng, $n, $range,
728 xref($self, $ng, $n, $mid));
729 } else { # numeric range
730 $range = $self->{article} unless defined $range;
731 my $r = get_range($self, $range);
732 return $r unless ref $r;
733 my $ng = $self->{ng};
735 my ($beg, $end) = @$r;
736 more($self, $xhdr ? r221 : r225);
737 long_response($self, sub {
738 my $r = $mm->msg_range(\$beg, $end);
740 more($self, join("\r\n", map {
742 "$num ".xref($self, $ng, $num, $_->[1]);
749 sub over_header_for {
750 my ($over, $num, $field) = @_;
751 my $smsg = $over->get_art($num) or return;
752 return PublicInbox::SearchMsg::date($smsg) if $field eq 'date';
756 sub hdr_searchmsg ($$$$) {
757 my ($self, $xhdr, $field, $range) = @_;
758 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
759 my ($ng, $n) = mid_lookup($self, $1);
760 return r430 unless defined $n;
761 my $v = over_header_for($ng->over, $n, $field);
762 hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
763 } else { # numeric range
764 $range = $self->{article} unless defined $range;
765 my $over = $self->{ng}->over;
766 my $mm = $self->{ng}->mm;
767 my $r = get_range($self, $range);
768 return $r unless ref $r;
769 my ($beg, $end) = @$r;
770 more($self, $xhdr ? r221 : r225);
772 long_response($self, sub {
773 my $msgs = $over->query_xover($cur, $end);
774 my $nr = scalar @$msgs or return;
776 foreach my $s (@$msgs) {
777 $tmp .= $s->{num} . ' ' . $s->$field . "\r\n";
780 $self->msg_more($tmp);
781 $cur = $msgs->[-1]->{num} + 1;
787 my ($self, $xhdr, $header, $range) = @_;
788 my $sub = lc $header;
789 if ($sub eq 'message-id') {
790 hdr_message_id($self, $xhdr, $range);
791 } elsif ($sub eq 'xref') {
792 hdr_xref($self, $xhdr, $range);
793 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
795 hdr_searchmsg($self, $xhdr, $sub, $range);
796 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
797 hdr_searchmsg($self, $xhdr, $1, $range);
799 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
805 my ($self, $header, $range) = @_;
806 do_hdr($self, 0, $header, $range);
810 sub cmd_xhdr ($$;$) {
811 my ($self, $header, $range) = @_;
812 do_hdr($self, 1, $header, $range);
815 sub hdr_mid_prefix ($$$$$) {
816 my ($self, $xhdr, $ng, $n, $mid) = @_;
817 return $mid if $xhdr;
819 # HDR for RFC 3977 users
820 if (my $self_ng = $self->{ng}) {
821 ($self_ng eq $ng) ? $n : '0';
827 sub hdr_mid_response ($$$$$$) {
828 my ($self, $xhdr, $ng, $n, $mid, $v) = @_;
831 $res .= r221 . "\r\n";
832 $res .= "$mid $v\r\n";
834 $res .= r225 . "\r\n";
835 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
836 $res .= "$pfx $v\r\n";
838 res($self, $res .= '.');
842 sub cmd_xrover ($;$) {
843 my ($self, $range) = @_;
844 my $ng = $self->{ng} or return '412 no newsgroup selected';
845 (defined $range && $range =~ /[<>]/) and
846 return '420 No article(s) selected'; # no message IDs
848 $range = $self->{article} unless defined $range;
849 my $r = get_range($self, $range);
850 return $r unless ref $r;
851 my ($beg, $end) = @$r;
853 my $over = $ng->over;
854 more($self, '224 Overview information follows');
856 long_response($self, sub {
857 my $h = over_header_for($over, $beg, 'references');
858 more($self, "$beg $h") if defined($h);
863 sub over_line ($$$$) {
864 my ($self, $ng, $num, $smsg) = @_;
865 # n.b. field access and procedural calls can be
866 # 10%-15% faster than OO method calls:
867 my $s = join("\t", $num,
870 PublicInbox::SearchMsg::date($smsg),
875 "Xref: " . xref($self, $ng, $num, $smsg->{mid}));
881 my ($self, $range) = @_;
882 if ($range && $range =~ /\A<(.+)>\z/) {
883 my ($ng, $n) = mid_lookup($self, $1);
884 defined $n or return r430;
885 my $smsg = $ng->over->get_art($n) or return r430;
886 more($self, '224 Overview information follows (multi-line)');
888 # Only set article number column if it's the current group
889 my $self_ng = $self->{ng};
890 $n = 0 if (!$self_ng || $self_ng ne $ng);
891 more($self, over_line($self, $ng, $n, $smsg));
894 cmd_xover($self, $range);
898 sub cmd_xover ($;$) {
899 my ($self, $range) = @_;
900 $range = $self->{article} unless defined $range;
901 my $r = get_range($self, $range);
902 return $r unless ref $r;
903 my ($beg, $end) = @$r;
904 more($self, "224 Overview information follows for $beg to $end");
905 my $over = $self->{ng}->over;
907 long_response($self, sub {
908 my $msgs = $over->query_xover($cur, $end);
909 my $nr = scalar @$msgs or return;
912 more($self, join("\r\n", map {
913 over_line($self, $self->{ng}, $_->{num}, $_);
915 $cur = $msgs->[-1]->{num} + 1;
919 sub compressed { undef }
921 sub cmd_starttls ($) {
923 my $sock = $self->{sock} or return;
925 return r502 if (ref($sock) eq 'IO::Socket::SSL' || $self->compressed);
926 my $opt = $self->{nntpd}->{accept_tls} or
927 return '580 can not initiate TLS negotiation';
928 res($self, '382 Continue with TLS negotiation');
929 $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
930 $self->requeue if PublicInbox::DS::accept_tls_step($self);
935 sub cmd_compress ($$) {
936 my ($self, $alg) = @_;
937 return '503 Only DEFLATE is supported' if uc($alg) ne 'DEFLATE';
938 return r502 if $self->compressed || !$have_deflate;
939 PublicInbox::NNTPdeflate->enable($self);
944 sub zflush {} # overridden by NNTPdeflate
947 my ($self, $mid) = @_;
948 return r501 unless $mid =~ /\A<(.+)>\z/;
951 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
952 my $n = $ng->mm->num_for($mid);
953 push @paths, "$ng->{newsgroup}/$n" if defined $n;
955 return '430 no such article on server' unless @paths;
956 '223 '.join(' ', @paths);
959 sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
961 sub more ($$) { $_[0]->msg_more($_[1] . "\r\n") }
965 my $done = $self->write(\($_[1]));
966 return 0 unless $self->{sock};
972 my ($self, $fmt, @args) = @_;
973 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
977 my ($self, $fmt, @args) = @_;
978 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
981 # callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
985 return unless $self->flush_write && $self->{sock};
987 update_idle_time($self);
988 # only read more requests if we've drained the write buffer,
989 # otherwise we can be buffering infinitely w/o backpressure
991 my $rbuf = $self->{rbuf} // (\(my $x = ''));
994 if (index($$rbuf, "\n") < 0) {
995 my $off = bytes::length($$rbuf);
996 $r = $self->do_read($rbuf, LINE_MAX, $off) or return;
998 while ($r > 0 && $$rbuf =~ s/\A[ \t]*([^\n]*?)\r?\n//) {
1000 return $self->close if $line =~ /[[:cntrl:]]/s;
1002 my $fd = fileno($self->{sock});
1003 $r = eval { process_line($self, $line) };
1004 my $pending = $self->{wbuf} ? ' pending' : '';
1005 out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
1008 return $self->close if $r < 0;
1009 my $len = bytes::length($$rbuf);
1010 return $self->close if ($len >= LINE_MAX);
1011 $self->rbuf_idle($rbuf);
1012 update_idle_time($self);
1014 # maybe there's more pipelined data, or we'll have
1015 # to register it for socket-readiness notifications
1016 $self->requeue unless $self->{wbuf};
1019 sub not_idle_long ($$) {
1020 my ($self, $now) = @_;
1021 my $sock = $self->{sock} or return;
1022 my $ary = $EXPMAP->{fileno($sock)} or return;
1023 my $exp_at = $ary->[0] + $EXPTIME;
1027 # for graceful shutdown in PublicInbox::Daemon:
1029 my ($self, $now) = @_;
1030 ($self->{rbuf} || $self->{wbuf} || not_idle_long($self, $now));
1033 # this is an import to prevent "perl -c" from complaining about fields
1035 $have_deflate = eval { require PublicInbox::NNTPdeflate } and
1036 $CAPABILITIES .= "COMPRESS DEFLATE\r\n";