]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAtomStream.pm
treewide: "require" + "use" cleanup and docs
[public-inbox.git] / lib / PublicInbox / WwwAtomStream.pm
1 # Copyright (C) 2016-2019 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_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->($self->{ctx});
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 to_uuid ($) {
57         my ($any) = @_;
58         utf8::encode($any); # really screwed up In-Reply-To fields exist
59         $any = sha1_hex($any);
60         my $h = '[a-f0-9]';
61         my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
62         'urn:uuid:' . join('-', @uuid5);
63 }
64
65 sub atom_header {
66         my ($ctx, $title) = @_;
67         my $ibx = $ctx->{-inbox};
68         my $base_url = $ctx->{feed_base_url};
69         my $search_q = $ctx->{search_query};
70         my $self_url = $base_url;
71         my $mid = $ctx->{mid};
72         my $page_id;
73         if (defined $mid) { # per-thread
74                 $self_url .= mid_escape($mid).'/t.atom';
75                 $page_id = to_uuid("t\n".$mid)
76         } elsif (defined $search_q) {
77                 my $query = $search_q->{'q'};
78                 $title = title_tag("$query - search results");
79                 $base_url .= '?' . $search_q->qs_html(x => undef);
80                 $self_url .= '?' . $search_q->qs_html;
81                 $page_id = to_uuid("q\n".$query);
82         } else {
83                 $title = title_tag($ibx->description);
84                 $self_url .= 'new.atom';
85                 $page_id = "mailto:$ibx->{-primary_address}";
86         }
87         my $mtime = (stat($ibx->{inboxdir}))[9] || time;
88
89         qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
90         qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
91         qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
92         qq{$title} .
93         qq(<link\nrel="alternate"\ntype="text/html") .
94                 qq(\nhref="$base_url"/>) .
95         qq(<link\nrel="self"\nhref="$self_url"/>) .
96         qq(<id>$page_id</id>) .
97         feed_updated(gmtime($mtime));
98 }
99
100 # returns undef or string
101 sub feed_entry {
102         my ($self, $smsg) = @_;
103         my $ctx = $self->{ctx};
104         my $mime = $smsg->{mime};
105         my $hdr = $mime->header_obj;
106         my $mid = $smsg->mid;
107         my $irt = PublicInbox::View::in_reply_to($hdr);
108         my $uuid = to_uuid($mid);
109         my $base = $ctx->{feed_base_url};
110         if (defined $irt) {
111                 my $irt_uuid = to_uuid($irt);
112                 $irt = mid_escape($irt);
113                 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
114                         qq(href="$base$irt/"/>);
115         } else {
116                 $irt = '';
117         }
118         my $href = $base . mid_escape($mid) . '/';
119         my $t = msg_timestamp($hdr);
120         my @t = gmtime(defined $t ? $t : time);
121         my $updated = feed_updated(@t);
122
123         my $title = $hdr->header('Subject');
124         $title = '(no subject)' unless defined $title && $title ne '';
125         $title = title_tag($title);
126
127         my $from = $hdr->header('From') or return;
128         my ($email) = PublicInbox::Address::emails($from);
129         my $name = join(', ',PublicInbox::Address::names($from));
130         $name = ascii_html($name);
131         $email = ascii_html($email);
132
133         my $s = '';
134         if (delete $ctx->{emit_header}) {
135                 $s .= atom_header($ctx, $title);
136         }
137         $s .= "<entry><author><name>$name</name><email>$email</email>" .
138                 "</author>$title$updated" .
139                 qq(<link\nhref="$href"/>).
140                 "<id>$uuid</id>$irt" .
141                 qq{<content\ntype="xhtml">} .
142                 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
143                 qq(<pre\nstyle="white-space:pre-wrap">) .
144                 PublicInbox::View::multipart_text_as_html($mime, $href, $ctx) .
145                 '</pre></div></content></entry>';
146 }
147
148 sub feed_updated {
149         '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';
150 }
151
152 1;