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