]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAtomStream.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / WwwAtomStream.pm
1 # Copyright (C) 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 HTTP responses
5 # See PublicInbox::GzipFilter for details.
6 package PublicInbox::WwwAtomStream;
7 use strict;
8 use parent 'PublicInbox::GzipFilter';
9
10 use POSIX qw(strftime);
11 use PublicInbox::SHA qw(sha1_hex);
12 use PublicInbox::Address;
13 use PublicInbox::Hval qw(ascii_html mid_href);
14 use PublicInbox::MsgTime qw(msg_timestamp);
15
16 sub new {
17         my ($class, $ctx, $cb) = @_;
18         $ctx->{feed_base_url} = $ctx->{ibx}->base_url($ctx->{env});
19         $ctx->{-spfx} = $ctx->{feed_base_url} if $ctx->{ibx}->{coderepo};
20         $ctx->{cb} = $cb || \&PublicInbox::GzipFilter::close;
21         $ctx->{emit_header} = 1;
22         bless $ctx, $class;
23 }
24
25 sub async_next ($) {
26         my ($http) = @_; # PublicInbox::HTTP
27         my $ctx = $http->{forward} or return;
28         eval {
29                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
30                         $ctx->smsg_blob($smsg);
31                 } else {
32                         $ctx->write('</feed>');
33                         $ctx->close;
34                 }
35         };
36         warn "E: $@" if $@;
37 }
38
39 sub async_eml { # for async_blob_cb
40         my ($ctx, $eml) = @_;
41         my $smsg = delete $ctx->{smsg};
42         $smsg->{mid} // $smsg->populate($eml);
43         $ctx->write(feed_entry($ctx, $smsg, $eml));
44 }
45
46 sub response {
47         my ($class, $ctx, $cb) = @_;
48         my $res_hdr = [ 'Content-Type' => 'application/atom+xml' ];
49         $class->new($ctx, $cb);
50         $ctx->psgi_response(200, $res_hdr);
51 }
52
53 # called once for each message by PSGI server
54 sub getline {
55         my ($self) = @_;
56         my $cb = $self->{cb} or return;
57         while (my $smsg = $cb->($self)) {
58                 my $eml = $self->{ibx}->smsg_eml($smsg) or next;
59                 return $self->translate(feed_entry($self, $smsg, $eml));
60         }
61         delete $self->{cb};
62         $self->zflush('</feed>');
63 }
64
65 # private
66
67 sub title_tag {
68         my ($title) = @_;
69         $title =~ tr/\t\n / /s; # squeeze spaces
70         # try to avoid the type attribute in title:
71         $title = ascii_html($title);
72         my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
73         "<title$type>$title</title>";
74 }
75
76 sub to_uuid ($) {
77         my ($any) = @_;
78         utf8::encode($any); # really screwed up In-Reply-To fields exist
79         $any = sha1_hex($any);
80         my $h = '[a-f0-9]';
81         my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
82         'urn:uuid:' . join('-', @uuid5);
83 }
84
85 sub atom_header {
86         my ($ctx, $title) = @_;
87         my $ibx = $ctx->{ibx};
88         my $base_url = $ctx->{feed_base_url};
89         my $search_q = $ctx->{search_query};
90         my $self_url = $base_url;
91         my $mid = $ctx->{mid};
92         my $page_id;
93         if (defined $mid) { # per-thread
94                 $self_url .= mid_href($mid).'/t.atom';
95                 $page_id = to_uuid("t\n".$mid)
96         } elsif (defined $search_q) {
97                 my $query = $search_q->{'q'};
98                 $title = title_tag("$query - search results");
99                 $base_url .= '?' . $search_q->qs_html(x => undef);
100                 $self_url .= '?' . $search_q->qs_html;
101                 $page_id = to_uuid("q\n".$query);
102         } else {
103                 $title = title_tag($ibx->description);
104                 $self_url .= 'new.atom';
105                 if (defined(my $addr = $ibx->{-primary_address})) {
106                         $page_id = "mailto:$addr";
107                 } else {
108                         $page_id = to_uuid($self_url);
109                 }
110         }
111         qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
112         qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
113         qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
114         qq{$title} .
115         qq(<link\nrel="alternate"\ntype="text/html") .
116                 qq(\nhref="$base_url"/>) .
117         qq(<link\nrel="self"\nhref="$self_url"/>) .
118         qq(<id>$page_id</id>) .
119         feed_updated($ibx->modified);
120 }
121
122 # returns undef or string
123 sub feed_entry {
124         my ($ctx, $smsg, $eml) = @_;
125         my $mid = $smsg->{mid};
126         my $irt = PublicInbox::View::in_reply_to($eml);
127         my $uuid = to_uuid($mid);
128         my $base = $ctx->{feed_base_url};
129         if (defined $irt) {
130                 my $irt_uuid = to_uuid($irt);
131                 $irt = mid_href($irt);
132                 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
133                         qq(href="$base$irt/"/>);
134         } else {
135                 $irt = '';
136         }
137         my $href = $base . mid_href($mid) . '/';
138         my $updated = feed_updated(msg_timestamp($eml));
139
140         my $title = $eml->header('Subject');
141         $title = '(no subject)' unless defined $title && $title ne '';
142         $title = title_tag($title);
143
144         my $from = $eml->header('From') // $eml->header('Sender') //
145                 $ctx->{ibx}->{-primary_address};
146         my ($email) = PublicInbox::Address::emails($from);
147         my $name = ascii_html(join(', ', PublicInbox::Address::names($from)));
148         $email = ascii_html($email // $ctx->{ibx}->{-primary_address});
149
150         print { $ctx->zfh }
151                 (delete($ctx->{emit_header}) ? atom_header($ctx, $title) : ''),
152                 "<entry><author><name>$name</name><email>$email</email>" .
153                 "</author>$title$updated" .
154                 qq(<link\nhref="$href"/>) .
155                 "<id>$uuid</id>$irt" .
156                 qq{<content\ntype="xhtml">} .
157                 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
158                 qq(<pre\nstyle="white-space:pre-wrap">);
159         $ctx->{mhref} = $href;
160         $ctx->{changed_href} = "${href}#related";
161         $eml->each_part(\&PublicInbox::View::add_text_body, $ctx, 1);
162         '</pre></div></content></entry>';
163 }
164
165 sub feed_updated {
166         my ($t) = @_;
167         '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($t)) . '</updated>';
168 }
169
170 1;