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