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', # atom standard
13 MAX_PER_PAGE => 25, # this needs to be tunable
18 my ($class, $args) = @_;
19 require XML::Atom::SimpleFeed;
20 require PublicInbox::View;
22 my $max = $args->{max} || MAX_PER_PAGE;
24 my $feed_opts = get_feedopts($args);
25 my $addr = $feed_opts->{address};
26 $addr = $addr->[0] if ref($addr);
27 my $feed = XML::Atom::SimpleFeed->new(
28 title => $feed_opts->{description} || "unnamed feed",
29 link => $feed_opts->{url} || "http://example.com/",
32 href => $feed_opts->{atomurl} ||
33 "http://example.com/atom.xml",
35 id => 'mailto:' . ($addr || 'public-inbox@example.com'),
36 updated => POSIX::strftime(DATEFMT, gmtime),
39 my $git = PublicInbox::GitCatFile->new($args->{git_dir});
40 each_recent_blob($args, sub {
42 add_to_feed($feed_opts, $feed, $add, $git);
44 $git = undef; # destroy pipes
45 Email::Address->purge_cache;
49 sub generate_html_index {
50 my ($class, $args) = @_;
51 require PublicInbox::Thread;
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;
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" href="' .
77 $feed_opts->{atomurl} . '" type="application/atom+xml"/>' .
80 # sort by date, most recent at top
83 $b->topmost->message->header('X-PI-Date') <=>
84 $a->topmost->message->header('X-PI-Date')
87 dump_html_line($_, 0, \$html) for $th->rootset;
89 Email::Address->purge_cache;
91 my $footer = nav_footer($args->{cgi}, $last, $feed_opts);
92 my $list_footer = $args->{footer};
93 $footer .= "\n" . $list_footer if ($footer && $list_footer);
94 $footer = "<hr /><pre>$footer</pre>" if $footer;
95 $html . "</pre>$footer</html>";
101 my ($cgi, $last, $feed_opts) = @_;
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>!;
114 my $atom = "<a href=\"$feed_opts->{atomurl}\">atom</a>";
118 sub each_recent_blob {
119 my ($args, $cb) = @_;
120 my $max = $args->{max} || MAX_PER_PAGE;
121 my $hex = '[a-f0-9]';
122 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
123 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
124 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
125 my $cgi = $args->{cgi};
127 # revision ranges may be specified
129 my $r = $cgi->param('r') if $cgi;
130 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
134 # get recent messages
135 # we could use git log -z, but, we already know ssoma will not
136 # leave us with filenames with spaces in them..
137 my @cmd = ('git', "--git-dir=$args->{git_dir}",
138 qw/log --no-notes --no-color --raw -r/);
141 my $pid = open(my $log, '-|', @cmd) or
142 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
143 my %deleted; # only an optimization at this point
147 while (my $line = <$log>) {
148 if ($line =~ /$addmsg/o) {
150 next if $deleted{$add};
156 } elsif ($line =~ /$delmsg/o) {
158 } elsif ($line =~ /^commit (${hex}{40})/) {
164 while (my $line = <$log>) {
165 if ($line =~ /^commit (${hex}{40})/) {
171 push @commits, undef;
174 close $log; # we may EPIPE here
179 # private functions below
182 my $pi_config = $args->{pi_config};
183 my $listname = $args->{listname};
184 my $cgi = $args->{cgi};
186 if (open my $fh, '<', "$args->{git_dir}/description") {
187 chomp($rv{description} = <$fh>);
191 if ($pi_config && defined $listname && length $listname) {
192 foreach my $key (qw(address)) {
193 $rv{$key} = $pi_config->get($listname, $key) || "";
198 my $path_info = $cgi->path_info;
200 if (ref($cgi) eq 'CGI') {
201 $base = $cgi->url(-base);
203 $base = $cgi->base->as_string;
206 $url_base = $path_info;
207 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
208 $rv{atomurl} = "$base$url_base/atom.xml";
210 $url_base =~ s!/atom\.xml\z!!;
211 $rv{atomurl} = $base . $path_info;
212 $url_base = $base . $url_base; # XXX is this needed?
215 $url_base = "http://example.com";
216 $rv{atomurl} = "$url_base/atom.xml";
218 $rv{url} ||= "$url_base/";
219 $rv{midurl} = "$url_base/m/";
220 $rv{fullurl} = "$url_base/f/";
226 my ($mime, $name) = @_;
227 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
232 my @t = eval { strptime($date) };
234 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
237 # returns 0 (skipped) or 1 (added)
239 my ($feed_opts, $feed, $add, $git) = @_;
241 my $mime = do_cat_mail($git, $add) or return 0;
242 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
243 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
245 my $mid = $mime->header_obj->header_raw('Message-ID');
246 defined $mid or return 0;
247 $mid = PublicInbox::Hval->new_msgid($mid);
248 my $href = $mid->as_href . '.html';
249 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
250 defined($content) or return 0;
252 my $subject = mime_header($mime, 'Subject') or return 0;
254 my $from = mime_header($mime, 'From') or return 0;
255 my @from = Email::Address->parse($from);
256 my $name = $from[0]->name;
257 defined $name or $name = "";
258 my $email = $from[0]->address;
259 defined $email or $email = "";
261 my $date = $mime->header('Date');
262 $date = PublicInbox::Hval->new_oneline($date);
263 $date = feed_date($date->raw) or return 0;
266 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
269 author => { name => $name, email => $email },
272 content => { type => 'xhtml', content => $content },
273 link => $midurl . $href,
274 id => 'urn:uuid:' . join('-', @uuid5),
280 my ($self, $level, $html) = @_;
281 if ($self->message) {
282 $$html .= (' ' x $level);
283 my $mime = $self->message;
284 my $subj = $mime->header('Subject');
285 my $mid = $mime->header_obj->header_raw('Message-ID');
286 $mid = PublicInbox::Hval->new_msgid($mid);
287 my $href = 'm/' . $mid->as_href . '.html';
288 my $from = mime_header($mime, 'From');
290 my @from = Email::Address->parse($from);
291 $from = $from[0]->name;
292 (defined($from) && length($from)) or $from = $from[0]->address;
294 $from = PublicInbox::Hval->new_oneline($from)->as_html;
295 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
296 $$html .= "<a href=\"$href\">$subj</a> $from\n";
298 dump_html_line($self->child, $level+1, $html) if $self->child;
299 dump_html_line($self->next, $level, $html) if $self->next;
303 my ($git, $path) = @_;
304 my $str = $git->cat_file("HEAD:$path");
305 Email::MIME->new($str);