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 sub { emit_html_index($_[0], $ctx) };
41 $title =~ tr/\t\n / /s; # squeeze spaces
42 # try to avoid the type attribute in title:
43 $title = ascii_html($title);
44 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
45 "<title$type>$title</title>";
49 my ($feed_opts, $title) = @_;
51 $title = title_tag($feed_opts->{description}) unless (defined $title);
53 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
54 qq{<feed\nxmlns="http://www.w3.org/2005/Atom">} .
56 qq(<link\nrel="alternate"\ntype="text/html") .
57 qq(\nhref="$feed_opts->{url}"/>) .
58 qq(<link\nrel="self"\nhref="$feed_opts->{atomurl}"/>) .
59 qq(<id>mailto:$feed_opts->{id_addr}</id>);
64 my $feed_opts = get_feedopts($ctx);
65 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
66 my $max = $ctx->{max} || MAX_PER_PAGE;
67 my $x = atom_header($feed_opts);
68 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
69 each_recent_blob($ctx, sub {
70 my ($path, undef, $ts) = @_;
72 $fh->write($x . feed_updated(undef, $ts));
75 add_to_feed($feed_opts, $fh, $path, $git);
82 my $fh = $cb->([404, ['Content-Type' => 'text/plain']]);
83 $fh->write("No feed found for thread\n");
89 $fh->write('</feed>');
93 sub emit_atom_thread {
95 my $res = $ctx->{srch}->get_thread($ctx->{mid});
96 return _no_thread($cb) unless $res->{total};
97 my $feed_opts = get_feedopts($ctx);
98 my $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
100 my $html_url = $feed_opts->{atomurl} = $ctx->{self_url};
101 $html_url =~ s!/t\.atom\z!/!;
102 $feed_opts->{url} = $html_url;
103 $feed_opts->{emit_header} = 1;
105 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
106 foreach my $msg (@{$res->{msgs}}) {
107 add_to_feed($feed_opts, $fh, mid2path($msg->mid), $git);
112 sub _html_index_top {
113 my ($feed_opts, $srch) = @_;
115 my $title = ascii_html($feed_opts->{description} || '');
116 my $top = "<b>$title</b> (<a\nhref=\"new.atom\">Atom feed</a>)";
118 $top = qq{<form\naction=""><pre>$top} .
119 qq{ <input\nname=q\ntype=text />} .
120 qq{<input\ntype=submit\nvalue=search />} .
121 q{</pre></form><pre>}
123 $top = '<pre>' . $top . "\n";
126 "<html><head><title>$title</title>" .
127 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
128 "href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
129 PublicInbox::Hval::STYLE .
133 sub emit_html_index {
134 my ($res, $ctx) = @_;
135 my $feed_opts = get_feedopts($ctx);
136 my $fh = $res->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
138 my $max = $ctx->{max} || MAX_PER_PAGE;
140 my ($footer, $param, $last);
141 my $state = { ctx => $ctx, seen => {}, anchor_idx => 0, fh => $fh };
142 my $srch = $ctx->{srch};
143 $fh->write(_html_index_top($feed_opts, $srch));
145 # if the 'r' query parameter is given, it is a legacy permalink
146 # which we must continue supporting:
148 if ($qp && !$qp->{r} && $srch) {
149 $state->{srch} = $srch;
150 $last = PublicInbox::View::emit_index_topics($state);
153 $last = emit_index_nosrch($ctx, $state);
156 $footer = nav_footer($ctx, $last, $feed_opts, $state, $param);
158 my $list_footer = $ctx->{footer};
159 $footer .= "\n\n" . $list_footer if $list_footer;
160 $footer = "<hr /><pre>$footer</pre>";
162 $fh->write("$footer</body></html>");
166 sub emit_index_nosrch {
167 my ($ctx, $state) = @_;
168 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
169 my (undef, $last) = each_recent_blob($ctx, sub {
170 my ($path, $commit, $ts, $u, $subj) = @_;
171 $state->{first} ||= $commit;
173 my $mime = do_cat_mail($git, $path) or return 0;
174 PublicInbox::View::index_entry($mime, 0, $state);
181 my ($ctx, $last, $feed_opts, $state, $param) = @_;
182 my $qp = $ctx->{qp} or return '';
183 my $old_r = $qp->{$param};
186 my $first = $state->{first};
187 my $anchor = $state->{anchor_idx};
190 $next = qq!<a\nhref="?$param=$last"\nrel=next>next</a>!;
193 $head = $ctx->{env}->{PATH_INFO};
194 $head = qq!<a\nhref="$head">head</a>!;
196 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">Atom feed</a>";
197 "<a\nname=\"s$anchor\">page:</a> $next $head $atom";
200 sub each_recent_blob {
202 my $max = $ctx->{max} || MAX_PER_PAGE;
203 my $hex = '[a-f0-9]';
204 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
205 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
206 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
209 # revision ranges may be specified
211 my $r = $qp->{r} if $qp;
212 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
216 # get recent messages
217 # we could use git log -z, but, we already know ssoma will not
218 # leave us with filenames with spaces in them..
219 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
220 my $log = $git->popen(qw/log --no-notes --no-color --raw -r
221 --abbrev=16 --abbrev-commit/,
222 "--format=%h%x00%ct%x00%an%x00%s%x00",
224 my %deleted; # only an optimization at this point
227 my ($cur_commit, $first_commit, $last_commit);
230 while (defined(my $line = <$log>)) {
231 if ($line =~ /$addmsg/o) {
233 next if $deleted{$add}; # optimization-only
234 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
239 } elsif ($line =~ /$delmsg/o) {
241 } elsif ($line =~ /^${hex}{7,40}/o) {
242 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
243 unless (defined $first_commit) {
244 $first_commit = $cur_commit;
251 while (my $line = <$log>) {
252 if ($line =~ /^(${hex}{7,40})/o) {
260 ($first_commit, $last_commit);
263 # private functions below
266 my $pi_config = $ctx->{pi_config};
267 my $inbox = $ctx->{inbox};
268 my $obj = $ctx->{-inbox};
269 my $cgi = $ctx->{cgi};
270 my %rv = ( description => $obj ? $obj->description : 'FIXME' );
273 $rv{address} = $obj->{address};
274 $rv{id_addr} = $obj->{-primary_address};
275 } elsif ($pi_config && defined $inbox && $inbox ne '') {
277 my $addr = $pi_config->get($inbox, 'address') || "";
278 $rv{address} = $addr;
279 $addr = $addr->[0] if ref($addr);
280 $rv{id_addr} = $addr;
282 $rv{id_addr} ||= 'public-inbox@example.com';
286 $url_base = $obj->base_url($cgi); # CGI may be undef
287 if (my $mid = $ctx->{mid}) { # per-thread feed:
288 $rv{atomurl} = "$url_base$mid/t.atom";
290 $rv{atomurl} = $url_base."new.atom";
293 $url_base = 'http://example.com/';
294 $rv{atomurl} = $url_base.'new.atom';
296 $rv{url} ||= $url_base;
297 $rv{midurl} = $url_base;
303 my ($date, $ts) = @_;
304 my @t = eval { strptime($date) } if defined $date;
305 @t = gmtime($ts || time) unless scalar @t;
307 '<updated>' . strftime(DATEFMT, @t) . '</updated>';
310 # returns 0 (skipped) or 1 (added)
312 my ($feed_opts, $fh, $add, $git) = @_;
314 my $mime = do_cat_mail($git, $add) or return 0;
315 my $url = $feed_opts->{url};
316 my $midurl = $feed_opts->{midurl};
318 my $header_obj = $mime->header_obj;
319 my $mid = $header_obj->header_raw('Message-ID');
320 defined $mid or return 0;
321 $mid = PublicInbox::Hval->new_msgid($mid);
322 my $href = $midurl.$mid->as_href;
324 my $content = qq(<pre\nstyle="white-space:pre-wrap">) .
325 PublicInbox::View::multipart_text_as_html($mime, $href) .
327 my $date = $header_obj->header('Date');
328 my $updated = feed_updated($date);
330 my $title = $header_obj->header('Subject');
331 defined $title or return 0;
332 $title = title_tag($title);
334 my $from = $header_obj->header('From') or return 0;
335 my ($email) = PublicInbox::Address::emails($from);
336 my $name = PublicInbox::Address::from_name($from);
337 $name = ascii_html($name);
338 $email = ascii_html($email);
340 if (delete $feed_opts->{emit_header}) {
341 $fh->write(atom_header($feed_opts, $title) . $updated);
343 $fh->write("<entry><author><name>$name</name><email>$email</email>" .
344 "</author>$title$updated" .
345 qq{<content\ntype="xhtml">} .
346 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
347 $fh->write($content);
351 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
352 my $id = 'urn:uuid:' . join('-', @uuid5);
353 $fh->write(qq!</div></content><link\nhref="$href/"/>!.
354 "<id>$id</id></entry>");
359 my ($git, $path) = @_;
361 my $str = $git->cat_file("HEAD:$path");
362 Email::MIME->new($str);