]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiLcat.pm
lei import|lcat: improve+fix single message IMAP support
[public-inbox.git] / lib / PublicInbox / LeiLcat.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
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;
8 use strict;
9 use v5.10.1;
10 use PublicInbox::LeiViewText;
11 use URI::Escape qw(uri_unescape);
12 use PublicInbox::MID qw($MID_EXTRACT);
13
14 sub lcat_imap_uri ($$) {
15         my ($lei, $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};
22                 }
23                 push @{$lei->{lcat_blob}}, $oidhex;
24         } elsif (defined(my $fid = $lms->fid_for($$uri))) {
25                 push @{$lei->{lcat_fid}}, $fid;
26         } else {
27                 $lei->child_error(1 << 8, "# unknown folder: $uri");
28         }
29 }
30
31 sub extract_1 ($$) {
32         my ($lei, $x) = @_;
33         if ($x =~ m!\b(imaps?://[^>]+)!i) {
34                 my $u = $1;
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) {
39                 my $u = $1;
40                 $u =~ s/[\>\]\)\,\.\;]+\z//;
41                 require URI;
42                 $u = URI->new($u);
43                 my $p = $u->path;
44                 my $term;
45                 if ($p =~ m!([^/]+\@[^/]+)!) { # common msgid pattern
46                         $term = 'mid:'.uri_unescape($1);
47
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!) {
51
52                                 $lei->{mset_opt}->{threads} = 1;
53                         }
54                 } elsif ($u->scheme =~ /\Ahttps?/i &&
55                                 # some msgids don't have '@', see if it looks like
56                                 # a public-inbox URL:
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);
61                 }
62                 $term;
63         } elsif ($x =~ $MID_EXTRACT) { # <$MSGID>
64                 "mid:$1";
65         } elsif ($x =~ /\b((?:m|mid):\S+)/) { # our own prefixes (and mairix)
66                 $1;
67         } elsif ($x =~ /\bid:(\S+)/) { # notmuch convention
68                 "mid:$1";
69         } elsif ($x =~ /\bblob:([0-9a-f]{7,})\b/) {
70                 push @{$lei->{lcat_blob}}, $1; # cf. LeiToMail->wq_atexit_child
71                 '""'; # blank query
72         } else {
73                 undef;
74         }
75 }
76
77 sub extract_all {
78         my ($lei, @argv) = @_;
79         my $strict = !$lei->{opt}->{stdin};
80         my @q;
81         for my $x (@argv) {
82                 if (my $term = extract_1($lei,$x)) {
83                         push @q, $term;
84                 } elsif ($strict) {
85                         return $lei->fail(<<"");
86 could not extract Message-ID from $x
87
88                 }
89         }
90         @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
91 }
92
93 sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
94         my ($lei) = @_; # $_[1] = $rbuf
95         if (defined($_[1])) {
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)
100                                 or return;
101                         $lei->_start_query;
102                 };
103                 $lei->{mset_opt}->{qstr} .= $_[1];
104         } else {
105                 $lei->fail("error reading stdin: $!");
106         }
107 }
108
109 sub lei_lcat {
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
126
127                 require PublicInbox::InputPipe;
128                 PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei);
129                 return;
130         }
131         $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;
132         $lei->_start_query;
133 }
134
135 1;