]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInspect.pm
f18e31c5c8f494059bb7fd9f5544ae89b0f937fe
[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_docid ($$;$) {
82         my ($lei, $docid, $ent) = @_;
83         require PublicInbox::Search;
84         $ent //= {};
85         my $xdb;
86         if ($xdb = delete $ent->{xdb}) { # from inspect_num
87         } elsif (defined(my $dir = $lei->{opt}->{dir})) {
88                 no warnings 'once';
89                 $xdb = $PublicInbox::Search::X{Database}->new($dir);
90         } else {
91                 $xdb = $lei->{lse}->xdb;
92         }
93         $xdb or return $lei->fail('no Xapian DB');
94         my $doc = $xdb->get_document($docid); # raises
95         my $data = $doc->get_data;
96         $ent->{docid} = $docid;
97         $ent->{data_length} = length($data);
98         $ent->{description} = $doc->get_description;
99         $ent->{$_} = $doc->$_ for (qw(termlist_count values_count));
100         my $cur = $doc->termlist_begin;
101         my $end = $doc->termlist_end;
102         for (; $cur != $end; $cur++) {
103                 my $tn = $cur->get_termname;
104                 $tn =~ s/\A([A-Z]+)// or warn "$tn no prefix! (???)";
105                 my $term = ($1 // '');
106                 push @{$ent->{terms}->{$term}}, $tn;
107         }
108         @$_ = sort(@$_) for values %{$ent->{terms} // {}};
109         $cur = $doc->values_begin;
110         $end = $doc->values_end;
111         for (; $cur != $end; $cur++) {
112                 my $n = $cur->get_valueno;
113                 my $v = $cur->get_value;
114                 my $iv = PublicInbox::Search::sortable_unserialise($v);
115                 $v = $iv + 0 if defined $iv;
116                 # not using ->[$n] since we may have large gaps in $n
117                 $ent->{'values'}->{$n} = $v;
118         }
119         $ent;
120 }
121
122 sub dir2ibx ($$) {
123         my ($lei, $dir) = @_;
124         if (-f "$dir/ei.lock") {
125                 require PublicInbox::ExtSearch;
126                 PublicInbox::ExtSearch->new($dir);
127         } elsif (-f "$dir/inbox.lock" || -d "$dir/public-inbox") {
128                 require PublicInbox::Inbox; # v2, v1
129                 bless { inboxdir => $dir }, 'PublicInbox::Inbox';
130         } else {
131                 $lei->fail("no (indexed) inbox or extindex at $dir");
132         }
133 }
134
135 sub inspect_num ($$) {
136         my ($lei, $num) = @_;
137         my ($docid, $ibx);
138         my $ent = { num => $num };
139         if (defined(my $dir = $lei->{opt}->{dir})) {
140                 $ibx = dir2ibx($lei, $dir) or return;
141                 if ($ent->{xdb} = $ibx->xdb) {
142                         my $num2docid = $lei->{lse}->can('num2docid');
143                         $docid = $num2docid->($ibx, $num);
144                 }
145         } else {
146                 $ibx = $lei->{lse};
147                 $lei->{lse}->xdb; # set {nshard} for num2docid
148                 $docid = $lei->{lse}->num2docid($num);
149         }
150         if ($ibx && $ibx->over) {
151                 my $smsg = $ibx->over->get_art($num);
152                 $ent->{smsg} = _json_prep($smsg) if $smsg;
153         }
154         defined($docid) ? inspect_docid($lei, $docid, $ent) : $ent;
155 }
156
157 sub inspect_mid ($$) {
158         my ($lei, $mid) = @_;
159         my ($ibx, $over);
160         my $ent = { mid => $mid };
161         if (defined(my $dir = $lei->{opt}->{dir})) {
162                 my $num2docid = $lei->{lse}->can('num mid => [ $mid ] 2docid');
163                 $ibx = dir2ibx($lei, $dir) or return;
164                 # $ent->{xdb} = $ibx->xdb //
165                         # return $lei->fail("no Xapian DB for $dir");
166         } else {
167                 $ibx = $lei->{lse};
168                 $lei->{lse}->xdb; # set {nshard} for num2docid
169         }
170         if ($ibx && $ibx->over) {
171                 my ($id, $prev);
172                 while (my $smsg = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
173                         push @{$ent->{smsg}}, _json_prep($smsg);
174                 }
175         }
176         $ent;
177 }
178
179 sub inspect1 ($$$) {
180         my ($lei, $item, $more) = @_;
181         my $ent;
182         if ($item =~ /\Ablob:(.+)/) {
183                 $ent = inspect_blob($lei, $1);
184         } elsif ($item =~ m!\A(?:maildir|mh):!i || -d $item) {
185                 $ent = inspect_sync_folder($lei, $item);
186         } elsif ($item =~ m!\Adocid:([0-9]+)\z!) {
187                 $ent = inspect_docid($lei, $1 + 0);
188         } elsif ($item =~ m!\Anum:([0-9]+)\z!) {
189                 $ent = inspect_num($lei, $1 + 0);
190         } elsif ($item =~ m!\A(?:mid|m):(.+)\z!) {
191                 $ent = inspect_mid($lei, $1);
192         } elsif (my $iuri = imap_uri($item)) {
193                 if (defined($iuri->uid)) {
194                         $ent = inspect_imap_uid($lei, $iuri);
195                 } else {
196                         $ent = inspect_sync_folder($lei, $item);
197                 }
198         } elsif (my $nuri = nntp_uri($item)) {
199                 if (defined(my $mid = $nuri->message)) {
200                         $ent = inspect_mid($lei, $mid);
201                 } else {
202                         my ($group, $beg, $end) = $nuri->group;
203                         if (defined($beg)) {
204                                 $ent = inspect_nntp_range($lei, $nuri);
205                         } else {
206                                 $ent = inspect_sync_folder($lei, $item);
207                         }
208                 }
209         } else { # TODO: more things
210                 return $lei->fail("$item not understood");
211         }
212         $lei->out($lei->{json}->encode($ent));
213         $lei->out(',') if $more;
214         1;
215 }
216
217 sub inspect_argv { # via wq_do
218         my ($self) = @_;
219         my ($lei, $argv) = delete @$self{qw(lei argv)};
220         my $multi = scalar(@$argv) > 1;
221         $lei->{1}->autoflush(0);
222         $lei->out('[') if $multi;
223         while (defined(my $x = shift @$argv)) {
224                 inspect1($lei, $x, scalar(@$argv)) or return;
225         }
226         $lei->out(']') if $multi;
227 }
228
229 sub inspect_start ($$) {
230         my ($lei, $argv) = @_;
231         my $self = bless { lei => $lei, argv => $argv }, __PACKAGE__;
232         my ($op_c, $ops) = $lei->workers_start($self, 1);
233         $lei->{wq1} = $self;
234         $lei->wait_wq_events($op_c, $ops);
235         $self->wq_do('inspect_argv');
236         $self->wq_close(1);
237 }
238
239 sub ins_add { # InputPipe->consume callback
240         my ($lei) = @_; # $_[1] = $rbuf
241         if (defined $_[1]) {
242                 $_[1] eq '' and return eval {
243                         my $str = delete $lei->{istr};
244                         $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
245                         my $eml = PublicInbox::Eml->new(\$str);
246                         inspect_start($lei, [
247                                 'blob:'.$lei->git_oid($eml)->hexdigest,
248                                 map { "mid:$_" } @{mids($eml)} ]);
249                 };
250                 $lei->{istr} .= $_[1];
251         } else {
252                 $lei->fail("error reading stdin: $!");
253         }
254 }
255
256 sub lei_inspect {
257         my ($lei, @argv) = @_;
258         $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
259         $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
260                 my $sto = $lei->_lei_store;
261                 $sto ? $sto->search : undef;
262         } : undef;
263         my $isatty = -t $lei->{1};
264         $lei->{json}->pretty(1)->indent(2) if $lei->{opt}->{pretty} || $isatty;
265         $lei->start_pager if $isatty;
266         if ($lei->{opt}->{stdin}) {
267                 return $lei->fail(<<'') if @argv;
268 no args allowed on command-line with --stdin
269
270                 require PublicInbox::InputPipe;
271                 PublicInbox::InputPipe::consume($lei->{0}, \&ins_add, $lei);
272         } else {
273                 inspect_start($lei, \@argv);
274         }
275 }
276
277 sub _complete_inspect {
278         require PublicInbox::LeiRefreshMailSync;
279         PublicInbox::LeiRefreshMailSync::_complete_refresh_mail_sync(@_);
280         # TODO: message-ids?, blobs? could get expensive...
281 }
282
283 1;