]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/NNTP.pm
nntp: use grep operation for wildmat matching
[public-inbox.git] / lib / PublicInbox / NNTP.pm
index 102ef42cf4455d8e4035b288172f19d693f85901..6822219646ffe1f5b5cae2177bbff99544cb255f 100644 (file)
@@ -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);
        }
@@ -343,8 +343,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 +413,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 +426,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);