]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
use tables for rendering comment nesting
[public-inbox.git] / lib / PublicInbox / Feed.pm
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;
4 use strict;
5 use warnings;
6 use Email::Address;
7 use Email::MIME;
8 use Date::Parse qw(strptime);
9 use PublicInbox::Hval;
10 use PublicInbox::GitCatFile;
11 use PublicInbox::View;
12 use constant {
13         DATEFMT => '%Y-%m-%dT%H:%M:%SZ', # atom standard
14         MAX_PER_PAGE => 25, # this needs to be tunable
15 };
16
17 # main function
18 sub generate {
19         my ($class, $ctx) = @_;
20         require XML::Atom::SimpleFeed;
21         require POSIX;
22         my $max = $ctx->{max} || MAX_PER_PAGE;
23
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/",
30                 link => {
31                         rel => 'self',
32                         href => $feed_opts->{atomurl} ||
33                                 "http://example.com/atom.xml",
34                 },
35                 id => 'mailto:' . ($addr || 'public-inbox@example.com'),
36                 updated => POSIX::strftime(DATEFMT, gmtime),
37         );
38         $feed->no_generator;
39
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);
44         });
45         $git = undef; # destroy pipes
46         Email::Address->purge_cache;
47         $feed->as_string;
48 }
49
50 sub generate_html_index {
51         my ($class, $ctx) = @_;
52
53         my $max = $ctx->{max} || MAX_PER_PAGE;
54         my $feed_opts = get_feedopts($ctx);
55
56         my $title = $feed_opts->{description} || '';
57         $title = PublicInbox::Hval->new_oneline($title)->as_html;
58
59         my $html = "<html><head><title>$title</title>" .
60                 '<link rel="alternate" title="Atom feed"' . "\nhref=\"" .
61                 $feed_opts->{atomurl} . "\"\ntype=\"application/atom+xml\"/>" .
62                 '</head><body>';
63
64         my $state;
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 ];
70                 }
71                 my $mime = do_cat_mail($git, $_[0]) or return 0;
72                 $html .= PublicInbox::View->index_entry($mime, 0, $state);
73                 1;
74         });
75         Email::Address->purge_cache;
76         $git = undef; # destroy pipes.
77
78         my $footer = nav_footer($ctx->{cgi}, $last, $feed_opts, $state);
79         if ($footer) {
80                 my $list_footer = $ctx->{footer};
81                 $footer .= "\n" . $list_footer if $list_footer;
82                 $footer = "<hr /><pre>$footer</pre>";
83         }
84         $html .= "$footer</body></html>";
85 }
86
87 # private subs
88
89 sub nav_footer {
90         my ($cgi, $last, $feed_opts, $state) = @_;
91         $cgi or return '';
92         my $old_r = $cgi->param('r');
93         my $head = '    ';
94         my $next = '    ';
95         my $first = $state->[2];
96         my $anchor = $state->[3];
97
98         if ($last) {
99                 $next = qq!<a\nhref="?r=$last">next</a>!;
100         }
101         if ($old_r) {
102                 $head = $cgi->path_info;
103                 $head = qq!<a\nhref="$head">head</a>!;
104         }
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";
108 }
109
110 sub each_recent_blob {
111         my ($ctx, $cb) = @_;
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};
118
119         # revision ranges may be specified
120         my $range = 'HEAD';
121         my $r = $cgi->param('r') if $cgi;
122         if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
123                 $range = $r;
124         }
125
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/);
132         push @cmd, $range;
133
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
137         my $last;
138         my $nr = 0;
139         my ($cur_commit, $first_commit, $last_commit);
140         while (my $line = <$log>) {
141                 if ($line =~ /$addmsg/o) {
142                         my $add = $1;
143                         next if $deleted{$add}; # optimization-only
144                         $nr += $cb->($add, $cur_commit);
145                         if ($nr >= $max) {
146                                 $last = 1;
147                                 last;
148                         }
149                 } elsif ($line =~ /$delmsg/o) {
150                         $deleted{$1} = 1;
151                 } elsif ($line =~ /^commit (${hex}{7,40})/o) {
152                         $cur_commit = $1;
153                         $first_commit = $1 unless defined $first_commit;
154                 }
155         }
156
157         if ($last) {
158                 while (my $line = <$log>) {
159                         if ($line =~ /^commit (${hex}{7,40})/o) {
160                                 $last_commit = $1;
161                                 last;
162                         }
163                 }
164         }
165
166         close $log; # we may EPIPE here
167         # for pagination
168         ($first_commit, $last_commit);
169 }
170
171 # private functions below
172 sub get_feedopts {
173         my ($ctx) = @_;
174         my $pi_config = $ctx->{pi_config};
175         my $listname = $ctx->{listname};
176         my $cgi = $ctx->{cgi};
177         my %rv;
178         if (open my $fh, '<', "$ctx->{git_dir}/description") {
179                 chomp($rv{description} = <$fh>);
180                 close $fh;
181         }
182
183         if ($pi_config && defined $listname && length $listname) {
184                 foreach my $key (qw(address)) {
185                         $rv{$key} = $pi_config->get($listname, $key) || "";
186                 }
187         }
188         my $url_base;
189         if ($cgi) {
190                 my $path_info = $cgi->path_info;
191                 my $base;
192                 if (ref($cgi) eq 'CGI') {
193                         $base = $cgi->url(-base);
194                 } else {
195                         $base = $cgi->base->as_string;
196                         $base =~ s!/\z!!;
197                 }
198                 $url_base = $path_info;
199                 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
200                         $rv{atomurl} = "$base$url_base/atom.xml";
201                 } else {
202                         $url_base =~ s!/atom\.xml\z!!;
203                         $rv{atomurl} = $base . $path_info;
204                         $url_base = $base . $url_base; # XXX is this needed?
205                 }
206         } else {
207                 $url_base = "http://example.com";
208                 $rv{atomurl} = "$url_base/atom.xml";
209         }
210         $rv{url} ||= "$url_base/";
211         $rv{midurl} = "$url_base/m/";
212         $rv{fullurl} = "$url_base/f/";
213
214         \%rv;
215 }
216
217 sub mime_header {
218         my ($mime, $name) = @_;
219         PublicInbox::Hval->new_oneline($mime->header($name))->raw;
220 }
221
222 sub feed_date {
223         my ($date) = @_;
224         my @t = eval { strptime($date) };
225
226         scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
227 }
228
229 # returns 0 (skipped) or 1 (added)
230 sub add_to_feed {
231         my ($feed_opts, $feed, $add, $git) = @_;
232
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/';
236
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;
244         $mime = undef;
245
246         my $subject = mime_header($header_obj, 'Subject') or return 0;
247
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 = "";
254
255         my $date = $header_obj->header('Date');
256         $date = PublicInbox::Hval->new_oneline($date);
257         $date = feed_date($date->raw) or return 0;
258         $add =~ tr!/!!d;
259         my $h = '[a-f0-9]';
260         my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
261
262         $feed->add_entry(
263                 author => { name => $name, email => $email },
264                 title => $subject,
265                 updated => $date,
266                 content => { type => 'xhtml', content => $content },
267                 link => $midurl . $href,
268                 id => 'urn:uuid:' . join('-', @uuid5),
269         );
270         1;
271 }
272
273 sub do_cat_mail {
274         my ($git, $path) = @_;
275         my $mime = eval {
276                 my $str = $git->cat_file("HEAD:$path");
277                 Email::MIME->new($str);
278         };
279         $@ ? undef : $mime;
280 }
281
282 1;