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