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>
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->{-inbox}->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->{-inbox}->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->{-inbox};
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 $page_id = "mailto:$ibx->{-primary_address}";
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">) .
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);
116 # returns undef or string
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};
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/"/>);
131 my $href = $base . mid_href($mid) . '/';
132 my $updated = feed_updated(msg_timestamp($eml));
134 my $title = $eml->header('Subject');
135 $title = '(no subject)' unless defined $title && $title ne '';
136 $title = title_tag($title);
138 my $from = $eml->header('From') // $eml->header('Sender') //
139 $ctx->{-inbox}->{-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->{-inbox}->{-primary_address});
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">);
153 $ctx->{mhref} = $href;
154 PublicInbox::View::multipart_text_as_html($eml, $ctx);
156 $s .= '</pre></div></content></entry>';
160 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(@_)) . '</updated>';