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',
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);
92 $footer = "<hr /><pre>$footer</pre>" if $footer;
93 $html . "</pre>$footer</html>";
99 my ($cgi, $last) = @_;
101 my $old_r = $cgi->param('r');
106 $next = qq!<a href="?r=$last">next</a>!;
109 $head = $cgi->path_info;
110 $head = qq!<a href="$head">head</a>!;
115 sub each_recent_blob {
116 my ($args, $cb) = @_;
117 my $max = $args->{max} || MAX_PER_PAGE;
118 my $hex = '[a-f0-9]';
119 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
120 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
121 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
122 my $cgi = $args->{cgi};
124 # revision ranges may be specified
126 my $r = $cgi->param('r') if $cgi;
127 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
131 # get recent messages
132 # we could use git log -z, but, we already know ssoma will not
133 # leave us with filenames with spaces in them..
134 my @cmd = ('git', "--git-dir=$args->{git_dir}",
135 qw/log --no-notes --no-color --raw -r/);
138 my $pid = open(my $log, '-|', @cmd) or
139 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
140 my %deleted; # only an optimization at this point
144 while (my $line = <$log>) {
145 if ($line =~ /$addmsg/o) {
147 next if $deleted{$add};
153 } elsif ($line =~ /$delmsg/o) {
155 } elsif ($line =~ /^commit (${hex}{40})/) {
161 while (my $line = <$log>) {
162 if ($line =~ /^commit (${hex}{40})/) {
168 push @commits, undef;
171 close $log; # we may EPIPE here
176 # private functions below
179 my $pi_config = $args->{pi_config};
180 my $listname = $args->{listname};
181 my $cgi = $args->{cgi};
183 if (open my $fh, '<', "$args->{git_dir}/description") {
184 chomp($rv{description} = <$fh>);
188 if ($pi_config && defined $listname && length $listname) {
189 foreach my $key (qw(address)) {
190 $rv{$key} = $pi_config->get($listname, $key) || "";
195 my $path_info = $cgi->path_info;
197 if (ref($cgi) eq 'CGI') {
198 $base = $cgi->url(-base);
200 $base = $cgi->base->as_string;
203 $url_base = $path_info;
204 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
205 $rv{atomurl} = "$base$url_base/atom.xml";
207 $url_base =~ s!/atom\.xml\z!!;
208 $rv{atomurl} = $base . $path_info;
209 $url_base = $base . $url_base; # XXX is this needed?
212 $url_base = "http://example.com";
213 $rv{atomurl} = "$url_base/atom.xml";
215 $rv{url} ||= "$url_base/";
216 $rv{midurl} = "$url_base/m/";
217 $rv{fullurl} = "$url_base/f/";
223 my ($mime, $name) = @_;
224 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
229 my @t = eval { strptime($date) };
231 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
234 # returns 0 (skipped) or 1 (added)
236 my ($feed_opts, $feed, $add, $git) = @_;
238 my $mime = do_cat_mail($git, $add) or return 0;
239 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
240 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
242 my $mid = $mime->header_obj->header_raw('Message-ID');
243 defined $mid or return 0;
244 $mid = PublicInbox::Hval->new_msgid($mid);
245 my $href = $mid->as_href . '.html';
246 my $content = PublicInbox::View->as_feed_entry($mime, $fullurl . $href);
247 defined($content) or return 0;
249 my $subject = mime_header($mime, 'Subject') or return 0;
251 my $from = mime_header($mime, 'From') or return 0;
252 my @from = Email::Address->parse($from);
253 my $name = $from[0]->name;
254 defined $name or $name = "";
255 my $email = $from[0]->address;
256 defined $email or $email = "";
258 my $date = $mime->header('Date');
259 $date = PublicInbox::Hval->new_oneline($date);
260 $date = feed_date($date->raw) or return 0;
263 my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
266 author => { name => $name, email => $email },
269 content => { type => 'xhtml', content => $content },
270 link => $midurl . $href,
271 id => 'urn:uuid:' . join('-', @uuid5),
277 my ($self, $level, $html) = @_;
278 if ($self->message) {
279 $$html .= (' ' x $level);
280 my $mime = $self->message;
281 my $subj = $mime->header('Subject');
282 my $mid = $mime->header_obj->header_raw('Message-ID');
283 $mid = PublicInbox::Hval->new_msgid($mid);
284 my $href = 'm/' . $mid->as_href . '.html';
285 my $from = mime_header($mime, 'From');
287 my @from = Email::Address->parse($from);
288 $from = $from[0]->name;
289 (defined($from) && length($from)) or $from = $from[0]->address;
291 $from = PublicInbox::Hval->new_oneline($from)->as_html;
292 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
293 $$html .= "<a href=\"$href\">$subj</a> $from\n";
295 dump_html_line($self->child, $level+1, $html) if $self->child;
296 dump_html_line($self->next, $level, $html) if $self->next;
300 my ($git, $path) = @_;
301 my $str = $git->cat_file("HEAD:$path");
302 Email::MIME->new($$str);