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