1 # Copyright (C) 2015-2020 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 long_cb);
10 use PublicInbox::MID qw(mid_escape $MID_EXTRACT);
12 use POSIX qw(strftime);
13 use PublicInbox::DS qw(now);
14 use Digest::SHA qw(sha1_hex);
15 use Time::Local qw(timegm timelocal);
17 LINE_MAX => 512, # RFC 977 section 2.3
18 r501 => '501 command syntax error',
19 r502 => '502 Command unavailable',
20 r221 => '221 Header follows',
21 r224 => '224 Overview information follows (multi-line)',
22 r225 => '225 Headers follow (multi-line)',
23 r430 => '430 No article with that message-id',
25 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
27 my $ONE_MSGID = qr/\A$MID_EXTRACT\z/;
28 my @OVERVIEW = qw(Subject From Date Message-ID References);
29 my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines), '') .
31 my $LIST_HEADERS = join("\r\n", @OVERVIEW,
32 qw(:bytes :lines Xref To Cc)) . "\r\n";
33 my $CAPABILITIES = <<"";
34 101 Capability list:\r
38 LIST ACTIVE ACTIVE.TIMES NEWSGROUPS OVERVIEW.FMT\r
44 sub greet ($) { $_[0]->write($_[0]->{nntpd}->{greet}) };
47 my ($class, $sock, $nntpd) = @_;
48 my $self = fields::new($class);
51 if (ref($sock) eq 'IO::Socket::SSL' && !$sock->accept_SSL) {
52 return CORE::close($sock) if $! != EAGAIN;
53 $ev = PublicInbox::TLS::epollbit();
54 $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
56 $self->SUPER::new($sock, $ev | EPOLLONESHOT);
57 $self->{nntpd} = $nntpd;
59 $self->{wbuf} = $wbuf;
63 $self->update_idle_time;
69 my $tot = prototype $cb;
70 my ($nreq, undef) = split(';', $tot);
71 $nreq = ($nreq =~ tr/$//) - 1;
72 $tot = ($tot =~ tr/$//) - 1;
73 ($argc <= $tot && $argc >= $nreq);
76 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
77 sub process_line ($$) {
79 my ($req, @args) = split(/[ \t]+/, $l);
80 return 1 unless defined($req); # skip blank line
81 $req = $self->can('cmd_'.lc($req));
82 return res($self, '500 command not recognized') unless $req;
83 return res($self, r501) unless args_ok($req, scalar @args);
85 my $res = eval { $req->($self, @args) };
87 if ($err && $self->{sock}) {
90 err($self, 'error from: %s (%s)', $l, $err);
91 $res = '503 program fault - command not performed';
93 return 0 unless defined $res;
97 # The keyword argument is not used (rfc3977 5.2.2)
98 sub cmd_capabilities ($;$) {
99 my ($self, undef) = @_;
100 my $res = $CAPABILITIES;
101 if (ref($self->{sock}) ne 'IO::Socket::SSL' &&
102 $self->{nntpd}->{accept_tls}) {
103 $res .= "STARTTLS\r\n";
109 my ($self, $arg) = @_;
111 return r501 unless $arg eq 'READER';
112 '201 Posting prohibited';
115 sub cmd_slave ($) { '202 slave status noted' }
117 sub cmd_xgtitle ($;$) {
118 my ($self, $wildmat) = @_;
119 more($self, '282 list of groups and descriptions follows');
120 list_newsgroups($self, $wildmat);
124 sub list_overview_fmt ($) {
126 $self->msg_more($OVERVIEW_FMT);
129 sub list_headers ($;$) {
131 $self->msg_more($LIST_HEADERS);
134 sub list_active ($;$) {
135 my ($self, $wildmat) = @_;
136 wildmat2re($wildmat);
137 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
138 $ng->{newsgroup} =~ $wildmat or next;
139 group_line($self, $ng);
143 sub list_active_times ($;$) {
144 my ($self, $wildmat) = @_;
145 wildmat2re($wildmat);
146 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
147 $ng->{newsgroup} =~ $wildmat or next;
148 my $c = eval { $ng->mm->created_at } || time;
149 more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
153 sub list_newsgroups ($;$) {
154 my ($self, $wildmat) = @_;
155 wildmat2re($wildmat);
156 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
157 $ng->{newsgroup} =~ $wildmat or next;
158 my $d = $ng->description;
159 more($self, "$ng->{newsgroup} $d");
163 # LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
164 sub cmd_list ($;$$) {
165 my ($self, @args) = @_;
167 my $arg = shift @args;
168 $arg =~ tr/A-Z./a-z_/;
170 $arg = $self->can($arg);
171 return r501 unless $arg && args_ok($arg, scalar @args);
172 more($self, '215 information follows');
173 $arg->($self, @args);
175 more($self, '215 list of newsgroups follows');
176 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
177 group_line($self, $ng);
183 sub listgroup_range_i {
184 my ($self, $beg, $end) = @_;
185 my $r = $self->{ng}->mm->msg_range($beg, $end, 'num');
186 scalar(@$r) or return;
187 more($self, join("\r\n", map { $_->[0] } @$r));
191 sub listgroup_all_i {
192 my ($self, $num) = @_;
193 my $ary = $self->{ng}->mm->ids_after($num);
194 scalar(@$ary) or return;
195 more($self, join("\r\n", @$ary));
199 sub cmd_listgroup ($;$$) {
200 my ($self, $group, $range) = @_;
201 if (defined $group) {
202 my $res = cmd_group($self, $group);
203 return $res if ($res !~ /\A211 /);
206 $self->{ng} or return '412 no newsgroup selected';
207 if (defined $range) {
208 my $r = get_range($self, $range);
209 return $r unless ref $r;
210 long_response($self, \&listgroup_range_i, @$r);
211 } else { # grab every article number
212 long_response($self, \&listgroup_all_i, \(my $num = 0));
216 sub parse_time ($$;$) {
217 my ($date, $time, $gmt) = @_;
218 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
220 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
223 my ($YYYY, $MM, $DD);
224 if (bytes::length($date) == 8) { # RFC 3977 allows YYYYMMDD
225 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
226 } else { # legacy clients send YYMMDD
228 ($YY, $MM, $DD) = unpack('A2A2A2', $date);
229 my @now = $gmt ? gmtime : localtime;
230 my $cur_year = $now[5] + 1900;
231 my $cur_cent = int($cur_year / 100) * 100;
232 $YYYY = (($YY + $cur_cent) > $cur_year) ?
233 ($YY + 1900) : ($YY + $cur_cent);
236 timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
238 timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
242 sub group_line ($$) {
243 my ($self, $ng) = @_;
244 my ($min, $max) = $ng->mm->minmax;
245 more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
248 sub cmd_newgroups ($$$;$$) {
249 my ($self, $date, $time, $gmt, $dists) = @_;
250 my $ts = eval { parse_time($date, $time, $gmt) };
254 more($self, '231 list of new newsgroups follows');
255 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
256 my $c = eval { $ng->mm->created_at } || 0;
257 next unless $c > $ts;
258 group_line($self, $ng);
263 sub wildmat2re (;$) {
264 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
269 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
271 my $key = sha1_hex($orig . $salt);
272 $orig =~ s/([^\w\-])+/\Q$1/g;
276 my %map = ('*' => '.*', '?' => '.' );
277 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
279 $tmp =~ s#([a-f0-9]{40})#
280 my $orig = $keep{$1};
281 defined $orig ? $orig : $1;
284 $_[0] = qr/\A$tmp\z/;
288 return $_[0] = qr/\A\z/ unless defined $_[0];
289 my %map = ('*' => '.*', ',' => '|');
290 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
291 $_[0] = qr/\A(?:$_[0])\z/;
295 my ($self, $overs, $ts, $prev) = @_;
296 my $over = $overs->[0];
297 my $msgs = $over->query_ts($ts, $$prev);
300 join(">\r\n<", map { $_->mid } @$msgs ).
302 $$prev = $msgs->[-1]->{num};
305 if (@$overs) { # continue onto next newsgroup
308 } else { # break out of the long response.
314 sub cmd_newnews ($$$$;$$) {
315 my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
316 my $ts = eval { parse_time($date, $time, $gmt) };
318 more($self, '230 list of new articles by message-id follows');
319 my ($keep, $skip) = split('!', $newsgroups, 2);
323 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
324 $ng->{newsgroup} =~ $keep or next;
325 $ng->{newsgroup} =~ $skip and next;
326 my $over = $ng->over or next;
329 return '.' unless @overs;
332 long_response($self, \&newnews_i, \@overs, $ts, \$prev);
336 my ($self, $group) = @_;
337 my $no_such = '411 no such news group';
338 my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
341 my ($min, $max) = $ng->mm->minmax;
344 $self->{article} = $min;
345 my $est_size = $max - $min;
346 "211 $est_size $min $max $group";
349 sub article_adj ($$) {
350 my ($self, $off) = @_;
351 my $ng = $self->{ng} or return '412 no newsgroup selected';
353 my $n = $self->{article};
354 defined $n or return '420 no current article has been selected';
357 my $mid = $ng->mm->mid_for($n);
359 $n = $off > 0 ? 'next' : 'previous';
360 return "421 no $n article in this group";
362 $self->{article} = $n;
363 "223 $n <$mid> article retrieved - request text separately";
366 sub cmd_next ($) { article_adj($_[0], 1) }
367 sub cmd_last ($) { article_adj($_[0], -1) }
369 # We want to encourage using email and CC-ing everybody involved to avoid
370 # the single-point-of-failure a single server provides.
373 my $ng = $self->{ng};
374 $ng ? "440 mailto:$ng->{-primary_address} to post"
375 : '440 posting not allowed'
380 res($self, '205 closing connection - goodbye!');
385 sub header_append ($$$) {
386 my ($hdr, $k, $v) = @_;
387 my @v = $hdr->header($k);
391 $hdr->header_set($k, @v, $v);
395 my ($self, $ng, $n, $mid) = @_;
396 my $ret = $self->{nntpd}->{servername} . " $ng->{newsgroup}:$n";
398 # num_for is pretty cheap and sometimes we'll lookup the existence
399 # of an article without getting even the OVER info. In other words,
400 # I'm not sure if its worth optimizing by scanning To:/Cc: and
401 # PublicInbox::ExtMsg on the PSGI end is just as expensive
402 foreach my $other (@{$self->{nntpd}->{grouplist}}) {
403 next if $ng eq $other;
404 my $num = eval { $other->mm->num_for($mid) } or next;
405 $ret .= " $other->{newsgroup}:$num";
410 sub set_nntp_headers ($$$$$) {
411 my ($self, $hdr, $ng, $n, $mid) = @_;
413 # why? leafnode requires a Path: header for some inexplicable
414 # reason. We'll fake the shortest one possible.
415 $hdr->header_set('Path', 'y');
417 # leafnode (and maybe other NNTP clients) have trouble dealing
418 # with v2 messages which have multiple Message-IDs (either due
419 # to our own content-based dedupe or buggy git-send-email versions).
420 my @mids = $hdr->header('Message-ID');
421 if (scalar(@mids) > 1) {
423 $hdr->header_set('Message-ID', $mid0);
424 my @alt = $hdr->header('X-Alt-Message-ID');
425 my %seen = map { $_ => 1 } (@alt, $mid0);
426 push(@alt, grep { !$seen{$_}++ } @mids);
427 $hdr->header_set('X-Alt-Message-ID', @alt);
431 my $xref = xref($self, $ng, $n, $mid);
432 $hdr->header_set('Xref', $xref);
433 $xref =~ s/:[0-9]+//g;
434 $hdr->header_set('Newsgroups', (split(/ /, $xref, 2))[1]);
435 header_append($hdr, 'List-Post', "<mailto:$ng->{-primary_address}>");
436 if (my $url = $ng->base_url) {
437 $mid = mid_escape($mid);
438 header_append($hdr, 'Archived-At', "<$url$mid/>");
439 header_append($hdr, 'List-Archive', "<$url>");
443 sub art_lookup ($$$) {
444 my ($self, $art, $set_headers) = @_;
445 my $ng = $self->{ng};
449 if ($art =~ /\A[0-9]+\z/) {
450 $err = '423 no such article number in this group';
453 } elsif ($art =~ $ONE_MSGID) {
456 $n = $ng->mm->num_for($mid) if $ng;
457 goto found if defined $n;
458 foreach my $g (values %{$self->{nntpd}->{groups}}) {
459 $n = $g->mm->num_for($mid);
470 $err = '420 no current article has been selected';
471 $n = $self->{article};
472 defined $n or return $err;
474 $ng or return '412 no newsgroup has been selected';
475 $mid = $ng->mm->mid_for($n);
476 defined $mid or return $err;
479 my $smsg = $ng->over->get_art($n) or return $err;
480 my $msg = $ng->msg_by_smsg($smsg) or return $err;
482 # Email::Simple->new will modify $msg in-place as documented
483 # in its manpage, so what's left is the body and we won't need
484 # to call Email::Simple::body(), later
485 my $hdr = Email::Simple->new($msg)->header_obj;
486 set_nntp_headers($self, $hdr, $ng, $n, $mid) if $set_headers;
487 [ $n, $mid, $msg, $hdr ];
490 sub msg_body_write ($$) {
491 my ($self, $msg) = @_;
493 # these can momentarily double the memory consumption :<
494 $$msg =~ s/^\./../smg;
495 $$msg =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
496 $$msg .= "\r\n" unless $$msg =~ /\r\n\z/s;
497 $self->msg_more($$msg);
502 my ($self, $art) = @_;
503 $self->{article} = $art if defined $art && $art =~ /\A[0-9]+\z/;
506 sub msg_hdr_write ($$$) {
507 my ($self, $hdr, $body_follows) = @_;
508 $hdr = $hdr->as_string;
510 $hdr =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
512 # for leafnode compatibility, we need to ensure Message-ID headers
513 # are only a single line. We can't subclass Email::Simple::Header
514 # and override _default_fold_at in here, either; since that won't
515 # affect messages already in the archive.
516 $hdr =~ s/^(Message-ID:)[ \t]*\r\n[ \t]+([^\r]+)\r\n/$1 $2\r\n/igsm;
517 $hdr .= "\r\n" if $body_follows;
518 $self->msg_more($hdr);
521 sub cmd_article ($;$) {
522 my ($self, $art) = @_;
523 my $r = art_lookup($self, $art, 1);
524 return $r unless ref $r;
525 my ($n, $mid, $msg, $hdr) = @$r;
526 set_art($self, $art);
527 more($self, "220 $n <$mid> article retrieved - head and body follow");
528 msg_hdr_write($self, $hdr, 1);
529 msg_body_write($self, $msg);
533 my ($self, $art) = @_;
534 my $r = art_lookup($self, $art, 2);
535 return $r unless ref $r;
536 my ($n, $mid, undef, $hdr) = @$r;
537 set_art($self, $art);
538 more($self, "221 $n <$mid> article retrieved - head follows");
539 msg_hdr_write($self, $hdr, 0);
544 my ($self, $art) = @_;
545 my $r = art_lookup($self, $art, 0);
546 return $r unless ref $r;
547 my ($n, $mid, $msg) = @$r;
548 set_art($self, $art);
549 more($self, "222 $n <$mid> article retrieved - body follows");
550 msg_body_write($self, $msg);
554 my ($self, $art) = @_;
555 my $r = art_lookup($self, $art, 0);
556 return $r unless ref $r;
558 set_art($self, $art);
559 "223 $n <$mid> article retrieved - request text separately";
562 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
564 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
568 more($self, '100 help text follows');
573 my ($self, $range) = @_;
574 my $ng = $self->{ng} or return '412 no news group has been selected';
575 defined $range or return '420 No article(s) selected';
577 my ($min, $max) = $ng->mm->minmax;
578 if ($range =~ /\A([0-9]+)\z/) {
580 } elsif ($range =~ /\A([0-9]+)-\z/) {
581 ($beg, $end) = ($1, $max);
582 } elsif ($range =~ /\A([0-9]+)-([0-9]+)\z/) {
583 ($beg, $end) = ($1, $2);
587 $beg = $min if ($beg < $min);
588 $end = $max if ($end > $max);
589 return '420 No article(s) selected' if ($beg > $end);
595 # wbuf is unset or empty, here; {long} may add to it
596 my ($fd, $cb, $t0, @args) = @{$self->{long_cb}};
597 my $more = eval { $cb->($self, @args) };
598 if ($@ || !$self->{sock}) { # something bad happened...
599 delete $self->{long_cb};
600 my $elapsed = now() - $t0;
603 "%s during long response[$fd] - %0.6f",
606 out($self, " deferred[$fd] aborted - %0.6f", $elapsed);
608 } elsif ($more) { # $self->{wbuf}:
609 $self->update_idle_time;
611 # COMPRESS users all share the same DEFLATE context.
612 # Flush it here to ensure clients don't see
616 # no recursion, schedule another call ASAP, but only after
617 # all pending writes are done. autovivify wbuf:
618 my $new_size = push(@{$self->{wbuf}}, \&long_step);
620 # wbuf may be populated by $cb, no need to rearm if so:
621 $self->requeue if $new_size == 1;
623 delete $self->{long_cb};
625 my $elapsed = now() - $t0;
626 my $fd = fileno($self->{sock});
627 out($self, " deferred[$fd] done - %0.6f", $elapsed);
628 my $wbuf = $self->{wbuf}; # do NOT autovivify
629 $self->requeue unless $wbuf && @$wbuf;
633 sub long_response ($$;@) {
634 my ($self, $cb, @args) = @_; # cb returns true if more, false if done
636 my $sock = $self->{sock} or return;
637 # make sure we disable reading during a long response,
638 # clients should not be sending us stuff and making us do more
639 # work while we are stream a response to them
640 $self->{long_cb} = [ fileno($sock), $cb, now(), @args ];
641 long_step($self); # kick off!
645 sub hdr_msgid_range_i {
646 my ($self, $beg, $end) = @_;
647 my $r = $self->{ng}->mm->msg_range($beg, $end);
649 more($self, join("\r\n", map { "$_->[0] <$_->[1]>" } @$r));
653 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
654 my ($self, $xhdr, $range) = @_;
656 if (defined $range && $range =~ $ONE_MSGID) {
657 my ($ng, $n) = mid_lookup($self, $1);
658 return r430 unless $n;
659 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
660 } else { # numeric range
661 $range = $self->{article} unless defined $range;
662 my $r = get_range($self, $range);
663 return $r unless ref $r;
664 more($self, $xhdr ? r221 : r225);
665 long_response($self, \&hdr_msgid_range_i, @$r);
669 sub mid_lookup ($$) {
670 my ($self, $mid) = @_;
671 my $self_ng = $self->{ng};
673 my $n = $self_ng->mm->num_for($mid);
674 return ($self_ng, $n) if defined $n;
676 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
677 next if defined $self_ng && $ng eq $self_ng;
678 my $n = $ng->mm->num_for($mid);
679 return ($ng, $n) if defined $n;
685 my ($self, $beg, $end) = @_;
686 my $ng = $self->{ng};
687 my $r = $ng->mm->msg_range($beg, $end);
689 more($self, join("\r\n", map {
691 "$num ".xref($self, $ng, $num, $_->[1]);
696 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
697 my ($self, $xhdr, $range) = @_;
699 if (defined $range && $range =~ $ONE_MSGID) {
701 my ($ng, $n) = mid_lookup($self, $mid);
702 return r430 unless $n;
703 hdr_mid_response($self, $xhdr, $ng, $n, $range,
704 xref($self, $ng, $n, $mid));
705 } else { # numeric range
706 $range = $self->{article} unless defined $range;
707 my $r = get_range($self, $range);
708 return $r unless ref $r;
709 more($self, $xhdr ? r221 : r225);
710 long_response($self, \&xref_range_i, @$r);
714 sub over_header_for {
715 my ($over, $num, $field) = @_;
716 my $smsg = $over->get_art($num) or return;
717 return PublicInbox::Smsg::date($smsg) if $field eq 'date';
722 my ($self, $beg, $end, $field) = @_;
723 my $over = $self->{ng}->over;
724 my $msgs = $over->query_xover($$beg, $end);
725 scalar(@$msgs) or return;
727 foreach my $s (@$msgs) {
728 $tmp .= $s->{num} . ' ' . $s->$field . "\r\n";
731 $self->msg_more($tmp);
732 $$beg = $msgs->[-1]->{num} + 1;
735 sub hdr_smsg ($$$$) {
736 my ($self, $xhdr, $field, $range) = @_;
737 if (defined $range && $range =~ $ONE_MSGID) {
738 my ($ng, $n) = mid_lookup($self, $1);
739 return r430 unless defined $n;
740 my $v = over_header_for($ng->over, $n, $field);
741 hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
742 } else { # numeric range
743 $range = $self->{article} unless defined $range;
744 my $r = get_range($self, $range);
745 return $r unless ref $r;
746 more($self, $xhdr ? r221 : r225);
747 long_response($self, \&smsg_range_i, @$r, $field);
752 my ($self, $xhdr, $header, $range) = @_;
753 my $sub = lc $header;
754 if ($sub eq 'message-id') {
755 hdr_message_id($self, $xhdr, $range);
756 } elsif ($sub eq 'xref') {
757 hdr_xref($self, $xhdr, $range);
758 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
760 hdr_smsg($self, $xhdr, $sub, $range);
761 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
762 hdr_smsg($self, $xhdr, $1, $range);
764 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
770 my ($self, $header, $range) = @_;
771 do_hdr($self, 0, $header, $range);
775 sub cmd_xhdr ($$;$) {
776 my ($self, $header, $range) = @_;
777 do_hdr($self, 1, $header, $range);
780 sub hdr_mid_prefix ($$$$$) {
781 my ($self, $xhdr, $ng, $n, $mid) = @_;
782 return $mid if $xhdr;
784 # HDR for RFC 3977 users
785 if (my $self_ng = $self->{ng}) {
786 ($self_ng eq $ng) ? $n : '0';
792 sub hdr_mid_response ($$$$$$) {
793 my ($self, $xhdr, $ng, $n, $mid, $v) = @_;
796 $res .= r221 . "\r\n";
797 $res .= "$mid $v\r\n";
799 $res .= r225 . "\r\n";
800 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
801 $res .= "$pfx $v\r\n";
803 res($self, $res .= '.');
808 my ($self, $beg, $end) = @_;
809 my $h = over_header_for($self->{ng}->over, $$beg, 'references');
810 more($self, "$$beg $h") if defined($h);
814 sub cmd_xrover ($;$) {
815 my ($self, $range) = @_;
816 my $ng = $self->{ng} or return '412 no newsgroup selected';
817 (defined $range && $range =~ /[<>]/) and
818 return '420 No article(s) selected'; # no message IDs
820 $range = $self->{article} unless defined $range;
821 my $r = get_range($self, $range);
822 return $r unless ref $r;
823 more($self, '224 Overview information follows');
824 long_response($self, \&xrover_i, @$r);
827 sub over_line ($$$$) {
828 my ($self, $ng, $num, $smsg) = @_;
829 # n.b. field access and procedural calls can be
830 # 10%-15% faster than OO method calls:
831 my $s = join("\t", $num,
834 PublicInbox::Smsg::date($smsg),
839 "Xref: " . xref($self, $ng, $num, $smsg->{mid}));
845 my ($self, $range) = @_;
846 if ($range && $range =~ $ONE_MSGID) {
847 my ($ng, $n) = mid_lookup($self, $1);
848 defined $n or return r430;
849 my $smsg = $ng->over->get_art($n) or return r430;
850 more($self, '224 Overview information follows (multi-line)');
852 # Only set article number column if it's the current group
853 my $self_ng = $self->{ng};
854 $n = 0 if (!$self_ng || $self_ng ne $ng);
855 more($self, over_line($self, $ng, $n, $smsg));
858 cmd_xover($self, $range);
863 my ($self, $beg, $end) = @_;
864 my $ng = $self->{ng};
865 my $msgs = $ng->over->query_xover($$beg, $end);
866 my $nr = scalar @$msgs or return;
869 more($self, join("\r\n", map {
870 over_line($self, $ng, $_->{num}, $_);
872 $$beg = $msgs->[-1]->{num} + 1;
875 sub cmd_xover ($;$) {
876 my ($self, $range) = @_;
877 $range = $self->{article} unless defined $range;
878 my $r = get_range($self, $range);
879 return $r unless ref $r;
880 my ($beg, $end) = @$r;
881 more($self, "224 Overview information follows for $$beg to $end");
882 long_response($self, \&xover_i, @$r);
885 sub compressed { undef }
887 sub cmd_starttls ($) {
889 my $sock = $self->{sock} or return;
891 return r502 if (ref($sock) eq 'IO::Socket::SSL' || $self->compressed);
892 my $opt = $self->{nntpd}->{accept_tls} or
893 return '580 can not initiate TLS negotiation';
894 res($self, '382 Continue with TLS negotiation');
895 $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
896 $self->requeue if PublicInbox::DS::accept_tls_step($self);
901 sub cmd_compress ($$) {
902 my ($self, $alg) = @_;
903 return '503 Only DEFLATE is supported' if uc($alg) ne 'DEFLATE';
904 return r502 if $self->compressed || !$have_deflate;
905 PublicInbox::NNTPdeflate->enable($self);
910 sub zflush {} # overridden by NNTPdeflate
913 my ($self, $mid) = @_;
914 return r501 unless $mid =~ $ONE_MSGID;
917 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
918 my $n = $ng->mm->num_for($mid);
919 push @paths, "$ng->{newsgroup}/$n" if defined $n;
921 return '430 no such article on server' unless @paths;
922 '223 '.join(' ', @paths);
925 sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
927 sub more ($$) { $_[0]->msg_more($_[1] . "\r\n") }
931 my $done = $self->write(\($_[1]));
932 return 0 unless $self->{sock};
938 my ($self, $fmt, @args) = @_;
939 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
943 my ($self, $fmt, @args) = @_;
944 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
947 # callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
951 return unless $self->flush_write && $self->{sock};
953 $self->update_idle_time;
954 # only read more requests if we've drained the write buffer,
955 # otherwise we can be buffering infinitely w/o backpressure
957 my $rbuf = $self->{rbuf} // (\(my $x = ''));
960 if (index($$rbuf, "\n") < 0) {
961 my $off = bytes::length($$rbuf);
962 $r = $self->do_read($rbuf, LINE_MAX, $off) or return;
964 while ($r > 0 && $$rbuf =~ s/\A[ \t]*([^\n]*?)\r?\n//) {
966 return $self->close if $line =~ /[[:cntrl:]]/s;
968 my $fd = fileno($self->{sock});
969 $r = eval { process_line($self, $line) };
970 my $pending = $self->{wbuf} ? ' pending' : '';
971 out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
974 return $self->close if $r < 0;
975 my $len = bytes::length($$rbuf);
976 return $self->close if ($len >= LINE_MAX);
977 $self->rbuf_idle($rbuf);
978 $self->update_idle_time;
980 # maybe there's more pipelined data, or we'll have
981 # to register it for socket-readiness notifications
982 $self->requeue unless $self->{wbuf};
985 # for graceful shutdown in PublicInbox::Daemon:
987 my ($self, $now) = @_;
988 ($self->{rbuf} || $self->{wbuf} || $self->not_idle_long($now));
991 # this is an import to prevent "perl -c" from complaining about fields
993 $have_deflate = eval { require PublicInbox::NNTPdeflate } and
994 $CAPABILITIES .= "COMPRESS DEFLATE\r\n";