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->{-spfx} = $ctx->{feed_base_url} if $ctx->{ibx}->{coderepo};
20 $ctx->{cb} = $cb || \&PublicInbox::GzipFilter::close;
21 $ctx->{emit_header} = 1;
26 my ($http) = @_; # PublicInbox::HTTP
27 my $ctx = $http->{forward} or return;
29 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
30 $ctx->smsg_blob($smsg);
32 $ctx->write('</feed>');
39 sub async_eml { # for async_blob_cb
41 my $smsg = delete $ctx->{smsg};
42 $smsg->{mid} // $smsg->populate($eml);
43 $ctx->write(feed_entry($ctx, $smsg, $eml));
47 my ($class, $ctx, $cb) = @_;
48 my $res_hdr = [ 'Content-Type' => 'application/atom+xml' ];
49 $class->new($ctx, $cb);
50 $ctx->psgi_response(200, $res_hdr);
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->{ibx}->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->{ibx};
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 if (defined(my $addr = $ibx->{-primary_address})) {
106 $page_id = "mailto:$addr";
108 $page_id = to_uuid($self_url);
111 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
112 qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
113 qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
115 qq(<link\nrel="alternate"\ntype="text/html") .
116 qq(\nhref="$base_url"/>) .
117 qq(<link\nrel="self"\nhref="$self_url"/>) .
118 qq(<id>$page_id</id>) .
119 feed_updated($ibx->modified);
122 # returns undef or string
124 my ($ctx, $smsg, $eml) = @_;
125 my $mid = $smsg->{mid};
126 my $irt = PublicInbox::View::in_reply_to($eml);
127 my $uuid = to_uuid($mid);
128 my $base = $ctx->{feed_base_url};
130 my $irt_uuid = to_uuid($irt);
131 $irt = mid_href($irt);
132 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
133 qq(href="$base$irt/"/>);
137 my $href = $base . mid_href($mid) . '/';
138 my $updated = feed_updated(msg_timestamp($eml));
140 my $title = $eml->header('Subject');
141 $title = '(no subject)' unless defined $title && $title ne '';
142 $title = title_tag($title);
144 my $from = $eml->header('From') // $eml->header('Sender') //
145 $ctx->{ibx}->{-primary_address};
146 my ($email) = PublicInbox::Address::emails($from);
147 my $name = ascii_html(join(', ', PublicInbox::Address::names($from)));
148 $email = ascii_html($email // $ctx->{ibx}->{-primary_address});
151 (delete($ctx->{emit_header}) ? atom_header($ctx, $title) : ''),
152 "<entry><author><name>$name</name><email>$email</email>" .
153 "</author>$title$updated" .
154 qq(<link\nhref="$href"/>) .
155 "<id>$uuid</id>$irt" .
156 qq{<content\ntype="xhtml">} .
157 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
158 qq(<pre\nstyle="white-space:pre-wrap">);
159 $ctx->{mhref} = $href;
160 $ctx->{changed_href} = "${href}#related";
161 $eml->each_part(\&PublicInbox::View::add_text_body, $ctx, 1);
162 '</pre></div></content></entry>';
167 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($t)) . '</updated>';