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, $args) = @_;
21 require XML::Atom::SimpleFeed;
23 my $max = $args->{max} || MAX_PER_PAGE;
25 my $feed_opts = get_feedopts($args);
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),
40 my $git = PublicInbox::GitCatFile->new($args->{git_dir});
41 each_recent_blob($args, sub {
43 add_to_feed($feed_opts, $feed, $add, $git);
45 $git = undef; # destroy pipes
46 Email::Address->purge_cache;
50 sub generate_html_index {
51 my ($class, $args) = @_;
52 require PublicInbox::Thread;
54 my $max = $args->{max} || MAX_PER_PAGE;
55 my $feed_opts = get_feedopts($args);
57 my $title = $feed_opts->{description} || '';
58 $title = PublicInbox::Hval->new_oneline($title)->as_html;
61 my $git_dir = $args->{git_dir};
62 my $git = PublicInbox::GitCatFile->new($git_dir);
63 my ($first, $last) = each_recent_blob($args, sub {
64 my $mime = do_cat_mail($git, $_[0]) or return 0;
66 my $t = eval { str2time($mime->header('Date')) };
67 defined($t) or $t = 0;
68 $mime->header_set('X-PI-Date', $t);
69 push @messages, $mime;
72 $git = undef; # destroy pipes.
74 my $th = PublicInbox::Thread->new(@messages);
76 my $html = "<html><head><title>$title</title>" .
77 '<link rel="alternate" title="Atom feed"' . "\nhref=\"" .
78 $feed_opts->{atomurl} . "\"\ntype=\"application/atom+xml\"/>" .
79 '</head><body>' . PRE_WRAP;
81 # sort child messages in chronological order
84 $a->topmost->message->header('X-PI-Date') <=>
85 $b->topmost->message->header('X-PI-Date')
89 # except we sort top-level messages reverse chronologically
90 my $state = [ time, {}, $first, 0 ];
91 for (sort { (eval { $b->message->header('X-PI-Date') } || 0) <=>
92 (eval { $a->message->header('X-PI-Date') } || 0)
94 dump_msg($_, 0, \$html, $state);
96 Email::Address->purge_cache;
98 my $footer = nav_footer($args->{cgi}, $last, $feed_opts, $state);
100 my $list_footer = $args->{footer};
101 $footer .= "\n" . $list_footer if $list_footer;
102 $footer = "<hr />" . PRE_WRAP . "$footer</pre>";
104 $html . "</pre>$footer</body></html>";
110 my ($cgi, $last, $feed_opts, $state) = @_;
112 my $old_r = $cgi->param('r');
115 my $first = $state->[2];
116 my $anchor = $state->[3];
119 $next = qq!<a\nhref="?r=$last">next</a>!;
122 $head = $cgi->path_info;
123 $head = qq!<a\nhref="$head">head</a>!;
125 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
126 my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
127 "<a\nname=\"s$anchor\">page:</a> $next $head $atom $permalink";
130 sub each_recent_blob {
131 my ($args, $cb) = @_;
132 my $max = $args->{max} || MAX_PER_PAGE;
133 my $hex = '[a-f0-9]';
134 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
135 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
136 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
137 my $cgi = $args->{cgi};
139 # revision ranges may be specified
141 my $r = $cgi->param('r') if $cgi;
142 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
146 # get recent messages
147 # we could use git log -z, but, we already know ssoma will not
148 # leave us with filenames with spaces in them..
149 my @cmd = ('git', "--git-dir=$args->{git_dir}",
150 qw/log --no-notes --no-color --raw -r
151 --abbrev=16 --abbrev-commit/);
154 my $pid = open(my $log, '-|', @cmd) or
155 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
156 my %deleted; # only an optimization at this point
160 while (my $line = <$log>) {
161 if ($line =~ /$addmsg/o) {
163 next if $deleted{$add};
169 } elsif ($line =~ /$delmsg/o) {
171 } elsif ($line =~ /^commit (${hex}{7,40})/) {
177 while (my $line = <$log>) {
178 if ($line =~ /^commit (${hex}{7,40})/) {
184 push @commits, undef;
187 close $log; # we may EPIPE here
189 ($commits[0], $commits[-1]);
192 # private functions below
195 my $pi_config = $args->{pi_config};
196 my $listname = $args->{listname};
197 my $cgi = $args->{cgi};
199 if (open my $fh, '<', "$args->{git_dir}/description") {
200 chomp($rv{description} = <$fh>);
204 if ($pi_config && defined $listname && length $listname) {
205 foreach my $key (qw(address)) {
206 $rv{$key} = $pi_config->get($listname, $key) || "";
211 my $path_info = $cgi->path_info;
213 if (ref($cgi) eq 'CGI') {
214 $base = $cgi->url(-base);
216 $base = $cgi->base->as_string;
219 $url_base = $path_info;
220 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
221 $rv{atomurl} = "$base$url_base/atom.xml";
223 $url_base =~ s!/atom\.xml\z!!;
224 $rv{atomurl} = $base . $path_info;
225 $url_base = $base . $url_base; # XXX is this needed?
228 $url_base = "http://example.com";
229 $rv{atomurl} = "$url_base/atom.xml";
231 $rv{url} ||= "$url_base/";
232 $rv{midurl} = "$url_base/m/";
233 $rv{fullurl} = "$url_base/f/";
239 my ($mime, $name) = @_;
240 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
245 my @t = eval { strptime($date) };
247 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
250 # returns 0 (skipped) or 1 (added)
252 my ($feed_opts, $feed, $add, $git) = @_;
254 my $mime = do_cat_mail($git, $add) or return 0;
255 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
256 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
258 my $mid = $mime->header_obj->header_raw('Message-ID');
259 defined $mid or return 0;
260 $mid = PublicInbox::Hval->new_msgid($mid);
261 my $href = $mid->as_href . '.html';
262 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
263 defined($content) or return 0;
265 my $subject = mime_header($mime, 'Subject') or return 0;
267 my $from = mime_header($mime, 'From') or return 0;
268 my @from = Email::Address->parse($from);
269 my $name = $from[0]->name;
270 defined $name or $name = "";
271 my $email = $from[0]->address;
272 defined $email or $email = "";
274 my $date = $mime->header('Date');
275 $date = PublicInbox::Hval->new_oneline($date);
276 $date = feed_date($date->raw) or return 0;
279 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
282 author => { name => $name, email => $email },
285 content => { type => 'xhtml', content => $content },
286 link => $midurl . $href,
287 id => 'urn:uuid:' . join('-', @uuid5),
293 my ($self, $level, $html, $state) = @_;
294 my $mime = $self->message;
296 $$html .= PublicInbox::View->index_entry($mime, $level, $state);
298 dump_msg($self->child, $level+1, $html, $state) if $self->child;
299 dump_msg($self->next, $level, $html, $state) if $self->next;
303 my ($git, $path) = @_;
305 my $str = $git->cat_file("HEAD:$path");
306 Email::MIME->new($str);