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);
18 # called by PSGI server after getline:
22 my ($class, $ctx, $cb) = @_;
23 $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
24 bless { cb => $cb || \&close, ctx => $ctx, emit_header => 1 }, $class;
28 my ($class, $ctx, $code, $cb) = @_;
29 [ $code, [ 'Content-Type', 'application/atom+xml' ],
30 $class->new($ctx, $cb) ]
33 # called once for each message by PSGI server
36 if (my $middle = $self->{cb}) {
37 my $smsg = $middle->($self->{ctx});
38 return feed_entry($self, $smsg) if $smsg;
40 delete $self->{cb} ? '</feed>' : undef;
47 $title =~ tr/\t\n / /s; # squeeze spaces
48 # try to avoid the type attribute in title:
49 $title = ascii_html($title);
50 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
51 "<title$type>$title</title>";
56 utf8::encode($any); # really screwed up In-Reply-To fields exist
57 $any = sha1_hex($any);
59 my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
60 'urn:uuid:' . join('-', @uuid5);
64 my ($ctx, $title) = @_;
65 my $ibx = $ctx->{-inbox};
66 my $base_url = $ctx->{feed_base_url};
67 my $search_q = $ctx->{search_query};
68 my $self_url = $base_url;
69 my $mid = $ctx->{mid};
71 if (defined $mid) { # per-thread
72 $self_url .= mid_href($mid).'/t.atom';
73 $page_id = to_uuid("t\n".$mid)
74 } elsif (defined $search_q) {
75 my $query = $search_q->{'q'};
76 $title = title_tag("$query - search results");
77 $base_url .= '?' . $search_q->qs_html(x => undef);
78 $self_url .= '?' . $search_q->qs_html;
79 $page_id = to_uuid("q\n".$query);
81 $title = title_tag($ibx->description);
82 $self_url .= 'new.atom';
83 $page_id = "mailto:$ibx->{-primary_address}";
85 my $mtime = (stat($ibx->{inboxdir}))[9] || time;
87 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
88 qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
89 qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
91 qq(<link\nrel="alternate"\ntype="text/html") .
92 qq(\nhref="$base_url"/>) .
93 qq(<link\nrel="self"\nhref="$self_url"/>) .
94 qq(<id>$page_id</id>) .
95 feed_updated(gmtime($mtime));
98 # returns undef or string
100 my ($self, $smsg) = @_;
101 my $ctx = $self->{ctx};
102 my $mid = $smsg->mid; # may extract Message-ID from {mime}
103 my $mime = delete $smsg->{mime};
104 my $hdr = $mime->header_obj;
105 my $irt = PublicInbox::View::in_reply_to($hdr);
106 my $uuid = to_uuid($mid);
107 my $base = $ctx->{feed_base_url};
109 my $irt_uuid = to_uuid($irt);
110 $irt = mid_href($irt);
111 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
112 qq(href="$base$irt/"/>);
116 my $href = $base . mid_href($mid) . '/';
117 my $t = msg_timestamp($hdr);
118 my @t = gmtime(defined $t ? $t : time);
119 my $updated = feed_updated(@t);
121 my $title = $hdr->header('Subject');
122 $title = '(no subject)' unless defined $title && $title ne '';
123 $title = title_tag($title);
125 my $from = $hdr->header('From') or return;
126 my ($email) = PublicInbox::Address::emails($from);
127 my $name = join(', ',PublicInbox::Address::names($from));
128 $name = ascii_html($name);
129 $email = ascii_html($email);
132 if (delete $self->{emit_header}) {
133 $s .= atom_header($ctx, $title);
135 $s .= "<entry><author><name>$name</name><email>$email</email>" .
136 "</author>$title$updated" .
137 qq(<link\nhref="$href"/>).
138 "<id>$uuid</id>$irt" .
139 qq{<content\ntype="xhtml">} .
140 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
141 qq(<pre\nstyle="white-space:pre-wrap">);
143 $ctx->{mhref} = $href;
144 PublicInbox::View::multipart_text_as_html($mime, $ctx);
146 $s .= '</pre></div></content></entry>';
150 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';