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 $ibx = $ctx->{-inbox};
69 each_recent_blob($ctx, sub {
70 my ($path, undef, $ts) = @_;
72 $fh->write($x . feed_updated(undef, $ts));
75 my $s = feed_entry($feed_opts, $path, $ibx) or return 0;
84 $cb->([404, ['Content-Type', 'text/plain'],
85 ["No feed found for thread\n"]]);
90 $fh->write('</feed>');
94 sub emit_atom_thread {
96 my $res = $ctx->{srch}->get_thread($ctx->{mid});
97 return _no_thread($cb) unless $res->{total};
98 my $feed_opts = get_feedopts($ctx);
99 my $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
101 my $html_url = $feed_opts->{atomurl} = $ctx->{self_url};
102 $html_url =~ s!/t\.atom\z!/!;
103 $feed_opts->{url} = $html_url;
104 $feed_opts->{emit_header} = 1;
106 my $ibx = $ctx->{-inbox};
107 foreach my $msg (@{$res->{msgs}}) {
108 my $s = feed_entry($feed_opts, mid2path($msg->mid), $ibx);
109 $fh->write($s) if defined $s;
114 sub _html_index_top {
115 my ($feed_opts, $srch) = @_;
117 my $title = ascii_html($feed_opts->{description} || '');
118 my $top = "<b>$title</b> (<a\nhref=\"new.atom\">Atom feed</a>)";
120 $top = qq{<form\naction=""><pre>$top} .
121 qq{ <input\nname=q\ntype=text />} .
122 qq{<input\ntype=submit\nvalue=search />} .
123 q{</pre></form><pre>}
125 $top = '<pre>' . $top . "\n";
128 "<html><head><title>$title</title>" .
129 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
130 "href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
131 PublicInbox::Hval::STYLE .
135 sub emit_html_index {
136 my ($res, $ctx) = @_;
137 my $feed_opts = get_feedopts($ctx);
138 my $fh = $res->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
140 my $max = $ctx->{max} || MAX_PER_PAGE;
142 my ($footer, $param, $last);
143 my $state = { ctx => $ctx, seen => {}, anchor_idx => 0, fh => $fh };
144 my $srch = $ctx->{srch};
145 $fh->write(_html_index_top($feed_opts, $srch));
147 # if the 'r' query parameter is given, it is a legacy permalink
148 # which we must continue supporting:
150 if ($qp && !$qp->{r} && $srch) {
151 $state->{srch} = $srch;
152 $last = PublicInbox::View::emit_index_topics($state);
155 $last = emit_index_nosrch($ctx, $state);
158 $footer = nav_footer($ctx, $last, $feed_opts, $state, $param);
160 my $list_footer = $ctx->{footer};
161 $footer .= "\n\n" . $list_footer if $list_footer;
162 $footer = "<hr /><pre>$footer</pre>";
164 $fh->write("$footer</body></html>");
168 sub emit_index_nosrch {
169 my ($ctx, $state) = @_;
170 my $ibx = $ctx->{-inbox};
171 my $fh = $state->{fh};
172 my (undef, $last) = each_recent_blob($ctx, sub {
173 my ($path, $commit, $ts, $u, $subj) = @_;
174 $state->{first} ||= $commit;
176 my $mime = do_cat_mail($ibx, $path) or return 0;
177 $fh->write(PublicInbox::View::index_entry($mime, $state));
184 my ($ctx, $last, $feed_opts, $state, $param) = @_;
185 my $qp = $ctx->{qp} or return '';
186 my $old_r = $qp->{$param};
189 my $first = $state->{first};
190 my $anchor = $state->{anchor_idx};
193 $next = qq!<a\nhref="?$param=$last"\nrel=next>next</a>!;
196 $head = $ctx->{env}->{PATH_INFO};
197 $head = qq!<a\nhref="$head">head</a>!;
199 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">Atom feed</a>";
200 "<a\nname=\"s$anchor\">page:</a> $next $head $atom";
203 sub each_recent_blob {
205 my $max = $ctx->{max} || MAX_PER_PAGE;
206 my $hex = '[a-f0-9]';
207 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
208 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
209 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
212 # revision ranges may be specified
214 my $r = $qp->{r} if $qp;
215 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
219 # get recent messages
220 # we could use git log -z, but, we already know ssoma will not
221 # leave us with filenames with spaces in them..
222 my $log = $ctx->{-inbox}->git->popen(qw/log
223 --no-notes --no-color --raw -r
224 --abbrev=16 --abbrev-commit/,
225 "--format=%h%x00%ct%x00%an%x00%s%x00",
227 my %deleted; # only an optimization at this point
230 my ($cur_commit, $first_commit, $last_commit);
233 while (defined(my $line = <$log>)) {
234 if ($line =~ /$addmsg/o) {
236 next if $deleted{$add}; # optimization-only
237 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
242 } elsif ($line =~ /$delmsg/o) {
244 } elsif ($line =~ /^${hex}{7,40}/o) {
245 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
246 unless (defined $first_commit) {
247 $first_commit = $cur_commit;
254 while (my $line = <$log>) {
255 if ($line =~ /^(${hex}{7,40})/o) {
263 ($first_commit, $last_commit);
266 # private functions below
269 my $pi_config = $ctx->{pi_config};
270 my $inbox = $ctx->{inbox};
271 my $obj = $ctx->{-inbox};
272 my $cgi = $ctx->{cgi};
273 my %rv = ( description => $obj->description );
275 $rv{address} = $obj->{address};
276 $rv{id_addr} = $obj->{-primary_address};
278 $url_base = $obj->base_url($cgi); # CGI may be undef
279 if (my $mid = $ctx->{mid}) { # per-thread feed:
280 $rv{atomurl} = "$url_base$mid/t.atom";
282 $rv{atomurl} = $url_base."new.atom";
284 $rv{url} ||= $url_base;
285 $rv{midurl} = $url_base;
291 my ($date, $ts) = @_;
292 my @t = eval { strptime($date) } if defined $date;
293 @t = gmtime($ts || time) unless scalar @t;
295 '<updated>' . strftime(DATEFMT, @t) . '</updated>';
298 # returns undef or string
300 my ($feed_opts, $add, $ibx) = @_;
302 my $mime = do_cat_mail($ibx, $add) or return;
303 my $url = $feed_opts->{url};
304 my $midurl = $feed_opts->{midurl};
306 my $header_obj = $mime->header_obj;
307 my $mid = $header_obj->header_raw('Message-ID');
308 defined $mid or return;
309 $mid = PublicInbox::Hval->new_msgid($mid);
310 my $href = $midurl.$mid->as_href;
312 my $date = $header_obj->header('Date');
313 my $updated = feed_updated($date);
315 my $title = $header_obj->header('Subject');
316 defined $title or return;
317 $title = title_tag($title);
319 my $from = $header_obj->header('From') or return;
320 my ($email) = PublicInbox::Address::emails($from);
321 my $name = join(', ',PublicInbox::Address::names($from));
322 $name = ascii_html($name);
323 $email = ascii_html($email);
326 if (delete $feed_opts->{emit_header}) {
327 $s .= atom_header($feed_opts, $title) . $updated;
329 $s .= "<entry><author><name>$name</name><email>$email</email>" .
330 "</author>$title$updated" .
331 qq{<content\ntype="xhtml">} .
332 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
333 qq(<pre\nstyle="white-space:pre-wrap">) .
334 PublicInbox::View::multipart_text_as_html($mime, $href) .
339 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
340 my $id = 'urn:uuid:' . join('-', @uuid5);
341 $s .= qq!</div></content><link\nhref="$href/"/>!.
342 "<id>$id</id></entry>";
346 my ($ibx, $path) = @_;
347 my $mime = eval { $ibx->msg_by_path($path) } or return;
348 Email::MIME->new($mime);