- \%rv;
-}
-
-sub utf8_header {
- my ($simple, $name) = @_;
- my $val = $simple->header($name);
- return "" unless defined $val;
- $val =~ tr/\t\n / /s;
- $val =~ tr/\r//d;
- $enc_utf8->encode($enc_mime->decode($val));
-}
-
-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, $feed, $add, $top, $git) = @_;
-
- my $mime = do_cat_mail($git, 'Email::MIME', $add) or return 0;
- if ($top && $mime->header("In-Reply-To")) {
- return 0;
- }
-
- my $midurl = $feed_opts->{midurl} || 'http://example.com/m/';
- my $fullurl = $feed_opts->{fullurl} || 'http://example.com/f/';
-
- my $mid = $mime->header('Message-ID');
- $mid = PublicInbox::Hval->new_msgid($mid);
- my $href = $mid->as_href;
- my $content = PublicInbox::View->as_feed_entry($mime,
- "$fullurl$href.html");
- defined($content) or return 0;
-
- my $subject = utf8_header($mime, "Subject") || "";
- length($subject) or return 0;
-
- my $from = $mime->header('From') or return 0;
-
-
- my @from = Email::Address->parse($from);
- my $name = $from[0]->name;
- defined $name or $name = "";
- my $email = $from[0]->address;
- defined $email or $email = "";
-
- my $date = $mime->header('Date');
- $date or return 0;
- $date = PublicInbox::Hval->new_oneline($date);
- $date = feed_date($date->as_utf8) or return 0;
- $feed->add_entry(
- author => { name => $name, email => $email },
- title => $subject,
- updated => $date,
- content => { type => "html", content => $content },
- link => $midurl . $href,
- id => $add,
- );
- 1;
-}
-
-sub dump_html_line {
- my ($self, $level, $args) = @_; # args => [ $html, $midurl ]
- if ($self->message) {
- $args->[0] .= (' ' x $level);
- my $simple = $self->message;
- my $subj = $simple->header('Subject');
- my $mid = $simple->header('Message-ID');
- $mid = PublicInbox::Hval->new_msgid($mid);
- my $url = $args->[1] . $mid->as_href;
- my $from = utf8_header($simple, "From");
- my @from = Email::Address->parse($from);
- $from = $from[0]->name;
- (defined($from) && length($from)) or $from = $from[0]->address;
- $from = xs_html($from);
- $subj = PublicInbox::Hval->new_oneline($subj);
- $subj = $subj->as_html;
- $args->[0] .= "<a href=\"$url.html\">$subj</a> $from\n";
- }
- dump_html_line($self->child, $level+1, $args) if $self->child;
- dump_html_line($self->next, $level, $args) if $self->next;
-}
-
-sub xs_html {
- $enc_ascii->encode(escapeHTML($enc_utf8->decode($_[0])),
- Encode::HTMLCREF);
-}
-
-sub try_git_pm {
- my ($dir) = @_;
- eval { Git->repository(Directory => $dir) };
-};
-
-sub do_cat_mail {
- my ($git, $class, $sha1) = @_;
- my $str;
- if ($git) {
- open my $fh, '>', \$str or
- die "failed to setup string handle: $!\n";
- binmode $fh;
- my $bytes = $git->cat_blob($sha1, $fh);
- close $fh or die "failed to close string handle: $!\n";
- return if $bytes <= 0;
- } else {
- $str = `git cat-file blob $sha1`;
- return if $? != 0 || length($str) == 0;
- }
- $class->new($str);