1 # Copyright (C) 2016-2019 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);
16 use PublicInbox::MID qw/mid_clean mid_escape/;
17 use PublicInbox::MsgTime qw(msg_timestamp);
19 # called by PSGI server after getline:
23 my ($class, $ctx, $cb) = @_;
24 $ctx->{emit_header} = 1;
25 $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
26 bless { cb => $cb || *close, ctx => $ctx }, $class;
30 my ($class, $ctx, $code, $cb) = @_;
31 [ $code, [ 'Content-Type', 'application/atom+xml' ],
32 $class->new($ctx, $cb) ]
35 # called once for each message by PSGI server
38 if (my $middle = $self->{cb}) {
39 my $smsg = $middle->();
40 return feed_entry($self, $smsg) if $smsg;
42 delete $self->{cb} ? '</feed>' : undef;
49 $title =~ tr/\t\n / /s; # squeeze spaces
50 # try to avoid the type attribute in title:
51 $title = ascii_html($title);
52 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
53 "<title$type>$title</title>";
58 utf8::encode($any); # really screwed up In-Reply-To fields exist
59 $any = sha1_hex($any);
61 my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
62 'urn:uuid:' . join('-', @uuid5);
66 my ($ctx, $title) = @_;
67 my $ibx = $ctx->{-inbox};
68 my $base_url = $ctx->{feed_base_url};
69 my $search_q = $ctx->{search_query};
70 my $self_url = $base_url;
71 my $mid = $ctx->{mid};
73 if (defined $mid) { # per-thread
74 $self_url .= mid_escape($mid).'/t.atom';
75 $page_id = to_uuid("t\n".$mid)
76 } elsif (defined $search_q) {
77 my $query = $search_q->{'q'};
78 $title = title_tag("$query - search results");
79 $base_url .= '?' . $search_q->qs_html(x => undef);
80 $self_url .= '?' . $search_q->qs_html;
81 $page_id = to_uuid("q\n".$query);
83 $title = title_tag($ibx->description);
84 $self_url .= 'new.atom';
85 $page_id = "mailto:$ibx->{-primary_address}";
87 my $mtime = (stat($ibx->{mainrepo}))[9] || time;
89 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
90 qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
91 qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
93 qq(<link\nrel="alternate"\ntype="text/html") .
94 qq(\nhref="$base_url"/>) .
95 qq(<link\nrel="self"\nhref="$self_url"/>) .
96 qq(<id>$page_id</id>) .
97 feed_updated(gmtime($mtime));
100 # returns undef or string
102 my ($self, $smsg) = @_;
103 my $ctx = $self->{ctx};
104 my $mime = $smsg->{mime};
105 my $hdr = $mime->header_obj;
106 my $mid = $smsg->mid;
107 my $irt = PublicInbox::View::in_reply_to($hdr);
108 my $uuid = to_uuid($mid);
109 my $base = $ctx->{feed_base_url};
111 my $irt_uuid = to_uuid($irt);
112 $irt = mid_escape($irt);
113 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
114 qq(href="$base$irt/"/>);
118 my $href = $base . mid_escape($mid) . '/';
119 my $t = msg_timestamp($hdr);
120 my @t = gmtime(defined $t ? $t : time);
121 my $updated = feed_updated(@t);
123 my $title = $hdr->header('Subject');
124 $title = '(no subject)' unless defined $title && $title ne '';
125 $title = title_tag($title);
127 my $from = $hdr->header('From') or return;
128 my ($email) = PublicInbox::Address::emails($from);
129 my $name = join(', ',PublicInbox::Address::names($from));
130 $name = ascii_html($name);
131 $email = ascii_html($email);
134 if (delete $ctx->{emit_header}) {
135 $s .= atom_header($ctx, $title);
137 $s .= "<entry><author><name>$name</name><email>$email</email>" .
138 "</author>$title$updated" .
139 qq(<link\nhref="$href"/>).
140 "<id>$uuid</id>$irt" .
141 qq{<content\ntype="xhtml">} .
142 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
143 qq(<pre\nstyle="white-space:pre-wrap">) .
144 PublicInbox::View::multipart_text_as_html($mime, $href, $ctx) .
145 '</pre></div></content></entry>';
149 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';