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;
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);
17 use PublicInbox::GzipFilter qw(gzf_maybe);
19 # called by PSGI server after getline:
23 my ($class, $ctx, $cb) = @_;
24 $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
25 $ctx->{cb} = $cb || \&close;
26 $ctx->{emit_header} = 1;
31 my ($class, $ctx, $code, $cb) = @_;
32 my $h = [ 'Content-Type' => 'application/atom+xml' ];
33 $class->new($ctx, $cb);
34 $ctx->{gzf} = gzf_maybe($h, $ctx->{env});
38 # called once for each message by PSGI server
42 if (my $middle = $self->{cb}) {
43 my $smsg = $middle->($self);
44 feed_entry($self, $smsg) if $smsg;
46 } // (delete($self->{cb}) ? '</feed>' : undef);
48 # gzf may be GzipFilter, `undef' or `0'
49 my $gzf = $self->{gzf} or return $buf;
51 return $gzf->translate($buf) if defined $buf;
52 $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
53 $gzf->translate(undef);
60 $title =~ tr/\t\n / /s; # squeeze spaces
61 # try to avoid the type attribute in title:
62 $title = ascii_html($title);
63 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
64 "<title$type>$title</title>";
69 utf8::encode($any); # really screwed up In-Reply-To fields exist
70 $any = sha1_hex($any);
72 my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
73 'urn:uuid:' . join('-', @uuid5);
77 my ($ctx, $title) = @_;
78 my $ibx = $ctx->{-inbox};
79 my $base_url = $ctx->{feed_base_url};
80 my $search_q = $ctx->{search_query};
81 my $self_url = $base_url;
82 my $mid = $ctx->{mid};
84 if (defined $mid) { # per-thread
85 $self_url .= mid_href($mid).'/t.atom';
86 $page_id = to_uuid("t\n".$mid)
87 } elsif (defined $search_q) {
88 my $query = $search_q->{'q'};
89 $title = title_tag("$query - search results");
90 $base_url .= '?' . $search_q->qs_html(x => undef);
91 $self_url .= '?' . $search_q->qs_html;
92 $page_id = to_uuid("q\n".$query);
94 $title = title_tag($ibx->description);
95 $self_url .= 'new.atom';
96 $page_id = "mailto:$ibx->{-primary_address}";
98 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
99 qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
100 qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
102 qq(<link\nrel="alternate"\ntype="text/html") .
103 qq(\nhref="$base_url"/>) .
104 qq(<link\nrel="self"\nhref="$self_url"/>) .
105 qq(<id>$page_id</id>) .
106 feed_updated($ibx->modified);
109 # returns undef or string
111 my ($ctx, $smsg) = @_;
112 my $eml = $ctx->{-inbox}->smsg_eml($smsg) or return '';
113 my $hdr = $eml->header_obj;
114 my $mid = $smsg->{mid};
115 my $irt = PublicInbox::View::in_reply_to($hdr);
116 my $uuid = to_uuid($mid);
117 my $base = $ctx->{feed_base_url};
119 my $irt_uuid = to_uuid($irt);
120 $irt = mid_href($irt);
121 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
122 qq(href="$base$irt/"/>);
126 my $href = $base . mid_href($mid) . '/';
127 my $updated = feed_updated(msg_timestamp($hdr));
129 my $title = $hdr->header('Subject');
130 $title = '(no subject)' unless defined $title && $title ne '';
131 $title = title_tag($title);
133 my $from = $hdr->header('From') or return;
134 my ($email) = PublicInbox::Address::emails($from);
135 my $name = join(', ',PublicInbox::Address::names($from));
136 $name = ascii_html($name);
137 $email = ascii_html($email);
140 if (delete $ctx->{emit_header}) {
141 $s .= atom_header($ctx, $title);
143 $s .= "<entry><author><name>$name</name><email>$email</email>" .
144 "</author>$title$updated" .
145 qq(<link\nhref="$href"/>).
146 "<id>$uuid</id>$irt" .
147 qq{<content\ntype="xhtml">} .
148 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
149 qq(<pre\nstyle="white-space:pre-wrap">);
151 $ctx->{mhref} = $href;
152 PublicInbox::View::multipart_text_as_html($eml, $ctx);
154 $s .= '</pre></div></content></entry>';
158 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(@_)) . '</updated>';