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;
12 use PublicInbox::MID qw(mid_escape);
14 require PublicInbox::EvCleanup;
16 use POSIX qw(strftime);
17 use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
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',
26 long_response_limit => 0xffffffff,
29 sub now () { clock_gettime(CLOCK_MONOTONIC) };
31 my @OVERVIEW = qw(Subject From Date Message-ID References);
32 my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines)) . ":\r\n";
33 my $LIST_HEADERS = join("\r\n", @OVERVIEW,
34 qw(:bytes :lines Xref To Cc)) . "\r\n";
36 # disable commands with easy DoS potential:
37 # LISTGROUP could get pretty bad, too...
38 my %DISABLED; # = map { $_ => 1 } qw(xover list_overview_fmt newnews xhdr);
40 my $EXPMAP; # fd -> [ idle_time, $self ]
42 our $EXPTIME = 180; # 3 minutes
50 foreach my $nntp (@$q) {
51 # for request && response protocols, always finish writing
52 # before finishing reading:
53 if (my $long_cb = $nntp->{long_res}) {
54 $nntp->write($long_cb);
55 } elsif (&Danga::Socket::POLLIN & $nntp->{event_watch}) {
61 sub update_idle_time ($) {
64 defined $fd and $EXPMAP->{$fd} = [ 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);
97 $self->{nntpd} = $nntpd;
98 res($self, '201 server ready - post via email');
100 $self->watch_read(1);
101 update_idle_time($self);
102 $expt ||= PublicInbox::EvCleanup::later(*expire_old);
107 my ($cb, $argc) = @_;
108 my $tot = prototype $cb;
109 my ($nreq, undef) = split(';', $tot);
110 $nreq = ($nreq =~ tr/$//) - 1;
111 $tot = ($tot =~ tr/$//) - 1;
112 ($argc <= $tot && $argc >= $nreq);
115 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
116 sub process_line ($$) {
118 my ($req, @args) = split(/\s+/, $l);
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->{closed}) {
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 do_more($self, $OVERVIEW_FMT);
160 sub list_headers ($;$) {
162 do_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';
228 long_response($self, 0, long_response_limit, sub {
230 my $nr = $self->{ng}->mm->id_batch($$i, sub {
232 more($self, join("\r\n", @$ary));
235 # -1 to adjust for implicit increment in long_response
236 $$i = $nr ? $$i + $nr - 1 : long_response_limit;
240 sub parse_time ($$;$) {
241 my ($date, $time, $gmt) = @_;
242 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
244 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
247 my @now = $gmt ? gmtime : localtime;
248 my ($YYYY, $MM, $DD);
249 if (length($date) == 8) { # RFC 3977 allows YYYYMMDD
250 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
251 } else { # legacy clients send YYMMDD
252 ($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
253 my $cur_year = $now[5] + 1900;
254 if ($YYYY > $cur_year) {
255 $YYYY += int($cur_year / 1000) * 1000 - 100;
259 timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
261 timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
265 sub group_line ($$) {
266 my ($self, $ng) = @_;
267 my ($min, $max) = $ng->mm->minmax;
268 more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
271 sub cmd_newgroups ($$$;$$) {
272 my ($self, $date, $time, $gmt, $dists) = @_;
273 my $ts = eval { parse_time($date, $time, $gmt) };
277 more($self, '231 list of new newsgroups follows');
278 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
279 my $c = eval { $ng->mm->created_at } || 0;
280 next unless $c > $ts;
281 group_line($self, $ng);
286 sub wildmat2re (;$) {
287 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
292 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
294 my $key = sha1_hex($orig . $salt);
295 $orig =~ s/([^\w\-])+/\Q$1/g;
299 my %map = ('*' => '.*', '?' => '.' );
300 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
302 $tmp =~ s#([a-f0-9]{40})#
303 my $orig = $keep{$1};
304 defined $orig ? $orig : $1;
307 $_[0] = qr/\A$tmp\z/;
311 return $_[0] = qr/\A\z/ unless defined $_[0];
312 my %map = ('*' => '.*', ',' => '|');
313 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
314 $_[0] = qr/\A(?:$_[0])\z/;
317 sub cmd_newnews ($$$$;$$) {
318 my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
319 my $ts = eval { parse_time($date, $time, $gmt) };
321 more($self, '230 list of new articles by message-id follows');
322 my ($keep, $skip) = split('!', $newsgroups, 2);
326 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
327 $ng->{newsgroup} =~ $keep or next;
328 $ng->{newsgroup} =~ $skip and next;
329 my $srch = $ng->search or next;
332 return '.' unless @srch;
335 my $opts = { asc => 1, limit => 1000, offset => 0 };
336 long_response($self, 0, long_response_limit, sub {
339 my $res = $srch->query($ts, $opts);
340 my $msgs = $res->{msgs};
341 if (my $nr = scalar @$msgs) {
343 join(">\r\n<", map { $_->mid } @$msgs ).
345 $opts->{offset} += $nr;
348 if (@srch) { # continue onto next newsgroup
350 } else { # break out of the long response.
351 $$i = long_response_limit;
358 my ($self, $group) = @_;
359 my $no_such = '411 no such news group';
360 my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
363 my ($min, $max) = $ng->mm->minmax;
366 $self->{article} = $min;
367 my $est_size = $max - $min;
368 "211 $est_size $min $max $group";
371 sub article_adj ($$) {
372 my ($self, $off) = @_;
373 my $ng = $self->{ng} or return '412 no newsgroup selected';
375 my $n = $self->{article};
376 defined $n or return '420 no current article has been selected';
379 my $mid = $ng->mm->mid_for($n);
381 $n = $off > 0 ? 'next' : 'previous';
382 return "421 no $n article in this group";
384 $self->{article} = $n;
385 "223 $n <$mid> article retrieved - request text separately";
388 sub cmd_next ($) { article_adj($_[0], 1) }
389 sub cmd_last ($) { article_adj($_[0], -1) }
391 # We want to encourage using email and CC-ing everybody involved to avoid
392 # the single-point-of-failure a single server provides.
395 my $ng = $self->{ng};
396 $ng ? "440 mailto:$ng->{-primary_address} to post"
397 : '440 posting not allowed'
402 res($self, '205 closing connection - goodbye!');
407 sub header_append ($$$) {
408 my ($hdr, $k, $v) = @_;
409 my @v = $hdr->header($k);
413 $hdr->header_set($k, @v, $v);
416 sub set_nntp_headers {
417 my ($hdr, $ng, $n, $mid) = @_;
420 $hdr->header_set('Newsgroups', $ng->{newsgroup});
421 $hdr->header_set('Xref', xref($ng, $n));
422 header_append($hdr, 'List-Post', "<mailto:$ng->{-primary_address}>");
423 if (my $url = $ng->base_url) {
424 $mid = mid_escape($mid);
425 header_append($hdr, 'Archived-At', "<$url$mid/>");
426 header_append($hdr, 'List-Archive', "<$url>");
430 sub art_lookup ($$$) {
431 my ($self, $art, $set_headers) = @_;
432 my $ng = $self->{ng};
436 if ($art =~ /\A\d+\z/o) {
437 $err = '423 no such article number in this group';
440 } elsif ($art =~ /\A<([^>]+)>\z/) {
443 $n = $ng->mm->num_for($mid) if $ng;
444 goto found if defined $n;
445 foreach my $g (values %{$self->{nntpd}->{groups}}) {
446 $n = $g->mm->num_for($mid);
457 $err = '420 no current article has been selected';
458 $n = $self->{article};
459 defined $n or return $err;
461 $ng or return '412 no newsgroup has been selected';
462 $mid = $ng->mm->mid_for($n);
463 defined $mid or return $err;
467 my $s = eval { $ng->msg_by_mid($mid, \$bytes) } or return $err;
468 $s = Email::Simple->new($s);
471 set_nntp_headers($s->header_obj, $ng, $n, $mid);
472 $lines = $s->body =~ tr!\n!\n!;
475 $s->body_set('') if ($set_headers == 2);
477 [ $n, $mid, $s, $bytes, $lines, $ng ];
480 sub simple_body_write ($$) {
484 $body =~ s/^\./../smg;
485 $body =~ s/(?<!\r)\n/\r\n/sg;
486 do_more($self, $body);
487 do_more($self, "\r\n") unless $body =~ /\r\n\z/s;
492 my ($self, $art) = @_;
493 $self->{article} = $art if defined $art && $art =~ /\A\d+\z/;
497 my $hdr = $_[0]->header_obj->as_string;
502 sub cmd_article ($;$) {
503 my ($self, $art) = @_;
504 my $r = art_lookup($self, $art, 1);
505 return $r unless ref $r;
506 my ($n, $mid, $s) = @$r;
507 set_art($self, $art);
508 more($self, "220 $n <$mid> article retrieved - head and body follow");
509 do_more($self, _header($s));
510 do_more($self, "\r\n");
511 simple_body_write($self, $s);
515 my ($self, $art) = @_;
516 my $r = art_lookup($self, $art, 2);
517 return $r unless ref $r;
518 my ($n, $mid, $s) = @$r;
519 set_art($self, $art);
520 more($self, "221 $n <$mid> article retrieved - head follows");
521 do_more($self, _header($s));
526 my ($self, $art) = @_;
527 my $r = art_lookup($self, $art, 0);
528 return $r unless ref $r;
529 my ($n, $mid, $s) = @$r;
530 set_art($self, $art);
531 more($self, "222 $n <$mid> article retrieved - body follows");
532 simple_body_write($self, $s);
536 my ($self, $art) = @_;
537 my $r = art_lookup($self, $art, 0);
538 return $r unless ref $r;
539 my ($n, $mid, undef) = @$r;
540 set_art($self, $art);
541 "223 $n <$mid> article retrieved - request text separately";
544 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
546 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
550 more($self, '100 help text follows');
555 my ($self, $range) = @_;
556 my $ng = $self->{ng} or return '412 no news group has been selected';
557 defined $range or return '420 No article(s) selected';
559 my ($min, $max) = $ng->mm->minmax;
560 if ($range =~ /\A(\d+)\z/) {
562 } elsif ($range =~ /\A(\d+)-\z/) {
563 ($beg, $end) = ($1, $max);
564 } elsif ($range =~ /\A(\d+)-(\d+)\z/) {
565 ($beg, $end) = ($1, $2);
569 $beg = $min if ($beg < $min);
570 $end = $max if ($end > $max);
571 return '420 No article(s) selected' if ($beg > $end);
575 sub long_response ($$$$) {
576 my ($self, $beg, $end, $cb) = @_;
577 die "BUG: nested long response" if $self->{long_res};
579 my $fd = $self->{fd};
580 defined $fd or return;
581 # make sure we disable reading during a long response,
582 # clients should not be sending us stuff and making us do more
583 # work while we are stream a response to them
584 $self->watch_read(0);
586 $self->{long_res} = sub {
587 # limit our own running time for fairness with other
588 # clients and to avoid buffering too much:
593 eval { $cb->(\$beg) };
594 } until (($err = $@) || $self->{closed} ||
595 ++$beg > $end || !--$lim || $self->{write_buf_size});
597 if ($err || $self->{closed}) {
598 $self->{long_res} = undef;
602 "%s during long response[$fd] - %0.6f",
605 if ($self->{closed}) {
606 out($self, " deferred[$fd] aborted - %0.6f",
609 update_idle_time($self);
610 $self->watch_read(1);
612 } elsif (!$lim || $self->{write_buf_size}) {
613 # no recursion, schedule another call ASAP
614 # but only after all pending writes are done
615 update_idle_time($self);
618 $nextt ||= PublicInbox::EvCleanup::asap(*next_tick);
620 $self->{long_res} = undef;
621 $self->watch_read(1);
623 out($self, " deferred[$fd] done - %0.6f", now() - $t0);
626 $self->{long_res}->(); # kick off!
630 sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
631 my ($self, $xhdr, $range) = @_;
633 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
634 my ($ng, $n) = mid_lookup($self, $1);
635 return r430 unless $n;
636 hdr_mid_response($self, $xhdr, $ng, $n, $range, $range);
637 } else { # numeric range
638 $range = $self->{article} unless defined $range;
639 my $r = get_range($self, $range);
640 return $r unless ref $r;
641 my $mm = $self->{ng}->mm;
642 my ($beg, $end) = @$r;
643 more($self, $xhdr ? r221 : r225);
644 long_response($self, $beg, $end, sub {
646 my $mid = $mm->mid_for($$i);
647 more($self, "$$i <$mid>") if defined $mid;
654 "$ng->{domain} $ng->{newsgroup}:$n"
657 sub mid_lookup ($$) {
658 my ($self, $mid) = @_;
659 my $self_ng = $self->{ng};
661 my $n = $self_ng->mm->num_for($mid);
662 return ($self_ng, $n) if defined $n;
664 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
665 next if defined $self_ng && $ng eq $self_ng;
666 my $n = $ng->mm->num_for($mid);
667 return ($ng, $n) if defined $n;
672 sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
673 my ($self, $xhdr, $range) = @_;
675 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
676 my ($ng, $n) = mid_lookup($self, $1);
677 return r430 unless $n;
678 hdr_mid_response($self, $xhdr, $ng, $n, $range, xref($ng, $n));
679 } else { # numeric range
680 $range = $self->{article} unless defined $range;
681 my $r = get_range($self, $range);
682 return $r unless ref $r;
683 my $ng = $self->{ng};
685 my ($beg, $end) = @$r;
686 more($self, $xhdr ? r221 : r225);
687 long_response($self, $beg, $end, sub {
689 my $mid = $mm->mid_for($$i);
690 more($self, "$$i ".xref($ng, $$i)) if defined $mid;
695 sub search_header_for {
696 my ($srch, $mid, $field) = @_;
697 my $smsg = $srch->lookup_mail($mid) or return;
701 sub hdr_searchmsg ($$$$) {
702 my ($self, $xhdr, $field, $range) = @_;
703 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
704 my ($ng, $n) = mid_lookup($self, $1);
705 return r430 unless $n;
706 my $v = search_header_for($ng->search, $range, $field);
707 hdr_mid_response($self, $xhdr, $ng, $n, $range, $v);
708 } else { # numeric range
709 $range = $self->{article} unless defined $range;
710 my $srch = $self->{ng}->search;
711 my $mm = $self->{ng}->mm;
712 my $r = get_range($self, $range);
713 return $r unless ref $r;
714 my ($beg, $end) = @$r;
715 more($self, $xhdr ? r221 : r225);
717 long_response($self, $beg, $end, sub {
719 my $res = $srch->query_xover($beg, $end, $off);
720 my $msgs = $res->{msgs};
721 my $nr = scalar @$msgs or return;
724 foreach my $s (@$msgs) {
725 $tmp .= $s->num . ' ' . $s->$field . "\r\n";
728 do_more($self, $tmp);
729 # -1 to adjust for implicit increment in long_response
730 $$i = $nr ? $$i + $nr - 1 : long_response_limit;
736 my ($self, $xhdr, $header, $range) = @_;
737 my $sub = lc $header;
738 if ($sub eq 'message-id') {
739 hdr_message_id($self, $xhdr, $range);
740 } elsif ($sub eq 'xref') {
741 hdr_xref($self, $xhdr, $range);
742 } elsif ($sub =~ /\A(?:subject|references|date|from|to|cc|
744 hdr_searchmsg($self, $xhdr, $sub, $range);
745 } elsif ($sub =~ /\A:(bytes|lines)\z/) {
746 hdr_searchmsg($self, $xhdr, $1, $range);
748 $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
754 my ($self, $header, $range) = @_;
755 do_hdr($self, 0, $header, $range);
759 sub cmd_xhdr ($$;$) {
760 my ($self, $header, $range) = @_;
761 do_hdr($self, 1, $header, $range);
764 sub hdr_mid_prefix ($$$$$) {
765 my ($self, $xhdr, $ng, $n, $mid) = @_;
766 return $mid if $xhdr;
768 # HDR for RFC 3977 users
769 if (my $self_ng = $self->{ng}) {
770 ($self_ng eq $ng) ? $n : '0';
776 sub hdr_mid_response ($$$$$$) {
777 my ($self, $xhdr, $ng, $n, $mid, $v) = @_; # r: art_lookup result
780 $res .= r221 . "\r\n";
781 $res .= "$mid $v\r\n";
783 $res .= r225 . "\r\n";
784 my $pfx = hdr_mid_prefix($self, $xhdr, $ng, $n, $mid);
785 $res .= "$pfx $v\r\n";
787 res($self, $res .= '.');
791 sub cmd_xrover ($;$) {
792 my ($self, $range) = @_;
793 my $ng = $self->{ng} or return '412 no newsgroup selected';
794 (defined $range && $range =~ /[<>]/) and
795 return '420 No article(s) selected'; # no message IDs
797 $range = $self->{article} unless defined $range;
798 my $r = get_range($self, $range);
799 return $r unless ref $r;
800 my ($beg, $end) = @$r;
802 my $srch = $ng->search;
803 more($self, '224 Overview information follows');
804 long_response($self, $beg, $end, sub {
806 my $mid = $mm->mid_for($$i) or return;
807 my $h = search_header_for($srch, $mid, 'references');
808 more($self, "$$i $h");
813 my ($num, $smsg) = @_;
814 # n.b. field access and procedural calls can be
815 # 10%-15% faster than OO method calls:
816 my $s = join("\t", $num,
819 PublicInbox::SearchMsg::date($smsg),
820 '<'.PublicInbox::SearchMsg::mid($smsg).'>',
822 PublicInbox::SearchMsg::bytes($smsg),
823 PublicInbox::SearchMsg::lines($smsg));
829 my ($self, $range) = @_;
830 if ($range && $range =~ /\A<(.+)>\z/) {
831 my ($ng, $n) = mid_lookup($self, $1);
832 my $smsg = $ng->search->lookup_mail($range) or
833 return '430 No article with that message-id';
834 more($self, '224 Overview information follows (multi-line)');
836 # Only set article number column if it's the current group
837 my $self_ng = $self->{ng};
838 $n = 0 if (!$self_ng || $self_ng ne $ng);
839 more($self, over_line($n, $smsg));
842 cmd_xover($self, $range);
846 sub cmd_xover ($;$) {
847 my ($self, $range) = @_;
848 $range = $self->{article} unless defined $range;
849 my $r = get_range($self, $range);
850 return $r unless ref $r;
851 my ($beg, $end) = @$r;
852 more($self, "224 Overview information follows for $beg to $end");
853 my $srch = $self->{ng}->search;
855 long_response($self, $beg, $end, sub {
857 my $res = $srch->query_xover($beg, $end, $off);
858 my $msgs = $res->{msgs};
859 my $nr = scalar @$msgs or return;
863 more($self, join("\r\n", map {
864 over_line(PublicInbox::SearchMsg::num($_), $_);
867 # -1 to adjust for implicit increment in long_response
868 $$i = $nr ? $$i + $nr - 1 : long_response_limit;
873 my ($self, $mid) = @_;
874 return r501 unless $mid =~ /\A<(.+)>\z/;
877 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
878 my $n = $ng->mm->num_for($mid);
879 push @paths, "$ng->{newsgroup}/$n" if defined $n;
881 return '430 no such article on server' unless @paths;
882 '223 '.join(' ', @paths);
886 my ($self, $line) = @_;
887 do_write($self, $line . "\r\n");
891 my ($self, $line) = @_;
892 do_more($self, $line . "\r\n");
896 my ($self, $data) = @_;
897 my $done = $self->write($data);
898 return if $self->{closed};
900 # Do not watch for readability if we have data in the queue,
901 # instead re-enable watching for readability when we can
902 $self->watch_read(0) if (!$done || $self->{long_res});
908 my ($self, $fmt, @args) = @_;
909 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
913 my ($self, $fmt, @args) = @_;
914 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
917 use constant MSG_MORE => ($^O eq 'linux') ? 0x8000 : 0;
920 my ($self, $data) = @_;
921 if (MSG_MORE && !$self->{write_buf_size}) {
922 my $n = send($self->{sock}, $data, MSG_MORE);
924 my $dlen = length($data);
925 return 1 if $n == $dlen; # all done!
926 $data = substr($data, $n, $dlen - $n);
929 do_write($self, $data);
932 # callbacks for Danga::Socket
934 sub event_hup { $_[0]->close }
935 sub event_err { $_[0]->close }
939 update_idle_time($self);
940 # only continue watching for readability when we are done writing:
941 if ($self->write(undef) == 1 && !$self->{long_res}) {
942 $self->watch_read(1);
948 use constant LINE_MAX => 512; # RFC 977 section 2.3
951 my $buf = $self->read(LINE_MAX) or return $self->close;
952 $self->{rbuf} .= $$buf;
953 while ($r > 0 && $self->{rbuf} =~ s/\A\s*([^\r\n]+)\r?\n//) {
955 return $self->close if $line =~ /[[:cntrl:]]/s;
957 my $fd = $self->{fd};
958 $r = eval { process_line($self, $line) };
959 my $d = $self->{long_res} ?
960 " deferred[$fd]" : '';
961 out($self, "[$fd] %s - %0.6f$d", $line, now() - $t0);
964 return $self->close if $r < 0;
965 my $len = length($self->{rbuf});
966 return $self->close if ($len >= LINE_MAX);
967 update_idle_time($self);
971 my ($self, $bool) = @_;
972 my $rv = $self->SUPER::watch_read($bool);
973 if ($bool && $self->{rbuf} ne '') {
974 # Force another read if there is a pipelined request.
975 # We don't know if the socket has anything for us to read,
976 # and we must double-check again by the time the timer fires
977 # in case we really did dispatch a read event and started
978 # another long response.
980 $nextt ||= PublicInbox::EvCleanup::asap(*next_tick);
985 sub not_idle_long ($$) {
986 my ($self, $now) = @_;
987 defined(my $fd = $self->{fd}) or return;
988 my $ary = $EXPMAP->{$fd} or return;
989 my $exp_at = $ary->[0] + $EXPTIME;
993 # for graceful shutdown in PublicInbox::Daemon:
995 my ($self, $now) = @_;
996 ($self->{rbuf} ne '' || $self->{long_res} || $self->{write_buf_size} ||
997 not_idle_long($self, $now));