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/;
14 DATEFMT => '%Y-%m-%dT%H:%M:%SZ', # atom standard
15 MAX_PER_PAGE => 25, # this needs to be tunable
18 use Encode qw/find_encoding/;
19 my $enc_utf8 = find_encoding('UTF-8');
24 sub { emit_atom($_[0], $ctx) };
27 sub generate_html_index {
29 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 $fh->write(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>} .
54 '<updated>' . POSIX::strftime(DATEFMT, gmtime) . '</updated>');
56 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
57 each_recent_blob($ctx, sub {
58 my ($add, undef) = @_;
59 add_to_feed($feed_opts, $fh, $add, $git);
61 $git = undef; # destroy pipes
62 Email::Address->purge_cache;
63 $fh->write("</feed>");
70 my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
72 my $max = $ctx->{max} || MAX_PER_PAGE;
73 my $feed_opts = get_feedopts($ctx);
75 my $title = $feed_opts->{description} || '';
76 $title = PublicInbox::Hval->new_oneline($title)->as_html;
77 my $atom_url = $feed_opts->{atomurl};
79 $fh->write("<html><head><title>$title</title>" .
80 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
81 "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
82 '</head><body>' . PublicInbox::View::PRE_WRAP .
83 "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)\n");
86 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
88 my $srch = $ctx->{srch};
89 $srch and $topics = [ [], {} ];
90 my (undef, $last) = each_recent_blob($ctx, sub {
91 my ($path, $commit, $ts, $u, $subj) = @_;
95 first_commit => $commit,
100 add_topic($git, $srch, $topics, $path, $ts, $u, $subj);
102 my $mime = do_cat_mail($git, $path) or return 0;
103 PublicInbox::View::index_entry($fh, $mime, 0, $state);
107 Email::Address->purge_cache;
108 $git = undef; # destroy pipes.
110 my $footer = nav_footer($ctx->{cgi}, $last, $feed_opts, $state);
112 my $list_footer = $ctx->{footer};
113 $footer .= "\n" . $list_footer if $list_footer;
114 $footer = "<hr /><pre>$footer</pre>";
116 $fh->write(dump_topics($topics)) if $topics;
117 $fh->write("$footer</body></html>");
122 my ($cgi, $last, $feed_opts, $state) = @_;
124 my $old_r = $cgi->param('r');
127 my $first = $state->{first_commit};
128 my $anchor = $state->{anchor_idx};
131 $next = qq!<a\nhref="?r=$last">next</a>!;
134 $head = $cgi->path_info;
135 $head = qq!<a\nhref="$head">head</a>!;
137 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
138 my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
139 "<a\nname=\"s$anchor\">page:</a> $next $head $atom $permalink";
142 sub each_recent_blob {
144 my $max = $ctx->{max} || MAX_PER_PAGE;
145 my $hex = '[a-f0-9]';
146 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
147 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
148 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
149 my $cgi = $ctx->{cgi};
151 # revision ranges may be specified
153 my $r = $cgi->param('r') if $cgi;
154 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
158 # get recent messages
159 # we could use git log -z, but, we already know ssoma will not
160 # leave us with filenames with spaces in them..
161 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
162 qw/log --no-notes --no-color --raw -r
163 --abbrev=16 --abbrev-commit/,
164 "--format=%h%x00%ct%x00%an%x00%s%x00");
167 my $pid = open(my $log, '-|', @cmd) or
168 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
169 my %deleted; # only an optimization at this point
172 my ($cur_commit, $first_commit, $last_commit);
174 while (defined(my $line = <$log>)) {
175 if ($line =~ /$addmsg/o) {
177 next if $deleted{$add}; # optimization-only
178 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
183 } elsif ($line =~ /$delmsg/o) {
185 } elsif ($line =~ /^${hex}{7,40}/o) {
186 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
187 unless (defined $first_commit) {
188 $first_commit = $cur_commit;
194 while (my $line = <$log>) {
195 if ($line =~ /^(${hex}{7,40})/o) {
202 close $log; # we may EPIPE here
204 ($first_commit, $last_commit);
207 # private functions below
210 my $pi_config = $ctx->{pi_config};
211 my $listname = $ctx->{listname};
212 my $cgi = $ctx->{cgi};
214 if (open my $fh, '<', "$ctx->{git_dir}/description") {
215 chomp($rv{description} = <$fh>);
219 if ($pi_config && defined $listname && length $listname) {
220 foreach my $key (qw(address)) {
221 $rv{$key} = $pi_config->get($listname, $key) || "";
226 my $path_info = $cgi->path_info;
228 if (ref($cgi) eq 'CGI') {
229 $base = $cgi->url(-base);
231 $base = $cgi->base->as_string;
234 $url_base = $path_info;
235 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
236 $rv{atomurl} = "$base$url_base/atom.xml";
238 $url_base =~ s!/atom\.xml\z!!;
239 $rv{atomurl} = $base . $path_info;
240 $url_base = $base . $url_base; # XXX is this needed?
243 $url_base = "http://example.com";
244 $rv{atomurl} = "$url_base/atom.xml";
246 $rv{url} ||= "$url_base/";
247 $rv{midurl} = "$url_base/m/";
248 $rv{fullurl} = "$url_base/f/";
254 my ($mime, $name) = @_;
255 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
260 my @t = eval { strptime($date) };
262 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
265 # returns 0 (skipped) or 1 (added)
267 my ($feed_opts, $fh, $add, $git) = @_;
269 my $mime = do_cat_mail($git, $add) or return 0;
270 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
272 my $header_obj = $mime->header_obj;
273 my $mid = $header_obj->header('Message-ID');
274 defined $mid or return 0;
275 $mid = PublicInbox::Hval->new_msgid($mid);
276 my $href = $mid->as_href . '.html';
277 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
278 defined($content) or return 0;
281 my $title = mime_header($header_obj, 'Subject') or return 0;
282 $title = PublicInbox::Hval->new_oneline($title)->as_html;
283 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
285 my $from = mime_header($header_obj, 'From') or return 0;
286 my @from = Email::Address->parse($from) or return 0;
287 my $name = PublicInbox::Hval->new_oneline($from[0]->name)->as_html;
288 my $email = $from[0]->address;
289 $email = PublicInbox::Hval->new_oneline($email)->as_html;
291 my $date = $header_obj->header('Date');
292 $date = PublicInbox::Hval->new_oneline($date);
293 $date = feed_date($date->raw) or return 0;
295 $fh->write("<entry><author><name>$name</name><email>$email</email>" .
296 "</author><title$type>$title</title>" .
297 "<updated>$date</updated>" .
298 qq{<content\ntype="xhtml">} .
299 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
300 $fh->write($content);
304 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
305 my $id = 'urn:uuid:' . join('-', @uuid5);
306 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
307 $fh->write(qq{</div></content><link\nhref="$midurl$href"/>}.
308 "<id>$id</id></entry>");
313 my ($git, $path) = @_;
315 my $str = $git->cat_file("HEAD:$path");
316 Email::MIME->new($str);
321 # accumulate recent topics if search is supported
323 my ($git, $srch, $topics, $path, $ts, $u, $subj) = @_;
324 my ($order, $subjs) = @$topics;
327 # legacy ssoma did not set commit titles based on Subject
328 $subj = $enc_utf8->decode($subj);
329 if ($subj eq 'mda') {
330 my $mime = do_cat_mail($git, $path) or return 0;
331 $header_obj = $mime->header_obj;
332 $subj = mime_header($header_obj, 'Subject');
335 $subj = $srch->subject_normalized($subj);
336 if (++$subjs->{$subj} == 1) {
337 unless ($header_obj) {
338 my $mime = do_cat_mail($git, $path) or return 0;
339 $header_obj = $mime->header_obj;
341 my $mid = $header_obj->header('Message-ID');
342 $mid = mid_compress(mid_clean($mid));
343 $u = $enc_utf8->decode($u);
344 push @$order, [ $mid, $ts, $u, $subj ];
347 0; # old topic, continue going
352 my ($order, $subjs) = @$topics;
354 $dst .= "\n[No recent topics]" unless (scalar @$order);
355 while (defined(my $info = shift @$order)) {
356 my ($mid, $ts, $u, $subj) = @$info;
357 my $n = delete $subjs->{$subj};
358 $mid = PublicInbox::Hval->new($mid)->as_href;
359 $subj = PublicInbox::Hval->new($subj)->as_html;
360 $u = PublicInbox::Hval->new($u)->as_html;
361 $dst .= "\n<a\nhref=\"t/$mid.html#u\"><b>$subj</b></a>\n- ";
362 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
364 $dst .= "created by $u @ $ts UTC\n"
366 # $n isn't the total number of posts on the topic,
367 # just the number of posts in the current "git log"
368 # window, so leave it unlabeled
369 $dst .= "updated by $u @ $ts UTC ($n)\n"