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 PublicInbox::Config;
13 sub inspect_blob ($$) {
14 my ($lei, $oidhex) = @_;
16 if (my $lse = $lei->{lse}) {
17 my @docids = $lse ? $lse->over->blob_exists($oidhex) : ();
18 $ent->{'lei/store'} = \@docids if @docids;
20 if (my $loc = $lms ? $lms->locations_for($oidhex) : undef) {
21 $ent->{'mail-sync'} = $loc;
27 sub inspect_imap_uid ($$) {
28 my ($lei, $uid_uri) = @_;
30 my $lse = $lei->{lse} or return $ent;
31 my $lms = $lse->lms or return $ent;
32 my $oidhex = $lms->imap_oid($lei, $uid_uri);
33 if (ref(my $err = $oidhex)) { # art2folder error
34 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
36 $ent->{$$uid_uri} = $oidhex;
40 sub inspect_sync_folder ($$) {
41 my ($lei, $folder) = @_;
43 my $lse = $lei->{lse} or return $ent;
44 my $lms = $lse->lms or return $ent;
45 my $folders = [ $folder ];
46 my $err = $lms->arg2folder($lei, $folders);
49 $lei->qerr("# no folders match $folder (non-fatal)");
52 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
54 for my $f (@$folders) {
55 $ent->{$f} = $lms->location_stats($f); # may be undef
61 my ($lei, $item, $more) = @_;
63 if ($item =~ /\Ablob:(.+)/) {
64 $ent = inspect_blob($lei, $1);
65 } elsif ($item =~ m!\Aimaps?://!i) {
66 require PublicInbox::URIimap;
67 my $uri = PublicInbox::URIimap->new($item);
68 if (defined($uri->uid)) {
69 $ent = inspect_imap_uid($lei, $uri);
71 $ent = inspect_sync_folder($lei, $item);
73 } elsif ($item =~ m!\A(?:maildir|mh):!i || -d $item) {
74 $ent = inspect_sync_folder($lei, $item);
75 } else { # TODO: more things
76 return $lei->fail("$item not understood");
78 $lei->out($lei->{json}->encode($ent));
79 $lei->out(',') if $more;
84 my ($lei, @argv) = @_;
85 $lei->{1}->autoflush(0);
86 my $multi = scalar(@argv) > 1;
87 $lei->out('[') if $multi;
88 $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
89 $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
90 my $sto = $lei->_lei_store;
91 $sto ? $sto->search : undef;
93 if ($lei->{opt}->{pretty} || -t $lei->{1}) {
94 $lei->{json}->pretty(1)->indent(2);
96 while (defined(my $x = shift @argv)) {
97 inspect1($lei, $x, scalar(@argv)) or return;
99 $lei->out(']') if $multi;
102 sub _complete_inspect {
103 my ($lei, @argv) = @_;
104 my $sto = $lei->_lei_store or return;
105 my $lms = $sto->search->lms or return;
106 my $match_cb = $lei->complete_url_prepare(\@argv);
107 map { $match_cb->($_) } $lms->folders;