unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH] ExtMsg, NewsWWW: account for publicinbox.nameIsUrl
@ 2025-01-08 10:12 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2025-01-08 10:12 UTC (permalink / raw)
  To: meta

As with WwwListing, these generate full URLs which go across
multiple inboxes, so support the new publicinbox.nameIsUrl
feature which allows users with many inboxes on the same domain
to avoid setting `publicinbox.*.url' for each one.
---
 lib/PublicInbox/ExtMsg.pm  | 20 +++++++++++++-------
 lib/PublicInbox/NewsWWW.pm | 12 +++++++-----
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/lib/PublicInbox/ExtMsg.pm b/lib/PublicInbox/ExtMsg.pm
index 95feb885..d994b783 100644
--- a/lib/PublicInbox/ExtMsg.pm
+++ b/lib/PublicInbox/ExtMsg.pm
@@ -76,7 +76,8 @@ sub search_partial ($$) {
 sub ext_msg_i {
 	my ($other, $ctx) = @_;
 
-	return if $other->{name} eq $ctx->{ibx}->{name} || !$other->base_url;
+	return if $other->{name} eq $ctx->{ibx}->{name} ||
+		!$other->base_url(@{$ctx->{-url_env}});
 
 	my $mm = $other->mm or return;
 
@@ -117,7 +118,7 @@ sub ext_msg_ALL ($) {
 			$k =~ s/:[0-9]+:$x->{blob}\z// or next;
 			next if $k eq $cur_key;
 			my $ibx = $by_eidx_key->{$k} // next;
-			$ibx->base_url or next;
+			$ibx->base_url(@{$ctx->{-url_env}}) or next;
 			push(@{$ctx->{found}}, $ibx) unless $seen{$k}++;
 		}
 	}
@@ -132,8 +133,11 @@ sub ext_msg_ALL ($) {
 	partial_response($ctx);
 }
 
+# only public entry point
 sub ext_msg {
 	my ($ctx) = @_;
+	@{$ctx->{-url_env}} = $ctx->{www}->{pi_cfg}->{
+			lc 'publicinbox.nameIsUrl'} ? ($ctx->{env}) : ();
 	ext_msg_ALL($ctx) // sub {
 		$ctx->{-wcb} = $_[0]; # HTTP server write callback
 
@@ -188,8 +192,9 @@ sub finalize_exact {
 	finalize_partial($ctx);
 }
 
-sub _url_pfx ($$) {
-	my ($ctx, $u) = @_;
+sub _url_pfx ($$;$) {
+	my ($ctx, $ibx, $env) = @_;
+	my $u = $ibx->base_url(@{$env // $ctx->{-url_env}}) // return;
 	(index($u, '://') < 0 && index($u, '/') != 0) ?
 		"$ctx->{-upfx}../$u" : $u;
 }
@@ -209,10 +214,11 @@ sub partial_response ($) {
 		$n_partial .= '+' if ($n_partial == PARTIAL_MAX);
 		$s .= "\n$n_partial partial match$es found:\n\n";
 		my $cur_name = $ctx->{ibx}->{name};
+		my $e = [ $ctx->{env} ];
 		foreach my $pair (@{$ctx->{partial}}) {
 			my ($ibx, $res) = @$pair;
-			my $e = $ibx->{name} eq $cur_name ? $ctx->{env} : undef;
-			my $u = _url_pfx($ctx, $ibx->base_url($e) // next);
+			my $u = _url_pfx($ctx, $ibx,
+				$ibx->{name} eq $cur_name ? $e : undef) // next;
 			foreach my $m (@$res) {
 				my $href = mid_href($m);
 				my $html = ascii_html($m);
@@ -265,7 +271,7 @@ sub exact {
 	$ctx->{-html_tip} = join('',
 			"<pre>Message-ID: &lt;$html&gt;\nfound in $end:\n\n",
 				(map {
-					my $u = _url_pfx($ctx, $_->base_url);
+					my $u = _url_pfx($ctx, $_);
 					qq(<a\nhref="$u$href/">$u$html/</a>\n)
 				} @$found),
 			$ext_urls, '</pre>');
diff --git a/lib/PublicInbox/NewsWWW.pm b/lib/PublicInbox/NewsWWW.pm
index d13731ae..28dc652d 100644
--- a/lib/PublicInbox/NewsWWW.pm
+++ b/lib/PublicInbox/NewsWWW.pm
@@ -25,11 +25,11 @@ sub redirect ($$) {
 }
 
 sub try_inbox {
-	my ($ibx, $arg) = @_;
+	my ($ibx, $arg, @url_env) = @_;
 	return if scalar(@$arg) > 1;
 
 	# do not pass $env since HTTP_HOST may differ
-	my $url = $ibx->base_url or return;
+	my $url = $ibx->base_url(@url_env) or return;
 
 	my ($mid) = @$arg;
 	eval { $ibx->mm->num_for($mid) } or return;
@@ -49,8 +49,9 @@ sub call {
 		[ 404, [qw(Content-Type text/plain)], ["404 Not Found\n"] ];
 	my ($ng, $article) = @parts;
 	my $pi_cfg = $self->{pi_cfg};
+	my @url_env = $pi_cfg->{lc 'publicinbox.nameIsUrl'} ? ($env) : ();
 	if (my $ibx = $pi_cfg->lookup_newsgroup($ng)) {
-		my $url = prurl($env, $ibx->{url});
+		my $url = prurl($env, $ibx->{url} // $ibx->base_url(@url_env));
 		my $code = 301;
 		if (defined $article && $article =~ /\A[0-9]+\z/) {
 			my $mid = eval { $ibx->mm->mid_for($article) };
@@ -83,7 +84,8 @@ sub call {
 					s/:[0-9]+:$x->{blob}\z// or next;
 					my $ibx = $by_eidx_key->{$_} // next;
 					my $url = $ALL->base_url($env) //
-							$ibx->base_url // next;
+						$ibx->base_url(@url_env) //
+						next;
 					$url .= mid_escape($mid) . '/';
 					return redirect(302, $url);
 				}
@@ -92,7 +94,7 @@ sub call {
 	} else { # slow path, scan every inbox
 		for my $mid (@try) {
 			my $arg = [ $mid ]; # [1] => result
-			$pi_cfg->each_inbox(\&try_inbox, $arg);
+			$pi_cfg->each_inbox(\&try_inbox, $arg, @url_env);
 			return $arg->[1] if $arg->[1];
 		}
 	}

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2025-01-08 10:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-08 10:12 [PATCH] ExtMsg, NewsWWW: account for publicinbox.nameIsUrl Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).