1 # Copyright (C) 2016-2018 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>";
57 my ($ctx, $title) = @_;
58 my $ibx = $ctx->{-inbox};
59 my $base_url = $ctx->{feed_base_url};
60 my $search_q = $ctx->{search_query};
61 my $self_url = $base_url;
62 my $mid = $ctx->{mid};
63 if (defined $mid) { # per-thread
64 $self_url .= mid_escape($mid).'/t.atom';
65 } elsif (defined $search_q) {
66 my $query = $search_q->{'q'};
67 $title = title_tag("$query - search results");
68 $base_url .= '?' . $search_q->qs_html(x => undef);
69 $self_url .= '?' . $search_q->qs_html;
71 $title = title_tag($ibx->description);
72 $self_url .= 'new.atom';
74 my $mtime = (stat($ibx->{mainrepo}))[9] || time;
76 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
77 qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
78 qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
80 qq(<link\nrel="alternate"\ntype="text/html") .
81 qq(\nhref="$base_url"/>) .
82 qq(<link\nrel="self"\nhref="$self_url"/>) .
83 qq(<id>mailto:$ibx->{-primary_address}</id>) .
84 feed_updated(gmtime($mtime));
89 utf8::encode($mid); # really screwed up In-Reply-To fields exist
90 $mid = sha1_hex($mid);
92 my (@uuid5) = ($mid =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
93 'urn:uuid:' . join('-', @uuid5);
96 # returns undef or string
98 my ($self, $smsg) = @_;
99 my $ctx = $self->{ctx};
100 my $mime = $smsg->{mime};
101 my $hdr = $mime->header_obj;
102 my $mid = $smsg->mid;
103 my $irt = PublicInbox::View::in_reply_to($hdr);
104 my $uuid = mid2uuid($mid);
105 my $base = $ctx->{feed_base_url};
107 my $irt_uuid = mid2uuid($irt);
108 $irt = mid_escape($irt);
109 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
110 qq(href="$base$irt/"/>);
114 my $href = $base . mid_escape($mid) . '/';
115 my $t = msg_timestamp($hdr);
116 my @t = gmtime(defined $t ? $t : time);
117 my $updated = feed_updated(@t);
119 my $title = $hdr->header('Subject');
120 $title = '(no subject)' unless defined $title && $title ne '';
121 $title = title_tag($title);
123 my $from = $hdr->header('From') or return;
124 my ($email) = PublicInbox::Address::emails($from);
125 my $name = join(', ',PublicInbox::Address::names($from));
126 $name = ascii_html($name);
127 $email = ascii_html($email);
130 if (delete $ctx->{emit_header}) {
131 $s .= atom_header($ctx, $title);
133 $s .= "<entry><author><name>$name</name><email>$email</email>" .
134 "</author>$title$updated" .
135 qq(<link\nhref="$href"/>).
136 "<id>$uuid</id>$irt" .
137 qq{<content\ntype="xhtml">} .
138 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
139 qq(<pre\nstyle="white-space:pre-wrap">) .
140 PublicInbox::View::multipart_text_as_html($mime, $href, $ctx) .
141 '</pre></div></content></entry>';
145 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';