]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchView.pm
view: implement optional address obfuscation
[public-inbox.git] / lib / PublicInbox / SearchView.pm
index cec87c6ad1c84c891fe41d4eac62ec556e6b3a97..777710e3649148bcfec7f2e552ba9bb1416bdca9 100644 (file)
@@ -5,11 +5,12 @@
 package PublicInbox::SearchView;
 use strict;
 use warnings;
+use URI::Escape qw(uri_unescape uri_escape);
 use PublicInbox::SearchMsg;
-use PublicInbox::Hval qw/ascii_html/;
+use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
 use PublicInbox::View;
 use PublicInbox::WwwAtomStream;
-use PublicInbox::MID qw(mid2path mid_mime mid_clean mid_escape);
+use PublicInbox::MID qw(mid2path mid_mime mid_clean mid_escape MID_ESC);
 use PublicInbox::MIME;
 require PublicInbox::Git;
 require PublicInbox::SearchThread;
@@ -29,19 +30,27 @@ sub sres_top_html {
                mset => 1,
                relevance => $q->{r},
        };
-       my ($mset, $total);
+       my ($mset, $total, $err, $cb);
+retry:
        eval {
                $mset = $ctx->{srch}->query($q->{'q'}, $opts);
                $total = $mset->get_matches_estimated;
        };
-       my $err = $@;
+       $err = $@;
        ctx_prepare($q, $ctx);
-       my $cb;
        if ($err) {
                $code = 400;
                $ctx->{-html_tip} = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
                $cb = *noop;
        } elsif ($total == 0) {
+               if (defined($ctx->{-uxs_retried})) {
+                       # undo retry damage:
+                       $q->{'q'} = $ctx->{-uxs_retried};
+               } elsif (index($q->{'q'}, '%') >= 0) {
+                       $ctx->{-uxs_retried} = $q->{'q'};
+                       $q->{'q'} = uri_unescape($q->{'q'});
+                       goto retry;
+               }
                $code = 404;
                $ctx->{-html_tip} = "<pre>\n[No results found]</pre><hr>";
                $cb = *noop;
@@ -49,7 +58,7 @@ sub sres_top_html {
                my $x = $q->{x};
                return adump($_[0], $mset, $q, $ctx) if $x eq 'A';
 
-               $ctx->{-html_tip} = search_nav_top($mset, $q) . "\n\n";
+               $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx) . "\n\n";
                if ($x eq 't') {
                        $cb = mset_thread($ctx, $mset, $q);
                } else {
@@ -80,6 +89,7 @@ sub mset_summary {
        my $pfx = ' ' x $pad;
        my $res = \($ctx->{-html_tip});
        my $srch = $ctx->{srch};
+       my $obfs = $ctx->{-inbox}->{obfuscate};
        foreach my $m ($mset->items) {
                my $rank = sprintf("%${pad}d", $m->get_rank + 1);
                my $pct = $m->get_percent;
@@ -93,6 +103,10 @@ sub mset_summary {
                }
                my $s = ascii_html($smsg->subject);
                my $f = ascii_html($smsg->from_name);
+               if ($obfs) {
+                       obfuscate_addrs($s);
+                       obfuscate_addrs($f);
+               }
                my $ts = PublicInbox::View::fmt_ts($smsg->ts);
                my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->{href};
                $$res .= qq{$rank. <b><a\nhref="$mid/">}.
@@ -113,9 +127,22 @@ sub err_txt {
 }
 
 sub search_nav_top {
-       my ($mset, $q) = @_;
+       my ($mset, $q, $ctx) = @_;
 
-       my $rv = "<pre>Search results ordered by [";
+       my $rv = '<pre>';
+       my $initial_q = $ctx->{-uxs_retried};
+       if (defined $initial_q) {
+               my $rewritten = $q->{'q'};
+               utf8::decode($initial_q);
+               utf8::decode($rewritten);
+               $initial_q = ascii_html($initial_q);
+               $rewritten = ascii_html($rewritten);
+               $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
+               $rv .= " returned no results, used:\n";
+               $rv .= " <b>$rewritten</b>\n instead\n\n";
+       }
+
+       $rv .= 'Search results ordered by [';
        if ($q->{r}) {
                my $d = $q->qs_html(r => 0);
                $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
@@ -201,8 +228,11 @@ sub mset_thread {
        $ctx->{seen} = {};
        $ctx->{s_nr} = scalar(@$msgs).'+ results';
 
+       # reduce hash lookups in skel_dump
+       $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
        PublicInbox::View::walk_thread($rootset, $ctx,
                *PublicInbox::View::pre_thread);
+
        @$msgs = reverse @$msgs if $r;
        my $mime;
        sub {
@@ -222,7 +252,9 @@ sub mset_thread {
 
 sub ctx_prepare {
        my ($q, $ctx) = @_;
-       my $qh = ascii_html($q->{'q'});
+       my $qh = $q->{'q'};
+       utf8::decode($qh);
+       $qh = ascii_html($qh);
        $ctx->{-q_value_html} = $qh;
        $ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
        $ctx->{-title_html} = "$qh - search results";
@@ -254,8 +286,9 @@ sub adump {
 package PublicInbox::SearchQuery;
 use strict;
 use warnings;
+use URI::Escape qw(uri_escape);
 use PublicInbox::Hval;
-use PublicInbox::MID qw(mid_escape);
+use PublicInbox::MID qw(MID_ESC);
 
 sub new {
        my ($class, $qp) = @_;
@@ -280,7 +313,7 @@ sub qs_html {
                $self = $tmp;
        }
 
-       my $q = mid_escape($self->{'q'});
+       my $q = uri_escape($self->{'q'}, MID_ESC);
        $q =~ s/%20/+/g; # improve URL readability
        my $qs = "q=$q";