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);
13 use PublicInbox::Address;
14 use PublicInbox::Hval qw(ascii_html);
15 use PublicInbox::MID qw/mid_clean mid2path 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">} .
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));
84 # returns undef or string
86 my ($self, $mime) = @_;
87 my $ctx = $self->{ctx};
88 my $hdr = $mime->header_obj;
89 my $mid = mid_clean($hdr->header_raw('Message-ID'));
91 my $uuid = mid2path($mid);
94 my (@uuid5) = ($uuid =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
95 $uuid = 'urn:uuid:' . join('-', @uuid5);
97 $mid = PublicInbox::Hval->new_msgid($mid);
98 my $href = $ctx->{feed_base_url} . $mid->{href}. '/';
100 my $date = $hdr->header('Date');
101 my @t = eval { strptime($date) } if defined $date;
102 @t = gmtime(time) unless scalar @t;
103 my $updated = feed_updated(@t);
105 my $title = $hdr->header('Subject');
106 $title = '(no subject)' unless defined $title && $title ne '';
107 $title = title_tag($title);
109 my $from = $hdr->header('From') or return;
110 my ($email) = PublicInbox::Address::emails($from);
111 my $name = join(', ',PublicInbox::Address::names($from));
112 $name = ascii_html($name);
113 $email = ascii_html($email);
116 if (delete $ctx->{emit_header}) {
117 $s .= atom_header($ctx, $title);
119 $s .= "<entry><author><name>$name</name><email>$email</email>" .
120 "</author>$title$updated" .
121 qq{<content\ntype="xhtml">} .
122 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
123 qq(<pre\nstyle="white-space:pre-wrap">) .
124 PublicInbox::View::multipart_text_as_html($mime, $href) .
126 qq!</div></content><link\nhref="$href"/>!.
127 "<id>$uuid</id></entry>";
131 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';