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);
10 use PublicInbox::GitCatFile;
11 use PublicInbox::View;
12 use PublicInbox::MID qw/mid_clean mid_compressed/;
14 DATEFMT => '%Y-%m-%dT%H:%M:%SZ', # atom standard
15 MAX_PER_PAGE => 25, # this needs to be tunable
18 use Encode qw/find_encoding/;
19 my $enc_utf8 = find_encoding('UTF-8');
23 my ($class, $ctx) = @_;
24 require XML::Atom::SimpleFeed;
26 my $max = $ctx->{max} || MAX_PER_PAGE;
28 my $feed_opts = get_feedopts($ctx);
29 my $addr = $feed_opts->{address};
30 $addr = $addr->[0] if ref($addr);
31 my $feed = XML::Atom::SimpleFeed->new(
32 title => $feed_opts->{description} || "unnamed feed",
33 link => $feed_opts->{url} || "http://example.com/",
36 href => $feed_opts->{atomurl} ||
37 "http://example.com/atom.xml",
39 id => 'mailto:' . ($addr || 'public-inbox@example.com'),
40 updated => POSIX::strftime(DATEFMT, gmtime),
44 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
45 each_recent_blob($ctx, sub {
46 my ($add, undef) = @_;
47 add_to_feed($feed_opts, $feed, $add, $git);
49 $git = undef; # destroy pipes
50 Email::Address->purge_cache;
54 sub generate_html_index {
55 my ($class, $ctx) = @_;
57 my $max = $ctx->{max} || MAX_PER_PAGE;
58 my $feed_opts = get_feedopts($ctx);
60 my $title = $feed_opts->{description} || '';
61 $title = PublicInbox::Hval->new_oneline($title)->as_html;
62 my $atom_url = $feed_opts->{atomurl};
64 my $html = "<html><head><title>$title</title>" .
65 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
66 "href=\"$atom_url\"\ntype=\"application/atom+xml\"/>" .
67 '</head><body>' . PublicInbox::View::PRE_WRAP .
68 "<b>$title</b> (<a\nhref=\"$atom_url\">Atom feed</a>)\n";
71 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
73 my $srch = $ctx->{srch};
74 $srch and $topics = [ [], {} ];
75 my (undef, $last) = each_recent_blob($ctx, sub {
76 my ($path, $commit, $ts, $u, $subj) = @_;
77 $state ||= [ undef, {}, $commit, 0 ];
80 add_topic($git, $srch, $topics, $path, $ts, $u, $subj);
82 my $mime = do_cat_mail($git, $path) or return 0;
84 PublicInbox::View->index_entry($mime, 0, $state);
88 Email::Address->purge_cache;
89 $git = undef; # destroy pipes.
91 my $footer = nav_footer($ctx->{cgi}, $last, $feed_opts, $state);
93 my $list_footer = $ctx->{footer};
94 $footer .= "\n" . $list_footer if $list_footer;
95 $footer = "<hr /><pre>$footer</pre>";
97 dump_topics(\$html, $topics) if $topics;
98 $html .= "$footer</body></html>";
104 my ($cgi, $last, $feed_opts, $state) = @_;
106 my $old_r = $cgi->param('r');
109 # $state = [ undef, {}, $first_commit, $last_anchor ];
110 my $first = $state->[2];
111 my $anchor = $state->[3];
114 $next = qq!<a\nhref="?r=$last">next</a>!;
117 $head = $cgi->path_info;
118 $head = qq!<a\nhref="$head">head</a>!;
120 my $atom = "<a\nhref=\"$feed_opts->{atomurl}\">atom</a>";
121 my $permalink = "<a\nhref=\"?r=$first\">permalink</a>";
122 "<a\nname=\"s$anchor\">page:</a> $next $head $atom $permalink";
125 sub each_recent_blob {
127 my $max = $ctx->{max} || MAX_PER_PAGE;
128 my $hex = '[a-f0-9]';
129 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
130 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
131 my $refhex = qr/${hex}{4,40}(?:~\d+)?/;
132 my $cgi = $ctx->{cgi};
134 # revision ranges may be specified
136 my $r = $cgi->param('r') if $cgi;
137 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
141 # get recent messages
142 # we could use git log -z, but, we already know ssoma will not
143 # leave us with filenames with spaces in them..
144 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
145 qw/log --no-notes --no-color --raw -r
146 --abbrev=16 --abbrev-commit/,
147 "--format=%h%x00%ct%x00%an%x00%s%x00");
150 my $pid = open(my $log, '-|', @cmd) or
151 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
152 my %deleted; # only an optimization at this point
155 my ($cur_commit, $first_commit, $last_commit);
157 while (defined(my $line = <$log>)) {
158 if ($line =~ /$addmsg/o) {
160 next if $deleted{$add}; # optimization-only
161 $nr += $cb->($add, $cur_commit, $ts, $u, $subj);
166 } elsif ($line =~ /$delmsg/o) {
168 } elsif ($line =~ /^${hex}{7,40}/o) {
169 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
170 unless (defined $first_commit) {
171 $first_commit = $cur_commit;
177 while (my $line = <$log>) {
178 if ($line =~ /^(${hex}{7,40})/o) {
185 close $log; # we may EPIPE here
187 ($first_commit, $last_commit);
190 # private functions below
193 my $pi_config = $ctx->{pi_config};
194 my $listname = $ctx->{listname};
195 my $cgi = $ctx->{cgi};
197 if (open my $fh, '<', "$ctx->{git_dir}/description") {
198 chomp($rv{description} = <$fh>);
202 if ($pi_config && defined $listname && length $listname) {
203 foreach my $key (qw(address)) {
204 $rv{$key} = $pi_config->get($listname, $key) || "";
209 my $path_info = $cgi->path_info;
211 if (ref($cgi) eq 'CGI') {
212 $base = $cgi->url(-base);
214 $base = $cgi->base->as_string;
217 $url_base = $path_info;
218 if ($url_base =~ s!/(?:|index\.html)?\z!!) {
219 $rv{atomurl} = "$base$url_base/atom.xml";
221 $url_base =~ s!/atom\.xml\z!!;
222 $rv{atomurl} = $base . $path_info;
223 $url_base = $base . $url_base; # XXX is this needed?
226 $url_base = "http://example.com";
227 $rv{atomurl} = "$url_base/atom.xml";
229 $rv{url} ||= "$url_base/";
230 $rv{midurl} = "$url_base/m/";
231 $rv{fullurl} = "$url_base/f/";
237 my ($mime, $name) = @_;
238 PublicInbox::Hval->new_oneline($mime->header($name))->raw;
243 my @t = eval { strptime($date) };
245 scalar(@t) ? POSIX::strftime(DATEFMT, @t) : 0;
248 # returns 0 (skipped) or 1 (added)
250 my ($feed_opts, $feed, $add, $git) = @_;
252 my $mime = do_cat_mail($git, $add) or return 0;
253 my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
254 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
256 my $header_obj = $mime->header_obj;
257 my $mid = $header_obj->header('Message-ID');
258 defined $mid or return 0;
259 $mid = PublicInbox::Hval->new_msgid($mid);
260 my $href = $mid->as_href . '.html';
261 my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
262 defined($content) or return 0;
265 my $subject = mime_header($header_obj, 'Subject') or return 0;
267 my $from = mime_header($header_obj, '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 = $header_obj->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 ($git, $path) = @_;
295 my $str = $git->cat_file("HEAD:$path");
296 Email::MIME->new($str);
301 # accumulate recent topics if search is supported
303 my ($git, $srch, $topics, $path, $ts, $u, $subj) = @_;
304 my ($order, $subjs) = @$topics;
307 # legacy ssoma did not set commit titles based on Subject
308 $subj = $enc_utf8->decode($subj);
309 if ($subj eq 'mda') {
310 my $mime = do_cat_mail($git, $path) or return 0;
311 $header_obj = $mime->header_obj;
312 $subj = mime_header($header_obj, 'Subject');
315 $subj = $srch->subject_normalized($subj);
316 if (++$subjs->{$subj} == 1) {
317 unless ($header_obj) {
318 my $mime = do_cat_mail($git, $path) or return 0;
319 $header_obj = $mime->header_obj;
321 my $mid = $header_obj->header('Message-ID');
322 $mid = mid_compressed(mid_clean($mid));
323 $u = $enc_utf8->decode($u);
324 push @$order, [ $mid, $ts, $u, $subj ];
327 0; # old topic, continue going
331 my ($dst, $topics) = @_;
332 my ($order, $subjs) = @$topics;
333 $$dst .= "\n[No recent topics]" unless (scalar @$order);
334 while (defined(my $info = shift @$order)) {
335 my ($mid, $ts, $u, $subj) = @$info;
336 my $n = delete $subjs->{$subj};
337 $mid = PublicInbox::Hval->new($mid)->as_href;
338 $subj = PublicInbox::Hval->new($subj)->as_html;
339 $u = PublicInbox::Hval->new($u)->as_html;
340 $$dst .= "\n<a\nhref=\"t/$mid.html#u\"><b>$subj</b></a>\n- ";
341 $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
343 $$dst .= "created by $u @ $ts UTC\n"
345 # $n isn't the total number of posts on the topic,
346 # just the number of posts in the current "git log"
347 # window, so leave it unlabeled
348 $$dst .= "updated by $u @ $ts UTC ($n)\n"