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