]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
get a basic CGI feed sender running
[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 constant DATEFMT => '%Y-%m-%dT%H:%M:%SZ';
16 our $dt_parser = DateTime::Format::Mail->new(loose => 1);
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 $dt = $dt_parser->parse_datetime($date);
93         $dt ? $dt->strftime(DATEFMT) : 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;