]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
feed: use Date::Parse to parse dates
[public-inbox.git] / lib / PublicInbox / Feed.pm
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;
4 use strict;
5 use warnings;
6 use XML::Atom::SimpleFeed;
7 use Email::MIME;
8 use Email::Address;
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 Date::Parse qw(strptime);
16 use constant DATEFMT => '%Y-%m-%dT%H:%M:%SZ';
17
18 # main function
19 # FIXME: takes too many args, cleanup
20 sub generate {
21         my ($class, $git_dir, $max, $pi_config, $listname, $cgi, $top) = @_;
22         $max ||= 25;
23
24         local $ENV{GIT_DIR} = $git_dir;
25         my $feed_opts = get_feedopts($pi_config, $listname, $cgi);
26
27         my $feed = XML::Atom::SimpleFeed->new(
28                 title => $feed_opts->{description} || "unnamed feed",
29                 link => $feed_opts->{url} || "http://example.com/",
30                 link => {
31                         rel => 'self',
32                         href => $feed_opts->{atomurl} ||
33                                 "http://example.com/atom",
34                 },
35                 id => $feed_opts->{address} || 'public-inbox@example.com',
36                 updated => strftime(DATEFMT, gmtime),
37         );
38
39         my @entries;
40
41         # get recent messages
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";
46         my %deleted;
47         my $nr = 0;
48         foreach my $line (<$log>) {
49                 if ($line =~ /^:000000 100644 0{40} ([a-f0-9]{40})/) {
50                         my $add = $1;
51                         next if $deleted{$add};
52                         $nr += add_to_feed($feed_opts, $feed, $add, $top);
53                         last if $nr >= $max;
54                 } elsif ($line =~ /^:100644 000000 ([a-f0-9]{40}) 0{40}/) {
55                         $deleted{$1} = 1;
56                 }
57         }
58
59         close $log;
60
61         $feed->as_string;
62 }
63
64 # private functions below
65 sub get_feedopts {
66         my ($pi_config, $listname, $cgi) = @_;
67         my %rv;
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);
71                 }
72         }
73         if ($cgi) {
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;
80         }
81
82         \%rv;
83 }
84
85 sub utf8_header {
86         my ($mime, $name) = @_;
87         encode('utf8', decode('MIME-Header', $mime->header($name)));
88 }
89
90 sub feed_date {
91         my ($date) = @_;
92         my @t = eval { strptime($date) };
93
94         scalar(@t) ? strftime(DATEFMT, @t) : 0;
95 }
96
97 # returns 0 (skipped) or 1 (added)
98 sub add_to_feed {
99         my ($feed_opts, $feed, $add, $top) = @_;
100
101         # we can use git cat-file --batch if performance becomes a
102         # problem, but I doubt it...
103         my $str = `git cat-file blob $add`;
104         return 0 if $? != 0;
105         my $mime = Email::MIME->new($str);
106
107         if ($top && $mime->header("In-Reply-To")) {
108                 return 0;
109         }
110
111         my $content = msg_content($mime);
112         defined($content) or return 0;
113
114         my $midurl = $feed_opts->{midurl} || "http://example.com/mid/%s.html";
115         my $mid = utf8_header($mime, "Message-ID") or return 0;
116         $mid =~ s/\A<//;
117         $mid =~ s/>\z//;
118
119         my $subject = utf8_header($mime, "Subject") || "";
120         defined($subject) && length($subject) or return 0;
121
122         my $from = utf8_header($mime, "From") or return 0;
123
124         my @from = Email::Address->parse($from);
125         my $name = $from[0]->name;
126         defined $name or $name = "";
127         my $email = $from[0]->address;
128         defined $email or $email = "";
129
130         my $url = sprintf($midurl, uri_escape($mid));
131         my $date = utf8_header($mime, "Date");
132         $date or return 0;
133         $date = feed_date($date) or return 0;
134         $feed->add_entry(
135                 author => { name => $name, email => $email },
136                 title => $subject,
137                 updated => $date,
138                 content => { type => "html", content => $content },
139                 link => $url,
140                 id => $add,
141         );
142         1;
143 }
144
145 # returns a plain-text message body without quoted text
146 # returns undef if there was nothing
147 sub msg_content {
148         my ($mime) = @_;
149         my $rv;
150
151         # scan through all parts, looking for displayable text
152         $mime->walk_parts(sub {
153                 return if $rv;
154                 my ($part) = @_;
155                 return if $part->subparts; # walk_parts already recurses
156                 my $ct = $part->content_type || 'text/plain';
157                 return if $ct !~ m!\btext/[a-z0-9\+\._-]+\b!i;
158                 my @body;
159                 my $killed_wrote; # omit "So-and-so wrote:" line
160
161                 # no quoted text in Atom feed summary
162                 # $part->body should already be decoded for us (no QP)
163
164                 my $state = 0; # 0: beginning, 1: keep, 2: quoted
165                 foreach my $l (split(/\r?\n/, $part->body)) {
166                         if ($state == 0) {
167                                 # drop leading blank lines
168                                 next if $l =~ /\A\s*\z/;
169
170                                 $state = ($l =~ /\A>/) ? 2 : 1; # fall-through
171                         }
172                         if ($state == 2) { # quoted text, drop it
173                                 if ($l !~ /\A>/) {
174                                         push @body, "<quoted text snipped>";
175                                         if ($l =~ /\S/) {
176                                                 push @body, $l;
177                                         }
178                                         $state = 1;
179                                 }
180                         }
181                         if ($state == 1) { # stuff we may keep
182                                 if ($l =~ /\A>/) {
183                                         # drop "So-and-so wrote:" line
184                                         if (@body && !$killed_wrote &&
185                                             $body[-1] =~ /:\z/) {
186                                                 $killed_wrote = 1;
187                                                 pop @body;
188                                         }
189                                         $state = 2;
190                                 } else {
191                                         push @body, $l;
192                                 }
193                         }
194                 }
195                 $rv = "<pre>" .
196                         join("\n", map { escapeHTML($_) } @body) .
197                         "</pre>";
198         });
199
200         $rv;
201 }
202
203 1;