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