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) };
40 my (undef, $last) = each_recent_blob($ctx, sub {
41 my ($path, $commit, $ts, $u, $subj) = @_;
42 $ctx->{first} ||= $commit;
46 return [404, ['Content-Type', 'text/plain'],
47 ["No messages, yet\n"] ];
49 $ctx->{-html_tip} = '<pre>';
51 PublicInbox::WwwStream->response($ctx, 200, sub {
52 while (my $path = shift @paths) {
53 my $m = do_cat_mail($ctx->{-inbox}, $path) or next;
54 my $more = scalar @paths;
55 my $s = PublicInbox::View::index_entry($m, $ctx, $more);
56 $s .= '</pre>' unless $more;
67 $title =~ tr/\t\n / /s; # squeeze spaces
68 # try to avoid the type attribute in title:
69 $title = ascii_html($title);
70 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
71 "<title$type>$title</title>";
75 my ($feed_opts, $title) = @_;
77 $title = title_tag($feed_opts->{description}) unless (defined $title);
79 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
80 qq{<feed\nxmlns="http://www.w3.org/2005/Atom">} .
82 qq(<link\nrel="alternate"\ntype="text/html") .
83 qq(\nhref="$feed_opts->{url}"/>) .
84 qq(<link\nrel="self"\nhref="$feed_opts->{atomurl}"/>) .
85 qq(<id>mailto:$feed_opts->{id_addr}</id>);
90 my $feed_opts = get_feedopts($ctx);
91 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
92 my $max = $ctx->{max} || MAX_PER_PAGE;
93 my $x = atom_header($feed_opts);
94 my $ibx = $ctx->{-inbox};
95 each_recent_blob($ctx, sub {
96 my ($path, undef, $ts) = @_;
98 $fh->write($x . feed_updated(undef, $ts));
101 my $s = feed_entry($feed_opts, $path, $ibx) or return 0;
110 $cb->([404, ['Content-Type', 'text/plain'],
111 ["No feed found for thread\n"]]);
116 $fh->write('</feed>');
120 sub emit_atom_thread {
122 my $mid = $ctx->{mid};
123 my $res = $ctx->{srch}->get_thread($mid);
124 return _no_thread($cb) unless $res->{total};
125 my $feed_opts = get_feedopts($ctx);
126 my $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
127 my $ibx = $ctx->{-inbox};
128 my $html_url = $ibx->base_url($ctx->{env});
129 $html_url .= PublicInbox::Hval->new_msgid($mid)->as_href;
131 $feed_opts->{url} = $html_url;
132 $feed_opts->{emit_header} = 1;
134 foreach my $msg (@{$res->{msgs}}) {
135 my $s = feed_entry($feed_opts, mid2path($msg->mid), $ibx);
136 $fh->write($s) if defined $s;
141 sub _html_index_top {
142 my ($feed_opts, $srch) = @_;
144 my $title = ascii_html($feed_opts->{description} || '');
145 my $top = "<b>$title</b> (<a\nhref=\"new.atom\">Atom feed</a>)";
147 $top = qq{<form\naction=""><pre>$top} .
148 qq{ <input\nname=q\ntype=text />} .
149 qq{<input\ntype=submit\nvalue=search />} .
150 q{</pre></form><pre>}
152 $top = '<pre>' . $top . "\n";
155 "<html><head><title>$title</title>" .
156 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
157 "href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
158 PublicInbox::Hval::STYLE .
162 sub emit_html_index {
163 my ($res, $ctx) = @_;
164 my $feed_opts = get_feedopts($ctx);
165 my $fh = $res->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
167 my $max = $ctx->{max} || MAX_PER_PAGE;
170 my ($footer, $param, $last);
172 $ctx->{anchor_idx} = 0;
174 my $srch = $ctx->{srch};
175 $fh->write(_html_index_top($feed_opts, $srch));
177 # if the 'r' query parameter is given, it is a legacy permalink
178 # which we must continue supporting:
180 if ($qp && !$qp->{r} && $srch) {
181 $last = PublicInbox::View::emit_index_topics($ctx);
184 $last = emit_index_nosrch($ctx);
187 $footer = nav_footer($ctx, $last, $feed_opts, $param);
189 my $list_footer = $ctx->{footer};
190 $footer .= "\n\n" . $list_footer if $list_footer;
191 $footer = "<hr /><pre>$footer</pre>";
193 $fh->write("$footer</body></html>");
197 sub emit_index_nosrch {
199 my $ibx = $ctx->{-inbox};
201 my (undef, $last) = each_recent_blob($ctx, sub {
202 my ($path, $commit, $ts, $u, $subj) = @_;
203 $ctx->{first} ||= $commit;
205 my $mime = do_cat_mail($ibx, $path) or return 0;
206 $fh->write(PublicInbox::View::index_entry($mime, $ctx, 1));
213 my ($ctx, $last, $feed_opts, $param) = @_;
214 my $qp = $ctx->{qp} or return '';
215 my $old_r = $qp->{$param};
218 my $first = $ctx->{first};
219 my $anchor = $ctx->{anchor_idx};
222 $next = qq!<a\nhref="?$param=$last"\nrel=next>next</a>!;
225 $head = $ctx->{env}->{PATH_INFO};
226 $head = qq!<a\nhref="$head">head</a>!;
228 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">Atom feed</a>";
229 "<a\nname=\"s$anchor\">page:</a> $next $head $atom";
232 sub each_recent_blob {
234 my $max = $ctx->{max} || MAX_PER_PAGE;
235 my $hex = '[a-f0-9]';
236 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
237 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
238 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
241 # revision ranges may be specified
243 my $r = $qp->{r} if $qp;
244 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
248 # get recent messages
249 # we could use git log -z, but, we already know ssoma will not
250 # leave us with filenames with spaces in them..
251 my $log = $ctx->{-inbox}->git->popen(qw/log
252 --no-notes --no-color --raw -r
253 --abbrev=16 --abbrev-commit/,
254 "--format=%h%x00%ct%x00%an%x00%s%x00",
256 my %deleted; # only an optimization at this point
259 my ($cur_commit, $first_commit, $last_commit);
262 while (defined(my $line = <$log>)) {
263 if ($line =~ /$addmsg/o) {
265 next if $deleted{$add}; # optimization-only
266 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
271 } elsif ($line =~ /$delmsg/o) {
273 } elsif ($line =~ /^${hex}{7,40}/o) {
274 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
275 unless (defined $first_commit) {
276 $first_commit = $cur_commit;
283 while (my $line = <$log>) {
284 if ($line =~ /^(${hex}{7,40})/o) {
292 ($first_commit, $last_commit);
295 # private functions below
298 my $pi_config = $ctx->{pi_config};
299 my $inbox = $ctx->{inbox};
300 my $obj = $ctx->{-inbox};
301 my %rv = ( description => $obj->description );
303 $rv{address} = $obj->{address};
304 $rv{id_addr} = $obj->{-primary_address};
305 my $url_base = $obj->base_url($ctx->{env});
306 if (my $mid = $ctx->{mid}) { # per-thread feed:
307 $rv{atomurl} = "$url_base$mid/t.atom";
309 $rv{atomurl} = $url_base."new.atom";
311 $rv{url} ||= $url_base;
312 $rv{midurl} = $url_base;
318 my ($date, $ts) = @_;
319 my @t = eval { strptime($date) } if defined $date;
320 @t = gmtime($ts || time) unless scalar @t;
322 '<updated>' . strftime(DATEFMT, @t) . '</updated>';
325 # returns undef or string
327 my ($feed_opts, $add, $ibx) = @_;
329 my $mime = do_cat_mail($ibx, $add) or return;
330 my $url = $feed_opts->{url};
331 my $midurl = $feed_opts->{midurl};
333 my $header_obj = $mime->header_obj;
334 my $mid = $header_obj->header_raw('Message-ID');
335 defined $mid or return;
336 $mid = PublicInbox::Hval->new_msgid($mid);
337 my $href = $midurl.$mid->as_href;
339 my $date = $header_obj->header('Date');
340 my $updated = feed_updated($date);
342 my $title = $header_obj->header('Subject');
343 defined $title or return;
344 $title = title_tag($title);
346 my $from = $header_obj->header('From') or return;
347 my ($email) = PublicInbox::Address::emails($from);
348 my $name = join(', ',PublicInbox::Address::names($from));
349 $name = ascii_html($name);
350 $email = ascii_html($email);
353 if (delete $feed_opts->{emit_header}) {
354 $s .= atom_header($feed_opts, $title) . $updated;
356 $s .= "<entry><author><name>$name</name><email>$email</email>" .
357 "</author>$title$updated" .
358 qq{<content\ntype="xhtml">} .
359 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
360 qq(<pre\nstyle="white-space:pre-wrap">) .
361 PublicInbox::View::multipart_text_as_html($mime, $href) .
366 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
367 my $id = 'urn:uuid:' . join('-', @uuid5);
368 $s .= qq!</div></content><link\nhref="$href/"/>!.
369 "<id>$id</id></entry>";
373 my ($ibx, $path) = @_;
374 my $mime = eval { $ibx->msg_by_path($path) } or return;
375 Email::MIME->new($mime);