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 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;
10 use parent 'PublicInbox::GzipFilter';
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);
19 my ($class, $ctx, $cb) = @_;
20 $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
21 $ctx->{cb} = $cb || \&PublicInbox::GzipFilter::close;
22 $ctx->{emit_header} = 1;
27 my ($http) = @_; # PublicInbox::HTTP
28 my $ctx = $http->{forward} or return;
30 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
31 $ctx->smsg_blob($smsg);
33 $ctx->{http_out}->write($ctx->translate('</feed>'));
40 sub async_eml { # ->{async_eml} for async_blob_cb
42 my $smsg = delete $ctx->{smsg};
43 $ctx->{http_out}->write($ctx->translate(feed_entry($ctx, $smsg, $eml)))
47 my ($class, $ctx, $code, $cb) = @_;
48 my $res_hdr = [ 'Content-Type' => 'application/atom+xml' ];
49 $class->new($ctx, $cb);
50 $ctx->psgi_response($code, $res_hdr, \&async_next, \&async_eml);
53 # called once for each message by PSGI server
56 my $cb = $self->{cb} or return;
57 while (my $smsg = $cb->($self)) {
58 my $eml = $self->{-inbox}->smsg_eml($smsg) or next;
59 return $self->translate(feed_entry($self, $smsg, $eml));
62 $self->zflush('</feed>');
69 $title =~ tr/\t\n / /s; # squeeze spaces
70 # try to avoid the type attribute in title:
71 $title = ascii_html($title);
72 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
73 "<title$type>$title</title>";
78 utf8::encode($any); # really screwed up In-Reply-To fields exist
79 $any = sha1_hex($any);
81 my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
82 'urn:uuid:' . join('-', @uuid5);
86 my ($ctx, $title) = @_;
87 my $ibx = $ctx->{-inbox};
88 my $base_url = $ctx->{feed_base_url};
89 my $search_q = $ctx->{search_query};
90 my $self_url = $base_url;
91 my $mid = $ctx->{mid};
93 if (defined $mid) { # per-thread
94 $self_url .= mid_href($mid).'/t.atom';
95 $page_id = to_uuid("t\n".$mid)
96 } elsif (defined $search_q) {
97 my $query = $search_q->{'q'};
98 $title = title_tag("$query - search results");
99 $base_url .= '?' . $search_q->qs_html(x => undef);
100 $self_url .= '?' . $search_q->qs_html;
101 $page_id = to_uuid("q\n".$query);
103 $title = title_tag($ibx->description);
104 $self_url .= 'new.atom';
105 $page_id = "mailto:$ibx->{-primary_address}";
107 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
108 qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
109 qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
111 qq(<link\nrel="alternate"\ntype="text/html") .
112 qq(\nhref="$base_url"/>) .
113 qq(<link\nrel="self"\nhref="$self_url"/>) .
114 qq(<id>$page_id</id>) .
115 feed_updated($ibx->modified);
118 # returns undef or string
120 my ($ctx, $smsg, $eml) = @_;
121 my $hdr = $eml->header_obj;
122 my $mid = $smsg->{mid};
123 my $irt = PublicInbox::View::in_reply_to($hdr);
124 my $uuid = to_uuid($mid);
125 my $base = $ctx->{feed_base_url};
127 my $irt_uuid = to_uuid($irt);
128 $irt = mid_href($irt);
129 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
130 qq(href="$base$irt/"/>);
134 my $href = $base . mid_href($mid) . '/';
135 my $updated = feed_updated(msg_timestamp($hdr));
137 my $title = $hdr->header('Subject');
138 $title = '(no subject)' unless defined $title && $title ne '';
139 $title = title_tag($title);
141 my $from = $hdr->header('From') or return;
142 my ($email) = PublicInbox::Address::emails($from);
143 my $name = join(', ',PublicInbox::Address::names($from));
144 $name = ascii_html($name);
145 $email = ascii_html($email);
148 if (delete $ctx->{emit_header}) {
149 $s .= atom_header($ctx, $title);
151 $s .= "<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">);
159 $ctx->{mhref} = $href;
160 PublicInbox::View::multipart_text_as_html($eml, $ctx);
162 $s .= '</pre></div></content></entry>';
166 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(@_)) . '</updated>';