]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/NNTP.pm
nntp: NEWNEWS: speed up filtering
[public-inbox.git] / lib / PublicInbox / NNTP.pm
index 46398cd4896af40c4e550fb1e3c070c5f39d51b2..5cbf5a16e4e1b8b38c79312540c125828d5be4b7 100644 (file)
@@ -53,7 +53,7 @@ sub new ($$$) {
        my $wbuf;
        if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
                return CORE::close($sock) if $! != EAGAIN;
-               $ev = PublicInbox::TLS::epollbit();
+               $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
                $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
        }
        $self->SUPER::new($sock, $ev | EPOLLONESHOT);
@@ -136,29 +136,29 @@ sub list_headers ($;$) {
 sub list_active ($;$) {
        my ($self, $wildmat) = @_;
        wildmat2re($wildmat);
-       foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
-               $ng->{newsgroup} =~ $wildmat or next;
-               group_line($self, $ng);
+       my $groups = $self->{nntpd}->{groups};
+       for my $ngname (grep(/$wildmat/, @{$self->{nntpd}->{groupnames}})) {
+               group_line($self, $groups->{$ngname});
        }
 }
 
 sub list_active_times ($;$) {
        my ($self, $wildmat) = @_;
        wildmat2re($wildmat);
-       foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
-               $ng->{newsgroup} =~ $wildmat or next;
-               my $c = eval { $ng->mm->created_at } || time;
-               more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
+       my $groups = $self->{nntpd}->{groups};
+       for my $ngname (grep(/$wildmat/, @{$self->{nntpd}->{groupnames}})) {
+               my $ibx = $groups->{$ngname};
+               my $c = eval { $ibx->uidvalidity } // time;
+               more($self, "$ngname $c $ibx->{-primary_address}");
        }
 }
 
 sub list_newsgroups ($;$) {
        my ($self, $wildmat) = @_;
        wildmat2re($wildmat);
-       foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
-               $ng->{newsgroup} =~ $wildmat or next;
-               my $d = $ng->description;
-               more($self, "$ng->{newsgroup} $d");
+       my $groups = $self->{nntpd}->{groups};
+       for my $ngname (grep(/$wildmat/, @{$self->{nntpd}->{groupnames}})) {
+               more($self, "$ngname ".$groups->{$ngname}->description);
        }
 }
 
@@ -244,7 +244,7 @@ sub parse_time ($$;$) {
 sub group_line ($$) {
        my ($self, $ng) = @_;
        my ($min, $max) = $ng->mm->minmax;
-       more($self, "$ng->{newsgroup} $max $min n") if defined $min && defined $max;
+       more($self, "$ng->{newsgroup} $max $min n");
 }
 
 sub cmd_newgroups ($$$;$$) {
@@ -255,7 +255,7 @@ sub cmd_newgroups ($$$;$$) {
        # TODO dists
        more($self, '231 list of new newsgroups follows');
        foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
-               my $c = eval { $ng->mm->created_at } || 0;
+               my $c = eval { $ng->uidvalidity } // 0;
                next unless $c > $ts;
                group_line($self, $ng);
        }
@@ -294,23 +294,28 @@ sub ngpat2re (;$) {
 }
 
 sub newnews_i {
-       my ($self, $overs, $ts, $prev) = @_;
-       my $over = $overs->[0];
-       my $msgs = $over->query_ts($ts, $$prev);
-       if (scalar @$msgs) {
-               more($self, '<' .
-                       join(">\r\n<", map { $_->{mid} } @$msgs ).
-                       '>');
-               $$prev = $msgs->[-1]->{num};
-       } else {
-               shift @$overs;
-               if (@$overs) { # continue onto next newsgroup
-                       $$prev = 0;
-                       return 1;
-               } else { # break out of the long response.
-                       return;
+       my ($self, $names, $ts, $prev) = @_;
+       my $ngname = $names->[0];
+       if (my $ibx = $self->{nntpd}->{groups}->{$ngname}) {
+               if (my $over = $ibx->over) {
+                       my $msgs = $over->query_ts($ts, $$prev);
+                       if (scalar @$msgs) {
+                               more($self, '<' .
+                                       join(">\r\n<",
+                                               map { $_->{mid} } @$msgs ) .
+                                       '>');
+                               $$prev = $msgs->[-1]->{num};
+                               return 1; # continue on current group
+                       }
                }
        }
+       shift @$names;
+       if (@$names) { # continue onto next newsgroup
+               $$prev = 0;
+               1;
+       } else { # all done, break out of the long_response
+               undef;
+       }
 }
 
 sub cmd_newnews ($$$$;$$) {
@@ -321,17 +326,11 @@ sub cmd_newnews ($$$$;$$) {
        my ($keep, $skip) = split('!', $newsgroups, 2);
        ngpat2re($keep);
        ngpat2re($skip);
-       my @overs;
-       foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
-               $ng->{newsgroup} =~ $keep or next;
-               $ng->{newsgroup} =~ $skip and next;
-               my $over = $ng->over or next;
-               push @overs, $over;
-       };
-       return '.' unless @overs;
-
+       my @names = grep(!/$skip/, grep(/$keep/,
+                               @{$self->{nntpd}->{groupnames}}));
+       return '.' unless scalar(@names);
        my $prev = 0;
-       long_response($self, \&newnews_i, \@overs, $ts, \$prev);
+       long_response($self, \&newnews_i, \@names, $ts, \$prev);
 }
 
 sub cmd_group ($$) {
@@ -343,8 +342,6 @@ sub cmd_group ($$) {
 
        $self->{ng} = $ng;
        my ($min, $max) = $ng->mm->minmax;
-       $min ||= 0;
-       $max ||= 0;
        $self->{article} = $min;
        my $est_size = $max - $min;
        "211 $est_size $min $max $group";
@@ -415,10 +412,6 @@ sub set_nntp_headers ($$) {
        my ($hdr, $smsg) = @_;
        my ($mid) = $smsg->{mid};
 
-       # why? leafnode requires a Path: header for some inexplicable
-       # reason.  We'll fake the shortest one possible.
-       $hdr->header_set('Path', 'y');
-
        # leafnode (and maybe other NNTP clients) have trouble dealing
        # with v2 messages which have multiple Message-IDs (either due
        # to our own content-based dedupe or buggy git-send-email versions).
@@ -432,12 +425,21 @@ sub set_nntp_headers ($$) {
                $hdr->header_set('X-Alt-Message-ID', @alt);
        }
 
-       # clobber some
+       # clobber some existing headers
        my $ibx = $smsg->{-ibx};
        my $xref = xref($smsg->{nntp}, $ibx, $smsg->{num}, $mid);
        $hdr->header_set('Xref', $xref);
-       $xref =~ s/:[0-9]+//g;
-       $hdr->header_set('Newsgroups', (split(/ /, $xref, 2))[1]);
+
+       # RFC 5536 3.1.4
+       my ($server_name, $newsgroups) = split(/ /, $xref, 2);
+       $newsgroups =~ s/:[0-9]+\b//g; # drop NNTP article numbers
+       $newsgroups =~ tr/ /,/;
+       $hdr->header_set('Newsgroups', $newsgroups);
+
+       # *something* here is required for leafnode, try to follow
+       # RFC 5536 3.1.5...
+       $hdr->header_set('Path', $server_name . '!not-for-mail');
+
        header_append($hdr, 'List-Post', "<mailto:$ibx->{-primary_address}>");
        if (my $url = $ibx->base_url) {
                $mid = mid_escape($mid);
@@ -446,8 +448,8 @@ sub set_nntp_headers ($$) {
        }
 }
 
-sub art_lookup ($$) {
-       my ($self, $art) = @_;
+sub art_lookup ($$$) {
+       my ($self, $art, $code) = @_;
        my $ng = $self->{ng};
        my ($n, $mid);
        my $err;
@@ -484,7 +486,17 @@ find_mid:
 found:
        my $smsg = $ng->over->get_art($n) or return $err;
        $smsg->{-ibx} = $ng;
-       $smsg;
+       if ($code == 223) { # STAT
+               set_art($self, $n);
+               "223 $n <$smsg->{mid}> article retrieved - " .
+                       "request text separately";
+       } else { # HEAD | BODY | ARTICLE
+               $smsg->{nntp} = $self;
+               $smsg->{nntp_code} = $code;
+               set_art($self, $art);
+               # this dereferences to `undef'
+               ${git_async_cat($ng->git, $smsg->{blob}, \&blob_cb, $smsg)};
+       }
 }
 
 sub msg_body_write ($$) {
@@ -520,7 +532,7 @@ sub msg_hdr_write ($$) {
 sub blob_cb { # called by git->cat_async via git_async_cat
        my ($bref, $oid, $type, $size, $smsg) = @_;
        my $self = $smsg->{nntp};
-       my $code = $smsg->{nntp_code} // 220;
+       my $code = $smsg->{nntp_code};
        if (!defined($oid)) {
                # it's possible to have TOCTOU if an admin runs
                # public-inbox-(edit|purge), just move onto the next message
@@ -553,40 +565,22 @@ sub blob_cb { # called by git->cat_async via git_async_cat
 
 sub cmd_article ($;$) {
        my ($self, $art) = @_;
-       my $smsg = art_lookup($self, $art);
-       return $smsg unless ref $smsg;
-       set_art($self, $art);
-       $smsg->{nntp} = $self;
-       ${git_async_cat($smsg->{-ibx}->git, $smsg->{blob}, \&blob_cb, $smsg)};
+       art_lookup($self, $art, 220);
 }
 
 sub cmd_head ($;$) {
        my ($self, $art) = @_;
-       my $smsg = art_lookup($self, $art);
-       return $smsg unless ref $smsg;
-       set_art($self, $art);
-       $smsg->{nntp} = $self;
-       $smsg->{nntp_code} = 221;
-       ${git_async_cat($smsg->{-ibx}->git, $smsg->{blob}, \&blob_cb, $smsg)};
+       art_lookup($self, $art, 221);
 }
 
 sub cmd_body ($;$) {
        my ($self, $art) = @_;
-       my $smsg = art_lookup($self, $art);
-       return $smsg unless ref $smsg;
-       set_art($self, $art);
-       $smsg->{nntp} = $self;
-       $smsg->{nntp_code} = 222;
-       ${git_async_cat($smsg->{-ibx}->git, $smsg->{blob}, \&blob_cb, $smsg)};
+       art_lookup($self, $art, 222);
 }
 
 sub cmd_stat ($;$) {
        my ($self, $art) = @_;
-       my $smsg = art_lookup($self, $art); # art may be msgid
-       return $smsg unless ref $smsg;
-       $art = $smsg->{num};
-       set_art($self, $art);
-       "223 $art <$smsg->{mid}> article retrieved - request text separately";
+       art_lookup($self, $art, 223); # art may be msgid
 }
 
 sub cmd_ihave ($) { '435 article not wanted - do not send it' }