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