- close $log; # we may EPIPE here
- # for pagination
- ($first_commit, $last_commit);
-}
-
-# private functions below
-sub get_feedopts {
- my ($ctx) = @_;
- my $pi_config = $ctx->{pi_config};
- my $listname = $ctx->{listname};
- my $cgi = $ctx->{cgi};
- my %rv;
- if (open my $fh, '<', "$ctx->{git_dir}/description") {
- chomp($rv{description} = <$fh>);
- close $fh;
- } else {
- $rv{description} = '($GIT_DIR/description missing)';
- }
-
- if ($pi_config && defined $listname && $listname ne '') {
- my $addr = $pi_config->get($listname, 'address') || "";
- $rv{address} = $addr;
- $addr = $addr->[0] if ref($addr);
- $rv{id_addr} = $addr;
- }
- $rv{id_addr} ||= 'public-inbox@example.com';
-
- my $url_base;
- if ($cgi) {
- my $base;
- if (ref($cgi) eq 'CGI') {
- $base = $cgi->url(-base);
- } else {
- $base = $cgi->base->as_string;
- $base =~ s!/\z!!;
- }
- $url_base = "$base/$listname";
- if (my $mid = $ctx->{mid}) { # per-thread feed:
- $rv{atomurl} = "$url_base/$mid/t.atom";
- } else {
- $rv{atomurl} = "$url_base/new.atom";
- }
- } else {
- $url_base = "http://example.com";
- $rv{atomurl} = "$url_base/new.atom";
- }
- $rv{url} ||= "$url_base/";
- $rv{midurl} = "$url_base/";
-
- \%rv;
-}
-
-sub mime_header {
- my ($mime, $name) = @_;
- PublicInbox::Hval->new_oneline($mime->header($name))->raw;
-}
-
-sub feed_date {
- my ($date) = @_;
- my @t = eval { strptime($date) };
-
- scalar(@t) ? strftime(DATEFMT, @t) : 0;
-}
-
-# returns 0 (skipped) or 1 (added)
-sub add_to_feed {
- my ($feed_opts, $fh, $add, $git) = @_;
-
- my $mime = do_cat_mail($git, $add) or return 0;
- my $url = $feed_opts->{url};
- my $midurl = $feed_opts->{midurl};
-
- my $header_obj = $mime->header_obj;
- my $mid = $header_obj->header('Message-ID');
- defined $mid or return 0;
- $mid = PublicInbox::Hval->new_msgid($mid);
- my $href = $mid->as_href;
- my $content = PublicInbox::View->feed_entry($mime, "$midurl$href/f/");
- defined($content) or return 0;
- $mime = undef;
-
- my $date = $header_obj->header('Date');
- $date = PublicInbox::Hval->new_oneline($date);
- $date = feed_date($date->raw) or return 0;
- $date = "<updated>$date</updated>";
-
- my $title = mime_header($header_obj, 'Subject') or return 0;
- $title = title_tag($title);
-
- my $from = mime_header($header_obj, 'From') or return 0;
- my @from = Email::Address->parse($from) or return 0;
- my $name = PublicInbox::Hval->new_oneline($from[0]->name)->as_html;
- my $email = $from[0]->address;
- $email = PublicInbox::Hval->new_oneline($email)->as_html;
-
- if (delete $feed_opts->{emit_header}) {
- $fh->write(atom_header($feed_opts, $title) . $date);
- }
- $fh->write("<entry><author><name>$name</name><email>$email</email>" .
- "</author>$title$date" .
- qq{<content\ntype="xhtml">} .
- qq{<div\nxmlns="http://www.w3.org/1999/xhtml">});
- $fh->write($content);
-
- $add =~ tr!/!!d;
- my $h = '[a-f0-9]';
- my (@uuid5) = ($add =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
- my $id = 'urn:uuid:' . join('-', @uuid5);
- $fh->write(qq!</div></content><link\nhref="$midurl$href/"/>!.
- "<id>$id</id></entry>");
- 1;
-}
-
-sub do_cat_mail {
- my ($git, $path) = @_;
- my $mime = eval {
- my $str = $git->cat_file("HEAD:$path");
- Email::MIME->new($str);
- };
- $@ ? undef : $mime;
-}
-
-# accumulate recent topics if search is supported
-sub add_topic {
- my ($git, $srch, $topics, $path, $ts, $u, $subj) = @_;
- my ($order, $subjs) = @$topics;
- my $header_obj;
-
- # legacy ssoma did not set commit titles based on Subject
- $subj = $enc_utf8->decode($subj);
- if ($subj eq 'mda') {
- my $mime = do_cat_mail($git, $path) or return 0;
- $header_obj = $mime->header_obj;
- $subj = mime_header($header_obj, 'Subject');
- }
-
- my $topic = $subj = $srch->subject_normalized($subj);
-
- # kill "[PATCH v2]" etc. for summarization
- $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
-
- if (++$subjs->{$topic} == 1) {
- unless ($header_obj) {
- my $mime = do_cat_mail($git, $path) or return 0;
- $header_obj = $mime->header_obj;
- }
- my $mid = $header_obj->header('Message-ID');
- $mid = mid_compress(mid_clean($mid));
- $u = $enc_utf8->decode($u);
- push @$order, [ $mid, $ts, $u, $subj, $topic ];
- return 1;
- }
- 0; # old topic, continue going
-}
-
-sub dump_topics {
- my ($topics) = @_;
- my ($order, $subjs) = @$topics;
- my $dst = '';
- $dst .= "\n[No recent topics]" unless (scalar @$order);
- while (defined(my $info = shift @$order)) {
- my ($mid, $ts, $u, $subj, $topic) = @$info;
- my $n = delete $subjs->{$topic};
- $mid = PublicInbox::Hval->new($mid)->as_href;
- $subj = PublicInbox::Hval->new($subj)->as_html;
- $u = PublicInbox::Hval->new($u)->as_html;
- $dst .= "\n<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n- ";
- $ts = strftime('%Y-%m-%d %H:%M', gmtime($ts));
- if ($n == 1) {
- $dst .= "created by $u @ $ts UTC\n"
- } else {
- # $n isn't the total number of posts on the topic,
- # just the number of posts in the current "git log"
- # window, so leave it unlabeled
- $dst .= "updated by $u @ $ts UTC ($n)\n"
- }
- }
- $dst .= '</pre>'