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/;
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_html_index {
30 sub { emit_html_index($_[0], $ctx) };
37 my $fh = $cb->([ 200, ['Content-Type' => 'application/xml']]);
38 my $max = $ctx->{max} || MAX_PER_PAGE;
39 my $feed_opts = get_feedopts($ctx);
40 my $addr = $feed_opts->{address};
41 $addr = $addr->[0] if ref($addr);
42 $addr ||= 'public-inbox@example.com';
43 my $title = $feed_opts->{description} || "unnamed feed";
44 $title = PublicInbox::Hval->new_oneline($title)->as_html;
45 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
46 my $url = $feed_opts->{url} || "http://example.com/";
47 my $atomurl = $feed_opts->{atomurl};
48 my $x = qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
49 qq{<feed\nxmlns="http://www.w3.org/2005/Atom">} .
50 qq{<title$type>$title</title>} .
51 qq{<link\nhref="$url"/>} .
52 qq{<link\nrel="self"\nhref="$atomurl"/>} .
53 qq{<id>mailto:$addr</id>};
55 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
56 each_recent_blob($ctx, sub {
57 my ($path, undef, $ts) = @_;
59 $fh->write($x . '<updated>'.
60 strftime(DATEFMT, gmtime($ts)) .
64 add_to_feed($feed_opts, $fh, $path, $git);
66 $git = undef; # destroy pipes
67 Email::Address->purge_cache;
68 $fh->write("</feed>");
75 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
77 my $max = $ctx->{max} || MAX_PER_PAGE;
78 my $feed_opts = get_feedopts($ctx);
80 my $title = $feed_opts->{description} || '';
81 $title = PublicInbox::Hval->new_oneline($title)->as_html;
82 my $atom_url = $feed_opts->{atomurl};
84 $fh->write("<html><head><title>$title</title>" .
85 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
86 "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
87 '</head><body>' . PublicInbox::View::PRE_WRAP .
88 "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)\n");
91 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
93 my $srch = $ctx->{srch};
94 $srch and $topics = [ [], {} ];
95 my (undef, $last) = each_recent_blob($ctx, sub {
96 my ($path, $commit, $ts, $u, $subj) = @_;
100 first_commit => $commit,
105 add_topic($git, $srch, $topics, $path, $ts, $u, $subj);
107 my $mime = do_cat_mail($git, $path) or return 0;
108 PublicInbox::View::index_entry($fh, $mime, 0, $state);
112 Email::Address->purge_cache;
113 $git = undef; # destroy pipes.
115 my $footer = nav_footer($ctx->{cgi}, $last, $feed_opts, $state);
117 my $list_footer = $ctx->{footer};
118 $footer .= "\n" . $list_footer if $list_footer;
119 $footer = "<hr /><pre>$footer</pre>";
121 $fh->write(dump_topics($topics)) if $topics;
122 $fh->write("$footer</body></html>");
127 my ($cgi, $last, $feed_opts, $state) = @_;
129 my $old_r = $cgi->param('r');
132 my $first = $state->{first_commit};
133 my $anchor = $state->{anchor_idx};
136 $next = qq!<a\nhref="?r=$last">next</a>!;
139 $head = $cgi->path_info;
140 $head = qq!<a\nhref="$head">head</a>!;
142 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
143 my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
144 "<a\nname=\"s$anchor\">page:</a> $next $head $atom $permalink";
147 sub each_recent_blob {
149 my $max = $ctx->{max} || MAX_PER_PAGE;
150 my $hex = '[a-f0-9]';
151 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
152 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
153 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
154 my $cgi = $ctx->{cgi};
156 # revision ranges may be specified
158 my $r = $cgi->param('r') if $cgi;
159 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
163 # get recent messages
164 # we could use git log -z, but, we already know ssoma will not
165 # leave us with filenames with spaces in them..
166 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
167 qw/log --no-notes --no-color --raw -r
168 --abbrev=16 --abbrev-commit/,
169 "--format=%h%x00%ct%x00%an%x00%s%x00");
172 my $pid = open(my $log, '-|', @cmd) or
173 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
174 my %deleted; # only an optimization at this point
177 my ($cur_commit, $first_commit, $last_commit);
179 while (defined(my $line = <$log>)) {
180 if ($line =~ /$addmsg/o) {
182 next if $deleted{$add}; # optimization-only
183 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
188 } elsif ($line =~ /$delmsg/o) {
190 } elsif ($line =~ /^${hex}{7,40}/o) {
191 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
192 unless (defined $first_commit) {
193 $first_commit = $cur_commit;
199 while (my $line = <$log>) {
200 if ($line =~ /^(${hex}{7,40})/o) {
207 close $log; # we may EPIPE here
209 ($first_commit, $last_commit);
212 # private functions below
215 my $pi_config = $ctx->{pi_config};
216 my $listname = $ctx->{listname};
217 my $cgi = $ctx->{cgi};
219 if (open my $fh, '<', "$ctx->{git_dir}/description") {
220 chomp($rv{description} = <$fh>);
224 if ($pi_config && defined $listname && $listname ne '') {
225 foreach my $key (qw(address)) {
226 $rv{$key} = $pi_config->get($listname, $key) || "";
231 my $path_info = $cgi->path_info;
233 if (ref($cgi) eq 'CGI') {
234 $base = $cgi->url(-base);
236 $base = $cgi->base->as_string;
239 $url_base = $path_info;
240 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
241 $rv{atomurl} = "$base$url_base/atom.xml";
243 $url_base =~ s!/atom\.xml\z!!;
244 $rv{atomurl} = $base . $path_info;
245 $url_base = $base . $url_base; # XXX is this needed?
248 $url_base = "http://example.com";
249 $rv{atomurl} = "$url_base/atom.xml";
251 $rv{url} ||= "$url_base/";
252 $rv{midurl} = "$url_base/m/";
253 $rv{fullurl} = "$url_base/f/";
259 my ($mime, $name) = @_;
260 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
265 my @t = eval { strptime($date) };
267 scalar(@t) ? strftime(DATEFMT, @t) : 0;
270 # returns 0 (skipped) or 1 (added)
272 my ($feed_opts, $fh, $add, $git) = @_;
274 my $mime = do_cat_mail($git, $add) or return 0;
275 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
277 my $header_obj = $mime->header_obj;
278 my $mid = $header_obj->header('Message-ID');
279 defined $mid or return 0;
280 $mid = PublicInbox::Hval->new_msgid($mid);
281 my $href = $mid->as_href . '/';
282 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
283 defined($content) or return 0;
286 my $title = mime_header($header_obj, 'Subject') or return 0;
287 $title = PublicInbox::Hval->new_oneline($title)->as_html;
288 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
290 my $from = mime_header($header_obj, 'From') or return 0;
291 my @from = Email::Address->parse($from) or return 0;
292 my $name = PublicInbox::Hval->new_oneline($from[0]->name)->as_html;
293 my $email = $from[0]->address;
294 $email = PublicInbox::Hval->new_oneline($email)->as_html;
296 my $date = $header_obj->header('Date');
297 $date = PublicInbox::Hval->new_oneline($date);
298 $date = feed_date($date->raw) or return 0;
300 $fh->write("<entry><author><name>$name</name><email>$email</email>" .
301 "</author><title$type>$title</title>" .
302 "<updated>$date</updated>" .
303 qq{<content\ntype="xhtml">} .
304 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
305 $fh->write($content);
309 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
310 my $id = 'urn:uuid:' . join('-', @uuid5);
311 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
312 $fh->write(qq{</div></content><link\nhref="$midurl$href"/>}.
313 "<id>$id</id></entry>");
318 my ($git, $path) = @_;
320 my $str = $git->cat_file("HEAD:$path");
321 Email::MIME->new($str);
326 # accumulate recent topics if search is supported
328 my ($git, $srch, $topics, $path, $ts, $u, $subj) = @_;
329 my ($order, $subjs) = @$topics;
332 # legacy ssoma did not set commit titles based on Subject
333 $subj = $enc_utf8->decode($subj);
334 if ($subj eq 'mda') {
335 my $mime = do_cat_mail($git, $path) or return 0;
336 $header_obj = $mime->header_obj;
337 $subj = mime_header($header_obj, 'Subject');
340 my $topic = $subj = $srch->subject_normalized($subj);
342 # kill "[PATCH v2]" etc. for summarization
343 $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
345 if (++$subjs->{$topic} == 1) {
346 unless ($header_obj) {
347 my $mime = do_cat_mail($git, $path) or return 0;
348 $header_obj = $mime->header_obj;
350 my $mid = $header_obj->header('Message-ID');
351 $mid = mid_compress(mid_clean($mid));
352 $u = $enc_utf8->decode($u);
353 push @$order, [ $mid, $ts, $u, $subj, $topic ];
356 0; # old topic, continue going
361 my ($order, $subjs) = @$topics;
363 $dst .= "\n[No recent topics]" unless (scalar @$order);
364 while (defined(my $info = shift @$order)) {
365 my ($mid, $ts, $u, $subj, $topic) = @$info;
366 my $n = delete $subjs->{$topic};
367 $mid = PublicInbox::Hval->new($mid)->as_href;
368 $subj = PublicInbox::Hval->new($subj)->as_html;
369 $u = PublicInbox::Hval->new($u)->as_html;
370 $dst .= "\n<a\nhref=\"t/$mid/#u\"><b>$subj</b></a>\n- ";
371 $ts = strftime('%Y-%m-%d %H:%M', gmtime($ts));
373 $dst .= "created by $u @ $ts UTC\n"
375 # $n isn't the total number of posts on the topic,
376 # just the number of posts in the current "git log"
377 # window, so leave it unlabeled
378 $dst .= "updated by $u @ $ts UTC ($n)\n"