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;
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 my $feed_opts = get_feedopts($args);
27 my $addr = $feed_opts->{address};
28 $addr = $addr->[0] if ref($addr);
29 my $feed = XML::Atom::SimpleFeed->new(
30 title => $feed_opts->{description} || "unnamed feed",
31 link => $feed_opts->{url} || "http://example.com/",
34 href => $feed_opts->{atomurl} ||
35 "http://example.com/atom.xml",
37 id => 'mailto:' . ($addr || 'public-inbox@example.com'),
38 updated => POSIX::strftime(DATEFMT, gmtime),
41 my $git = PublicInbox::GitCatFile->new($args->{git_dir});
42 each_recent_blob($args, sub {
44 add_to_feed($feed_opts, $feed, $add, $git);
49 sub generate_html_index {
50 my ($class, $args) = @_;
53 my $max = $args->{max} || MAX_PER_PAGE;
54 my $feed_opts = get_feedopts($args);
56 my $title = $feed_opts->{description} || '';
57 $title = PublicInbox::Hval->new_oneline($title)->as_html;
60 my $git = PublicInbox::GitCatFile->new($args->{git_dir});
61 my $last = each_recent_blob($args, sub {
62 my $mime = do_cat_mail($git, $_[0]) or return 0;
63 $mime->body_set(''); # save some memory
65 my $t = eval { str2time($mime->header('Date')) };
66 defined($t) or $t = 0;
67 $mime->header_set('X-PI-Date', $t);
68 push @messages, $mime;
72 my $th = Mail::Thread->new(@messages);
74 my $html = "<html><head><title>$title</title>" .
75 '<link rel="alternate" title="Atom feed" href="' .
76 $feed_opts->{atomurl} . '" type="application/atom+xml"/>' .
79 # sort by date, most recent at top
82 $b->topmost->message->header('X-PI-Date') <=>
83 $a->topmost->message->header('X-PI-Date')
86 dump_html_line($_, 0, \$html) for $th->rootset;
88 my $footer = nav_footer($args->{cgi}, $last);
89 $footer = "<hr /><pre>$footer</pre>" if $footer;
90 $html . "</pre>$footer</html>";
96 my ($cgi, $last) = @_;
98 my $old_r = $cgi->param('r');
103 $next = qq!<a href="?r=$last">next</a>!;
106 $head = $cgi->path_info;
107 $head = qq!<a href="$head">head</a>!;
112 sub each_recent_blob {
113 my ($args, $cb) = @_;
114 my $max = $args->{max} || MAX_PER_PAGE;
115 my $hex = '[a-f0-9]';
116 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
117 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
118 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
119 my $cgi = $args->{cgi};
121 # revision ranges may be specified
123 my $r = $cgi->param('r') if $cgi;
124 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
128 # get recent messages
129 # we could use git log -z, but, we already know ssoma will not
130 # leave us with filenames with spaces in them..
131 my @cmd = ('git', "--git-dir=$args->{git_dir}",
132 qw/log --no-notes --no-color --raw -r/);
135 my $pid = open(my $log, '-|', @cmd) or
136 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
137 my %deleted; # only an optimization at this point
141 while (my $line = <$log>) {
142 if ($line =~ /$addmsg/o) {
144 next if $deleted{$add};
150 } elsif ($line =~ /$delmsg/o) {
152 } elsif ($line =~ /^commit (${hex}{40})/) {
158 while (my $line = <$log>) {
159 if ($line =~ /^commit (${hex}{40})/) {
165 push @commits, undef;
168 close $log; # we may EPIPE here
173 # private functions below
176 my $pi_config = $args->{pi_config};
177 my $listname = $args->{listname};
178 my $cgi = $args->{cgi};
180 if (open my $fh, '<', "$args->{git_dir}/description") {
181 chomp($rv{description} = <$fh>);
185 if ($pi_config && defined $listname && length $listname) {
186 foreach my $key (qw(address)) {
187 $rv{$key} = $pi_config->get($listname, $key) || "";
192 my $path_info = $cgi->path_info;
194 if (ref($cgi) eq 'CGI') {
195 $base = $cgi->url(-base);
197 $base = $cgi->base->as_string;
200 $url_base = $path_info;
201 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
202 $rv{atomurl} = "$base$url_base/atom.xml";
204 $url_base =~ s!/atom\.xml\z!!;
205 $rv{atomurl} = $base . $path_info;
206 $url_base = $base . $url_base; # XXX is this needed?
209 $url_base = "http://example.com";
210 $rv{atomurl} = "$url_base/atom.xml";
212 $rv{url} ||= "$url_base/";
213 $rv{midurl} = "$url_base/m/";
214 $rv{fullurl} = "$url_base/f/";
220 my ($mime, $name) = @_;
221 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
226 my @t = eval { strptime($date) };
228 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
231 # returns 0 (skipped) or 1 (added)
233 my ($feed_opts, $feed, $add, $git) = @_;
235 my $mime = do_cat_mail($git, $add) or return 0;
236 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
237 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
239 my $mid = $mime->header_obj->header_raw('Message-ID');
240 defined $mid or return 0;
241 $mid = PublicInbox::Hval->new_msgid($mid);
242 my $href = $mid->as_href . '.html';
243 my $content = PublicInbox::View->as_feed_entry($mime, $fullurl . $href);
244 defined($content) or return 0;
246 my $subject = mime_header($mime, 'Subject') or return 0;
248 my $from = mime_header($mime, '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 = $mime->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 ($self, $level, $html) = @_;
275 if ($self->message) {
276 $$html .= (' ' x $level);
277 my $mime = $self->message;
278 my $subj = $mime->header('Subject');
279 my $mid = $mime->header_obj->header_raw('Message-ID');
280 $mid = PublicInbox::Hval->new_msgid($mid);
281 my $href = 'm/' . $mid->as_href . '.html';
282 my $from = mime_header($mime, 'From');
284 my @from = Email::Address->parse($from);
285 $from = $from[0]->name;
286 (defined($from) && length($from)) or $from = $from[0]->address;
288 $from = PublicInbox::Hval->new_oneline($from)->as_html;
289 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
290 $$html .= "<a href=\"$href\">$subj</a> $from\n";
292 dump_html_line($self->child, $level+1, $html) if $self->child;
293 dump_html_line($self->next, $level, $html) if $self->next;
297 my ($git, $path) = @_;
298 my $str = $git->cat_file("HEAD:$path");
299 Email::MIME->new($$str);