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 = PublicInbox::GitCatFile->new($args->{git_dir});
62 my ($first, $last) = each_recent_blob($args, sub {
63 my $mime = do_cat_mail($git, $_[0]) or return 0;
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;
71 $git = undef; # destroy pipes.
73 my $th = PublicInbox::Thread->new(@messages);
75 my $html = "<html><head><title>$title</title>" .
76 '<link rel="alternate" title="Atom feed"' . "\nhref=\"" .
77 $feed_opts->{atomurl} . "\"\ntype=\"application/atom+xml\"/>" .
78 '</head><body>' . PRE_WRAP;
80 # sort child messages in chronological order
83 $a->topmost->message->header('X-PI-Date') <=>
84 $b->topmost->message->header('X-PI-Date')
89 # except we sort top-level messages reverse chronologically
90 for (sort { (eval { $b->message->header('X-PI-Date') } || 0) <=>
91 (eval { $a->message->header('X-PI-Date') } || 0)
93 dump_msg($_, 0, \$html, time, \%seen);
96 Email::Address->purge_cache;
98 my $footer = nav_footer($args->{cgi}, $first, $last, $feed_opts);
99 my $list_footer = $args->{footer};
100 $footer .= "\n" . $list_footer if ($footer && $list_footer);
101 $footer = "<hr />" . PRE_WRAP . "$footer</pre>" if $footer;
102 $html . "</pre>$footer</html>";
108 my ($cgi, $first, $last, $feed_opts) = @_;
110 my $old_r = $cgi->param('r');
115 $next = qq!<a\nhref="?r=$last">next</a>!;
118 $head = $cgi->path_info;
119 $head = qq!<a\nhref="$head">head</a>!;
121 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
122 my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
123 "$next $head $atom $permalink";
126 sub each_recent_blob {
127 my ($args, $cb) = @_;
128 my $max = $args->{max} || MAX_PER_PAGE;
129 my $hex = '[a-f0-9]';
130 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
131 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
132 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
133 my $cgi = $args->{cgi};
135 # revision ranges may be specified
137 my $r = $cgi->param('r') if $cgi;
138 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
142 # get recent messages
143 # we could use git log -z, but, we already know ssoma will not
144 # leave us with filenames with spaces in them..
145 my @cmd = ('git', "--git-dir=$args->{git_dir}",
146 qw/log --no-notes --no-color --raw -r/);
149 my $pid = open(my $log, '-|', @cmd) or
150 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
151 my %deleted; # only an optimization at this point
155 while (my $line = <$log>) {
156 if ($line =~ /$addmsg/o) {
158 next if $deleted{$add};
164 } elsif ($line =~ /$delmsg/o) {
166 } elsif ($line =~ /^commit (${hex}{40})/) {
172 while (my $line = <$log>) {
173 if ($line =~ /^commit (${hex}{40})/) {
179 push @commits, undef;
182 close $log; # we may EPIPE here
184 ($commits[0], $commits[-1]);
187 # private functions below
190 my $pi_config = $args->{pi_config};
191 my $listname = $args->{listname};
192 my $cgi = $args->{cgi};
194 if (open my $fh, '<', "$args->{git_dir}/description") {
195 chomp($rv{description} = <$fh>);
199 if ($pi_config && defined $listname && length $listname) {
200 foreach my $key (qw(address)) {
201 $rv{$key} = $pi_config->get($listname, $key) || "";
206 my $path_info = $cgi->path_info;
208 if (ref($cgi) eq 'CGI') {
209 $base = $cgi->url(-base);
211 $base = $cgi->base->as_string;
214 $url_base = $path_info;
215 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
216 $rv{atomurl} = "$base$url_base/atom.xml";
218 $url_base =~ s!/atom\.xml\z!!;
219 $rv{atomurl} = $base . $path_info;
220 $url_base = $base . $url_base; # XXX is this needed?
223 $url_base = "http://example.com";
224 $rv{atomurl} = "$url_base/atom.xml";
226 $rv{url} ||= "$url_base/";
227 $rv{midurl} = "$url_base/m/";
228 $rv{fullurl} = "$url_base/f/";
234 my ($mime, $name) = @_;
235 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
240 my @t = eval { strptime($date) };
242 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
245 # returns 0 (skipped) or 1 (added)
247 my ($feed_opts, $feed, $add, $git) = @_;
249 my $mime = do_cat_mail($git, $add) or return 0;
250 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
251 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
253 my $mid = $mime->header_obj->header_raw('Message-ID');
254 defined $mid or return 0;
255 $mid = PublicInbox::Hval->new_msgid($mid);
256 my $href = $mid->as_href . '.html';
257 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
258 defined($content) or return 0;
260 my $subject = mime_header($mime, 'Subject') or return 0;
262 my $from = mime_header($mime, 'From') or return 0;
263 my @from = Email::Address->parse($from);
264 my $name = $from[0]->name;
265 defined $name or $name = "";
266 my $email = $from[0]->address;
267 defined $email or $email = "";
269 my $date = $mime->header('Date');
270 $date = PublicInbox::Hval->new_oneline($date);
271 $date = feed_date($date->raw) or return 0;
274 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
277 author => { name => $name, email => $email },
280 content => { type => 'xhtml', content => $content },
281 link => $midurl . $href,
282 id => 'urn:uuid:' . join('-', @uuid5),
288 my ($self, $level, $html, $now, $seen) = @_;
289 my $mime = $self->message;
292 PublicInbox::View->index_entry($mime, $now, $level, $seen);
294 dump_msg($self->child, $level+1, $html, $now, $seen) if $self->child;
295 dump_msg($self->next, $level, $html, $now, $seen) if $self->next;
299 my ($git, $path) = @_;
301 my $str = $git->cat_file("HEAD:$path");
302 Email::MIME->new($str);