1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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(Danga::Socket);
9 use fields qw(nntpd article rbuf ng long_res);
10 use PublicInbox::Search;
11 use PublicInbox::Msgmap;
13 use PublicInbox::MID qw(mid2path);
15 use Data::Dumper qw(Dumper);
16 use POSIX qw(strftime);
17 use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
18 use URI::Escape qw(uri_escape_utf8);
20 r501 => '501 command syntax error',
21 r221 => '221 Header follows',
22 r224 => '224 Overview information follows (multi-line)',
23 r225 => '225 Headers follow (multi-line)',
24 r430 => '430 No article with that message-id',
25 long_response_limit => 0xffffffff,
28 sub now () { clock_gettime(CLOCK_MONOTONIC) };
30 my @OVERVIEW = qw(Subject From Date Message-ID References);
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 # LISTGROUP could get pretty bad, too...
37 my %DISABLED; # = map { $_ => 1 } qw(xover list_overview_fmt newnews xhdr);
39 my $EXPMAP; # fd -> [ idle_time, $self ]
41 our $EXPTIME = 180; # 3 minutes
42 my $WEAKEN = {}; # string(nntpd) -> nntpd
51 foreach my $nntp (@$q) {
52 # for request && response protocols, always finish writing
53 # before finishing reading:
54 if (my $long_cb = $nntp->{long_res}) {
55 $nntp->write($long_cb);
56 } elsif (&Danga::Socket::POLLIN & $nntp->{event_watch}) {
62 sub update_idle_time ($) {
64 my $tmp = $self->{sock} or return;
66 defined $tmp and $EXPMAP->{$tmp} = [ now(), $self ];
69 # reduce FD pressure by closing some "git cat-file --batch" processes
70 # and unused FDs for msgmap and Xapian indices
71 sub weaken_groups () {
73 foreach my $nntpd (values %$WEAKEN) {
74 $_->weaken_all foreach (@{$nntpd->{grouplist}});
82 my $old = $now - $exp;
83 my $next = $now + $exp;
86 while (my ($fd, $v) = each %$EXPMAP) {
87 my ($idle_time, $nntp) = @$v;
88 if ($idle_time < $old) {
89 $nntp->close; # idempotent
91 my $nexp = $idle_time + $exp;
92 $next = $nexp if ($nexp < $next);
100 $next = 0 if $next < 0;
101 $EXPTIMER = Danga::Socket->AddTimer($next, *expire_old);
105 # noop to kick outselves out of the loop ASAP so descriptors
107 Danga::Socket->AddTimer(0, sub {});
109 # grace period for reaping resources
110 $WEAKTIMER ||= Danga::Socket->AddTimer(30, *weaken_groups);
115 my ($class, $sock, $nntpd) = @_;
116 my $self = fields::new($class);
117 binmode $sock, ':utf8'; # RFC 3977
118 $self->SUPER::new($sock);
119 $self->{nntpd} = $nntpd;
120 res($self, '201 server ready - post via email');
122 $self->watch_read(1);
123 update_idle_time($self);
124 $WEAKEN->{"$nntpd"} = $nntpd;
125 $EXPTIMER ||= Danga::Socket->AddTimer($EXPTIME, *expire_old);
130 my ($cb, $argc) = @_;
131 my $tot = prototype $cb;
132 my ($nreq, undef) = split(';', $tot);
133 $nreq = ($nreq =~ tr/$//) - 1;
134 $tot = ($tot =~ tr/$//) - 1;
135 ($argc <= $tot && $argc >= $nreq);
138 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
139 sub process_line ($$) {
141 my ($req, @args) = split(/\s+/, $l);
145 $req = $DISABLED{$req} ? undef : *{'cmd_'.$req}{CODE};
147 return res($self, '500 command not recognized') unless $req;
148 return res($self, r501) unless args_ok($req, scalar @args);
150 my $res = eval { $req->($self, @args) };
152 if ($err && !$self->{closed}) {
153 chomp($l = Dumper(\$l));
154 err($self, 'error from: %s (%s)', $l, $err);
155 $res = '503 program fault - command not performed';
157 return 0 unless defined $res;
162 my ($self, $arg) = @_;
164 return r501 unless $arg eq 'READER';
165 '201 Posting prohibited';
168 sub cmd_slave ($) { '202 slave status noted' }
170 sub cmd_xgtitle ($;$) {
171 my ($self, $wildmat) = @_;
172 more($self, '282 list of groups and descriptions follows');
173 list_newsgroups($self, $wildmat);
177 sub list_overview_fmt ($) {
179 do_more($self, $OVERVIEW_FMT);
182 sub list_headers ($;$) {
184 do_more($self, $LIST_HEADERS);
187 sub list_active ($;$) {
188 my ($self, $wildmat) = @_;
189 wildmat2re($wildmat);
190 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
191 $ng->{name} =~ $wildmat or next;
192 group_line($self, $ng);
196 sub list_active_times ($;$) {
197 my ($self, $wildmat) = @_;
198 wildmat2re($wildmat);
199 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
200 $ng->{name} =~ $wildmat or next;
201 my $c = eval { $ng->mm->created_at } || time;
202 more($self, "$ng->{name} $c $ng->{address}");
206 sub list_newsgroups ($;$) {
207 my ($self, $wildmat) = @_;
208 wildmat2re($wildmat);
209 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
210 $ng->{name} =~ $wildmat or next;
211 my $d = $ng->description;
212 more($self, "$ng->{name} $d");
216 # LIST SUBSCRIPTIONS, DISTRIB.PATS are not supported
217 sub cmd_list ($;$$) {
218 my ($self, @args) = @_;
220 my $arg = shift @args;
221 $arg =~ tr/A-Z./a-z_/;
223 return r501 if $DISABLED{$arg};
229 return r501 unless $arg && args_ok($arg, scalar @args);
230 more($self, '215 information follows');
231 $arg->($self, @args);
233 more($self, '215 list of newsgroups follows');
234 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
235 group_line($self, $ng);
241 sub cmd_listgroup ($;$) {
242 my ($self, $group) = @_;
243 if (defined $group) {
244 my $res = cmd_group($self, $group);
245 return $res if ($res !~ /\A211 /);
249 $self->{ng} or return '412 no newsgroup selected';
250 long_response($self, 0, long_response_limit, sub {
252 my $nr = $self->{ng}->mm->id_batch($$i, sub {
254 more($self, join("\r\n", @$ary));
257 # -1 to adjust for implicit increment in long_response
258 $$i = $nr ? $$i + $nr - 1 : long_response_limit;
262 sub parse_time ($$;$) {
263 my ($date, $time, $gmt) = @_;
264 use Time::Local qw();
265 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
267 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
270 my @now = $gmt ? gmtime : localtime;
271 my ($YYYY, $MM, $DD);
272 if (length($date) == 8) { # RFC 3977 allows YYYYMMDD
273 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
274 } else { # legacy clients send YYMMDD
275 ($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
276 if ($YYYY > strftime('%y', @now)) {
277 my $cur_year = $now[5] + 1900;
278 $YYYY += int($cur_year / 1000) * 1000 - 100;
282 Time::Local::timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
284 Time::Local::timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
288 sub group_line ($$) {
289 my ($self, $ng) = @_;
290 my ($min, $max) = $ng->mm->minmax;
291 more($self, "$ng->{name} $max $min n") if defined $min && defined $max;
294 sub cmd_newgroups ($$$;$$) {
295 my ($self, $date, $time, $gmt, $dists) = @_;
296 my $ts = eval { parse_time($date, $time, $gmt) };
300 more($self, '231 list of new newsgroups follows');
301 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
302 my $c = eval { $ng->mm->created_at } || 0;
303 next unless $c > $ts;
304 group_line($self, $ng);
309 sub wildmat2re (;$) {
310 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
313 use Digest::SHA qw(sha1_hex);
316 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
318 my $key = sha1_hex($orig . $salt);
319 $orig =~ s/([^\w\-])+/\Q$1/g;
323 my %map = ('*' => '.*', '?' => '.' );
324 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
326 $tmp =~ s#([a-f0-9]{40})#
327 my $orig = $keep{$1};
328 defined $orig ? $orig : $1;
331 $_[0] = qr/\A$tmp\z/;
335 return $_[0] = qr/\A\z/ unless defined $_[0];
336 my %map = ('*' => '.*', ',' => '|');
337 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
338 $_[0] = qr/\A(?:$_[0])\z/;
341 sub cmd_newnews ($$$$;$$) {
342 my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
343 my $ts = eval { parse_time($date, $time, $gmt) };
345 more($self, '230 list of new articles by message-id follows');
346 my ($keep, $skip) = split('!', $newsgroups, 2);
350 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
351 $ng->{name} =~ $keep or next;
352 $ng->{name} =~ $skip and next;
353 my $srch = $ng->search or next;
356 return '.' unless @srch;
359 my $opts = { asc => 1, limit => 1000, offset => 0 };
360 long_response($self, 0, long_response_limit, sub {
363 my $res = $srch->query($ts, $opts);
364 my $msgs = $res->{msgs};
365 if (my $nr = scalar @$msgs) {
367 join(">\r\n<", map { $_->mid } @$msgs ).
369 $opts->{offset} += $nr;
372 if (@srch) { # continue onto next newsgroup
374 } else { # break out of the long response.
375 $$i = long_response_limit;
382 my ($self, $group) = @_;
383 my $no_such = '411 no such news group';
384 my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
387 my ($min, $max) = $ng->mm->minmax;
390 $self->{article} = $min;
391 my $est_size = $max - $min;
392 "211 $est_size $min $max $group";
395 sub article_adj ($$) {
396 my ($self, $off) = @_;
397 my $ng = $self->{ng} or return '412 no newsgroup selected';
399 my $n = $self->{article};
400 defined $n or return '420 no current article has been selected';
403 my $mid = $ng->mm->mid_for($n);
405 $n = $off > 0 ? 'next' : 'previous';
406 return "421 no $n article in this group";
408 $self->{article} = $n;
409 "223 $n <$mid> article retrieved - request text separately";
412 sub cmd_next ($) { article_adj($_[0], 1) }
413 sub cmd_last ($) { article_adj($_[0], -1) }
415 # We want to encourage using email and CC-ing everybody involved to avoid
416 # the single-point-of-failure a single server provides.
419 my $ng = $self->{ng};
420 $ng ? "440 mailto:$ng->{address} to post" : '440 posting not allowed'
425 res($self, '205 closing connection - goodbye!');
430 sub header_append ($$$) {
431 my ($hdr, $k, $v) = @_;
432 my @v = $hdr->header($k);
436 $hdr->header_set($k, @v, $v);
439 sub set_nntp_headers {
440 my ($hdr, $ng, $n, $mid) = @_;
443 $hdr->header_set('Newsgroups', $ng->{name});
444 $hdr->header_set('Xref', xref($ng, $n));
445 header_append($hdr, 'List-Post', "<mailto:$ng->{address}>");
446 if (my $url = $ng->{url}) {
447 $mid = uri_escape_utf8($mid);
448 header_append($hdr, 'Archived-At', "<$url$mid/>");
449 header_append($hdr, 'List-Archive', "<$url>");
453 sub art_lookup ($$$) {
454 my ($self, $art, $set_headers) = @_;
455 my $ng = $self->{ng};
459 if ($art =~ /\A\d+\z/o) {
460 $err = '423 no such article number in this group';
463 } elsif ($art =~ /\A<([^>]+)>\z/) {
466 $n = $ng->mm->num_for($mid) if $ng;
467 goto found if defined $n;
468 foreach my $g (values %{$self->{nntpd}->{groups}}) {
469 $n = $g->mm->num_for($mid);
480 $err = '420 no current article has been selected';
481 $n = $self->{article};
482 defined $n or return $err;
484 $ng or return '412 no newsgroup has been selected';
485 $mid = $ng->mm->mid_for($n);
486 defined $mid or return $err;
489 my $o = 'HEAD:' . mid2path($mid);
491 my $s = eval { Email::MIME->new($ng->gcf->cat_file($o, \$bytes)) };
492 return $err unless $s;
495 set_nntp_headers($s->header_obj, $ng, $n, $mid);
496 $lines = $s->body =~ tr!\n!\n!;
499 $s->body_set('') if ($set_headers == 2);
501 [ $n, $mid, $s, $bytes, $lines, $ng ];
504 sub simple_body_write ($$) {
508 $body =~ s/^\./../smg;
509 $body =~ s/(?<!\r)\n/\r\n/sg;
510 do_more($self, $body);
511 do_more($self, "\r\n") unless $body =~ /\r\n\z/s;
516 my ($self, $art) = @_;
517 $self->{article} = $art if defined $art && $art =~ /\A\d+\z/;
520 sub cmd_article ($;$) {
521 my ($self, $art) = @_;
522 my $r = art_lookup($self, $art, 1);
523 return $r unless ref $r;
524 my ($n, $mid, $s) = @$r;
525 set_art($self, $art);
526 more($self, "220 $n <$mid> article retrieved - head and body follow");
527 do_more($self, $s->header_obj->as_string);
528 do_more($self, "\r\n");
529 simple_body_write($self, $s);
533 my ($self, $art) = @_;
534 my $r = art_lookup($self, $art, 2);
535 return $r unless ref $r;
536 my ($n, $mid, $s) = @$r;
537 set_art($self, $art);
538 more($self, "221 $n <$mid> article retrieved - head follows");
539 do_more($self, $s->header_obj->as_string);
544 my ($self, $art) = @_;
545 my $r = art_lookup($self, $art, 0);
546 return $r unless ref $r;
547 my ($n, $mid, $s) = @$r;
548 set_art($self, $art);
549 more($self, "222 $n <$mid> article retrieved - body follows");
550 simple_body_write($self, $s);
554 my ($self, $art) = @_;
555 my $r = art_lookup($self, $art, 0);
556 return $r unless ref $r;
557 my ($n, $mid, undef) = @$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(\d+)\z/) {
580 } elsif ($range =~ /\A(\d+)-\z/) {
581 ($beg, $end) = ($1, $max);
582 } elsif ($range =~ /\A(\d+)-(\d+)\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);
593 sub long_response ($$$$) {
594 my ($self, $beg, $end, $cb) = @_;
595 die "BUG: nested long response" if $self->{long_res};
597 my $fd = $self->{fd};
598 defined $fd or return;
599 # make sure we disable reading during a long response,
600 # clients should not be sending us stuff and making us do more
601 # work while we are stream a response to them
602 $self->watch_read(0);
604 $self->{long_res} = sub {
605 # limit our own running time for fairness with other
606 # clients and to avoid buffering too much:
611 eval { $cb->(\$beg) };
612 } until (($err = $@) || $self->{closed} ||
613 ++$beg > $end || !--$lim || $self->{write_buf_size});
615 if ($err || $self->{closed}) {
616 $self->{long_res} = undef;
620 "%s during long response[$fd] - %0.6f",
623 if ($self->{closed}) {
624 out($self, " deferred[$fd] aborted - %0.6f",
627 update_idle_time($self);
628 $self->watch_read(1);
630 } elsif (!$lim || $self->{write_buf_size}) {
631 # no recursion, schedule another call ASAP
632 # but only after all pending writes are done
633 update_idle_time($self);
636 $next_tick ||= Danga::Socket->AddTimer(0, *next_tick);
638 $self->{long_res} = undef;
639 $self->watch_read(1);
641 out($self, " deferred[$fd] done - %0.6f", now() - $t0);
644 $self->{long_res}->(); # kick off!
648 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
649 my ($self, $xhdr, $range) = @_;
651 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
652 my ($ng, $n) = mid_lookup($self, $1);
653 return r430 unless $n;
654 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
655 } else { # numeric range
656 $range = $self->{article} unless defined $range;
657 my $r = get_range($self, $range);
658 return $r unless ref $r;
659 my $mm = $self->{ng}->mm;
660 my ($beg, $end) = @$r;
661 more($self, $xhdr ? r221 : r225);
662 long_response($self, $beg, $end, sub {
664 my $mid = $mm->mid_for($$i);
665 more($self, "$$i <$mid>") if defined $mid;
672 "$ng->{domain} $ng->{name}:$n"
675 sub mid_lookup ($$) {
676 my ($self, $mid) = @_;
677 my $self_ng = $self->{ng};
679 my $n = $self_ng->mm->num_for($mid);
680 return ($self_ng, $n) if defined $n;
682 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
683 next if defined $self_ng && $ng eq $self_ng;
684 my $n = $ng->mm->num_for($mid);
685 return ($ng, $n) if defined $n;
690 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
691 my ($self, $xhdr, $range) = @_;
693 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
694 my ($ng, $n) = mid_lookup($self, $1);
695 return r430 unless $n;
696 hdr_mid_response($self, $xhdr, $ng, $n, $range, xref($ng, $n));
697 } else { # numeric range
698 $range = $self->{article} unless defined $range;
699 my $r = get_range($self, $range);
700 return $r unless ref $r;
701 my $ng = $self->{ng};
703 my ($beg, $end) = @$r;
704 more($self, $xhdr ? r221 : r225);
705 long_response($self, $beg, $end, sub {
707 my $mid = $mm->mid_for($$i);
708 more($self, "$$i ".xref($ng, $$i)) if defined $mid;
713 sub search_header_for {
714 my ($srch, $mid, $field) = @_;
715 my $smsg = $srch->lookup_message($mid) or return;
716 $smsg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
720 sub hdr_searchmsg ($$$$) {
721 my ($self, $xhdr, $field, $range) = @_;
722 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
723 my ($ng, $n) = mid_lookup($self, $1);
724 return r430 unless $n;
725 my $v = search_header_for($ng->search, $range, $field);
726 hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
727 } else { # numeric range
728 $range = $self->{article} unless defined $range;
729 my $srch = $self->{ng}->search;
730 my $mm = $self->{ng}->mm;
731 my $r = get_range($self, $range);
732 return $r unless ref $r;
733 my ($beg, $end) = @$r;
734 more($self, $xhdr ? r221 : r225);
736 long_response($self, $beg, $end, sub {
738 my $res = $srch->query_xover($beg, $end, $off);
739 my $msgs = $res->{msgs};
740 my $nr = scalar @$msgs or return;
743 foreach my $s (@$msgs) {
744 $tmp .= $s->num . ' ' . $s->$field . "\r\n";
746 do_more($self, $tmp);
747 # -1 to adjust for implicit increment in long_response
748 $$i = $nr ? $$i + $nr - 1 : long_response_limit;
754 my ($self, $xhdr, $header, $range) = @_;
755 my $sub = lc $header;
756 if ($sub eq 'message-id') {
757 hdr_message_id($self, $xhdr, $range);
758 } elsif ($sub eq 'xref') {
759 hdr_xref($self, $xhdr, $range);
760 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
762 hdr_searchmsg($self, $xhdr, $sub, $range);
763 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
764 hdr_searchmsg($self, $xhdr, $1, $range);
766 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
772 my ($self, $header, $range) = @_;
773 do_hdr($self, 0, $header, $range);
777 sub cmd_xhdr ($$;$) {
778 my ($self, $header, $range) = @_;
779 do_hdr($self, 1, $header, $range);
782 sub hdr_mid_prefix ($$$$$) {
783 my ($self, $xhdr, $ng, $n, $mid) = @_;
784 return $mid if $xhdr;
786 # HDR for RFC 3977 users
787 if (my $self_ng = $self->{ng}) {
788 ($self_ng eq $ng) ? $n : '0';
794 sub hdr_mid_response ($$$$$$) {
795 my ($self, $xhdr, $ng, $n, $mid, $v) = @_; # r: art_lookup result
798 $res .= r221 . "\r\n";
799 $res .= "$mid $v\r\n";
801 $res .= r225 . "\r\n";
802 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
803 $res .= "$pfx $v\r\n";
805 res($self, $res .= '.');
809 sub cmd_xrover ($;$) {
810 my ($self, $range) = @_;
811 my $ng = $self->{ng} or return '412 no newsgroup selected';
812 (defined $range && $range =~ /[<>]/) and
813 return '420 No article(s) selected'; # no message IDs
815 $range = $self->{article} unless defined $range;
816 my $r = get_range($self, $range);
817 return $r unless ref $r;
818 my ($beg, $end) = @$r;
820 my $srch = $ng->search;
821 more($self, '224 Overview information follows');
822 long_response($self, $beg, $end, sub {
824 my $mid = $mm->mid_for($$i) or return;
825 my $h = search_header_for($srch, $mid, 'references');
826 more($self, "$$i $h");
831 my ($num, $smsg) = @_;
832 # n.b. field access and procedural calls can be
833 # 10%-15% faster than OO method calls:
837 PublicInbox::SearchMsg::date($smsg),
838 '<'.PublicInbox::SearchMsg::mid($smsg).'>',
840 PublicInbox::SearchMsg::bytes($smsg),
841 PublicInbox::SearchMsg::lines($smsg));
845 my ($self, $range) = @_;
846 if ($range && $range =~ /\A<(.+)>\z/) {
847 my ($ng, $n) = mid_lookup($self, $1);
848 my $smsg = $ng->search->lookup_message($range) or
849 return '430 No article with that message-id';
850 more($self, '224 Overview information follows (multi-line)');
851 $smsg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
853 # Only set article number column if it's the current group
854 my $self_ng = $self->{ng};
855 $n = 0 if (!$self_ng || $self_ng ne $ng);
856 more($self, over_line($n, $smsg));
859 cmd_xover($self, $range);
863 sub cmd_xover ($;$) {
864 my ($self, $range) = @_;
865 $range = $self->{article} unless defined $range;
866 my $r = get_range($self, $range);
867 return $r unless ref $r;
868 my ($beg, $end) = @$r;
869 more($self, "224 Overview information follows for $beg to $end");
870 my $srch = $self->{ng}->search;
872 long_response($self, $beg, $end, sub {
874 my $res = $srch->query_xover($beg, $end, $off);
875 my $msgs = $res->{msgs};
876 my $nr = scalar @$msgs or return;
880 more($self, join("\r\n", map {
881 over_line(PublicInbox::SearchMsg::num($_), $_);
884 # -1 to adjust for implicit increment in long_response
885 $$i = $nr ? $$i + $nr - 1 : long_response_limit;
890 my ($self, $mid) = @_;
891 return r501 unless $mid =~ /\A<(.+)>\z/;
894 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
895 my $n = $ng->mm->num_for($mid);
896 push @paths, "$ng->{name}/$n" if defined $n;
898 return '430 no such article on server' unless @paths;
899 '223 '.join(' ', @paths);
903 my ($self, $line) = @_;
904 do_write($self, $line . "\r\n");
908 my ($self, $line) = @_;
909 do_more($self, $line . "\r\n");
913 my ($self, $data) = @_;
914 my $done = $self->write($data);
915 die if $self->{closed};
917 # Do not watch for readability if we have data in the queue,
918 # instead re-enable watching for readability when we can
919 $self->watch_read(0) if (!$done || $self->{long_res});
925 my ($self, $fmt, @args) = @_;
926 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
930 my ($self, $fmt, @args) = @_;
931 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
934 use constant MSG_MORE => ($^O eq 'linux') ? 0x8000 : 0;
937 my ($self, $data) = @_;
938 if (MSG_MORE && !$self->{write_buf_size}) {
939 my $n = send($self->{sock}, $data, MSG_MORE);
941 my $dlen = length($data);
942 return 1 if $n == $dlen; # all done!
943 $data = substr($data, $n, $dlen - $n);
946 do_write($self, $data);
949 # callbacks for Danga::Socket
951 sub event_hup { $_[0]->close }
952 sub event_err { $_[0]->close }
956 update_idle_time($self);
957 # only continue watching for readability when we are done writing:
958 if ($self->write(undef) == 1 && !$self->{long_res}) {
959 $self->watch_read(1);
965 use constant LINE_MAX => 512; # RFC 977 section 2.3
968 my $buf = $self->read(LINE_MAX) or return $self->close;
969 $self->{rbuf} .= $$buf;
970 while ($r > 0 && $self->{rbuf} =~ s/\A\s*([^\r\n]+)\r?\n//) {
973 my $fd = $self->{fd};
974 $r = eval { process_line($self, $line) };
975 my $d = $self->{long_res} ?
976 " deferred[$fd]" : '';
977 out($self, "[$fd] %s - %0.6f$d", $line, now() - $t0);
980 return $self->close if $r < 0;
981 my $len = length($self->{rbuf});
982 return $self->close if ($len >= LINE_MAX);
983 update_idle_time($self);
987 my ($self, $bool) = @_;
988 my $rv = $self->SUPER::watch_read($bool);
989 if ($bool && $self->{rbuf} ne '') {
990 # Force another read if there is a pipelined request.
991 # We don't know if the socket has anything for us to read,
992 # and we must double-check again by the time the timer fires
993 # in case we really did dispatch a read event and started
994 # another long response.
996 $next_tick ||= Danga::Socket->AddTimer(0, *next_tick);
1001 # for graceful shutdown in PublicInbox::Daemon:
1004 ($self->{rbuf} ne '' || $self->{long_res} || $self->{write_buf_size});