]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Feed.pm
da877f3f536d50209ee5d3965ad5cefcc4788826
[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 sub generate {
20         my ($class, $git_dir, $max) = @_;
21         $max ||= 25;
22
23         local $ENV{GIT_DIR} = $git_dir;
24         my $feed_opts = get_feedopts();
25
26         my $feed = XML::Atom::SimpleFeed->new(
27                 title => $feed_opts->{title},
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->{email} || '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);
52                         last if $nr >= $max;
53                 } elsif ($line =~ /^:100644 000000 ([a-f0-9]{40}) 0{40}/) {
54                         $deleted{$1} = 1;
55                 }
56         }
57         close $log;
58
59         $feed->as_string;
60 }
61
62 # private functions below
63 sub get_feedopts {
64         my %rv;
65         foreach my $key (qw(title url atomUrl email)) {
66                 my $tmp = `git config publicInboxFeed.$key`;
67                 chomp $tmp;
68                 $rv{$key} = $tmp;
69         }
70         \%rv;
71 }
72
73 sub utf8_header {
74         my ($mime, $name) = @_;
75         encode('utf8', decode('MIME-Header', $mime->header($name)));
76 }
77
78 sub feed_date {
79         my ($date) = @_;
80         my $dt = $dt_parser->parse_datetime($date);
81         $dt ? $dt->strftime(DATEFMT) : 0;
82 }
83
84 # returns 0 (skipped) or 1 (added)
85 sub add_to_feed {
86         my ($feed_opts, $feed, $add) = @_;
87
88         # we can use git cat-file --batch if performance becomes a
89         # problem, but I doubt it...
90         my $str = `git cat-file blob $add`;
91         return 0 if $? != 0;
92         my $mime = Email::MIME->new($str);
93
94         my $content = msg_content($mime);
95         defined($content) or return 0;
96
97         my $mid_url = $feed_opts->{mid_url} || "http://example.com/mid/%s";
98         my $mid = utf8_header($mime, "Message-ID") or return 0;
99         $mid =~ s/\A<//;
100         $mid =~ s/>\z//;
101
102         my $subject = utf8_header($mime, "Subject") || "";
103         defined($subject) && length($subject) or return 0;
104
105         my $from = utf8_header($mime, "From") or return 0;
106
107         my @from = Email::Address->parse($from);
108         my $name = $from[0]->name;
109         defined $name or $name = "";
110         my $email = $from[0]->address;
111         defined $email or $email = "";
112
113         my $url = sprintf($mid_url, uri_escape($mid));
114         my $date = utf8_header($mime, "Date");
115         $date or return 0;
116         $date = feed_date($date) or return 0;
117         $feed->add_entry(
118                 author => { name => $name, email => $email },
119                 title => $subject,
120                 updated => $date,
121                 content => { type => "html", content => $content },
122                 link => $url,
123                 id => $add,
124         );
125         1;
126 }
127
128 # returns a plain-text message body without quoted text
129 # returns undef if there was nothing
130 sub msg_content {
131         my ($mime) = @_;
132         my $rv;
133
134         # scan through all parts, looking for displayable text
135         $mime->walk_parts(sub {
136                 return if $rv;
137                 my ($part) = @_;
138                 return if $part->subparts; # walk_parts already recurses
139                 my $ct = $part->content_type || 'text/plain';
140                 return if $ct !~ m!\btext/[a-z0-9\+\._-]+\b!i;
141                 my @body;
142                 my $killed_wrote; # omit "So-and-so wrote:" line
143
144                 # no quoted text in Atom feed summary
145                 # $part->body should already be decoded for us (no QP)
146
147                 my $state = 0; # 0: beginning, 1: keep, 2: quoted
148                 foreach my $l (split(/\r?\n/, $part->body)) {
149                         if ($state == 0) {
150                                 # drop leading blank lines
151                                 next if $l =~ /\A\s*\z/;
152
153                                 $state = ($l =~ /\A>/) ? 2 : 1; # fall-through
154                         }
155                         if ($state == 2) { # quoted text, drop it
156                                 if ($l !~ /\A>/) {
157                                         push @body, "<quoted text snipped>";
158                                         if ($l =~ /\S/) {
159                                                 push @body, $l;
160                                         }
161                                         $state = 1;
162                                 }
163                         }
164                         if ($state == 1) { # stuff we may keep
165                                 if ($l =~ /\A>/) {
166                                         # drop "So-and-so wrote:" line
167                                         if (@body && !$killed_wrote &&
168                                             $body[-1] =~ /:\z/) {
169                                                 $killed_wrote = 1;
170                                                 pop @body;
171                                         }
172                                         $state = 2;
173                                 } else {
174                                         push @body, $l;
175                                 }
176                         }
177                 }
178                 $rv = "<pre>" .
179                         join("\n", map { escapeHTML($_) } @body) .
180                         "</pre>";
181         });
182
183         $rv;
184 }
185
186 1;