]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAtomStream.pm
5720384cf648f4f50c93f661f2ab467bf2392d77
[public-inbox.git] / lib / PublicInbox / WwwAtomStream.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Atom body stream for which yields getline+close methods
5 package PublicInbox::WwwAtomStream;
6 use strict;
7 use warnings;
8
9 # FIXME: locale-independence:
10 use POSIX qw(strftime);
11 use Date::Parse qw(strptime);
12
13 use PublicInbox::Address;
14 use PublicInbox::Hval qw(ascii_html);
15 use PublicInbox::MID qw/mid_clean mid2path mid_escape/;
16
17 # called by PSGI server after getline:
18 sub close {}
19
20 sub new {
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;
25 }
26
27 sub response {
28         my ($class, $ctx, $code, $cb) = @_;
29         [ $code, [ 'Content-Type', 'application/atom+xml' ],
30           $class->new($ctx, $cb) ]
31 }
32
33 # called once for each message by PSGI server
34 sub getline {
35         my ($self) = @_;
36         if (my $middle = $self->{cb}) {
37                 my $mime = $middle->();
38                 return feed_entry($self, $mime) if $mime;
39         }
40         delete $self->{cb} ? '</feed>' : undef;
41 }
42
43 # private
44
45 sub title_tag {
46         my ($title) = @_;
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>";
52 }
53
54 sub atom_header {
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;
68         } else {
69                 $title = title_tag($ibx->description);
70                 $self_url .= 'new.atom';
71         }
72         my $mtime = (stat($ibx->{mainrepo}))[9] || time;
73
74         qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
75         qq{<feed\nxmlns="http://www.w3.org/2005/Atom">} .
76         qq{$title} .
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));
82 }
83
84 # returns undef or string
85 sub feed_entry {
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'));
90
91         my $uuid = mid2path($mid);
92         $uuid =~ tr!/!!d;
93         my $h = '[a-f0-9]';
94         my (@uuid5) = ($uuid =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
95         $uuid = 'urn:uuid:' . join('-', @uuid5);
96
97         $mid = PublicInbox::Hval->new_msgid($mid);
98         my $href = $ctx->{feed_base_url} . $mid->{href}. '/';
99
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);
104
105         my $title = $hdr->header('Subject');
106         $title = '(no subject)' unless defined $title && $title ne '';
107         $title = title_tag($title);
108
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);
114
115         my $s = '';
116         if (delete $ctx->{emit_header}) {
117                 $s .= atom_header($ctx, $title);
118         }
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) .
125                 '</pre>' .
126                 qq!</div></content><link\nhref="$href"/>!.
127                 "<id>$uuid</id></entry>";
128 }
129
130 sub feed_updated {
131         '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';
132 }
133
134 1;