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