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 eval { require Git }; # this is GPLv2+, so we are OK to use it
12 DATEFMT => '%Y-%m-%dT%H:%M:%SZ',
16 # FIXME: workaround https://rt.cpan.org/Public/Bug/Display.html?id=22817
20 my ($class, $args) = @_;
21 require XML::Atom::SimpleFeed;
22 require PublicInbox::View;
24 my $max = $args->{max} || MAX_PER_PAGE;
26 local $ENV{GIT_DIR} = $args->{git_dir};
27 my $feed_opts = get_feedopts($args);
28 my $addr = $feed_opts->{address};
29 $addr = $addr->[0] if ref($addr);
30 my $feed = XML::Atom::SimpleFeed->new(
31 title => $feed_opts->{description} || "unnamed feed",
32 link => $feed_opts->{url} || "http://example.com/",
35 href => $feed_opts->{atomurl} ||
36 "http://example.com/atom.xml",
38 id => 'mailto:' . ($addr || 'public-inbox@example.com'),
39 updated => POSIX::strftime(DATEFMT, gmtime),
42 my $git = try_git_pm($args->{git_dir});
43 each_recent_blob($args, sub {
45 add_to_feed($feed_opts, $feed, $add, $git);
50 sub generate_html_index {
51 my ($class, $args) = @_;
54 my $max = $args->{max} || MAX_PER_PAGE;
55 local $ENV{GIT_DIR} = $args->{git_dir};
56 my $feed_opts = get_feedopts($args);
58 my $title = $feed_opts->{description} || '';
59 $title = PublicInbox::Hval->new_oneline($title)->as_html;
62 my $git = try_git_pm($args->{git_dir});
63 my $last = each_recent_blob($args, sub {
64 my $mime = do_cat_mail($git, $_[0]) or return 0;
65 $mime->body_set(''); # save some memory
67 my $t = eval { str2time($mime->header('Date')) };
68 defined($t) or $t = 0;
69 $mime->header_set('X-PI-Date', $t);
70 push @messages, $mime;
74 my $th = Mail::Thread->new(@messages);
77 "<html><head><title>$title</title>" .
78 '<link rel="alternate" title="Atom feed" href="' .
79 $feed_opts->{atomurl} . '" type="application/atom+xml"/>' .
80 '</head><body><pre>');
81 push @out, $feed_opts->{midurl};
83 # sort by date, most recent at top
86 $b->topmost->message->header('X-PI-Date') <=>
87 $a->topmost->message->header('X-PI-Date')
90 dump_html_line($_, 0, \@out) for $th->rootset;
92 my $footer = nav_footer($args->{cgi}, $last);
93 $footer = "<hr /><pre>$footer</pre>" if $footer;
94 $out[0] . "</pre>$footer</html>";
100 my ($cgi, $last) = @_;
102 my $old_r = $cgi->param('r');
107 $next = $cgi->path_info . "?r=$last";
108 $next = qq!<a href="$next">next</a>!;
111 $head = $cgi->path_info;
112 $head = qq!<a href="$head">head</a>!;
117 sub each_recent_blob {
118 my ($args, $cb) = @_;
119 my $max = $args->{max} || MAX_PER_PAGE;
120 my $hex = '[a-f0-9]';
121 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
122 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
123 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
124 my $cgi = $args->{cgi};
126 # revision ranges may be specified
128 my $r = $cgi->param('r') if $cgi;
129 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
133 # get recent messages
134 # we could use git log -z, but, we already know ssoma will not
135 # leave us with filenames with spaces in them..
136 my @cmd = qw/git log --no-notes --no-color --raw -r/;
139 my $pid = open(my $log, '-|', @cmd) or
140 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
141 my %deleted; # only an optimization at this point
145 while (my $line = <$log>) {
146 if ($line =~ /$addmsg/o) {
148 next if $deleted{$add};
154 } elsif ($line =~ /$delmsg/o) {
156 } elsif ($line =~ /^commit (${hex}{40})/) {
162 while (my $line = <$log>) {
163 if ($line =~ /^commit (${hex}{40})/) {
169 push @commits, undef;
172 close $log; # we may EPIPE here
177 # private functions below
180 my $pi_config = $args->{pi_config};
181 my $listname = $args->{listname};
182 my $cgi = $args->{cgi};
184 if (open my $fh, '<', "$args->{git_dir}/description") {
185 chomp($rv{description} = <$fh>);
189 if ($pi_config && defined $listname && length $listname) {
190 foreach my $key (qw(address)) {
191 $rv{$key} = $pi_config->get($listname, $key) || "";
196 my $path_info = $cgi->path_info;
198 if (ref($cgi) eq 'CGI') {
199 $base = $cgi->url(-base);
201 $base = "${$cgi->base}";
204 $url_base = $path_info;
205 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
206 $rv{atomurl} = "$base$url_base/atom.xml";
208 $url_base =~ s!/atom\.xml\z!!;
209 $rv{atomurl} = $base . $path_info;
210 $url_base = $base . $url_base; # XXX is this needed?
213 $url_base = "http://example.com";
214 $rv{atomurl} = "$url_base/atom.xml";
216 $rv{url} ||= "$url_base/";
217 $rv{midurl} = "$url_base/m/";
218 $rv{fullurl} = "$url_base/f/";
224 my ($mime, $name) = @_;
225 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
230 my @t = eval { strptime($date) };
232 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
235 # returns 0 (skipped) or 1 (added)
237 my ($feed_opts, $feed, $add, $git) = @_;
239 my $mime = do_cat_mail($git, $add) or return 0;
240 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
241 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
243 my $mid = $mime->header_obj->header_raw('Message-ID');
244 defined $mid or return 0;
245 $mid = PublicInbox::Hval->new_msgid($mid);
246 my $href = $mid->as_href;
247 my $content = PublicInbox::View->as_feed_entry($mime,
248 "$fullurl$href.html");
249 defined($content) or return 0;
251 my $subject = mime_header($mime, 'Subject') or return 0;
253 my $from = mime_header($mime, 'From') or return 0;
254 my @from = Email::Address->parse($from);
255 my $name = $from[0]->name;
256 defined $name or $name = "";
257 my $email = $from[0]->address;
258 defined $email or $email = "";
260 my $date = $mime->header('Date');
261 $date = PublicInbox::Hval->new_oneline($date);
262 $date = feed_date($date->raw) or return 0;
265 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
268 author => { name => $name, email => $email },
271 content => { type => 'xhtml', content => $content },
272 link => $midurl . $href,
273 id => 'urn:uuid:' . join('-', @uuid5),
279 my ($self, $level, $args) = @_; # args => [ $html, $midurl ]
280 if ($self->message) {
281 $args->[0] .= (' ' x $level);
282 my $mime = $self->message;
283 my $subj = $mime->header('Subject');
284 my $mid = $mime->header_obj->header_raw('Message-ID');
285 $mid = PublicInbox::Hval->new_msgid($mid);
286 my $url = $args->[1] . $mid->as_href;
287 my $from = mime_header($mime, 'From');
289 my @from = Email::Address->parse($from);
290 $from = $from[0]->name;
291 (defined($from) && length($from)) or $from = $from[0]->address;
293 $from = PublicInbox::Hval->new_oneline($from)->as_html;
294 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
295 $args->[0] .= "<a href=\"$url.html\">$subj</a> $from\n";
297 dump_html_line($self->child, $level+1, $args) if $self->child;
298 dump_html_line($self->next, $level, $args) if $self->next;
303 eval { Git->repository(Directory => $dir) };
307 my ($git, $path) = @_;
310 open my $fh, '>', \$str or
311 die "failed to setup string handle: $!\n";
316 local $SIG{__WARN__} = sub { $err .= $_[0] };
317 $bytes = $git->cat_blob("HEAD:$path", $fh);
319 close $fh or die "failed to close string handle: $!\n";
321 if ($bytes < 0 && $err &&
322 $err !~ /doesn't exist in the repository/) {
325 return if $bytes <= 0;
327 $str = `git cat-file blob HEAD:$path`;
328 return if $? != 0 || length($str) == 0;
330 Email::MIME->new($str);