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_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 my @oidhex = $lms->imap_oidhex($lei, $uri);
35 push @{$lei->{lcat_blob}}, @oidhex;
36 } elsif (defined(my $fid = $lms->fid_for($$uri))) {
37 push @{$lei->{lcat_fid}}, $fid;
39 lcat_folder($lei, $lms, $$uri);
45 if ($x =~ m!\b(imaps?://[^>]+)!i) {
47 require PublicInbox::URIimap;
48 lcat_imap_uri($lei, PublicInbox::URIimap->new($u));
49 '""'; # blank query, using {lcat_blob} or {lcat_fid}
50 } elsif ($x =~ m!\b(maildir:.+)!i) {
51 lcat_folder($lei, undef, $1);
52 '""'; # blank query, using {lcat_blob} or {lcat_fid}
53 } elsif ($x =~ m!\b([a-z]+?://\S+)!i) {
55 $u =~ s/[\>\]\)\,\.\;]+\z//;
60 if ($p =~ m!([^/]+\@[^/]+)!) { # common msgid pattern
61 $term = 'mid:'.uri_unescape($1);
63 # is it a URL which returns the full thread?
64 if ($u->scheme =~ /\Ahttps?/i &&
65 $p =~ m!/(?:T/?|t/?|t\.mbox\.gz|t\.atom)\b!) {
67 $lei->{mset_opt}->{threads} = 1;
69 } elsif ($u->scheme =~ /\Ahttps?/i &&
70 # some msgids don't have '@', see if it looks like
72 $p =~ m!/([^/]+)/(raw|t/?|T/?|
73 t\.mbox\.gz|t\.atom)\z!x) {
74 $lei->{mset_opt}->{threads} = 1 if $2 && $2 ne 'raw';
75 $term = 'mid:'.uri_unescape($1);
78 } elsif ($x =~ $MID_EXTRACT) { # <$MSGID>
80 } elsif ($x =~ /\b((?:m|mid):\S+)/) { # our own prefixes (and mairix)
82 } elsif ($x =~ /\bid:(\S+)/) { # notmuch convention
84 } elsif ($x =~ /\bblob:([0-9a-f]{7,})\b/) {
85 push @{$lei->{lcat_blob}}, $1; # cf. LeiToMail->wq_atexit_child
93 my ($lei, @argv) = @_;
94 my $strict = !$lei->{opt}->{stdin};
97 if (my $term = extract_1($lei,$x)) {
100 return $lei->fail(<<"");
101 could not extract Message-ID from $x
105 @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
108 sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
109 my ($lei) = @_; # $_[1] = $rbuf
110 if (defined($_[1])) {
111 $_[1] eq '' and return eval {
112 $lei->fchdir or return;
113 my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
114 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv)
118 $lei->{mset_opt}->{qstr} .= $_[1];
120 $lei->fail("error reading stdin: $!");
125 my ($lei, @argv) = @_;
126 my $lxs = $lei->lxs_prepare or return;
127 $lei->ale->refresh_externals($lxs, $lei);
129 my $opt = $lei->{opt};
130 my %mset_opt = map { $_ => $opt->{$_} } qw(threads limit offset);
131 $mset_opt{asc} = $opt->{'reverse'} ? 1 : 0;
132 $mset_opt{limit} //= 10000;
133 $opt->{sort} //= 'relevance';
134 $mset_opt{relevance} = 1;
135 $lei->{mset_opt} = \%mset_opt;
136 $opt->{'format'} //= 'text' unless defined($opt->{output});
137 if ($lei->{opt}->{stdin}) {
138 return $lei->fail(<<'') if @argv;
139 no args allowed on command-line with --stdin
141 require PublicInbox::InputPipe;
142 PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei);
145 $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;
150 my ($lei, @argv) = @_;
151 my $lms = $lei->lms or return;
152 my $match_cb = $lei->complete_url_prepare(\@argv);
153 map { $match_cb->($_) } $lms->folders;