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 eval { $lms->arg2folder($lei, $folders) };
20 $lei->child_error(0, "# unknown folder: $folder");
22 for my $f (@$folders) {
23 my $fid = $lms->fid_for($f);
24 push @{$lei->{lcat_todo}}, { fid => $fid };
29 sub lcat_imap_uri ($$) {
31 my $lms = $lei->lms or return;
32 # cf. LeiXsearch->lcat_dump
33 if (defined $uri->uid) {
34 push @{$lei->{lcat_todo}}, $lms->imap_oidhex($lei, $uri);
35 } elsif (defined(my $fid = $lms->fid_for($$uri))) {
36 push @{$lei->{lcat_todo}}, { fid => $fid };
38 lcat_folder($lei, $lms, $$uri);
44 if ($x =~ m!\b(imaps?://[^>]+)!i) {
46 require PublicInbox::URIimap;
47 lcat_imap_uri($lei, PublicInbox::URIimap->new($u));
48 '""'; # blank query, using {lcat_todo}
49 } elsif ($x =~ m!\b(maildir:.+)!i) {
50 lcat_folder($lei, undef, $1);
51 '""'; # blank query, using {lcat_todo}
52 } elsif ($x =~ m!\b([a-z]+?://\S+)!i) {
54 $u =~ s/[\>\]\)\,\.\;]+\z//;
59 if ($p =~ m!([^/]+\@[^/]+)!) { # common msgid pattern
60 $term = 'mid:'.uri_unescape($1);
62 # is it a URL which returns the full thread?
63 if ($u->scheme =~ /\Ahttps?/i &&
64 $p =~ m!/(?:T/?|t/?|t\.mbox\.gz|t\.atom)\b!) {
66 $lei->{mset_opt}->{threads} = 1;
68 } elsif ($u->scheme =~ /\Ahttps?/i &&
69 # some msgids don't have '@', see if it looks like
71 $p =~ m!/([^/]+)/(raw|t/?|T/?|
72 t\.mbox\.gz|t\.atom)\z!x) {
73 $lei->{mset_opt}->{threads} = 1 if $2 && $2 ne 'raw';
74 $term = 'mid:'.uri_unescape($1);
77 } elsif ($x =~ $MID_EXTRACT) { # <$MSGID>
79 } elsif ($x =~ /\b((?:m|mid):\S+)/) { # our own prefixes (and mairix)
81 } elsif ($x =~ /\bid:(\S+)/) { # notmuch convention
83 } elsif ($x =~ /\bblob:([0-9a-f]{7,})\b/) {
84 push @{$lei->{lcat_todo}}, $1; # cf. LeiToMail->wq_atexit_child
92 my ($lei, @argv) = @_;
93 my $strict = !$lei->{opt}->{stdin};
96 if (my $term = extract_1($lei,$x)) {
99 return $lei->fail(<<"");
100 could not extract Message-ID from $x
104 @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
107 sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
108 my ($lei) = @_; # $_[1] = $rbuf
109 if (defined($_[1])) {
110 $_[1] eq '' and return eval {
111 $lei->fchdir or return;
112 my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
113 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv)
117 $lei->{mset_opt}->{qstr} .= $_[1];
119 $lei->fail("error reading stdin: $!");
124 my ($lei, @argv) = @_;
125 my $lxs = $lei->lxs_prepare or return;
126 $lei->ale->refresh_externals($lxs, $lei);
128 my $opt = $lei->{opt};
129 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
130 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
131 $mset_opt{limit} //= 10000;
132 $opt->{sort} //= 'relevance';
133 $mset_opt{relevance} = 1;
134 $lei->{mset_opt} = \%mset_opt;
135 $opt->{'format'} //= 'text' unless defined($opt->{output});
136 if ($lei->{opt}->{stdin}) {
137 return $lei->fail(<<'') if @argv;
138 no args allowed on command-line with --stdin
140 require PublicInbox::InputPipe;
141 PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei);
144 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;
149 my ($lei, @argv) = @_;
150 my $lms = $lei->lms or return;
151 my $match_cb = $lei->complete_url_prepare(\@argv);
152 map { $match_cb->($_) } $lms->folders;