- # 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>);
- } 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) {
- $url_base = $cgi->base->as_string . $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 feed_updated {
- my ($date, $ts) = @_;
- my @t = eval { strptime($date) } if defined $date;
- @t = gmtime($ts || time) unless scalar @t;
-
- '<updated>' . strftime(DATEFMT, @t) . '</updated>';
-}
-
-# 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_raw('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);
- defined($content) or return 0;
- $mime = undef;
-
- my $date = $header_obj->header('Date');
- my $updated = feed_updated($date);
-
- my $title = $header_obj->header('Subject');
- defined $title or return 0;
- $title = title_tag($title);
-
- my $from = $header_obj->header('From') or return 0;
- my @from = Email::Address->parse($from) or return 0;
- my $name = ascii_html($from[0]->name);
- my $email = $from[0]->address;
- $email = ascii_html($email);
-
- if (delete $feed_opts->{emit_header}) {
- $fh->write(atom_header($feed_opts, $title) . $updated);
- }
- $fh->write("<entry><author><name>$name</name><email>$email</email>" .
- "</author>$title$updated" .
- 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;