]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
run update-copyrights from gnulib for 2019
[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         my $html_url = $ibx->base_url($ctx->{env});
35         $html_url .= PublicInbox::Hval->new_msgid($mid)->{href};
36         $ctx->{-html_url} = $html_url;
37         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
38                 while (my $smsg = shift @$msgs) {
39                         $ibx->smsg_mime($smsg) and return $smsg;
40                 }
41         });
42 }
43
44 sub generate_html_index {
45         my ($ctx) = @_;
46         # if the 'r' query parameter is given, it is a legacy permalink
47         # which we must continue supporting:
48         my $qp = $ctx->{qp};
49         my $ibx = $ctx->{-inbox};
50         if ($qp && !$qp->{r} && $ibx->over) {
51                 return PublicInbox::View::index_topics($ctx);
52         }
53
54         my $env = $ctx->{env};
55         my $url = $ibx->base_url($env) . 'new.html';
56         my $qs = $env->{QUERY_STRING};
57         $url .= "?$qs" if $qs ne '';
58         [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
59                 [ "Redirecting to $url\n" ] ];
60 }
61
62 sub new_html {
63         my ($ctx) = @_;
64         my $msgs = recent_msgs($ctx);
65         if (!@$msgs) {
66                 return [404, ['Content-Type', 'text/plain'],
67                         ["No messages, yet\n"] ];
68         }
69         $ctx->{-html_tip} = '<pre>';
70         $ctx->{-upfx} = '';
71         $ctx->{-hr} = 1;
72         my $ibx = $ctx->{-inbox};
73         PublicInbox::WwwStream->response($ctx, 200, sub {
74                 while (my $smsg = shift @$msgs) {
75                         my $m = $ibx->smsg_mime($smsg) or next;
76                         my $more = scalar @$msgs;
77                         return PublicInbox::View::index_entry($m, $ctx, $more);
78                 }
79                 PublicInbox::View::pagination_footer($ctx, './new.html');
80         });
81 }
82
83 # private subs
84
85 sub _no_thread () {
86         [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
87 }
88
89 sub recent_msgs {
90         my ($ctx) = @_;
91         my $ibx = $ctx->{-inbox};
92         my $max = $ibx->{feedmax};
93         my $qp = $ctx->{qp};
94         my $v = $ibx->{version} || 1;
95         if ($v > 2) {
96                 die "BUG: unsupported inbox version: $v\n";
97         }
98         if ($ibx->over) {
99                 return PublicInbox::View::paginate_recent($ctx, $max);
100         }
101
102         my $hex = '[a-f0-9]';
103         my $addmsg = qr!^:000000 100644 \S+ (\S+) A\t${hex}{2}/${hex}{38}$!;
104         my $delmsg = qr!^:100644 000000 (\S+) \S+ D\t(${hex}{2}/${hex}{38})$!;
105         my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~[0-9]+)?/;
106
107         # revision ranges may be specified
108         my $range = 'HEAD';
109         my $r = $qp->{r} if $qp;
110         if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
111                 $range = $r;
112         }
113
114         # get recent messages
115         # we could use git log -z, but, we already know ssoma will not
116         # leave us with filenames with spaces in them..
117         my $log = $ibx->git->popen(qw/log
118                                 --no-notes --no-color --raw -r
119                                 --no-abbrev --abbrev-commit/,
120                                 "--format=%H", $range);
121         my %deleted; # only an optimization at this point
122         my $last;
123         my $last_commit;
124         local $/ = "\n";
125         my @oids;
126         while (defined(my $line = <$log>)) {
127                 if ($line =~ /$addmsg/o) {
128                         my $add = $1;
129                         next if $deleted{$add}; # optimization-only
130                         push @oids, $add;
131                         if (scalar(@oids) >= $max) {
132                                 $last = 1;
133                                 last;
134                         }
135                 } elsif ($line =~ /$delmsg/o) {
136                         $deleted{$1} = 1;
137                 }
138         }
139
140         if ($last) {
141                 local $/ = "\n";
142                 while (my $line = <$log>) {
143                         if ($line =~ /^(${hex}{7,40})/) {
144                                 $last_commit = $1;
145                                 last;
146                         }
147                 }
148         }
149
150         $ctx->{next_page} = "r=$last_commit" if $last_commit;
151         [ map { bless {blob => $_ }, 'PublicInbox::SearchMsg' } @oids ];
152 }
153
154 1;