1 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::Feed;
6 use XML::Atom::SimpleFeed;
9 use URI::Escape qw/uri_escape/;
10 use Encode qw/encode decode/;
11 use Encode::MIME::Header;
12 use DateTime::Format::Mail;
13 use CGI qw(escapeHTML);
14 use POSIX qw(strftime);
15 use constant DATEFMT => '%Y-%m-%dT%H:%M:%SZ';
16 our $dt_parser = DateTime::Format::Mail->new(loose => 1);
19 # FIXME: takes too many args, cleanup
21 my ($class, $git_dir, $max, $pi_config, $listname, $cgi, $top) = @_;
24 local $ENV{GIT_DIR} = $git_dir;
25 my $feed_opts = get_feedopts($pi_config, $listname, $cgi);
27 my $feed = XML::Atom::SimpleFeed->new(
28 title => $feed_opts->{description} || "unnamed feed",
29 link => $feed_opts->{url} || "http://example.com/",
32 href => $feed_opts->{atomurl} ||
33 "http://example.com/atom",
35 id => $feed_opts->{address} || 'public-inbox@example.com',
36 updated => strftime(DATEFMT, gmtime),
42 # we could use git log -z, but, we already know ssoma will not
43 # leave us with filenames with spaces in them..
44 my $cmd = "git log --no-color --raw -r --no-abbrev HEAD |";
45 my $pid = open my $log, $cmd or die "open `$cmd' pipe failed: $!\n";
48 foreach my $line (<$log>) {
49 if ($line =~ /^:000000 100644 0{40} ([a-f0-9]{40})/) {
51 next if $deleted{$add};
52 $nr += add_to_feed($feed_opts, $feed, $add, $top);
54 } elsif ($line =~ /^:100644 000000 ([a-f0-9]{40}) 0{40}/) {
64 # private functions below
66 my ($pi_config, $listname, $cgi) = @_;
68 if ($pi_config && defined $listname && length $listname) {
69 foreach my $key (qw(description address url atomurl midurl)) {
70 $rv{$key} = $pi_config->get($listname, $key);
74 my $cgi_url = $cgi->self_url;
75 my $url_base = $cgi_url;
76 $url_base =~ s!/?(?:index|all)\.atom\.xml\z!!;
77 $rv{url} ||= "$url_base/";
78 $rv{midurl} ||= "$url_base/mid/%s.html";
79 $rv{atomurl} = $cgi_url;
86 my ($mime, $name) = @_;
87 encode('utf8', decode('MIME-Header', $mime->header($name)));
92 my $dt = $dt_parser->parse_datetime($date);
93 $dt ? $dt->strftime(DATEFMT) : 0;
96 # returns 0 (skipped) or 1 (added)
98 my ($feed_opts, $feed, $add, $top) = @_;
100 # we can use git cat-file --batch if performance becomes a
101 # problem, but I doubt it...
102 my $str = `git cat-file blob $add`;
104 my $mime = Email::MIME->new($str);
106 if ($top && $mime->header("In-Reply-To")) {
110 my $content = msg_content($mime);
111 defined($content) or return 0;
113 my $midurl = $feed_opts->{midurl} || "http://example.com/mid/%s.html";
114 my $mid = utf8_header($mime, "Message-ID") or return 0;
118 my $subject = utf8_header($mime, "Subject") || "";
119 defined($subject) && length($subject) or return 0;
121 my $from = utf8_header($mime, "From") or return 0;
123 my @from = Email::Address->parse($from);
124 my $name = $from[0]->name;
125 defined $name or $name = "";
126 my $email = $from[0]->address;
127 defined $email or $email = "";
129 my $url = sprintf($midurl, uri_escape($mid));
130 my $date = utf8_header($mime, "Date");
132 $date = feed_date($date) or return 0;
134 author => { name => $name, email => $email },
137 content => { type => "html", content => $content },
144 # returns a plain-text message body without quoted text
145 # returns undef if there was nothing
150 # scan through all parts, looking for displayable text
151 $mime->walk_parts(sub {
154 return if $part->subparts; # walk_parts already recurses
155 my $ct = $part->content_type || 'text/plain';
156 return if $ct !~ m!\btext/[a-z0-9\+\._-]+\b!i;
158 my $killed_wrote; # omit "So-and-so wrote:" line
160 # no quoted text in Atom feed summary
161 # $part->body should already be decoded for us (no QP)
163 my $state = 0; # 0: beginning, 1: keep, 2: quoted
164 foreach my $l (split(/\r?\n/, $part->body)) {
166 # drop leading blank lines
167 next if $l =~ /\A\s*\z/;
169 $state = ($l =~ /\A>/) ? 2 : 1; # fall-through
171 if ($state == 2) { # quoted text, drop it
173 push @body, "<quoted text snipped>";
180 if ($state == 1) { # stuff we may keep
182 # drop "So-and-so wrote:" line
183 if (@body && !$killed_wrote &&
184 $body[-1] =~ /:\z/) {
195 join("\n", map { escapeHTML($_) } @body) .