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 qw/ascii_html/;
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
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 Email::Address->purge_cache;
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 $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
107 foreach my $msg (@{$res->{msgs}}) {
108 add_to_feed($feed_opts, $fh, mid2path($msg->mid), $git);
113 sub emit_html_index {
114 my ($res, $ctx) = @_;
115 my $feed_opts = get_feedopts($ctx);
116 my $fh = $res->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
118 my $max = $ctx->{max} || MAX_PER_PAGE;
120 my $title = ascii_html($feed_opts->{description} || '');
121 my ($footer, $param, $last);
122 my $state = { ctx => $ctx, seen => {}, anchor_idx => 0, fh => $fh };
123 my $srch = $ctx->{srch};
125 my $top = "<b>$title</b> (<a\nhref=\"new.atom\">Atom feed</a>)";
128 $top = qq{<form\naction=""><pre>$top} .
129 qq{ <input\nname=q\ntype=text />} .
130 qq{<input\ntype=submit\nvalue=search />} .
131 q{</pre></form><pre>}
133 $top = '<pre>' . $top . "\n";
136 $fh->write("<html><head><title>$title</title>" .
137 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
138 "href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
139 PublicInbox::Hval::STYLE .
140 "</head><body>$top");
142 # if the 'r' query parameter is given, it is a legacy permalink
143 # which we must continue supporting:
144 my $cgi = $ctx->{cgi};
145 if ($cgi && !$cgi->param('r') && $srch) {
146 $state->{srch} = $srch;
147 $last = PublicInbox::View::emit_index_topics($state);
150 $last = emit_index_nosrch($ctx, $state);
153 $footer = nav_footer($cgi, $last, $feed_opts, $state, $param);
155 my $list_footer = $ctx->{footer};
156 $footer .= "\n\n" . $list_footer if $list_footer;
157 $footer = "<hr /><pre>$footer</pre>";
159 $fh->write("$footer</body></html>");
163 sub emit_index_nosrch {
164 my ($ctx, $state) = @_;
165 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
166 my (undef, $last) = each_recent_blob($ctx, sub {
167 my ($path, $commit, $ts, $u, $subj) = @_;
168 $state->{first} ||= $commit;
170 my $mime = do_cat_mail($git, $path) or return 0;
171 PublicInbox::View::index_entry($mime, 0, $state);
174 Email::Address->purge_cache;
179 my ($cgi, $last, $feed_opts, $state, $param) = @_;
181 my $old_r = $cgi->param($param);
184 my $first = $state->{first};
185 my $anchor = $state->{anchor_idx};
188 $next = qq!<a\nhref="?$param=$last"\nrel=next>next</a>!;
191 $head = $cgi->path_info;
192 $head = qq!<a\nhref="$head">head</a>!;
194 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">Atom feed</a>";
195 "<a\nname=\"s$anchor\">page:</a> $next $head $atom";
198 sub each_recent_blob {
200 my $max = $ctx->{max} || MAX_PER_PAGE;
201 my $hex = '[a-f0-9]';
202 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
203 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
204 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
205 my $cgi = $ctx->{cgi};
207 # revision ranges may be specified
209 my $r = $cgi->param('r') if $cgi;
210 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
214 # get recent messages
215 # we could use git log -z, but, we already know ssoma will not
216 # leave us with filenames with spaces in them..
217 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
218 my $log = $git->popen(qw/log --no-notes --no-color --raw -r
219 --abbrev=16 --abbrev-commit/,
220 "--format=%h%x00%ct%x00%an%x00%s%x00",
222 my %deleted; # only an optimization at this point
225 my ($cur_commit, $first_commit, $last_commit);
227 while (defined(my $line = <$log>)) {
228 if ($line =~ /$addmsg/o) {
230 next if $deleted{$add}; # optimization-only
231 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
236 } elsif ($line =~ /$delmsg/o) {
238 } elsif ($line =~ /^${hex}{7,40}/o) {
239 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
240 unless (defined $first_commit) {
241 $first_commit = $cur_commit;
247 while (my $line = <$log>) {
248 if ($line =~ /^(${hex}{7,40})/o) {
256 ($first_commit, $last_commit);
259 # private functions below
262 my $pi_config = $ctx->{pi_config};
263 my $inbox = $ctx->{inbox};
264 my $obj = $ctx->{-inbox};
265 my $cgi = $ctx->{cgi};
266 my %rv = ( description => $obj ? $obj->description : 'FIXME' );
269 $rv{address} = $obj->{address};
270 $rv{id_addr} = $obj->{-primary_address};
271 } elsif ($pi_config && defined $inbox && $inbox ne '') {
273 my $addr = $pi_config->get($inbox, 'address') || "";
274 $rv{address} = $addr;
275 $addr = $addr->[0] if ref($addr);
276 $rv{id_addr} = $addr;
278 $rv{id_addr} ||= 'public-inbox@example.com';
282 $url_base = $obj->base_url($cgi); # CGI may be undef
283 if (my $mid = $ctx->{mid}) { # per-thread feed:
284 $rv{atomurl} = "$url_base$mid/t.atom";
286 $rv{atomurl} = $url_base."new.atom";
289 $url_base = 'http://example.com/';
290 $rv{atomurl} = $url_base.'new.atom';
292 $rv{url} ||= $url_base;
293 $rv{midurl} = $url_base;
299 my ($date, $ts) = @_;
300 my @t = eval { strptime($date) } if defined $date;
301 @t = gmtime($ts || time) unless scalar @t;
303 '<updated>' . strftime(DATEFMT, @t) . '</updated>';
306 # returns 0 (skipped) or 1 (added)
308 my ($feed_opts, $fh, $add, $git) = @_;
310 my $mime = do_cat_mail($git, $add) or return 0;
311 my $url = $feed_opts->{url};
312 my $midurl = $feed_opts->{midurl};
314 my $header_obj = $mime->header_obj;
315 my $mid = $header_obj->header_raw('Message-ID');
316 defined $mid or return 0;
317 $mid = PublicInbox::Hval->new_msgid($mid);
318 my $href = $mid->as_href;
319 my $content = PublicInbox::View->feed_entry($mime);
320 defined($content) or return 0;
323 my $date = $header_obj->header('Date');
324 my $updated = feed_updated($date);
326 my $title = $header_obj->header('Subject');
327 defined $title or return 0;
328 $title = title_tag($title);
330 my $from = $header_obj->header('From') or return 0;
331 my @from = Email::Address->parse($from) or return 0;
332 my $name = ascii_html($from[0]->name);
333 my $email = $from[0]->address;
334 $email = ascii_html($email);
336 if (delete $feed_opts->{emit_header}) {
337 $fh->write(atom_header($feed_opts, $title) . $updated);
339 $fh->write("<entry><author><name>$name</name><email>$email</email>" .
340 "</author>$title$updated" .
341 qq{<content\ntype="xhtml">} .
342 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
343 $fh->write($content);
347 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
348 my $id = 'urn:uuid:' . join('-', @uuid5);
349 $fh->write(qq!</div></content><link\nhref="$midurl$href/"/>!.
350 "<id>$id</id></entry>");
355 my ($git, $path) = @_;
357 my $str = $git->cat_file("HEAD:$path");
358 Email::MIME->new($str);