]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInspect.pm
lei inspect: use LeiMailSync->match_imap_url
[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 PublicInbox::Config;
12
13 sub inspect_blob ($$) {
14         my ($lei, $oidhex) = @_;
15         my $ent = {};
16         if (my $lse = $lei->{lse}) {
17                 my @docids = $lse ? $lse->over->blob_exists($oidhex) : ();
18                 $ent->{'lei/store'} = \@docids if @docids;
19                 my $lms = $lse->lms;
20                 if (my $loc = $lms ? $lms->locations_for($oidhex) : undef) {
21                         $ent->{'mail-sync'} = $loc;
22                 }
23         }
24         $ent;
25 }
26
27 sub inspect_sync_folder ($$) {
28         my ($lei, $folder) = @_;
29         my $ent = {};
30         my $lse = $lei->{lse} or return $ent;
31         my $lms = $lse->lms or return $ent;
32         my @folders;
33         if ($folder =~ m!\Aimaps?://!i) {
34                 @folders = map { $_->as_string } $lms->match_imap_url($folder);
35         } elsif ($folder =~ m!\A(maildir|mh):(.+)!i) {
36                 my $type = lc $1;
37                 $folders[0] = "$type:".$lei->abs_path($2);
38         } elsif (-d $folder) {
39                 $folders[0] = 'maildir:'.$lei->abs_path($folder);
40         } else {
41                 $lei->fail("$folder not understood");
42         }
43         $lei->qerr("# no folders match $folder (non-fatal)") if !@folders;
44         for my $f (@folders) {
45                 $ent->{$f} = $lms->location_stats($f); # may be undef
46         }
47         $ent
48 }
49
50 sub inspect1 ($$$) {
51         my ($lei, $item, $more) = @_;
52         my $ent;
53         if ($item =~ /\Ablob:(.+)/) {
54                 $ent = inspect_blob($lei, $1);
55         } elsif ($item =~ m!\Aimaps?://!i ||
56                         $item =~ m!\A(?:maildir|mh):!i || -d $item) {
57                 $ent = inspect_sync_folder($lei, $item);
58         } else { # TODO: more things
59                 return $lei->fail("$item not understood");
60         }
61         $lei->out($lei->{json}->encode($ent));
62         $lei->out(',') if $more;
63         1;
64 }
65
66 sub lei_inspect {
67         my ($lei, @argv) = @_;
68         $lei->{1}->autoflush(0);
69         my $multi = scalar(@argv) > 1;
70         $lei->out('[') if $multi;
71         $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
72         $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
73                 my $sto = $lei->_lei_store;
74                 $sto ? $sto->search : undef;
75         } : undef;
76         if ($lei->{opt}->{pretty} || -t $lei->{1}) {
77                 $lei->{json}->pretty(1)->indent(2);
78         }
79         while (defined(my $x = shift @argv)) {
80                 inspect1($lei, $x, scalar(@argv)) or return;
81         }
82         $lei->out(']') if $multi;
83 }
84
85 1;