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);
12 use PublicInbox::MID qw($MID_EXTRACT);
14 sub lcat_imap_uid_uri ($$) {
15 my ($lei, $uid_uri) = @_;
16 my $lms = $lei->{lse}->lms or return;
17 my $oidhex = $lms->imap_oid($lei, $uid_uri);
18 if (ref(my $err = $oidhex)) { # art2folder error
19 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
21 push @{$lei->{lcat_blob}}, $oidhex; # cf. LeiToMail->wq_atexit_child
26 if ($x =~ m!\b(imaps?://[^>]+)!i) {
28 require PublicInbox::URIimap;
29 $u = PublicInbox::URIimap->new($u);
30 defined($u->uid) ? lcat_imap_uid_uri($lei, $u) :
31 $lei->child_error(1 << 8, "# no UID= in $u");
32 '""'; # blank query, using {lcat_blob}
33 } elsif ($x =~ m!\b([a-z]+?://\S+)!i) {
35 $u =~ s/[\>\]\)\,\.\;]+\z//;
40 if ($p =~ m!([^/]+\@[^/]+)!) { # common msgid pattern
41 $term = 'mid:'.uri_unescape($1);
43 # is it a URL which returns the full thread?
44 if ($u->scheme =~ /\Ahttps?/i &&
45 $p =~ m!/(?:T/?|t/?|t\.mbox\.gz|t\.atom)\b!) {
47 $lei->{mset_opt}->{threads} = 1;
49 } elsif ($u->scheme =~ /\Ahttps?/i &&
50 # some msgids don't have '@', see if it looks like
52 $p =~ m!/([^/]+)/(raw|t/?|T/?|
53 t\.mbox\.gz|t\.atom)\z!x) {
54 $lei->{mset_opt}->{threads} = 1 if $2 && $2 ne 'raw';
55 $term = 'mid:'.uri_unescape($1);
58 } elsif ($x =~ $MID_EXTRACT) { # <$MSGID>
60 } elsif ($x =~ /\b((?:m|mid):\S+)/) { # our own prefixes (and mairix)
62 } elsif ($x =~ /\bid:(\S+)/) { # notmuch convention
64 } elsif ($x =~ /\bblob:([0-9a-f]{7,})\b/) {
65 push @{$lei->{lcat_blob}}, $1; # cf. LeiToMail->wq_atexit_child
73 my ($lei, @argv) = @_;
74 my $strict = !$lei->{opt}->{stdin};
77 if (my $term = extract_1($lei,$x)) {
80 return $lei->fail(<<"");
81 could not extract Message-ID from $x
85 @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
88 sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
89 my ($lei) = @_; # $_[1] = $rbuf
91 $_[1] eq '' and return eval {
92 if (my $dfd = $lei->{3}) {
93 chdir($dfd) or return $lei->fail("fchdir: $!");
95 my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
96 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv)
100 $lei->{mset_opt}->{qstr} .= $_[1];
102 $lei->fail("error reading stdin: $!");
107 my ($lei, @argv) = @_;
108 my $lxs = $lei->lxs_prepare or return;
109 $lei->ale->refresh_externals($lxs);
110 my $sto = $lei->_lei_store(1);
111 $lei->{lse} = $sto->search;
112 my $opt = $lei->{opt};
113 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
114 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
115 $mset_opt{limit} //= 10000;
116 $opt->{sort} //= 'relevance';
117 $mset_opt{relevance} = 1;
118 $lei->{mset_opt} = \%mset_opt;
119 $opt->{'format'} //= 'text' unless defined($opt->{output});
120 if ($lei->{opt}->{stdin}) {
121 return $lei->fail(<<'') if @argv;
122 no args allowed on command-line with --stdin
124 require PublicInbox::InputPipe;
125 PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei);
128 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;