| 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 | =head1 NAME | ||
| 18 | |||
| 19 | App::Phoebe::MokuPona - serve files from moku pona | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This serves files from your moku pona directory. See L<App::mokupona>. | ||
| 24 | |||
| 25 | If you need to change the directory (defaults to C<$HOME/.moku-pona>), or if you | ||
| 26 | need to change the host (defaults to the first one), use the following for your | ||
| 27 | F<config> file: | ||
| 28 | |||
| 29 | package App::Phoebe::MokuPona; | ||
| 30 | our $dir = "/home/alex/.moku-pona"; | ||
| 31 | our $host = "alexschroeder.ch"; | ||
| 32 | use App::Phoebe::MokuPona; | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | package App::Phoebe::MokuPona; | ||
| 37 | use App::Phoebe qw(@extensions $server $log success result port); | ||
| 38 | use Modern::Perl; | ||
| 39 | use URI::Escape; | ||
| 40 | use File::Slurper qw(read_text); | ||
| 41 | use Encode qw(encode_utf8 decode_utf8); | ||
| 42 | # moku pona | ||
| 43 | |||
| 44 | our $dir ||= "$ENV{HOME}/.moku-pona"; | ||
| 45 | our $host ||= (keys %{$server->{host}})[0]; | ||
| 46 | |||
| 47 | push(@extensions, \&mokupona); | ||
| 48 | |||
| 49 | sub mokupona { | ||
| 50 | my $stream = shift; | ||
| 51 | my $url = shift; | ||
| 52 | my $port = port($stream); | ||
| 53 | if ($url =~ m!^gemini://$host(?::$port)?/do/moku-pona$!) { | ||
| 54 | result($stream, "31", "gemini://$host/do/moku-pona/updates.txt"); | ||
| 55 | return 1; | ||
| 56 | } elsif ($url =~ m!^gemini://$host(?::$port)?/do/moku-pona/([^/]+)$!) { | ||
| 57 | my $file = decode_utf8(uri_unescape($1)); | ||
| 58 | if (-f "$dir/$file") { | ||
| 59 | success($stream); | ||
| 60 | $stream->write(encode_utf8 read_text("$dir/$file")); | ||
| 61 | } else { | ||
| 62 | result($stream, "40", "Cannot read $dir/$file"); | ||
| 63 | } | ||
| 64 | return 1; | ||
| 65 | } | ||
| 66 | return 0; | ||
| 67 | } |