]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAtomStream.pm
wwwatomstream: call gmtime with scalar
[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                 if (defined(my $addr = $ibx->{-primary_address})) {
104                         $page_id = "mailto:$addr";
105                 } else {
106                         $page_id = to_uuid($self_url);
107                 }
108         }
109         qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
110         qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
111         qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
112         qq{$title} .
113         qq(<link\nrel="alternate"\ntype="text/html") .
114                 qq(\nhref="$base_url"/>) .
115         qq(<link\nrel="self"\nhref="$self_url"/>) .
116         qq(<id>$page_id</id>) .
117         feed_updated($ibx->modified);
118 }
119
120 # returns undef or string
121 sub feed_entry {
122         my ($ctx, $smsg, $eml) = @_;
123         my $mid = $smsg->{mid};
124         my $irt = PublicInbox::View::in_reply_to($eml);
125         my $uuid = to_uuid($mid);
126         my $base = $ctx->{feed_base_url};
127         if (defined $irt) {
128                 my $irt_uuid = to_uuid($irt);
129                 $irt = mid_href($irt);
130                 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
131                         qq(href="$base$irt/"/>);
132         } else {
133                 $irt = '';
134         }
135         my $href = $base . mid_href($mid) . '/';
136         my $updated = feed_updated(msg_timestamp($eml));
137
138         my $title = $eml->header('Subject');
139         $title = '(no subject)' unless defined $title && $title ne '';
140         $title = title_tag($title);
141
142         my $from = $eml->header('From') // $eml->header('Sender') //
143                 $ctx->{ibx}->{-primary_address};
144         my ($email) = PublicInbox::Address::emails($from);
145         my $name = ascii_html(join(', ', PublicInbox::Address::names($from)));
146         $email = ascii_html($email // $ctx->{ibx}->{-primary_address});
147
148         my $s = delete($ctx->{emit_header}) ? atom_header($ctx, $title) : '';
149         $s .= "<entry><author><name>$name</name><email>$email</email>" .
150                 "</author>$title$updated" .
151                 qq(<link\nhref="$href"/>).
152                 "<id>$uuid</id>$irt" .
153                 qq{<content\ntype="xhtml">} .
154                 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
155                 qq(<pre\nstyle="white-space:pre-wrap">);
156         $ctx->{obuf} = \$s;
157         $ctx->{mhref} = $href;
158         PublicInbox::View::multipart_text_as_html($eml, $ctx);
159         delete $ctx->{obuf};
160         $s .= '</pre></div></content></entry>';
161 }
162
163 sub feed_updated {
164         my ($t) = @_;
165         '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($t)) . '</updated>';
166 }
167
168 1;