]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
config: support "inboxdir" in addition to "mainrepo"
[public-inbox.git] / lib / PublicInbox / Feed.pm
1 # Copyright (C) 2013-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Used for generating Atom feeds for web-accessible mailing list archives.
5 package PublicInbox::Feed;
6 use strict;
7 use warnings;
8 use PublicInbox::MIME;
9 use PublicInbox::View;
10 use PublicInbox::WwwAtomStream;
11 use PublicInbox::SearchMsg; # this loads w/o Search::Xapian
12
13 # main function
14 sub generate {
15         my ($ctx) = @_;
16         my $msgs = recent_msgs($ctx);
17         return _no_thread() unless @$msgs;
18
19         my $ibx = $ctx->{-inbox};
20         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
21                 while (my $smsg = shift @$msgs) {
22                         $ibx->smsg_mime($smsg) and return $smsg;
23                 }
24         });
25 }
26
27 sub generate_thread_atom {
28         my ($ctx) = @_;
29         my $mid = $ctx->{mid};
30         my $ibx = $ctx->{-inbox};
31         my $msgs = $ibx->over->get_thread($mid);
32         return _no_thread() unless @$msgs;
33
34         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
35                 while (my $smsg = shift @$msgs) {
36                         $ibx->smsg_mime($smsg) and return $smsg;
37                 }
38         });
39 }
40
41 sub generate_html_index {
42         my ($ctx) = @_;
43         # if the 'r' query parameter is given, it is a legacy permalink
44         # which we must continue supporting:
45         my $qp = $ctx->{qp};
46         my $ibx = $ctx->{-inbox};
47         if ($qp && !$qp->{r} && $ibx->over) {
48                 return PublicInbox::View::index_topics($ctx);
49         }
50
51         my $env = $ctx->{env};
52         my $url = $ibx->base_url($env) . 'new.html';
53         my $qs = $env->{QUERY_STRING};
54         $url .= "?$qs" if $qs ne '';
55         [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
56                 [ "Redirecting to $url\n" ] ];
57 }
58
59 sub new_html {
60         my ($ctx) = @_;
61         my $msgs = recent_msgs($ctx);
62         if (!@$msgs) {
63                 return [404, ['Content-Type', 'text/plain'],
64                         ["No messages, yet\n"] ];
65         }
66         $ctx->{-html_tip} = '<pre>';
67         $ctx->{-upfx} = '';
68         $ctx->{-hr} = 1;
69         my $ibx = $ctx->{-inbox};
70         PublicInbox::WwwStream->response($ctx, 200, sub {
71                 while (my $smsg = shift @$msgs) {
72                         my $m = $ibx->smsg_mime($smsg) or next;
73                         my $more = scalar @$msgs;
74                         return PublicInbox::View::index_entry($m, $ctx, $more);
75                 }
76                 PublicInbox::View::pagination_footer($ctx, './new.html');
77         });
78 }
79
80 # private subs
81
82 sub _no_thread () {
83         [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
84 }
85
86 sub recent_msgs {
87         my ($ctx) = @_;
88         my $ibx = $ctx->{-inbox};
89         my $max = $ibx->{feedmax};
90         my $qp = $ctx->{qp};
91         my $v = $ibx->{version} || 1;
92         if ($v > 2) {
93                 die "BUG: unsupported inbox version: $v\n";
94         }
95         if ($ibx->over) {
96                 return PublicInbox::View::paginate_recent($ctx, $max);
97         }
98
99         my $hex = '[a-f0-9]';
100         my $addmsg = qr!^:000000 100644 \S+ (\S+) A\t${hex}{2}/${hex}{38}$!;
101         my $delmsg = qr!^:100644 000000 (\S+) \S+ D\t(${hex}{2}/${hex}{38})$!;
102         my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~[0-9]+)?/;
103
104         # revision ranges may be specified
105         my $range = 'HEAD';
106         my $r = $qp->{r} if $qp;
107         if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
108                 $range = $r;
109         }
110
111         # get recent messages
112         # we could use git log -z, but, we already know ssoma will not
113         # leave us with filenames with spaces in them..
114         my $log = $ibx->git->popen(qw/log
115                                 --no-notes --no-color --raw -r
116                                 --no-abbrev --abbrev-commit/,
117                                 "--format=%H", $range);
118         my %deleted; # only an optimization at this point
119         my $last;
120         my $last_commit;
121         local $/ = "\n";
122         my @oids;
123         while (defined(my $line = <$log>)) {
124                 if ($line =~ /$addmsg/o) {
125                         my $add = $1;
126                         next if $deleted{$add}; # optimization-only
127                         push @oids, $add;
128                         if (scalar(@oids) >= $max) {
129                                 $last = 1;
130                                 last;
131                         }
132                 } elsif ($line =~ /$delmsg/o) {
133                         $deleted{$1} = 1;
134                 }
135         }
136
137         if ($last) {
138                 local $/ = "\n";
139                 while (my $line = <$log>) {
140                         if ($line =~ /^(${hex}{7,40})/) {
141                                 $last_commit = $1;
142                                 last;
143                         }
144                 }
145         }
146
147         $ctx->{next_page} = "r=$last_commit" if $last_commit;
148         [ map { bless {blob => $_ }, 'PublicInbox::SearchMsg' } @oids ];
149 }
150
151 1;