]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
view: support SHA-1 of Message-IDs for message links
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 898f42a8c56379ab48bfde3ad34ca11f97a32bd8..1814286b5e7647e923e8d1d3376f94d062829510 100644 (file)
@@ -13,6 +13,8 @@ use strict;
 use warnings;
 use PublicInbox::Config;
 use URI::Escape qw(uri_escape_utf8 uri_unescape);
+use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
+use constant PI_URL => 'http://public-inbox.org/';
 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
 our $pi_config;
 BEGIN {
@@ -119,6 +121,7 @@ sub get_index {
                        listname => $ctx->{listname},
                        pi_config => $pi_config,
                        cgi => $cgi,
+                       footer => footer($ctx),
                        top => $top,
                }) ]
        ];
@@ -127,13 +130,19 @@ sub get_index {
 # just returns a string ref for the blob in the current ctx
 sub mid2blob {
        my ($ctx) = @_;
-       require Digest::SHA;
-       my $hex = Digest::SHA::sha1_hex($ctx->{mid});
-       $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
-                       die "BUG: not a SHA-1 hex: $hex";
+       my $hex = $ctx->{mid};
+       my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
+
+       unless (defined $x38) {
+               # compatibility with old links
+               require Digest::SHA;
+               $hex = Digest::SHA::sha1_hex($hex);
+               ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
+               defined $x38 or die "BUG: not a SHA-1 hex: $hex";
+       }
 
        my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
-                       qw(cat-file blob), "HEAD:$1/$2");
+                       qw(cat-file blob), "HEAD:$x2/$x38");
        my $cmd = join(' ', @cmd);
        my $pid = open my $fh, '-|';
        defined $pid or die "fork failed: $!\n";
@@ -164,9 +173,10 @@ sub get_mid_html {
        my $mid_href = PublicInbox::Hval::ascii_html(
                                                uri_escape_utf8($ctx->{mid}));
        my $pfx = "../f/$mid_href.html";
+       my $foot = footer($ctx);
        require Email::MIME;
        [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-               [ PublicInbox::View->msg_html(Email::MIME->new($x), $pfx) ] ];
+         [ PublicInbox::View->msg_html(Email::MIME->new($x), $pfx, $foot) ] ];
 }
 
 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
@@ -176,8 +186,9 @@ sub get_full_html {
        return r404() unless $x;
        require PublicInbox::View;
        require Email::MIME;
-       [ 200, [ 'Content-Type' => 'text/html' ],
-               [ PublicInbox::View->msg_html(Email::MIME->new($x))] ];
+       my $foot = footer($ctx);
+       [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
+         [ PublicInbox::View->msg_html(Email::MIME->new($x), undef, $foot)] ];
 }
 
 sub self_url {
@@ -205,4 +216,69 @@ sub do_redirect {
        ]
 }
 
+sub ctx_get {
+       my ($ctx, $key) = @_;
+       my $val = $ctx->{$key};
+       (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
+       $val;
+}
+
+sub try_cat {
+       my ($path) = @_;
+       my $rv;
+       if (open(my $fh, '<', $path)) {
+               local $/;
+               $rv = <$fh>;
+               close $fh;
+       }
+       $rv;
+}
+
+sub footer {
+       my ($ctx) = @_;
+       return '' unless $ctx;
+       my $git_dir = ctx_get($ctx, 'git_dir');
+
+       # favor user-supplied footer
+       my $footer = try_cat("$git_dir/public-inbox/footer.html");
+       if (defined $footer) {
+               chomp $footer;
+               return $footer;
+       }
+
+       # auto-generate a footer
+       my $listname = ctx_get($ctx, 'listname');
+       my $desc = try_cat("$git_dir/description");
+       $desc = '$GIT_DIR/description missing' unless defined $desc;
+       chomp $desc;
+
+       my $urls = try_cat("$git_dir/cloneurl");
+       my @urls = split(/\r?\n/, $urls || '');
+       my $nurls = scalar @urls;
+       if ($nurls == 0) {
+               $urls = '($GIT_DIR/cloneurl missing)';
+       } elsif ($nurls == 1) {
+               $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
+                       '">ssoma</a>: ' . $urls[0];
+       } else {
+               $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
+                       "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
+       }
+
+       my $addr = $pi_config->get($listname, 'address');
+       if (ref($addr) eq 'ARRAY') {
+               $addr = $addr->[0]; # first address is primary
+       }
+
+       $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
+       $desc =  $desc;
+       join("\n",
+               '- ' . $desc,
+               "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
+                       'anybody may post in plain-text (not HTML):',
+               $addr,
+               $urls
+       );
+}
+
 1;