1 # Copyright (C) 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->write('</feed>');
38 sub async_eml { # for async_blob_cb
40 my $smsg = delete $ctx->{smsg};
41 $smsg->{mid} // $smsg->populate($eml);
42 $ctx->write(feed_entry($ctx, $smsg, $eml));
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);
52 # called once for each message by PSGI server
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));
61 $self->zflush('</feed>');
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>";
77 utf8::encode($any); # really screwed up In-Reply-To fields exist
78 $any = sha1_hex($any);
80 my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
81 'urn:uuid:' . join('-', @uuid5);
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};
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);
102 $title = title_tag($ibx->description);
103 $self_url .= 'new.atom';
104 if (defined(my $addr = $ibx->{-primary_address})) {
105 $page_id = "mailto:$addr";
107 $page_id = to_uuid($self_url);
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">) .
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);
121 # returns undef or string
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};
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/"/>);
136 my $href = $base . mid_href($mid) . '/';
137 my $updated = feed_updated(msg_timestamp($eml));
139 my $title = $eml->header('Subject');
140 $title = '(no subject)' unless defined $title && $title ne '';
141 $title = title_tag($title);
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});
149 my $s = delete($ctx->{emit_header}) ? atom_header($ctx, $title) : '';
150 $s .= "<entry><author><name>$name</name><email>$email</email>" .
151 "</author>$title$updated" .
152 qq(<link\nhref="$href"/>).
153 "<id>$uuid</id>$irt" .
154 qq{<content\ntype="xhtml">} .
155 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
156 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>';
166 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($t)) . '</updated>';