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 bless { cb => $cb || \&close, ctx => $ctx, emit_header => 1 }, $class;
29 my ($class, $ctx, $code, $cb) = @_;
30 my $h = [ 'Content-Type' => 'application/atom+xml' ];
31 my $self = $class->new($ctx, $cb);
32 $self->{gzf} = gzf_maybe($h, $ctx->{env});
36 # called once for each message by PSGI server
40 if (my $middle = $self->{cb}) {
41 my $smsg = $middle->($self->{ctx});
42 feed_entry($self, $smsg) if $smsg;
44 } // (delete($self->{cb}) ? '</feed>' : undef);
46 # gzf may be GzipFilter, `undef' or `0'
47 my $gzf = $self->{gzf} or return $buf;
49 return $gzf->translate($buf) if defined $buf;
50 $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
51 $gzf->translate(undef);
58 $title =~ tr/\t\n / /s; # squeeze spaces
59 # try to avoid the type attribute in title:
60 $title = ascii_html($title);
61 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
62 "<title$type>$title</title>";
67 utf8::encode($any); # really screwed up In-Reply-To fields exist
68 $any = sha1_hex($any);
70 my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
71 'urn:uuid:' . join('-', @uuid5);
75 my ($ctx, $title) = @_;
76 my $ibx = $ctx->{-inbox};
77 my $base_url = $ctx->{feed_base_url};
78 my $search_q = $ctx->{search_query};
79 my $self_url = $base_url;
80 my $mid = $ctx->{mid};
82 if (defined $mid) { # per-thread
83 $self_url .= mid_href($mid).'/t.atom';
84 $page_id = to_uuid("t\n".$mid)
85 } elsif (defined $search_q) {
86 my $query = $search_q->{'q'};
87 $title = title_tag("$query - search results");
88 $base_url .= '?' . $search_q->qs_html(x => undef);
89 $self_url .= '?' . $search_q->qs_html;
90 $page_id = to_uuid("q\n".$query);
92 $title = title_tag($ibx->description);
93 $self_url .= 'new.atom';
94 $page_id = "mailto:$ibx->{-primary_address}";
96 my $mtime = (stat($ibx->{inboxdir}))[9] || time;
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($mtime);
109 # returns undef or string
111 my ($self, $smsg) = @_;
112 my $ctx = $self->{ctx};
113 my $eml = $ctx->{-inbox}->smsg_eml($smsg) or return '';
114 my $hdr = $eml->header_obj;
115 my $mid = $smsg->{mid};
116 my $irt = PublicInbox::View::in_reply_to($hdr);
117 my $uuid = to_uuid($mid);
118 my $base = $ctx->{feed_base_url};
120 my $irt_uuid = to_uuid($irt);
121 $irt = mid_href($irt);
122 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
123 qq(href="$base$irt/"/>);
127 my $href = $base . mid_href($mid) . '/';
128 my $updated = feed_updated(msg_timestamp($hdr));
130 my $title = $hdr->header('Subject');
131 $title = '(no subject)' unless defined $title && $title ne '';
132 $title = title_tag($title);
134 my $from = $hdr->header('From') or return;
135 my ($email) = PublicInbox::Address::emails($from);
136 my $name = join(', ',PublicInbox::Address::names($from));
137 $name = ascii_html($name);
138 $email = ascii_html($email);
141 if (delete $self->{emit_header}) {
142 $s .= atom_header($ctx, $title);
144 $s .= "<entry><author><name>$name</name><email>$email</email>" .
145 "</author>$title$updated" .
146 qq(<link\nhref="$href"/>).
147 "<id>$uuid</id>$irt" .
148 qq{<content\ntype="xhtml">} .
149 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
150 qq(<pre\nstyle="white-space:pre-wrap">);
152 $ctx->{mhref} = $href;
153 PublicInbox::View::multipart_text_as_html($eml, $ctx);
155 $s .= '</pre></div></content></entry>';
159 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(@_)) . '</updated>';