1 # Copyright (C) 2016 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 package PublicInbox::WwwAtomStream;
9 use POSIX qw(strftime);
10 use Date::Parse qw(strptime);
11 use Digest::SHA qw(sha1_hex);
12 use PublicInbox::Address;
13 use PublicInbox::Hval qw(ascii_html);
14 use PublicInbox::MID qw/mid_clean mid_escape/;
16 # called by PSGI server after getline:
20 my ($class, $ctx, $cb) = @_;
21 $ctx->{emit_header} = 1;
22 $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
23 bless { cb => $cb || *close, ctx => $ctx }, $class;
27 my ($class, $ctx, $code, $cb) = @_;
28 [ $code, [ 'Content-Type', 'application/atom+xml' ],
29 $class->new($ctx, $cb) ]
32 # called once for each message by PSGI server
35 if (my $middle = $self->{cb}) {
36 my $mime = $middle->();
37 return feed_entry($self, $mime) if $mime;
39 delete $self->{cb} ? '</feed>' : undef;
46 $title =~ tr/\t\n / /s; # squeeze spaces
47 # try to avoid the type attribute in title:
48 $title = ascii_html($title);
49 my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
50 "<title$type>$title</title>";
54 my ($ctx, $title) = @_;
55 my $ibx = $ctx->{-inbox};
56 my $base_url = $ctx->{feed_base_url};
57 my $search_q = $ctx->{search_query};
58 my $self_url = $base_url;
59 my $mid = $ctx->{mid};
60 if (defined $mid) { # per-thread
61 $self_url .= mid_escape($mid).'/t.atom';
62 } elsif (defined $search_q) {
63 my $query = $search_q->{'q'};
64 $title = title_tag("$query - search results");
65 $base_url .= '?' . $search_q->qs_html(x => undef);
66 $self_url .= '?' . $search_q->qs_html;
68 $title = title_tag($ibx->description);
69 $self_url .= 'new.atom';
71 my $mtime = (stat($ibx->{mainrepo}))[9] || time;
73 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
74 qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
75 qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
77 qq(<link\nrel="alternate"\ntype="text/html") .
78 qq(\nhref="$base_url"/>) .
79 qq(<link\nrel="self"\nhref="$self_url"/>) .
80 qq(<id>mailto:$ibx->{-primary_address}</id>) .
81 feed_updated(gmtime($mtime));
86 utf8::encode($mid); # really screwed up In-Reply-To fields exist
87 $mid = sha1_hex($mid);
89 my (@uuid5) = ($mid =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
90 'urn:uuid:' . join('-', @uuid5);
93 # returns undef or string
95 my ($self, $mime) = @_;
96 my $ctx = $self->{ctx};
97 my $hdr = $mime->header_obj;
98 my $mid = mid_clean($hdr->header_raw('Message-ID'));
99 my $irt = PublicInbox::View::in_reply_to($hdr);
100 my $uuid = mid2uuid($mid);
101 my $base = $ctx->{feed_base_url};
103 my $irt_uuid = mid2uuid($irt);
104 $irt = mid_escape($irt);
105 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
106 qq(href="$base$irt/"/>);
110 my $href = $base . mid_escape($mid) . '/';
111 my $date = $hdr->header('Date');
112 my @t = eval { strptime($date) } if defined $date;
113 @t = gmtime(time) unless scalar @t;
114 my $updated = feed_updated(@t);
116 my $title = $hdr->header('Subject');
117 $title = '(no subject)' unless defined $title && $title ne '';
118 $title = title_tag($title);
120 my $from = $hdr->header('From') or return;
121 my ($email) = PublicInbox::Address::emails($from);
122 my $name = join(', ',PublicInbox::Address::names($from));
123 $name = ascii_html($name);
124 $email = ascii_html($email);
127 if (delete $ctx->{emit_header}) {
128 $s .= atom_header($ctx, $title);
130 $s .= "<entry><author><name>$name</name><email>$email</email>" .
131 "</author>$title$updated" .
132 qq{<content\ntype="xhtml">} .
133 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
134 qq(<pre\nstyle="white-space:pre-wrap">) .
135 PublicInbox::View::multipart_text_as_html($mime, $href) .
137 qq!</div></content><link\nhref="$href"/>!.
138 "<id>$uuid</id>$irt</entry>";
142 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';