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 # lcat: local cat, display a local message by Message-ID or blob,
5 # extracting from URL necessary
6 # "lei lcat <URL|SPEC>"
7 package PublicInbox::LeiLcat;
10 use PublicInbox::LeiViewText;
11 use URI::Escape qw(uri_unescape);
13 use PublicInbox::MID qw($MID_EXTRACT);
16 my ($lei, $out, $op_p) = @_;
17 my $l = bless { %$lei }, ref($lei);
19 $l->{''} = $op_p; # daemon only
21 $l->qerr("# updating $out");
23 $l->qerr("# $out done");
30 if ($x =~ m!\b([a-z]+?://\S+)!i) {
32 $u =~ s/[\>\]\)\,\.\;]+\z//;
36 if ($p =~ m!([^/]+\@[^/]+)!) { # common msgid pattern
37 $term = 'mid:'.uri_unescape($1);
39 # is it a URL which returns the full thread?
40 if ($u->scheme =~ /\Ahttps?/i &&
41 $p =~ m!/(?:T/?|t/?|t\.mbox\.gz|t\.atom)\b!) {
43 $lei->{mset_opt}->{threads} = 1;
45 } elsif ($u->scheme =~ /\Ahttps?/i &&
46 # some msgids don't have '@', see if it looks like
48 $p =~ m!/([^/]+)/(raw|t/?|T/?|
49 t\.mbox\.gz|t\.atom)\z!x) {
50 $lei->{mset_opt}->{threads} = 1 if $2 && $2 ne 'raw';
51 $term = 'mid:'.uri_unescape($1);
54 } elsif ($x =~ $MID_EXTRACT) { # <$MSGID>
56 } elsif ($x =~ /\b((?:m|mid):\S+)/) { # our own prefixes (and mairix)
58 } elsif ($x =~ /\bid:(\S+)/) { # notmuch convention
66 my ($lei, @argv) = @_;
67 my $strict = !$lei->{opt}->{stdin};
70 if (my $term = extract_1($lei,$x)) {
73 return $lei->fail(<<"");
74 could not extract Message-ID from $x
78 @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
81 sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
82 my ($lei) = @_; # $_[1] = $rbuf
84 $_[1] eq '' and return eval {
85 if (my $dfd = $lei->{3}) {
86 chdir($dfd) or return $lei->fail("fchdir: $!");
88 my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
89 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv)
93 $lei->{mset_opt}->{qstr} .= $_[1];
95 $lei->fail("error reading stdin: $!");
100 my ($lei, @argv) = @_;
101 my $lxs = $lei->lxs_prepare or return;
102 $lei->ale->refresh_externals($lxs);
103 my $sto = $lei->_lei_store(1);
104 $lei->{lse} = $sto->search;
105 my $opt = $lei->{opt};
106 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
107 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
108 $mset_opt{limit} //= 10000;
109 $opt->{sort} //= 'relevance';
110 $mset_opt{relevance} = 1;
111 $lei->{mset_opt} = \%mset_opt;
112 $opt->{'format'} //= 'mboxrd' unless defined($opt->{output});
113 if ($lei->{opt}->{stdin}) {
114 return $lei->fail(<<'') if @argv;
115 no args allowed on command-line with --stdin
117 require PublicInbox::InputPipe;
118 PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei);
121 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;