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