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)
4 # Used for generating Atom feeds for web-accessible mailing list archives.
5 package PublicInbox::Feed;
9 use Date::Parse qw(strptime);
10 use PublicInbox::Hval qw/ascii_html/;
12 use PublicInbox::View;
13 use PublicInbox::MID qw/mid_clean mid2path/;
14 use PublicInbox::Address;
15 use POSIX qw/strftime/;
17 DATEFMT => '%Y-%m-%dT%H:%M:%SZ', # Atom standard
18 MAX_PER_PAGE => 25, # this needs to be tunable
24 sub { emit_atom($_[0], $ctx) };
27 sub generate_thread_atom {
29 sub { emit_atom_thread($_[0], $ctx) };
32 sub generate_html_index {
34 # if the 'r' query parameter is given, it is a legacy permalink
35 # which we must continue supporting:
37 if ($qp && !$qp->{r} && $ctx->{srch}) {
38 return PublicInbox::View::index_topics($ctx);
41 my $env = $ctx->{env};
42 my $url = $ctx->{-inbox}->base_url($env) . 'new.html';
43 my $qs = $env->{QUERY_STRING};
44 $url .= "?$qs" if $qs ne '';
45 [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
46 [ "Redirecting to $url\n" ] ];
52 my (undef, $last) = each_recent_blob($ctx, sub {
53 my ($path, $commit, $ts, $u, $subj) = @_;
54 $ctx->{first} ||= $commit;
58 return [404, ['Content-Type', 'text/plain'],
59 ["No messages, yet\n"] ];
61 $ctx->{-html_tip} = '<pre>';
63 PublicInbox::WwwStream->response($ctx, 200, sub {
64 while (my $path = shift @paths) {
65 my $m = do_cat_mail($ctx->{-inbox}, $path) or next;
66 my $more = scalar @paths;
67 my $s = PublicInbox::View::index_entry($m, $ctx, $more);
68 $s .= '</pre>' unless $more;
71 new_html_footer($ctx, $last);
79 $title =~ tr/\t\n / /s; # squeeze spaces
80 # try to avoid the type attribute in title:
81 $title = ascii_html($title);
82 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
83 "<title$type>$title</title>";
87 my ($feed_opts, $title) = @_;
89 $title = title_tag($feed_opts->{description}) unless (defined $title);
91 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
92 qq{<feed\nxmlns="http://www.w3.org/2005/Atom">} .
94 qq(<link\nrel="alternate"\ntype="text/html") .
95 qq(\nhref="$feed_opts->{url}"/>) .
96 qq(<link\nrel="self"\nhref="$feed_opts->{atomurl}"/>) .
97 qq(<id>mailto:$feed_opts->{id_addr}</id>);
102 my $feed_opts = get_feedopts($ctx);
103 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
104 my $max = $ctx->{max} || MAX_PER_PAGE;
105 my $x = atom_header($feed_opts);
106 my $ibx = $ctx->{-inbox};
107 each_recent_blob($ctx, sub {
108 my ($path, undef, $ts) = @_;
110 $fh->write($x . feed_updated(undef, $ts));
113 my $s = feed_entry($feed_opts, $path, $ibx) or return 0;
122 $cb->([404, ['Content-Type', 'text/plain'],
123 ["No feed found for thread\n"]]);
128 $fh->write('</feed>');
132 sub emit_atom_thread {
134 my $mid = $ctx->{mid};
135 my $res = $ctx->{srch}->get_thread($mid);
136 return _no_thread($cb) unless $res->{total};
137 my $feed_opts = get_feedopts($ctx);
138 my $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
139 my $ibx = $ctx->{-inbox};
140 my $html_url = $ibx->base_url($ctx->{env});
141 $html_url .= PublicInbox::Hval->new_msgid($mid)->as_href;
143 $feed_opts->{url} = $html_url;
144 $feed_opts->{emit_header} = 1;
146 foreach my $msg (@{$res->{msgs}}) {
147 my $s = feed_entry($feed_opts, mid2path($msg->mid), $ibx);
148 $fh->write($s) if defined $s;
153 sub _html_index_top {
154 my ($feed_opts, $srch) = @_;
156 my $title = ascii_html($feed_opts->{description} || '');
157 my $top = "<b>$title</b> (<a\nhref=\"new.atom\">Atom feed</a>)";
159 $top = qq{<form\naction=""><pre>$top} .
160 qq{ <input\nname=q\ntype=text />} .
161 qq{<input\ntype=submit\nvalue=search />} .
162 q{</pre></form><pre>}
164 $top = '<pre>' . $top . "\n";
167 "<html><head><title>$title</title>" .
168 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
169 "href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
170 PublicInbox::Hval::STYLE .
174 sub new_html_footer {
175 my ($ctx, $last) = @_;
176 my $qp = delete $ctx->{qp} or return;
177 my $old_r = $qp->{r};
182 $next = qq!<a\nhref="?r=$last"\nrel=next>next</a>!;
185 $latest = qq! <a\nhref='./new.html'>latest</a>!;
187 "<hr><pre>page: $next$latest</pre>";
190 sub each_recent_blob {
192 my $max = $ctx->{max} || MAX_PER_PAGE;
193 my $hex = '[a-f0-9]';
194 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
195 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
196 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
199 # revision ranges may be specified
201 my $r = $qp->{r} if $qp;
202 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
206 # get recent messages
207 # we could use git log -z, but, we already know ssoma will not
208 # leave us with filenames with spaces in them..
209 my $log = $ctx->{-inbox}->git->popen(qw/log
210 --no-notes --no-color --raw -r
211 --abbrev=16 --abbrev-commit/,
212 "--format=%h%x00%ct%x00%an%x00%s%x00",
214 my %deleted; # only an optimization at this point
217 my ($cur_commit, $first_commit, $last_commit);
220 while (defined(my $line = <$log>)) {
221 if ($line =~ /$addmsg/o) {
223 next if $deleted{$add}; # optimization-only
224 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
229 } elsif ($line =~ /$delmsg/o) {
231 } elsif ($line =~ /^${hex}{7,40}/o) {
232 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
233 unless (defined $first_commit) {
234 $first_commit = $cur_commit;
241 while (my $line = <$log>) {
242 if ($line =~ /^(${hex}{7,40})/o) {
250 ($first_commit, $last_commit);
253 # private functions below
256 my $inbox = $ctx->{inbox};
257 my $obj = $ctx->{-inbox};
258 my %rv = ( description => $obj->description );
260 $rv{address} = $obj->{address};
261 $rv{id_addr} = $obj->{-primary_address};
262 my $url_base = $obj->base_url($ctx->{env});
263 if (my $mid = $ctx->{mid}) { # per-thread feed:
264 $rv{atomurl} = "$url_base$mid/t.atom";
266 $rv{atomurl} = $url_base."new.atom";
268 $rv{url} ||= $url_base;
269 $rv{midurl} = $url_base;
275 my ($date, $ts) = @_;
276 my @t = eval { strptime($date) } if defined $date;
277 @t = gmtime($ts || time) unless scalar @t;
279 '<updated>' . strftime(DATEFMT, @t) . '</updated>';
282 # returns undef or string
284 my ($feed_opts, $add, $ibx) = @_;
286 my $mime = do_cat_mail($ibx, $add) or return;
287 my $url = $feed_opts->{url};
288 my $midurl = $feed_opts->{midurl};
290 my $header_obj = $mime->header_obj;
291 my $mid = $header_obj->header_raw('Message-ID');
292 defined $mid or return;
293 $mid = PublicInbox::Hval->new_msgid($mid);
294 my $href = $midurl . $mid->as_href . '/';
296 my $date = $header_obj->header('Date');
297 my $updated = feed_updated($date);
299 my $title = $header_obj->header('Subject');
300 defined $title or return;
301 $title = title_tag($title);
303 my $from = $header_obj->header('From') or return;
304 my ($email) = PublicInbox::Address::emails($from);
305 my $name = join(', ',PublicInbox::Address::names($from));
306 $name = ascii_html($name);
307 $email = ascii_html($email);
310 if (delete $feed_opts->{emit_header}) {
311 $s .= atom_header($feed_opts, $title) . $updated;
313 $s .= "<entry><author><name>$name</name><email>$email</email>" .
314 "</author>$title$updated" .
315 qq{<content\ntype="xhtml">} .
316 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
317 qq(<pre\nstyle="white-space:pre-wrap">) .
318 PublicInbox::View::multipart_text_as_html($mime, $href) .
323 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
324 my $id = 'urn:uuid:' . join('-', @uuid5);
325 $s .= qq!</div></content><link\nhref="$href"/>!.
326 "<id>$id</id></entry>";
330 my ($ibx, $path) = @_;
331 my $mime = eval { $ibx->msg_by_path($path) } or return;
332 Email::MIME->new($mime);