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>
4 # Atom body stream for HTTP responses
5 # See PublicInbox::GzipFilter for details.
6 package PublicInbox::WwwAtomStream;
8 use parent 'PublicInbox::GzipFilter';
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);
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;
25 my ($http) = @_; # PublicInbox::HTTP
26 my $ctx = $http->{forward} or return;
28 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
29 $ctx->smsg_blob($smsg);
31 $ctx->{http_out}->write($ctx->translate('</feed>'));
38 sub async_eml { # for async_blob_cb
40 my $smsg = delete $ctx->{smsg};
41 $ctx->{http_out}->write($ctx->translate(feed_entry($ctx, $smsg, $eml)))
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);
51 # called once for each message by PSGI server
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));
60 $self->zflush('</feed>');
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>";
76 utf8::encode($any); # really screwed up In-Reply-To fields exist
77 $any = sha1_hex($any);
79 my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
80 'urn:uuid:' . join('-', @uuid5);
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};
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);
101 $title = title_tag($ibx->description);
102 $self_url .= 'new.atom';
103 if (defined(my $addr = $ibx->{-primary_address})) {
104 $page_id = "mailto:$addr";
106 $page_id = to_uuid($self_url);
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">) .
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);
120 # returns undef or string
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};
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/"/>);
135 my $href = $base . mid_href($mid) . '/';
136 my $updated = feed_updated(msg_timestamp($eml));
138 my $title = $eml->header('Subject');
139 $title = '(no subject)' unless defined $title && $title ne '';
140 $title = title_tag($title);
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});
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">);
157 $ctx->{mhref} = $href;
158 PublicInbox::View::multipart_text_as_html($eml, $ctx);
160 $s .= '</pre></div></content></entry>';
165 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($t)) . '</updated>';