]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAtomStream.pm
atom: implement message threading per RFC 4685
[public-inbox.git] / lib / PublicInbox / WwwAtomStream.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Atom body stream for which yields getline+close methods
5 package PublicInbox::WwwAtomStream;
6 use strict;
7 use warnings;
8
9 # FIXME: locale-independence:
10 use POSIX qw(strftime);
11 use Date::Parse qw(strptime);
12 use Digest::SHA qw(sha1_hex);
13 use PublicInbox::Address;
14 use PublicInbox::Hval qw(ascii_html);
15 use PublicInbox::MID qw/mid_clean mid_escape/;
16
17 # called by PSGI server after getline:
18 sub close {}
19
20 sub new {
21         my ($class, $ctx, $cb) = @_;
22         $ctx->{emit_header} = 1;
23         $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
24         bless { cb => $cb || *close, ctx => $ctx }, $class;
25 }
26
27 sub response {
28         my ($class, $ctx, $code, $cb) = @_;
29         [ $code, [ 'Content-Type', 'application/atom+xml' ],
30           $class->new($ctx, $cb) ]
31 }
32
33 # called once for each message by PSGI server
34 sub getline {
35         my ($self) = @_;
36         if (my $middle = $self->{cb}) {
37                 my $mime = $middle->();
38                 return feed_entry($self, $mime) if $mime;
39         }
40         delete $self->{cb} ? '</feed>' : undef;
41 }
42
43 # private
44
45 sub title_tag {
46         my ($title) = @_;
47         $title =~ tr/\t\n / /s; # squeeze spaces
48         # try to avoid the type attribute in title:
49         $title = ascii_html($title);
50         my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
51         "<title$type>$title</title>";
52 }
53
54 sub atom_header {
55         my ($ctx, $title) = @_;
56         my $ibx = $ctx->{-inbox};
57         my $base_url = $ctx->{feed_base_url};
58         my $search_q = $ctx->{search_query};
59         my $self_url = $base_url;
60         my $mid = $ctx->{mid};
61         if (defined $mid) { # per-thread
62                 $self_url .= mid_escape($mid).'/t.atom';
63         } elsif (defined $search_q) {
64                 my $query = $search_q->{'q'};
65                 $title = title_tag("$query - search results");
66                 $base_url .= '?' . $search_q->qs_html(x => undef);
67                 $self_url .= '?' . $search_q->qs_html;
68         } else {
69                 $title = title_tag($ibx->description);
70                 $self_url .= 'new.atom';
71         }
72         my $mtime = (stat($ibx->{mainrepo}))[9] || time;
73
74         qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
75         qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
76         qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
77         qq{$title} .
78         qq(<link\nrel="alternate"\ntype="text/html") .
79                 qq(\nhref="$base_url"/>) .
80         qq(<link\nrel="self"\nhref="$self_url"/>) .
81         qq(<id>mailto:$ibx->{-primary_address}</id>) .
82         feed_updated(gmtime($mtime));
83 }
84
85 sub mid2uuid ($) {
86         my ($mid) = @_;
87         utf8::encode($mid); # really screwed up In-Reply-To fields exist
88         $mid = sha1_hex($mid);
89         my $h = '[a-f0-9]';
90         my (@uuid5) = ($mid =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
91         'urn:uuid:' . join('-', @uuid5);
92 }
93
94 # returns undef or string
95 sub feed_entry {
96         my ($self, $mime) = @_;
97         my $ctx = $self->{ctx};
98         my $hdr = $mime->header_obj;
99         my $mid = mid_clean($hdr->header_raw('Message-ID'));
100         my $irt = PublicInbox::View::in_reply_to($hdr);
101         my $uuid = mid2uuid($mid);
102         my $base = $ctx->{feed_base_url};
103         if (defined $irt) {
104                 my $irt_uuid = mid2uuid($irt);
105                 $irt = mid_escape($irt);
106                 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
107                         qq(href="$base$irt/"/>);
108         } else {
109                 $irt = '';
110         }
111         my $href = $base . mid_escape($mid) . '/';
112         my $date = $hdr->header('Date');
113         my @t = eval { strptime($date) } if defined $date;
114         @t = gmtime(time) unless scalar @t;
115         my $updated = feed_updated(@t);
116
117         my $title = $hdr->header('Subject');
118         $title = '(no subject)' unless defined $title && $title ne '';
119         $title = title_tag($title);
120
121         my $from = $hdr->header('From') or return;
122         my ($email) = PublicInbox::Address::emails($from);
123         my $name = join(', ',PublicInbox::Address::names($from));
124         $name = ascii_html($name);
125         $email = ascii_html($email);
126
127         my $s = '';
128         if (delete $ctx->{emit_header}) {
129                 $s .= atom_header($ctx, $title);
130         }
131         $s .= "<entry><author><name>$name</name><email>$email</email>" .
132                 "</author>$title$updated" .
133                 qq{<content\ntype="xhtml">} .
134                 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
135                 qq(<pre\nstyle="white-space:pre-wrap">) .
136                 PublicInbox::View::multipart_text_as_html($mime, $href) .
137                 '</pre>' .
138                 qq!</div></content><link\nhref="$href"/>!.
139                 "<id>$uuid</id>$irt</entry>";
140 }
141
142 sub feed_updated {
143         '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';
144 }
145
146 1;