| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =encoding utf8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | App::Phoebe::WebComments - allow comments on a Phoebe wiki via the web | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This extension allows visitors on the web to add comments. | ||
| 26 | |||
| 27 | Comments are appended to a "comments page". For every page I<Foo> the comments | ||
| 28 | are found on I<Comments on Foo>. This prefix is fixed, currently. | ||
| 29 | |||
| 30 | On the comments page, each new comment starts with the character LEFT SPEECH | ||
| 31 | BUBBLE (🗨). This character is fixed, currently. | ||
| 32 | |||
| 33 | There is no configuration. Simply add it to your F<config> file: | ||
| 34 | |||
| 35 | use App::Phoebe::WebComments; | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | package App::Phoebe::WebComments; | ||
| 40 | use App::Phoebe qw(@footer @extensions @request_handlers $server $log port space | ||
| 41 | host_regex space_regex quote_html wiki_dir with_lock | ||
| 42 | bogus_hash to_url); | ||
| 43 | use App::Phoebe::Web qw(handle_http_header http_error); | ||
| 44 | use Modern::Perl; | ||
| 45 | use URI::Escape; | ||
| 46 | use File::Slurper qw(write_text); | ||
| 47 | use Encode qw(decode_utf8 encode_utf8); | ||
| 48 | use File::Slurper qw(read_text); | ||
| 49 | use utf8; | ||
| 50 | |||
| 51 | push(@footer, \&add_comment_web_link_to_footer); | ||
| 52 | |||
| 53 | sub add_comment_web_link_to_footer { | ||
| 54 | my ($self, $host, $space, $id, $revision, $scheme) = @_; | ||
| 55 | # only leave comments on current comment pages | ||
| 56 | return "" if $revision; | ||
| 57 | $space = "/" . uri_escape_utf8($space) if $space; | ||
| 58 | $space //= ""; | ||
| 59 | return "=> $space/page/" . uri_escape_utf8("Comments on $id") . " Comments" | ||
| 60 | if $id !~ /^Comments on / and not grep { $_ eq \&add_comment_link_to_footer } @footer; | ||
| 61 | return "=> $space/do/comment/" . uri_escape_utf8($id) . " Leave a short comment" if $scheme eq "html"; | ||
| 62 | } | ||
| 63 | |||
| 64 | unshift(@request_handlers, '^POST .* HTTP/1\.[01]$' => \&handle_http_header); | ||
| 65 | |||
| 66 | push(@extensions, \&process_comment_requests_via_http); | ||
| 67 | |||
| 68 | sub process_comment_requests_via_http { | ||
| 69 | my ($stream, $url, $headers, $buffer) = @_; | ||
| 70 | my $hosts = host_regex(); | ||
| 71 | my $spaces = space_regex(); | ||
| 72 | my $port = port($stream); | ||
| 73 | my ($host, $space, $id, $token, $query); | ||
| 74 | if (($space, $id) = $url =~ m!^GET (?:/($spaces))?/do/comment/([^/#?]+) HTTP/1\.[01]$! | ||
| 75 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 76 | serve_comment_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id))); | ||
| 77 | return 1; | ||
| 78 | } elsif (($space, $id) = $url =~ m!^POST (?:/($spaces))?/do/comment/([^/#?]+) HTTP/1\.[01]$! | ||
| 79 | and ($host) = $headers->{host} =~ m!^($hosts)(?::$port)$!) { | ||
| 80 | append_comment_via_http($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $buffer); | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub serve_comment_via_http { | ||
| 87 | my ($stream, $host, $space, $id) = @_; | ||
| 88 | $log->info("Serve comments for $id via HTTP"); | ||
| 89 | $stream->write("HTTP/1.1 200 OK\r\n"); | ||
| 90 | $stream->write("Content-Type: text/html\r\n"); | ||
| 91 | $stream->write("\r\n"); | ||
| 92 | $stream->write("<!DOCTYPE html>\r\n"); | ||
| 93 | $stream->write("<html>\r\n"); | ||
| 94 | $stream->write("<head>\r\n"); | ||
| 95 | $stream->write("<meta charset=\"utf-8\">\r\n"); | ||
| 96 | $stream->write(encode_utf8 "<title>" . quote_html($id) . "</title>\r\n"); | ||
| 97 | $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\r\n"); | ||
| 98 | $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\r\n"); | ||
| 99 | $stream->write("</head>\r\n"); | ||
| 100 | $stream->write("<body>\r\n"); | ||
| 101 | $stream->write(encode_utf8 "<h1>" . quote_html($id) . "</h1>\r\n"); | ||
| 102 | $stream->write("<form method=\"POST\">\r\n"); | ||
| 103 | $stream->write("<p><label for=\"token\">Token:</label>\r\n"); | ||
| 104 | $stream->write("<br><input type=\"text\" id=\"token\" name=\"token\" required>\r\n"); | ||
| 105 | $stream->write("<p><label for=\"comment\">Comment:</label>\r\n"); | ||
| 106 | $stream->write("<br><textarea style=\"width: 100%; height: 10em;\" id=\"comment\" name=\"comment\" required></textarea>\r\n"); | ||
| 107 | $stream->write("<p><input type=\"submit\" value=\"Save\">\r\n"); | ||
| 108 | $stream->write("</form>\r\n"); | ||
| 109 | $stream->write("</body>\r\n"); | ||
| 110 | $stream->write("</html>\r\n"); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub append_comment_via_http { | ||
| 114 | my ($stream, $host, $space, $id, $buffer) = @_; | ||
| 115 | $log->info("Save comments for $id via HTTP"); | ||
| 116 | my %params; | ||
| 117 | for (split(/&/, $buffer)) { | ||
| 118 | my ($key, $value) = map { s/\+/ /g; decode_utf8(uri_unescape($_)) } split(/=/, $_, 2); | ||
| 119 | $params{$key} = $value; | ||
| 120 | } | ||
| 121 | $log->debug("Parameters: " . join(", ", map { "$_ => '$params{$_}'" } keys %params)); | ||
| 122 | my $token = quotemeta($params{token}||""); | ||
| 123 | my @tokens = @{$server->{wiki_token}}; | ||
| 124 | push(@tokens, @{$server->{wiki_space_token}->{$space}}) | ||
| 125 | if $space and $server->{wiki_space_token}->{$space}; | ||
| 126 | return http_error($stream, "Token required") if not $token and @tokens; | ||
| 127 | return http_error($stream, "Wrong token") if not grep(/^$token$/, @tokens); | ||
| 128 | my $comment = $params{comment}; | ||
| 129 | return http_error($stream, "Comment required") if not $comment; | ||
| 130 | my $dir = wiki_dir($host, $space); | ||
| 131 | my $file = "$dir/page/$id.gmi"; | ||
| 132 | my $text; | ||
| 133 | if (-e $file) { | ||
| 134 | $text = read_text($file) . "\n\n🗨 " . $comment; | ||
| 135 | } else { | ||
| 136 | $text = $comment; | ||
| 137 | } | ||
| 138 | # We don't need to close the stream because this is called via process_gemini | ||
| 139 | # which always closes the stream in the end. | ||
| 140 | with_lock($stream, $host, $space, sub { write_page_for_http($stream, $host, $space, $id, $text) } ); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub write_page_for_http { | ||
| 144 | my $stream = shift; | ||
| 145 | my $host = shift; | ||
| 146 | my $space = shift; | ||
| 147 | my $id = shift; | ||
| 148 | my $text = shift; | ||
| 149 | $log->info("Writing page $id"); | ||
| 150 | my $dir = wiki_dir($host, $space); | ||
| 151 | my $file = "$dir/page/$id.gmi"; | ||
| 152 | my $revision = 0; | ||
| 153 | if (-e $file) { | ||
| 154 | my $old = read_text($file); | ||
| 155 | if ($old eq $text) { | ||
| 156 | $log->info("$id is unchanged"); | ||
| 157 | my $message = to_url($stream, $host, $space, "page/$id", "https"); | ||
| 158 | $stream->write("HTTP/1.1 302 Found\r\n"); | ||
| 159 | $stream->write("Location: $message\r\n"); | ||
| 160 | $stream->write("\r\n"); | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | mkdir "$dir/keep" unless -d "$dir/keep"; | ||
| 164 | if (-d "$dir/keep/$id") { | ||
| 165 | foreach (read_dir("$dir/keep/$id")) { | ||
| 166 | $revision = $1 if m/^(\d+)\.gmi$/ and $1 > $revision; | ||
| 167 | } | ||
| 168 | $revision++; | ||
| 169 | } else { | ||
| 170 | mkdir "$dir/keep/$id"; | ||
| 171 | $revision = 1; | ||
| 172 | } | ||
| 173 | rename $file, "$dir/keep/$id/$revision.gmi"; | ||
| 174 | } else { | ||
| 175 | my $index = "$dir/index"; | ||
| 176 | if (not open(my $fh, ">>:encoding(UTF-8)", $index)) { | ||
| 177 | $log->error("Cannot write index $index: $!"); | ||
| 178 | return http_error($stream, "Unable to write index"); | ||
| 179 | } else { | ||
| 180 | say $fh $id; | ||
| 181 | close($fh); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | my $changes = "$dir/changes.log"; | ||
| 185 | if (not open(my $fh, ">>:encoding(UTF-8)", $changes)) { | ||
| 186 | $log->error("Cannot write log $changes: $!"); | ||
| 187 | return http_error($stream, "Unable to write log"); | ||
| 188 | } else { | ||
| 189 | my $peerhost = $stream->handle->peerhost; | ||
| 190 | say $fh join("\x1f", scalar(time), $id, $revision + 1, bogus_hash($peerhost)); | ||
| 191 | close($fh); | ||
| 192 | } | ||
| 193 | mkdir "$dir/page" unless -d "$dir/page"; | ||
| 194 | eval { write_text($file, $text) }; | ||
| 195 | if ($@) { | ||
| 196 | $log->error("Unable to save $id: $@"); | ||
| 197 | return http_error($stream, "Unable to save $id"); | ||
| 198 | } else { | ||
| 199 | $log->info("Wrote $id"); | ||
| 200 | my $message = to_url($stream, $host, $space, "page/$id", "https"); | ||
| 201 | $stream->write("HTTP/1.1 302 Found\r\n"); | ||
| 202 | $stream->write("Location: $message\r\n"); | ||
| 203 | $stream->write("\r\n"); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | 1; |