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