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_uri ($$) {
16 my $lms = $lei->{lse}->lms or return;
17 # cf. LeiToMail->wq_atexit_child
18 if (defined $uri->uid) {
19 my $oidhex = $lms->imap_oid($lei, $uri);
20 if (ref(my $err = $oidhex)) { # art2folder error
21 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
23 push @{$lei->{lcat_blob}}, $oidhex;
24 } elsif (defined(my $fid = $lms->fid_for($$uri))) {
25 push @{$lei->{lcat_fid}}, $fid;
27 $lei->child_error(1 << 8, "# unknown folder: $uri");
33 if ($x =~ m!\b(imaps?://[^>]+)!i) {
35 require PublicInbox::URIimap;
36 lcat_imap_uri($lei, PublicInbox::URIimap->new($u));
37 '""'; # blank query, using {lcat_blob} or {lcat
38 } elsif ($x =~ m!\b([a-z]+?://\S+)!i) {
40 $u =~ s/[\>\]\)\,\.\;]+\z//;
45 if ($p =~ m!([^/]+\@[^/]+)!) { # common msgid pattern
46 $term = 'mid:'.uri_unescape($1);
48 # is it a URL which returns the full thread?
49 if ($u->scheme =~ /\Ahttps?/i &&
50 $p =~ m!/(?:T/?|t/?|t\.mbox\.gz|t\.atom)\b!) {
52 $lei->{mset_opt}->{threads} = 1;
54 } elsif ($u->scheme =~ /\Ahttps?/i &&
55 # some msgids don't have '@', see if it looks like
57 $p =~ m!/([^/]+)/(raw|t/?|T/?|
58 t\.mbox\.gz|t\.atom)\z!x) {
59 $lei->{mset_opt}->{threads} = 1 if $2 && $2 ne 'raw';
60 $term = 'mid:'.uri_unescape($1);
63 } elsif ($x =~ $MID_EXTRACT) { # <$MSGID>
65 } elsif ($x =~ /\b((?:m|mid):\S+)/) { # our own prefixes (and mairix)
67 } elsif ($x =~ /\bid:(\S+)/) { # notmuch convention
69 } elsif ($x =~ /\bblob:([0-9a-f]{7,})\b/) {
70 push @{$lei->{lcat_blob}}, $1; # cf. LeiToMail->wq_atexit_child
78 my ($lei, @argv) = @_;
79 my $strict = !$lei->{opt}->{stdin};
82 if (my $term = extract_1($lei,$x)) {
85 return $lei->fail(<<"");
86 could not extract Message-ID from $x
90 @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
93 sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
94 my ($lei) = @_; # $_[1] = $rbuf
96 $_[1] eq '' and return eval {
97 $lei->fchdir or return;
98 my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
99 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv)
103 $lei->{mset_opt}->{qstr} .= $_[1];
105 $lei->fail("error reading stdin: $!");
110 my ($lei, @argv) = @_;
111 my $lxs = $lei->lxs_prepare or return;
112 $lei->ale->refresh_externals($lxs);
113 my $sto = $lei->_lei_store(1);
114 $lei->{lse} = $sto->search;
115 my $opt = $lei->{opt};
116 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
117 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
118 $mset_opt{limit} //= 10000;
119 $opt->{sort} //= 'relevance';
120 $mset_opt{relevance} = 1;
121 $lei->{mset_opt} = \%mset_opt;
122 $opt->{'format'} //= 'text' unless defined($opt->{output});
123 if ($lei->{opt}->{stdin}) {
124 return $lei->fail(<<'') if @argv;
125 no args allowed on command-line with --stdin
127 require PublicInbox::InputPipe;
128 PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei);
131 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;
136 my ($lei, @argv) = @_;
137 my $sto = $lei->_lei_store or return;
138 my $lms = $sto->search->lms or return;
139 my $match_cb = $lei->complete_url_prepare(\@argv);
140 grep(m!\A[a-z]+://!, map { $match_cb->($_) } $lms->folders);