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 CGI qw(escapeHTML);
13 use POSIX qw(strftime);
14 use Date::Parse qw(strptime);
15 use constant DATEFMT => '%Y-%m-%dT%H:%M:%SZ';
16 use PublicInbox::View;
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)) {
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/";
79 $rv{fullurl} = "$url_base/full/";
80 $rv{atomurl} = $cgi_url;
87 my ($mime, $name) = @_;
88 encode('utf8', decode('MIME-Header', $mime->header($name)));
93 my @t = eval { strptime($date) };
95 scalar(@t) ? strftime(DATEFMT, @t) : 0;
98 # returns 0 (skipped) or 1 (added)
100 my ($feed_opts, $feed, $add, $top) = @_;
102 # we can use git cat-file --batch if performance becomes a
103 # problem, but I doubt it...
104 my $str = `git cat-file blob $add`;
106 my $mime = Email::MIME->new($str);
108 if ($top && $mime->header("In-Reply-To")) {
112 my $midurl = $feed_opts->{midurl} || 'http://example.com/mid/';
113 my $fullurl = $feed_opts->{fullurl} || 'http://example.com/full/';
115 my $content = PublicInbox::View->as_feed_entry($mime, $fullurl);
116 defined($content) or return 0;
118 my $mid = utf8_header($mime, "Message-ID") or return 0;
122 my $subject = utf8_header($mime, "Subject") || "";
123 defined($subject) && length($subject) or return 0;
125 my $from = utf8_header($mime, "From") or return 0;
127 my @from = Email::Address->parse($from);
128 my $name = $from[0]->name;
129 defined $name or $name = "";
130 my $email = $from[0]->address;
131 defined $email or $email = "";
133 my $url = $midurl . uri_escape($mid);
134 my $date = utf8_header($mime, "Date");
136 $date = feed_date($date) or return 0;
138 author => { name => $name, email => $email },
141 content => { type => "html", content => $content },