+sub end_feed {
+ my ($fh) = @_;
+ Email::Address->purge_cache;
+ $fh->write('</feed>');
+ $fh->close;
+}
+
+sub emit_atom_thread {
+ my ($cb, $ctx) = @_;
+ my $res = $ctx->{srch}->get_thread($ctx->{mid});
+ return _no_thread($cb) unless $res->{total};
+ my $fh = $cb->([200, ['Content-Type' => 'application/atom+xml']]);
+ my $feed_opts = get_feedopts($ctx);
+
+ my $html_url = $feed_opts->{atomurl} = $ctx->{self_url};
+ $html_url =~ s!/t\.atom\z!/!;
+ $feed_opts->{url} = $html_url;
+ $feed_opts->{emit_header} = 1;
+
+ my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
+ foreach my $msg (@{$res->{msgs}}) {
+ add_to_feed($feed_opts, $fh, mid2path($msg->mid), $git);
+ }
+ end_feed($fh);
+}
+
+sub emit_html_index {
+ my ($cb, $ctx) = @_;
+ my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
+
+ my $max = $ctx->{max} || MAX_PER_PAGE;
+ my $feed_opts = get_feedopts($ctx);
+
+ my $title = $feed_opts->{description} || '';
+ my ($footer, $param, $last);
+ my $state = { ctx => $ctx, seen => {}, anchor_idx => 0 };
+ my $srch = $ctx->{srch};
+
+ my $top = "<b>$title</b> (<a\nhref=\"new.atom\">Atom feed</a>)";
+
+ if ($srch) {
+ $top = qq{<form\naction=""><pre>$top} .
+ qq{ <input\nname=q\ntype=text />} .
+ qq{<input\ntype=submit\nvalue=search />} .
+ q{</pre></form><pre>}
+ } else {
+ $top = '<pre>' . $top . "\n";
+ }
+
+ $fh->write("<html><head><title>$title</title>" .
+ "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
+ "href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
+ PublicInbox::Hval::STYLE .
+ "</head><body>$top");
+
+ # if the 'r' query parameter is given, it is a legacy permalink
+ # which we must continue supporting:
+ my $cgi = $ctx->{cgi};
+ if ($cgi && !$cgi->param('r') && $srch) {
+ $state->{srch} = $srch;
+ $last = PublicInbox::View::emit_index_topics($state, $fh);
+ $param = 'o';
+ } else {
+ $last = emit_index_nosrch($ctx, $state, $fh);
+ $param = 'r';
+ }
+ $footer = nav_footer($cgi, $last, $feed_opts, $state, $param);
+ if ($footer) {
+ my $list_footer = $ctx->{footer};
+ $footer .= "\n\n" . $list_footer if $list_footer;
+ $footer = "<hr /><pre>$footer</pre>";
+ }
+ $fh->write("$footer</body></html>");
+ $fh->close;
+}
+
+sub emit_index_nosrch {
+ my ($ctx, $state, $fh) = @_;
+ my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
+ my (undef, $last) = each_recent_blob($ctx, sub {
+ my ($path, $commit, $ts, $u, $subj) = @_;
+ $state->{first} ||= $commit;
+
+ my $mime = do_cat_mail($git, $path) or return 0;
+ PublicInbox::View::index_entry($fh, $mime, 0, $state);
+ 1;
+ });
+ Email::Address->purge_cache;
+ $last;
+}