]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInspect.pm
lei inspect: fix "mid:" prefix, expand to Xapian
[public-inbox.git] / lib / PublicInbox / LeiInspect.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 # "lei inspect" general purpose inspector for stuff in SQLite and
5 # Xapian.  Will eventually be useful with plain public-inboxes,
6 # not just lei/store.  This is totally half-baked at the moment
7 # but useful for testing.
8 package PublicInbox::LeiInspect;
9 use strict;
10 use v5.10.1;
11 use parent qw(PublicInbox::IPC);
12 use PublicInbox::Config;
13 use PublicInbox::MID qw(mids);
14 use PublicInbox::NetReader qw(imap_uri nntp_uri);
15
16 sub _json_prep ($) {
17         my ($smsg) = @_;
18         $smsg->{$_} += 0 for qw(bytes lines); # integerize
19         +{ %$smsg } # unbless and scalarize
20 }
21
22 sub inspect_blob ($$) {
23         my ($lei, $oidhex) = @_;
24         my $ent = {};
25         if (my $lse = $lei->{lse}) {
26                 my $oidbin = pack('H*', $oidhex);
27                 my @docids = $lse ? $lse->over->oidbin_exists($oidbin) : ();
28                 $ent->{'lei/store'} = \@docids if @docids;
29                 my $lms = $lei->lms;
30                 if (my $loc = $lms ? $lms->locations_for($oidbin) : undef) {
31                         $ent->{'mail-sync'} = $loc;
32                 }
33         }
34         $ent;
35 }
36
37 sub inspect_imap_uid ($$) {
38         my ($lei, $uid_uri) = @_;
39         my $ent = {};
40         my $lms = $lei->lms or return $ent;
41         my @oidhex = $lms->imap_oidhex($lei, $uid_uri);
42         $ent->{$$uid_uri} = @oidhex == 1 ? $oidhex[0] :
43                         ((@oidhex == 0) ? undef : \@oidhex);
44         $ent;
45 }
46
47 sub inspect_nntp_range {
48         my ($lei, $uri) = @_;
49         my ($ng, $beg, $end) = $uri->group;
50         $uri = $uri->clone;
51         $uri->group($ng);
52         my $ent = {};
53         my $ret = { "$uri" => $ent };
54         my $lms = $lei->lms or return $ret;
55         my $folders = [ $$uri ];
56         eval { $lms->arg2folder($lei, $folders) };
57         $lei->qerr("# no folders match $$uri (non-fatal)") if $@;
58         $end //= $beg;
59         for my $art ($beg..$end) {
60                 my @oidhex = map { unpack('H*', $_) }
61                         $lms->num_oidbin($folders->[0], $art);
62                 $ent->{$art} = @oidhex == 1 ? $oidhex[0] :
63                                 ((@oidhex == 0) ? undef : \@oidhex);
64         }
65         $ret;
66 }
67
68 sub inspect_sync_folder ($$) {
69         my ($lei, $folder) = @_;
70         my $ent = {};
71         my $lms = $lei->lms or return $ent;
72         my $folders = [ $folder ];
73         eval { $lms->arg2folder($lei, $folders) };
74         $lei->qerr("# no folders match $folder (non-fatal)") if $@;
75         for my $f (@$folders) {
76                 $ent->{$f} = $lms->location_stats($f); # may be undef
77         }
78         $ent
79 }
80
81 sub _inspect_doc ($$) {
82         my ($ent, $doc) = @_;
83         my $data = $doc->get_data;
84         $ent->{data_length} = length($data);
85         $ent->{description} = $doc->get_description;
86         $ent->{$_} = $doc->$_ for (qw(termlist_count values_count));
87         my $cur = $doc->termlist_begin;
88         my $end = $doc->termlist_end;
89         for (; $cur != $end; $cur++) {
90                 my $tn = $cur->get_termname;
91                 $tn =~ s/\A([A-Z]+)// or warn "$tn no prefix! (???)";
92                 my $term = ($1 // '');
93                 push @{$ent->{terms}->{$term}}, $tn;
94         }
95         @$_ = sort(@$_) for values %{$ent->{terms} // {}};
96         $cur = $doc->values_begin;
97         $end = $doc->values_end;
98         for (; $cur != $end; $cur++) {
99                 my $n = $cur->get_valueno;
100                 my $v = $cur->get_value;
101                 my $iv = PublicInbox::Search::sortable_unserialise($v);
102                 $v = $iv + 0 if defined $iv;
103                 # not using ->[$n] since we may have large gaps in $n
104                 $ent->{'values'}->{$n} = $v;
105         }
106         $ent;
107 }
108
109 sub inspect_docid ($$;$) {
110         my ($lei, $docid, $ent) = @_;
111         require PublicInbox::Search;
112         $ent //= {};
113         my $xdb;
114         if ($xdb = delete $ent->{xdb}) { # from inspect_num
115         } elsif (defined(my $dir = $lei->{opt}->{dir})) {
116                 no warnings 'once';
117                 $xdb = $PublicInbox::Search::X{Database}->new($dir);
118         } elsif ($lei->{lse}) {
119                 $xdb = $lei->{lse}->xdb;
120         }
121         $xdb or return $lei->fail('no Xapian DB');
122         my $doc = $xdb->get_document($docid); # raises
123         $ent->{docid} = $docid;
124         _inspect_doc($ent, $doc);
125 }
126
127 sub dir2ibx ($$) {
128         my ($lei, $dir) = @_;
129         if (-f "$dir/ei.lock") {
130                 require PublicInbox::ExtSearch;
131                 PublicInbox::ExtSearch->new($dir);
132         } elsif (-f "$dir/inbox.lock" || -d "$dir/public-inbox") {
133                 require PublicInbox::Inbox; # v2, v1
134                 bless { inboxdir => $dir }, 'PublicInbox::Inbox';
135         } else {
136                 $lei->fail("no (indexed) inbox or extindex at $dir");
137         }
138 }
139
140 sub inspect_num ($$) {
141         my ($lei, $num) = @_;
142         my ($docid, $ibx);
143         my $ent = { num => $num };
144         if (defined(my $dir = $lei->{opt}->{dir})) {
145                 $ibx = dir2ibx($lei, $dir) or return;
146                 $ent->{xdb} = $ibx->xdb and # for inspect_docid
147                         $docid = PublicInbox::LeiSearch::num2docid($ibx, $num);
148         } elsif ($lei->{lse}) {
149                 $ibx = $lei->{lse};
150                 $lei->{lse}->xdb; # set {nshard} for num2docid
151                 $docid = $lei->{lse}->num2docid($num);
152         }
153         if ($ibx && $ibx->over) {
154                 my $smsg = $ibx->over->get_art($num);
155                 $ent->{smsg} = _json_prep($smsg) if $smsg;
156         }
157         defined($docid) ? inspect_docid($lei, $docid, $ent) : $ent;
158 }
159
160 sub inspect_mid ($$) {
161         my ($lei, $mid) = @_;
162         my $ibx;
163         my $ent = { mid => $mid };
164         if (defined(my $dir = $lei->{opt}->{dir})) {
165                 $ibx = dir2ibx($lei, $dir)
166         } else {
167                 $ibx = $lei->{lse};
168         }
169         if ($ibx && $ibx->over) {
170                 my ($id, $prev);
171                 while (my $smsg = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
172                         push @{$ent->{smsg}}, _json_prep($smsg);
173                 }
174         }
175         if ($ibx && $ibx->search) {
176                 my $mset = $ibx->search->mset(qq{mid:"$mid"});
177                 for (sort { $a->get_docid <=> $b->get_docid } $mset->items) {
178                         my $tmp = { docid => $_->get_docid };
179                         _inspect_doc($tmp, $_->get_document);
180                         push @{$ent->{xdoc}}, $tmp;
181                 }
182         }
183         $ent;
184 }
185
186 sub inspect1 ($$$) {
187         my ($lei, $item, $more) = @_;
188         my $ent;
189         if ($item =~ /\Ablob:(.+)/) {
190                 $ent = inspect_blob($lei, $1);
191         } elsif ($item =~ m!\A(?:maildir|mh):!i || -d $item) {
192                 $ent = inspect_sync_folder($lei, $item);
193         } elsif ($item =~ m!\Adocid:([0-9]+)\z!) {
194                 $ent = inspect_docid($lei, $1 + 0);
195         } elsif ($item =~ m!\Anum:([0-9]+)\z!) {
196                 $ent = inspect_num($lei, $1 + 0);
197         } elsif ($item =~ m!\A(?:mid|m):(.+)\z!) {
198                 $ent = inspect_mid($lei, $1);
199         } elsif (my $iuri = imap_uri($item)) {
200                 if (defined($iuri->uid)) {
201                         $ent = inspect_imap_uid($lei, $iuri);
202                 } else {
203                         $ent = inspect_sync_folder($lei, $item);
204                 }
205         } elsif (my $nuri = nntp_uri($item)) {
206                 if (defined(my $mid = $nuri->message)) {
207                         $ent = inspect_mid($lei, $mid);
208                 } else {
209                         my ($group, $beg, $end) = $nuri->group;
210                         if (defined($beg)) {
211                                 $ent = inspect_nntp_range($lei, $nuri);
212                         } else {
213                                 $ent = inspect_sync_folder($lei, $item);
214                         }
215                 }
216         } else { # TODO: more things
217                 return $lei->fail("$item not understood");
218         }
219         $lei->out($lei->{json}->encode($ent));
220         $lei->out(',') if $more;
221         1;
222 }
223
224 sub inspect_argv { # via wq_do
225         my ($self) = @_;
226         my ($lei, $argv) = delete @$self{qw(lei argv)};
227         my $multi = scalar(@$argv) > 1;
228         $lei->{1}->autoflush(0);
229         $lei->out('[') if $multi;
230         while (defined(my $x = shift @$argv)) {
231                 inspect1($lei, $x, scalar(@$argv)) or return;
232         }
233         $lei->out(']') if $multi;
234 }
235
236 sub inspect_start ($$) {
237         my ($lei, $argv) = @_;
238         my $self = bless { lei => $lei, argv => $argv }, __PACKAGE__;
239         my ($op_c, $ops) = $lei->workers_start($self, 1);
240         $lei->{wq1} = $self;
241         $lei->wait_wq_events($op_c, $ops);
242         $self->wq_do('inspect_argv');
243         $self->wq_close(1);
244 }
245
246 sub ins_add { # InputPipe->consume callback
247         my ($lei) = @_; # $_[1] = $rbuf
248         if (defined $_[1]) {
249                 $_[1] eq '' and return eval {
250                         my $str = delete $lei->{istr};
251                         $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
252                         my $eml = PublicInbox::Eml->new(\$str);
253                         inspect_start($lei, [
254                                 'blob:'.$lei->git_oid($eml)->hexdigest,
255                                 map { "mid:$_" } @{mids($eml)} ]);
256                 };
257                 $lei->{istr} .= $_[1];
258         } else {
259                 $lei->fail("error reading stdin: $!");
260         }
261 }
262
263 sub lei_inspect {
264         my ($lei, @argv) = @_;
265         $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
266         $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
267                 my $sto = $lei->_lei_store;
268                 $sto ? $sto->search : undef;
269         } : undef;
270         my $isatty = -t $lei->{1};
271         $lei->{json}->pretty(1)->indent(2) if $lei->{opt}->{pretty} || $isatty;
272         $lei->start_pager if $isatty;
273         if ($lei->{opt}->{stdin}) {
274                 return $lei->fail(<<'') if @argv;
275 no args allowed on command-line with --stdin
276
277                 require PublicInbox::InputPipe;
278                 PublicInbox::InputPipe::consume($lei->{0}, \&ins_add, $lei);
279         } else {
280                 inspect_start($lei, \@argv);
281         }
282 }
283
284 sub _complete_inspect {
285         require PublicInbox::LeiRefreshMailSync;
286         PublicInbox::LeiRefreshMailSync::_complete_refresh_mail_sync(@_);
287         # TODO: message-ids?, blobs? could get expensive...
288 }
289
290 1;