| 1 | #!/home/alex/perl5/perlbrew/perls/perl-5.32.0/bin/perl | ||||||||||||||||||||||||||||||||||
| 2 | # Copyright (C) 2018–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 General Public License as published by the Free Software | ||||||||||||||||||||||||||||||||||
| 6 | # Foundation, either version 3 of the License, or (at your option) any later | ||||||||||||||||||||||||||||||||||
| 7 | # 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 General Public License for more details. | ||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License along with | ||||||||||||||||||||||||||||||||||
| 14 | # this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | =head1 Moku Pona | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | Moku Pona is a Gemini based feed reader. It can monitor URLs to feeds or regular | ||||||||||||||||||||||||||||||||||
| 21 | pages for changes and keeps and updated list of these in a Gemini list. Moku | ||||||||||||||||||||||||||||||||||
| 22 | Pona knows how to fetch Gopher URLs, Gemini URLs, and regular web URLs. | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | You manage your subscriptions using the command-line, with Moku Pona. | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | You serve the resulting file using a Gemini server. | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | You read it all using your Gemini client. | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | =head2 Limitations | ||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | When Moku Pona isn't watching a feed it can only detect changes on a page. Thus, | ||||||||||||||||||||||||||||||||||
| 33 | if there is an item that points to a phlog or blog, that's great. Sometimes | ||||||||||||||||||||||||||||||||||
| 34 | people put their phlog in a folder per year. If the Gopher menu lists each | ||||||||||||||||||||||||||||||||||
| 35 | folder and a date with the latest change, then that's great, you can use it. | ||||||||||||||||||||||||||||||||||
| 36 | Without it, you're in trouble: you need to subscribe to the item for the current | ||||||||||||||||||||||||||||||||||
| 37 | year in order to see changes, but when the next year comes around, you're | ||||||||||||||||||||||||||||||||||
| 38 | subscribed to the wrong item. Sometimes you're lucky and there will be a menu | ||||||||||||||||||||||||||||||||||
| 39 | somewhere with a timestamp for the last change. Use that instead. Good luck! | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | =head2 License | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | GNU Affero General Public License | ||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | =head2 Installation | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | Using C<cpan>: | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | cpan App::mokupona | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | Manual install: | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | perl Makefile.PL | ||||||||||||||||||||||||||||||||||
| 54 | make | ||||||||||||||||||||||||||||||||||
| 55 | make install | ||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | =head2 Dependencies | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | There are some Perl dependencies you need to satisfy in order to run this | ||||||||||||||||||||||||||||||||||
| 60 | program: | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | =over | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | =item L<Modern::Perl>, or C<libmodern-perl-perl> | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | =item L<Mojo::IOLoop>, or C<libmojolicious-perl> | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | =item L<XML::LibXML>, or C<libxml-libxml-perl> | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | =item L<URI::Escape>, or C<liburi-escape-xs-perl> | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | =item L<Encode::Locale>, or c<libencode-locale-perl> | ||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | =back | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 9 | ▶ | use Modern::Perl '2018'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 79 | 9 | ▶ | use File::Copy qw(copy); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 80 | 9 | ▶ | use Encode::Locale qw(decode_argv); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 81 | 9 | ▶ | use Encode qw(decode_utf8); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 82 | 9 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 83 | 9 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 84 | 9 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 85 | 9 | ▶ | use List::Util qw(none); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 9 | decode_argv(); | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | 9 | ▶ | if (-t) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 90 | 0 | binmode(STDIN, ":encoding(console_in)"); | |||||||||||||||||||||||||||||||||
| 91 | 0 | binmode(STDOUT, ":encoding(console_out)"); | |||||||||||||||||||||||||||||||||
| 92 | 0 | binmode(STDERR, ":encoding(console_out)"); | |||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | =head2 The Data Directory | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | Moku Pona keeps the list of URLs you are subscribed to in directory. It's | ||||||||||||||||||||||||||||||||||
| 98 | probably C<~/.moku-pona> on your system. | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | =over | ||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | =item If you have the C<MOKU_PONA> environment variable set, then that's your data | ||||||||||||||||||||||||||||||||||
| 103 | directory. | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | =item If you have the C<XDG_DATA_HOME> environment variable set, then your data | ||||||||||||||||||||||||||||||||||
| 106 | directory is F<$XDG_DATA_HOME/moku-pona>. | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | =item If you you have the C<HOME> environment variable set, and you have a | ||||||||||||||||||||||||||||||||||
| 109 | F<$HOME/.local> directory, then your data directory is | ||||||||||||||||||||||||||||||||||
| 110 | F<$HOME/.local/moku-pona>. | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | =item If you have the C<HOME> environment variable set, then your data directory | ||||||||||||||||||||||||||||||||||
| 113 | is F<$HOME/.moku-pona>. | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | =item If you have the C<APPDATA> environment variable set (Windows), then your | ||||||||||||||||||||||||||||||||||
| 116 | data directory is F<$APPDATA/moku-pona>. | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | =item The last option is to have the C<LOGDIR> environment variable set. | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | =back | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | The data directory contains a copy of the latest resources. The names of these | ||||||||||||||||||||||||||||||||||
| 123 | cache files are simply the URL with all the slashes replaced by a hyphen. | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | 9 | our $data_dir = data_dir(); | |||||||||||||||||||||||||||||||||
| 128 | # say "Using $data_dir"; | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | 9 | ▶ | sub data_dir { | ||||||||||||||||||||||||||||||||
|
Subroutine
data_dir: called CC=18 SCAR=42.3
| |||||||||||||||||||||||||||||||||||
| 131 | 9 | ▶ | return $ENV{MOKU_PONA} if $ENV{MOKU_PONA}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | # find an existing directory | ||||||||||||||||||||||||||||||||||
| 133 | 9 | ▶ | return $ENV{XDG_DATA_HOME} . '/moku-pona' if $ENV{XDG_DATA_HOME} and -d $ENV{XDG_DATA_HOME} . '/moku-pona'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"XDG_DATA_HOME"} and -d $ENV{"XDG_DATA_HOME"} . '/moku-pona'
MC/DC0%0/2
$ENV{"XDG_DATA_HOME"} and -d $ENV{"XDG_DATA_HOME"} . '/moku-pona'
$ENV{"XDG_DATA_HOME"}
-d $ENV{"XDG_DATA_HOME"} . '/moku-pona'
Truth table33%1/3
A and B
A= $ENV{"XDG_DATA_HOME"}B= -d $ENV{"XDG_DATA_HOME"} . '/moku-pona'
| |||||||||||||||||||||||||||||||||||
| 134 | 9 | ▶ | return $ENV{HOME} . '/.local/moku-pona' if $ENV{HOME} and -d $ENV{HOME} . '/.local/moku-pona'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"HOME"} and -d $ENV{"HOME"} . '/.local/moku-pona'
MC/DC0%0/2
$ENV{"HOME"} and -d $ENV{"HOME"} . '/.local/moku-pona'
$ENV{"HOME"}
-d $ENV{"HOME"} . '/.local/moku-pona'
Truth table33%1/3
A and B
A= $ENV{"HOME"}B= -d $ENV{"HOME"} . '/.local/moku-pona'
| |||||||||||||||||||||||||||||||||||
| 135 | 9 | ▶ | return $ENV{HOME} . '/.moku-pona' if $ENV{HOME} and -d $ENV{HOME} . '/.moku-pona'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"HOME"} and -d $ENV{"HOME"} . '/.moku-pona'
MC/DC0%0/2
$ENV{"HOME"} and -d $ENV{"HOME"} . '/.moku-pona'
$ENV{"HOME"}
-d $ENV{"HOME"} . '/.moku-pona'
Truth table33%1/3
A and B
A= $ENV{"HOME"}B= -d $ENV{"HOME"} . '/.moku-pona'
| |||||||||||||||||||||||||||||||||||
| 136 | 9 | ▶ | return $ENV{APPDATA} . '/moku-pona' if $ENV{APPDATA} and -d $ENV{APPDATA} . '/.moku-pona'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"APPDATA"} and -d $ENV{"APPDATA"} . '/.moku-pona'
MC/DC0%0/2
$ENV{"APPDATA"} and -d $ENV{"APPDATA"} . '/.moku-pona'
$ENV{"APPDATA"}
-d $ENV{"APPDATA"} . '/.moku-pona'
Truth table33%1/3
A and B
A= $ENV{"APPDATA"}B= -d $ENV{"APPDATA"} . '/.moku-pona'
| |||||||||||||||||||||||||||||||||||
| 137 | 9 | ▶ | return $ENV{LOGDIR} . '/.moku-pona' if $ENV{LOGDIR} and -d $ENV{LOGDIR} . '/.moku-pona'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"LOGDIR"} and -d $ENV{"LOGDIR"} . '/.moku-pona'
MC/DC0%0/2
$ENV{"LOGDIR"} and -d $ENV{"LOGDIR"} . '/.moku-pona'
$ENV{"LOGDIR"}
-d $ENV{"LOGDIR"} . '/.moku-pona'
Truth table33%1/3
A and B
A= $ENV{"LOGDIR"}B= -d $ENV{"LOGDIR"} . '/.moku-pona'
| |||||||||||||||||||||||||||||||||||
| 138 | # or use a new one | ||||||||||||||||||||||||||||||||||
| 139 | 9 | ▶ | return $ENV{XDG_DATA_HOME} . '/moku-pona' if $ENV{XDG_DATA_HOME}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | 9 | ▶ | return $ENV{HOME} . '/.local/moku-pona' if $ENV{HOME} and -d $ENV{HOME} . '/.local'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"HOME"} and -d $ENV{"HOME"} . '/.local'
MC/DC0%0/2
$ENV{"HOME"} and -d $ENV{"HOME"} . '/.local'
$ENV{"HOME"}
-d $ENV{"HOME"} . '/.local'
Truth table33%1/3
A and B
A= $ENV{"HOME"}B= -d $ENV{"HOME"} . '/.local'
| |||||||||||||||||||||||||||||||||||
| 141 | 9 | ▶ | return $ENV{HOME} . '/.moku-pona' if $ENV{HOME}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return $ENV{APPDATA} . '/moku-pona' if $ENV{APPDATA}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | return $ENV{LOGDIR} . '/.moku-pona' if $ENV{LOGDIR}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 144 | 0 | die "Please set the MOKU_PONA environment variable to a directory name\n"; | |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | =pod | ||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | The C<sites.txt> file is a file containing a gemtext list of links, i.e. entries | ||||||||||||||||||||||||||||||||||
| 150 | such as these: | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | => gemini://alexschroeder.ch Alex Schroeder | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | 9 | our $site_list = $data_dir . '/sites.txt'; | |||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | =pod | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | The C<updates.txt> file is a file containing a gemtext list of links based on | ||||||||||||||||||||||||||||||||||
| 161 | C<sites.txt>, but with a timestamp of their last change, and with new updates | ||||||||||||||||||||||||||||||||||
| 162 | moved to the top. The ISO date is simply inserted after the URL: | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | => gemini://alexschroeder.ch 2020-11-07 Alex Schroeder | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | 9 | our $updated_list = $data_dir . '/updates.txt'; | |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||
| 170 | =pod | ||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | In order to be at least somewhat backwards compatible with Moku Pona versions | ||||||||||||||||||||||||||||||||||
| 173 | 1.1 and earlier, C<sites.txt> may contain Gopher menu items. These are converted | ||||||||||||||||||||||||||||||||||
| 174 | to Gemini URLs during processing and thus the C<updates.txt> file still contains | ||||||||||||||||||||||||||||||||||
| 175 | regular gemtext. | ||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||
| 177 | 1Alex Schroeder ⭾ ⭾ alexschroeder.ch ⭾ 70 | ||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||
| 181 | 27 | ▶ | sub convert { | ||||||||||||||||||||||||||||||||
|
Subroutine
convert: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||
| 182 | 27 | for (@_) { | |||||||||||||||||||||||||||||||||
| 183 | 49 | ▶ | next if /^=> /; # is already a gemini link | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 184 | 17 | my ($type, $desc, $selector, $host, $port) = /^([^\t])([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t\r]*)/; | |||||||||||||||||||||||||||||||||
| 185 | 17 | ▶ | if ($host and $port) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$host and $port
MC/DC50%1/2
$host and $port
$host
$port
Truth table66%2/3
A and B
A= $hostB= $port
| |||||||||||||||||||||||||||||||||||
| 186 | 13 | ▶ | $port //= 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$port //= 0
MC/DC0%0/1
$port //= 0
$port
Truth table50%1/2
A
A= $port
| |||||||||||||||||||||||||||||||||||
| 187 | 13 | $_ = "=> gopher://$host:$port/$type$selector $desc"; | |||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||
| 190 | 27 | return @_; | |||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||
| 193 | =pod | ||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||
| 195 | As was said above, however, the recommended format is the use of URLs. Moku Pona | ||||||||||||||||||||||||||||||||||
| 196 | supports Gemini, Gopher, and the web (gemini, gopher, gophers, http, and https | ||||||||||||||||||||||||||||||||||
| 197 | schemes). | ||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | sub query_gemini { | ||||||||||||||||||||||||||||||||
|
Subroutine
query_gemini: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||
| 202 | 0 | my $url = shift; | |||||||||||||||||||||||||||||||||
| 203 | 0 | my $responses = shift; | |||||||||||||||||||||||||||||||||
| 204 | 0 | my($scheme, $authority, $path, $query, $fragment) = | |||||||||||||||||||||||||||||||||
| 205 | $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|; | ||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | die "⚠ The URL '$url' must use the gemini scheme\n" unless $scheme and $scheme eq 'gemini'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$scheme and $scheme eq "gemini"
MC/DC0%0/2
$scheme and $scheme eq "gemini"
$scheme
$scheme eq "gemini"
Truth table0%0/3
A and B
A= $schemeB= $scheme eq "gemini"
| |||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | die "⚠ The URL '$url' must have an authority\n" unless $authority; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 208 | 0 | my ($host, $port) = split(/:/, $authority, 2); | |||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | $port //= 1965; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$port //= 1965
MC/DC0%0/1
$port //= 1965
$port
Truth table0%0/2
A
A= $port
| |||||||||||||||||||||||||||||||||||
| 210 | Mojo::IOLoop->client( | ||||||||||||||||||||||||||||||||||
| 211 | {port => $port, address => $host, tls => 1, | ||||||||||||||||||||||||||||||||||
| 212 | tls_options => { SSL_verify_mode => 0x00 }, timeout => 120} | ||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 214 | 0 | my ($loop, $err, $stream) = @_; | |||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 216 | 0 | warn "Cannot connect to $url: $err\n"; | |||||||||||||||||||||||||||||||||
| 217 | 0 | return; | |||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||
| 219 | 0 | $stream->timeout(300); # 5 min | |||||||||||||||||||||||||||||||||
| 220 | 0 | my $header; | |||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | $stream->on(read => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 222 | 0 | my ($stream, $bytes) = @_; | |||||||||||||||||||||||||||||||||
| 223 | 0 | $responses->{$url} .= $bytes; | |||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | $header = $responses->{$url} =~ s/^.*\r\n// unless $header}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 225 | 0 | $stream->write("$url\r\n")}) | |||||||||||||||||||||||||||||||||
| 226 | 0 | } | |||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||
| 228 | 12 | ▶ | sub query_gopher { | ||||||||||||||||||||||||||||||||
|
Subroutine
query_gopher: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||
| 229 | 12 | my $url = shift; | |||||||||||||||||||||||||||||||||
| 230 | 12 | my $responses = shift; | |||||||||||||||||||||||||||||||||
| 231 | 12 | my ($selector, $host, $port) = url_to_gopher($url); | |||||||||||||||||||||||||||||||||
| 232 | 12 | my $tls = $url =~ /^gophers/; | |||||||||||||||||||||||||||||||||
| 233 | Mojo::IOLoop->client( | ||||||||||||||||||||||||||||||||||
| 234 | {port => $port, address => $host, tls => $tls, timeout => 120 } | ||||||||||||||||||||||||||||||||||
| 235 | 12 | ▶ | => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.3
| |||||||||||||||||||||||||||||||||||
| 236 | 12 | my ($loop, $err, $stream) = @_; | |||||||||||||||||||||||||||||||||
| 237 | 12 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 238 | 0 | warn "Cannot connect to $url: $err\n"; | |||||||||||||||||||||||||||||||||
| 239 | 0 | return; | |||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||
| 241 | 12 | $stream->timeout(300); # 5 min | |||||||||||||||||||||||||||||||||
| 242 | $stream->on( | ||||||||||||||||||||||||||||||||||
| 243 | 12 | ▶ | read => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 244 | 12 | my ($stream, $bytes) = @_; | |||||||||||||||||||||||||||||||||
| 245 | 12 | $responses->{$url} .= $bytes}); | |||||||||||||||||||||||||||||||||
| 246 | 12 | $stream->write("$selector\r\n")}) | |||||||||||||||||||||||||||||||||
| 247 | 12 | } | |||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | 12 | ▶ | sub url_to_gopher { | ||||||||||||||||||||||||||||||||
|
Subroutine
url_to_gopher: called CC=8 SCAR=23.1
| |||||||||||||||||||||||||||||||||||
| 250 | 12 | my $url = shift; | |||||||||||||||||||||||||||||||||
| 251 | 12 | ▶ | my $name = shift||$url; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $url
MC/DC0%0/2
shift() || $url
shift()
$url
Truth table33%1/3
A || B
A= shift()B= $url
| |||||||||||||||||||||||||||||||||||
| 252 | 12 | my($scheme, $authority, $path, $query, $fragment) = | |||||||||||||||||||||||||||||||||
| 253 | $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|; | ||||||||||||||||||||||||||||||||||
| 254 | 12 | ▶ | $scheme ||= "gopher"; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$scheme ||= "gopher"
MC/DC0%0/1
$scheme ||= "gopher"
$scheme
Truth table50%1/2
A
A= $scheme
| |||||||||||||||||||||||||||||||||||
| 255 | 12 | ▶ | return unless $scheme =~ /^gophers?$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 256 | 12 | ▶ | $path = substr($path, 1) if substr($path, 0, 1) eq "/"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 257 | 12 | ▶ | my $type = $path ? substr($path, 0, 1) : "1"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 258 | 12 | ▶ | my $selector = $path ? substr($path, 1) : ""; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 259 | 12 | my ($host, $port) = split(/:/, $authority, 2); | |||||||||||||||||||||||||||||||||
| 260 | 12 | ▶ | return ($selector, $host, $port||70); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$port || 70
MC/DC0%0/1
$port || 70
$port
Truth table50%1/2
A
A= $port
| |||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | sub query_web { | ||||||||||||||||||||||||||||||||
|
Subroutine
query_web: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||
| 264 | 0 | my $url = shift; | |||||||||||||||||||||||||||||||||
| 265 | 0 | my $responses = shift; | |||||||||||||||||||||||||||||||||
| 266 | 0 | my($scheme, $authority, $path, $query, $fragment) = | |||||||||||||||||||||||||||||||||
| 267 | $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|; | ||||||||||||||||||||||||||||||||||
| 268 | 0 | my $tls = $scheme eq "https"; | |||||||||||||||||||||||||||||||||
| 269 | 0 | my ($host, $port) = split(/:/, $authority, 2); | |||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | $path ||= "/"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$path ||= "/"
MC/DC0%0/1
$path ||= "/"
$path
Truth table0%0/2
A
A= $path
| |||||||||||||||||||||||||||||||||||
| 271 | 0 | my $selector = $path; | |||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | $selector .= "?" . $query if $query; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 273 | # ignore the fragment | ||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | $port ||= $tls ? 443 : 80; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$port ||= $tls ? 443 : 80
MC/DC0%0/2
$port ||= $tls ? 443 : 80
$port
$tls ? 443 : 80
Truth table0%0/3
A ||= B
A= $portB= $tls ? 443 : 80
| |||||||||||||||||||||||||||||||||||
| 275 | Mojo::IOLoop->client( | ||||||||||||||||||||||||||||||||||
| 276 | {port => $port, address => $host, tls => $tls, timeout => 120 } | ||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 278 | 0 | my ($loop, $err, $stream) = @_; | |||||||||||||||||||||||||||||||||
| 279 | 0 | my $header; | |||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | $stream->on(read => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 281 | 0 | my ($stream, $bytes) = @_; | |||||||||||||||||||||||||||||||||
| 282 | 0 | $responses->{$url} .= $bytes; | |||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | $header = $responses->{$url} =~ s/^.*\r\n\r\n//s unless $header}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 284 | 0 | $stream->write("GET $selector HTTP/1.0\r\n" | |||||||||||||||||||||||||||||||||
| 285 | . "Host: $host:$port\r\n" | ||||||||||||||||||||||||||||||||||
| 286 | . "User-Agent: moku-pona\r\n" | ||||||||||||||||||||||||||||||||||
| 287 | 0 | . "\r\n")}); | |||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||
| 290 | =head2 Migration from 1.1 | ||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||
| 292 | The best way to migrate your setup is probably to use the C<list> subcommand | ||||||||||||||||||||||||||||||||||
| 293 | explained later, and to recreate your list of subscriptions. Then your | ||||||||||||||||||||||||||||||||||
| 294 | C<sites.txt> file will use gemtext format. | ||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||
| 296 | moku-pona list | grep "moku-pona add" > commands | ||||||||||||||||||||||||||||||||||
| 297 | mv ~/.moku-pona/sites.txt ~/.moku-pona/sites.txt~ | ||||||||||||||||||||||||||||||||||
| 298 | sh commands | ||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||
| 300 | =cut | ||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||
| 302 | 30 | ▶ | sub load_site { | ||||||||||||||||||||||||||||||||
|
Subroutine
load_site: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 303 | 30 | my $file = $site_list; | |||||||||||||||||||||||||||||||||
| 304 | 30 | ▶ | return [] if not -f $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 305 | 22 | ▶ | open(my $fh, "<:encoding(UTF-8)", $file) or die "Cannot read $file: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 306 | 22 | my @lines = <$fh>; | |||||||||||||||||||||||||||||||||
| 307 | 22 | chomp(@lines); | |||||||||||||||||||||||||||||||||
| 308 | 22 | @lines = grep(/^=> /, convert(@lines)); # from gopher | |||||||||||||||||||||||||||||||||
| 309 | 22 | return \@lines; | |||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||
| 312 | 20 | ▶ | sub load_file { | ||||||||||||||||||||||||||||||||
|
Subroutine
load_file: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 313 | 20 | my $file = shift; | |||||||||||||||||||||||||||||||||
| 314 | 20 | ▶ | return "" if not -f $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 315 | 17 | ▶ | open(my $fh, "<:encoding(UTF-8)", $file) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 316 | or die "Cannot read $file: $!\n"; | ||||||||||||||||||||||||||||||||||
| 317 | 17 | local $/ = undef; | |||||||||||||||||||||||||||||||||
| 318 | 17 | return <$fh>; | |||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | 24 | ▶ | sub save_file { | ||||||||||||||||||||||||||||||||
|
Subroutine
save_file: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 322 | 24 | my $file = shift; | |||||||||||||||||||||||||||||||||
| 323 | 24 | my $data = shift; | |||||||||||||||||||||||||||||||||
| 324 | 24 | ▶ | mkdir $data_dir unless -d $data_dir; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 325 | 24 | ▶ | open(my $fh, ">:encoding(UTF-8)", $file) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 326 | or die "Cannot write $file: $!\n"; | ||||||||||||||||||||||||||||||||||
| 327 | 24 | print $fh $data; | |||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||
| 330 | =head2 List your subscriptions | ||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||
| 332 | moku-pona list [names...] | ||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | This lists all your current subscriptions in a format that is suitable for a | ||||||||||||||||||||||||||||||||||
| 335 | shell script. Optionally, only list a subset of the lines. All lines are matched | ||||||||||||||||||||||||||||||||||
| 336 | against the regular expressions you provide and are only listed if there is at | ||||||||||||||||||||||||||||||||||
| 337 | least one match, if you provided any. | ||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||
| 339 | Example: | ||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||
| 341 | moku-pona list alex | ||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||
| 343 | In this particular case, since I'm testing my own server, the result would be: | ||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||
| 345 | moku-pona add https://alexschroeder.ch/wiki?action=rss "rss" | ||||||||||||||||||||||||||||||||||
| 346 | moku-pona add gemini://alexschroeder.ch/ "gemini" | ||||||||||||||||||||||||||||||||||
| 347 | moku-pona add gopher://alexschroeder.ch/ "gopher" | ||||||||||||||||||||||||||||||||||
| 348 | moku-pona add gophers://alexschroeder.ch:7443/ "gophers" | ||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||
| 350 | =cut | ||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||
| 352 | 1 | ▶ | sub do_list { | ||||||||||||||||||||||||||||||||
|
Subroutine
do_list: called CC=6 SCAR=20.2
| |||||||||||||||||||||||||||||||||||
| 353 | 1 | my @args = @_; | |||||||||||||||||||||||||||||||||
| 354 | 1 | my $site = load_site(); | |||||||||||||||||||||||||||||||||
| 355 | 1 | ▶ | if (@args) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 356 | 0 | print("Subscribed items in $site_list matching @args:\n"); | |||||||||||||||||||||||||||||||||
| 357 | } else { | ||||||||||||||||||||||||||||||||||
| 358 | 1 | print("Subscribed items in $site_list:\n"); | |||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||
| 360 | 1 | ▶ | print("none\n") unless @$site; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 361 | 1 | for my $line (@$site) { | |||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | next if @args and none { $line =~ /$_/ } @args; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.7
Branch: 1 branch
Condition33%1/3
@args and &none(sub {
$line =~ /$_/u;
}
, @args)
MC/DC0%0/2
@args and &none(sub {
$line =~ /$_/u;
}
, @args)
@args
&none(sub {
$line =~ /$_/u;
}
, @args)
Truth table33%1/3
A and B
A= @argsB= &none(sub {
$line =~ /$_/u;
}
, @args)
| |||||||||||||||||||||||||||||||||||
| 363 | # skip item type | ||||||||||||||||||||||||||||||||||
| 364 | 2 | my ($uri, $name) = $line =~ /^=> (\S+)\s+(.*)/; | |||||||||||||||||||||||||||||||||
| 365 | 2 | print(qq{moku-pona add $uri "$name"\n}); | |||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||
| 369 | =head2 Add a subscription | ||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||
| 371 | moku-pona add url [description] | ||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||
| 373 | This adds a URL to the list of subscribed items. If the target is an Atom or RSS | ||||||||||||||||||||||||||||||||||
| 374 | feed, then that's also supported. You can provide an optional description for | ||||||||||||||||||||||||||||||||||
| 375 | this URL. If you don't provide a description, the URL will be used as the item's | ||||||||||||||||||||||||||||||||||
| 376 | description. | ||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||
| 378 | Example: | ||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||
| 380 | moku-pona add gemini://alexschroeder.ch kensanata | ||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||
| 384 | 8 | ▶ | sub do_add { | ||||||||||||||||||||||||||||||||
|
Subroutine
do_add: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||||||||
| 385 | 8 | my $uri = shift; | |||||||||||||||||||||||||||||||||
| 386 | 8 | my $name = shift; | |||||||||||||||||||||||||||||||||
| 387 | 8 | ▶ | $name ||= $uri; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$name ||= $uri
MC/DC0%0/2
$name ||= $uri
$name
$uri
Truth table33%1/3
A ||= B
A= $nameB= $uri
| |||||||||||||||||||||||||||||||||||
| 388 | 8 | my $line = "=> $uri $name"; | |||||||||||||||||||||||||||||||||
| 389 | 8 | my $site = load_site(); | |||||||||||||||||||||||||||||||||
| 390 | 8 | my $uri_re = quotemeta($uri); | |||||||||||||||||||||||||||||||||
| 391 | 8 | my $name_re = quotemeta($name); | |||||||||||||||||||||||||||||||||
| 392 | 8 | ▶ | if (grep(/^=> $uri_re /, @$site)) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 393 | 0 | warn("$uri already exists in $site_list\n"); | |||||||||||||||||||||||||||||||||
| 394 | } elsif (grep(/^=> \S+ $name_re$/, @$site)) { | ||||||||||||||||||||||||||||||||||
| 395 | 0 | warn("$name already exists in $site_list\n"); | |||||||||||||||||||||||||||||||||
| 396 | } else { | ||||||||||||||||||||||||||||||||||
| 397 | 8 | push(@$site, $line); | |||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||
| 399 | 8 | save_file($site_list, join("\n", @$site, "")); | |||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||
| 402 | =head2 Remove a subscription | ||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||
| 404 | moku-pona remove description | ||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||
| 406 | This removes one or more URLs from the list of subscribed items. | ||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||
| 408 | Example: | ||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||
| 410 | moku-pona remove kensanata | ||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||
| 412 | =cut | ||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||
| 414 | 2 | ▶ | sub do_remove { | ||||||||||||||||||||||||||||||||
|
Subroutine
do_remove: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||
| 415 | 2 | my @args = @_; | |||||||||||||||||||||||||||||||||
| 416 | 2 | my $site = load_site(); | |||||||||||||||||||||||||||||||||
| 417 | 2 | my $count = 0; | |||||||||||||||||||||||||||||||||
| 418 | 2 | my $i = 0; | |||||||||||||||||||||||||||||||||
| 419 | 2 | ▶ | while (@args and $i < @$site) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@args and $i < @$site
MC/DC50%1/2
@args and $i < @$site
@args
$i < @$site
Truth table66%2/3
A and B
A= @argsB= $i < @$site
| |||||||||||||||||||||||||||||||||||
| 420 | 4 | my $line = $site->[$i]; | |||||||||||||||||||||||||||||||||
| 421 | 4 | my ($uri, $name) = $line =~ /^=> (\S+)\s+(.*)/; | |||||||||||||||||||||||||||||||||
| 422 | 4 | my $found = 0; | |||||||||||||||||||||||||||||||||
| 423 | 4 | my $j = 0; | |||||||||||||||||||||||||||||||||
| 424 | 4 | ▶ | while ($j < @args) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 425 | 4 | ▶ | if ($name eq $args[$j]) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 426 | 2 | $count++; | |||||||||||||||||||||||||||||||||
| 427 | 2 | $found = 1; | |||||||||||||||||||||||||||||||||
| 428 | 2 | splice(@$site, $i, 1); # remove the site found | |||||||||||||||||||||||||||||||||
| 429 | 2 | splice(@args, $j, 1); # remove the arg found | |||||||||||||||||||||||||||||||||
| 430 | } else { | ||||||||||||||||||||||||||||||||||
| 431 | 2 | $j++; | |||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||
| 434 | 4 | ▶ | $i++ unless $found; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||
| 436 | 2 | ▶ | if ($count) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 437 | 2 | ▶ | printf("Removed %d %s\n", $count, | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 438 | $count == 1 ? "subscription" : "subscriptions"); | ||||||||||||||||||||||||||||||||||
| 439 | 2 | save_file($site_list, join("\n", @$site, "")); | |||||||||||||||||||||||||||||||||
| 440 | } else { | ||||||||||||||||||||||||||||||||||
| 441 | 0 | warn("No subscriptions matching @args found\n"); | |||||||||||||||||||||||||||||||||
| 442 | 0 | warn("Use moku-pona list to find the correct descriptions.\n"); | |||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||
| 446 | =head2 Clean up the data directory | ||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||
| 448 | moku-pona cleanup [--confirm] | ||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||
| 450 | When Moku Pona updates, copies of the URL targets are saved in the data | ||||||||||||||||||||||||||||||||||
| 451 | directory. If you remove a subscription (see above), that leaves a cache file in | ||||||||||||||||||||||||||||||||||
| 452 | the data directory that is no longer used – and it leaves an entry in | ||||||||||||||||||||||||||||||||||
| 453 | C<updates.txt> that is no longer wanted. The cleanup command fixes this. It | ||||||||||||||||||||||||||||||||||
| 454 | deletes all the cached pages that you are no longer subscribed to, and it | ||||||||||||||||||||||||||||||||||
| 455 | removes those entries from C<updates.txt> as well. | ||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||
| 457 | Actually, just to be sure, if you run it without the C<--confirm> argument, it | ||||||||||||||||||||||||||||||||||
| 458 | simply prints which files it would trash. Rerun it with the C<--confirm> | ||||||||||||||||||||||||||||||||||
| 459 | argument to actually do it. | ||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||
| 461 | Example: | ||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||
| 463 | moku-pona cleanup | ||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||
| 465 | =cut | ||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||
| 467 | 2 | ▶ | sub do_cleanup { | ||||||||||||||||||||||||||||||||
|
Subroutine
do_cleanup: called CC=18 SCAR=29.0
| |||||||||||||||||||||||||||||||||||
| 468 | 2 | ▶ | my $confirm = shift||'' eq '--confirm'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || !1
MC/DC100%1/1
shift() || !1
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 469 | 2 | my $todo = 0; | |||||||||||||||||||||||||||||||||
| 470 | # get a hash map telling us the cache files we expect based on our sites | ||||||||||||||||||||||||||||||||||
| 471 | 2 | my $site = load_site(); | |||||||||||||||||||||||||||||||||
| 472 | my %caches = map { | ||||||||||||||||||||||||||||||||||
| 473 | 2 | my ($uri, $name) = /^=> (\S+)\s+(.*)/; | |||||||||||||||||||||||||||||||||
| 474 | 2 | $uri =~ s/[\/:]/-/g; | |||||||||||||||||||||||||||||||||
| 475 | 2 | "$data_dir/$uri" => 1; | |||||||||||||||||||||||||||||||||
| 476 | } @$site; | ||||||||||||||||||||||||||||||||||
| 477 | # get a list of text files in the directory | ||||||||||||||||||||||||||||||||||
| 478 | 2 | ▶ | opendir(my $dh, $data_dir) or die "Cannot read $data_dir: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 479 | 2 | my @files = map { "$data_dir/$_" } grep { /^[^.]/ } readdir($dh); | |||||||||||||||||||||||||||||||||
| 480 | 2 | closedir($dh); | |||||||||||||||||||||||||||||||||
| 481 | # remove unnecessary cache files | ||||||||||||||||||||||||||||||||||
| 482 | 2 | for my $file (@files) { | |||||||||||||||||||||||||||||||||
| 483 | 8 | ▶ | next if $file eq $site_list; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 484 | 6 | ▶ | next if $file eq $updated_list; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 485 | 4 | ▶ | next if $caches{$file}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 486 | 2 | ▶ | if ($confirm) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 487 | 1 | unlink $file; | |||||||||||||||||||||||||||||||||
| 488 | } else { | ||||||||||||||||||||||||||||||||||
| 489 | 1 | print "trash $file\n"; | |||||||||||||||||||||||||||||||||
| 490 | 1 | $todo++; | |||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||
| 493 | # check updates list | ||||||||||||||||||||||||||||||||||
| 494 | 2 | ▶ | if (-f $updated_list) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 495 | 2 | ▶ | open(my $fh, "<:encoding(UTF-8)", $updated_list) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 496 | or die "Cannot read $updated_list: $!\n"; | ||||||||||||||||||||||||||||||||||
| 497 | 2 | my @lines = <$fh>; | |||||||||||||||||||||||||||||||||
| 498 | 2 | chomp(@lines); | |||||||||||||||||||||||||||||||||
| 499 | # decide what to do about each line in updates, looking just at the names | ||||||||||||||||||||||||||||||||||
| 500 | 2 | my %sites = map { s/^=> (\S+)\s+(.*)/$2/; $_ => 1 } @$site; | |||||||||||||||||||||||||||||||||
| 501 | 2 | my @deletes; | |||||||||||||||||||||||||||||||||
| 502 | my @keeps; | ||||||||||||||||||||||||||||||||||
| 503 | 2 | for my $line (@lines) { | |||||||||||||||||||||||||||||||||
| 504 | 4 | ▶ | if ($line =~ /^=> \S+ \d\d\d\d-\d\d-\d\d (.+)/ and not $sites{$1}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line =~ /^=> \S+ \d\d\d\d-\d\d-\d\d (.+)/u and not $sites{$1}
MC/DC50%1/2
$line =~ /^=> \S+ \d\d\d\d-\d\d-\d\d (.+)/u and not $sites{$1}
$line =~ /^=> \S+ \d\d\d\d-\d\d-\d\d (.+)/u
not $sites{$1}
Truth table66%2/3
A and not(B)
A= $line =~ /^=> \S+ \d\d\d\d-\d\d-\d\d (.+)/uB= not $sites{$1}
| |||||||||||||||||||||||||||||||||||
| 505 | 2 | push(@deletes, $line); | |||||||||||||||||||||||||||||||||
| 506 | 2 | $todo++; | |||||||||||||||||||||||||||||||||
| 507 | } else { | ||||||||||||||||||||||||||||||||||
| 508 | 2 | push(@keeps, $line); | |||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||
| 511 | 2 | ▶ | print "Removing these entries from updates:\n" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@deletes and not $confirm
MC/DC50%1/2
@deletes and not $confirm
@deletes
not $confirm
Truth table66%2/3
A and not(B)
A= @deletesB= not $confirm
| |||||||||||||||||||||||||||||||||||
| 512 | . join("\n", @deletes, "") if @deletes and not $confirm; | ||||||||||||||||||||||||||||||||||
| 513 | # save | ||||||||||||||||||||||||||||||||||
| 514 | 2 | ▶ | save_file($updated_list, join("\n", @keeps, "")) if $confirm; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||
| 516 | 2 | ▶ | if ($todo && !$confirm) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$todo and not $confirm
MC/DC50%1/2
$todo and not $confirm
$todo
not $confirm
Truth table66%2/3
A and not(B)
A= $todoB= not $confirm
| |||||||||||||||||||||||||||||||||||
| 517 | 1 | print "\n"; | |||||||||||||||||||||||||||||||||
| 518 | 1 | print "Use moku-pona cleanup --confirm to do it.\n"; | |||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||
| 522 | =head2 Update | ||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||
| 524 | moku-pona update [--quiet] [names...] | ||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||
| 526 | This updates all the subscribed items and generates a new local page for you to | ||||||||||||||||||||||||||||||||||
| 527 | visit: C<updates.txt>. | ||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||
| 529 | Example: | ||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||
| 531 | moku-pona update | ||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||
| 533 | If you call it from a cron job, you might want to use the C<--quiet> argument to | ||||||||||||||||||||||||||||||||||
| 534 | prevent it from printing all the sites it's contacting (since cron will then | ||||||||||||||||||||||||||||||||||
| 535 | mail this to you and you might not care for it unless there's a problem). If | ||||||||||||||||||||||||||||||||||
| 536 | there's a problem, you'll still get a message. | ||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||
| 538 | This is how I call it from my C<crontab>, for example | ||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||
| 540 | #m h dom mon dow command | ||||||||||||||||||||||||||||||||||
| 541 | 11 7,14 * * * /home/alex/bin/moku-pona update --quiet | ||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||
| 543 | If you're testing things, you can also fetch just a limited number of items by | ||||||||||||||||||||||||||||||||||
| 544 | listing them. | ||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||
| 546 | Example: | ||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||
| 548 | moku-pona update "RPG Planet" | ||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||
| 550 | The C<updates.txt> files may contain lines that are not links at the top. These | ||||||||||||||||||||||||||||||||||
| 551 | will remain untouched. The rest is links. New items are added at the beginning | ||||||||||||||||||||||||||||||||||
| 552 | of the links and older copies of such items are removed from the links. | ||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||
| 554 | =cut | ||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||
| 556 | 5 | ▶ | sub add_update { | ||||||||||||||||||||||||||||||||
|
Subroutine
add_update: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 557 | 5 | my $line = shift; | |||||||||||||||||||||||||||||||||
| 558 | 5 | my ($uri, $name) = $line =~ /^=> (\S+)\s+(.*)/; | |||||||||||||||||||||||||||||||||
| 559 | # add current date | ||||||||||||||||||||||||||||||||||
| 560 | 5 | my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(); # UTC | |||||||||||||||||||||||||||||||||
| 561 | 5 | my $date = sprintf('%4d-%02d-%02d', $year + 1900, $mon + 1, $mday); | |||||||||||||||||||||||||||||||||
| 562 | 5 | $line = "=> $uri $date $name"; | |||||||||||||||||||||||||||||||||
| 563 | # load file | ||||||||||||||||||||||||||||||||||
| 564 | 5 | my @lines; | |||||||||||||||||||||||||||||||||
| 565 | 5 | ▶ | if (-f $updated_list) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 566 | 4 | ▶ | open(my $fh, "<:encoding(UTF-8)", $updated_list) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 567 | or die "Cannot read $updated_list: $!\n"; | ||||||||||||||||||||||||||||||||||
| 568 | 4 | @lines = convert(<$fh>); # from gohper | |||||||||||||||||||||||||||||||||
| 569 | 4 | chomp(@lines); | |||||||||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||||||||
| 571 | # start the new list with the non-list links | ||||||||||||||||||||||||||||||||||
| 572 | 5 | my @new = grep(!/^=>/, @lines); | |||||||||||||||||||||||||||||||||
| 573 | # add the line to the new list | ||||||||||||||||||||||||||||||||||
| 574 | 5 | push(@new, $line); | |||||||||||||||||||||||||||||||||
| 575 | # add the remaining links to the new list, except for the ones matching the name of the new line | ||||||||||||||||||||||||||||||||||
| 576 | 5 | $name = quotemeta($name); | |||||||||||||||||||||||||||||||||
| 577 | 5 | push(@new, grep(!/\d\d\d\d-\d\d-\d\d $name$/, grep(/^=>/, @lines))); | |||||||||||||||||||||||||||||||||
| 578 | # save | ||||||||||||||||||||||||||||||||||
| 579 | 5 | save_file($updated_list, join("\n", @new, "")); | |||||||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||
| 582 | 6 | ▶ | sub do_update { | ||||||||||||||||||||||||||||||||
|
Subroutine
do_update: called CC=16 SCAR=32.2
| |||||||||||||||||||||||||||||||||||
| 583 | 0 | my $quiet = grep { $_ eq '--quiet' } @_; | |||||||||||||||||||||||||||||||||
| 584 | 0 | my @sites = grep { $_ ne '--quiet' } @_; | |||||||||||||||||||||||||||||||||
| 585 | 6 | my $site = load_site(); | |||||||||||||||||||||||||||||||||
| 586 | 6 | my %responses; | |||||||||||||||||||||||||||||||||
| 587 | my @uris; | ||||||||||||||||||||||||||||||||||
| 588 | 6 | my %names; | |||||||||||||||||||||||||||||||||
| 589 | 6 | my %lines; | |||||||||||||||||||||||||||||||||
| 590 | 6 | for my $line (@$site) { | |||||||||||||||||||||||||||||||||
| 591 | 12 | my ($uri, $name) = $line =~ /^=> (\S+)(?:[ \t]+(.*))?/; | |||||||||||||||||||||||||||||||||
| 592 | 12 | ▶ | $name ||= $uri; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$name ||= $uri
MC/DC0%0/2
$name ||= $uri
$name
$uri
Truth table33%1/3
A ||= B
A= $nameB= $uri
| |||||||||||||||||||||||||||||||||||
| 593 | 0 | ▶ | next unless @sites == 0 or grep { $_ eq $name } @sites; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@sites == 0 or grep {$_ eq $name;} @sites
MC/DC0%0/2
@sites == 0 or grep {$_ eq $name;} @sites
@sites == 0
grep {$_ eq $name;} @sites
Truth table33%1/3
A or B
A= @sites == 0B= grep {$_ eq $name;} @sites
| |||||||||||||||||||||||||||||||||||
| 594 | 12 | ▶ | say("Fetching $name...") unless $quiet; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 595 | 12 | push(@uris, $uri); | |||||||||||||||||||||||||||||||||
| 596 | 12 | $names{$uri} = $name; | |||||||||||||||||||||||||||||||||
| 597 | 12 | $lines{$uri} = $line; | |||||||||||||||||||||||||||||||||
| 598 | 12 | ▶ | if ($uri =~ /^gopher/) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 599 | 12 | query_gopher($uri, \%responses); | |||||||||||||||||||||||||||||||||
| 600 | } elsif ($uri =~ /^gemini/) { | ||||||||||||||||||||||||||||||||||
| 601 | 0 | query_gemini($uri, \%responses); | |||||||||||||||||||||||||||||||||
| 602 | } elsif ($uri =~ /^http/) { | ||||||||||||||||||||||||||||||||||
| 603 | 0 | query_web($uri, \%responses); | |||||||||||||||||||||||||||||||||
| 604 | } else { | ||||||||||||||||||||||||||||||||||
| 605 | 0 | warn "Don't know how to fetch $uri\n"; | |||||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||
| 609 | 6 | ▶ | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||
| 611 | 6 | for my $uri (keys %responses) { | |||||||||||||||||||||||||||||||||
| 612 | 12 | my $name = $names{$uri}; | |||||||||||||||||||||||||||||||||
| 613 | # decode the UTF-8 when we have the entire response | ||||||||||||||||||||||||||||||||||
| 614 | 12 | my $new = decode_utf8($responses{$uri}); | |||||||||||||||||||||||||||||||||
| 615 | 12 | ▶ | if (not $new) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 616 | 0 | warn("$name returned an empty document\n"); | |||||||||||||||||||||||||||||||||
| 617 | 0 | next; | |||||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||||
| 619 | 12 | my $filename = $uri; | |||||||||||||||||||||||||||||||||
| 620 | 12 | $filename =~ s/[\/:]/-/g; | |||||||||||||||||||||||||||||||||
| 621 | 12 | my $cache = "$data_dir/$filename"; | |||||||||||||||||||||||||||||||||
| 622 | 12 | ▶ | if ($new =~ /^<(\?xml|rss)/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 623 | 2 | $new = to_gemini($new); | |||||||||||||||||||||||||||||||||
| 624 | 2 | my $encoded = uri_escape_utf8($filename); | |||||||||||||||||||||||||||||||||
| 625 | 2 | $lines{$uri} = "=> $encoded $name"; # now referring to the local cache file | |||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||||
| 627 | 12 | my $old = load_file($cache); | |||||||||||||||||||||||||||||||||
| 628 | 12 | ▶ | if ($new ne $old) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 629 | 5 | ▶ | say "$name updated" unless $quiet; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 630 | 5 | add_update($lines{$uri}); | |||||||||||||||||||||||||||||||||
| 631 | 5 | save_file($cache, $new); | |||||||||||||||||||||||||||||||||
| 632 | } else { | ||||||||||||||||||||||||||||||||||
| 633 | 7 | ▶ | say "$name unchanged" unless $quiet; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||
| 638 | =head2 Subscribing to feeds | ||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||
| 640 | When the result of an update is an XML document, then it is parsed and the links | ||||||||||||||||||||||||||||||||||
| 641 | of its items (if RSS) or entries (if Atom) are extracted and saved in the cache | ||||||||||||||||||||||||||||||||||
| 642 | file in the data directory. The effect is this: | ||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||
| 644 | =over | ||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||
| 646 | =item If you subscribe to a regular page, then the link to it in C<updates.txt> | ||||||||||||||||||||||||||||||||||
| 647 | moves to the top when it changes. | ||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||
| 649 | =item If you subscribe to a feed, then the link in C<updates.txt> moves to the | ||||||||||||||||||||||||||||||||||
| 650 | top when it changes and it links to a file in the data directory that links to | ||||||||||||||||||||||||||||||||||
| 651 | the individual items in the feed. | ||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||
| 653 | =back | ||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||
| 655 | Example: | ||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||
| 657 | moku-pona add https://campaignwiki.org/rpg/feed.xml "RPG" | ||||||||||||||||||||||||||||||||||
| 658 | moku-pona update | ||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||
| 660 | This adds the RPG entry to C<updates.txt> as follows: | ||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||
| 662 | => https%3A--campaignwiki.org-rpg-feed.xml 2020-11-07 RPG | ||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||
| 664 | And if you check the file C<https:--campaignwiki.org-rpg-feed.xml>, you'll see | ||||||||||||||||||||||||||||||||||
| 665 | that it's a regular Gemini list. You'll find 100 links like the following: | ||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||
| 667 | => https://alexschroeder.ch/wiki/2020-11-05_Episode_34 Episode 34 | ||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||
| 669 | Now use C<moku-pona publish> (see below) to move the files to the correct | ||||||||||||||||||||||||||||||||||
| 670 | directory where your Gemini server expects them. | ||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||
| 672 | =cut | ||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||
| 674 | # Convert a RSS or Atom feed to Gemini links | ||||||||||||||||||||||||||||||||||
| 675 | 5 | ▶ | sub to_gemini { | ||||||||||||||||||||||||||||||||
|
Subroutine
to_gemini: called CC=9 SCAR=22.5
| |||||||||||||||||||||||||||||||||||
| 676 | 5 | my $xml = shift; | |||||||||||||||||||||||||||||||||
| 677 | 5 | my $dom = eval { | |||||||||||||||||||||||||||||||||
| 678 | 5 | require XML::LibXML; | |||||||||||||||||||||||||||||||||
| 679 | 5 | my $parser = XML::LibXML->new(recover => 2); # no errors, no warnings | |||||||||||||||||||||||||||||||||
| 680 | 5 | $parser->load_xml(string => $xml); | |||||||||||||||||||||||||||||||||
| 681 | }; | ||||||||||||||||||||||||||||||||||
| 682 | 5 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 683 | 0 | warn "$@\n"; | |||||||||||||||||||||||||||||||||
| 684 | 0 | return ''; | |||||||||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||||||||
| 686 | 5 | my $root = $dom->documentElement(); | |||||||||||||||||||||||||||||||||
| 687 | 5 | my $xpc = XML::LibXML::XPathContext->new; | |||||||||||||||||||||||||||||||||
| 688 | 5 | $xpc->registerNs('atom', 'http://www.w3.org/2005/Atom'); | |||||||||||||||||||||||||||||||||
| 689 | 5 | ▶ | my $nodes = $xpc->findnodes('//atom:entry', $root) || $root->findnodes('//item'); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$xpc->findnodes('//atom:entry', $root) || $root->findnodes('//item')
MC/DC0%0/2
$xpc->findnodes('//atom:entry', $root) || $root->findnodes('//item')
$xpc->findnodes('//atom:entry', $root)
$root->findnodes('//item')
Truth table66%2/3
A || B
A= $xpc->findnodes('//atom:entry', $root)B= $root->findnodes('//item')
| |||||||||||||||||||||||||||||||||||
| 690 | 5 | my @lines; | |||||||||||||||||||||||||||||||||
| 691 | 5 | for my $node ($nodes->get_nodelist) { | |||||||||||||||||||||||||||||||||
| 692 | 7 | ▶ | my $titles = $xpc->findnodes('atom:title', $node) || $node->getChildrenByTagName('title'); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$xpc->findnodes('atom:title', $node) || $node->getChildrenByTagName('title')
MC/DC0%0/2
$xpc->findnodes('atom:title', $node) || $node->getChildrenByTagName('title')
$xpc->findnodes('atom:title', $node)
$node->getChildrenByTagName('title')
Truth table66%2/3
A || B
A= $xpc->findnodes('atom:title', $node)B= $node->getChildrenByTagName('title')
| |||||||||||||||||||||||||||||||||||
| 693 | 7 | my $first = $titles->shift; | |||||||||||||||||||||||||||||||||
| 694 | 7 | ▶ | my $title = $first ? $first->textContent : "untitled"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 695 | 7 | $title =~ s/\s+$//; # trim right | |||||||||||||||||||||||||||||||||
| 696 | 7 | $title =~ s/^\s+//; # trim left | |||||||||||||||||||||||||||||||||
| 697 | 7 | ▶ | my $links = $xpc->findnodes('atom:link', $node) || $node->getChildrenByTagName('link'); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$xpc->findnodes('atom:link', $node) || $node->getChildrenByTagName('link')
MC/DC0%0/2
$xpc->findnodes('atom:link', $node) || $node->getChildrenByTagName('link')
$xpc->findnodes('atom:link', $node)
$node->getChildrenByTagName('link')
Truth table66%2/3
A || B
A= $xpc->findnodes('atom:link', $node)B= $node->getChildrenByTagName('link')
| |||||||||||||||||||||||||||||||||||
| 698 | 7 | ▶ | next unless $links; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 699 | 6 | my $link = $links->shift; # take the first | |||||||||||||||||||||||||||||||||
| 700 | 6 | ▶ | my $href = $link->getAttribute('href') || $link->textContent; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$link->getAttribute('href') || $link->textContent
MC/DC0%0/2
$link->getAttribute('href') || $link->textContent
$link->getAttribute('href')
$link->textContent
Truth table66%2/3
A || B
A= $link->getAttribute('href')B= $link->textContent
| |||||||||||||||||||||||||||||||||||
| 701 | 6 | push(@lines, "=> $href $title"); | |||||||||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||||||||
| 703 | 5 | return join("\n", @lines, ""); | |||||||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||
| 706 | =head2 Publishing your subscription | ||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||
| 708 | moku-pona publish <directory> | ||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||
| 710 | This takes the important files from your data directory and copies them to a | ||||||||||||||||||||||||||||||||||
| 711 | target directory. You could just use symbolic links for C<sites.txt> and | ||||||||||||||||||||||||||||||||||
| 712 | C<updates.txt>, of course. But if you've subscribed to actual feeds as described | ||||||||||||||||||||||||||||||||||
| 713 | above, then the cache files need to get copied as well! | ||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||
| 715 | Example: | ||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||
| 717 | mkdir ~/subs | ||||||||||||||||||||||||||||||||||
| 718 | moku-pona publish ~/subs | ||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||
| 720 | =head2 Serving your subscriptions via Gemini | ||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||
| 722 | This depends entirely on your Gemini server. If you like it really simple, you | ||||||||||||||||||||||||||||||||||
| 723 | can use L<Lupa Pona|App::lupapona>. Here's how to create the certificate and key | ||||||||||||||||||||||||||||||||||
| 724 | files, copy them to the C<~/subs> directory created above, and run C<lupa-pona> | ||||||||||||||||||||||||||||||||||
| 725 | for a quick test. | ||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||
| 727 | make cert | ||||||||||||||||||||||||||||||||||
| 728 | cp *.pem ~/subs | ||||||||||||||||||||||||||||||||||
| 729 | cd ~/subs | ||||||||||||||||||||||||||||||||||
| 730 | lupa-pona | ||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||
| 732 | =cut | ||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||
| 734 | 1 | ▶ | sub do_publish { | ||||||||||||||||||||||||||||||||
|
Subroutine
do_publish: called CC=8 SCAR=21.6
| |||||||||||||||||||||||||||||||||||
| 735 | 1 | my $target = shift; | |||||||||||||||||||||||||||||||||
| 736 | 1 | ▶ | die "Target $target is not a directory\n" unless -d $target; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 737 | 1 | ▶ | die "Source $site_list does not exist\n" unless -f $site_list; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 738 | 1 | ▶ | die "Source $updated_list does not exist\n" unless -f $updated_list; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 739 | 1 | my $path; | |||||||||||||||||||||||||||||||||
| 740 | # copy site list | ||||||||||||||||||||||||||||||||||
| 741 | 1 | copy($site_list, "$target/sites.txt"); | |||||||||||||||||||||||||||||||||
| 742 | # copy updates but with local links for the feed files | ||||||||||||||||||||||||||||||||||
| 743 | 1 | ▶ | open(my $in, "<:encoding(UTF-8)", $updated_list) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 744 | or die "Cannot read $updated_list: $!\n"; | ||||||||||||||||||||||||||||||||||
| 745 | 1 | ▶ | open(my $out, ">:encoding(UTF-8)", "$target/updates.txt") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 746 | or die "Cannot write $target/updates.txt: $!\n"; | ||||||||||||||||||||||||||||||||||
| 747 | 1 | for my $line (<$in>) { | |||||||||||||||||||||||||||||||||
| 748 | 1 | chomp($line); | |||||||||||||||||||||||||||||||||
| 749 | 1 | ($line) = convert($line); | |||||||||||||||||||||||||||||||||
| 750 | 1 | my ($uri, $name) = $line =~ /^=> file:\/\/\/(\S+)\s+(.*)/; | |||||||||||||||||||||||||||||||||
| 751 | # if the target is a local file, then that's because it is the result of a | ||||||||||||||||||||||||||||||||||
| 752 | # to_gemini call in do_update, so we need to copy it as well | ||||||||||||||||||||||||||||||||||
| 753 | 1 | $uri =~ s/[\/:]/-/g; | |||||||||||||||||||||||||||||||||
| 754 | 1 | ▶ | if (-f "$data_dir/$uri") { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 755 | 1 | copy("$data_dir/$uri", "$target/$uri"); | |||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||
| 757 | 1 | print $out "$line\n"; | |||||||||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||
| 761 | 0 | ▶ | sub do_help { | ||||||||||||||||||||||||||||||||
|
Subroutine
do_help: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 762 | 0 | my $parser = Pod::Text->new(); | |||||||||||||||||||||||||||||||||
| 763 | 0 | $parser->parse_file($0); | |||||||||||||||||||||||||||||||||
| 764 | } | ||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||
| 766 | 0 | ▶ | sub main { | ||||||||||||||||||||||||||||||||
|
Subroutine
main: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||
| 767 | 0 | ▶ | my $command = shift(@ARGV) || "help"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @ARGV || 'help'
MC/DC0%0/1
shift @ARGV || 'help'
shift @ARGV
Truth table0%0/2
A
A= shift @ARGV
| |||||||||||||||||||||||||||||||||||
| 768 | 0 | ▶ | if ($command eq "add") { do_add(@ARGV) } | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 769 | 0 | elsif ($command eq "remove") { do_remove(@ARGV) } | |||||||||||||||||||||||||||||||||
| 770 | 0 | elsif ($command eq "list") { do_list(@ARGV) } | |||||||||||||||||||||||||||||||||
| 771 | 0 | elsif ($command eq "cleanup") { do_cleanup(@ARGV) } | |||||||||||||||||||||||||||||||||
| 772 | 0 | elsif ($command eq "update") { do_update(@ARGV) } | |||||||||||||||||||||||||||||||||
| 773 | 0 | elsif ($command eq "convert") { do_convert() } | |||||||||||||||||||||||||||||||||
| 774 | 0 | elsif ($command eq "publish") { do_publish(@ARGV) } | |||||||||||||||||||||||||||||||||
| 775 | 0 | else { do_help() } | |||||||||||||||||||||||||||||||||
| 776 | } | ||||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||||
| 778 | 9 | ▶ | main() if $0 =~ /\bmoku-pona$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||
| 780 | 9 | 1; | |||||||||||||||||||||||||||||||||