1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Each instance of this represents a NNTP client socket
5 package PublicInbox::NNTP;
8 use base qw(PublicInbox::DS);
9 use fields qw(nntpd article rbuf ng long_res);
10 use PublicInbox::Search;
11 use PublicInbox::Msgmap;
12 use PublicInbox::MID qw(mid_escape);
14 require PublicInbox::EvCleanup;
16 use POSIX qw(strftime);
17 PublicInbox::DS->import(qw(now msg_more));
18 use Digest::SHA qw(sha1_hex);
19 use Time::Local qw(timegm timelocal);
21 r501 => '501 command syntax error',
22 r221 => '221 Header follows',
23 r224 => '224 Overview information follows (multi-line)',
24 r225 => '225 Headers follow (multi-line)',
25 r430 => '430 No article with that message-id',
27 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
30 my @OVERVIEW = qw(Subject From Date Message-ID References Xref);
31 my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines)) . ":\r\n";
32 my $LIST_HEADERS = join("\r\n", @OVERVIEW,
33 qw(:bytes :lines Xref To Cc)) . "\r\n";
35 # disable commands with easy DoS potential:
36 my %DISABLED; # = map { $_ => 1 } qw(xover list_overview_fmt newnews xhdr);
38 my $EXPMAP; # fd -> [ idle_time, $self ]
40 our $EXPTIME = 180; # 3 minutes
48 foreach my $nntp (@$q) {
49 # for request && response protocols, always finish writing
50 # before finishing reading:
51 if (my $long_cb = $nntp->{long_res}) {
52 $nntp->write($long_cb);
54 # pipelined request, we bypassed socket-readiness
61 sub update_idle_time ($) {
63 my $sock = $self->{sock} or return;
64 $EXPMAP->{fileno($sock)} = [ now(), $self ];
70 my $old = $now - $exp;
73 while (my ($fd, $v) = each %$EXPMAP) {
74 my ($idle_time, $nntp) = @$v;
75 if ($idle_time < $old) {
76 $nntp->close; # idempotent
84 $expt = PublicInbox::EvCleanup::later(*expire_old);
87 # noop to kick outselves out of the loop ASAP so descriptors
89 PublicInbox::EvCleanup::asap(sub {});
94 my ($class, $sock, $nntpd) = @_;
95 my $self = fields::new($class);
96 $self->SUPER::new($sock, EPOLLIN | EPOLLONESHOT);
97 $self->{nntpd} = $nntpd;
98 res($self, '201 ' . $nntpd->{servername} . ' ready - post via email');
100 update_idle_time($self);
101 $expt ||= PublicInbox::EvCleanup::later(*expire_old);
106 my ($cb, $argc) = @_;
107 my $tot = prototype $cb;
108 my ($nreq, undef) = split(';', $tot);
109 $nreq = ($nreq =~ tr/$//) - 1;
110 $tot = ($tot =~ tr/$//) - 1;
111 ($argc <= $tot && $argc >= $nreq);
114 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
115 sub process_line ($$) {
117 my ($req, @args) = split(/[ \t]/, $l);
118 return 1 unless defined($req); # skip blank line
122 $req = $DISABLED{$req} ? undef : *{'cmd_'.$req}{CODE};
124 return res($self, '500 command not recognized') unless $req;
125 return res($self, r501) unless args_ok($req, scalar @args);
127 my $res = eval { $req->($self, @args) };
129 if ($err && $self->{sock}) {
132 err($self, 'error from: %s (%s)', $l, $err);
133 $res = '503 program fault - command not performed';
135 return 0 unless defined $res;
140 my ($self, $arg) = @_;
142 return r501 unless $arg eq 'READER';
143 '201 Posting prohibited';
146 sub cmd_slave ($) { '202 slave status noted' }
148 sub cmd_xgtitle ($;$) {
149 my ($self, $wildmat) = @_;
150 more($self, '282 list of groups and descriptions follows');
151 list_newsgroups($self, $wildmat);
155 sub list_overview_fmt ($) {
157 msg_more($self, $OVERVIEW_FMT);
160 sub list_headers ($;$) {
162 msg_more($self, $LIST_HEADERS);
165 sub list_active ($;$) {
166 my ($self, $wildmat) = @_;
167 wildmat2re($wildmat);
168 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
169 $ng->{newsgroup} =~ $wildmat or next;
170 group_line($self, $ng);
174 sub list_active_times ($;$) {
175 my ($self, $wildmat) = @_;
176 wildmat2re($wildmat);
177 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
178 $ng->{newsgroup} =~ $wildmat or next;
179 my $c = eval { $ng->mm->created_at } || time;
180 more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
184 sub list_newsgroups ($;$) {
185 my ($self, $wildmat) = @_;
186 wildmat2re($wildmat);
187 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
188 $ng->{newsgroup} =~ $wildmat or next;
189 my $d = $ng->description;
190 more($self, "$ng->{newsgroup} $d");
194 # LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
195 sub cmd_list ($;$$) {
196 my ($self, @args) = @_;
198 my $arg = shift @args;
199 $arg =~ tr/A-Z./a-z_/;
201 return r501 if $DISABLED{$arg};
207 return r501 unless $arg && args_ok($arg, scalar @args);
208 more($self, '215 information follows');
209 $arg->($self, @args);
211 more($self, '215 list of newsgroups follows');
212 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
213 group_line($self, $ng);
219 sub cmd_listgroup ($;$) {
220 my ($self, $group) = @_;
221 if (defined $group) {
222 my $res = cmd_group($self, $group);
223 return $res if ($res !~ /\A211 /);
227 $self->{ng} or return '412 no newsgroup selected';
229 long_response($self, sub {
230 my $ary = $self->{ng}->mm->ids_after(\$n);
231 scalar @$ary or return;
232 more($self, join("\r\n", @$ary));
237 sub parse_time ($$;$) {
238 my ($date, $time, $gmt) = @_;
239 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
241 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
244 my @now = $gmt ? gmtime : localtime;
245 my ($YYYY, $MM, $DD);
246 if (length($date) == 8) { # RFC 3977 allows YYYYMMDD
247 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
248 } else { # legacy clients send YYMMDD
249 ($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
250 my $cur_year = $now[5] + 1900;
251 if ($YYYY > $cur_year) {
252 $YYYY += int($cur_year / 1000) * 1000 - 100;
256 timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
258 timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
262 sub group_line ($$) {
263 my ($self, $ng) = @_;
264 my ($min, $max) = $ng->mm->minmax;
265 more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
268 sub cmd_newgroups ($$$;$$) {
269 my ($self, $date, $time, $gmt, $dists) = @_;
270 my $ts = eval { parse_time($date, $time, $gmt) };
274 more($self, '231 list of new newsgroups follows');
275 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
276 my $c = eval { $ng->mm->created_at } || 0;
277 next unless $c > $ts;
278 group_line($self, $ng);
283 sub wildmat2re (;$) {
284 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
289 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
291 my $key = sha1_hex($orig . $salt);
292 $orig =~ s/([^\w\-])+/\Q$1/g;
296 my %map = ('*' => '.*', '?' => '.' );
297 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
299 $tmp =~ s#([a-f0-9]{40})#
300 my $orig = $keep{$1};
301 defined $orig ? $orig : $1;
304 $_[0] = qr/\A$tmp\z/;
308 return $_[0] = qr/\A\z/ unless defined $_[0];
309 my %map = ('*' => '.*', ',' => '|');
310 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
311 $_[0] = qr/\A(?:$_[0])\z/;
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 @over;
332 long_response($self, sub {
334 my $msgs = $over->query_ts($ts, $prev);
337 join(">\r\n<", map { $_->mid } @$msgs ).
339 $prev = $msgs->[-1]->{num};
342 if (@over) { # continue onto next newsgroup
345 } else { # break out of the long response.
353 my ($self, $group) = @_;
354 my $no_such = '411 no such news group';
355 my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
358 my ($min, $max) = $ng->mm->minmax;
361 $self->{article} = $min;
362 my $est_size = $max - $min;
363 "211 $est_size $min $max $group";
366 sub article_adj ($$) {
367 my ($self, $off) = @_;
368 my $ng = $self->{ng} or return '412 no newsgroup selected';
370 my $n = $self->{article};
371 defined $n or return '420 no current article has been selected';
374 my $mid = $ng->mm->mid_for($n);
376 $n = $off > 0 ? 'next' : 'previous';
377 return "421 no $n article in this group";
379 $self->{article} = $n;
380 "223 $n <$mid> article retrieved - request text separately";
383 sub cmd_next ($) { article_adj($_[0], 1) }
384 sub cmd_last ($) { article_adj($_[0], -1) }
386 # We want to encourage using email and CC-ing everybody involved to avoid
387 # the single-point-of-failure a single server provides.
390 my $ng = $self->{ng};
391 $ng ? "440 mailto:$ng->{-primary_address} to post"
392 : '440 posting not allowed'
397 res($self, '205 closing connection - goodbye!');
402 sub header_append ($$$) {
403 my ($hdr, $k, $v) = @_;
404 my @v = $hdr->header($k);
408 $hdr->header_set($k, @v, $v);
412 my ($self, $ng, $n, $mid) = @_;
413 my $ret = $self->{nntpd}->{servername} . " $ng->{newsgroup}:$n";
415 # num_for is pretty cheap and sometimes we'll lookup the existence
416 # of an article without getting even the OVER info. In other words,
417 # I'm not sure if its worth optimizing by scanning To:/Cc: and
418 # PublicInbox::ExtMsg on the PSGI end is just as expensive
419 foreach my $other (@{$self->{nntpd}->{grouplist}}) {
420 next if $ng eq $other;
421 my $num = eval { $other->mm->num_for($mid) } or next;
422 $ret .= " $other->{newsgroup}:$num";
427 sub set_nntp_headers ($$$$$) {
428 my ($self, $hdr, $ng, $n, $mid) = @_;
430 # why? leafnode requires a Path: header for some inexplicable
431 # reason. We'll fake the shortest one possible.
432 $hdr->header_set('Path', 'y');
434 # leafnode (and maybe other NNTP clients) have trouble dealing
435 # with v2 messages which have multiple Message-IDs (either due
436 # to our own content-based dedupe or buggy git-send-email versions).
437 my @mids = $hdr->header('Message-ID');
438 if (scalar(@mids) > 1) {
440 $hdr->header_set('Message-ID', $mid0);
441 my @alt = $hdr->header('X-Alt-Message-ID');
442 my %seen = map { $_ => 1 } (@alt, $mid0);
443 foreach my $m (@mids) {
447 $hdr->header_set('X-Alt-Message-ID', @alt);
451 my $xref = xref($self, $ng, $n, $mid);
452 $hdr->header_set('Xref', $xref);
453 $xref =~ s/:[0-9]+//g;
454 $hdr->header_set('Newsgroups', (split(/ /, $xref, 2))[1]);
455 header_append($hdr, 'List-Post', "<mailto:$ng->{-primary_address}>");
456 if (my $url = $ng->base_url) {
457 $mid = mid_escape($mid);
458 header_append($hdr, 'Archived-At', "<$url$mid/>");
459 header_append($hdr, 'List-Archive', "<$url>");
463 sub art_lookup ($$$) {
464 my ($self, $art, $set_headers) = @_;
465 my $ng = $self->{ng};
469 if ($art =~ /\A[0-9]+\z/) {
470 $err = '423 no such article number in this group';
473 } elsif ($art =~ /\A<([^>]+)>\z/) {
476 $n = $ng->mm->num_for($mid) if $ng;
477 goto found if defined $n;
478 foreach my $g (values %{$self->{nntpd}->{groups}}) {
479 $n = $g->mm->num_for($mid);
490 $err = '420 no current article has been selected';
491 $n = $self->{article};
492 defined $n or return $err;
494 $ng or return '412 no newsgroup has been selected';
495 $mid = $ng->mm->mid_for($n);
496 defined $mid or return $err;
499 my $smsg = $ng->over->get_art($n) or return $err;
500 my $msg = $ng->msg_by_smsg($smsg) or return $err;
501 my $s = Email::Simple->new($msg);
503 set_nntp_headers($self, $s->header_obj, $ng, $n, $mid);
506 $s->body_set('') if ($set_headers == 2);
508 [ $n, $mid, $s, $smsg->bytes, $smsg->lines, $ng ];
511 sub simple_body_write ($$) {
515 $body =~ s/^\./../smg;
516 $body =~ s/(?<!\r)\n/\r\n/sg;
517 msg_more($self, $body);
518 msg_more($self, "\r\n") unless $body =~ /\r\n\z/s;
523 my ($self, $art) = @_;
524 $self->{article} = $art if defined $art && $art =~ /\A[0-9]+\z/;
528 my $hdr = $_[0]->header_obj->as_string;
530 $hdr =~ s/(?<!\r)\n/\r\n/sg;
532 # for leafnode compatibility, we need to ensure Message-ID headers
533 # are only a single line. We can't subclass Email::Simple::Header
534 # and override _default_fold_at in here, either; since that won't
535 # affect messages already in the archive.
536 $hdr =~ s/^(Message-ID:)[ \t]*\r\n[ \t]+([^\r]+)\r\n/$1 $2\r\n/igsm;
541 sub cmd_article ($;$) {
542 my ($self, $art) = @_;
543 my $r = art_lookup($self, $art, 1);
544 return $r unless ref $r;
545 my ($n, $mid, $s) = @$r;
546 set_art($self, $art);
547 more($self, "220 $n <$mid> article retrieved - head and body follow");
548 msg_more($self, _header($s));
549 msg_more($self, "\r\n");
550 simple_body_write($self, $s);
554 my ($self, $art) = @_;
555 my $r = art_lookup($self, $art, 2);
556 return $r unless ref $r;
557 my ($n, $mid, $s) = @$r;
558 set_art($self, $art);
559 more($self, "221 $n <$mid> article retrieved - head follows");
560 msg_more($self, _header($s));
565 my ($self, $art) = @_;
566 my $r = art_lookup($self, $art, 0);
567 return $r unless ref $r;
568 my ($n, $mid, $s) = @$r;
569 set_art($self, $art);
570 more($self, "222 $n <$mid> article retrieved - body follows");
571 simple_body_write($self, $s);
575 my ($self, $art) = @_;
576 my $r = art_lookup($self, $art, 0);
577 return $r unless ref $r;
578 my ($n, $mid, undef) = @$r;
579 set_art($self, $art);
580 "223 $n <$mid> article retrieved - request text separately";
583 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
585 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
589 more($self, '100 help text follows');
594 my ($self, $range) = @_;
595 my $ng = $self->{ng} or return '412 no news group has been selected';
596 defined $range or return '420 No article(s) selected';
598 my ($min, $max) = $ng->mm->minmax;
599 if ($range =~ /\A([0-9]+)\z/) {
601 } elsif ($range =~ /\A([0-9]+)-\z/) {
602 ($beg, $end) = ($1, $max);
603 } elsif ($range =~ /\A([0-9]+)-([0-9]+)\z/) {
604 ($beg, $end) = ($1, $2);
608 $beg = $min if ($beg < $min);
609 $end = $max if ($end > $max);
610 return '420 No article(s) selected' if ($beg > $end);
614 sub long_response ($$) {
615 my ($self, $cb) = @_;
616 die "BUG: nested long response" if $self->{long_res};
618 my $fd = fileno($self->{sock});
619 defined $fd or return;
620 # make sure we disable reading during a long response,
621 # clients should not be sending us stuff and making us do more
622 # work while we are stream a response to them
624 $self->{long_res} = sub {
625 my $more = eval { $cb->() };
626 if ($@ || !$self->{sock}) { # something bad happened...
627 delete $self->{long_res};
631 "%s during long response[$fd] - %0.6f",
635 update_idle_time($self);
638 out($self, " deferred[$fd] aborted - %0.6f",
641 } elsif ($more) { # $self->{wbuf}:
642 # no recursion, schedule another call ASAP
643 # but only after all pending writes are done
644 update_idle_time($self);
647 $nextt ||= PublicInbox::EvCleanup::asap(*next_tick);
649 delete $self->{long_res};
652 out($self, " deferred[$fd] done - %0.6f", now() - $t0);
655 $self->{long_res}->(); # kick off!
659 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
660 my ($self, $xhdr, $range) = @_;
662 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
663 my ($ng, $n) = mid_lookup($self, $1);
664 return r430 unless $n;
665 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
666 } else { # numeric range
667 $range = $self->{article} unless defined $range;
668 my $r = get_range($self, $range);
669 return $r unless ref $r;
670 my $mm = $self->{ng}->mm;
671 my ($beg, $end) = @$r;
672 more($self, $xhdr ? r221 : r225);
673 long_response($self, sub {
674 my $r = $mm->msg_range(\$beg, $end);
676 more($self, join("\r\n", map {
684 sub mid_lookup ($$) {
685 my ($self, $mid) = @_;
686 my $self_ng = $self->{ng};
688 my $n = $self_ng->mm->num_for($mid);
689 return ($self_ng, $n) if defined $n;
691 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
692 next if defined $self_ng && $ng eq $self_ng;
693 my $n = $ng->mm->num_for($mid);
694 return ($ng, $n) if defined $n;
699 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
700 my ($self, $xhdr, $range) = @_;
702 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
704 my ($ng, $n) = mid_lookup($self, $mid);
705 return r430 unless $n;
706 hdr_mid_response($self, $xhdr, $ng, $n, $range,
707 xref($self, $ng, $n, $mid));
708 } else { # numeric range
709 $range = $self->{article} unless defined $range;
710 my $r = get_range($self, $range);
711 return $r unless ref $r;
712 my $ng = $self->{ng};
714 my ($beg, $end) = @$r;
715 more($self, $xhdr ? r221 : r225);
716 long_response($self, sub {
717 my $r = $mm->msg_range(\$beg, $end);
719 more($self, join("\r\n", map {
721 "$num ".xref($self, $ng, $num, $_->[1]);
728 sub over_header_for {
729 my ($over, $num, $field) = @_;
730 my $smsg = $over->get_art($num) or return;
731 return PublicInbox::SearchMsg::date($smsg) if $field eq 'date';
735 sub hdr_searchmsg ($$$$) {
736 my ($self, $xhdr, $field, $range) = @_;
737 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
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 $over = $self->{ng}->over;
745 my $mm = $self->{ng}->mm;
746 my $r = get_range($self, $range);
747 return $r unless ref $r;
748 my ($beg, $end) = @$r;
749 more($self, $xhdr ? r221 : r225);
751 long_response($self, sub {
752 my $msgs = $over->query_xover($cur, $end);
753 my $nr = scalar @$msgs or return;
755 foreach my $s (@$msgs) {
756 $tmp .= $s->{num} . ' ' . $s->$field . "\r\n";
759 msg_more($self, $tmp);
760 $cur = $msgs->[-1]->{num} + 1;
766 my ($self, $xhdr, $header, $range) = @_;
767 my $sub = lc $header;
768 if ($sub eq 'message-id') {
769 hdr_message_id($self, $xhdr, $range);
770 } elsif ($sub eq 'xref') {
771 hdr_xref($self, $xhdr, $range);
772 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
774 hdr_searchmsg($self, $xhdr, $sub, $range);
775 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
776 hdr_searchmsg($self, $xhdr, $1, $range);
778 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
784 my ($self, $header, $range) = @_;
785 do_hdr($self, 0, $header, $range);
789 sub cmd_xhdr ($$;$) {
790 my ($self, $header, $range) = @_;
791 do_hdr($self, 1, $header, $range);
794 sub hdr_mid_prefix ($$$$$) {
795 my ($self, $xhdr, $ng, $n, $mid) = @_;
796 return $mid if $xhdr;
798 # HDR for RFC 3977 users
799 if (my $self_ng = $self->{ng}) {
800 ($self_ng eq $ng) ? $n : '0';
806 sub hdr_mid_response ($$$$$$) {
807 my ($self, $xhdr, $ng, $n, $mid, $v) = @_; # r: art_lookup result
810 $res .= r221 . "\r\n";
811 $res .= "$mid $v\r\n";
813 $res .= r225 . "\r\n";
814 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
815 $res .= "$pfx $v\r\n";
817 res($self, $res .= '.');
821 sub cmd_xrover ($;$) {
822 my ($self, $range) = @_;
823 my $ng = $self->{ng} or return '412 no newsgroup selected';
824 (defined $range && $range =~ /[<>]/) and
825 return '420 No article(s) selected'; # no message IDs
827 $range = $self->{article} unless defined $range;
828 my $r = get_range($self, $range);
829 return $r unless ref $r;
830 my ($beg, $end) = @$r;
832 my $over = $ng->over;
833 more($self, '224 Overview information follows');
835 long_response($self, sub {
836 my $h = over_header_for($over, $beg, 'references');
837 more($self, "$beg $h") if defined($h);
842 sub over_line ($$$$) {
843 my ($self, $ng, $num, $smsg) = @_;
844 # n.b. field access and procedural calls can be
845 # 10%-15% faster than OO method calls:
846 my $s = join("\t", $num,
849 PublicInbox::SearchMsg::date($smsg),
854 "Xref: " . xref($self, $ng, $num, $smsg->{mid}));
860 my ($self, $range) = @_;
861 if ($range && $range =~ /\A<(.+)>\z/) {
862 my ($ng, $n) = mid_lookup($self, $1);
863 defined $n or return r430;
864 my $smsg = $ng->over->get_art($n) or return r430;
865 more($self, '224 Overview information follows (multi-line)');
867 # Only set article number column if it's the current group
868 my $self_ng = $self->{ng};
869 $n = 0 if (!$self_ng || $self_ng ne $ng);
870 more($self, over_line($self, $ng, $n, $smsg));
873 cmd_xover($self, $range);
877 sub cmd_xover ($;$) {
878 my ($self, $range) = @_;
879 $range = $self->{article} unless defined $range;
880 my $r = get_range($self, $range);
881 return $r unless ref $r;
882 my ($beg, $end) = @$r;
883 more($self, "224 Overview information follows for $beg to $end");
884 my $over = $self->{ng}->over;
886 long_response($self, sub {
887 my $msgs = $over->query_xover($cur, $end);
888 my $nr = scalar @$msgs or return;
891 more($self, join("\r\n", map {
892 over_line($self, $self->{ng}, $_->{num}, $_);
894 $cur = $msgs->[-1]->{num} + 1;
899 my ($self, $mid) = @_;
900 return r501 unless $mid =~ /\A<(.+)>\z/;
903 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
904 my $n = $ng->mm->num_for($mid);
905 push @paths, "$ng->{newsgroup}/$n" if defined $n;
907 return '430 no such article on server' unless @paths;
908 '223 '.join(' ', @paths);
911 sub res ($$) { do_write($_[0], $_[1] . "\r\n") }
913 sub more ($$) { msg_more($_[0], $_[1] . "\r\n") }
917 my $done = $self->write(\($_[1]));
918 return 0 unless $self->{sock};
924 my ($self, $fmt, @args) = @_;
925 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
929 my ($self, $fmt, @args) = @_;
930 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
936 return unless $self->flush_write && $self->{sock};
938 update_idle_time($self);
939 # only read more requests if we've drained the write buffer,
940 # otherwise we can be buffering infinitely w/o backpressure
942 use constant LINE_MAX => 512; # RFC 977 section 2.3
943 my $rbuf = \($self->{rbuf});
946 if (index($$rbuf, "\n") < 0) {
947 my $off = length($$rbuf);
948 $r = sysread($self->{sock}, $$rbuf, LINE_MAX, $off);
949 unless (defined $r) {
950 return $! == EAGAIN ? $self->watch_in1 : $self->close;
952 return $self->close if $r == 0;
955 while ($r > 0 && $$rbuf =~ s/\A[ \t\r\n]*([^\r\n]*)\r?\n//) {
957 return $self->close if $line =~ /[[:cntrl:]]/s;
959 my $fd = fileno($self->{sock});
960 $r = eval { process_line($self, $line) };
961 my $d = $self->{long_res} ?
962 " deferred[$fd]" : '';
963 out($self, "[$fd] %s - %0.6f$d", $line, now() - $t0);
966 return $self->close if $r < 0;
967 my $len = length($$rbuf);
968 return $self->close if ($len >= LINE_MAX);
969 update_idle_time($self);
971 # maybe there's more pipelined data, or we'll have
972 # to register it for socket-readiness notifications
973 check_read($self) unless ($self->{long_res} || $self->{wbuf});
978 if (index($self->{rbuf}, "\n") >= 0) {
979 # Force another read if there is a pipelined request.
980 # We don't know if the socket has anything for us to read,
981 # and we must double-check again by the time the timer fires
982 # in case we really did dispatch a read event and started
983 # another long response.
985 $nextt ||= PublicInbox::EvCleanup::asap(*next_tick);
987 # no pipelined requests available, let the kernel know
988 # to wake us up if there's more
989 $self->watch_in1; # PublicInbox::DS::watch_in1
993 sub not_idle_long ($$) {
994 my ($self, $now) = @_;
995 my $sock = $self->{sock} or return;
996 my $ary = $EXPMAP->{fileno($sock)} or return;
997 my $exp_at = $ary->[0] + $EXPTIME;
1001 # for graceful shutdown in PublicInbox::Daemon:
1003 my ($self, $now) = @_;
1004 ($self->{rbuf} ne '' || $self->{long_res} ||
1005 $self->{wbuf} || not_idle_long($self, $now));