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);
46 $git = undef; # destroy pipes
47 Email::Address->purge_cache;
51 sub generate_html_index {
52 my ($class, $args) = @_;
55 my $max = $args->{max} || MAX_PER_PAGE;
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 = PublicInbox::GitCatFile->new($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;
73 $git = undef; # destroy pipes.
75 my $th = Mail::Thread->new(@messages);
77 my $html = "<html><head><title>$title</title>" .
78 '<link rel="alternate" title="Atom feed" href="' .
79 $feed_opts->{atomurl} . '" type="application/atom+xml"/>' .
82 # sort by date, most recent at top
85 $b->topmost->message->header('X-PI-Date') <=>
86 $a->topmost->message->header('X-PI-Date')
89 dump_html_line($_, 0, \$html) for $th->rootset;
91 Email::Address->purge_cache;
93 my $footer = nav_footer($args->{cgi}, $last);
94 $footer = "<hr /><pre>$footer</pre>" if $footer;
95 $html . "</pre>$footer</html>";
101 my ($cgi, $last) = @_;
103 my $old_r = $cgi->param('r');
108 $next = qq!<a href="?r=$last">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 = ('git', "--git-dir=$args->{git_dir}",
137 qw/log --no-notes --no-color --raw -r/);
140 my $pid = open(my $log, '-|', @cmd) or
141 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
142 my %deleted; # only an optimization at this point
146 while (my $line = <$log>) {
147 if ($line =~ /$addmsg/o) {
149 next if $deleted{$add};
155 } elsif ($line =~ /$delmsg/o) {
157 } elsif ($line =~ /^commit (${hex}{40})/) {
163 while (my $line = <$log>) {
164 if ($line =~ /^commit (${hex}{40})/) {
170 push @commits, undef;
173 close $log; # we may EPIPE here
178 # private functions below
181 my $pi_config = $args->{pi_config};
182 my $listname = $args->{listname};
183 my $cgi = $args->{cgi};
185 if (open my $fh, '<', "$args->{git_dir}/description") {
186 chomp($rv{description} = <$fh>);
190 if ($pi_config && defined $listname && length $listname) {
191 foreach my $key (qw(address)) {
192 $rv{$key} = $pi_config->get($listname, $key) || "";
197 my $path_info = $cgi->path_info;
199 if (ref($cgi) eq 'CGI') {
200 $base = $cgi->url(-base);
202 $base = $cgi->base->as_string;
205 $url_base = $path_info;
206 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
207 $rv{atomurl} = "$base$url_base/atom.xml";
209 $url_base =~ s!/atom\.xml\z!!;
210 $rv{atomurl} = $base . $path_info;
211 $url_base = $base . $url_base; # XXX is this needed?
214 $url_base = "http://example.com";
215 $rv{atomurl} = "$url_base/atom.xml";
217 $rv{url} ||= "$url_base/";
218 $rv{midurl} = "$url_base/m/";
219 $rv{fullurl} = "$url_base/f/";
225 my ($mime, $name) = @_;
226 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
231 my @t = eval { strptime($date) };
233 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
236 # returns 0 (skipped) or 1 (added)
238 my ($feed_opts, $feed, $add, $git) = @_;
240 my $mime = do_cat_mail($git, $add) or return 0;
241 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
242 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
244 my $mid = $mime->header_obj->header_raw('Message-ID');
245 defined $mid or return 0;
246 $mid = PublicInbox::Hval->new_msgid($mid);
247 my $href = $mid->as_href . '.html';
248 my $content = PublicInbox::View->as_feed_entry($mime, $fullurl . $href);
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, $html) = @_;
280 if ($self->message) {
281 $$html .= (' ' 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 $href = 'm/' . $mid->as_href . '.html';
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 $$html .= "<a href=\"$href\">$subj</a> $from\n";
297 dump_html_line($self->child, $level+1, $html) if $self->child;
298 dump_html_line($self->next, $level, $html) if $self->next;
302 my ($git, $path) = @_;
303 my $str = $git->cat_file("HEAD:$path");
304 Email::MIME->new($$str);