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);
16 sub inspect_blob ($$) {
17 my ($lei, $oidhex) = @_;
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;
24 if (my $loc = $lms ? $lms->locations_for($oidbin) : undef) {
25 $ent->{'mail-sync'} = $loc;
31 sub inspect_imap_uid ($$) {
32 my ($lei, $uid_uri) = @_;
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);
41 sub inspect_nntp_range {
43 my ($ng, $beg, $end) = $uri->group;
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 $@;
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);
62 sub inspect_sync_folder ($$) {
63 my ($lei, $folder) = @_;
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
75 sub inspect_docid ($$;$) {
76 my ($lei, $docid, $ent) = @_;
77 require PublicInbox::Search;
80 if ($xdb = delete $ent->{xdb}) { # from inspect_num
81 } elsif (defined(my $dir = $lei->{opt}->{dir})) {
83 $xdb = $PublicInbox::Search::X{Database}->new($dir);
85 $xdb = $lei->{lse}->xdb;
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;
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;
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';
125 $lei->fail("no (indexed) inbox or extindex at $dir");
129 sub inspect_num ($$) {
130 my ($lei, $num) = @_;
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);
141 $lei->{lse}->xdb; # set {nshard} for num2docid
142 $docid = $lei->{lse}->num2docid($num);
144 if ($ibx && $ibx->over) {
145 my $smsg = $ibx->over->get_art($num);
146 $ent->{smsg} = { %$smsg } if $smsg;
148 defined($docid) ? inspect_docid($lei, $docid, $ent) : $ent;
151 sub inspect_mid ($$) {
152 my ($lei, $mid) = @_;
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");
162 $lei->{lse}->xdb; # set {nshard} for num2docid
164 if ($ibx && $ibx->over) {
166 while (my $smsg = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
167 push @{$ent->{smsg}}, { %$smsg }
174 my ($lei, $item, $more) = @_;
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);
190 $ent = inspect_sync_folder($lei, $item);
192 } elsif (my $nuri = nntp_uri($item)) {
193 if (defined(my $mid = $nuri->message)) {
194 $ent = inspect_mid($lei, $mid);
196 my ($group, $beg, $end) = $nuri->group;
198 $ent = inspect_nntp_range($lei, $nuri);
200 $ent = inspect_sync_folder($lei, $item);
203 } else { # TODO: more things
204 return $lei->fail("$item not understood");
206 $lei->out($lei->{json}->encode($ent));
207 $lei->out(',') if $more;
211 sub inspect_argv { # via wq_do
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;
220 $lei->out(']') if $multi;
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);
228 $lei->wait_wq_events($op_c, $ops);
229 $self->wq_do('inspect_argv');
233 sub ins_add { # InputPipe->consume callback
234 my ($lei) = @_; # $_[1] = $rbuf
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)} ]);
244 $lei->{istr} .= $_[1];
246 $lei->fail("error reading stdin: $!");
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;
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
264 require PublicInbox::InputPipe;
265 PublicInbox::InputPipe::consume($lei->{0}, \&ins_add, $lei);
267 inspect_start($lei, \@argv);
271 sub _complete_inspect {
272 require PublicInbox::LeiRefreshMailSync;
273 PublicInbox::LeiRefreshMailSync::_complete_refresh_mail_sync(@_);
274 # TODO: message-ids?, blobs? could get expensive...