]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/NNTP.pm
mm: min/max: return 0 instead of undef
[public-inbox.git] / lib / PublicInbox / NNTP.pm
index 46398cd4896af40c4e550fb1e3c070c5f39d51b2..b641bd23e5304f964586544dec4dd340e2f67eff 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);
@@ -147,7 +147,7 @@ sub list_active_times ($;$) {
        wildmat2re($wildmat);
        foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
                $ng->{newsgroup} =~ $wildmat or next;
-               my $c = eval { $ng->mm->created_at } || time;
+               my $c = eval { $ng->uidvalidity } // time;
                more($self, "$ng->{newsgroup} $c $ng->{-primary_address}");
        }
 }
@@ -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);
@@ -446,8 +449,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 +487,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 +533,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 +566,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' }