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 # FIXME: locale-independence:
10 use POSIX qw(strftime);
11 use Date::Parse qw(strptime);
12 use Digest::SHA qw(sha1_hex);
13 use PublicInbox::Address;
14 use PublicInbox::Hval qw(ascii_html);
15 use PublicInbox::MID qw/mid_clean mid_escape/;
17 # called by PSGI server after getline:
21 my ($class, $ctx, $cb) = @_;
22 $ctx->{emit_header} = 1;
23 $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
24 bless { cb => $cb || *close, ctx => $ctx }, $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 $mime = $middle->();
38 return feed_entry($self, $mime) if $mime;
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>";
55 my ($ctx, $title) = @_;
56 my $ibx = $ctx->{-inbox};
57 my $base_url = $ctx->{feed_base_url};
58 my $search_q = $ctx->{search_query};
59 my $self_url = $base_url;
60 my $mid = $ctx->{mid};
61 if (defined $mid) { # per-thread
62 $self_url .= mid_escape($mid).'/t.atom';
63 } elsif (defined $search_q) {
64 my $query = $search_q->{'q'};
65 $title = title_tag("$query - search results");
66 $base_url .= '?' . $search_q->qs_html(x => undef);
67 $self_url .= '?' . $search_q->qs_html;
69 $title = title_tag($ibx->description);
70 $self_url .= 'new.atom';
72 my $mtime = (stat($ibx->{mainrepo}))[9] || time;
74 qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
75 qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
76 qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
78 qq(<link\nrel="alternate"\ntype="text/html") .
79 qq(\nhref="$base_url"/>) .
80 qq(<link\nrel="self"\nhref="$self_url"/>) .
81 qq(<id>mailto:$ibx->{-primary_address}</id>) .
82 feed_updated(gmtime($mtime));
87 utf8::encode($mid); # really screwed up In-Reply-To fields exist
88 $mid = sha1_hex($mid);
90 my (@uuid5) = ($mid =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
91 'urn:uuid:' . join('-', @uuid5);
94 # returns undef or string
96 my ($self, $mime) = @_;
97 my $ctx = $self->{ctx};
98 my $hdr = $mime->header_obj;
99 my $mid = mid_clean($hdr->header_raw('Message-ID'));
100 my $irt = PublicInbox::View::in_reply_to($hdr);
101 my $uuid = mid2uuid($mid);
102 my $base = $ctx->{feed_base_url};
104 my $irt_uuid = mid2uuid($irt);
105 $irt = mid_escape($irt);
106 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
107 qq(href="$base$irt/"/>);
111 my $href = $base . mid_escape($mid) . '/';
112 my $date = $hdr->header('Date');
113 my @t = eval { strptime($date) } if defined $date;
114 @t = gmtime(time) unless scalar @t;
115 my $updated = feed_updated(@t);
117 my $title = $hdr->header('Subject');
118 $title = '(no subject)' unless defined $title && $title ne '';
119 $title = title_tag($title);
121 my $from = $hdr->header('From') or return;
122 my ($email) = PublicInbox::Address::emails($from);
123 my $name = join(', ',PublicInbox::Address::names($from));
124 $name = ascii_html($name);
125 $email = ascii_html($email);
128 if (delete $ctx->{emit_header}) {
129 $s .= atom_header($ctx, $title);
131 $s .= "<entry><author><name>$name</name><email>$email</email>" .
132 "</author>$title$updated" .
133 qq{<content\ntype="xhtml">} .
134 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
135 qq(<pre\nstyle="white-space:pre-wrap">) .
136 PublicInbox::View::multipart_text_as_html($mime, $href) .
138 qq!</div></content><link\nhref="$href"/>!.
139 "<id>$uuid</id>$irt</entry>";
143 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';