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_folder ($$$) {
15 my ($lei, $lms, $folder) = @_;
16 $lms //= $lei->lms or return;
17 my $folders = [ $folder];
18 my $err = $lms->arg2folder($lei, $folders);
19 $lei->qerr(@{$err->{qerr}}) if $err && $err->{qerr};
20 if ($err && $err->{fail}) {
21 $lei->child_error(0, "# unknown folder: $folder");
23 for my $f (@$folders) {
24 my $fid = $lms->fid_for($f);
25 push @{$lei->{lcat_fid}}, $fid;
30 sub lcat_imap_uri ($$) {
32 my $lms = $lei->lms or return;
33 # cf. LeiXsearch->lcat_dump
34 if (defined $uri->uid) {
35 my @oidhex = $lms->imap_oidhex($lei, $uri);
36 push @{$lei->{lcat_blob}}, @oidhex;
37 } elsif (defined(my $fid = $lms->fid_for($$uri))) {
38 push @{$lei->{lcat_fid}}, $fid;
40 lcat_folder($lei, $lms, $$uri);
46 if ($x =~ m!\b(imaps?://[^>]+)!i) {
48 require PublicInbox::URIimap;
49 lcat_imap_uri($lei, PublicInbox::URIimap->new($u));
50 '""'; # blank query, using {lcat_blob} or {lcat_fid}
51 } elsif ($x =~ m!\b(maildir:.+)!i) {
52 lcat_folder($lei, undef, $1);
53 '""'; # blank query, using {lcat_blob} or {lcat_fid}
54 } elsif ($x =~ m!\b([a-z]+?://\S+)!i) {
56 $u =~ s/[\>\]\)\,\.\;]+\z//;
61 if ($p =~ m!([^/]+\@[^/]+)!) { # common msgid pattern
62 $term = 'mid:'.uri_unescape($1);
64 # is it a URL which returns the full thread?
65 if ($u->scheme =~ /\Ahttps?/i &&
66 $p =~ m!/(?:T/?|t/?|t\.mbox\.gz|t\.atom)\b!) {
68 $lei->{mset_opt}->{threads} = 1;
70 } elsif ($u->scheme =~ /\Ahttps?/i &&
71 # some msgids don't have '@', see if it looks like
73 $p =~ m!/([^/]+)/(raw|t/?|T/?|
74 t\.mbox\.gz|t\.atom)\z!x) {
75 $lei->{mset_opt}->{threads} = 1 if $2 && $2 ne 'raw';
76 $term = 'mid:'.uri_unescape($1);
79 } elsif ($x =~ $MID_EXTRACT) { # <$MSGID>
81 } elsif ($x =~ /\b((?:m|mid):\S+)/) { # our own prefixes (and mairix)
83 } elsif ($x =~ /\bid:(\S+)/) { # notmuch convention
85 } elsif ($x =~ /\bblob:([0-9a-f]{7,})\b/) {
86 push @{$lei->{lcat_blob}}, $1; # cf. LeiToMail->wq_atexit_child
94 my ($lei, @argv) = @_;
95 my $strict = !$lei->{opt}->{stdin};
98 if (my $term = extract_1($lei,$x)) {
101 return $lei->fail(<<"");
102 could not extract Message-ID from $x
106 @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
109 sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
110 my ($lei) = @_; # $_[1] = $rbuf
111 if (defined($_[1])) {
112 $_[1] eq '' and return eval {
113 $lei->fchdir or return;
114 my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
115 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv)
119 $lei->{mset_opt}->{qstr} .= $_[1];
121 $lei->fail("error reading stdin: $!");
126 my ($lei, @argv) = @_;
127 my $lxs = $lei->lxs_prepare or return;
128 $lei->ale->refresh_externals($lxs, $lei);
130 my $opt = $lei->{opt};
131 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
132 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
133 $mset_opt{limit} //= 10000;
134 $opt->{sort} //= 'relevance';
135 $mset_opt{relevance} = 1;
136 $lei->{mset_opt} = \%mset_opt;
137 $opt->{'format'} //= 'text' unless defined($opt->{output});
138 if ($lei->{opt}->{stdin}) {
139 return $lei->fail(<<'') if @argv;
140 no args allowed on command-line with --stdin
142 require PublicInbox::InputPipe;
143 PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei);
146 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;
151 my ($lei, @argv) = @_;
152 my $lms = $lei->lms or return;
153 my $match_cb = $lei->complete_url_prepare(\@argv);
154 map { $match_cb->($_) } $lms->folders;