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;
10 use Date::Parse qw(strptime);
11 use PublicInbox::Hval;
13 use PublicInbox::View;
14 use PublicInbox::MID qw/mid_clean mid2path/;
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
21 use Encode qw/find_encoding/;
22 my $enc_utf8 = find_encoding('UTF-8');
27 sub { emit_atom($_[0], $ctx) };
30 sub generate_thread_atom {
32 sub { emit_atom_thread($_[0], $ctx) };
35 sub generate_html_index {
37 sub { emit_html_index($_[0], $ctx) };
44 # try to avoid the type attribute in title:
45 $title = PublicInbox::Hval->new_oneline($title)->as_html;
46 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
47 "<title$type>$title</title>";
51 my ($feed_opts, $title) = @_;
53 $title = title_tag($feed_opts->{description}) unless (defined $title);
55 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
56 qq{<feed\nxmlns="http://www.w3.org/2005/Atom">} .
58 qq(<link\nrel="alternate"\ntype="text/html") .
59 qq(\nhref="$feed_opts->{url}"/>) .
60 qq(<link\nrel="self"\nhref="$feed_opts->{atomurl}"/>) .
61 qq(<id>mailto:$feed_opts->{id_addr}</id>);
66 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
67 my $max = $ctx->{max} || MAX_PER_PAGE;
68 my $feed_opts = get_feedopts($ctx);
69 my $x = atom_header($feed_opts);
70 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
71 each_recent_blob($ctx, sub {
72 my ($path, undef, $ts) = @_;
74 $fh->write($x . feed_updated(undef, $ts));
77 add_to_feed($feed_opts, $fh, $path, $git);
84 my $fh = $cb->([404, ['Content-Type' => 'text/plain']]);
85 $fh->write("No feed found for thread\n");
91 Email::Address->purge_cache;
92 $fh->write('</feed>');
96 sub emit_atom_thread {
98 my $res = $ctx->{srch}->get_thread($ctx->{mid});
99 return _no_thread($cb) unless $res->{total};
100 my $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
101 my $feed_opts = get_feedopts($ctx);
103 my $html_url = $feed_opts->{atomurl} = $ctx->{self_url};
104 $html_url =~ s!/t\.atom\z!/!;
105 $feed_opts->{url} = $html_url;
106 $feed_opts->{emit_header} = 1;
108 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
109 foreach my $msg (@{$res->{msgs}}) {
110 add_to_feed($feed_opts, $fh, mid2path($msg->mid), $git);
115 sub emit_html_index {
117 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
119 my $max = $ctx->{max} || MAX_PER_PAGE;
120 my $feed_opts = get_feedopts($ctx);
122 my $title = $feed_opts->{description} || '';
123 $title = PublicInbox::Hval->new_oneline($title)->as_html;
124 my ($footer, $param, $last);
125 my $state = { ctx => $ctx, seen => {}, anchor_idx => 0 };
126 my $srch = $ctx->{srch};
128 my $top = "<b>$title</b> (<a\nhref=\"new.atom\">Atom feed</a>)";
131 $top = qq{<form\naction=""><pre>$top} .
132 qq{ <input\nname=q\ntype=text />} .
133 qq{<input\ntype=submit\nvalue=search />} .
134 q{</pre></form><pre>}
136 $top = '<pre>' . $top . "\n";
139 $fh->write("<html><head><title>$title</title>" .
140 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
141 "href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
142 PublicInbox::Hval::STYLE .
143 "</head><body>$top");
145 # if the 'r' query parameter is given, it is a legacy permalink
146 # which we must continue supporting:
147 my $cgi = $ctx->{cgi};
148 if ($cgi && !$cgi->param('r') && $srch) {
149 $state->{srch} = $srch;
150 $last = PublicInbox::View::emit_index_topics($state, $fh);
153 $last = emit_index_nosrch($ctx, $state, $fh);
156 $footer = nav_footer($cgi, $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, $fh) = @_;
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($fh, $mime, 0, $state);
177 Email::Address->purge_cache;
182 my ($cgi, $last, $feed_opts, $state, $param) = @_;
184 my $old_r = $cgi->param($param);
187 my $first = $state->{first};
188 my $anchor = $state->{anchor_idx};
191 $next = qq!<a\nhref="?$param=$last">next</a>!;
194 $head = $cgi->path_info;
195 $head = qq!<a\nhref="$head">head</a>!;
197 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">Atom feed</a>";
198 "<a\nname=\"s$anchor\">page:</a> $next $head $atom";
201 sub each_recent_blob {
203 my $max = $ctx->{max} || MAX_PER_PAGE;
204 my $hex = '[a-f0-9]';
205 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
206 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
207 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
208 my $cgi = $ctx->{cgi};
210 # revision ranges may be specified
212 my $r = $cgi->param('r') if $cgi;
213 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
217 # get recent messages
218 # we could use git log -z, but, we already know ssoma will not
219 # leave us with filenames with spaces in them..
220 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
221 my $log = $git->popen(qw/log --no-notes --no-color --raw -r
222 --abbrev=16 --abbrev-commit/,
223 "--format=%h%x00%ct%x00%an%x00%s%x00",
225 my %deleted; # only an optimization at this point
228 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;
250 while (my $line = <$log>) {
251 if ($line =~ /^(${hex}{7,40})/o) {
258 close $log; # we may EPIPE here
260 ($first_commit, $last_commit);
263 # private functions below
266 my $pi_config = $ctx->{pi_config};
267 my $listname = $ctx->{listname};
268 my $cgi = $ctx->{cgi};
270 if (open my $fh, '<', "$ctx->{git_dir}/description") {
271 chomp($rv{description} = <$fh>);
274 $rv{description} = '($GIT_DIR/description missing)';
277 if ($pi_config && defined $listname && $listname ne '') {
278 my $addr = $pi_config->get($listname, 'address') || "";
279 $rv{address} = $addr;
280 $addr = $addr->[0] if ref($addr);
281 $rv{id_addr} = $addr;
283 $rv{id_addr} ||= 'public-inbox@example.com';
287 $url_base = $cgi->base->as_string . $listname;
288 if (my $mid = $ctx->{mid}) { # per-thread feed:
289 $rv{atomurl} = "$url_base/$mid/t.atom";
291 $rv{atomurl} = "$url_base/new.atom";
294 $url_base = "http://example.com";
295 $rv{atomurl} = "$url_base/new.atom";
297 $rv{url} ||= "$url_base/";
298 $rv{midurl} = "$url_base/";
304 my ($mime, $name) = @_;
305 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
309 my ($date, $ts) = @_;
310 my @t = eval { strptime($date) } if defined $date;
311 @t = gmtime($ts || time) unless scalar @t;
313 '<updated>' . strftime(DATEFMT, @t) . '</updated>';
316 # returns 0 (skipped) or 1 (added)
318 my ($feed_opts, $fh, $add, $git) = @_;
320 my $mime = do_cat_mail($git, $add) or return 0;
321 my $url = $feed_opts->{url};
322 my $midurl = $feed_opts->{midurl};
324 my $header_obj = $mime->header_obj;
325 my $mid = $header_obj->header('Message-ID');
326 defined $mid or return 0;
327 $mid = PublicInbox::Hval->new_msgid($mid);
328 my $href = $mid->as_href;
329 my $content = PublicInbox::View->feed_entry($mime, "$midurl$href/f/");
330 defined($content) or return 0;
333 my $date = $header_obj->header('Date');
334 my $updated = feed_updated($date);
336 my $title = mime_header($header_obj, 'Subject') or return 0;
337 $title = title_tag($title);
339 my $from = mime_header($header_obj, 'From') or return 0;
340 my @from = Email::Address->parse($from) or return 0;
341 my $name = PublicInbox::Hval->new_oneline($from[0]->name)->as_html;
342 my $email = $from[0]->address;
343 $email = PublicInbox::Hval->new_oneline($email)->as_html;
345 if (delete $feed_opts->{emit_header}) {
346 $fh->write(atom_header($feed_opts, $title) . $updated);
348 $fh->write("<entry><author><name>$name</name><email>$email</email>" .
349 "</author>$title$updated" .
350 qq{<content\ntype="xhtml">} .
351 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
352 $fh->write($content);
356 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
357 my $id = 'urn:uuid:' . join('-', @uuid5);
358 $fh->write(qq!</div></content><link\nhref="$midurl$href/"/>!.
359 "<id>$id</id></entry>");
364 my ($git, $path) = @_;
366 my $str = $git->cat_file("HEAD:$path");
367 Email::MIME->new($str);