]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/NNTP.pm
nntp: start adding CRLF to responses natively
[public-inbox.git] / lib / PublicInbox / NNTP.pm
index aea04c05464b28b30d5e8d7a713e5da0446c9282..1f9058ca2b886604a99c923200bc36b974fa23b6 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Each instance of this represents a NNTP client socket
@@ -9,6 +9,7 @@
 # long_cb: long_response private data
 package PublicInbox::NNTP;
 use strict;
+use v5.10.1;
 use parent qw(PublicInbox::DS);
 use PublicInbox::MID qw(mid_escape $MID_EXTRACT);
 use PublicInbox::Eml;
@@ -23,9 +24,9 @@ use constant {
        LINE_MAX => 512, # RFC 977 section 2.3
        r501 => '501 command syntax error',
        r502 => '502 Command unavailable',
-       r221 => '221 Header follows',
+       r221 => "221 Header follows\r\n",
        r224 => '224 Overview information follows (multi-line)',
-       r225 => '225 Headers follow (multi-line)',
+       r225 => "225 Headers follow (multi-line)\r\n",
        r430 => '430 No article with that message-id',
 };
 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
@@ -64,14 +65,13 @@ sub new ($$$) {
        } else {
                greet($self);
        }
-       $self->update_idle_time;
        $self;
 }
 
 sub args_ok ($$) {
        my ($cb, $argc) = @_;
        my $tot = prototype $cb;
-       my ($nreq, undef) = split(';', $tot);
+       my ($nreq, undef) = split(/;/, $tot);
        $nreq = ($nreq =~ tr/$//) - 1;
        $tot = ($tot =~ tr/$//) - 1;
        ($argc <= $tot && $argc >= $nreq);
@@ -82,8 +82,8 @@ sub process_line ($$) {
        my ($self, $l) = @_;
        my ($req, @args) = split(/[ \t]+/, $l);
        return 1 unless defined($req); # skip blank line
-       $req = $self->can('cmd_'.lc($req));
-       return res($self, '500 command not recognized') unless $req;
+       $req = $self->can('cmd_'.lc($req)) //
+               return $self->write(\"500 command not recognized\r\n");
        return res($self, r501) unless args_ok($req, scalar @args);
 
        my $res = eval { $req->($self, @args) };
@@ -210,7 +210,7 @@ sub listgroup_range_i {
 
 sub listgroup_all_i {
        my ($self, $num) = @_;
-       my $ary = $self->{ibx}->mm(1)->ids_after($num);
+       my $ary = $self->{ibx}->over(1)->ids_after($num);
        scalar(@$ary) or return;
        more($self, join("\r\n", @$ary));
        1;
@@ -349,7 +349,7 @@ sub cmd_newnews ($$$$;$$) {
        my $ts = eval { parse_time($date, $time, $gmt) };
        return r501 if $@;
        more($self, '230 list of new articles by message-id follows');
-       my ($keep, $skip) = split('!', $newsgroups, 2);
+       my ($keep, $skip) = split(/!/, $newsgroups, 2);
        ngpat2re($keep);
        ngpat2re($skip);
        my @names = grep(!/$skip/, grep(/$keep/,
@@ -381,11 +381,10 @@ sub article_adj ($$) {
        defined $n or return '420 no current article has been selected';
 
        $n += $off;
-       my $mid = $ibx->mm(1)->mid_for($n);
-       unless ($mid) {
+       my $mid = $ibx->mm(1)->mid_for($n) // do {
                $n = $off > 0 ? 'next' : 'previous';
                return "421 no $n article in this group";
-       }
+       };
        $self->{article} = $n;
        "223 $n <$mid> article retrieved - request text separately";
 }
@@ -404,7 +403,7 @@ sub cmd_post ($) {
 
 sub cmd_quit ($) {
        my ($self) = @_;
-       res($self, '205 closing connection - goodbye!');
+       $self->write(\"205 closing connection - goodbye!\r\n");
        $self->shutdn;
        undef;
 }
@@ -651,8 +650,6 @@ sub long_step {
                out($self, " deferred[$fd] aborted - %0.6f", $elapsed);
                $self->close;
        } elsif ($more) { # $self->{wbuf}:
-               $self->update_idle_time;
-
                # COMPRESS users all share the same DEFLATE context.
                # Flush it here to ensure clients don't see
                # each other's data
@@ -666,7 +663,7 @@ sub long_step {
                $self->requeue if $new_size == 1;
        } else { # all done!
                delete $self->{long_cb};
-               res($self, '.');
+               $self->write(\".\r\n");
                my $elapsed = now() - $t0;
                my $fd = fileno($self->{sock});
                out($self, " deferred[$fd] done - %0.6f", $elapsed);
@@ -706,7 +703,7 @@ sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
                $range = $self->{article} unless defined $range;
                my $r = get_range($self, $range);
                return $r unless ref $r;
-               more($self, $xhdr ? r221 : r225);
+               $self->msg_more($xhdr ? r221 : r225);
                long_response($self, \&hdr_msgid_range_i, @$r);
        }
 }
@@ -778,7 +775,7 @@ sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
                $range = $self->{article} unless defined $range;
                my $r = get_range($self, $range);
                return $r unless ref $r;
-               more($self, $xhdr ? r221 : r225);
+               $self->msg_more($xhdr ? r221 : r225);
                long_response($self, \&xref_range_i, @$r);
        }
 }
@@ -822,7 +819,7 @@ sub hdr_smsg ($$$$) {
                $range = $self->{article} unless defined $range;
                my $r = get_range($self, $range);
                return $r unless ref $r;
-               more($self, $xhdr ? r221 : r225);
+               $self->msg_more($xhdr ? r221 : r225);
                long_response($self, \&smsg_range_i, @$r, $field);
        }
 }
@@ -840,7 +837,7 @@ sub do_hdr ($$$;$) {
        } elsif ($sub =~ /\A:(bytes|lines)\z/) {
                hdr_smsg($self, $xhdr, $1, $range);
        } else {
-               $xhdr ? (r221 . "\r\n.") : "503 HDR not permitted on $header";
+               $xhdr ? (r221 . '.') : "503 HDR not permitted on $header";
        }
 }
 
@@ -870,16 +867,9 @@ sub hdr_mid_prefix ($$$$$) {
 
 sub hdr_mid_response ($$$$$$) {
        my ($self, $xhdr, $ibx, $n, $mid, $v) = @_;
-       my $res = '';
-       if ($xhdr) {
-               $res .= r221 . "\r\n";
-               $res .= "$mid $v\r\n";
-       } else {
-               $res .= r225 . "\r\n";
-               my $pfx = hdr_mid_prefix($self, $xhdr, $ibx, $n, $mid);
-               $res .= "$pfx $v\r\n";
-       }
-       res($self, $res .= '.');
+       $self->write(($xhdr ? r221.$mid :
+                  r225.hdr_mid_prefix($self, $xhdr, $ibx, $n, $mid)) .
+               " $v\r\n.\r\n");
        undef;
 }
 
@@ -975,7 +965,7 @@ sub cmd_starttls ($) {
        return r502 if ($sock->can('accept_SSL') || $self->compressed);
        my $opt = $self->{nntpd}->{accept_tls} or
                return '580 can not initiate TLS negotiation';
-       res($self, '382 Continue with TLS negotiation');
+       $self->write(\"382 Continue with TLS negotiation\r\n");
        $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
        $self->requeue if PublicInbox::DS::accept_tls_step($self);
        undef;
@@ -1050,7 +1040,6 @@ sub event_step {
 
        return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
 
-       $self->update_idle_time;
        # only read more requests if we've drained the write buffer,
        # otherwise we can be buffering infinitely w/o backpressure
 
@@ -1072,17 +1061,15 @@ sub event_step {
        out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0);
        return $self->close if $r < 0;
        $self->rbuf_idle($rbuf);
-       $self->update_idle_time;
 
        # maybe there's more pipelined data, or we'll have
        # to register it for socket-readiness notifications
        $self->requeue unless $pending;
 }
 
-# for graceful shutdown in PublicInbox::Daemon:
-sub busy {
-       my ($self, $now) = @_;
-       ($self->{rbuf} || $self->{wbuf} || $self->not_idle_long($now));
+sub busy { # for graceful shutdown in PublicInbox::Daemon:
+       my ($self) = @_;
+       defined($self->{rbuf}) || defined($self->{wbuf})
 }
 
 1;