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