1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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;
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 use POSIX qw(strftime);
16 use PublicInbox::LeiOverview;
17 *iso8601 = \&PublicInbox::LeiOverview::iso8601;
21 $smsg->{$_} += 0 for qw(bytes lines); # integerize
22 $smsg->{dt} = iso8601($smsg->{ds}) if defined($smsg->{ds});
23 $smsg->{rt} = iso8601($smsg->{ts}) if defined($smsg->{ts});
24 +{ %$smsg } # unbless and scalarize
27 sub inspect_blob ($$) {
28 my ($lei, $oidhex) = @_;
30 if (my $lse = $lei->{lse}) {
31 my $oidbin = pack('H*', $oidhex);
32 my @docids = $lse ? $lse->over->oidbin_exists($oidbin) : ();
33 $ent->{'lei/store'} = \@docids if @docids;
35 if (my $loc = $lms ? $lms->locations_for($oidbin) : undef) {
36 $ent->{'mail-sync'} = $loc;
42 sub inspect_imap_uid ($$) {
43 my ($lei, $uid_uri) = @_;
45 my $lms = $lei->lms or return $ent;
46 my @oidhex = $lms->imap_oidhex($lei, $uid_uri);
47 $ent->{$$uid_uri} = @oidhex == 1 ? $oidhex[0] :
48 ((@oidhex == 0) ? undef : \@oidhex);
52 sub inspect_nntp_range {
54 my ($ng, $beg, $end) = $uri->group;
58 my $ret = { "$uri" => $ent };
59 my $lms = $lei->lms or return $ret;
60 my $folders = [ $$uri ];
61 eval { $lms->arg2folder($lei, $folders) };
62 $lei->qerr("# no folders match $$uri (non-fatal)") if $@;
64 for my $art ($beg..$end) {
65 my @oidhex = map { unpack('H*', $_) }
66 $lms->num_oidbin($folders->[0], $art);
67 $ent->{$art} = @oidhex == 1 ? $oidhex[0] :
68 ((@oidhex == 0) ? undef : \@oidhex);
73 sub inspect_sync_folder ($$) {
74 my ($lei, $folder) = @_;
76 my $lms = $lei->lms or return $ent;
77 my $folders = [ $folder ];
78 eval { $lms->arg2folder($lei, $folders) };
79 $lei->qerr("# no folders match $folder (non-fatal)") if $@;
80 for my $f (@$folders) {
81 $ent->{$f} = $lms->location_stats($f); # may be undef
86 sub _inspect_doc ($$) {
88 my $data = $doc->get_data;
89 $ent->{data_length} = length($data);
90 $ent->{description} = $doc->get_description;
91 $ent->{$_} = $doc->$_ for (qw(termlist_count values_count));
92 my $cur = $doc->termlist_begin;
93 my $end = $doc->termlist_end;
94 for (; $cur != $end; $cur++) {
95 my $tn = $cur->get_termname;
96 $tn =~ s/\A([A-Z]+)// or warn "$tn no prefix! (???)";
97 my $term = ($1 // '');
98 push @{$ent->{terms}->{$term}}, $tn;
100 @$_ = sort(@$_) for values %{$ent->{terms} // {}};
101 $cur = $doc->values_begin;
102 $end = $doc->values_end;
103 for (; $cur != $end; $cur++) {
104 my $n = $cur->get_valueno;
105 my $v = $cur->get_value;
106 my $iv = PublicInbox::Search::sortable_unserialise($v);
107 $v = $iv + 0 if defined $iv;
108 # not using ->[$n] since we may have large gaps in $n
109 $ent->{'values'}->{$n} = $v;
114 sub inspect_docid ($$;$) {
115 my ($lei, $docid, $ent) = @_;
116 require PublicInbox::Search;
119 if ($xdb = delete $ent->{xdb}) { # from inspect_num
120 } elsif (defined(my $dir = $lei->{opt}->{dir})) {
122 $xdb = $PublicInbox::Search::X{Database}->new($dir);
123 } elsif ($lei->{lse}) {
124 $xdb = $lei->{lse}->xdb;
126 $xdb or return $lei->fail('no Xapian DB');
127 my $doc = $xdb->get_document($docid); # raises
128 $ent->{docid} = $docid;
129 _inspect_doc($ent, $doc);
133 my ($lei, $dir) = @_;
134 if (-f "$dir/ei.lock") {
135 require PublicInbox::ExtSearch;
136 PublicInbox::ExtSearch->new($dir);
137 } elsif (-f "$dir/inbox.lock" || -d "$dir/public-inbox") {
138 require PublicInbox::Inbox; # v2, v1
139 bless { inboxdir => $dir }, 'PublicInbox::Inbox';
141 $lei->fail("no (indexed) inbox or extindex at $dir");
145 sub inspect_num ($$) {
146 my ($lei, $num) = @_;
148 my $ent = { num => $num };
149 if (defined(my $dir = $lei->{opt}->{dir})) {
150 $ibx = dir2ibx($lei, $dir) or return;
151 if (my $srch = $ibx->search) {
152 $ent->{xdb} = $srch->xdb and
153 $docid = $srch->num2docid($num);
155 } elsif ($lei->{lse}) {
157 $lei->{lse}->xdb; # set {nshard} for num2docid
158 $docid = $lei->{lse}->num2docid($num);
160 if ($ibx && $ibx->over) {
161 my $smsg = $ibx->over->get_art($num);
162 $ent->{smsg} = _json_prep($smsg) if $smsg;
164 defined($docid) ? inspect_docid($lei, $docid, $ent) : $ent;
167 sub inspect_mid ($$) {
168 my ($lei, $mid) = @_;
170 my $ent = { mid => $mid };
171 if (defined(my $dir = $lei->{opt}->{dir})) {
172 $ibx = dir2ibx($lei, $dir)
176 if ($ibx && $ibx->over) {
178 while (my $smsg = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
179 push @{$ent->{smsg}}, _json_prep($smsg);
182 if ($ibx && $ibx->search) {
183 my $mset = $ibx->search->mset(qq{mid:"$mid"});
184 for (sort { $a->get_docid <=> $b->get_docid } $mset->items) {
185 my $tmp = { docid => $_->get_docid };
186 _inspect_doc($tmp, $_->get_document);
187 push @{$ent->{xdoc}}, $tmp;
194 my ($lei, $item, $more) = @_;
196 if ($item =~ /\Ablob:(.+)/) {
197 $ent = inspect_blob($lei, $1);
198 } elsif ($item =~ m!\A(?:maildir|mh):!i || -d $item) {
199 $ent = inspect_sync_folder($lei, $item);
200 } elsif ($item =~ m!\Adocid:([0-9]+)\z!) {
201 $ent = inspect_docid($lei, $1 + 0);
202 } elsif ($item =~ m!\Anum:([0-9]+)\z!) {
203 $ent = inspect_num($lei, $1 + 0);
204 } elsif ($item =~ m!\A(?:mid|m):(.+)\z!) {
205 $ent = inspect_mid($lei, $1);
206 } elsif (my $iuri = imap_uri($item)) {
207 if (defined($iuri->uid)) {
208 $ent = inspect_imap_uid($lei, $iuri);
210 $ent = inspect_sync_folder($lei, $item);
212 } elsif (my $nuri = nntp_uri($item)) {
213 if (defined(my $mid = $nuri->message)) {
214 $ent = inspect_mid($lei, $mid);
216 my ($group, $beg, $end) = $nuri->group;
218 $ent = inspect_nntp_range($lei, $nuri);
220 $ent = inspect_sync_folder($lei, $item);
223 } else { # TODO: more things
224 return $lei->fail("$item not understood");
226 $lei->out($lei->{json}->encode($ent));
227 $lei->out(',') if $more;
231 sub inspect_argv { # via wq_do
233 my ($lei, $argv) = delete @$self{qw(lei argv)};
234 my $multi = scalar(@$argv) > 1;
235 $lei->{1}->autoflush(0);
236 $lei->out('[') if $multi;
237 while (defined(my $x = shift @$argv)) {
238 inspect1($lei, $x, scalar(@$argv)) or return;
240 $lei->out(']') if $multi;
243 sub inspect_start ($$) {
244 my ($lei, $argv) = @_;
245 my $self = bless { lei => $lei, argv => $argv }, __PACKAGE__;
246 my ($op_c, $ops) = $lei->workers_start($self, 1);
248 $lei->wait_wq_events($op_c, $ops);
249 $self->wq_do('inspect_argv');
253 sub ins_add { # InputPipe->consume callback
254 my ($lei) = @_; # $_[1] = $rbuf
256 $_[1] eq '' and return eval {
257 my $str = delete $lei->{istr};
258 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
259 my $eml = PublicInbox::Eml->new(\$str);
260 inspect_start($lei, [
261 'blob:'.$lei->git_oid($eml)->hexdigest,
262 map { "mid:$_" } @{mids($eml)} ]);
264 $lei->{istr} .= $_[1];
266 $lei->fail("error reading stdin: $!");
271 my ($lei, @argv) = @_;
272 $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
273 $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
274 my $sto = $lei->_lei_store;
275 $sto ? $sto->search : undef;
277 my $isatty = -t $lei->{1};
278 $lei->{json}->pretty(1)->indent(2) if $lei->{opt}->{pretty} || $isatty;
279 $lei->start_pager if $isatty;
280 if ($lei->{opt}->{stdin}) {
281 return $lei->fail(<<'') if @argv;
282 no args allowed on command-line with --stdin
284 require PublicInbox::InputPipe;
285 PublicInbox::InputPipe::consume($lei->{0}, \&ins_add, $lei);
287 inspect_start($lei, \@argv);
291 sub _complete_inspect {
292 require PublicInbox::LeiRefreshMailSync;
293 PublicInbox::LeiRefreshMailSync::_complete_refresh_mail_sync(@_);
294 # TODO: message-ids?, blobs? could get expensive...
297 sub input_only_atfork_child {
299 $self->{lei}->_lei_atfork_child;
300 $self->SUPER::ipc_atfork_child;