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