user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
eb2634b42a1233e11254efa918cc6f1dd55607f6 blob 2954 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
 
# Copyright (C) 2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# "lei inspect" general purpose inspector for stuff in SQLite and
# Xapian.  Will eventually be useful with plain public-inboxes,
# not just lei/store.  This is totally half-baked at the moment
# but useful for testing.
package PublicInbox::LeiInspect;
use strict;
use v5.10.1;
use PublicInbox::Config;

sub inspect_blob ($$) {
	my ($lei, $oidhex) = @_;
	my $ent = {};
	if (my $lse = $lei->{lse}) {
		my @docids = $lse ? $lse->over->blob_exists($oidhex) : ();
		$ent->{'lei/store'} = \@docids if @docids;
		my $lms = $lse->lms;
		if (my $loc = $lms ? $lms->locations_for($oidhex) : undef) {
			$ent->{'mail-sync'} = $loc;
		}
	}
	$ent;
}

sub inspect_imap_uid ($$) {
	my ($lei, $uid_uri) = @_;
	my $ent = {};
	my $lse = $lei->{lse} or return $ent;
	my $lms = $lse->lms or return $ent;
	my $oidhex = $lms->imap_oid($lei, $uid_uri);
	if (ref(my $err = $oidhex)) { # art2folder error
		$lei->qerr(@{$err->{qerr}}) if $err->{qerr};
	}
	$ent->{$$uid_uri} = $oidhex;
	$ent;
}

sub inspect_sync_folder ($$) {
	my ($lei, $folder) = @_;
	my $ent = {};
	my $lse = $lei->{lse} or return $ent;
	my $lms = $lse->lms or return $ent;
	my $folders = [ $folder ];
	my $err = $lms->arg2folder($lei, $folders);
	if ($err) {
		if ($err->{fail}) {
			$lei->qerr("# no folders match $folder (non-fatal)");
			@$folders = ();
		}
		$lei->qerr(@{$err->{qerr}}) if $err->{qerr};
	}
	for my $f (@$folders) {
		$ent->{$f} = $lms->location_stats($f); # may be undef
	}
	$ent
}

sub inspect1 ($$$) {
	my ($lei, $item, $more) = @_;
	my $ent;
	if ($item =~ /\Ablob:(.+)/) {
		$ent = inspect_blob($lei, $1);
	} elsif ($item =~ m!\Aimaps?://!i) {
		require PublicInbox::URIimap;
		my $uri = PublicInbox::URIimap->new($item);
		if (defined($uri->uid)) {
			$ent = inspect_imap_uid($lei, $uri);
		} else {
			$ent = inspect_sync_folder($lei, $item);
		}
	} elsif ($item =~ m!\A(?:maildir|mh):!i || -d $item) {
		$ent = inspect_sync_folder($lei, $item);
	} else { # TODO: more things
		return $lei->fail("$item not understood");
	}
	$lei->out($lei->{json}->encode($ent));
	$lei->out(',') if $more;
	1;
}

sub lei_inspect {
	my ($lei, @argv) = @_;
	$lei->{1}->autoflush(0);
	my $multi = scalar(@argv) > 1;
	$lei->out('[') if $multi;
	$lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
	$lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
		my $sto = $lei->_lei_store;
		$sto ? $sto->search : undef;
	} : undef;
	if ($lei->{opt}->{pretty} || -t $lei->{1}) {
		$lei->{json}->pretty(1)->indent(2);
	}
	while (defined(my $x = shift @argv)) {
		inspect1($lei, $x, scalar(@argv)) or return;
	}
	$lei->out(']') if $multi;
}

sub _complete_inspect {
	my ($lei, @argv) = @_;
	my $sto = $lei->_lei_store or return;
	my $lms = $sto->search->lms or return;
	my $match_cb = $lei->complete_url_prepare(\@argv);
	map { $match_cb->($_) } $lms->folders;
}

1;
debug log:

solving eb2634b4 ...
found eb2634b4 in public-inbox.git

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).