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 style="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 $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" href="' .
77 $feed_opts->{atomurl} . '" type="application/atom+xml"/>' .
78 '</head><body>' . PRE_WRAP;
80 # sort by date, most recent at top
83 $b->topmost->message->header('X-PI-Date') <=>
84 $a->topmost->message->header('X-PI-Date')
88 dump_msg($_, 0, \$html, time, \%seen) for $th->rootset;
90 Email::Address->purge_cache;
92 my $footer = nav_footer($args->{cgi}, $last, $feed_opts);
93 my $list_footer = $args->{footer};
94 $footer .= "\n" . $list_footer if ($footer && $list_footer);
95 $footer = "<hr />" . PRE_WRAP . "$footer</pre>" if $footer;
96 $html . "</pre>$footer</html>";
102 my ($cgi, $last, $feed_opts) = @_;
104 my $old_r = $cgi->param('r');
109 $next = qq!<a href="?r=$last">next</a>!;
112 $head = $cgi->path_info;
113 $head = qq!<a href="$head">head</a>!;
115 my $atom = "<a href=\"$feed_opts->{atomurl}\">atom</a>";
119 sub each_recent_blob {
120 my ($args, $cb) = @_;
121 my $max = $args->{max} || MAX_PER_PAGE;
122 my $hex = '[a-f0-9]';
123 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
124 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
125 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
126 my $cgi = $args->{cgi};
128 # revision ranges may be specified
130 my $r = $cgi->param('r') if $cgi;
131 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
135 # get recent messages
136 # we could use git log -z, but, we already know ssoma will not
137 # leave us with filenames with spaces in them..
138 my @cmd = ('git', "--git-dir=$args->{git_dir}",
139 qw/log --no-notes --no-color --raw -r/);
142 my $pid = open(my $log, '-|', @cmd) or
143 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
144 my %deleted; # only an optimization at this point
148 while (my $line = <$log>) {
149 if ($line =~ /$addmsg/o) {
151 next if $deleted{$add};
157 } elsif ($line =~ /$delmsg/o) {
159 } elsif ($line =~ /^commit (${hex}{40})/) {
165 while (my $line = <$log>) {
166 if ($line =~ /^commit (${hex}{40})/) {
172 push @commits, undef;
175 close $log; # we may EPIPE here
180 # private functions below
183 my $pi_config = $args->{pi_config};
184 my $listname = $args->{listname};
185 my $cgi = $args->{cgi};
187 if (open my $fh, '<', "$args->{git_dir}/description") {
188 chomp($rv{description} = <$fh>);
192 if ($pi_config && defined $listname && length $listname) {
193 foreach my $key (qw(address)) {
194 $rv{$key} = $pi_config->get($listname, $key) || "";
199 my $path_info = $cgi->path_info;
201 if (ref($cgi) eq 'CGI') {
202 $base = $cgi->url(-base);
204 $base = $cgi->base->as_string;
207 $url_base = $path_info;
208 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
209 $rv{atomurl} = "$base$url_base/atom.xml";
211 $url_base =~ s!/atom\.xml\z!!;
212 $rv{atomurl} = $base . $path_info;
213 $url_base = $base . $url_base; # XXX is this needed?
216 $url_base = "http://example.com";
217 $rv{atomurl} = "$url_base/atom.xml";
219 $rv{url} ||= "$url_base/";
220 $rv{midurl} = "$url_base/m/";
221 $rv{fullurl} = "$url_base/f/";
227 my ($mime, $name) = @_;
228 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
233 my @t = eval { strptime($date) };
235 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
238 # returns 0 (skipped) or 1 (added)
240 my ($feed_opts, $feed, $add, $git) = @_;
242 my $mime = do_cat_mail($git, $add) or return 0;
243 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
244 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
246 my $mid = $mime->header_obj->header_raw('Message-ID');
247 defined $mid or return 0;
248 $mid = PublicInbox::Hval->new_msgid($mid);
249 my $href = $mid->as_href . '.html';
250 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
251 defined($content) or return 0;
253 my $subject = mime_header($mime, 'Subject') or return 0;
255 my $from = mime_header($mime, 'From') or return 0;
256 my @from = Email::Address->parse($from);
257 my $name = $from[0]->name;
258 defined $name or $name = "";
259 my $email = $from[0]->address;
260 defined $email or $email = "";
262 my $date = $mime->header('Date');
263 $date = PublicInbox::Hval->new_oneline($date);
264 $date = feed_date($date->raw) or return 0;
267 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
270 author => { name => $name, email => $email },
273 content => { type => 'xhtml', content => $content },
274 link => $midurl . $href,
275 id => 'urn:uuid:' . join('-', @uuid5),
281 my ($self, $level, $html, $now, $seen) = @_;
282 if ($self->message) {
283 my $mime = $self->message;
285 PublicInbox::View->index_entry($mime, $now, $level, $seen);
287 dump_msg($self->child, $level+1, $html, $now, $seen) if $self->child;
288 dump_msg($self->next, $level, $html, $now, $seen) if $self->next;
292 my ($git, $path) = @_;
293 my $str = $git->cat_file("HEAD:$path");
294 Email::MIME->new($str);