]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiQuery.pm
lei: run pager in client script
[public-inbox.git] / lib / PublicInbox / LeiQuery.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # handles lei <q|ls-query|rm-query|mv-query> commands
5 package PublicInbox::LeiQuery;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::MID qw($MID_EXTRACT);
9 use POSIX qw(strftime);
10 use PublicInbox::Address qw(pairs);
11 use PublicInbox::DS qw(dwaitpid);
12
13 sub _iso8601 ($) { strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($_[0])) }
14
15 # prepares an smsg for JSON
16 sub _smsg_unbless ($) {
17         my ($smsg) = @_;
18
19         delete @$smsg{qw(lines bytes)};
20         $smsg->{rcvd} = _iso8601(delete $smsg->{ts}); # JMAP receivedAt
21         $smsg->{dt} = _iso8601(delete $smsg->{ds}); # JMAP UTCDate
22
23         if (my $r = delete $smsg->{references}) {
24                 $smsg->{references} = [
25                                 map { "<$_>" } ($r =~ m/$MID_EXTRACT/go) ];
26         }
27         if (my $m = delete($smsg->{mid})) {
28                 $smsg->{'m'} = "<$m>";
29         }
30         # XXX breaking to/cc, into structured arrays or tables which
31         # distinguish "$phrase <$address>" causes pretty printing JSON
32         # to take up too much vertical space.  I can't get either
33         # Cpanel::JSON::XS or JSON::XS or jq(1) only indent when
34         # wrapping is necessary, rather than blindly indenting and
35         # adding vertical space everywhere.
36         for my $f (qw(from to cc)) {
37                 my $v = delete $smsg->{$f} or next;
38                 $smsg->{substr($f, 0, 1)} = $v;
39         }
40         $smsg->{'s'} = delete $smsg->{subject};
41         # can we be bothered to parse From/To/Cc into arrays?
42         scalar { %$smsg }; # unbless
43 }
44
45 sub _vivify_external { # _externals_each callback
46         my ($src, $dir) = @_;
47         if (-f "$dir/ei.lock") {
48                 require PublicInbox::ExtSearch;
49                 push @$src, PublicInbox::ExtSearch->new($dir);
50         } elsif (-f "$dir/inbox.lock" || -d "$dir/public-inbox") { # v2, v1
51                 require PublicInbox::Inbox;
52                 push @$src, bless { inboxdir => $dir }, 'PublicInbox::Inbox';
53         } else {
54                 warn "W: ignoring $dir, unable to determine type\n";
55         }
56 }
57
58 # the main "lei q SEARCH_TERMS" method
59 sub lei_q {
60         my ($self, @argv) = @_;
61         my $sto = $self->_lei_store(1);
62         my $cfg = $self->_lei_cfg(1);
63         my $opt = $self->{opt};
64         require PublicInbox::LeiDedupe;
65         my $dd = PublicInbox::LeiDedupe->new($self);
66
67         # --local is enabled by default
68         # src: LeiXSearch || LeiSearch || Inbox
69         my @srcs;
70         require PublicInbox::LeiXSearch;
71         my $lxs = PublicInbox::LeiXSearch->new;
72
73         # --external is enabled by default, but allow --no-external
74         if ($opt->{external} // 1) {
75                 $self->_externals_each(\&_vivify_external, \@srcs);
76         }
77         my $j = $opt->{jobs} // scalar(@srcs) > 3 ? 3 : scalar(@srcs);
78         $j = 1 if !$opt->{thread};
79         $j++ if $opt->{'local'}; # for sto->search below
80         if ($self->{sock}) {
81                 $self->atfork_prepare_wq($lxs);
82                 $lxs->wq_workers_start('lei_xsearch', $j, $self->oldset)
83                         // $self->wq_workers($j);
84         }
85         unshift(@srcs, $sto->search) if $opt->{'local'};
86         my $out = $opt->{output} // '-';
87         $out = 'json:/dev/stdout' if $out eq '-';
88         my $isatty = -t $self->{1};
89         # no forking workers after this
90         my $pid_old12 = $self->start_pager if $isatty;
91         my $json = substr($out, 0, 5) eq 'json:' ?
92                 ref(PublicInbox::Config->json)->new : undef;
93         if ($json) {
94                 if ($opt->{pretty} //= $isatty) {
95                         $json->pretty(1)->space_before(0);
96                         $json->indent_length($opt->{indent} // 2);
97                 }
98                 $json->utf8; # avoid Wide character in print warnings
99                 $json->ascii(1) if $opt->{ascii}; # for "\uXXXX"
100                 $json->canonical;
101         }
102
103         my %mset_opt = map { $_ => $opt->{$_} } qw(thread limit offset);
104         $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
105         $mset_opt{qstr} = join(' ', map {;
106                 # Consider spaces in argv to be for phrase search in Xapian.
107                 # In other words, the users should need only care about
108                 # normal shell quotes and not have to learn Xapian quoting.
109                 /\s/ ? (s/\A(\w+:)// ? qq{$1"$_"} : qq{"$_"}) : $_
110         } @argv);
111         if (defined(my $sort = $opt->{'sort'})) {
112                 if ($sort eq 'relevance') {
113                         $mset_opt{relevance} = 1;
114                 } elsif ($sort eq 'docid') {
115                         $mset_opt{relevance} = $mset_opt{asc} ? -1 : -2;
116                 } elsif ($sort =~ /\Areceived(?:-?[aA]t)?\z/) {
117                         # the default
118                 } else {
119                         die "unrecognized --sort=$sort\n";
120                 }
121         }
122         # $self->out($json->encode(\%mset_opt));
123         # descending docid order
124         $mset_opt{relevance} //= -2 if $opt->{thread};
125         # my $wcb = PublicInbox::LeiToMail->write_cb($out, $self);
126         $self->{mset_opt} = \%mset_opt;
127         $lxs->do_query($self, \@srcs);
128         if ($pid_old12) { # [ pid, stdout, stderr ]
129                 my $pid = $pid_old12->[0];
130                 $self->{$_} = $pid_old12->[$_] for (1, 2);
131                 dwaitpid($pid, undef, $self->{sock}) if $pid;
132         }
133 }
134
135 1;