1 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
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;
13 DATEFMT => '%Y-%m-%dT%H:%M:%SZ', # atom standard
14 MAX_PER_PAGE => 25, # this needs to be tunable
19 my ($class, $ctx) = @_;
20 require XML::Atom::SimpleFeed;
22 my $max = $ctx->{max} || MAX_PER_PAGE;
24 my $feed_opts = get_feedopts($ctx);
25 my $addr = $feed_opts->{address};
26 $addr = $addr->[0] if ref($addr);
27 my $feed = XML::Atom::SimpleFeed->new(
28 title => $feed_opts->{description} || "unnamed feed",
29 link => $feed_opts->{url} || "http://example.com/",
32 href => $feed_opts->{atomurl} ||
33 "http://example.com/atom.xml",
35 id => 'mailto:' . ($addr || 'public-inbox@example.com'),
36 updated => POSIX::strftime(DATEFMT, gmtime),
40 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
41 each_recent_blob($ctx, sub {
42 my ($add, undef) = @_;
43 add_to_feed($feed_opts, $feed, $add, $git);
45 $git = undef; # destroy pipes
46 Email::Address->purge_cache;
50 sub generate_html_index {
51 my ($class, $ctx) = @_;
53 my $max = $ctx->{max} || MAX_PER_PAGE;
54 my $feed_opts = get_feedopts($ctx);
56 my $title = $feed_opts->{description} || '';
57 $title = PublicInbox::Hval->new_oneline($title)->as_html;
59 my $html = "<html><head><title>$title</title>" .
60 '<link rel="alternate" title="Atom feed"' . "\nhref=\"" .
61 $feed_opts->{atomurl} . "\"\ntype=\"application/atom+xml\"/>" .
65 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
66 my (undef, $last) = each_recent_blob($ctx, sub {
67 my ($path, $commit) = @_;
68 unless (defined $state) {
69 $state = [ $ctx->{srch}, {}, $commit, 0 ];
71 my $mime = do_cat_mail($git, $_[0]) or return 0;
72 $html .= PublicInbox::View->index_entry($mime, 0, $state);
75 Email::Address->purge_cache;
76 $git = undef; # destroy pipes.
78 my $footer = nav_footer($ctx->{cgi}, $last, $feed_opts, $state);
80 my $list_footer = $ctx->{footer};
81 $footer .= "\n" . $list_footer if $list_footer;
82 $footer = "<hr /><pre>$footer</pre>";
84 $html .= "$footer</body></html>";
90 my ($cgi, $last, $feed_opts, $state) = @_;
92 my $old_r = $cgi->param('r');
95 my $first = $state->[2];
96 my $anchor = $state->[3];
99 $next = qq!<a\nhref="?r=$last">next</a>!;
102 $head = $cgi->path_info;
103 $head = qq!<a\nhref="$head">head</a>!;
105 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
106 my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
107 "<a\nname=\"s$anchor\">page:</a> $next $head $atom $permalink";
110 sub each_recent_blob {
112 my $max = $ctx->{max} || MAX_PER_PAGE;
113 my $hex = '[a-f0-9]';
114 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
115 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
116 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
117 my $cgi = $ctx->{cgi};
119 # revision ranges may be specified
121 my $r = $cgi->param('r') if $cgi;
122 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
126 # get recent messages
127 # we could use git log -z, but, we already know ssoma will not
128 # leave us with filenames with spaces in them..
129 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
130 qw/log --no-notes --no-color --raw -r
131 --abbrev=16 --abbrev-commit/);
134 my $pid = open(my $log, '-|', @cmd) or
135 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
136 my %deleted; # only an optimization at this point
139 my ($cur_commit, $first_commit, $last_commit);
140 while (my $line = <$log>) {
141 if ($line =~ /$addmsg/o) {
143 next if $deleted{$add}; # optimization-only
144 $nr += $cb->($add, $cur_commit);
149 } elsif ($line =~ /$delmsg/o) {
151 } elsif ($line =~ /^commit (${hex}{7,40})/o) {
153 $first_commit = $1 unless defined $first_commit;
158 while (my $line = <$log>) {
159 if ($line =~ /^commit (${hex}{7,40})/o) {
166 close $log; # we may EPIPE here
168 ($first_commit, $last_commit);
171 # private functions below
174 my $pi_config = $ctx->{pi_config};
175 my $listname = $ctx->{listname};
176 my $cgi = $ctx->{cgi};
178 if (open my $fh, '<', "$ctx->{git_dir}/description") {
179 chomp($rv{description} = <$fh>);
183 if ($pi_config && defined $listname && length $listname) {
184 foreach my $key (qw(address)) {
185 $rv{$key} = $pi_config->get($listname, $key) || "";
190 my $path_info = $cgi->path_info;
192 if (ref($cgi) eq 'CGI') {
193 $base = $cgi->url(-base);
195 $base = $cgi->base->as_string;
198 $url_base = $path_info;
199 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
200 $rv{atomurl} = "$base$url_base/atom.xml";
202 $url_base =~ s!/atom\.xml\z!!;
203 $rv{atomurl} = $base . $path_info;
204 $url_base = $base . $url_base; # XXX is this needed?
207 $url_base = "http://example.com";
208 $rv{atomurl} = "$url_base/atom.xml";
210 $rv{url} ||= "$url_base/";
211 $rv{midurl} = "$url_base/m/";
212 $rv{fullurl} = "$url_base/f/";
218 my ($mime, $name) = @_;
219 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
224 my @t = eval { strptime($date) };
226 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
229 # returns 0 (skipped) or 1 (added)
231 my ($feed_opts, $feed, $add, $git) = @_;
233 my $mime = do_cat_mail($git, $add) or return 0;
234 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
235 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
237 my $header_obj = $mime->header_obj;
238 my $mid = $header_obj->header_raw('Message-ID');
239 defined $mid or return 0;
240 $mid = PublicInbox::Hval->new_msgid($mid);
241 my $href = $mid->as_href . '.html';
242 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
243 defined($content) or return 0;
246 my $subject = mime_header($header_obj, 'Subject') or return 0;
248 my $from = mime_header($header_obj, 'From') or return 0;
249 my @from = Email::Address->parse($from);
250 my $name = $from[0]->name;
251 defined $name or $name = "";
252 my $email = $from[0]->address;
253 defined $email or $email = "";
255 my $date = $header_obj->header('Date');
256 $date = PublicInbox::Hval->new_oneline($date);
257 $date = feed_date($date->raw) or return 0;
260 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
263 author => { name => $name, email => $email },
266 content => { type => 'xhtml', content => $content },
267 link => $midurl . $href,
268 id => 'urn:uuid:' . join('-', @uuid5),
274 my ($git, $path) = @_;
276 my $str = $git->cat_file("HEAD:$path");
277 Email::MIME->new($str);