Phoebe.pm

filter: Statementstmt 3.4% 49 / 1426 Branchbran 0.0% 0 / 516 Conditioncond 0.6% 1 / 152 MC/DCmcdc 0.0% 0 / 89 Subroutinesub 9.0% 12 / 133 Podpod 0.0% 0 / 86 totaltotal 2.5% 62 / 2402 CC 351 SCAR 55.6
CC
351
Cov
2%
process_gemini
2070.0
changes
380.0
to_html
380.0
CRAP 259.3
« Phoebe/WebDAV.pm ↑ Summary Phoebe/Galleries.pm »
1 #!/usr/bin/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 - a Gemini-based wiki
22  
23 =head1 DESCRIPTION
24  
25 This module contains the core of the Phoebe wiki. Import functions and variables
26 from this module to write extensions, or to run it some other way. Usually,
27 F<script/phoebe> is used to start a Phoebe server. This is why all the
28 documentation regarding server startup can be found there.
29  
30 This section describes some hooks you can use to customize your wiki using the
31 F<config> file, or using a Perl file (ending in F<*.pl> or F<*.pm>) in the
32 F<conf.d> directory. Once you're happy with the changes you've made, restart the
33 server, or send a SIGHUP if you know the PID.
34  
35 Here are the ways you can hook into Phoebe code:
36  
37 C<@extensions> is a list of code references allowing you to handle additional
38 URLs; return 1 if you handle a URL; each code reference gets called with $stream
39 (L<Mojo::IOLoop::Stream>), the first line of the request (a Gemini URL, a Gopher
40 selector, a finger user, a HTTP request line), a hash reference (with the
41 headers of HTTP requests or the parameters of Titan requests), a buffer of bytes
42 (e.g. for Titan or HTTP PUT or POST requests), and (sometimes) size.
43  
44 C<@main_menu> adds more lines to the main menu, possibly links that aren't
45 simply links to existing pages.
46  
47 C<@footer> is a list of code references allowing you to add things like licenses
48 or contact information to every page; each code reference gets called with
49 $stream (L<Mojo::IOLoop::Stream>), $host, $space, $id, $revision, and $format
50 ('gemini' or 'html') used to serve the page; return a gemtext string to append
51 at the end; the alternative is to overwrite the C<footer> or C<html_footer> subs
52 – the default implementation for Gemini adds History, Raw text and HTML link,
53 and C<@footer> to the bottom of every page; the default implementation for HTTP
54 just adds C<@footer> to the bottom of every page.
55  
56 If you do hook into Phoebe's code, you probably want to make use of the
57 following variables:
58  
59 C<$server> stores the command line options provided by the user.
60  
61 C<$log> is how you log things.
62  
63 A very simple example to add a contact mail at the bottom of every page; this
64 works for both Gemini and the web:
65  
66 # tested by t/example-footer.t
67 use App::Phoebe::Web;
68 use App::Phoebe qw(@footer);
69 push(@footer, sub { '=> mailto:alex@alexschroeder.ch Mail' });
70  
71 This prints a very simply footer instead of the usual footer for Gemini, as the
72 C<footer> function is redefined. At the same time, the C<@footer> array is still
73 used for the web:
74  
75 # tested by t/example-footer2.t
76 package App::Phoebe;
77 use App::Phoebe::Web;
78 use Modern::Perl;
79 our (@footer); # HTML only
80 push(@footer, sub { '=> https://alexschroeder.ch/wiki/Contact Contact' });
81 # footer sub is Gemini only
82 no warnings qw(redefine);
83 sub footer {
84 return "\n" . '—' x 10 . "\n" . '=> mailto:alex@alexschroeder.ch Mail';
85 }
86  
87 This example shows you how to add a new route (a new path served by the wiki).
88 Instead of just writing "Test" to the page, you could of course run arbitrary
89 Perl code.
90  
91 # tested by t/example-route.t
92 our @config = (<<'EOT');
93 use App::Phoebe qw(@extensions @main_menu port host_regex success);
94 use Modern::Perl;
95 push(@main_menu, "=> /do/test Test");
96 push(@extensions, \&serve_test);
97 sub serve_test {
98 my $stream = shift;
99 my $url = shift;
100 my $hosts = host_regex();
101 my $port = port($stream);
102 if ($url =~ m!^gemini://($hosts):$port/do/test$!) {
103 success($stream, 'text/plain; charset=UTF-8');
104 $stream->write("Test\n");
105 return 1;
106 }
107 return;
108 }
109 EOT
110  
111 This example also shows how to redefine existing code in your config file
112 without the warning "Subroutine … redefined".
113  
114 Here's a more elaborate example to add a new action the main menu and a handler
115 for it, for Gemini only:
116  
117 # tested by t/example-new-action.t
118 package App::Phoebe;
119 use Modern::Perl;
120 our (@extensions, @main_menu);
121 push(@main_menu, "=> gemini://localhost/do/test Test");
122 push(@extensions, \&serve_test);
123 sub serve_test {
124 my $stream = shift;
125 my $url = shift;
126 my $headers = shift;
127 my $host = host_regex();
128 my $port = port($stream);
129 if ($url =~ m!^gemini://($host)(?::$port)?/do/test$!) {
130 result($stream, "20", "text/plain");
131 $stream->write("Test\n");
132 return 1;
133 }
134 return;
135 }
136 1;
137  
138 =cut
139  
140 package App::Phoebe;
141 1 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 1 ▶use File::Slurper qw(read_text read_binary read_dir write_text write_binary);
Subroutine BEGIN: called CC=1 SCAR=0.0
143 1 ▶use Encode qw(encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
144 1 ▶use Net::IDN::Encode qw(domain_to_ascii);
Subroutine BEGIN: called CC=1 SCAR=0.0
145 1 ▶use Socket qw(:addrinfo SOCK_RAW);
Subroutine BEGIN: called CC=1 SCAR=0.0
146 1 ▶use List::Util qw(first min any);
Subroutine BEGIN: called CC=1 SCAR=0.0
147 1 ▶use File::ReadBackwards;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 1 ▶use Algorithm::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 1 our $VERSION = 4.07;
155  
156 1 require Exporter;
157 1 our @ISA = qw(Exporter);
158 1 our @EXPORT_OK = qw(@extensions @main_menu @footer $log $server $full_url_regex
159 space port host_regex space_regex reserved_regex success
160 result get_ip_numbers run_extensions pages blog blog_pages
161 text save_page serve_index serve_page serve_raw serve_html
162 serve_history serve_diff html_page to_html @request_handlers
163 handle_request process_titan process_gemini valid_params
164 valid_id valid_mime_type valid_size valid_token print_link
165 all_logs gemini_link colourize modified changes diff
166 bogus_hash quote_html write_page @known_fingerprints
167 with_lock wiki_dir to_url handle_titan footer atom rss files
168 space_links search decode_query);
169  
170 # Phoebe variables you can set in the config file
171  
172 1 our (@extensions, @main_menu, @footer);
173 1 ▶our $log ||= Mojo::Log->new(level => 'warn');
Condition33%1/3
our $log ||= "Mojo::Log"->new("level", "warn")
l!l&&r!l&&!r
010
MC/DC0%0/2
our $log ||= "Mojo::Log"->new("level", "warn")
our $log "Mojo::Log"->new("level", "warn")
Truth table33%1/3
A ||= B
A= our $logB= "Mojo::Log"->new("level", "warn")
ABresult
1X1
011
000
174 1 our $server = {host => {}};
175  
176 1 our $protocols = 'https?|ftp|afs|news|nntp|mid|cid|mailto|wais|prospero|telnet|gophers?|irc|feed|gemini|xmpp';
177 1 our $chars = '[-a-zA-Z0-9/@=+$_~*.,;:?!\'"()&#%]'; # see RFC 2396
178 1 our $full_url_regex = "((?:$protocols):$chars+)";
179  
180 1 our @known_fingerprints; # only used by extensions
181  
182 # Conventions:
183 # - a regular exression on the first line of the request
184 # - a handle_foo sub to do wait until you're ready (take $stream and $data,
185 # where $data->{buffer} keeps growing with bytes)
186 # - a process_foo sub to finish the job and write stuff back to the $stream
187  
188 1 our @request_handlers = (
189 '^titan://' => \&handle_titan,
190 '^gemini://' => \&handle_gemini,
191 '^[^:/?#]+://([^/?#]*)([^?#]*)(?:\?([^#]*))?(?:#(.*))?$' => \&handle_url,
192 );
193  
194 # Phoebe subroutines you might want to call in your extensions
195  
196 0 ▶sub port {
Subroutine port: not called CC=2 SCAR=17.9
Pod undocumented
197 0 my $stream = shift;
198 0 ▶ return 1965 unless $stream; # if called in a test situation
Branch: 1 branch
BranchTrueFalseTotal
unless $stream 0 0 0
199 0 return $stream->handle->sockport; # the actual port
200 }
201  
202 0 ▶sub get_ip_numbers {
Subroutine get_ip_numbers: not called CC=5 SCAR=34.0
Pod undocumented
203 0 my $hostname = shift;
204 0 my $punycode = domain_to_ascii($hostname);
205 0 my @addresses;
206 0 my ($err, @res) = getaddrinfo($punycode, "", {socktype => SOCK_RAW});
207 0 ▶ $log->error("Cannot determine the IP number of $punycode: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
208 0 for my $ai (@res) {
209 0 my ($err, $ipaddr) = getnameinfo($ai->{addr}, NI_NUMERICHOST, NIx_NOSERV);
210 0 ▶ $log->error("Cannot get a readable IP number of $punycode: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
211 0 ▶ push(@addresses, $ipaddr) if $ipaddr;
Branch: 1 branch
BranchTrueFalseTotal
if $ipaddr 0 0 0
212 }
213 0 return @addresses;
214 }
215  
216 # The hostnames we know we want to serve because they were specified via --host
217 # options.
218 0 ▶sub host_regex {
Subroutine host_regex: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my $stream = shift;
220 0 my $re = join("|", map { quotemeta domain_to_ascii $_ } keys %{$server->{host}});
221 0 return qr($re)i; # case insensitive hostnames
222 }
223  
224 # A regular expression matching wiki spaces in URLs. The tricky part is that we
225 # must strip the hostnames, as these aren't repeated: for a URL like
226 # gemini://localhost:1965/alex/ the regular expression must just match 'alex'
227 # and it's space($stream, 'localhost', 'alex') that will check whether 'alex' is a
228 # legal space for localhost.
229 0 ▶sub space_regex {
Subroutine space_regex: not called CC=4 SCAR=30.0
Pod undocumented
230 0 my @spaces;
231 0 ▶ if (keys %{$server->{host}} > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$$App::Phoebe::server{'host'};} > 1) { } 0 0 0
elsif (@{$App::Phoebe::server->{'wiki_space'};}) { } 0 0 0
232 0 for (@{$server->{wiki_space}}) {
233 0 my ($space) = /\/(.*)/;
234 0 push(@spaces, $space);
235 }
236 0 } elsif (@{$server->{wiki_space}}) {
237 0 @spaces = @{$server->{wiki_space}};
238 }
239 0 return join("|", map { quotemeta } @spaces);
240 }
241  
242 # A regular expression matching parts of reserved paths in URLs. When looking at
243 # gemini://localhost:1965/page/test or gemini://localhost:1965/do/index and
244 # using a client that has an "up" command, you'd end up at
245 # gemini://localhost:1965/page – but what should happen in this case? We should
246 # redirect these requests to gemini://localhost:1965/, I think.
247 0 ▶sub reserved_regex {
Subroutine reserved_regex: not called CC=1 SCAR=6.9
Pod undocumented
248 0 return join("|", qw(do page raw file html history diff));
249 }
250  
251  
252 0 ▶sub success {
Subroutine success: not called CC=3 SCAR=24.8
Pod undocumented
253 0 my $stream = shift;
254 0 ▶ my $type = shift || 'text/gemini; charset=UTF-8';
Condition0%0/2
shift() || 'text/gemini; charset=UTF-8'
l!l
00
MC/DC0%0/1
shift() || 'text/gemini; charset=UTF-8'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
255 0 my $lang = shift;
256 0 ▶ if ($lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) { } 0 0 0
257 0 result($stream, "20", "$type; lang=$lang");
258 } else {
259 0 result($stream, "20", "$type");
260 }
261 }
262  
263 0 ▶sub result {
Subroutine result: not called CC=2 SCAR=17.9
Pod undocumented
264 0 my $stream = shift;
265 0 my $code = shift;
266 0 my $meta = shift;
267 0 ▶ my $data = shift||"";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
268 0 $stream->write("$code $meta\r\n$data");
269 }
270  
271 0 ▶sub handle_titan {
Subroutine handle_titan: not called CC=5 SCAR=34.0
Pod undocumented
272 0 my $stream = shift;
273 0 my $data = shift;
274 # extra processing of the request if we didn't do that, yet
275 0 ▶ $data->{upload} ||= is_upload($stream, $data->{request}) or return;
Branch: 1 branch
BranchTrueFalseTotal
$data->{"upload"} ||= is_upload($stream, $data->{"request"}) or return 0 0 0
Condition0%0/3
$data->{"upload"} ||= is_upload($stream, $data->{"request"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"upload"} ||= is_upload($stream, $data->{"request"})
$data->{"upload"} is_upload($stream, $data->{"request"})
Truth table0%0/3
A ||= B
A= $data->{"upload"}B= is_upload($stream, $data->{"request"})
ABresult
1X1
011
000
276 0 my $size = $data->{upload}->{params}->{size};
277 0 my $actual = length($data->{buffer});
278 0 ▶ if ($actual == $size) {
Branch: 2 branches
BranchTrueFalseTotal
if ($actual == $size) { } 0 0 0
elsif ($actual > $size) { } 0 0 0
279 0 $log->debug("Handle Titan request");
280 0 process_titan($stream, $data->{request}, $data->{upload}, $data->{buffer}, $size);
281 # do not close in case we're waiting for the lock
282 0 return;
283 } elsif ($actual > $size) {
284 0 $log->debug("Received more than the promised $size bytes");
285 0 result($stream, "59", "Received more than the promised $size bytes");
286 0 $stream->close_gracefully();
287 0 return;
288 }
289 0 $log->debug("Waiting for " . ($size - $actual) . " more bytes");
290 }
291  
292 0 ▶sub process_titan {
Subroutine process_titan: not called CC=3 SCAR=24.8
Pod undocumented
293 0 my ($stream, $request, $upload, $buffer, $size) = @_;
294 0 eval {
295 0 ▶ local $SIG{'ALRM'} = sub { $log->error("Timeout processing upload $request") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
296 0 alarm(10); # timeout
297 0 ▶ if (run_extensions($stream, $request, $upload, $buffer, $size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (run_extensions($stream, $request, $upload, $buffer, $size)) { } 0 0 0
298 # config file goes first
299 } else {
300 save_page($stream, $upload->{host}, $upload->{space}, $upload->{id},
301 0 $upload->{params}->{mime}, $buffer, $size);
302 }
303 0 alarm(0);
304 };
305 # save page might still be waiting for the lock so we must not close the
306 # stream: save_page will close the stream
307 0 ▶ return unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
308 0 $log->error("Error: $@");
309 0 $stream->close_gracefully();
310 }
311  
312 0 ▶sub save_page {
Subroutine save_page: not called CC=7 SCAR=40.3
Pod undocumented
313 0 my $stream = shift;
314 0 my $host = shift;
315 0 my $space = shift;
316 0 my $id = shift;
317 0 ▶ my $type = shift || "text/plain";
Condition0%0/2
shift() || 'text/plain'
l!l
00
MC/DC0%0/1
shift() || 'text/plain'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
318 0 my $data = shift;
319 0 my $length = shift;
320 # If the operation succeeds, we can close the stream; if the operation fails,
321 # we can close the stream; but if the operation was rescheduled, we must not
322 # close the stream!
323 0 ▶ if ($type ne "text/plain" and $type ne "text/gemini") {
Branch: 3 branches
BranchTrueFalseTotal
if ($type ne 'text/plain' and $type ne 'text/gemini') { } 0 0 0
elsif ($length == 0) { } 0 0 0
elsif (utf8::decode($data)) { } 0 0 0
Condition0%0/3
$type ne "text/plain" and $type ne "text/gemini"
!ll&&!rl&&r
000
MC/DC0%0/2
$type ne "text/plain" and $type ne "text/gemini"
$type ne "text/plain" $type ne "text/gemini"
Truth table0%0/3
A and B
A= $type ne "text/plain"B= $type ne "text/gemini"
ABresult
0X0
100
111
324 0 ▶ if ($length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 0) { } 0 0 0
325 with_lock($stream, $host, $space,
326 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
327 0 delete_file($stream, $host, $space, $id);
328 0 $stream->close_gracefully();
329 0 });
330 } else {
331 with_lock($stream, $host, $space,
332 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
333 0 write_file($stream, $host, $space, $id, $data, $type);
334 0 $stream->close_gracefully();
335 0 });
336 }
337 } elsif ($length == 0) {
338 with_lock($stream, $host, $space,
339 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
340 0 delete_page($stream, $host, $space, $id);
341 0 $stream->close_gracefully();
342 0 });
343 } elsif (utf8::decode($data)) { # decodes in-place and returns success
344 with_lock($stream, $host, $space,
345 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
346 0 write_page($stream, $host, $space, $id, $data);
347 0 $stream->close_gracefully();
348 0 });
349 } else {
350 0 $log->debug("The text is invalid UTF-8");
351 0 result($stream, "59", "The text is invalid UTF-8");
352 0 $stream->close_gracefully();
353 }
354 }
355  
356 # We can't use C<flock> because this defaults to C<fcntl> which means they are
357 # I<per process>
358 0 ▶sub with_lock {
Subroutine with_lock: not called CC=8 SCAR=42.8
Pod undocumented
359 0 my $stream = shift;
360 0 my $host = shift;
361 0 my $space = shift;
362 0 my $code = shift;
363 0 ▶ my $count = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
364 0 my $dir = wiki_dir($host, $space);
365 0 my $lock = "$dir/locked";
366 # remove stale locks
367 0 ▶ if (-e $lock) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $lock) 0 0 0
368 0 my $age = time() - modified($lock);
369 0 $log->debug("lock is ${age}s old");
370 0 ▶ rmdir $lock if -e $lock and $age > 5;
Branch: 1 branch
BranchTrueFalseTotal
if -e $lock and $age > 5 0 0 0
Condition0%0/3
-e $lock and $age > 5
!ll&&!rl&&r
000
MC/DC0%0/2
-e $lock and $age > 5
-e $lock $age > 5
Truth table0%0/3
A and B
A= -e $lockB= $age > 5
ABresult
0X0
100
111
371 }
372 0 ▶ if (mkdir($lock)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mkdir $lock) { } 0 0 0
elsif ($count > 25) { } 0 0 0
373 0 $log->debug("Running code with lock $lock");
374 0 eval { $code->() }; # protect against exceptions
375 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
376 0 $log->error("Unable to run code with locked $lock: $@");
377 0 result($stream, "40", "An error occured, unfortunately");
378 0 $stream->close_gracefully();
379 }
380 # in the successful case, with_lock doesn't close in case there is more code
381 # that needs to run, or possibly $code has closed the stream.
382 0 rmdir($lock);
383 } elsif ($count > 25) {
384 0 $log->error("Unable to unlock $lock");
385 0 result($stream, "40", "The wiki is locked; try again in a few seconds");
386 0 $stream->close_gracefully();
387 } else {
388 0 $log->debug("Waiting $count...");
389 0 ▶ Mojo::IOLoop->timer(0.2 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
390 0 with_lock($stream, $host, $space, $code, $count + 1)});
391 # don't close the stream
392 }
393 }
394  
395 0 ▶sub write_page {
Subroutine write_page: not called CC=12 SCAR=50.5
Pod undocumented
396 0 my $stream = shift;
397 0 my $host = shift;
398 0 my $space = shift;
399 0 my $id = shift;
400 0 my $text = shift;
401 0 $log->info("Writing page $id");
402 0 my $dir = wiki_dir($host, $space);
403 0 my $file = "$dir/page/$id.gmi";
404 0 my $revision = 0;
405 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 0 0 0
406 0 my $old = read_text($file);
407 0 ▶ if ($old eq $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old eq $text) 0 0 0
408 0 $log->info("$id is unchanged");
409 0 result($stream, "30", to_url($stream, $host, $space, "page/$id"));
410 0 return;
411 }
412 0 ▶ mkdir "$dir/keep" unless -d "$dir/keep";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$dir/keep" 0 0 0
413 0 ▶ if (-d "$dir/keep/$id") {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$dir/keep/$id") { } 0 0 0
414 0 foreach (read_dir("$dir/keep/$id")) {
415 0 ▶ $revision = $1 if m/^(\d+)\.gmi$/ and $1 > $revision;
Branch: 1 branch
BranchTrueFalseTotal
if /^(\d+)\.gmi$/u and $1 > $revision 0 0 0
Condition0%0/3
/^(\d+)\.gmi$/u and $1 > $revision
!ll&&!rl&&r
000
MC/DC0%0/2
/^(\d+)\.gmi$/u and $1 > $revision
/^(\d+)\.gmi$/u $1 > $revision
Truth table0%0/3
A and B
A= /^(\d+)\.gmi$/uB= $1 > $revision
ABresult
0X0
100
111
416 }
417 0 $revision++;
418 } else {
419 0 mkdir "$dir/keep/$id";
420 0 $revision = 1;
421 }
422 0 rename $file, "$dir/keep/$id/$revision.gmi";
423 } else {
424 0 my $index = "$dir/index";
425 0 ▶ if (not open(my $fh, ">>:encoding(UTF-8)", $index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not open(my $fh, '>>:encoding(UTF-8)', $index)) { } 0 0 0
426 0 $log->error("Cannot write index $index: $!");
427 0 result($stream, "59", "Unable to write index");
428 0 return;
429 } else {
430 0 say $fh $id;
431 0 close($fh);
432 }
433 }
434 0 my $changes = "$dir/changes.log";
435 0 ▶ if (not open(my $fh, ">>:encoding(UTF-8)", $changes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not open(my $fh, '>>:encoding(UTF-8)', $changes)) { } 0 0 0
436 0 $log->error("Cannot write log $changes: $!");
437 0 result($stream, "59", "Unable to write log");
438 0 return;
439 } else {
440 0 my $peerhost = $stream->handle->peerhost;
441 0 say $fh join("\x1f", scalar(time), $id, $revision + 1, bogus_hash($peerhost));
442 0 close($fh);
443 }
444 0 ▶ mkdir "$dir/page" unless -d "$dir/page";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$dir/page" 0 0 0
445 0 eval { write_text($file, $text) };
446 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
447 0 $log->error("Unable to save $id: $@");
448 0 result($stream, "59", "Unable to save $id");
449 } else {
450 0 $log->info("Wrote $id");
451 0 result($stream, "30", to_url($stream, $host, $space, "page/$id"));
452 }
453 }
454  
455 0 ▶sub delete_page {
Subroutine delete_page: not called CC=9 SCAR=45.0
Pod undocumented
456 0 my $stream = shift;
457 0 my $host = shift;
458 0 my $space = shift;
459 0 my $id = shift;
460 0 $log->info("Deleting page $id");
461 0 my $dir = wiki_dir($host, $space);
462 0 my $file = "$dir/page/$id.gmi";
463 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
464 0 my $revision = 0;
465 0 ▶ mkdir "$dir/keep" unless -d "$dir/keep";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$dir/keep" 0 0 0
466 0 ▶ if (-d "$dir/keep/$id") {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$dir/keep/$id") { } 0 0 0
467 0 foreach (read_dir("$dir/keep/$id")) {
468 0 ▶ $revision = $1 if m/^(\d+)\.gmi$/ and $1 > $revision;
Branch: 1 branch
BranchTrueFalseTotal
if /^(\d+)\.gmi$/u and $1 > $revision 0 0 0
Condition0%0/3
/^(\d+)\.gmi$/u and $1 > $revision
!ll&&!rl&&r
000
MC/DC0%0/2
/^(\d+)\.gmi$/u and $1 > $revision
/^(\d+)\.gmi$/u $1 > $revision
Truth table0%0/3
A and B
A= /^(\d+)\.gmi$/uB= $1 > $revision
ABresult
0X0
100
111
469 }
470 0 $revision++;
471 } else {
472 0 mkdir "$dir/keep/$id";
473 0 $revision = 1;
474 }
475 # effectively deleting the file
476 0 rename $file, "$dir/keep/$id/$revision.gmi";
477 }
478 0 my $index = "$dir/index";
479 0 ▶ if (-f $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $index) 0 0 0
480 # remove $id from the index
481 0 my @pages = grep { $_ ne $id } split /\n/, read_text $index;
482 0 write_text($index, join("\n", @pages, ""));
483 }
484 0 my $changes = "$dir/changes.log";
485 0 ▶ if (not open(my $fh, ">>:encoding(UTF-8)", $changes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not open(my $fh, '>>:encoding(UTF-8)', $changes)) { } 0 0 0
486 0 $log->error("Cannot write log $changes: $!");
487 0 result($stream, "59", "Unable to write log");
488 0 return;
489 } else {
490 0 my $peerhost = $stream->handle->peerhost;
491 0 say $fh join("\x1f", scalar(time), $id, "🖹", bogus_hash($peerhost));
492 0 close($fh);
493 }
494 0 $log->info("Deleted page $id");
495 0 result($stream, "30", to_url($stream, $host, $space, "page/$id"));
496 }
497  
498 0 ▶sub handle_gemini {
Subroutine handle_gemini: not called CC=2 SCAR=17.9
Pod undocumented
499 0 my $stream = shift;
500 0 my $data = shift;
501 0 $log->debug("Handle Gemini request");
502 $log->debug("Discarding " . length($data->{buffer}) . " bytes")
503 0 ▶ if $data->{buffer};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"buffer"} 0 0 0
504 0 process_gemini($stream, $data->{request});
505 }
506  
507 0 ▶sub process_gemini {
Subroutine process_gemini: not called CC=45 SCAR=76.4
Pod undocumented
508 0 my ($stream, $url) = @_;
509 0 eval {
510 0 ▶ local $SIG{'ALRM'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
511 0 $log->error("Timeout processing $url");
512 0 };
513 0 alarm(10); # timeout
514 0 my $hosts = host_regex();
515 0 my $port = port($stream);
516 0 my $spaces = space_regex();
517 0 my $reserved = reserved_regex($stream);
518 0 $log->debug("Serving ($hosts)(?::$port)?");
519 0 $log->debug("Spaces $spaces");
520 0 my($scheme, $authority, $path, $query, $fragment) =
521 $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
522 0 $log->info("Looking at $url");
523 0 my ($host, $space, $id, $n, $style, $filter);
524 0 ▶ if (run_extensions($stream, $url)) {
Branch: 32 branches
BranchTrueFalseTotal
if (run_extensions($stream, $url)) { } 0 0 0
elsif (not $url) { } 0 0 0
elsif (length $url > 1024) { } 0 0 0
elsif (($host, $n, $space) = $url =~ m[^(?:gemini:)?//($hosts)(:$port)?(?:/($spaces))?/(?:$reserved)$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/more(?:/(\d+))?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/index$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/files$]u) { } 0 0 0
elsif (($host) = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/spaces$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/data$]u) { } 0 0 0
elsif ($url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/match$]u) { } 0 0 0
elsif ($query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/match\?]u) { } 0 0 0
elsif ($url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/search$]u) { } 0 0 0
elsif ($query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/search\?]u) { } 0 0 0
elsif ($url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/new$]u) { } 0 0 0
elsif ($query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/new\?]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/changes(?:/(\d+))?(?:/(colour|fancy))?$]u) { } 0 0 0
elsif (($host, $filter, $n, $style) = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?/do/all(?:/(latest))?/changes(?:/(\d+))?(?:/(colour|fancy))?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/rss$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/blog/rss$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/atom$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/blog/atom$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/all/atom$]u) { } 0 0 0
elsif (($host) = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?/robots.txt(?:[#?].*)?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/history/([^/]*)(?:/(\d+))?(?:/(colour|fancy))?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/diff/([^/]*)(?:/(\d+))?(?:/(colour))?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/raw/([^/]*)(?:/(\d+))?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/html/([^/]*)(?:/(\d+))?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/page/([^/]+)(?:/(\d+))?$]u) { } 0 0 0
elsif (() = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/file/([^/]+)?$]u) { } 0 0 0
elsif (($host) = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(/|$)]u) { } 0 0 0
elsif ($authority) { } 0 0 0
525 # config file goes first
526 } elsif (not $url) {
527 0 $log->debug("The URL is empty");
528 0 result($stream, "59", "URL expected");
529 } elsif (length($url) > 1024) {
530 0 $log->debug("The URL is too long");
531 0 result($stream, "59", "The URL is too long");
532 } elsif (($host, $n, $space) = $url =~ m!^(?:gemini:)?//($hosts)(:$port)?(?:/($spaces))?/(?:$reserved)$!) {
533 # redirect gemini://localhost:2020/do to gemini://localhost:2020/
534 # redirect gemini://localhost:2020/space/do to gemini://localhost:2020/space
535 0 ▶ $space = space($stream, $host, $space) || "";
Condition0%0/2
space($stream, $host, $space) || ''
l!l
00
MC/DC0%0/1
space($stream, $host, $space) || ''
space($stream, $host, $space)
Truth table0%0/2
A
A= space($stream, $host, $space)
Aresult
11
00
536 0 ▶ result($stream, "31", "gemini://$host" . ($n ? ":$port" : "") . "/$space"); # this supports "up"
Branch: 1 branch
BranchTrueFalseTotal
$n ? : 0 0 0
537 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/?$!) {
538 0 serve_main_menu($stream, $host, space($stream, $host, $space));
539 } elsif (($host, $space, $n) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/more(?:/(\d+))?$!) {
540 0 serve_blog($stream, $host, space($stream, $host, $space), $n);
541 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/index$!) {
542 0 serve_index($stream, $host, space($stream, $host, $space));
543 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/files$!) {
544 0 serve_files($stream, $host, space($stream, $host, $space));
545 } elsif (($host) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/spaces$!) {
546 0 serve_spaces($stream, $host, $port);
547 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/data$!) {
548 0 serve_data($stream, $host, space($stream, $host, $space));
549 } elsif ($url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/match$!) {
550 0 result($stream, "10", "Find page by name (Perl regex)");
551 ▶ } elsif ($query and ($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/match\?!) {
Condition0%0/3
$query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/match\?]u
!ll&&!rl&&r
000
MC/DC0%0/2
$query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/match\?]u
$query () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/match\?]u
Truth table0%0/3
A and B
A= $queryB= () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/match\?]u
ABresult
0X0
100
111
552 0 serve_match($stream, $host, space($stream, $host, $space), decode_query($query));
553 } elsif ($url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/search$!) {
554 0 result($stream, "10", "Find page by content (Perl regex)");
555 ▶ } elsif ($query and ($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/search\?!) {
Condition0%0/3
$query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/search\?]u
!ll&&!rl&&r
000
MC/DC0%0/2
$query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/search\?]u
$query () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/search\?]u
Truth table0%0/3
A and B
A= $queryB= () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/search\?]u
ABresult
0X0
100
111
556 0 serve_search($stream, $host, space($stream, $host, $space), decode_query($query)); # search terms include spaces
557 } elsif ($url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/new$!) {
558 0 result($stream, "10", "New page");
559 # no URI escaping required
560 ▶ } elsif ($query and ($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/new\?!) {
Condition0%0/3
$query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/new\?]u
!ll&&!rl&&r
000
MC/DC0%0/2
$query and () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/new\?]u
$query () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/new\?]u
Truth table0%0/3
A and B
A= $queryB= () = $url =~ m[^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/new\?]u
ABresult
0X0
100
111
561 0 ▶ if ($space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space) { } 0 0 0
562 0 result($stream, "30", "gemini://$host:$port/$space/raw/$query");
563 } else {
564 0 result($stream, "30", "gemini://$host:$port/raw/$query");
565 }
566 } elsif (($host, $space, $n, $style) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/changes(?:/(\d+))?(?:/(colour|fancy))?$!) {
567 0 ▶ serve_changes($stream, $host, space($stream, $host, $space), $n||100, $style);
Condition0%0/2
$n || 100
l!l
00
MC/DC0%0/1
$n || 100
$n
Truth table0%0/2
A
A= $n
Aresult
11
00
568 } elsif (($host, $filter, $n, $style) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?/do/all(?:/(latest))?/changes(?:/(\d+))?(?:/(colour|fancy))?$!) {
569 0 ▶ serve_all_changes($stream, $host, $n||100, $style||"", $filter||"");
Condition0%0/6
$n || 100
l!l
00
$style || ''
l!l
00
$filter || ''
l!l
00
MC/DC0%0/1
$n || 100
$n
MC/DC0%0/1
$style || ''
$style
MC/DC0%0/1
$filter || ''
$filter
Truth table0%0/2
A
A= $n
Aresult
11
00
Truth table0%0/2
A
A= $style
Aresult
11
00
Truth table0%0/2
A
A= $filter
Aresult
11
00
570 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/rss$!) {
571 0 serve_rss($stream, $host, space($stream, $host, $space));
572 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/blog/rss$!) {
573 0 serve_blog_rss($stream, $host, space($stream, $host, $space));
574 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/atom$!) {
575 0 serve_atom($stream, $host, space($stream, $host, $space));
576 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/blog/atom$!) {
577 0 serve_blog_atom($stream, $host);
578 } elsif (($host, $space) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/do/all/atom$!) {
579 0 serve_all_atom($stream, $host);
580 } elsif (($host) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?/robots.txt(?:[#?].*)?$!) {
581 0 serve_raw($stream, $host, undef, "robots");
582 } elsif (($host, $space, $id, $n, $style) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/history/([^/]*)(?:/(\d+))?(?:/(colour|fancy))?$!) {
583 0 ▶ serve_history($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n||10, $style);
Condition0%0/2
$n || 10
l!l
00
MC/DC0%0/1
$n || 10
$n
Truth table0%0/2
A
A= $n
Aresult
11
00
584 } elsif (($host, $space, $id, $n, $style) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/diff/([^/]*)(?:/(\d+))?(?:/(colour))?$!) {
585 0 serve_diff($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n, $style);
586 } elsif (($host, $space, $id, $n) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/raw/([^/]*)(?:/(\d+))?$!) {
587 0 serve_raw($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n);
588 } elsif (($host, $space, $id, $n) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/html/([^/]*)(?:/(\d+))?$!) {
589 0 serve_html($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n);
590 } elsif (($host, $space, $id, $n) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/page/([^/]+)(?:/(\d+))?$!) {
591 0 serve_page($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n);
592 } elsif (($host, $space, $id) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(?:/($spaces))?/file/([^/]+)?$!) {
593 0 serve_file($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)));
594 } elsif (($host) = $url =~ m!^(?:gemini:)?//($hosts)(?::$port)?(/|$)!) {
595 0 $log->info("Unknown path for $url\r");
596 0 result($stream, "51", "Path not found for $url");
597 } elsif ($authority) {
598 0 $log->info("Unsupported proxy request for $url");
599 0 result($stream, "53", "Unsupported proxy request for $url");
600 } else {
601 0 $log->info("No handler for $url");
602 0 result($stream, "59", "Don't know how to handle $url");
603 }
604 0 $log->debug("Done");
605 };
606 0 ▶ $log->error("Error: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
607 0 alarm(0);
608 0 $stream->close_gracefully();
609 }
610  
611 0 ▶sub decode_query {
Subroutine decode_query: not called CC=2 SCAR=17.9
Pod undocumented
612 0 my $query = shift;
613 0 ▶ return $query unless $query;
Branch: 1 branch
BranchTrueFalseTotal
unless $query 0 0 0
614 0 $query = decode_utf8(uri_unescape($query));
615 0 $query =~ s/\+/ /g;
616 0 return $query;
617 }
618  
619 0 ▶sub run_extensions {
Subroutine run_extensions: not called CC=3 SCAR=24.8
Pod undocumented
620 0 foreach my $sub (@extensions) {
621 0 ▶ return 1 if $sub->(@_);
Branch: 1 branch
BranchTrueFalseTotal
if &$sub(@_) 0 0 0
622 }
623 0 return;
624 }
625  
626 0 ▶sub serve_main_menu {
Subroutine serve_main_menu: not called CC=8 SCAR=42.8
Pod undocumented
627 0 my $stream = shift;
628 0 ▶ my $host = shift||"";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
629 0 ▶ my $space = shift||"";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
630 0 $log->info("Serving main menu");
631 0 success($stream);
632 0 my $page = $server->{wiki_main_page};
633 0 ▶ if ($page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page) { } 0 0 0
634 0 $stream->write(encode_utf8 text($stream, $host, $space, $page) . "\n");
635 } else {
636 0 $stream->write("# Welcome to Phoebe!\n");
637 0 $stream->write("\n");
638 }
639 0 blog($stream, $host, $space, 10);
640 0 for my $id (@{$server->{wiki_page}}) {
641 0 print_link($stream, $host, $space, $id);
642 }
643 0 for my $line (@main_menu) {
644 0 $stream->write(encode_utf8 $line . "\n");
645 }
646 0 print_link($stream, $host, $space, "Changes", "do/changes");
647 0 print_link($stream, $host, $space, "Search matching page names", "do/match");
648 0 print_link($stream, $host, $space, "Search matching page content", "do/search");
649 0 print_link($stream, $host, $space, "New page", "do/new");
650 0 $stream->write("\n");
651 0 print_link($stream, $host, $space, "Index of all pages", "do/index");
652 0 print_link($stream, $host, $space, "Index of all files", "do/files");
653 print_link($stream, $host, undef, "Index of all spaces", "do/spaces")
654 0 ▶ if @{$server->{wiki_space}} or keys %{$server->{host}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$App::Phoebe::server->{"wiki_space"};} or keys %{$$App::Phoebe::server{"host"};} > 1 0 0 0
Condition0%0/3
@{$App::Phoebe::server->{"wiki_space"};} or keys %{$$App::Phoebe::server{"host"};} > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$App::Phoebe::server->{"wiki_space"};} or keys %{$$App::Phoebe::server{"host"};} > 1
@{$App::Phoebe::server->{"wiki_space"};} keys %{$$App::Phoebe::server{"host"};} > 1
Truth table0%0/3
A or B
A= @{$App::Phoebe::server->{"wiki_space"};}B= keys %{$$App::Phoebe::server{"host"};} > 1
ABresult
1X1
011
000
655 0 print_link($stream, $host, $space, "Download data", "do/data");
656 # a requirement of the GNU Affero General Public License
657 0 $stream->write("=> https://metacpan.org/pod/App::Phoebe Source code\n");
658 0 $stream->write("\n");
659 }
660  
661 0 ▶sub handle_request {
Subroutine handle_request: not called CC=4 SCAR=30.0
Pod undocumented
662 0 my $stream = shift;
663 0 my $data = shift;
664 0 ▶ if ($data->{buffer} =~ /^(.*)\r\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'buffer'} =~ /^(.*)\r\n/u) { } 0 0 0
665 0 $data->{request} = $1;
666 0 $data->{buffer} =~ s/.*\r\n//;
667 0 $log->debug("Looking at $data->{request}");
668 0 ▶ for (my $i = 0; $i < @request_handlers; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @App::Phoebe::request_handlers) 0 0 0
669 0 my $re = $request_handlers[$i];
670 0 ▶ if ($data->{request} =~ m!$re!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"request"} =~ /$re/iu) 0 0 0
671 0 $data->{handler} = $request_handlers[$i+1];
672 # and call the handler
673 0 $data->{handler}->($stream, $data);
674 0 return;
675 }
676 }
677 0 $log->debug("No handler found for $data->{request}");
678 0 result($stream, "59", "Cannot handle this request");
679 0 $stream->close_gracefully();
680 } else {
681 0 $log->debug("Waiting for more bytes...");
682 }
683 }
684  
685 # special generic URL error handling to satisfy gemini-diagnostics
686 0 ▶sub handle_url {
Subroutine handle_url: not called CC=2 SCAR=17.9
Pod undocumented
687 0 my $stream = shift;
688 0 my $data = shift;
689 0 $log->debug("Unhandled proxy request");
690 $log->debug("Discarding " . length($data->{buffer}) . " bytes")
691 0 ▶ if $data->{buffer};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"buffer"} 0 0 0
692 0 result($stream, "53", "No proxying for $data->{request}");
693 0 $stream->close_gracefully();
694 }
695  
696 # if you call this yourself, $id must look like "page/foo"
697 0 ▶sub to_url {
Subroutine to_url: not called CC=5 SCAR=34.0
Pod undocumented
698 0 my $stream = shift;
699 0 my $host = lc shift;
700 0 my $space = shift;
701 0 my $id = shift;
702 0 ▶ my $scheme = shift || "gemini";
Condition0%0/2
shift() || 'gemini'
l!l
00
MC/DC0%0/1
shift() || 'gemini'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
703 0 my $port = port($stream);
704 0 ▶ if ($space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space) 0 0 0
705 0 ▶ $space = "" if $space eq $host;
Branch: 1 branch
BranchTrueFalseTotal
if $space eq $host 0 0 0
706 0 $space =~ s/.*\///;
707 0 $space = uri_escape_utf8($space);
708 }
709 # don't encode the slash
710 return "$scheme://$host:$port/"
711 . ($space ? "$space/" : "")
712 0 ▶ . join("/", map { uri_escape_utf8($_) } split (/\//, $id));
Branch: 1 branch
BranchTrueFalseTotal
$space ? : 0 0 0
713 }
714  
715 0 ▶sub gemini_link {
Subroutine gemini_link: not called CC=3 SCAR=24.8
Pod undocumented
716 0 my $stream = shift;
717 0 my $host = shift;
718 0 my $space = shift;
719 0 my $title = shift;
720 0 my $id = shift;
721 0 ▶ if (not $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 0 0
722 0 $id = "page/$title";
723 }
724 0 ▶ return "=> $id $title" if $id =~ /^$full_url_regex$/;
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /^$App::Phoebe::full_url_regex$/u 0 0 0
725 0 my $url = to_url($stream, $host, $space, $id);
726 0 return "=> $url $title";
727 }
728  
729 0 ▶sub print_link {
Subroutine print_link: not called CC=1 SCAR=6.9
Pod undocumented
730 0 my $stream = shift;
731 0 my $host = shift;
732 0 my $space = shift;
733 0 my $title = shift;
734 0 my $id = shift;
735 0 $stream->write(encode_utf8 gemini_link($stream, $host, $space, $title, $id) . "\n");
736 }
737  
738 0 ▶sub newest_first {
Subroutine newest_first: not called CC=5 SCAR=34.0
Pod undocumented
739 0 my ($date_a, $article_a) = $a =~ /^(\d\d\d\d-\d\d(?:-\d\d)? ?)?(.*)/;
740 0 my ($date_b, $article_b) = $b =~ /^(\d\d\d\d-\d\d(?:-\d\d)? ?)?(.*)/;
741 0 ▶ return (($date_b and $date_a and $date_b cmp $date_a)
Condition0%0/12
$date_b && $date_a && $date_b cmp $date_a || $article_a cmp $article_b || $a cmp $b
l!l&&r!l&&!r
000
$date_b and $date_a and $date_b cmp $date_a or $article_a cmp $article_b
l!l&&r!l&&!r
000
$date_b and $date_a and $date_b cmp $date_a
!ll&&!rl&&r
000
$date_b and $date_a
!ll&&!rl&&r
000
MC/DC0%0/5
$date_b && $date_a && $date_b cmp $date_a || $article_a cmp $article_b || $a cmp $b
$date_b $date_a $date_b cmp $date_a $article_a cmp $article_b $a cmp $b
Truth table0%0/10
A and B and C or D || E
A= $date_bB= $date_aC= $date_b cmp $date_aD= $article_a cmp $article_bE= $a cmp $b
ABCDEresult
111XX1
0XX1X1
10X1X1
1101X1
0XX011
10X011
110011
0XX000
10X000
110000
742 || ($article_a cmp $article_b)
743 # this last one should be unnecessary
744 || ($a cmp $b));
745 }
746  
747 0 ▶sub pages {
Subroutine pages: not called CC=4 SCAR=30.0
Pod undocumented
748 0 my $stream = shift; # used by contributions like oddmuse.pl
749 0 my $host = shift;
750 0 my $space = shift;
751 0 my $re = shift;
752 0 my $dir = wiki_dir($host, $space);
753 0 my $index = "$dir/index";
754 0 ▶ if (not -f $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $index) 0 0 0
755 0 ▶ return if not -d "$dir/page";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$dir/page" 0 0 0
756 0 my @pages = map { s/\.gmi$//; $_ } read_dir("$dir/page");
757 0 write_text($index, join("\n", @pages, ""));
758 0 return sort newest_first @pages;
759 }
760 0 my @lines = sort newest_first split /\n/, read_text $index;
761 0 ▶ return grep /$re/i, @lines if $re;
Branch: 1 branch
BranchTrueFalseTotal
if $re 0 0 0
762 0 return @lines;
763 }
764  
765 0 ▶sub blog_pages {
Subroutine blog_pages: not called CC=1 SCAR=6.9
Pod undocumented
766 0 my $stream = shift; # used by contributions like oddmuse.pl
767 0 my $host = shift;
768 0 my $space = shift;
769 0 my $n = shift; # used by contributions like oddmuse.pl
770 0 return sort { $b cmp $a } pages($stream, $host, $space, '^\d\d\d\d-\d\d-\d\d');
771 }
772  
773 0 ▶sub blog {
Subroutine blog: not called CC=7 SCAR=40.3
Pod undocumented
774 0 my $stream = shift;
775 0 my $host = shift;
776 0 my $space = shift;
777 0 ▶ my $n = shift || 10;
Condition0%0/2
shift() || 10
l!l
00
MC/DC0%0/1
shift() || 10
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
778 0 my @blog = blog_pages($stream, $host, $space, $n);
779 0 ▶ return unless @blog;
Branch: 1 branch
BranchTrueFalseTotal
unless @blog 0 0 0
780 0 $stream->write("Blog:\n");
781 # we should check for pages marked for deletion!
782 0 for my $id (@blog[0 .. min($#blog, $n - 1)]) {
783 0 print_link($stream, $host, $space, $id);
784 }
785 0 ▶ print_link($stream, $host, $space, "More...", "do/more/" . ($n * 10)) if @blog > $n;
Branch: 1 branch
BranchTrueFalseTotal
if @blog > $n 0 0 0
786 0 ▶ print_link($stream, $host, $space, "Atom Feed", "do/blog/atom") if $n == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 10 0 0 0
787 0 ▶ print_link($stream, $host, $space, "RSS Feed", "do/blog/rss") if $n == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 10 0 0 0
788 0 $stream->write("\n");
789 }
790  
791 0 ▶sub quote_html {
Subroutine quote_html: not called CC=1 SCAR=6.9
Pod undocumented
792 0 my $html = shift;
793 0 $html =~ s/&/&amp;/g;
794 0 $html =~ s/</&lt;/g;
795 0 $html =~ s/>/&gt;/g;
796 0 $html =~ s/[\x00-\x08\x0b\x0c\x0e-\x1f]/ /g; # legal xml: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
797 0 return $html;
798 }
799  
800 0 ▶sub serve_blog {
Subroutine serve_blog: not called CC=4 SCAR=30.0
Pod undocumented
801 0 my $stream = shift;
802 0 my $host = shift;
803 0 my $space = shift;
804 0 my $n = shift;
805 0 success($stream);
806 0 $log->info("Serving blog");
807 0 $stream->write("# Blog\n");
808 0 my @blog = blog_pages($stream, $host, $space, $n);
809 0 ▶ if (not @blog) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@blog) 0 0 0
810 0 $stream->write("There are no blog pages.\n");
811 0 return;
812 }
813 0 $stream->write("Serving up to $n entries.\n");
814 0 for my $id (@blog[0 .. min($#blog, $n - 1)]) {
815 0 print_link($stream, $host, $space, $id);
816 }
817 0 ▶ print_link($stream, $host, $space, "More...", "do/more/" . ($n * 10)) if @blog > $n;
Branch: 1 branch
BranchTrueFalseTotal
if @blog > $n 0 0 0
818 }
819  
820 0 ▶sub serve_index {
Subroutine serve_index: not called CC=3 SCAR=24.8
Pod undocumented
821 0 my $stream = shift;
822 0 my $host = shift;
823 0 my $space = shift;
824 0 success($stream);
825 0 $log->info("Serving index of all pages");
826 0 $stream->write("# All Pages\n");
827 0 my @pages = pages($stream, $host, $space);
828 0 ▶ $stream->write("There are no pages.\n") unless @pages;
Branch: 1 branch
BranchTrueFalseTotal
unless @pages 0 0 0
829 0 for my $id (@pages) {
830 0 print_link($stream, $host, $space, $id);
831 }
832 }
833  
834 0 ▶sub files {
Subroutine files: not called CC=3 SCAR=24.8
Pod undocumented
835 0 my $stream = shift;
836 0 my $host = shift;
837 0 my $space = shift;
838 0 my $re = shift;
839 0 my $dir = wiki_dir($host, $space);
840 0 $dir = "$dir/file";
841 0 ▶ return if not -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
842 0 my @files = map { decode_utf8($_) } read_dir($dir);
843 0 ▶ return grep /$re/i, @files if $re;
Branch: 1 branch
BranchTrueFalseTotal
if $re 0 0 0
844 0 return @files;
845 }
846  
847 0 ▶sub serve_files {
Subroutine serve_files: not called CC=3 SCAR=24.8
Pod undocumented
848 0 my $stream = shift;
849 0 my $host = shift;
850 0 my $space = shift;
851 0 success($stream);
852 0 $log->info("Serving index of all files");
853 0 $stream->write("# All Files\n");
854 0 my @files = files($stream, $host, $space);
855 0 ▶ $stream->write("The are no files.\n") unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
856 0 for my $id (sort @files) {
857 0 print_link($stream, $host, $space, $id, "file/$id");
858 }
859 }
860  
861 0 ▶sub serve_spaces {
Subroutine serve_spaces: not called CC=2 SCAR=17.9
Pod undocumented
862 0 my $stream = shift;
863 0 my $host = shift;
864 0 my $port = shift;
865 0 success($stream);
866 0 $log->info("Serving all spaces");
867 0 $stream->write("# Spaces\n");
868 0 my $spaces = space_links($stream, "gemini", $host, $port);
869 0 for my $url (sort keys %$spaces) {
870 0 $stream->write(encode_utf8 "=> $url $spaces->{$url}\n");
871 }
872 }
873  
874 0 ▶sub serve_data {
Subroutine serve_data: not called CC=4 SCAR=30.0
Pod undocumented
875 0 my $stream = shift;
876 0 my $host = shift;
877 0 my $space = shift;
878 # use /bin/tar instead of Archive::Tar to save memory
879 0 my $dir = wiki_dir($host, $space);
880 0 my $file = "$dir/data.tar.gz";
881 0 ▶ if (-e $file and time() - modified($file) <= 300) { # data is valid for 5 minutes
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and time - modified($file) <= 300) { } 0 0 0
Condition0%0/3
-e $file and time - modified($file) <= 300
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and time - modified($file) <= 300
-e $file time - modified($file) <= 300
Truth table0%0/3
A and B
A= -e $fileB= time - modified($file) <= 300
ABresult
0X0
100
111
882 0 $log->info("Serving cached data archive");
883 0 success($stream, "application/tar");
884 0 $stream->write(read_binary($file));
885 } else {
886 0 write_binary($file, ""); # truncate in order to avoid "file changed as we read it" warning
887 0 my @command = ('/bin/tar', '--create', '--gzip',
888 '--file', $file,
889 '--exclude', "data.tar.gz",
890 '--directory', "$dir/..",
891 ((split(/\//,$dir))[-1]));
892 0 $log->debug("@command");
893 0 ▶ if (system(@command) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system(@command) == 0) { } 0 0 0
894 0 $log->info("Serving new data archive");
895 0 success($stream, "application/tar");
896 0 $stream->write(read_binary($file));
897 } else {
898 0 $log->error("Creation of data archive failed");
899 0 result($stream, "59", "Archive creation failed");
900 }
901 }
902 }
903  
904 0 ▶sub serve_match {
Subroutine serve_match: not called CC=4 SCAR=30.0
Pod undocumented
905 0 my $stream = shift;
906 0 my $host = shift;
907 0 my $space = shift;
908 0 my $match = shift;
909 0 ▶ if (not $match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($match) 0 0 0
910 0 result($stream, "59", "Search term is missing");
911 0 return;
912 }
913 0 success($stream);
914 0 $log->info("Serving pages matching $match");
915 0 $stream->write(encode_utf8 "# Search page titles for $match\n");
916 0 $stream->write("Use a Perl regular expression to match page titles.\n");
917 0 my @pages = pages($stream, $host, $space, $match);
918 0 ▶ $stream->write("No matching page names found.\n") unless @pages;
Branch: 1 branch
BranchTrueFalseTotal
unless @pages 0 0 0
919 0 for my $id (@pages) {
920 0 print_link($stream, $host, $space, $id);
921 }
922 }
923  
924 0 ▶sub serve_search {
Subroutine serve_search: not called CC=3 SCAR=24.8
Pod undocumented
925 0 my $stream = shift;
926 0 my $host = shift;
927 0 my $space = shift;
928 0 my $str = shift;
929 0 ▶ if (not $str) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str) 0 0 0
930 0 result($stream, "59", "Search term is missing");
931 0 return;
932 }
933 0 success($stream);
934 0 $log->info("Serving search result for $str");
935 0 $stream->write(encode_utf8 "# Search page content for $str\n");
936 0 $stream->write("Use a Perl regular expression to match page titles and page content.\n");
937 0 ▶ if (not search($stream, $host, $space, $str, sub { highlight($stream, @_) })) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (not search($stream, $host, $space, $str, sub { highlight($stream, @_); } )) 0 0 0
938 0 $stream->write("Search term not found.\n");
939 }
940 }
941  
942 0 ▶sub search {
Subroutine search: not called CC=5 SCAR=34.0
Pod undocumented
943 0 my $stream = shift;
944 0 my $host = shift;
945 0 my $space = shift;
946 0 my $str = shift;
947 0 my $func = shift;
948 0 my @pages = pages($stream, $host, $space);
949 0 ▶ return unless @pages;
Branch: 1 branch
BranchTrueFalseTotal
unless @pages 0 0 0
950 0 my $found = 0;
951 0 for my $id (@pages) {
952 0 my $text = text($stream, $host, $space, $id);
953 0 ▶ if ($id =~ /$str/i or $text =~ /$str/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /$str/iu or $text =~ /$str/iu) 0 0 0
Condition0%0/3
$id =~ /$str/iu or $text =~ /$str/iu
l!l&&r!l&&!r
000
MC/DC0%0/2
$id =~ /$str/iu or $text =~ /$str/iu
$id =~ /$str/iu $text =~ /$str/iu
Truth table0%0/3
A or B
A= $id =~ /$str/iuB= $text =~ /$str/iu
ABresult
1X1
011
000
954 0 $func->($host, $space, $id, $text, $str);
955 0 $found++;
956 }
957 }
958 0 return $found;
959 }
960  
961 0 ▶sub highlight {
Subroutine highlight: not called CC=6 SCAR=37.4
Pod undocumented
962 0 my $stream = shift;
963 0 my $host = shift;
964 0 my $space = shift;
965 0 my $id = shift;
966 0 my $text = shift;
967 0 my $str = shift;
968 0 my ($snippetlen, $maxsnippets) = (100, 4); # these seem nice.
969 # show a snippet from the beginning of the document
970 0 my $j = index($text, ' ', $snippetlen); # end on word boundary
971 0 my $t = substr($text, 0, $j);
972 0 my $result = "## $id\n$t … ";
973 0 $text = substr($text, $j); # to avoid rematching
974 0 my $jsnippet = 0 ;
975 0 ▶ while ($jsnippet < $maxsnippets and $text =~ m/($str)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jsnippet < $maxsnippets and $text =~ /($str)/iu) 0 0 0
Condition0%0/3
$jsnippet < $maxsnippets and $text =~ /($str)/iu
!ll&&!rl&&r
000
MC/DC0%0/2
$jsnippet < $maxsnippets and $text =~ /($str)/iu
$jsnippet < $maxsnippets $text =~ /($str)/iu
Truth table0%0/3
A and B
A= $jsnippet < $maxsnippetsB= $text =~ /($str)/iu
ABresult
0X0
100
111
976 0 $jsnippet++;
977 0 ▶ if (($j = index($text, $1)) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($j = index($text, $1)) > -1) 0 0 0
978 # get substr containing (start of) match, ending on word boundaries
979 0 my $start = index($text, ' ', $j - $snippetlen / 2);
980 0 ▶ $start = 0 if $start == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $start == -1 0 0 0
981 0 my $end = index($text, ' ', $j + $snippetlen / 2);
982 0 ▶ $end = length($text) if $end == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $end == -1 0 0 0
983 0 $t = substr($text, $start, $end - $start);
984 0 $result .= $t . ' … ';
985 # truncate text to avoid rematching the same string.
986 0 $text = substr($text, $end);
987 }
988 }
989 0 $stream->write(encode_utf8 $result . "\n");
990 0 print_link($stream, $host, $space, $id);
991 }
992  
993 0 ▶sub serve_changes {
Subroutine serve_changes: not called CC=8 SCAR=42.8
Pod undocumented
994 0 my $stream = shift;
995 0 my $host = shift;
996 0 my $space = shift;
997 0 my $n = shift;
998 0 my $style = shift;
999 0 $log->info("Serving $n changes");
1000 0 success($stream);
1001 0 $stream->write("# Changes\n");
1002 0 ▶ if (not $style) { print_link($stream, $host, $space, "Colour changes", "do/changes/$n/colour") }
Branch: 3 branches
BranchTrueFalseTotal
if (not $style) { } 0 0 0
elsif ($style eq 'colour') { } 0 0 0
elsif ($style eq 'fancy') { } 0 0 0
1003 0 elsif ($style eq "colour") { print_link($stream, $host, $space, "Fancy changes", "do/changes/$n/fancy") }
1004 0 elsif ($style eq "fancy") { print_link($stream, $host, $space, "Normal changes", "do/changes/$n") }
1005 print_link($stream, $host, undef, "Changes for all spaces", "do/all/changes")
1006 0 ▶ if @{$server->{wiki_space}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$App::Phoebe::server->{"wiki_space"};} 0 0 0
1007 0 print_link($stream, $host, $space, "Atom Feed", "do/atom");
1008 0 print_link($stream, $host, $space, "RSS Feed", "do/rss");
1009 0 my $dir = wiki_dir($host, $space);
1010 0 my $log = "$dir/changes.log";
1011 0 ▶ if (not -e $log) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $log) 0 0 0
1012 0 $stream->write("No changes.\n");
1013 0 return;
1014 }
1015 0 $stream->write("Showing up to $n changes.\n");
1016 0 my $fh = File::ReadBackwards->new($log);
1017 return unless changes($stream,
1018 $n,
1019 0 ▶ sub { $stream->write(encode_utf8 "## " . shift . "\n") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1020 0 ▶ sub { $stream->write(shift . " by " . colourize($stream, shift, $style) . "\n") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1021 0 ▶ sub { print_link($stream, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1022 0 ▶ sub { $stream->write(encode_utf8 join("\n", @_, "")) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1023 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1024 0 ▶ return unless $_ = decode_utf8($fh->readline);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = &decode_utf8(scalar $fh->readline) 0 0 0
1025 0 chomp;
1026 0 split(/\x1f/), $host, $space, 0 },
1027 undef, #$kept
1028 undef, #$filter
1029 0 ▶ $style);
Branch: 1 branch
BranchTrueFalseTotal
unless changes($stream, $n, sub { $stream->write(&encode_utf8('## ' . (shift()) . "\n")); } , sub { $stream->write((shift()) . ' by ' . colourize($stream, shift(), $style) . "\n"); } , sub { print_link($stream, @_); } , sub { $stream->write(&encode_utf8(join("\n", @_, ""))); } , sub { return unless $_ = &decode_utf8(scalar $fh->readline); chomp $_; split(/\x1f/u, $_, 0), $host, $space, 0; } , undef, undef, $style) 0 0 0
1030 0 $stream->write("\n");
1031 0 ▶ print_link($stream, $host, $space, "More...", "do/changes/" . 10 * $n . ($style ? "/$style" : ""));
Branch: 1 branch
BranchTrueFalseTotal
$style ? : 0 0 0
1032 }
1033  
1034 0 ▶sub serve_all_changes {
Subroutine serve_all_changes: not called CC=12 SCAR=50.5
Pod undocumented
1035 0 my $stream = shift;
1036 0 my $host = shift;
1037 0 my $n = shift;
1038 0 my $style = shift;
1039 0 my $filter = shift;
1040 0 ▶ $log->info($filter ? "Serving $n all $filter changes" : "Serving $n all changes");
Branch: 1 branch
BranchTrueFalseTotal
$filter ? : 0 0 0
1041 0 success($stream);
1042 0 $stream->write("# Changes for all spaces\n");
1043 # merge all logs
1044 0 my $log = all_logs($stream, $host, $n);
1045 0 ▶ if (not @$log) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$log) 0 0 0
1046 0 $stream->write("No changes.\n");
1047 0 return;
1048 }
1049 0 ▶ my $filter_segment = $filter ? "/$filter" : "";
Branch: 1 branch
BranchTrueFalseTotal
$filter ? : 0 0 0
1050 0 ▶ my $style_segment = $style ? "/$style" : "";
Branch: 1 branch
BranchTrueFalseTotal
$style ? : 0 0 0
1051 0 ▶ if (not $style) { print_link($stream, $host, undef, "Colour changes", "do/all$filter_segment/changes/$n/colour") }
Branch: 3 branches
BranchTrueFalseTotal
if (not $style) { } 0 0 0
elsif ($style eq 'colour') { } 0 0 0
elsif ($style eq 'fancy') { } 0 0 0
1052 0 elsif ($style eq "colour") { print_link($stream, $host, undef, "Fancy changes", "do/all$filter_segment/changes/$n/fancy") }
1053 0 elsif ($style eq "fancy") { print_link($stream, $host, undef, "Normal changes", "do/all$filter_segment/changes/$n") }
1054 0 ▶ if ($filter) { print_link($stream, $host, undef, "All changes", "do/all/changes/$n$style_segment") }
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) { } 0 0 0
1055 0 else { print_link($stream, $host, undef, "Latest changes", "do/all/latest/changes/$n$style_segment") }
1056 # taking the head of the @$log to get new log entries
1057 0 print_link($stream, $host, undef, "Atom Feed", "do/all/atom");
1058 0 ▶ my $filter_description = $filter ? " $filter" : "";
Branch: 1 branch
BranchTrueFalseTotal
$filter ? : 0 0 0
1059 0 $stream->write("Showing up to $n$filter_description changes.\n");
1060 return unless changes($stream,
1061 $n,
1062 0 ▶ sub { $stream->write("## " . shift . "\n") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1063 0 ▶ sub { $stream->write(shift . " by " . colourize($stream, shift, $style) . "\n") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1064 0 ▶ sub { print_link($stream, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1065 0 ▶ sub { $stream->write(encode_utf8 join("\n", @_, "")) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1066 0 ▶ sub { @{shift(@$log) }, 1 if @$log },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if @$log 0 0 0
1067 undef, # $kept
1068 0 ▶ $filter,
Branch: 1 branch
BranchTrueFalseTotal
unless changes($stream, $n, sub { $stream->write('## ' . (shift()) . "\n"); } , sub { $stream->write((shift()) . ' by ' . colourize($stream, shift(), $style) . "\n"); } , sub { print_link($stream, @_); } , sub { $stream->write(&encode_utf8(join("\n", @_, ""))); } , sub { @{shift @$log;}, 1 if @$log; } , undef, $filter, $style) 0 0 0
1069 $style);
1070 0 $stream->write("\n");
1071 0 ▶ print_link($stream, $host, undef, "More...", "do/all/changes/" . 10 * $n . ($style ? "/$style" : ""));
Branch: 1 branch
BranchTrueFalseTotal
$style ? : 0 0 0
1072 }
1073  
1074 0 ▶sub all_logs {
Subroutine all_logs: not called CC=6 SCAR=37.4
Pod undocumented
1075 0 my $stream = shift;
1076 0 my $host = shift;
1077 0 my $n = shift;
1078 0 my $filter = shift;
1079 # merge all logs
1080 0 my @log;
1081 0 my $dir = $server->{wiki_dir};
1082 0 my @spaces = space_dirs();
1083 0 for my $space (@spaces) {
1084 0 my $changes = $dir;
1085 0 ▶ $changes .= "/$space" if $space;
Branch: 1 branch
BranchTrueFalseTotal
if $space 0 0 0
1086 0 $changes .= "/changes.log";
1087 0 ▶ next unless -f $changes;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $changes 0 0 0
1088 0 $log->debug("Reading $changes");
1089 0 ▶ next unless my $fh = File::ReadBackwards->new($changes);
Branch: 1 branch
BranchTrueFalseTotal
unless my $fh = "File::ReadBackwards"->new($changes) 0 0 0
1090 0 ▶ if (keys %{$server->{host}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$App::Phoebe::server{'host'};} > 1) { } 0 0 0
1091 0 push(@log, @{read_log($stream, $fh, $n, split(/\//, $space, 2), $filter)});
1092 } else {
1093 0 push(@log, @{read_log($stream, $fh, $n, $host, $space, $filter)});
1094 }
1095 }
1096 0 @log = sort { $b->[0] <=> $a->[0] } @log;
1097 0 return \@log;
1098 }
1099  
1100 0 ▶sub read_log {
Subroutine read_log: not called CC=6 SCAR=37.4
Pod undocumented
1101 0 my $stream = shift;
1102 0 my $fh = shift; # File::ReadBackwards
1103 0 my $n = shift;
1104 0 my $host = shift;
1105 0 my $space = shift;
1106 0 my $filter = shift;
1107 0 my @changes;
1108 0 for (1 .. $n) {
1109 0 $_ = decode_utf8($fh->readline);
1110 # $_ can be undefined or a newline (which won't split)
1111 0 ▶ last unless $_ and $_ ne "\n";
Branch: 1 branch
BranchTrueFalseTotal
unless $_ and $_ ne "\n" 0 0 0
Condition0%0/3
$_ and $_ ne "\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$_ and $_ ne "\n"
$_ $_ ne "\n"
Truth table0%0/3
A and B
A= $_B= $_ ne "\n"
ABresult
0X0
100
111
1112 0 ▶ next if $filter and not /$filter/;
Branch: 1 branch
BranchTrueFalseTotal
if $filter and not /$filter/u 0 0 0
Condition0%0/3
$filter and not /$filter/u
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and not /$filter/u
$filter not /$filter/u
Truth table0%0/3
A and not(B)
A= $filterB= not /$filter/u
ABresult
0X0
100
111
1113 0 chomp;
1114 0 push(@changes, [split(/\x1f/), $host, $space]);
1115 }
1116 0 $log->debug("Read changes: " . @changes);
1117 0 return \@changes;
1118 }
1119  
1120 # $n is the number of changes to show. $header is a code reference that prints a
1121 # header for the date (one argument). $change is a code reference that prints
1122 # the time and code of the person making the change (two arguments). $link is a
1123 # code reference that prints a link (four arguments). $nolink is a code reference
1124 # that prints a name that isn't linked (one argument). $next is a code reference
1125 # that returns the list of attributes for the next change, these attributes
1126 # being: the timestamp (as returned by time); the page or file name; the page
1127 # revision or zero if a file; the code to represent the person that made the
1128 # change, represented as a string of octal digits that will be fed to the
1129 # colourize sub; the host, and the spaces, if any; and a boolean if space and
1130 # page or file name should both be shown (up to seven arguments). Finally, the
1131 # optional argument $kept is a code reference to say whether an old revision
1132 # actually exists. If not, there's no point in showing a diff link. The default
1133 # implementation checks for the existence of the keep file. $filter describes
1134 # how changes are to be filtered: 'latest' means that only the latest change
1135 # will be shown, i.e. a link to current revision. The default is to show all
1136 # changes. $style is "coloured" or "fancy" or undefined to indicate what sort of
1137 # changes we are looking at.
1138 0 ▶sub changes {
Subroutine changes: not called CC=19 SCAR=59.4
Pod undocumented
1139 0 my $stream = shift;
1140 0 my $n = shift;
1141 0 my $header = shift;
1142 0 my $change = shift;
1143 0 my $link = shift;
1144 0 my $nolink = shift;
1145 0 my $next = shift;
1146 0 ▶ my $kept = shift || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1147 0 my ($host, $space, $id, $revision) = @_;
1148 0 -e wiki_dir($host, $space) . "/keep/$id/$revision.gmi";
1149 0 ▶ };
Condition0%0/3
shift() || sub { my($host, $space, $id, $revision) = @_; -e wiki_dir($host, $space) . "/keep/$id/$revision.gmi"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || sub { my($host, $space, $id, $revision) = @_; -e wiki_dir($host, $space) . "/keep/$id/$revision.gmi"; }
shift() sub { my($host, $space, $id, $revision) = @_; -e wiki_dir($host, $space) . "/keep/$id/$revision.gmi"; }
Truth table0%0/3
A || B
A= shift()B= sub { my($host, $space, $id, $revision) = @_; -e wiki_dir($host, $space) . "/keep/$id/$revision.gmi"; }
ABresult
1X1
011
000
1150 0 ▶ my $filter = shift||'';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1151 0 my $style = shift;
1152 0 my $last_day = '';
1153 0 my %seen;
1154 0 for (1 .. $n) {
1155 0 my ($ts, $id, $revision, $code, $host, $space, $show_space) = $next->();
1156 0 ▶ return unless $ts and $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $ts and $id 0 0 0
Condition0%0/3
$ts and $id
!ll&&!rl&&r
000
MC/DC0%0/2
$ts and $id
$ts $id
Truth table0%0/3
A and B
A= $tsB= $id
ABresult
0X0
100
111
1157 0 my $name = name($stream, $id, $host, $space, $show_space);
1158 0 ▶ next if $filter eq "latest" and $seen{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $filter eq "latest" and $seen{$name} 0 0 0
Condition0%0/3
$filter eq "latest" and $seen{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
$filter eq "latest" and $seen{$name}
$filter eq "latest" $seen{$name}
Truth table0%0/3
A and B
A= $filter eq "latest"B= $seen{$name}
ABresult
0X0
100
111
1159 0 my $day = day($stream, $ts);
1160 0 ▶ if ($day ne $last_day) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day ne $last_day) 0 0 0
1161 0 $header->($day);
1162 0 $last_day = $day;
1163 }
1164 0 $change->(time_of_day($stream, $ts), $code);
1165 0 ▶ if ($revision eq "🖹") {
Branch: 3 branches
BranchTrueFalseTotal
if ($revision eq "\x{1f5b9}") { } 0 0 0
elsif ($revision eq "\x{1f5bb}") { } 0 0 0
elsif ($revision > 0) { } 0 0 0
1166 # a deleted page
1167 0 $link->($host, $space, "$name (deleted)", "page/$id");
1168 0 ▶ $link->($host, $space, "History", "history/$id" . ($style ? "/10/$style" : ""));
Branch: 1 branch
BranchTrueFalseTotal
$style ? : 0 0 0
1169 0 $seen{$name} = 1;
1170 } elsif ($revision eq "🖻") {
1171 # a deleted file
1172 0 $nolink->("$name (deleted file)");
1173 0 $seen{$name . "\x1c"} = 1;
1174 } elsif ($revision > 0) {
1175 # a page
1176 0 ▶ if ($seen{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($seen{$name}) { } 0 0 0
elsif ($filter eq 'latest') { } 0 0 0
1177 0 $link->($host, $space, "$name ($revision)", "page/$id/$revision");
1178 # there is no fancy diff, just colour diff
1179 0 ▶ $link->($host, $space, "Differences", "diff/$id/$revision" . ($style ? "/colour" : ""))
Branch: 2 branches
BranchTrueFalseTotal
if &$kept($host, $space, $id, $revision) 0 0 0
$style ? : 0 0 0
1180 if $kept->($host, $space, $id, $revision);
1181 } elsif ($filter eq "latest") {
1182 0 $link->($host, $space, "$name", "page/$id");
1183 0 $link->($host, $space, "History", "history/$id");
1184 0 $seen{$name} = 1;
1185 } else {
1186 0 $link->($host, $space, "$name (current)", "page/$id");
1187 0 ▶ $link->($host, $space, "History", "history/$id" . ($style ? "/10/$style" : ""));
Branch: 1 branch
BranchTrueFalseTotal
$style ? : 0 0 0
1188 0 $seen{$name} = 1;
1189 }
1190 } else {
1191 # a file
1192 0 ▶ if ($seen{$name . "\x1c"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$name . "\034"}) { } 0 0 0
1193 0 $nolink->("$name (file)");
1194 } else {
1195 0 $link->($host, $space, "$name (file)", "file/$id");
1196 0 $seen{$name . "\x1c"} = 1;
1197 }
1198 }
1199 }
1200 0 return () = $next->(); # return something, if there's more
1201 }
1202  
1203 0 ▶sub name {
Subroutine name: not called CC=5 SCAR=34.0
Pod undocumented
1204 0 my $stream = shift;
1205 0 my $id = shift;
1206 0 my $host = shift;
1207 0 my $space = shift;
1208 0 my $show_space = shift;
1209 0 ▶ if ($show_space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_space) 0 0 0
1210 0 ▶ if (keys %{$server->{host}} > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$$App::Phoebe::server{'host'};} > 1) { } 0 0 0
elsif ($space) { } 0 0 0
1211 0 ▶ if ($space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space) { } 0 0 0
1212 0 return "[$host/$space] $id";
1213 } else {
1214 0 return "[$host] $id";
1215 }
1216 } elsif ($space) {
1217 0 return "[$space] $id";
1218 }
1219 }
1220 0 return $id;
1221 }
1222  
1223 0 ▶sub colourize {
Subroutine colourize: not called CC=4 SCAR=30.0
Pod undocumented
1224 0 my $stream = shift;
1225 0 my $code = shift;
1226 0 my $style = shift;
1227 0 my %rgb;
1228 0 ▶ return $code unless $style;
Branch: 1 branch
BranchTrueFalseTotal
unless $style 0 0 0
1229 0 ▶ if ($style eq "colour") {
Branch: 2 branches
BranchTrueFalseTotal
if ($style eq 'colour') { } 0 0 0
elsif ($style eq 'fancy') { } 0 0 0
1230 # 3/4 bit
1231 0 return join("", map { "\033[1;3${_};4${_}m${_}" } split //, $code) . "\033[0m ";
1232 } elsif ($style eq "fancy") {
1233 # 24 bit!
1234 0 %rgb = (
1235 0 => "0;0;0",
1236 1 => "222;56;43",
1237 2 => "57;181;74",
1238 3 => "255;199;6",
1239 4 => "0;111;184",
1240 5 => "118;38;113",
1241 6 => "44;181;233",
1242 7 => "204;204;204");
1243 0 return join("", map { "\033[38;2;$rgb{$_};48;2;$rgb{$_}m$_" } split //, $code) . "\033[0m ";
1244 }
1245 0 return $code;
1246 }
1247  
1248 0 ▶sub serve_rss {
Subroutine serve_rss: not called CC=1 SCAR=6.9
Pod undocumented
1249 0 my $stream = shift;
1250 0 my $host = shift;
1251 0 my $space = shift;
1252 0 $log->info("Serving Gemini RSS");
1253 0 success($stream, "application/rss+xml");
1254 0 rss($stream, $host, $space, 'gemini');
1255 }
1256  
1257 0 ▶sub rss {
Subroutine rss: not called CC=7 SCAR=40.3
Pod undocumented
1258 0 my $stream = shift;
1259 0 my $host = shift;
1260 0 my $space = shift;
1261 0 my $scheme = shift;
1262 0 ▶ my $name = $server->{wiki_main_page} || "Phoebe";
Condition0%0/2
$App::Phoebe::server->{'wiki_main_page'} || 'Phoebe'
l!l
00
MC/DC0%0/1
$App::Phoebe::server->{'wiki_main_page'} || 'Phoebe'
$App::Phoebe::server->{'wiki_main_page'}
Truth table0%0/2
A
A= $App::Phoebe::server->{'wiki_main_page'}
Aresult
11
00
1263 0 my $port = port($stream);
1264 0 $stream->write("<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n");
1265 0 $stream->write("<channel>\n");
1266 0 $stream->write(encode_utf8 "<title>" . quote_html($name) . "</title>\n");
1267 0 $stream->write("<description>Changes on this wiki.</description>\n");
1268 0 $stream->write("<link>$scheme://$host:$port/</link>\n");
1269 0 $stream->write("<atom:link rel=\"self\" type=\"application/rss+xml\" href=\"$scheme://$host:$port/do/rss\" />\n");
1270 0 $stream->write("<generator>Phoebe</generator>\n");
1271 0 $stream->write("<docs>http://blogs.law.harvard.edu/tech/rss</docs>\n");
1272 0 my $dir = wiki_dir($host, $space);
1273 0 my $log = "$dir/changes.log";
1274 0 ▶ if (-e $log and my $fh = File::ReadBackwards->new($log)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $log and my $fh = "File::ReadBackwards"->new($log)) 0 0 0
Condition0%0/3
-e $log and my $fh = "File::ReadBackwards"->new($log)
!ll&&!rl&&r
000
MC/DC0%0/2
-e $log and my $fh = "File::ReadBackwards"->new($log)
-e $log my $fh = "File::ReadBackwards"->new($log)
Truth table0%0/3
A and B
A= -e $logB= my $fh = "File::ReadBackwards"->new($log)
ABresult
0X0
100
111
1275 0 my %seen;
1276 0 for (1 .. 100) {
1277 0 ▶ last unless $_ = decode_utf8($fh->readline);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = &decode_utf8(scalar $fh->readline) 0 0 0
1278 0 chomp;
1279 0 my ($ts, $id, $revision, $code) = split(/\x1f/);
1280 0 ▶ next if $seen{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$id} 0 0 0
1281 0 $seen{$id} = 1;
1282 0 $stream->write("<item>\n");
1283 0 $stream->write(encode_utf8 "<title>" . quote_html($id) . "</title>\n");
1284 0 my $link = to_url($stream, $host, $space, "page/$id", $scheme);
1285 0 $stream->write("<link>$link</link>\n");
1286 0 $stream->write("<guid>$link</guid>\n");
1287 0 $stream->write(encode_utf8 "<description>" . quote_html(text($stream, $host, $space, $id)) . "</description>\n");
1288 0 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ts); # Sat, 07 Sep 2002 00:00:01 GMT
1289 0 $stream->write("<pubDate>"
1290 . sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", qw(Sun Mon Tue Wed Thu Fri Sat)[$wday], $mday,
1291 qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$mon], $year + 1900, $hour, $min, $sec)
1292 . "</pubDate>\n");
1293 0 $stream->write("</item>\n");
1294 }
1295 }
1296 0 $stream->write("</channel>\n");
1297 0 $stream->write("</rss>\n");
1298 }
1299  
1300 0 ▶sub serve_blog_rss {
Subroutine serve_blog_rss: not called CC=1 SCAR=6.9
Pod undocumented
1301 0 my $stream = shift;
1302 0 my $host = shift;
1303 0 my $space = shift;
1304 0 $log->info("Serving Gemini Blog RSS");
1305 0 success($stream, "application/rss+xml");
1306 0 blog_rss($stream, $host, $space, 'gemini');
1307 }
1308  
1309 0 ▶sub blog_rss {
Subroutine blog_rss: not called CC=3 SCAR=24.8
Pod undocumented
1310 0 my $stream = shift;
1311 0 my $host = shift;
1312 0 my $space = shift;
1313 0 my $scheme = shift;
1314 0 ▶ my $name = $server->{wiki_main_page} || "Phoebe";
Condition0%0/2
$App::Phoebe::server->{'wiki_main_page'} || 'Phoebe'
l!l
00
MC/DC0%0/1
$App::Phoebe::server->{'wiki_main_page'} || 'Phoebe'
$App::Phoebe::server->{'wiki_main_page'}
Truth table0%0/2
A
A= $App::Phoebe::server->{'wiki_main_page'}
Aresult
11
00
1315 0 my $port = port($stream);
1316 0 $stream->write("<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n");
1317 0 $stream->write("<channel>\n");
1318 0 $stream->write(encode_utf8 "<title>" . quote_html($name) . "</title>\n");
1319 0 $stream->write("<description>Blog pages on this wiki.</description>\n");
1320 0 $stream->write("<link>$scheme://$host:$port/</link>\n");
1321 0 $stream->write("<atom:link rel=\"self\" type=\"application/rss+xml\" href=\"$scheme://$host:$port/do/blog/rss\" />\n");
1322 0 $stream->write("<generator>Phoebe</generator>\n");
1323 0 $stream->write("<docs>http://blogs.law.harvard.edu/tech/rss</docs>\n");
1324 0 my $dir = wiki_dir($host, $space);
1325 0 my @blog = blog_pages($stream, $host, $space, 10);
1326 0 my $ts = changes_for($host, $space, @blog);
1327 # hard coded: 10 pages blog RSS, no pagination
1328 0 for my $id (@blog[0 .. min($#blog, 9)]) {
1329 0 $stream->write("<item>\n");
1330 0 $stream->write(encode_utf8 "<title>" . quote_html($id) . "</title>\n");
1331 0 my $link = to_url($stream, $host, $space, "page/$id", $scheme);
1332 0 $stream->write("<link>$link</link>\n");
1333 0 $stream->write("<guid>$link</guid>\n");
1334 0 $stream->write(encode_utf8 "<description>" . quote_html(text($stream, $host, $space, $id)) . "</description>\n");
1335 0 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ts->{$id}); # Sat, 07 Sep 2002 00:00:01 GMT
1336 0 $stream->write("<pubDate>"
1337 . sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", qw(Sun Mon Tue Wed Thu Fri Sat)[$wday], $mday,
1338 qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$mon], $year + 1900, $hour, $min, $sec)
1339 . "</pubDate>\n");
1340 0 $stream->write("</item>\n");
1341 }
1342 0 $stream->write("</channel>\n");
1343 0 $stream->write("</rss>\n");
1344 }
1345  
1346 0 ▶sub changes_for {
Subroutine changes_for: not called CC=6 SCAR=37.4
Pod undocumented
1347 0 my $host = shift;
1348 0 my $space = shift;
1349 0 my @ids = @_;
1350 0 my %result;
1351 0 my $dir = wiki_dir($host, $space);
1352 0 my $log = "$dir/changes.log";
1353 0 ▶ if (-e $log and my $fh = File::ReadBackwards->new($log)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $log and my $fh = "File::ReadBackwards"->new($log)) 0 0 0
Condition0%0/3
-e $log and my $fh = "File::ReadBackwards"->new($log)
!ll&&!rl&&r
000
MC/DC0%0/2
-e $log and my $fh = "File::ReadBackwards"->new($log)
-e $log my $fh = "File::ReadBackwards"->new($log)
Truth table0%0/3
A and B
A= -e $logB= my $fh = "File::ReadBackwards"->new($log)
ABresult
0X0
100
111
1354 0 ▶ while (@ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ids) 0 0 0
1355 0 ▶ last unless $_ = decode_utf8($fh->readline);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = &decode_utf8(scalar $fh->readline) 0 0 0
1356 0 chomp;
1357 0 my ($ts, $id, $revision, $code) = split(/\x1f/);
1358 0 ▶ if (any { $_ eq $id } @ids) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $id; } , @ids)) 0 0 0
1359 0 @ids = grep { $_ ne $id } @ids;
1360 0 $result{$id} = $ts;
1361 }
1362 }
1363 }
1364 0 return \%result;
1365 }
1366  
1367 0 ▶sub serve_atom {
Subroutine serve_atom: not called CC=1 SCAR=6.9
Pod undocumented
1368 0 my $stream = shift;
1369 0 my $host = shift;
1370 0 my $space = shift;
1371 0 $log->info("Serving Gemini Atom");
1372 0 success($stream, "application/atom+xml");
1373 0 my $dir = wiki_dir($host, $space);
1374 0 my $log = "$dir/changes.log";
1375 0 my $fh = File::ReadBackwards->new($log);
1376 0 ▶ atom($stream, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1377 0 ▶ return unless $_ = decode_utf8($fh->readline);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = &decode_utf8(scalar $fh->readline) 0 0 0
1378 0 chomp;
1379 0 split(/\x1f/), $host, $space, 0
1380 0 }, $host, $space, 'gemini');
1381 }
1382  
1383 0 ▶sub serve_all_atom {
Subroutine serve_all_atom: not called CC=1 SCAR=6.9
Pod undocumented
1384 0 my $stream = shift;
1385 0 my $host = shift;
1386 0 $log->info("Serving Gemini Atom");
1387 0 success($stream, "application/atom+xml");
1388 0 my $log = all_logs($stream, $host, 30);
1389 0 ▶ atom($stream, sub { @{shift(@$log) }, 1 if @$log }, $host, undef, 'gemini');
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if @$log 0 0 0
1390 }
1391  
1392 # $next is a code reference that returns the list of attributes for the next
1393 # change, these attributes being: the timestamp (as returned by time); the page
1394 # or file name; the page revision or zero if a file; the code to represent the
1395 # person that made the change, represented as a string of octal digits that will
1396 # be fed to the colourize sub; the host, and the spaces, if any; and a boolean
1397 # if space and page or file name should both be shown (up to seven arguments).
1398 # $scheme is either 'gemini' or 'https'.
1399 0 ▶sub atom {
Subroutine atom: not called CC=13 SCAR=52.0
Pod undocumented
1400 0 my $stream = shift;
1401 0 my $next = shift;
1402 0 my $host = shift;
1403 0 my $space = shift;
1404 0 my $scheme = shift;
1405 0 my $first_host = shift;
1406 0 ▶ my $name = $server->{wiki_main_page} || "Phoebe";
Condition0%0/2
$App::Phoebe::server->{'wiki_main_page'} || 'Phoebe'
l!l
00
MC/DC0%0/1
$App::Phoebe::server->{'wiki_main_page'} || 'Phoebe'
$App::Phoebe::server->{'wiki_main_page'}
Truth table0%0/2
A
A= $App::Phoebe::server->{'wiki_main_page'}
Aresult
11
00
1407 0 my $port = port($stream);
1408 0 $stream->write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
1409 0 $stream->write("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n");
1410 0 $stream->write(encode_utf8 "<title>" . quote_html($name) . "</title>\n");
1411 0 my $link = to_url($stream, $host, $space, "", $scheme);
1412 0 $stream->write("<link href=\"$link\"/>\n");
1413 0 $link = to_url($stream, $host, $space, "do/atom", $scheme);
1414 0 $stream->write("<link rel=\"self\" type=\"application/atom+xml\" href=\"$link\"/>\n");
1415 0 $stream->write("<id>$link</id>\n");
1416 0 my $feed_ts = "0001-01-01T00:00:00Z";
1417 0 $stream->write("<generator uri=\"https://alexschroeder.ch/cgit/phoebe/about/\" version=\"1.0\">Phoebe</generator>\n");
1418 0 my %seen;
1419 0 for (1 .. 100) {
1420 0 my ($ts, $id, $revision, $code, $host, $space, $show_space) = $next->();
1421 0 ▶ last unless $ts and $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $ts and $id 0 0 0
Condition0%0/3
$ts and $id
!ll&&!rl&&r
000
MC/DC0%0/2
$ts and $id
$ts $id
Truth table0%0/3
A and B
A= $tsB= $id
ABresult
0X0
100
111
1422 0 my $name = name($stream, $id, $host, $space, $show_space);
1423 0 ▶ if ($revision eq "🖹") {
Branch: 3 branches
BranchTrueFalseTotal
if ($revision eq "\x{1f5b9}") { } 0 0 0
elsif ($revision eq "\x{1f5bb}") { } 0 0 0
elsif ($revision > 0) { } 0 0 0
1424 0 ▶ next if $seen{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name} 0 0 0
1425 # a deleted page
1426 0 $stream->write("<entry>\n");
1427 0 $stream->write(encode_utf8 "<title>" . quote_html($name) . " (deleted)</title>\n");
1428 0 $seen{$name} = 1;
1429 } elsif ($revision eq "🖻") {
1430 # a deleted file
1431 0 ▶ next if $seen{$name . "\x1c"};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name . "\034"} 0 0 0
1432 0 $stream->write("<entry>\n");
1433 0 $stream->write(encode_utf8 "<title>" . quote_html($name) . " (deleted file)</title>\n");
1434 0 $seen{$name . "\x1c"} = 1;
1435 } elsif ($revision > 0) {
1436 # a page
1437 0 ▶ next if $seen{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name} 0 0 0
1438 0 $stream->write("<entry>\n");
1439 0 $stream->write(encode_utf8 "<title>" . quote_html($name) . "</title>\n");
1440 0 my $link = to_url($stream, $host, $space, "page/$id", $scheme);
1441 0 $stream->write("<link href=\"$link\"/>\n");
1442 0 $stream->write("<id>$link</id>\n");
1443 0 $stream->write(encode_utf8 "<content type=\"text\">" . quote_html(text($stream, $host, $space, $id)) . "</content>\n");
1444 0 $seen{$name} = 1;
1445 } else {
1446 # a file
1447 0 ▶ next if $seen{$name . "\x1c"};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name . "\034"} 0 0 0
1448 0 $stream->write("<entry>\n");
1449 0 $stream->write(encode_utf8 "<title>" . quote_html($name) . " (file)</title>\n");
1450 0 my $link = to_url($stream, $host, $space, "file/$id", $scheme);
1451 0 $stream->write("<link href=\"$link\"/>\n");
1452 0 $stream->write("<id>$link</id>\n");
1453 0 $seen{$name . "\x1c"} = 1;
1454 }
1455 0 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($ts); # 2003-12-13T18:30:02Z
1456 0 $ts = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
1457 0 $stream->write("<updated>$ts</updated>\n");
1458 0 ▶ $feed_ts = $ts if $ts gt $feed_ts;
Branch: 1 branch
BranchTrueFalseTotal
if $ts gt $feed_ts 0 0 0
1459 0 $stream->write("<author><name>$code</name></author>\n");
1460 0 $stream->write("</entry>\n");
1461 }
1462 0 $stream->write("<updated>$feed_ts</updated>\n");
1463 0 $stream->write("</feed>\n");
1464 }
1465  
1466 0 ▶sub serve_blog_atom {
Subroutine serve_blog_atom: not called CC=1 SCAR=6.9
Pod undocumented
1467 0 my $stream = shift;
1468 0 my $host = shift;
1469 0 my $space = shift;
1470 0 $log->info("Serving Gemini Blog Atom");
1471 0 success($stream, "application/atom+xml");
1472 0 blog_atom($stream, $host, $space, 'gemini');
1473 }
1474  
1475 0 ▶sub blog_atom {
Subroutine blog_atom: not called CC=4 SCAR=30.0
Pod undocumented
1476 0 my $stream = shift;
1477 0 my $host = shift;
1478 0 my $space = shift;
1479 0 my $scheme = shift;
1480 0 ▶ my $name = $server->{wiki_main_page} || "Phoebe";
Condition0%0/2
$App::Phoebe::server->{'wiki_main_page'} || 'Phoebe'
l!l
00
MC/DC0%0/1
$App::Phoebe::server->{'wiki_main_page'} || 'Phoebe'
$App::Phoebe::server->{'wiki_main_page'}
Truth table0%0/2
A
A= $App::Phoebe::server->{'wiki_main_page'}
Aresult
11
00
1481 0 my $port = port($stream);
1482 0 $stream->write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
1483 0 $stream->write("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n");
1484 0 $stream->write(encode_utf8 "<title>" . quote_html($name) . "</title>\n");
1485 0 my $link = to_url($stream, $host, $space, "", $scheme);
1486 0 $stream->write("<link href=\"$link\"/>\n");
1487 0 $link = to_url($stream, $host, $space, "do/blog/atom", $scheme);
1488 0 $stream->write("<link rel=\"self\" type=\"application/atom+xml\" href=\"$link\"/>\n");
1489 0 $stream->write("<id>$link</id>\n");
1490 0 my $feed_ts = "0001-01-01T00:00:00Z";
1491 0 $stream->write("<generator uri=\"https://alexschroeder.ch/cgit/phoebe/about/\" version=\"1.0\">Phoebe</generator>\n");
1492 0 my $dir = wiki_dir($host, $space);
1493 0 my @blog = blog_pages($stream, $host, $space, 10);
1494 0 my $changes = changes_for($host, $space, @blog);
1495 # hard coded: 10 pages blog ATOM, no pagination
1496 0 for my $id (@blog[0 .. min($#blog, 9)]) {
1497 0 $stream->write("<entry>\n");
1498 0 $stream->write(encode_utf8 "<title>" . quote_html($id) . "</title>\n");
1499 0 my $link = to_url($stream, $host, $space, "page/$id", $scheme);
1500 0 $stream->write("<link href=\"$link\"/>\n");
1501 0 $stream->write("<id>$link</id>\n");
1502 0 $stream->write(encode_utf8 "<content type=\"text\">" . quote_html(text($stream, $host, $space, $id)) . "</content>\n");
1503 0 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($changes->{$id}); # 2003-12-13T18:30:02Z
1504 0 my $ts = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
1505 0 $stream->write("<updated>$ts</updated>\n");
1506 0 ▶ $feed_ts = $ts if $ts gt $feed_ts;
Branch: 1 branch
BranchTrueFalseTotal
if $ts gt $feed_ts 0 0 0
1507 0 $stream->write("</entry>\n");
1508 }
1509 0 $stream->write("<updated>$feed_ts</updated>\n");
1510 0 $stream->write("</feed>\n");
1511 }
1512  
1513 0 ▶sub serve_raw {
Subroutine serve_raw: not called CC=1 SCAR=6.9
Pod undocumented
1514 0 my $stream = shift;
1515 0 my $host = shift;
1516 0 my $space = shift;
1517 0 my $id = shift;
1518 0 my $revision = shift;
1519 0 $log->info("Serving raw $id");
1520 0 success($stream, 'text/plain; charset=UTF-8');
1521 0 $stream->write(encode_utf8 text($stream, $host, $space, $id, $revision));
1522 }
1523  
1524 0 ▶sub serve_diff {
Subroutine serve_diff: not called CC=3 SCAR=24.8
Pod undocumented
1525 0 my $stream = shift;
1526 0 my $host = shift;
1527 0 my $space = shift;
1528 0 my $id = shift;
1529 0 my $revision = shift;
1530 0 my $style = shift;
1531 0 $log->info("Serving the diff of $id");
1532 0 success($stream);
1533 0 $stream->write(encode_utf8 "# Differences for $id\n");
1534 0 ▶ if (not $style) { print_link($stream, $host, $space, "Colour diff", "diff/$id/$revision/colour") }
Branch: 1 branch
BranchTrueFalseTotal
if (not $style) { } 0 0 0
1535 0 else { print_link($stream, $host, $space, "Normal diff", "diff/$id/$revision") }
1536 0 $stream->write("Showing the differences between revision $revision and the current revision.\n");
1537 0 my $new = text($stream, $host, $space, $id);
1538 0 my $old = text($stream, $host, $space, $id, $revision);
1539 0 ▶ if (not $style) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $style) { } 0 0 0
1540 diff($old, $new,
1541 0 ▶ sub { $stream->write(encode_utf8 "$_\n") for @_ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
1542 0 ▶ sub { $stream->write(encode_utf8 "> $_\n") for map { $_||"⏎" } @_ },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/2
$_ || "\x{23ce}"
l!l
00
MC/DC0%0/1
$_ || "\x{23ce}"
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
1543 0 ▶ sub { $stream->write(encode_utf8 "> $_\n") for map { $_||"⏎" } @_ },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/2
$_ || "\x{23ce}"
l!l
00
MC/DC0%0/1
$_ || "\x{23ce}"
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
1544 0 ▶ sub { "「$_[0]」" });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1545 } else {
1546 diff($old, $new,
1547 0 ▶ sub { $stream->write(encode_utf8 "$_\n") for @_ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
1548 0 ▶ sub { $stream->write(encode_utf8 "> \033[31m$_\033[0m\n") for map { $_||"⏎" } @_ },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/2
$_ || "\x{23ce}"
l!l
00
MC/DC0%0/1
$_ || "\x{23ce}"
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
1549 0 ▶ sub { $stream->write(encode_utf8 "> \033[32m$_\033[0m\n") for map { $_||"⏎" } @_ },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/2
$_ || "\x{23ce}"
l!l
00
MC/DC0%0/1
$_ || "\x{23ce}"
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
1550 0 ▶ sub { "\033[1m$_[0]\033[22m" });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1551 }
1552 }
1553  
1554 # old text, new text, code reference to print a paragraph, print deleted text,
1555 # print added text
1556 0 ▶sub diff {
Subroutine diff: not called CC=10 SCAR=47.0
Pod undocumented
1557 0 my @old = split(/\n/, shift);
1558 0 my @new = split(/\n/, shift);
1559 0 my $paragraph = shift;
1560 0 my $deleted = shift;
1561 0 my $added = shift;
1562 0 my $highlight = shift;
1563 0 $log->debug("Preparing a diff");
1564 0 my $diff = Algorithm::Diff->new(\@old, \@new);
1565 0 $diff->Base(1); # line numbers, not indices
1566 0 ▶ while($diff->Next()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->Next) 0 0 0
1567 0 ▶ next if $diff->Same();
Branch: 1 branch
BranchTrueFalseTotal
if $diff->Same 0 0 0
1568 0 my $sep = '';
1569 0 my ($min1, $max1, $min2, $max2) = $diff->Get(qw(min1 max1 min2 max2));
1570 0 ▶ if ($diff->Diff == 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ($diff->Diff == 3) { } 0 0 0
elsif ($diff->Diff == 2) { } 0 0 0
elsif ($diff->Diff == 1) { } 0 0 0
1571 0 my ($from, $to) = refine([$diff->Items(1)], [$diff->Items(2)], $highlight);
1572 0 ▶ $paragraph->($min1 == $max1 ? "Changed line $min1 from:" : "Changed lines $min1–$max1 from:");
Branch: 1 branch
BranchTrueFalseTotal
$min1 == $max1 ? : 0 0 0
1573 0 $deleted->(@$from);
1574 0 ▶ $paragraph->($min2 == $max2 ? "to:" : "to lines $min2–$max2:");
Branch: 1 branch
BranchTrueFalseTotal
$min2 == $max2 ? : 0 0 0
1575 0 $added->(@$to);
1576 } elsif ($diff->Diff == 2) {
1577 0 ▶ $paragraph->($min2 == $max2 ? "Added line $min2:" : "Added lines $min2–$max2:");
Branch: 1 branch
BranchTrueFalseTotal
$min2 == $max2 ? : 0 0 0
1578 0 $added->($diff->Items(2));
1579 } elsif ($diff->Diff == 1) {
1580 0 ▶ $paragraph->($min1 == $max1 ? "Deleted line $min1:" : "Deleted lines $min1–$max1:");
Branch: 1 branch
BranchTrueFalseTotal
$min1 == $max1 ? : 0 0 0
1581 0 $deleted->($diff->Items(1));
1582 }
1583 }
1584 }
1585  
1586 # $from_lines and $to_lines are references to lists of lines. The lines are
1587 # concatenated and split by words.
1588 0 ▶sub refine {
Subroutine refine: not called CC=5 SCAR=34.0
Pod undocumented
1589 0 my $from_lines = shift;
1590 0 my $to_lines = shift;
1591 0 my $highlight = shift;
1592 0 my @from_words = split(/\b(?=\w)/, join("\n", @$from_lines));
1593 0 my @to_words = split(/\b(?=\w)/, join("\n", @$to_lines));
1594 0 my $diff = Algorithm::Diff->new(\@from_words, \@to_words);
1595 0 my ($from, $to);
1596 0 ▶ while($diff->Next()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->Next) 0 0 0
1597 0 ▶ if (my @list = $diff->Same()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@list) = $diff->Same) { } 0 0 0
1598 0 $from .= join('', @list);
1599 0 $to .= join('', @list);
1600 } else {
1601 # reassemble the chunks, and highlight them per line, don't strip trailing newlines!
1602 0 ▶ $from .= join("\n", map { $_ ? $highlight->($_) : $_ } (split(/\n/, join('', $diff->Items(1)), -1)));
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
1603 0 ▶ $to .= join("\n", map { $_ ? $highlight->($_) : $_ } (split(/\n/, join('', $diff->Items(2)), -1)));
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
1604 }
1605 }
1606 # return lines
1607 0 return [split(/\n/, $from)], [split(/\n/, $to)];
1608 }
1609  
1610 0 ▶sub serve_html {
Subroutine serve_html: not called CC=1 SCAR=6.9
Pod undocumented
1611 0 my $stream = shift;
1612 0 my $host = shift;
1613 0 my $space = shift;
1614 0 my $id = shift;
1615 0 my $revision = shift;
1616 0 success($stream, 'text/html');
1617 0 $log->info("Serving $id as HTML");
1618 0 html_page($stream, $host, $space, $id, $revision);
1619 }
1620  
1621 0 ▶sub html_page {
Subroutine html_page: not called CC=1 SCAR=6.9
Pod undocumented
1622 0 my $stream = shift;
1623 0 my $host = shift;
1624 0 my $space = shift;
1625 0 my $id = shift;
1626 0 my $revision = shift;
1627 0 $stream->write("<!DOCTYPE html>\n");
1628 0 $stream->write("<html>\n");
1629 0 $stream->write("<head>\n");
1630 0 $stream->write("<meta charset=\"utf-8\">\n");
1631 0 $stream->write(encode_utf8 "<title>" . quote_html($id) . "</title>\n");
1632 0 $stream->write("<link type=\"text/css\" rel=\"stylesheet\" href=\"/default.css\"/>\n");
1633 0 $stream->write("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
1634 0 $stream->write("</head>\n");
1635 0 $stream->write("<body>\n");
1636 0 $stream->write(encode_utf8 "<h1>" . quote_html($id) . "</h1>\n");
1637 0 $stream->write(encode_utf8 to_html(text($stream, $host, $space, $id, $revision)) . "\n");
1638 0 $stream->write(encode_utf8 to_html(html_footer($stream, $host, $space, $id, $revision)) . "\n");
1639 0 $stream->write("</body>\n");
1640 0 $stream->write("</html>\n");
1641 }
1642  
1643 # returns lines!
1644 0 ▶sub to_html {
Subroutine to_html: not called CC=19 SCAR=59.4
Pod undocumented
1645 0 my $text = shift;
1646 0 my @lines;
1647 my $list;
1648 0 my $code;
1649 0 for (split /\n/, quote_html($text)) {
1650 0 ▶ if (/^```(?:type=([a-z]+))?/) {
Branch: 6 branches
BranchTrueFalseTotal
if (/^```(?:type=([a-z]+))?/u) { } 0 0 0
elsif ($code) { } 0 0 0
elsif (/^\* +(.*)/u) { } 0 0 0
elsif (() = /^=&gt;\s*(\S+)\s*(.*)/u) { } 0 0 0
elsif (/^(#{1,6})\s*(.*)/u) { } 0 0 0
elsif (/^&gt;\s*(.*)/u) { } 0 0 0
1651 0 ▶ my $type = $1||"default";
Condition0%0/2
$1 || 'default'
l!l
00
MC/DC0%0/1
$1 || 'default'
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
1652 0 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 0 0 0
1653 0 push @lines, "</pre>";
1654 0 $code = 0;
1655 } else {
1656 0 ▶ push @lines, "</ul>" if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 0 0
1657 0 $list = 0;
1658 0 push @lines, "<pre class=\"$type\">";
1659 0 $code = 1;
1660 }
1661 } elsif ($code) {
1662 0 push @lines, $_;
1663 } elsif (/^\* +(.*)/) {
1664 0 ▶ push @lines, "<ul>" unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
1665 0 push @lines, "<li>$1";
1666 0 $list = 1;
1667 } elsif (my ($url, $text) = /^=&gt;\s*(\S+)\s*(.*)/) { # quoted HTML!
1668 0 ▶ push @lines, "<ul>" unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
1669 0 ▶ $text ||= $url;
Condition0%0/3
$text ||= $url
l!l&&r!l&&!r
000
MC/DC0%0/2
$text ||= $url
$text $url
Truth table0%0/3
A ||= B
A= $textB= $url
ABresult
1X1
011
000
1670 0 push @lines, "<li><a href=\"$url\">$text</a>";
1671 0 $list = 1;
1672 } elsif (/^(#{1,6})\s*(.*)/) {
1673 0 ▶ push @lines, "</ul>" if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 0 0
1674 0 $list = 0;
1675 0 my $level = length($1);
1676 0 push @lines, "<h$level>$2</h$level>";
1677 } elsif (/^&gt;\s*(.*)/) { # quoted HTML!
1678 0 ▶ push @lines, "</ul>" if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 0 0
1679 0 $list = 0;
1680 0 push @lines, "<blockquote>$1</blockquote>";
1681 } else {
1682 0 ▶ push @lines, "</ul>" if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 0 0
1683 0 $list = 0;
1684 0 push @lines, "<p>$_";
1685 }
1686 }
1687 0 ▶ push @lines, "</pre>" if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
1688 0 ▶ push @lines, "</ul>" if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 0 0
1689 0 return join("\n", @lines);
1690 }
1691  
1692 0 ▶sub html_footer {
Subroutine html_footer: not called CC=4 SCAR=30.0
Pod undocumented
1693 0 my $stream = shift;
1694 0 my $host = shift;
1695 0 my $space = shift;
1696 0 my $id = shift;
1697 0 ▶ my $revision = shift||"";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1698 0 my @links;
1699 0 push(@links, $_->($stream, $host, $space, $id, $revision, "html")) for @footer;
1700 0 my $html = join("\n", grep /\S/, @links);
1701 0 ▶ return "\n\nMore:\n$html" if $html =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ /\S/u 0 0 0
1702 0 return "";
1703 }
1704  
1705 0 ▶sub day {
Subroutine day: not called CC=1 SCAR=6.9
Pod undocumented
1706 0 my $stream = shift;
1707 0 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift);
1708 0 return sprintf('%4d-%02d-%02d', $year + 1900, $mon + 1, $mday);
1709 }
1710  
1711 0 ▶sub time_of_day {
Subroutine time_of_day: not called CC=1 SCAR=6.9
Pod undocumented
1712 0 my $stream = shift;
1713 0 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift);
1714 0 return sprintf('%02d:%02d UTC', $hour, $min);
1715 }
1716  
1717 0 ▶sub modified {
Subroutine modified: not called CC=1 SCAR=6.9
Pod undocumented
1718 0 my $ts = (stat(shift))[9];
1719 0 return $ts;
1720 }
1721  
1722 0 ▶sub serve_history {
Subroutine serve_history: not called CC=7 SCAR=40.3
Pod undocumented
1723 0 my $stream = shift;
1724 0 my $host = shift;
1725 0 my $space = shift;
1726 0 my $id = shift;
1727 0 my $n = shift;
1728 0 my $style = shift;
1729 0 success($stream);
1730 0 $log->info("Serve history for $id");
1731 0 $stream->write(encode_utf8 "# Page history for $id\n");
1732 0 ▶ if (not $style) { print_link($stream, $host, $space, "Colour history", "history/$id/$n/colour") }
Branch: 3 branches
BranchTrueFalseTotal
if (not $style) { } 0 0 0
elsif ($style eq 'colour') { } 0 0 0
elsif ($style eq 'fancy') { } 0 0 0
1733 0 elsif ($style eq "colour") { print_link($stream, $host, $space, "Fancy history", "history/$id/$n/fancy") }
1734 0 elsif ($style eq "fancy") { print_link($stream, $host, $space, "Normal history", "history/$id/$n") }
1735 0 my $dir = wiki_dir($host, $space);
1736 0 my $log = "$dir/changes.log";
1737 0 ▶ if (not -e $log) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $log) 0 0 0
1738 0 $stream->write("No changes.\n");
1739 0 return;
1740 }
1741 0 $stream->write("Showing up to $n changes.\n");
1742 0 my $fh = File::ReadBackwards->new($log);
1743 return unless changes($stream,
1744 $n,
1745 0 ▶ sub { $stream->write("## " . shift . "\n") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1746 0 ▶ sub { $stream->write(shift . " by " . colourize($stream, shift, $style) . "\n") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1747 0 ▶ sub { print_link($stream, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1748 0 ▶ sub { $stream->write(join("\n", @_, "")) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1749 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1750 0 ▶ READ:
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = &decode_utf8(scalar $fh->readline) 0 0 0
1751 return unless $_ = decode_utf8($fh->readline);
1752 0 chomp;
1753 0 my ($ts, $id_log, $revision, $code) = split(/\x1f/);
1754 0 ▶ goto READ if $id_log ne $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id_log ne $id 0 0 0
1755 0 $ts, $id_log, $revision, $code, $host, $space, 0 },
1756 undef, #$kept
1757 undef, #$filter
1758 0 ▶ $style);
Branch: 1 branch
BranchTrueFalseTotal
unless changes($stream, $n, sub { $stream->write('## ' . (shift()) . "\n"); } , sub { $stream->write((shift()) . ' by ' . colourize($stream, shift(), $style) . "\n"); } , sub { print_link($stream, @_); } , sub { $stream->write(join("\n", @_, "")); } , sub { READ: return unless $_ = &decode_utf8(scalar $fh->readline); chomp $_; my($ts, $id_log, $revision, $code) = split(/\x1f/u, $_, 5); goto READ if $id_log ne $id; $ts, $id_log, $revision, $code, $host, $space, 0; } , undef, undef, $style) 0 0 0
1759 0 $stream->write("\n");
1760 0 ▶ print_link($stream, $host, $space, "More...", "history/" . uri_escape_utf8($id) . "/" . 10 * $n . ($style ? "/$style" : ""));
Branch: 1 branch
BranchTrueFalseTotal
$style ? : 0 0 0
1761 }
1762  
1763 0 ▶sub footer {
Subroutine footer: not called CC=3 SCAR=24.8
Pod undocumented
1764 0 my $stream = shift;
1765 0 my $host = shift;
1766 0 my $space = shift;
1767 0 my $id = shift;
1768 0 ▶ my $revision = shift||"";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1769 0 my @links;
1770 0 push(@links, gemini_link($stream, $host, $space, "History", "history/$id"));
1771 0 push(@links, gemini_link($stream, $host, $space, "Raw text", "raw/$id/$revision"));
1772 0 push(@links, gemini_link($stream, $host, $space, "HTML", "html/$id/$revision"));
1773 0 push(@links, $_->($stream, $host, $space, $id, $revision, "gemini")) for @footer;
1774 0 return join("\n", "\n\nMore:", (grep /\S/, @links), ""); # includes a trailing newline
1775 }
1776  
1777 0 ▶sub serve_page {
Subroutine serve_page: not called CC=1 SCAR=6.9
Pod undocumented
1778 0 my $stream = shift;
1779 0 my $host = shift;
1780 0 my $space = shift;
1781 0 my $id = shift;
1782 0 my $revision = shift;
1783 0 $log->info("Serve Gemini page $id");
1784 0 success($stream);
1785 0 $stream->write(encode_utf8 "# $id\n");
1786 0 $stream->write(encode_utf8 text($stream, $host, $space, $id, $revision));
1787 0 $stream->write(encode_utf8 footer($stream, $host, $space, $id, $revision));
1788 }
1789  
1790 0 ▶sub text {
Subroutine text: not called CC=7 SCAR=40.3
Pod undocumented
1791 0 my $stream = shift; # used by contributions like oddmuse.pl
1792 0 my $host = shift;
1793 0 my $space = shift;
1794 0 my $id = shift;
1795 0 my $revision = shift;
1796 0 my $dir = wiki_dir($host, $space);
1797 0 ▶ return read_text "$dir/keep/$id/$revision.gmi" if $revision and -f "$dir/keep/$id/$revision.gmi";
Branch: 1 branch
BranchTrueFalseTotal
if $revision and -f "$dir/keep/$id/$revision.gmi" 0 0 0
Condition0%0/3
$revision and -f "$dir/keep/$id/$revision.gmi"
!ll&&!rl&&r
000
MC/DC0%0/2
$revision and -f "$dir/keep/$id/$revision.gmi"
$revision -f "$dir/keep/$id/$revision.gmi"
Truth table0%0/3
A and B
A= $revisionB= -f "$dir/keep/$id/$revision.gmi"
ABresult
0X0
100
111
1798 0 ▶ return read_text "$dir/page/$id.gmi" if -f "$dir/page/$id.gmi";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$dir/page/$id.gmi" 0 0 0
1799 0 ▶ return robots() if $id eq "robots" and not $space;
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "robots" and not $space 0 0 0
Condition0%0/3
$id eq "robots" and not $space
!ll&&!rl&&r
000
MC/DC0%0/2
$id eq "robots" and not $space
$id eq "robots" not $space
Truth table0%0/3
A and not(B)
A= $id eq "robots"B= not $space
ABresult
0X0
100
111
1800 0 ▶ return "This this revision is no longer available." if $revision;
Branch: 1 branch
BranchTrueFalseTotal
if $revision 0 0 0
1801 0 return "This page does not yet exist.";
1802 }
1803  
1804 0 ▶sub robots () {
Subroutine robots: not called CC=1 SCAR=6.9
Pod undocumented
1805 0 my $ban = << 'EOT';
1806 User-agent: *
1807 Disallow: /raw
1808 Disallow: /html
1809 Disallow: /diff
1810 Disallow: /history
1811 Disallow: /do/comment
1812 Disallow: /do/changes
1813 Disallow: /do/all/changes
1814 Disallow: /do/all/latest/changes
1815 Disallow: /do/rss
1816 Disallow: /do/blog/rss
1817 Disallow: /do/atom
1818 Disallow: /do/blog/atom
1819 Disallow: /do/all/atom
1820 Disallow: /do/new
1821 Disallow: /do/more
1822 Disallow: /do/match
1823 Disallow: /do/search
1824 # allowing do/index!
1825 Crawl-delay: 10
1826 EOT
1827 0 my @disallows = $ban =~ /Disallow: (.*)/g;
1828 return $ban
1829 . join("\n",
1830 map {
1831 0 my $space = (split /\//)[-1];
1832 0 join("\n", "# $space", map { "Disallow: /$space$_" } @disallows)
1833 0 } @{$server->{wiki_space}}) . "\n";
1834 }
1835  
1836 0 ▶sub serve_file {
Subroutine serve_file: not called CC=4 SCAR=30.0
Pod undocumented
1837 0 my $stream = shift;
1838 0 my $host = shift;
1839 0 my $space = shift;
1840 0 my $id = shift;
1841 0 my $revision = shift;
1842 0 $log->info("Serve file $id");
1843 0 my $dir = wiki_dir($host, $space);
1844 0 my $file = "$dir/file/$id";
1845 0 my $meta = "$dir/meta/$id";
1846 0 ▶ if (not -f $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not -f $file) { } 0 0 0
elsif (not -f $meta) { } 0 0 0
1847 0 result($stream, "40", "File not found");
1848 0 return;
1849 } elsif (not -f $meta) {
1850 0 result($stream, "40", "Metadata not found");
1851 0 return;
1852 }
1853 0 my %meta = (map { split(/: /, $_, 2) } split /\n/, read_text $meta);
1854 0 ▶ if (not $meta{'content-type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta{"content-type"}) 0 0 0
1855 0 result($stream, "59", "Metadata corrupt");
1856 0 return;
1857 }
1858 0 success($stream, $meta{'content-type'});
1859 0 $stream->write(read_binary($file));
1860 }
1861  
1862 0 ▶sub bogus_hash {
Subroutine bogus_hash: not called CC=2 SCAR=17.9
Pod undocumented
1863 0 my $str = shift;
1864 0 ▶ return "0000" unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 0 0
1865 0 my $num = unpack("L",B::hash($str)); # 32-bit integer
1866 0 my $code = sprintf("%o", $num); # octal is 0-7
1867 0 return substr($code, 0, 4); # four numbers
1868 }
1869  
1870 0 ▶sub write_file {
Subroutine write_file: not called CC=8 SCAR=42.8
Pod undocumented
1871 0 my $stream = shift;
1872 0 my $host = shift;
1873 0 my $space = shift;
1874 0 my $id = shift;
1875 0 my $data = shift;
1876 0 my $type = shift;
1877 0 $log->info("Writing file $id");
1878 0 my $dir = wiki_dir($host, $space);
1879 0 my $file = "$dir/file/$id";
1880 0 my $meta = "$dir/meta/$id";
1881 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
1882 0 my $old = read_binary($file);
1883 0 ▶ if ($old eq $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old eq $data) 0 0 0
1884 0 $log->info("$id is unchanged");
1885 0 result($stream, "30", to_url($stream, $host, $space, "page/$id"));
1886 0 return;
1887 }
1888 }
1889 0 my $changes = "$dir/changes.log";
1890 0 ▶ if (not open(my $fh, ">>:encoding(UTF-8)", $changes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not open(my $fh, '>>:encoding(UTF-8)', $changes)) { } 0 0 0
1891 0 $log->error("Cannot log $changes: $!");
1892 0 result($stream, "59", "Unable to write log");
1893 0 return;
1894 } else {
1895 0 my $peerhost = $stream->handle->peerhost;
1896 0 say $fh join("\x1f", scalar(time), $id, 0, bogus_hash($peerhost));
1897 0 close($fh);
1898 }
1899 0 ▶ mkdir "$dir/file" unless -d "$dir/file";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$dir/file" 0 0 0
1900 0 eval { write_binary($file, $data) };
1901 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1902 0 result($stream, "59", "Unable to save $id");
1903 0 return;
1904 }
1905 0 ▶ mkdir "$dir/meta" unless -d "$dir/meta";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$dir/meta" 0 0 0
1906 0 eval { write_text($meta, "content-type: $type\n") };
1907 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1908 0 result($stream, "59", "Unable to save metadata for $id");
1909 0 return;
1910 }
1911 0 $log->info("Wrote $id");
1912 0 result($stream, "30", to_url($stream, $host, $space, "file/$id"));
1913 }
1914  
1915 0 ▶sub delete_file {
Subroutine delete_file: not called CC=2 SCAR=17.9
Pod undocumented
1916 0 my $stream = shift;
1917 0 my $host = shift;
1918 0 my $space = shift;
1919 0 my $id = shift;
1920 0 $log->info("Deleting file $id");
1921 0 my $dir = wiki_dir($host, $space);
1922 0 unlink("$dir/file/$id", "$dir/meta/$id");
1923 0 my $changes = "$dir/changes.log";
1924 0 ▶ if (not open(my $fh, ">>:encoding(UTF-8)", $changes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not open(my $fh, '>>:encoding(UTF-8)', $changes)) { } 0 0 0
1925 0 $log->error("Cannot write log $changes: $!");
1926 0 result($stream, "59", "Unable to write log");
1927 0 return;
1928 } else {
1929 0 my $peerhost = $stream->handle->peerhost;
1930 0 say $fh join("\x1f", scalar(time), $id, "🖻", bogus_hash($peerhost));
1931 0 close($fh);
1932 }
1933 0 success($stream);
1934 0 $stream->write("# $id\n");
1935 0 $stream->write("The file was deleted.\n");
1936 }
1937  
1938 0 ▶sub allow_deny_hook {
Subroutine allow_deny_hook: not called CC=1 SCAR=6.9
Pod undocumented
1939 0 my $stream = shift;
1940 0 my $client = shift;
1941 # consider adding rate limiting?
1942 0 return 1;
1943 }
1944  
1945 0 ▶sub wiki_dir {
Subroutine wiki_dir: not called CC=5 SCAR=34.0
Pod undocumented
1946 0 my $host = shift;
1947 0 my $space = shift;
1948 0 my $dir = $server->{wiki_dir};
1949 0 ▶ if (keys %{$server->{host}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$App::Phoebe::server{"host"};} > 1) 0 0 0
1950 0 $dir .= "/$host";
1951 0 ▶ mkdir($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
1952 }
1953 0 ▶ $dir .= "/$space" if $space;
Branch: 1 branch
BranchTrueFalseTotal
if $space 0 0 0
1954 0 ▶ mkdir($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
1955 0 return $dir;
1956 }
1957  
1958 # If we are serving multiple hostnames, we need to check whether the space
1959 # supplied in the URL matches a known hostname/space combo.
1960 0 ▶sub space {
Subroutine space: not called CC=5 SCAR=34.0
Pod undocumented
1961 0 my $stream = shift;
1962 0 my $host = shift;
1963 0 my $space = shift;
1964 0 ▶ $space = decode_utf8(uri_unescape($space)) if $space;
Branch: 1 branch
BranchTrueFalseTotal
if $space 0 0 0
1965 0 ▶ if (keys %{$server->{host}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$App::Phoebe::server{"host"};} > 1) 0 0 0
1966 0 ▶ return undef unless $space;
Branch: 1 branch
BranchTrueFalseTotal
unless $space 0 0 0
1967 0 ▶ return $space if grep { $_ eq "$host/$space" } @{$server->{wiki_space}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "$host/$space";} @{$$App::Phoebe::server{"wiki_space"};} 0 0 0
1968 # else it's an error and we jump out to the eval {} in handle_url
1969 0 result($stream, "40", "$host doesn't know about $space");
1970 0 die "unknown space: $host/$space\n"; # is caught in the eval
1971 }
1972 # Without wildcards, just return the space. We already know that the space
1973 # matched the regular expression of spaces.
1974 0 return $space;
1975 }
1976  
1977 0 ▶sub space_dirs {
Subroutine space_dirs: not called CC=2 SCAR=17.9
Pod undocumented
1978 0 my @spaces;
1979 0 ▶ if (keys %{$server->{host}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$App::Phoebe::server{'host'};} > 1) { } 0 0 0
1980 0 push @spaces, keys %{$server->{host}};
1981 } else {
1982 0 push @spaces, undef;
1983 }
1984 0 push @spaces, @{$server->{wiki_space}};
1985 0 return @spaces;
1986 }
1987  
1988 # A list of links to all the spaces we have. The tricky part here is that we
1989 # want to create appropriate links if we're virtual hosting. Keys are URLs,
1990 # values are names.
1991 0 ▶sub space_links {
Subroutine space_links: not called CC=6 SCAR=37.4
Pod undocumented
1992 0 my $stream = shift;
1993 0 my $scheme = shift;
1994 0 my $host = shift;
1995 0 my $port = shift;
1996 0 my %spaces;
1997 0 ▶ if (keys %{$server->{host}} > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$$App::Phoebe::server{'host'};} > 1) { } 0 0 0
elsif (@{$App::Phoebe::server->{'wiki_space'};}) { } 0 0 0
1998 0 for (keys %{$server->{host}}) {
1999 0 $spaces{"$scheme://$_:$port/"} = $_;
2000 }
2001 0 for my $space (@{$server->{wiki_space}}) {
2002 0 my ($ahost, $aspace) = split(/\//m, $space, 2);
2003 0 $spaces{"$scheme://$ahost:$port/$aspace/"} = $space;
2004 }
2005 0 } elsif (@{$server->{wiki_space}}) {
2006 0 $spaces{"$scheme://$host:$port/"} = "Main space";
2007 0 for (sort @{$server->{wiki_space}}) {
2008 0 $spaces{"$scheme://$host:$port/$_/"} = $_;
2009 }
2010 }
2011 0 return \%spaces;
2012 }
2013  
2014 0 ▶sub is_upload {
Subroutine is_upload: not called CC=4 SCAR=30.0
Pod undocumented
2015 0 my $stream = shift;
2016 0 my $request = shift;
2017 0 $log->info("Looking at $request");
2018 0 my $hosts = host_regex();
2019 0 my $spaces_regex = space_regex();
2020 0 my $port = port($stream);
2021 0 ▶ if ($request =~ m!^titan://($hosts)(?::$port)?!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request =~ m[^titan://($hosts)(?::$port)?]u) 0 0 0
2022 0 my $host = $1;
2023 0 my($scheme, $authority, $path, $query, $fragment) =
2024 $request =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
2025 0 ▶ if ($path =~ m!^(?:/($spaces_regex))?(?:/raw|/page|/file)?/([^/;=&]+(?:;\w+=[^;=&]+)+)!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^(?:/($spaces_regex))?(?:/raw|/page|/file)?/([^/;=&]+(?:;\w+=[^;=&]+)+)]u) { } 0 0 0
2026 0 my $space = $1;
2027 0 my ($id, @params) = split(/[;=&]/, $2);
2028 0 my $params = { map {decode_utf8(uri_unescape($_))} @params };
2029 0 ▶ if (valid_params($stream, $host, $space, $id, $params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (valid_params($stream, $host, $space, $id, $params)) 0 0 0
2030 return {
2031 0 host => $host,
2032 space => space($stream, $host, $space),
2033 id => decode_utf8(uri_unescape($id)),
2034 params => $params,
2035 }
2036 }
2037 } else {
2038 0 $log->debug("The path $path is malformed");
2039 0 result($stream, "59", "The path $path is malformed");
2040 0 $stream->close_gracefully();
2041 }
2042 }
2043 0 return 0;
2044 }
2045  
2046 0 ▶sub valid_params {
Subroutine valid_params: not called CC=5 SCAR=34.0
Pod undocumented
2047 0 my $stream = shift;
2048 0 my $host = shift;
2049 0 my $space = shift;
2050 0 my $id = shift;
2051 0 my $params = shift;
2052 0 ▶ return unless valid_id($stream, $host, $space, $id, $params);
Branch: 1 branch
BranchTrueFalseTotal
unless valid_id($stream, $host, $space, $id, $params) 0 0 0
2053 0 ▶ return unless valid_token($stream, $host, $space, $id, $params);
Branch: 1 branch
BranchTrueFalseTotal
unless valid_token($stream, $host, $space, $id, $params) 0 0 0
2054 0 ▶ return unless valid_mime_type($stream, $host, $space, $id, $params);
Branch: 1 branch
BranchTrueFalseTotal
unless valid_mime_type($stream, $host, $space, $id, $params) 0 0 0
2055 0 ▶ return unless valid_size($stream, $host, $space, $id, $params);
Branch: 1 branch
BranchTrueFalseTotal
unless valid_size($stream, $host, $space, $id, $params) 0 0 0
2056 0 return 1;
2057 }
2058  
2059 0 ▶sub valid_id {
Subroutine valid_id: not called CC=3 SCAR=24.8
Pod undocumented
2060 0 my $stream = shift;
2061 0 my $host = shift;
2062 0 my $space = shift;
2063 0 my $id = shift;
2064 0 ▶ if (not $id) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $id) { } 0 0 0
elsif ($id =~ /[[:cntrl:]]/u) { } 0 0 0
2065 0 $log->debug("The URL lacks a page name");
2066 0 result($stream, "59", "The URL lacks a page name");
2067 0 $stream->close_gracefully();
2068 0 return;
2069 } elsif ($id =~ /[[:cntrl:]]/) {
2070 0 $log->debug("Page names must not contain any control characters");
2071 0 result($stream, "59", "Page names must not contain any control characters");
2072 0 $stream->close_gracefully();
2073 0 return;
2074 }
2075 0 return 1;
2076 }
2077  
2078 0 ▶sub valid_token {
Subroutine valid_token: not called CC=7 SCAR=40.3
Pod undocumented
2079 0 my $stream = shift;
2080 0 my $host = shift;
2081 0 my $space = shift;
2082 0 my $id = shift;
2083 0 my $params = shift;
2084 0 ▶ my $token = quotemeta($params->{token}||"");
Condition0%0/2
$params->{'token'} || ''
l!l
00
MC/DC0%0/1
$params->{'token'} || ''
$params->{'token'}
Truth table0%0/2
A
A= $params->{'token'}
Aresult
11
00
2085 0 my @tokens = @{$server->{wiki_token}};
2086 0 push(@tokens, @{$server->{wiki_space_token}->{$space}})
2087 0 ▶ if $space and $server->{wiki_space_token}->{$space};
Branch: 1 branch
BranchTrueFalseTotal
if $space and $App::Phoebe::server->{"wiki_space_token"}{$space} 0 0 0
Condition0%0/3
$space and $App::Phoebe::server->{"wiki_space_token"}{$space}
!ll&&!rl&&r
000
MC/DC0%0/2
$space and $App::Phoebe::server->{"wiki_space_token"}{$space}
$space $App::Phoebe::server->{"wiki_space_token"}{$space}
Truth table0%0/3
A and B
A= $spaceB= $App::Phoebe::server->{"wiki_space_token"}{$space}
ABresult
0X0
100
111
2088 0 $log->debug("Valid tokens: @tokens");
2089 0 $log->debug("Spaces: " . join(", ", keys %{$server->{wiki_space_token}}));
2090 0 ▶ if (not $token and @tokens) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $token and @tokens) { } 0 0 0
elsif (not grep(/^$token$/u, @tokens)) { } 0 0 0
Condition0%0/3
not $token and @tokens
!ll&&!rl&&r
000
MC/DC0%0/2
not $token and @tokens
not $token @tokens
Truth table0%0/3
not(A) and B
A= not $tokenB= @tokens
ABresult
0X0
100
111
2091 0 $log->debug("Uploads require a token");
2092 0 result($stream, "59", "Uploads require a token");
2093 0 $stream->close_gracefully();
2094 0 return;
2095 } elsif (not grep(/^$token$/, @tokens)) {
2096 0 $log->debug("Your token is the wrong token");
2097 0 result($stream, "59", "Your token is the wrong token");
2098 0 $stream->close_gracefully();
2099 0 return;
2100 }
2101 0 return 1;
2102 }
2103  
2104 0 ▶sub valid_mime_type {
Subroutine valid_mime_type: not called CC=7 SCAR=40.3
Pod undocumented
2105 0 my $stream = shift;
2106 0 my $host = shift;
2107 0 my $space = shift;
2108 0 my $id = shift;
2109 0 my $params = shift;
2110 0 ▶ my $type = $params->{mime} || "text/plain";
Condition0%0/2
$params->{'mime'} || 'text/plain'
l!l
00
MC/DC0%0/1
$params->{'mime'} || 'text/plain'
$params->{'mime'}
Truth table0%0/2
A
A= $params->{'mime'}
Aresult
11
00
2111 0 my ($main_type) = split(/\//, $type, 1);
2112 0 my @types = @{$server->{wiki_mime_type}};
2113 # the wiki always allows text/plain or text/gemini
2114 0 ▶ if ($type eq "text/plain" or $type eq "text/gemini") {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'text/plain' or $type eq 'text/gemini') { } 0 0 0
elsif (not @types) { } 0 0 0
elsif (not grep(/^$type$/u, @types) || grep(/^$main_type$/u, @types)) { } 0 0 0
Condition0%0/3
$type eq "text/plain" or $type eq "text/gemini"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "text/plain" or $type eq "text/gemini"
$type eq "text/plain" $type eq "text/gemini"
Truth table0%0/3
A or B
A= $type eq "text/plain"B= $type eq "text/gemini"
ABresult
1X1
011
000
2115 0 return 1;
2116 } elsif (not @types) {
2117 0 $log->debug("This wiki does not allow file uploads");
2118 0 result($stream, "59", "This wiki does not allow file uploads");
2119 0 $stream->close_gracefully();
2120 0 return;
2121 ▶ } elsif (not grep(/^$type$/, @types) and not grep(/^$main_type$/, @types)) {
Condition0%0/3
grep(/^$type$/u, @types) || grep(/^$main_type$/u, @types)
l!l&&r!l&&!r
000
MC/DC0%0/2
grep(/^$type$/u, @types) || grep(/^$main_type$/u, @types)
grep(/^$type$/u, @types) grep(/^$main_type$/u, @types)
Truth table0%0/3
A || B
A= grep(/^$type$/u, @types)B= grep(/^$main_type$/u, @types)
ABresult
1X1
011
000
2122 0 $log->debug("This wiki does not allow $type");
2123 0 result($stream, "59", "This wiki does not allow $type, only @types");
2124 0 $stream->close_gracefully();
2125 0 return;
2126 }
2127 0 return 1;
2128 }
2129  
2130 0 ▶sub valid_size {
Subroutine valid_size: not called CC=3 SCAR=24.8
Pod undocumented
2131 0 my $stream = shift;
2132 0 my $host = shift;
2133 0 my $space = shift;
2134 0 my $id = shift;
2135 0 my $params = shift;
2136 0 my $size = $params->{size};
2137 0 ▶ if ($size !~ /^\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $size =~ /^\d+$/u) { } 0 0 0
elsif ($size > $App::Phoebe::server->{'wiki_page_size_limit'}) { } 0 0 0
2138 0 $log->debug("You need to send along the number of bytes, not '$size'");
2139 0 result($stream, "59", "You need to send along the number of bytes, not '$size'");
2140 0 $stream->close_gracefully();
2141 0 return;
2142 } elsif ($size > $server->{wiki_page_size_limit}) {
2143 0 $log->debug("This wiki does not allow more than $server->{wiki_page_size_limit} bytes per page");
2144 0 result($stream, "59", "This wiki does not allow more than $server->{wiki_page_size_limit} bytes per page");
2145 0 $stream->close_gracefully();
2146 0 return;
2147 }
2148 0 return 1;
2149 }
2150  
2151 1 1;
2152  
2153 __DATA__
« Phoebe/WebDAV.pm ↑ Summary Phoebe/Galleries.pm »