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 str2time);
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
15 PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">",
20 my ($class, $ctx) = @_;
21 require XML::Atom::SimpleFeed;
23 my $max = $ctx->{max} || MAX_PER_PAGE;
25 my $feed_opts = get_feedopts($ctx);
26 my $addr = $feed_opts->{address};
27 $addr = $addr->[0] if ref($addr);
28 my $feed = XML::Atom::SimpleFeed->new(
29 title => $feed_opts->{description} || "unnamed feed",
30 link => $feed_opts->{url} || "http://example.com/",
33 href => $feed_opts->{atomurl} ||
34 "http://example.com/atom.xml",
36 id => 'mailto:' . ($addr || 'public-inbox@example.com'),
37 updated => POSIX::strftime(DATEFMT, gmtime),
41 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
42 each_recent_blob($ctx, sub {
43 my ($add, undef) = @_;
44 add_to_feed($feed_opts, $feed, $add, $git);
46 $git = undef; # destroy pipes
47 Email::Address->purge_cache;
51 sub generate_html_index {
52 my ($class, $ctx) = @_;
54 my $max = $ctx->{max} || MAX_PER_PAGE;
55 my $feed_opts = get_feedopts($ctx);
57 my $title = $feed_opts->{description} || '';
58 $title = PublicInbox::Hval->new_oneline($title)->as_html;
60 my $html = "<html><head><title>$title</title>" .
61 '<link rel="alternate" title="Atom feed"' . "\nhref=\"" .
62 $feed_opts->{atomurl} . "\"\ntype=\"application/atom+xml\"/>" .
63 '</head><body>' . PRE_WRAP;
66 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
67 my (undef, $last) = each_recent_blob($ctx, sub {
68 my ($path, $commit) = @_;
69 unless (defined $state) {
70 $state = [ $ctx->{srch}, {}, $commit, 0 ];
72 my $mime = do_cat_mail($git, $_[0]) or return 0;
73 my $t = eval { str2time($mime->header('Date')) };
74 defined($t) or $t = 0;
75 $mime->header_set('X-PI-TS', $t);
76 $html .= PublicInbox::View->index_entry($mime, 0, $state);
79 Email::Address->purge_cache;
80 $git = undef; # destroy pipes.
82 my $footer = nav_footer($ctx->{cgi}, $last, $feed_opts, $state);
84 my $list_footer = $ctx->{footer};
85 $footer .= "\n" . $list_footer if $list_footer;
86 $footer = "<hr />" . PRE_WRAP . "$footer</pre>";
88 $html . "</pre>$footer</body></html>";
94 my ($cgi, $last, $feed_opts, $state) = @_;
96 my $old_r = $cgi->param('r');
99 my $first = $state->[2];
100 my $anchor = $state->[3];
103 $next = qq!<a\nhref="?r=$last">next</a>!;
106 $head = $cgi->path_info;
107 $head = qq!<a\nhref="$head">head</a>!;
109 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
110 my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
111 "<a\nname=\"s$anchor\">page:</a> $next $head $atom $permalink";
114 sub each_recent_blob {
116 my $max = $ctx->{max} || MAX_PER_PAGE;
117 my $hex = '[a-f0-9]';
118 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
119 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
120 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
121 my $cgi = $ctx->{cgi};
123 # revision ranges may be specified
125 my $r = $cgi->param('r') if $cgi;
126 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
130 # get recent messages
131 # we could use git log -z, but, we already know ssoma will not
132 # leave us with filenames with spaces in them..
133 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
134 qw/log --no-notes --no-color --raw -r
135 --abbrev=16 --abbrev-commit/);
138 my $pid = open(my $log, '-|', @cmd) or
139 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
140 my %deleted; # only an optimization at this point
143 my ($cur_commit, $first_commit, $last_commit);
144 while (my $line = <$log>) {
145 if ($line =~ /$addmsg/o) {
147 next if $deleted{$add}; # optimization-only
148 $nr += $cb->($add, $cur_commit);
153 } elsif ($line =~ /$delmsg/o) {
155 } elsif ($line =~ /^commit (${hex}{7,40})/o) {
157 $first_commit = $1 unless defined $first_commit;
162 while (my $line = <$log>) {
163 if ($line =~ /^commit (${hex}{7,40})/o) {
170 close $log; # we may EPIPE here
172 ($first_commit, $last_commit);
175 # private functions below
178 my $pi_config = $ctx->{pi_config};
179 my $listname = $ctx->{listname};
180 my $cgi = $ctx->{cgi};
182 if (open my $fh, '<', "$ctx->{git_dir}/description") {
183 chomp($rv{description} = <$fh>);
187 if ($pi_config && defined $listname && length $listname) {
188 foreach my $key (qw(address)) {
189 $rv{$key} = $pi_config->get($listname, $key) || "";
194 my $path_info = $cgi->path_info;
196 if (ref($cgi) eq 'CGI') {
197 $base = $cgi->url(-base);
199 $base = $cgi->base->as_string;
202 $url_base = $path_info;
203 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
204 $rv{atomurl} = "$base$url_base/atom.xml";
206 $url_base =~ s!/atom\.xml\z!!;
207 $rv{atomurl} = $base . $path_info;
208 $url_base = $base . $url_base; # XXX is this needed?
211 $url_base = "http://example.com";
212 $rv{atomurl} = "$url_base/atom.xml";
214 $rv{url} ||= "$url_base/";
215 $rv{midurl} = "$url_base/m/";
216 $rv{fullurl} = "$url_base/f/";
222 my ($mime, $name) = @_;
223 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
228 my @t = eval { strptime($date) };
230 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
233 # returns 0 (skipped) or 1 (added)
235 my ($feed_opts, $feed, $add, $git) = @_;
237 my $mime = do_cat_mail($git, $add) or return 0;
238 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
239 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
241 my $header_obj = $mime->header_obj;
242 my $mid = $header_obj->header_raw('Message-ID');
243 defined $mid or return 0;
244 $mid = PublicInbox::Hval->new_msgid($mid);
245 my $href = $mid->as_href . '.html';
246 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
247 defined($content) or return 0;
250 my $subject = mime_header($header_obj, 'Subject') or return 0;
252 my $from = mime_header($header_obj, 'From') or return 0;
253 my @from = Email::Address->parse($from);
254 my $name = $from[0]->name;
255 defined $name or $name = "";
256 my $email = $from[0]->address;
257 defined $email or $email = "";
259 my $date = $header_obj->header('Date');
260 $date = PublicInbox::Hval->new_oneline($date);
261 $date = feed_date($date->raw) or return 0;
264 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
267 author => { name => $name, email => $email },
270 content => { type => 'xhtml', content => $content },
271 link => $midurl . $href,
272 id => 'urn:uuid:' . join('-', @uuid5),
278 my ($git, $path) = @_;
280 my $str = $git->cat_file("HEAD:$path");
281 Email::MIME->new($str);