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