-# 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;
- }
-
- if ($pi_config && defined $listname && length $listname) {
- foreach my $key (qw(address)) {
- $rv{$key} = $pi_config->get($listname, $key) || "";
- }
- }
- my $url_base;
- if ($cgi) {
- my $path_info = $cgi->path_info;
- my $base;
- if (ref($cgi) eq 'CGI') {
- $base = $cgi->url(-base);
- } else {
- $base = $cgi->base->as_string;
- $base =~ s!/\z!!;
- }
- $url_base = $path_info;
- if ($url_base =~ s!/(?:|index\.html)?\z!!) {
- $rv{atomurl} = "$base$url_base/atom.xml";
- } else {
- $url_base =~ s!/atom\.xml\z!!;
- $rv{atomurl} = $base . $path_info;
- $url_base = $base . $url_base; # XXX is this needed?
- }
- } else {
- $url_base = "http://example.com";
- $rv{atomurl} = "$url_base/atom.xml";
- }
- $rv{url} ||= "$url_base/";
- $rv{midurl} = "$url_base/m/";
- $rv{fullurl} = "$url_base/f/";
-
- \%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) ? POSIX::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 $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
-
- 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 . '.html';
- my $content = PublicInbox::View->feed_entry($mime, $fullurl . $href);
- defined($content) or return 0;
- $mime = undef;
-
- my $title = mime_header($header_obj, 'Subject') or return 0;
- $title = PublicInbox::Hval->new_oneline($title)->as_html;
- my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
-
- 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;
-
- my $date = $header_obj->header('Date');
- $date = PublicInbox::Hval->new_oneline($date);
- $date = feed_date($date->raw) or return 0;
-
- $fh->write("<entry><author><name>$name</name><email>$email</email>" .
- "</author><title$type>$title</title>" .
- "<updated>$date</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);
- my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
- $fh->write(qq{</div></content><link\nhref="$midurl$href"/>}.
- "<id>$id</id></entry>");
- 1;
-}
-