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)
3 package PublicInbox::Feed;
8 use Date::Parse qw(strptime);
10 use PublicInbox::GitCatFile;
11 use PublicInbox::View;
12 use PublicInbox::MID qw/mid_clean mid2path/;
13 use POSIX qw/strftime/;
15 DATEFMT => '%Y-%m-%dT%H:%M:%SZ', # atom standard
16 MAX_PER_PAGE => 25, # this needs to be tunable
19 use Encode qw/find_encoding/;
20 my $enc_utf8 = find_encoding('UTF-8');
25 sub { emit_atom($_[0], $ctx) };
28 sub generate_thread_atom {
30 sub { emit_atom_thread($_[0], $ctx) };
33 sub generate_html_index {
35 sub { emit_html_index($_[0], $ctx) };
42 # try to avoid the type attribute in title:
43 $title = PublicInbox::Hval->new_oneline($title)->as_html;
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\nhref="$feed_opts->{url}"/>) .
57 qq(<link\nrel="self"\nhref="$feed_opts->{atomurl}"/>) .
58 qq(<id>mailto:$feed_opts->{id_addr}</id>);
63 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
64 my $max = $ctx->{max} || MAX_PER_PAGE;
65 my $feed_opts = get_feedopts($ctx);
66 my $x = atom_header($feed_opts);
67 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
68 each_recent_blob($ctx, sub {
69 my ($path, undef, $ts) = @_;
71 $fh->write($x . '<updated>' .
72 strftime(DATEFMT, gmtime($ts)) .
76 add_to_feed($feed_opts, $fh, $path, $git);
78 $git = undef; # destroy pipes
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 = PublicInbox::GitCatFile->new($ctx->{git_dir});
109 foreach my $msg (@{$res->{msgs}}) {
110 add_to_feed($feed_opts, $fh, mid2path($msg->mid), $git);
112 $git = undef; # destroy pipes
116 sub emit_html_index {
118 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
120 my $max = $ctx->{max} || MAX_PER_PAGE;
121 my $feed_opts = get_feedopts($ctx);
123 my $title = $feed_opts->{description} || '';
124 $title = PublicInbox::Hval->new_oneline($title)->as_html;
125 my $atom_url = $feed_opts->{atomurl};
126 my ($footer, $param, $last, $srch);
127 my $state = { ctx => $ctx, seen => {}, anchor_idx => 0 };
129 $fh->write("<html><head><title>$title</title>" .
130 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
131 "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
132 '</head><body>' . PublicInbox::View::PRE_WRAP .
133 "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)\n");
135 # if the 'r' query parameter is given, it is a legacy permalink
136 # which we must continue supporting:
137 my $cgi = $ctx->{cgi};
138 if ($cgi && !$cgi->param('r') && ($srch = $ctx->{srch})) {
139 $state->{srch} = $srch;
140 $last = PublicInbox::View::emit_index_topics($state, $fh);
143 $last = emit_index_nosrch($ctx, $state, $fh);
146 $footer = nav_footer($cgi, $last, $feed_opts, $state, $param);
148 my $list_footer = $ctx->{footer};
149 $footer .= "\n\n" . $list_footer if $list_footer;
150 $footer = "<hr /><pre>$footer</pre>";
152 $fh->write("$footer</body></html>");
156 sub emit_index_nosrch {
157 my ($ctx, $state, $fh) = @_;
158 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
159 my (undef, $last) = each_recent_blob($ctx, sub {
160 my ($path, $commit, $ts, $u, $subj) = @_;
161 $state->{first} ||= $commit;
163 my $mime = do_cat_mail($git, $path) or return 0;
164 PublicInbox::View::index_entry($fh, $mime, 0, $state);
167 Email::Address->purge_cache;
172 my ($cgi, $last, $feed_opts, $state, $param) = @_;
174 my $old_r = $cgi->param($param);
177 my $first = $state->{first};
178 my $anchor = $state->{anchor_idx};
181 $next = qq!<a\nhref="?$param=$last">next</a>!;
184 $head = $cgi->path_info;
185 $head = qq!<a\nhref="$head">head</a>!;
187 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
188 "<a\nname=\"s$anchor\">page:</a> $next $head $atom";
191 sub each_recent_blob {
193 my $max = $ctx->{max} || MAX_PER_PAGE;
194 my $hex = '[a-f0-9]';
195 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
196 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
197 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
198 my $cgi = $ctx->{cgi};
200 # revision ranges may be specified
202 my $r = $cgi->param('r') if $cgi;
203 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
207 # get recent messages
208 # we could use git log -z, but, we already know ssoma will not
209 # leave us with filenames with spaces in them..
210 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
211 qw/log --no-notes --no-color --raw -r
212 --abbrev=16 --abbrev-commit/,
213 "--format=%h%x00%ct%x00%an%x00%s%x00");
216 my $pid = open(my $log, '-|', @cmd) or
217 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
218 my %deleted; # only an optimization at this point
221 my ($cur_commit, $first_commit, $last_commit);
223 while (defined(my $line = <$log>)) {
224 if ($line =~ /$addmsg/o) {
226 next if $deleted{$add}; # optimization-only
227 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
232 } elsif ($line =~ /$delmsg/o) {
234 } elsif ($line =~ /^${hex}{7,40}/o) {
235 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
236 unless (defined $first_commit) {
237 $first_commit = $cur_commit;
243 while (my $line = <$log>) {
244 if ($line =~ /^(${hex}{7,40})/o) {
251 close $log; # we may EPIPE here
253 ($first_commit, $last_commit);
256 # private functions below
259 my $pi_config = $ctx->{pi_config};
260 my $listname = $ctx->{listname};
261 my $cgi = $ctx->{cgi};
263 if (open my $fh, '<', "$ctx->{git_dir}/description") {
264 chomp($rv{description} = <$fh>);
267 $rv{description} = '($GIT_DIR/description missing)';
270 if ($pi_config && defined $listname && $listname ne '') {
271 my $addr = $pi_config->get($listname, 'address') || "";
272 $rv{address} = $addr;
273 $addr = $addr->[0] if ref($addr);
274 $rv{id_addr} = $addr;
276 $rv{id_addr} ||= 'public-inbox@example.com';
281 if (ref($cgi) eq 'CGI') {
282 $base = $cgi->url(-base);
284 $base = $cgi->base->as_string;
287 $url_base = "$base/$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;
310 my @t = eval { strptime($date) };
312 scalar(@t) ? strftime(DATEFMT, @t) : 0;
315 # returns 0 (skipped) or 1 (added)
317 my ($feed_opts, $fh, $add, $git) = @_;
319 my $mime = do_cat_mail($git, $add) or return 0;
320 my $url = $feed_opts->{url};
321 my $midurl = $feed_opts->{midurl};
323 my $header_obj = $mime->header_obj;
324 my $mid = $header_obj->header('Message-ID');
325 defined $mid or return 0;
326 $mid = PublicInbox::Hval->new_msgid($mid);
327 my $href = $mid->as_href;
328 my $content = PublicInbox::View->feed_entry($mime, "$midurl$href/f/");
329 defined($content) or return 0;
332 my $date = $header_obj->header('Date');
333 $date = PublicInbox::Hval->new_oneline($date);
334 $date = feed_date($date->raw) or return 0;
335 $date = "<updated>$date</updated>";
337 my $title = mime_header($header_obj, 'Subject') or return 0;
338 $title = title_tag($title);
340 my $from = mime_header($header_obj, 'From') or return 0;
341 my @from = Email::Address->parse($from) or return 0;
342 my $name = PublicInbox::Hval->new_oneline($from[0]->name)->as_html;
343 my $email = $from[0]->address;
344 $email = PublicInbox::Hval->new_oneline($email)->as_html;
346 if (delete $feed_opts->{emit_header}) {
347 $fh->write(atom_header($feed_opts, $title) . $date);
349 $fh->write("<entry><author><name>$name</name><email>$email</email>" .
350 "</author>$title$date" .
351 qq{<content\ntype="xhtml">} .
352 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
353 $fh->write($content);
357 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
358 my $id = 'urn:uuid:' . join('-', @uuid5);
359 $fh->write(qq!</div></content><link\nhref="$midurl$href/"/>!.
360 "<id>$id</id></entry>");
365 my ($git, $path) = @_;
367 my $str = $git->cat_file("HEAD:$path");
368 Email::MIME->new($str);