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 mid_compress 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/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/xml']]);
101 my $feed_opts = get_feedopts($ctx);
103 my $html_url = $feed_opts->{atomurl} = $ctx->{self_url};
104 $html_url =~ s!/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};
127 $fh->write("<html><head><title>$title</title>" .
128 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
129 "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
130 '</head><body>' . PublicInbox::View::PRE_WRAP .
131 "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)\n");
134 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
136 my $srch = $ctx->{srch};
137 $srch and $topics = [ [], {} ];
138 my (undef, $last) = each_recent_blob($ctx, sub {
139 my ($path, $commit, $ts, $u, $subj) = @_;
143 first_commit => $commit,
148 add_topic($git, $srch, $topics, $path, $ts, $u, $subj);
150 my $mime = do_cat_mail($git, $path) or return 0;
151 PublicInbox::View::index_entry($fh, $mime, 0, $state);
155 Email::Address->purge_cache;
156 $git = undef; # destroy pipes.
158 my $footer = nav_footer($ctx->{cgi}, $last, $feed_opts, $state);
160 my $list_footer = $ctx->{footer};
161 $footer .= "\n" . $list_footer if $list_footer;
162 $footer = "<hr /><pre>$footer</pre>";
164 $fh->write(dump_topics($topics)) if $topics;
165 $fh->write("$footer</body></html>");
170 my ($cgi, $last, $feed_opts, $state) = @_;
172 my $old_r = $cgi->param('r');
175 my $first = $state->{first_commit};
176 my $anchor = $state->{anchor_idx};
179 $next = qq!<a\nhref="?r=$last">next</a>!;
182 $head = $cgi->path_info;
183 $head = qq!<a\nhref="$head">head</a>!;
185 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
186 my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
187 "<a\nname=\"s$anchor\">page:</a> $next $head $atom $permalink";
190 sub each_recent_blob {
192 my $max = $ctx->{max} || MAX_PER_PAGE;
193 my $hex = '[a-f0-9]';
194 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
195 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
196 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
197 my $cgi = $ctx->{cgi};
199 # revision ranges may be specified
201 my $r = $cgi->param('r') if $cgi;
202 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
206 # get recent messages
207 # we could use git log -z, but, we already know ssoma will not
208 # leave us with filenames with spaces in them..
209 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
210 qw/log --no-notes --no-color --raw -r
211 --abbrev=16 --abbrev-commit/,
212 "--format=%h%x00%ct%x00%an%x00%s%x00");
215 my $pid = open(my $log, '-|', @cmd) or
216 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
217 my %deleted; # only an optimization at this point
220 my ($cur_commit, $first_commit, $last_commit);
222 while (defined(my $line = <$log>)) {
223 if ($line =~ /$addmsg/o) {
225 next if $deleted{$add}; # optimization-only
226 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
231 } elsif ($line =~ /$delmsg/o) {
233 } elsif ($line =~ /^${hex}{7,40}/o) {
234 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
235 unless (defined $first_commit) {
236 $first_commit = $cur_commit;
242 while (my $line = <$log>) {
243 if ($line =~ /^(${hex}{7,40})/o) {
250 close $log; # we may EPIPE here
252 ($first_commit, $last_commit);
255 # private functions below
258 my $pi_config = $ctx->{pi_config};
259 my $listname = $ctx->{listname};
260 my $cgi = $ctx->{cgi};
262 if (open my $fh, '<', "$ctx->{git_dir}/description") {
263 chomp($rv{description} = <$fh>);
266 $rv{description} = '($GIT_DIR/description missing)';
269 if ($pi_config && defined $listname && $listname ne '') {
270 my $addr = $pi_config->get($listname, 'address') || "";
271 $rv{address} = $addr;
272 $addr = $addr->[0] if ref($addr);
273 $rv{id_addr} = $addr;
275 $rv{id_addr} ||= 'public-inbox@example.com';
280 if (ref($cgi) eq 'CGI') {
281 $base = $cgi->url(-base);
283 $base = $cgi->base->as_string;
286 $url_base = "$base/$listname";
287 if (my $mid = $ctx->{mid}) { # per-thread feed:
288 $rv{atomurl} = "$url_base/t/$mid/atom";
290 $rv{atomurl} = "$url_base/new.atom";
293 $url_base = "http://example.com";
294 $rv{atomurl} = "$url_base/new.atom";
296 $rv{url} ||= "$url_base/";
297 $rv{midurl} = "$url_base/m/";
298 $rv{fullurl} = "$url_base/f/";
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 $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
322 my $header_obj = $mime->header_obj;
323 my $mid = $header_obj->header('Message-ID');
324 defined $mid or return 0;
325 $mid = PublicInbox::Hval->new_msgid($mid);
326 my $href = $mid->as_href . '/';
327 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
328 defined($content) or return 0;
331 my $date = $header_obj->header('Date');
332 $date = PublicInbox::Hval->new_oneline($date);
333 $date = feed_date($date->raw) or return 0;
335 my $title = mime_header($header_obj, 'Subject') or return 0;
336 $title = title_tag($title);
338 my $from = mime_header($header_obj, 'From') or return 0;
339 my @from = Email::Address->parse($from) or return 0;
340 my $name = PublicInbox::Hval->new_oneline($from[0]->name)->as_html;
341 my $email = $from[0]->address;
342 $email = PublicInbox::Hval->new_oneline($email)->as_html;
344 if (delete $feed_opts->{emit_header}) {
345 $fh->write(atom_header($feed_opts, $title) .
346 "<updated>$date</updated>");
348 $fh->write("<entry><author><name>$name</name><email>$email</email>" .
349 "</author>$title$date" .
350 qq{<content\ntype="xhtml">} .
351 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
352 $fh->write($content);
356 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
357 my $id = 'urn:uuid:' . join('-', @uuid5);
358 my $midurl = $feed_opts->{midurl};
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);
373 # accumulate recent topics if search is supported
375 my ($git, $srch, $topics, $path, $ts, $u, $subj) = @_;
376 my ($order, $subjs) = @$topics;
379 # legacy ssoma did not set commit titles based on Subject
380 $subj = $enc_utf8->decode($subj);
381 if ($subj eq 'mda') {
382 my $mime = do_cat_mail($git, $path) or return 0;
383 $header_obj = $mime->header_obj;
384 $subj = mime_header($header_obj, 'Subject');
387 my $topic = $subj = $srch->subject_normalized($subj);
389 # kill "[PATCH v2]" etc. for summarization
390 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
392 if (++$subjs->{$topic} == 1) {
393 unless ($header_obj) {
394 my $mime = do_cat_mail($git, $path) or return 0;
395 $header_obj = $mime->header_obj;
397 my $mid = $header_obj->header('Message-ID');
398 $mid = mid_compress(mid_clean($mid));
399 $u = $enc_utf8->decode($u);
400 push @$order, [ $mid, $ts, $u, $subj, $topic ];
403 0; # old topic, continue going
408 my ($order, $subjs) = @$topics;
410 $dst .= "\n[No recent topics]" unless (scalar @$order);
411 while (defined(my $info = shift @$order)) {
412 my ($mid, $ts, $u, $subj, $topic) = @$info;
413 my $n = delete $subjs->{$topic};
414 $mid = PublicInbox::Hval->new($mid)->as_href;
415 $subj = PublicInbox::Hval->new($subj)->as_html;
416 $u = PublicInbox::Hval->new($u)->as_html;
417 $dst .= "\n<a\nhref=\"t/$mid/#u\"><b>$subj</b></a>\n- ";
418 $ts = strftime('%Y-%m-%d %H:%M', gmtime($ts));
420 $dst .= "created by $u @ $ts UTC\n"
422 # $n isn't the total number of posts on the topic,
423 # just the number of posts in the current "git log"
424 # window, so leave it unlabeled
425 $dst .= "updated by $u @ $ts UTC ($n)\n"