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 . feed_updated(undef, $ts));
74 add_to_feed($feed_opts, $fh, $path, $git);
76 $git = undef; # destroy pipes
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 $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
99 my $feed_opts = get_feedopts($ctx);
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 = PublicInbox::GitCatFile->new($ctx->{git_dir});
107 foreach my $msg (@{$res->{msgs}}) {
108 add_to_feed($feed_opts, $fh, mid2path($msg->mid), $git);
110 $git = undef; # destroy pipes
114 sub emit_html_index {
116 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
118 my $max = $ctx->{max} || MAX_PER_PAGE;
119 my $feed_opts = get_feedopts($ctx);
121 my $title = $feed_opts->{description} || '';
122 $title = PublicInbox::Hval->new_oneline($title)->as_html;
123 my $atom_url = $feed_opts->{atomurl};
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=\"$atom_url\">Atom feed</a>)";
131 $top = qq{<form\naction=""><tt>$top} .
132 qq{ <input\nname=q\ntype=text />} .
133 qq{<input\ntype=submit\nvalue=search />} .
135 PublicInbox::View::PRE_WRAP;
137 $top = PublicInbox::View::PRE_WRAP . $top . "\n";
140 $fh->write("<html><head><title>$title</title>" .
141 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
142 "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
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 = PublicInbox::GitCatFile->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</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 @cmd = ('git', "--git-dir=$ctx->{git_dir}",
221 qw/log --no-notes --no-color --raw -r
222 --abbrev=16 --abbrev-commit/,
223 "--format=%h%x00%ct%x00%an%x00%s%x00");
226 my $pid = open(my $log, '-|', @cmd) or
227 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
228 my %deleted; # only an optimization at this point
231 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;
253 while (my $line = <$log>) {
254 if ($line =~ /^(${hex}{7,40})/o) {
261 close $log; # we may EPIPE here
263 ($first_commit, $last_commit);
266 # private functions below
269 my $pi_config = $ctx->{pi_config};
270 my $listname = $ctx->{listname};
271 my $cgi = $ctx->{cgi};
273 if (open my $fh, '<', "$ctx->{git_dir}/description") {
274 chomp($rv{description} = <$fh>);
277 $rv{description} = '($GIT_DIR/description missing)';
280 if ($pi_config && defined $listname && $listname ne '') {
281 my $addr = $pi_config->get($listname, 'address') || "";
282 $rv{address} = $addr;
283 $addr = $addr->[0] if ref($addr);
284 $rv{id_addr} = $addr;
286 $rv{id_addr} ||= 'public-inbox@example.com';
291 if (ref($cgi) eq 'CGI') {
292 $base = $cgi->url(-base);
294 $base = $cgi->base->as_string;
297 $url_base = "$base/$listname";
298 if (my $mid = $ctx->{mid}) { # per-thread feed:
299 $rv{atomurl} = "$url_base/$mid/t.atom";
301 $rv{atomurl} = "$url_base/new.atom";
304 $url_base = "http://example.com";
305 $rv{atomurl} = "$url_base/new.atom";
307 $rv{url} ||= "$url_base/";
308 $rv{midurl} = "$url_base/";
314 my ($mime, $name) = @_;
315 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
319 my ($date, $ts) = @_;
320 my @t = eval { strptime($date) } if defined $date;
321 @t = gmtime($ts || time) unless scalar @t;
323 '<updated>' . strftime(DATEFMT, @t) . '</updated>';
326 # returns 0 (skipped) or 1 (added)
328 my ($feed_opts, $fh, $add, $git) = @_;
330 my $mime = do_cat_mail($git, $add) or return 0;
331 my $url = $feed_opts->{url};
332 my $midurl = $feed_opts->{midurl};
334 my $header_obj = $mime->header_obj;
335 my $mid = $header_obj->header('Message-ID');
336 defined $mid or return 0;
337 $mid = PublicInbox::Hval->new_msgid($mid);
338 my $href = $mid->as_href;
339 my $content = PublicInbox::View->feed_entry($mime, "$midurl$href/f/");
340 defined($content) or return 0;
343 my $date = $header_obj->header('Date');
344 my $updated = feed_updated($date);
346 my $title = mime_header($header_obj, 'Subject') or return 0;
347 $title = title_tag($title);
349 my $from = mime_header($header_obj, 'From') or return 0;
350 my @from = Email::Address->parse($from) or return 0;
351 my $name = PublicInbox::Hval->new_oneline($from[0]->name)->as_html;
352 my $email = $from[0]->address;
353 $email = PublicInbox::Hval->new_oneline($email)->as_html;
355 if (delete $feed_opts->{emit_header}) {
356 $fh->write(atom_header($feed_opts, $title) . $updated);
358 $fh->write("<entry><author><name>$name</name><email>$email</email>" .
359 "</author>$title$updated" .
360 qq{<content\ntype="xhtml">} .
361 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
362 $fh->write($content);
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 $fh->write(qq!</div></content><link\nhref="$midurl$href/"/>!.
369 "<id>$id</id></entry>");
374 my ($git, $path) = @_;
376 my $str = $git->cat_file("HEAD:$path");
377 Email::MIME->new($str);