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 warn "E: $smsg->{blob} missing in $self->{ng}->{inboxdir}\n";
527 return $self->requeue;
528 } elsif ($smsg->{blob} ne $oid) {
530 die "BUG: $smsg->{blob} != $oid";
532 my $r = "$code $smsg->{num} <$smsg->{mid}> article retrieved - ";
533 my $eml = PublicInbox::Eml->new($bref);
535 more($self, $r .= 'head and body follow');
536 msg_hdr_write($self, $eml, $smsg);
537 $self->msg_more("\r\n");
538 msg_body_write($self, $bref);
539 } elsif ($code == 221) {
540 more($self, $r .= 'head follows');
541 msg_hdr_write($self, $eml, $smsg);
542 } elsif ($code == 222) {
543 more($self, $r .= 'body follows');
544 msg_body_write($self, $bref);
547 die "BUG: bad code: $r";
549 $self->write(\".\r\n"); # flushes (includes ->zflush)
553 sub cmd_article ($;$) {
554 my ($self, $art) = @_;
555 my $smsg = art_lookup($self, $art);
556 return $smsg unless ref $smsg;
557 set_art($self, $art);
558 $smsg->{nntp} = $self;
559 ${git_async_cat($self->{ng}->git, $smsg->{blob}, \&blob_cb, $smsg)};
563 my ($self, $art) = @_;
564 my $smsg = art_lookup($self, $art);
565 return $smsg unless ref $smsg;
566 set_art($self, $art);
567 $smsg->{nntp} = $self;
568 $smsg->{nntp_code} = 221;
569 ${git_async_cat($self->{ng}->git, $smsg->{blob}, \&blob_cb, $smsg)};
573 my ($self, $art) = @_;
574 my $smsg = art_lookup($self, $art);
575 return $smsg unless ref $smsg;
576 set_art($self, $art);
577 $smsg->{nntp} = $self;
578 $smsg->{nntp_code} = 222;
579 ${git_async_cat($self->{ng}->git, $smsg->{blob}, \&blob_cb, $smsg)};
583 my ($self, $art) = @_;
584 my $smsg = art_lookup($self, $art); # art may be msgid
585 return $smsg unless ref $smsg;
587 set_art($self, $art);
588 "223 $art <$smsg->{mid}> article retrieved - request text separately";
591 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
593 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
597 more($self, '100 help text follows');
602 my ($self, $range) = @_;
603 my $ng = $self->{ng} or return '412 no news group has been selected';
604 defined $range or return '420 No article(s) selected';
606 my ($min, $max) = $ng->mm->minmax;
607 if ($range =~ /\A([0-9]+)\z/) {
609 } elsif ($range =~ /\A([0-9]+)-\z/) {
610 ($beg, $end) = ($1, $max);
611 } elsif ($range =~ /\A([0-9]+)-([0-9]+)\z/) {
612 ($beg, $end) = ($1, $2);
616 $beg = $min if ($beg < $min);
617 $end = $max if ($end > $max);
618 return '420 No article(s) selected' if ($beg > $end);
624 # wbuf is unset or empty, here; {long} may add to it
625 my ($fd, $cb, $t0, @args) = @{$self->{long_cb}};
626 my $more = eval { $cb->($self, @args) };
627 if ($@ || !$self->{sock}) { # something bad happened...
628 delete $self->{long_cb};
629 my $elapsed = now() - $t0;
632 "%s during long response[$fd] - %0.6f",
635 out($self, " deferred[$fd] aborted - %0.6f", $elapsed);
637 } elsif ($more) { # $self->{wbuf}:
638 $self->update_idle_time;
640 # COMPRESS users all share the same DEFLATE context.
641 # Flush it here to ensure clients don't see
645 # no recursion, schedule another call ASAP, but only after
646 # all pending writes are done. autovivify wbuf:
647 my $new_size = push(@{$self->{wbuf}}, \&long_step);
649 # wbuf may be populated by $cb, no need to rearm if so:
650 $self->requeue if $new_size == 1;
652 delete $self->{long_cb};
654 my $elapsed = now() - $t0;
655 my $fd = fileno($self->{sock});
656 out($self, " deferred[$fd] done - %0.6f", $elapsed);
657 my $wbuf = $self->{wbuf}; # do NOT autovivify
658 $self->requeue unless $wbuf && @$wbuf;
662 sub long_response ($$;@) {
663 my ($self, $cb, @args) = @_; # cb returns true if more, false if done
665 my $sock = $self->{sock} or return;
666 # make sure we disable reading during a long response,
667 # clients should not be sending us stuff and making us do more
668 # work while we are stream a response to them
669 $self->{long_cb} = [ fileno($sock), $cb, now(), @args ];
670 long_step($self); # kick off!
674 sub hdr_msgid_range_i {
675 my ($self, $beg, $end) = @_;
676 my $r = $self->{ng}->mm->msg_range($beg, $end);
678 more($self, join("\r\n", map { "$_->[0] <$_->[1]>" } @$r));
682 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
683 my ($self, $xhdr, $range) = @_;
685 if (defined $range && $range =~ $ONE_MSGID) {
686 my ($ng, $n) = mid_lookup($self, $1);
687 return r430 unless $n;
688 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
689 } else { # numeric range
690 $range = $self->{article} unless defined $range;
691 my $r = get_range($self, $range);
692 return $r unless ref $r;
693 more($self, $xhdr ? r221 : r225);
694 long_response($self, \&hdr_msgid_range_i, @$r);
698 sub mid_lookup ($$) {
699 my ($self, $mid) = @_;
700 my $self_ng = $self->{ng};
702 my $n = $self_ng->mm->num_for($mid);
703 return ($self_ng, $n) if defined $n;
705 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
706 next if defined $self_ng && $ng eq $self_ng;
707 my $n = $ng->mm->num_for($mid);
708 return ($ng, $n) if defined $n;
714 my ($self, $beg, $end) = @_;
715 my $ng = $self->{ng};
716 my $r = $ng->mm->msg_range($beg, $end);
718 more($self, join("\r\n", map {
720 "$num ".xref($self, $ng, $num, $_->[1]);
725 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
726 my ($self, $xhdr, $range) = @_;
728 if (defined $range && $range =~ $ONE_MSGID) {
730 my ($ng, $n) = mid_lookup($self, $mid);
731 return r430 unless $n;
732 hdr_mid_response($self, $xhdr, $ng, $n, $range,
733 xref($self, $ng, $n, $mid));
734 } else { # numeric range
735 $range = $self->{article} unless defined $range;
736 my $r = get_range($self, $range);
737 return $r unless ref $r;
738 more($self, $xhdr ? r221 : r225);
739 long_response($self, \&xref_range_i, @$r);
743 sub over_header_for {
744 my ($over, $num, $field) = @_;
745 my $smsg = $over->get_art($num) or return;
746 return PublicInbox::Smsg::date($smsg) if $field eq 'date';
751 my ($self, $beg, $end, $field) = @_;
752 my $over = $self->{ng}->over;
753 my $msgs = $over->query_xover($$beg, $end);
754 scalar(@$msgs) or return;
757 # ->{$field} is faster than ->$field invocations, so favor that.
758 if ($field eq 'date') {
760 $tmp .= "$s->{num} ".PublicInbox::Smsg::date($s)."\r\n"
764 $tmp .= "$s->{num} $s->{$field}\r\n";
768 $self->msg_more($tmp);
769 $$beg = $msgs->[-1]->{num} + 1;
772 sub hdr_smsg ($$$$) {
773 my ($self, $xhdr, $field, $range) = @_;
774 if (defined $range && $range =~ $ONE_MSGID) {
775 my ($ng, $n) = mid_lookup($self, $1);
776 return r430 unless defined $n;
777 my $v = over_header_for($ng->over, $n, $field);
778 hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
779 } else { # numeric range
780 $range = $self->{article} unless defined $range;
781 my $r = get_range($self, $range);
782 return $r unless ref $r;
783 more($self, $xhdr ? r221 : r225);
784 long_response($self, \&smsg_range_i, @$r, $field);
789 my ($self, $xhdr, $header, $range) = @_;
790 my $sub = lc $header;
791 if ($sub eq 'message-id') {
792 hdr_message_id($self, $xhdr, $range);
793 } elsif ($sub eq 'xref') {
794 hdr_xref($self, $xhdr, $range);
795 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
797 hdr_smsg($self, $xhdr, $sub, $range);
798 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
799 hdr_smsg($self, $xhdr, $1, $range);
801 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
807 my ($self, $header, $range) = @_;
808 do_hdr($self, 0, $header, $range);
812 sub cmd_xhdr ($$;$) {
813 my ($self, $header, $range) = @_;
814 do_hdr($self, 1, $header, $range);
817 sub hdr_mid_prefix ($$$$$) {
818 my ($self, $xhdr, $ng, $n, $mid) = @_;
819 return $mid if $xhdr;
821 # HDR for RFC 3977 users
822 if (my $self_ng = $self->{ng}) {
823 ($self_ng eq $ng) ? $n : '0';
829 sub hdr_mid_response ($$$$$$) {
830 my ($self, $xhdr, $ng, $n, $mid, $v) = @_;
833 $res .= r221 . "\r\n";
834 $res .= "$mid $v\r\n";
836 $res .= r225 . "\r\n";
837 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
838 $res .= "$pfx $v\r\n";
840 res($self, $res .= '.');
845 my ($self, $beg, $end) = @_;
846 my $h = over_header_for($self->{ng}->over, $$beg, 'references');
847 more($self, "$$beg $h") if defined($h);
851 sub cmd_xrover ($;$) {
852 my ($self, $range) = @_;
853 my $ng = $self->{ng} or return '412 no newsgroup selected';
854 (defined $range && $range =~ /[<>]/) and
855 return '420 No article(s) selected'; # no message IDs
857 $range = $self->{article} unless defined $range;
858 my $r = get_range($self, $range);
859 return $r unless ref $r;
860 more($self, '224 Overview information follows');
861 long_response($self, \&xrover_i, @$r);
864 sub over_line ($$$$) {
865 my ($self, $ng, $num, $smsg) = @_;
866 # n.b. field access and procedural calls can be
867 # 10%-15% faster than OO method calls:
868 my $s = join("\t", $num,
871 PublicInbox::Smsg::date($smsg),
876 "Xref: " . xref($self, $ng, $num, $smsg->{mid}));
882 my ($self, $range) = @_;
883 if ($range && $range =~ $ONE_MSGID) {
884 my ($ng, $n) = mid_lookup($self, $1);
885 defined $n or return r430;
886 my $smsg = $ng->over->get_art($n) or return r430;
887 more($self, '224 Overview information follows (multi-line)');
889 # Only set article number column if it's the current group
890 my $self_ng = $self->{ng};
891 $n = 0 if (!$self_ng || $self_ng ne $ng);
892 more($self, over_line($self, $ng, $n, $smsg));
895 cmd_xover($self, $range);
900 my ($self, $beg, $end) = @_;
901 my $ng = $self->{ng};
902 my $msgs = $ng->over->query_xover($$beg, $end);
903 my $nr = scalar @$msgs or return;
906 more($self, join("\r\n", map {
907 over_line($self, $ng, $_->{num}, $_);
909 $$beg = $msgs->[-1]->{num} + 1;
912 sub cmd_xover ($;$) {
913 my ($self, $range) = @_;
914 $range = $self->{article} unless defined $range;
915 my $r = get_range($self, $range);
916 return $r unless ref $r;
917 my ($beg, $end) = @$r;
918 more($self, "224 Overview information follows for $$beg to $end");
919 long_response($self, \&xover_i, @$r);
922 sub compressed { undef }
924 sub cmd_starttls ($) {
926 my $sock = $self->{sock} or return;
928 return r502 if ($sock->can('accept_SSL') || $self->compressed);
929 my $opt = $self->{nntpd}->{accept_tls} or
930 return '580 can not initiate TLS negotiation';
931 res($self, '382 Continue with TLS negotiation');
932 $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
933 $self->requeue if PublicInbox::DS::accept_tls_step($self);
938 sub cmd_compress ($$) {
939 my ($self, $alg) = @_;
940 return '503 Only DEFLATE is supported' if uc($alg) ne 'DEFLATE';
941 return r502 if $self->compressed;
942 PublicInbox::NNTPdeflate->enable($self);
947 sub zflush {} # overridden by NNTPdeflate
950 my ($self, $mid) = @_;
951 return r501 unless $mid =~ $ONE_MSGID;
954 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
955 my $n = $ng->mm->num_for($mid);
956 push @paths, "$ng->{newsgroup}/$n" if defined $n;
958 return '430 no such article on server' unless @paths;
959 '223 '.join(' ', @paths);
962 sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
964 sub more ($$) { $_[0]->msg_more($_[1] . "\r\n") }
968 my $done = $self->write(\($_[1]));
969 return 0 unless $self->{sock};
975 my ($self, $fmt, @args) = @_;
976 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
980 my ($self, $fmt, @args) = @_;
981 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
984 # callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
988 return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
990 $self->update_idle_time;
991 # only read more requests if we've drained the write buffer,
992 # otherwise we can be buffering infinitely w/o backpressure
994 my $rbuf = $self->{rbuf} // \(my $x = '');
995 my $line = index($$rbuf, "\n");
997 return $self->close if length($$rbuf) >= LINE_MAX;
998 $self->do_read($rbuf, LINE_MAX, length($$rbuf)) or return;
999 $line = index($$rbuf, "\n");
1001 $line = substr($$rbuf, 0, $line + 1, '');
1002 $line =~ s/\r?\n\z//s;
1003 return $self->close if $line =~ /[[:cntrl:]]/s;
1006 my $fd = fileno($self->{sock});
1007 my $r = eval { process_line($self, $line) };
1008 my $pending = $self->{wbuf} ? ' pending' : '';
1009 out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
1010 return $self->close if $r < 0;
1011 $self->rbuf_idle($rbuf);
1012 $self->update_idle_time;
1014 # maybe there's more pipelined data, or we'll have
1015 # to register it for socket-readiness notifications
1016 $self->requeue unless $pending;
1019 # for graceful shutdown in PublicInbox::Daemon:
1021 my ($self, $now) = @_;
1022 ($self->{rbuf} || $self->{wbuf} || $self->not_idle_long($now));