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
6 # nntpd: PublicInbox::NNTPD ref
7 # article: per-session current article number
8 # ng: PublicInbox::Inbox ref
9 # long_cb: long_response private data
10 package PublicInbox::NNTP;
12 use parent qw(PublicInbox::DS);
13 use PublicInbox::MID qw(mid_escape $MID_EXTRACT);
15 use POSIX qw(strftime);
16 use PublicInbox::DS qw(now);
17 use Digest::SHA qw(sha1_hex);
18 use Time::Local qw(timegm timelocal);
19 use PublicInbox::GitAsyncCat;
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);
31 my $ONE_MSGID = qr/\A$MID_EXTRACT\z/;
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 sub greet ($) { $_[0]->write($_[0]->{nntpd}->{greet}) };
50 my ($class, $sock, $nntpd) = @_;
51 my $self = bless { nntpd => $nntpd }, $class;
54 if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
55 return CORE::close($sock) if $! != EAGAIN;
56 $ev = PublicInbox::TLS::epollbit();
57 $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
59 $self->SUPER::new($sock, $ev | EPOLLONESHOT);
61 $self->{wbuf} = $wbuf;
65 $self->update_idle_time;
71 my $tot = prototype $cb;
72 my ($nreq, undef) = split(';', $tot);
73 $nreq = ($nreq =~ tr/$//) - 1;
74 $tot = ($tot =~ tr/$//) - 1;
75 ($argc <= $tot && $argc >= $nreq);
78 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
79 sub process_line ($$) {
81 my ($req, @args) = split(/[ \t]+/, $l);
82 return 1 unless defined($req); # skip blank line
83 $req = $self->can('cmd_'.lc($req));
84 return res($self, '500 command not recognized') unless $req;
85 return res($self, r501) unless args_ok($req, scalar @args);
87 my $res = eval { $req->($self, @args) };
89 if ($err && $self->{sock}) {
92 err($self, 'error from: %s (%s)', $l, $err);
93 $res = '503 program fault - command not performed';
95 return 0 unless defined $res;
99 # The keyword argument is not used (rfc3977 5.2.2)
100 sub cmd_capabilities ($;$) {
101 my ($self, undef) = @_;
102 my $res = $CAPABILITIES;
103 if (!$self->{sock}->can('accept_SSL') &&
104 $self->{nntpd}->{accept_tls}) {
105 $res .= "STARTTLS\r\n";
111 my ($self, $arg) = @_;
113 return r501 unless $arg eq 'READER';
114 '201 Posting prohibited';
117 sub cmd_slave ($) { '202 slave status noted' }
119 sub cmd_xgtitle ($;$) {
120 my ($self, $wildmat) = @_;
121 more($self, '282 list of groups and descriptions follows');
122 list_newsgroups($self, $wildmat);
126 sub list_overview_fmt ($) {
128 $self->msg_more($OVERVIEW_FMT);
131 sub list_headers ($;$) {
133 $self->msg_more($LIST_HEADERS);
136 sub list_active ($;$) {
137 my ($self, $wildmat) = @_;
138 wildmat2re($wildmat);
139 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
140 $ng->{newsgroup} =~ $wildmat or next;
141 group_line($self, $ng);
145 sub list_active_times ($;$) {
146 my ($self, $wildmat) = @_;
147 wildmat2re($wildmat);
148 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
149 $ng->{newsgroup} =~ $wildmat or next;
150 my $c = eval { $ng->mm->created_at } || time;
151 more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
155 sub list_newsgroups ($;$) {
156 my ($self, $wildmat) = @_;
157 wildmat2re($wildmat);
158 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
159 $ng->{newsgroup} =~ $wildmat or next;
160 my $d = $ng->description;
161 more($self, "$ng->{newsgroup} $d");
165 # LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
166 sub cmd_list ($;$$) {
167 my ($self, @args) = @_;
169 my $arg = shift @args;
170 $arg =~ tr/A-Z./a-z_/;
172 $arg = $self->can($arg);
173 return r501 unless $arg && args_ok($arg, scalar @args);
174 more($self, '215 information follows');
175 $arg->($self, @args);
177 more($self, '215 list of newsgroups follows');
178 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
179 group_line($self, $ng);
185 sub listgroup_range_i {
186 my ($self, $beg, $end) = @_;
187 my $r = $self->{ng}->mm->msg_range($beg, $end, 'num');
188 scalar(@$r) or return;
189 more($self, join("\r\n", map { $_->[0] } @$r));
193 sub listgroup_all_i {
194 my ($self, $num) = @_;
195 my $ary = $self->{ng}->mm->ids_after($num);
196 scalar(@$ary) or return;
197 more($self, join("\r\n", @$ary));
201 sub cmd_listgroup ($;$$) {
202 my ($self, $group, $range) = @_;
203 if (defined $group) {
204 my $res = cmd_group($self, $group);
205 return $res if ($res !~ /\A211 /);
208 $self->{ng} or return '412 no newsgroup selected';
209 if (defined $range) {
210 my $r = get_range($self, $range);
211 return $r unless ref $r;
212 long_response($self, \&listgroup_range_i, @$r);
213 } else { # grab every article number
214 long_response($self, \&listgroup_all_i, \(my $num = 0));
218 sub parse_time ($$;$) {
219 my ($date, $time, $gmt) = @_;
220 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
222 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
225 my ($YYYY, $MM, $DD);
226 if (bytes::length($date) == 8) { # RFC 3977 allows YYYYMMDD
227 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
228 } else { # legacy clients send YYMMDD
230 ($YY, $MM, $DD) = unpack('A2A2A2', $date);
231 my @now = $gmt ? gmtime : localtime;
232 my $cur_year = $now[5] + 1900;
233 my $cur_cent = int($cur_year / 100) * 100;
234 $YYYY = (($YY + $cur_cent) > $cur_year) ?
235 ($YY + 1900) : ($YY + $cur_cent);
238 timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
240 timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
244 sub group_line ($$) {
245 my ($self, $ng) = @_;
246 my ($min, $max) = $ng->mm->minmax;
247 more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
250 sub cmd_newgroups ($$$;$$) {
251 my ($self, $date, $time, $gmt, $dists) = @_;
252 my $ts = eval { parse_time($date, $time, $gmt) };
256 more($self, '231 list of new newsgroups follows');
257 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
258 my $c = eval { $ng->mm->created_at } || 0;
259 next unless $c > $ts;
260 group_line($self, $ng);
265 sub wildmat2re (;$) {
266 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
271 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
273 my $key = sha1_hex($orig . $salt);
274 $orig =~ s/([^\w\-])+/\Q$1/g;
278 my %map = ('*' => '.*', '?' => '.' );
279 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
281 $tmp =~ s#([a-f0-9]{40})#
282 my $orig = $keep{$1};
283 defined $orig ? $orig : $1;
286 $_[0] = qr/\A$tmp\z/;
290 return $_[0] = qr/\A\z/ unless defined $_[0];
291 my %map = ('*' => '.*', ',' => '|');
292 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
293 $_[0] = qr/\A(?:$_[0])\z/;
297 my ($self, $overs, $ts, $prev) = @_;
298 my $over = $overs->[0];
299 my $msgs = $over->query_ts($ts, $$prev);
302 join(">\r\n<", map { $_->{mid} } @$msgs ).
304 $$prev = $msgs->[-1]->{num};
307 if (@$overs) { # continue onto next newsgroup
310 } else { # break out of the long response.
316 sub cmd_newnews ($$$$;$$) {
317 my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
318 my $ts = eval { parse_time($date, $time, $gmt) };
320 more($self, '230 list of new articles by message-id follows');
321 my ($keep, $skip) = split('!', $newsgroups, 2);
325 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
326 $ng->{newsgroup} =~ $keep or next;
327 $ng->{newsgroup} =~ $skip and next;
328 my $over = $ng->over or next;
331 return '.' unless @overs;
334 long_response($self, \&newnews_i, \@overs, $ts, \$prev);
338 my ($self, $group) = @_;
339 my $no_such = '411 no such news group';
340 my $nntpd = $self->{nntpd};
341 my $ng = $nntpd->{groups}->{$group} or return $no_such;
345 my ($min, $max) = $ng->mm->minmax;
348 $self->{article} = $min;
349 my $est_size = $max - $min;
350 "211 $est_size $min $max $group";
353 sub article_adj ($$) {
354 my ($self, $off) = @_;
355 my $ng = $self->{ng} or return '412 no newsgroup selected';
357 my $n = $self->{article};
358 defined $n or return '420 no current article has been selected';
361 my $mid = $ng->mm->mid_for($n);
363 $n = $off > 0 ? 'next' : 'previous';
364 return "421 no $n article in this group";
366 $self->{article} = $n;
367 "223 $n <$mid> article retrieved - request text separately";
370 sub cmd_next ($) { article_adj($_[0], 1) }
371 sub cmd_last ($) { article_adj($_[0], -1) }
373 # We want to encourage using email and CC-ing everybody involved to avoid
374 # the single-point-of-failure a single server provides.
377 my $ng = $self->{ng};
378 $ng ? "440 mailto:$ng->{-primary_address} to post"
379 : '440 posting not allowed'
384 res($self, '205 closing connection - goodbye!');
389 sub header_append ($$$) {
390 my ($hdr, $k, $v) = @_;
391 my @v = $hdr->header_raw($k);
395 $hdr->header_set($k, @v, $v);
399 my ($self, $ng, $n, $mid) = @_;
400 my $ret = $self->{nntpd}->{servername} . " $ng->{newsgroup}:$n";
402 # num_for is pretty cheap and sometimes we'll lookup the existence
403 # of an article without getting even the OVER info. In other words,
404 # I'm not sure if its worth optimizing by scanning To:/Cc: and
405 # PublicInbox::ExtMsg on the PSGI end is just as expensive
406 foreach my $other (@{$self->{nntpd}->{grouplist}}) {
407 next if $ng eq $other;
408 my $num = eval { $other->mm->num_for($mid) } or next;
409 $ret .= " $other->{newsgroup}:$num";
414 sub set_nntp_headers ($$$) {
415 my ($self, $hdr, $smsg) = @_;
416 my ($mid) = $smsg->{mid};
418 # why? leafnode requires a Path: header for some inexplicable
419 # reason. We'll fake the shortest one possible.
420 $hdr->header_set('Path', 'y');
422 # leafnode (and maybe other NNTP clients) have trouble dealing
423 # with v2 messages which have multiple Message-IDs (either due
424 # to our own content-based dedupe or buggy git-send-email versions).
425 my @mids = $hdr->header_raw('Message-ID');
426 if (scalar(@mids) > 1) {
428 $hdr->header_set('Message-ID', $mid0);
429 my @alt = $hdr->header_raw('X-Alt-Message-ID');
430 my %seen = map { $_ => 1 } (@alt, $mid0);
431 push(@alt, grep { !$seen{$_}++ } @mids);
432 $hdr->header_set('X-Alt-Message-ID', @alt);
436 my $ng = $self->{ng};
437 my $xref = xref($self, $ng, $smsg->{num}, $mid);
438 $hdr->header_set('Xref', $xref);
439 $xref =~ s/:[0-9]+//g;
440 $hdr->header_set('Newsgroups', (split(/ /, $xref, 2))[1]);
441 header_append($hdr, 'List-Post', "<mailto:$ng->{-primary_address}>");
442 if (my $url = $ng->base_url) {
443 $mid = mid_escape($mid);
444 header_append($hdr, 'Archived-At', "<$url$mid/>");
445 header_append($hdr, 'List-Archive', "<$url>");
449 sub art_lookup ($$) {
450 my ($self, $art) = @_;
451 my $ng = $self->{ng};
455 if ($art =~ /\A[0-9]+\z/) {
456 $err = '423 no such article number in this group';
459 } elsif ($art =~ $ONE_MSGID) {
462 $n = $ng->mm->num_for($mid) if $ng;
463 goto found if defined $n;
464 foreach my $g (values %{$self->{nntpd}->{groups}}) {
465 $n = $g->mm->num_for($mid);
476 $err = '420 no current article has been selected';
477 $n = $self->{article};
478 defined $n or return $err;
480 $ng or return '412 no newsgroup has been selected';
481 $mid = $ng->mm->mid_for($n);
482 defined $mid or return $err;
485 my $smsg = $ng->over->get_art($n) or return $err;
489 sub msg_body_write ($$) {
490 my ($self, $msg) = @_;
492 # these can momentarily double the memory consumption :<
493 $$msg =~ s/^\./../smg;
494 $$msg =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
495 $$msg .= "\r\n" unless $$msg =~ /\r\n\z/s;
496 $self->msg_more($$msg);
500 my ($self, $art) = @_;
501 $self->{article} = $art if defined $art && $art =~ /\A[0-9]+\z/;
504 sub msg_hdr_write ($$$) {
505 my ($self, $eml, $smsg) = @_;
506 set_nntp_headers($self, $eml, $smsg);
508 my $hdr = $eml->{hdr} // \(my $x = '');
509 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
510 $$hdr =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
511 $$hdr =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
513 # for leafnode compatibility, we need to ensure Message-ID headers
514 # are only a single line.
515 $$hdr =~ s/^(Message-ID:)[ \t]*\r\n[ \t]+([^\r]+)\r\n/$1 $2\r\n/igsm;
516 $self->msg_more($$hdr);
519 sub blob_cb { # called by git->cat_async via git_async_cat
520 my ($bref, $oid, $type, $size, $smsg) = @_;
521 my $self = $smsg->{nntp};
522 my $code = $smsg->{nntp_code} // 220;
523 if (!defined($oid)) {
524 # it's possible to have TOCTOU if an admin runs
525 # public-inbox-(edit|purge), just move onto the next message
526 return $self->requeue;
527 } elsif ($smsg->{blob} ne $oid) {
529 die "BUG: $smsg->{blob} != $oid";
531 my $r = "$code $smsg->{num} <$smsg->{mid}> article retrieved - ";
532 my $eml = PublicInbox::Eml->new($bref);
534 more($self, $r .= 'head and body follow');
535 msg_hdr_write($self, $eml, $smsg);
536 $self->msg_more("\r\n");
537 msg_body_write($self, $bref);
538 } elsif ($code == 221) {
539 more($self, $r .= 'head follows');
540 msg_hdr_write($self, $eml, $smsg);
541 } elsif ($code == 222) {
542 more($self, $r .= 'body follows');
543 msg_body_write($self, $bref);
546 die "BUG: bad code: $r";
548 $self->write(\".\r\n"); # flushes (includes ->zflush)
552 sub cmd_article ($;$) {
553 my ($self, $art) = @_;
554 my $smsg = art_lookup($self, $art);
555 return $smsg unless ref $smsg;
556 set_art($self, $art);
557 $smsg->{nntp} = $self;
558 ${git_async_cat($self->{ng}->git, $smsg->{blob}, \&blob_cb, $smsg)};
562 my ($self, $art) = @_;
563 my $smsg = art_lookup($self, $art);
564 return $smsg unless ref $smsg;
565 set_art($self, $art);
566 $smsg->{nntp} = $self;
567 $smsg->{nntp_code} = 221;
568 ${git_async_cat($self->{ng}->git, $smsg->{blob}, \&blob_cb, $smsg)};
572 my ($self, $art) = @_;
573 my $smsg = art_lookup($self, $art);
574 return $smsg unless ref $smsg;
575 set_art($self, $art);
576 $smsg->{nntp} = $self;
577 $smsg->{nntp_code} = 222;
578 ${git_async_cat($self->{ng}->git, $smsg->{blob}, \&blob_cb, $smsg)};
582 my ($self, $art) = @_;
583 my $r = art_lookup($self, $art);
584 return $r unless ref $r;
586 set_art($self, $art);
587 "223 $n <$mid> article retrieved - request text separately";
590 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
592 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
596 more($self, '100 help text follows');
601 my ($self, $range) = @_;
602 my $ng = $self->{ng} or return '412 no news group has been selected';
603 defined $range or return '420 No article(s) selected';
605 my ($min, $max) = $ng->mm->minmax;
606 if ($range =~ /\A([0-9]+)\z/) {
608 } elsif ($range =~ /\A([0-9]+)-\z/) {
609 ($beg, $end) = ($1, $max);
610 } elsif ($range =~ /\A([0-9]+)-([0-9]+)\z/) {
611 ($beg, $end) = ($1, $2);
615 $beg = $min if ($beg < $min);
616 $end = $max if ($end > $max);
617 return '420 No article(s) selected' if ($beg > $end);
623 # wbuf is unset or empty, here; {long} may add to it
624 my ($fd, $cb, $t0, @args) = @{$self->{long_cb}};
625 my $more = eval { $cb->($self, @args) };
626 if ($@ || !$self->{sock}) { # something bad happened...
627 delete $self->{long_cb};
628 my $elapsed = now() - $t0;
631 "%s during long response[$fd] - %0.6f",
634 out($self, " deferred[$fd] aborted - %0.6f", $elapsed);
636 } elsif ($more) { # $self->{wbuf}:
637 $self->update_idle_time;
639 # COMPRESS users all share the same DEFLATE context.
640 # Flush it here to ensure clients don't see
644 # no recursion, schedule another call ASAP, but only after
645 # all pending writes are done. autovivify wbuf:
646 my $new_size = push(@{$self->{wbuf}}, \&long_step);
648 # wbuf may be populated by $cb, no need to rearm if so:
649 $self->requeue if $new_size == 1;
651 delete $self->{long_cb};
653 my $elapsed = now() - $t0;
654 my $fd = fileno($self->{sock});
655 out($self, " deferred[$fd] done - %0.6f", $elapsed);
656 my $wbuf = $self->{wbuf}; # do NOT autovivify
657 $self->requeue unless $wbuf && @$wbuf;
661 sub long_response ($$;@) {
662 my ($self, $cb, @args) = @_; # cb returns true if more, false if done
664 my $sock = $self->{sock} or return;
665 # make sure we disable reading during a long response,
666 # clients should not be sending us stuff and making us do more
667 # work while we are stream a response to them
668 $self->{long_cb} = [ fileno($sock), $cb, now(), @args ];
669 long_step($self); # kick off!
673 sub hdr_msgid_range_i {
674 my ($self, $beg, $end) = @_;
675 my $r = $self->{ng}->mm->msg_range($beg, $end);
677 more($self, join("\r\n", map { "$_->[0] <$_->[1]>" } @$r));
681 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
682 my ($self, $xhdr, $range) = @_;
684 if (defined $range && $range =~ $ONE_MSGID) {
685 my ($ng, $n) = mid_lookup($self, $1);
686 return r430 unless $n;
687 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
688 } else { # numeric range
689 $range = $self->{article} unless defined $range;
690 my $r = get_range($self, $range);
691 return $r unless ref $r;
692 more($self, $xhdr ? r221 : r225);
693 long_response($self, \&hdr_msgid_range_i, @$r);
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;
713 my ($self, $beg, $end) = @_;
714 my $ng = $self->{ng};
715 my $r = $ng->mm->msg_range($beg, $end);
717 more($self, join("\r\n", map {
719 "$num ".xref($self, $ng, $num, $_->[1]);
724 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
725 my ($self, $xhdr, $range) = @_;
727 if (defined $range && $range =~ $ONE_MSGID) {
729 my ($ng, $n) = mid_lookup($self, $mid);
730 return r430 unless $n;
731 hdr_mid_response($self, $xhdr, $ng, $n, $range,
732 xref($self, $ng, $n, $mid));
733 } else { # numeric range
734 $range = $self->{article} unless defined $range;
735 my $r = get_range($self, $range);
736 return $r unless ref $r;
737 more($self, $xhdr ? r221 : r225);
738 long_response($self, \&xref_range_i, @$r);
742 sub over_header_for {
743 my ($over, $num, $field) = @_;
744 my $smsg = $over->get_art($num) or return;
745 return PublicInbox::Smsg::date($smsg) if $field eq 'date';
750 my ($self, $beg, $end, $field) = @_;
751 my $over = $self->{ng}->over;
752 my $msgs = $over->query_xover($$beg, $end);
753 scalar(@$msgs) or return;
756 # ->{$field} is faster than ->$field invocations, so favor that.
757 if ($field eq 'date') {
759 $tmp .= "$s->{num} ".PublicInbox::Smsg::date($s)."\r\n"
763 $tmp .= "$s->{num} $s->{$field}\r\n";
767 $self->msg_more($tmp);
768 $$beg = $msgs->[-1]->{num} + 1;
771 sub hdr_smsg ($$$$) {
772 my ($self, $xhdr, $field, $range) = @_;
773 if (defined $range && $range =~ $ONE_MSGID) {
774 my ($ng, $n) = mid_lookup($self, $1);
775 return r430 unless defined $n;
776 my $v = over_header_for($ng->over, $n, $field);
777 hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
778 } else { # numeric range
779 $range = $self->{article} unless defined $range;
780 my $r = get_range($self, $range);
781 return $r unless ref $r;
782 more($self, $xhdr ? r221 : r225);
783 long_response($self, \&smsg_range_i, @$r, $field);
788 my ($self, $xhdr, $header, $range) = @_;
789 my $sub = lc $header;
790 if ($sub eq 'message-id') {
791 hdr_message_id($self, $xhdr, $range);
792 } elsif ($sub eq 'xref') {
793 hdr_xref($self, $xhdr, $range);
794 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
796 hdr_smsg($self, $xhdr, $sub, $range);
797 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
798 hdr_smsg($self, $xhdr, $1, $range);
800 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
806 my ($self, $header, $range) = @_;
807 do_hdr($self, 0, $header, $range);
811 sub cmd_xhdr ($$;$) {
812 my ($self, $header, $range) = @_;
813 do_hdr($self, 1, $header, $range);
816 sub hdr_mid_prefix ($$$$$) {
817 my ($self, $xhdr, $ng, $n, $mid) = @_;
818 return $mid if $xhdr;
820 # HDR for RFC 3977 users
821 if (my $self_ng = $self->{ng}) {
822 ($self_ng eq $ng) ? $n : '0';
828 sub hdr_mid_response ($$$$$$) {
829 my ($self, $xhdr, $ng, $n, $mid, $v) = @_;
832 $res .= r221 . "\r\n";
833 $res .= "$mid $v\r\n";
835 $res .= r225 . "\r\n";
836 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
837 $res .= "$pfx $v\r\n";
839 res($self, $res .= '.');
844 my ($self, $beg, $end) = @_;
845 my $h = over_header_for($self->{ng}->over, $$beg, 'references');
846 more($self, "$$beg $h") if defined($h);
850 sub cmd_xrover ($;$) {
851 my ($self, $range) = @_;
852 my $ng = $self->{ng} or return '412 no newsgroup selected';
853 (defined $range && $range =~ /[<>]/) and
854 return '420 No article(s) selected'; # no message IDs
856 $range = $self->{article} unless defined $range;
857 my $r = get_range($self, $range);
858 return $r unless ref $r;
859 more($self, '224 Overview information follows');
860 long_response($self, \&xrover_i, @$r);
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::Smsg::date($smsg),
875 "Xref: " . xref($self, $ng, $num, $smsg->{mid}));
881 my ($self, $range) = @_;
882 if ($range && $range =~ $ONE_MSGID) {
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);
899 my ($self, $beg, $end) = @_;
900 my $ng = $self->{ng};
901 my $msgs = $ng->over->query_xover($$beg, $end);
902 my $nr = scalar @$msgs or return;
905 more($self, join("\r\n", map {
906 over_line($self, $ng, $_->{num}, $_);
908 $$beg = $msgs->[-1]->{num} + 1;
911 sub cmd_xover ($;$) {
912 my ($self, $range) = @_;
913 $range = $self->{article} unless defined $range;
914 my $r = get_range($self, $range);
915 return $r unless ref $r;
916 my ($beg, $end) = @$r;
917 more($self, "224 Overview information follows for $$beg to $end");
918 long_response($self, \&xover_i, @$r);
921 sub compressed { undef }
923 sub cmd_starttls ($) {
925 my $sock = $self->{sock} or return;
927 return r502 if ($sock->can('accept_SSL') || $self->compressed);
928 my $opt = $self->{nntpd}->{accept_tls} or
929 return '580 can not initiate TLS negotiation';
930 res($self, '382 Continue with TLS negotiation');
931 $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
932 $self->requeue if PublicInbox::DS::accept_tls_step($self);
937 sub cmd_compress ($$) {
938 my ($self, $alg) = @_;
939 return '503 Only DEFLATE is supported' if uc($alg) ne 'DEFLATE';
940 return r502 if $self->compressed;
941 PublicInbox::NNTPdeflate->enable($self);
946 sub zflush {} # overridden by NNTPdeflate
949 my ($self, $mid) = @_;
950 return r501 unless $mid =~ $ONE_MSGID;
953 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
954 my $n = $ng->mm->num_for($mid);
955 push @paths, "$ng->{newsgroup}/$n" if defined $n;
957 return '430 no such article on server' unless @paths;
958 '223 '.join(' ', @paths);
961 sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
963 sub more ($$) { $_[0]->msg_more($_[1] . "\r\n") }
967 my $done = $self->write(\($_[1]));
968 return 0 unless $self->{sock};
974 my ($self, $fmt, @args) = @_;
975 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
979 my ($self, $fmt, @args) = @_;
980 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
983 # callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
987 return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
989 $self->update_idle_time;
990 # only read more requests if we've drained the write buffer,
991 # otherwise we can be buffering infinitely w/o backpressure
993 my $rbuf = $self->{rbuf} // \(my $x = '');
994 my $line = index($$rbuf, "\n");
996 return $self->close if length($$rbuf) >= LINE_MAX;
997 $self->do_read($rbuf, LINE_MAX, length($$rbuf)) or return;
998 $line = index($$rbuf, "\n");
1000 $line = substr($$rbuf, 0, $line + 1, '');
1001 $line =~ s/\r?\n\z//s;
1002 return $self->close if $line =~ /[[:cntrl:]]/s;
1005 my $fd = fileno($self->{sock});
1006 my $r = eval { process_line($self, $line) };
1007 my $pending = $self->{wbuf} ? ' pending' : '';
1008 out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
1009 return $self->close if $r < 0;
1010 $self->rbuf_idle($rbuf);
1011 $self->update_idle_time;
1013 # maybe there's more pipelined data, or we'll have
1014 # to register it for socket-readiness notifications
1015 $self->requeue unless $pending;
1018 # for graceful shutdown in PublicInbox::Daemon:
1020 my ($self, $now) = @_;
1021 ($self->{rbuf} || $self->{wbuf} || $self->not_idle_long($now));