]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
update copyrights for 2018
[public-inbox.git] / lib / PublicInbox / Feed.pm
1 # Copyright (C) 2013-2018 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
12 # main function
13 sub generate {
14         my ($ctx) = @_;
15         my @paths;
16         each_recent_blob($ctx, sub { push @paths, $_[0] });
17         return _no_thread() unless @paths;
18
19         my $ibx = $ctx->{-inbox};
20         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
21                 while (my $path = shift @paths) {
22                         my $mime = do_cat_mail($ibx, $path) or next;
23                         return $mime;
24                 }
25         });
26 }
27
28 sub generate_thread_atom {
29         my ($ctx) = @_;
30         my $mid = $ctx->{mid};
31         my $res = $ctx->{srch}->get_thread($mid);
32         return _no_thread() unless $res->{total};
33
34         my $ibx = $ctx->{-inbox};
35         my $html_url = $ibx->base_url($ctx->{env});
36         $html_url .= PublicInbox::Hval->new_msgid($mid)->{href};
37         $ctx->{-html_url} = $html_url;
38         my $msgs = $res->{msgs};
39         PublicInbox::WwwAtomStream->response($ctx, 200, sub {
40                 while (my $msg = shift @$msgs) {
41                         $msg = $ibx->msg_by_smsg($msg) and
42                                 return PublicInbox::MIME->new($msg);
43                 }
44         });
45 }
46
47 sub generate_html_index {
48         my ($ctx) = @_;
49         # if the 'r' query parameter is given, it is a legacy permalink
50         # which we must continue supporting:
51         my $qp = $ctx->{qp};
52         if ($qp && !$qp->{r} && $ctx->{srch}) {
53                 return PublicInbox::View::index_topics($ctx);
54         }
55
56         my $env = $ctx->{env};
57         my $url = $ctx->{-inbox}->base_url($env) . 'new.html';
58         my $qs = $env->{QUERY_STRING};
59         $url .= "?$qs" if $qs ne '';
60         [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
61                 [ "Redirecting to $url\n" ] ];
62 }
63
64 sub new_html {
65         my ($ctx) = @_;
66         my @paths;
67         my (undef, $last) = each_recent_blob($ctx, sub {
68                 my ($path, $commit, $ts, $u, $subj) = @_;
69                 $ctx->{first} ||= $commit;
70                 push @paths, $path;
71         });
72         if (!@paths) {
73                 return [404, ['Content-Type', 'text/plain'],
74                         ["No messages, yet\n"] ];
75         }
76         $ctx->{-html_tip} = '<pre>';
77         $ctx->{-upfx} = '';
78         $ctx->{-hr} = 1;
79         PublicInbox::WwwStream->response($ctx, 200, sub {
80                 while (my $path = shift @paths) {
81                         my $m = do_cat_mail($ctx->{-inbox}, $path) or next;
82                         my $more = scalar @paths;
83                         my $s = PublicInbox::View::index_entry($m, $ctx, $more);
84                         return $s;
85                 }
86                 new_html_footer($ctx, $last);
87         });
88 }
89
90 # private subs
91
92 sub _no_thread () {
93         [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
94 }
95
96 sub new_html_footer {
97         my ($ctx, $last) = @_;
98         my $qp = delete $ctx->{qp} or return;
99         my $old_r = $qp->{r};
100         my $latest = '';
101         my $next = '    ';
102
103         if ($last) {
104                 $next = qq!<a\nhref="?r=$last"\nrel=next>next</a>!;
105         }
106         if ($old_r) {
107                 $latest = qq! <a\nhref='./new.html'>latest</a>!;
108         }
109         "<hr><pre>page: $next$latest</pre>";
110 }
111
112 sub each_recent_blob {
113         my ($ctx, $cb) = @_;
114         my $max = $ctx->{-inbox}->{feedmax};
115         my $hex = '[a-f0-9]';
116         my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
117         my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
118         my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
119         my $qp = $ctx->{qp};
120
121         # revision ranges may be specified
122         my $range = 'HEAD';
123         my $r = $qp->{r} if $qp;
124         if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
125                 $range = $r;
126         }
127
128         # get recent messages
129         # we could use git log -z, but, we already know ssoma will not
130         # leave us with filenames with spaces in them..
131         my $log = $ctx->{-inbox}->git->popen(qw/log
132                                 --no-notes --no-color --raw -r
133                                 --abbrev=16 --abbrev-commit/,
134                                 "--format=%h%x00%ct%x00%an%x00%s%x00",
135                                 $range);
136         my %deleted; # only an optimization at this point
137         my $last;
138         my $nr = 0;
139         my ($cur_commit, $first_commit, $last_commit);
140         my ($ts, $subj, $u);
141         local $/ = "\n";
142         while (defined(my $line = <$log>)) {
143                 if ($line =~ /$addmsg/o) {
144                         my $add = $1;
145                         next if $deleted{$add}; # optimization-only
146                         $cb->($add, $cur_commit, $ts, $u, $subj) and $nr++;
147                         if ($nr >= $max) {
148                                 $last = 1;
149                                 last;
150                         }
151                 } elsif ($line =~ /$delmsg/o) {
152                         $deleted{$1} = 1;
153                 } elsif ($line =~ /^${hex}{7,40}/o) {
154                         ($cur_commit, $ts, $u, $subj) = split("\0", $line);
155                         unless (defined $first_commit) {
156                                 $first_commit = $cur_commit;
157                         }
158                 }
159         }
160
161         if ($last) {
162                 local $/ = "\n";
163                 while (my $line = <$log>) {
164                         if ($line =~ /^(${hex}{7,40})/o) {
165                                 $last_commit = $1;
166                                 last;
167                         }
168                 }
169         }
170
171         # for pagination
172         ($first_commit, $last_commit);
173 }
174
175 sub do_cat_mail {
176         my ($ibx, $path) = @_;
177         my $mime = eval { $ibx->msg_by_path($path) } or return;
178         PublicInbox::MIME->new($mime);
179 }
180
181 1;