]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiLcat.pm
ccb1823d8824b0a32b80db979aa9528dd1d46e9f
[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_folder ($$$) {
15         my ($lei, $lms, $folder) = @_;
16         $lms //= $lei->lms or return;
17         my $folders = [ $folder];
18         eval { $lms->arg2folder($lei, $folders) };
19         if ($@) {
20                 $lei->child_error(0, "# unknown folder: $folder");
21         } else {
22                 for my $f (@$folders) {
23                         my $fid = $lms->fid_for($f);
24                         push @{$lei->{lcat_fid}}, $fid;
25                 }
26         }
27 }
28
29 sub lcat_imap_uri ($$) {
30         my ($lei, $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;
38         } else {
39                 lcat_folder($lei, $lms, $$uri);
40         }
41 }
42
43 sub extract_1 ($$) {
44         my ($lei, $x) = @_;
45         if ($x =~ m!\b(imaps?://[^>]+)!i) {
46                 my $u = $1;
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) {
54                 my $u = $1;
55                 $u =~ s/[\>\]\)\,\.\;]+\z//;
56                 require URI;
57                 $u = URI->new($u);
58                 my $p = $u->path;
59                 my $term;
60                 if ($p =~ m!([^/]+\@[^/]+)!) { # common msgid pattern
61                         $term = 'mid:'.uri_unescape($1);
62
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!) {
66
67                                 $lei->{mset_opt}->{threads} = 1;
68                         }
69                 } elsif ($u->scheme =~ /\Ahttps?/i &&
70                                 # some msgids don't have '@', see if it looks like
71                                 # a public-inbox URL:
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);
76                 }
77                 $term;
78         } elsif ($x =~ $MID_EXTRACT) { # <$MSGID>
79                 "mid:$1";
80         } elsif ($x =~ /\b((?:m|mid):\S+)/) { # our own prefixes (and mairix)
81                 $1;
82         } elsif ($x =~ /\bid:(\S+)/) { # notmuch convention
83                 "mid:$1";
84         } elsif ($x =~ /\bblob:([0-9a-f]{7,})\b/) {
85                 push @{$lei->{lcat_blob}}, $1; # cf. LeiToMail->wq_atexit_child
86                 '""'; # blank query
87         } else {
88                 undef;
89         }
90 }
91
92 sub extract_all {
93         my ($lei, @argv) = @_;
94         my $strict = !$lei->{opt}->{stdin};
95         my @q;
96         for my $x (@argv) {
97                 if (my $term = extract_1($lei,$x)) {
98                         push @q, $term;
99                 } elsif ($strict) {
100                         return $lei->fail(<<"");
101 could not extract Message-ID from $x
102
103                 }
104         }
105         @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv");
106 }
107
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)
115                                 or return;
116                         $lei->_start_query;
117                 };
118                 $lei->{mset_opt}->{qstr} .= $_[1];
119         } else {
120                 $lei->fail("error reading stdin: $!");
121         }
122 }
123
124 sub lei_lcat {
125         my ($lei, @argv) = @_;
126         my $lxs = $lei->lxs_prepare or return;
127         $lei->ale->refresh_externals($lxs, $lei);
128         $lei->_lei_store(1);
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
140
141                 require PublicInbox::InputPipe;
142                 PublicInbox::InputPipe::consume($lei->{0}, \&_stdin, $lei);
143                 return;
144         }
145         $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;
146         $lei->_start_query;
147 }
148
149 sub _complete_lcat {
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;
154 }
155
156 1;