Phoebe/Oracle.pm untested

filter: Statementstmt 0.0% 0 / 401 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 647 CC 62 SCAR 43.3
CC
62
Cov
0%
serve_question
210.0
oracle
110.0
answer_question
90.0
CRAP 75.7
1 # -*- mode: perl -*-
2 # Copyright (C) 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::Oracle - an anonymous question asking game
22  
23 =head1 DESCRIPTION
24  
25 By default, Phoebe creates a wiki editable by all. With this extension, the
26 C</oracle> space turns into a special site: if you have a client certificate,
27 you can pose questions and get answers.
28  
29 When you ask a question, you can delete any answers on it, and the question
30 itself. Once it has gotten three answers, it is hidden from view and only you
31 can decide wether to delete it, or whether to publish it. If the question is no
32 longer waiting for answers, deleting every answer deletes the question, too.
33  
34 You can only answer questions not your own. You can answer every question just
35 once (even if you or the question asker deletes your answer, there is no going
36 back). You can delete your answer. If the question is no longer waiting for
37 answers, deleting the last answer deletes the question, too.
38  
39 Simply add it to your F<config> file. If you are virtual hosting, name the host
40 or hosts for your capsules.
41  
42 package App::Phoebe::Oracle;
43 use Modern::Perl;
44 our @oracle_hosts = qw(transjovian.org);
45 use App::Phoebe::Oracle;
46  
47 If you don't want to use C</oracle> for the game, you can change it:
48  
49 our $oracle_space = 'truth';
50  
51 If you want to change the maximu number of answers that a question may have:
52  
53 our $max_answers = 5;
54  
55 If you want to notify Antenna whenever a new question has been asked:
56  
57 use App::Phoebe qw($log);
58 use IO::Socket::SSL;
59 # a very simple Gemini client
60 sub query {
61 my $url = shift;
62 my($scheme, $authority, $path, $query, $fragment) =
63 $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(\S*))?|;
64 my ($host, $port) = split(/:/, $authority);
65 my $socket = IO::Socket::SSL->new(
66 PeerHost => $host, PeerPort => $port||1965,
67 # don't verify the server certificate
68 SSL_verify_mode => SSL_VERIFY_NONE, );
69 $socket->print($url);
70 local $/ = undef; # slurp
71 return <$socket>;
72 }
73 # wrap the save_data sub in our own code
74 *old_save_oracle_data = \&App::Phoebe::Oracle::save_data;
75 *App::Phoebe::Oracle::save_data = \&new_save_oracle_data;
76 # call Antenna after saving
77 sub new_save_oracle_data {
78 old_save_oracle_data(@_);
79 my $gemlog = "gemini://transjovian.org/oracle/log";
80 my $res = query("gemini://warmedal.se/~antenna/submit?$gemlog");
81 my ($code) = $res =~ /^(\d+)/;
82 $log->info("Antenna: $code");
83 }
84  
85 =cut
86  
87 package App::Phoebe::Oracle;
88 use App::Phoebe qw($server $log @extensions host_regex port success result print_link wiki_dir with_lock to_url);
89 use File::Slurper qw(read_binary write_binary);
90 use Mojo::JSON qw(decode_json encode_json);
91 use Net::IDN::Encode qw(domain_to_ascii);
92 use List::Util qw(first any none);
93 use Encode qw(encode_utf8 decode_utf8);
94 use POSIX qw(strftime);
95 use Modern::Perl;
96 use URI::Escape;
97 use utf8;
98  
99 push(@extensions, \&oracle);
100  
101 our $oracle_space = "oracle";
102 our @oracle_hosts;
103 our $max_answers = 3;
104  
105 sub oracle {
106 my $stream = shift;
107 my $url = shift;
108 my $hosts = oracle_regex();
109 my $port = port($stream);
110 my ($host, $question, $answer, $number, @numbers);
111 if (($host) = $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/?$!) {
112 return serve_main_menu($stream, $host);
113 } elsif (($host) = $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/questions$!) {
114 return serve_questions($stream, $host);
115 } elsif (($host, $question) = $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/ask(?:\?([^#]+))?$!) {
116 return ask_question($stream, $host, decode_query($question));
117 } elsif (($host, $number) = $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/question/(\d+)$!) {
118 return serve_question($stream, $host, $number);
119 } elsif (($host, $number, $answer) =
120 $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/question/(\d+)/answer(?:\?([^#]+))?$!) {
121 return answer_question($stream, $host, $number, decode_query($answer));
122 } elsif (($host, $number) = $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/question/(\d+)/publish$!) {
123 return publish_question($stream, $host, $number);
124 } elsif (($host, $number) = $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/question/(\d+)/delete$!) {
125 return delete_question($stream, $host, $number);
126 } elsif (($host, @numbers) = $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/question/(\d+)/(\d+)/delete$!) {
127 return delete_answer($stream, $host, @numbers);
128 } elsif (($host) = $url =~ m!^gemini://($hosts)(?::$port)?/$oracle_space/log$!) {
129 return serve_log($stream, $host);
130 }
131 return;
132 }
133  
134 sub oracle_regex {
135 return join("|", map { quotemeta domain_to_ascii $_ } @oracle_hosts) || host_regex();
136 }
137  
138 sub load_data {
139 my $host = shift;
140 my $dir = wiki_dir($host, $oracle_space);
141 return [] unless -f "$dir/oracle.json";
142 return decode_json read_binary("$dir/oracle.json");
143 }
144  
145 sub save_data {
146 my ($stream, $host, $data) = @_;
147 my $dir = wiki_dir($host, $oracle_space);
148 my $bytes = encode_json $data;
149 # We don't close the stream on a successful call.
150 with_lock($stream, $host, $oracle_space, sub {
151 write_binary("$dir/oracle.json", $bytes)});
152 }
153  
154 sub new_number {
155 my $data = shift;
156 while (1) {
157 my $n = int(rand(10000));
158 return $n unless any { $n eq $_->{number} } @$data;
159 }
160 }
161  
162 sub decode_query {
163 my $text = shift;
164 return '' unless $text;
165 $text =~ s/\+/ /g;
166 return decode_utf8(uri_unescape($text));
167 }
168  
169 sub serve_main_menu {
170 my ($stream, $host) = @_;
171 my $data = load_data($host);
172 my $fingerprint = $stream->handle->get_fingerprint();
173 success($stream);
174 $log->info("Serving oracles");
175 $stream->write("# Oracle\n");
176 if ($fingerprint) {
177 $stream->write("You have an identity or a client certificate picked, so you can ask a question or answer questions by others.\n");
178 } else {
179 $stream->write("You need to use an identity or pick a client certificate if you want to ask a question or give an answer.\n");
180 }
181 $stream->write("=> /$oracle_space/ask Ask a question\n");
182 $stream->write("=> /$oracle_space/log Check the log\n");
183 # skipping answered and unpublished questions, unless you asked the question
184 my @questions = grep {
185 $_->{status} ne 'answered'
186 or $fingerprint and $fingerprint eq $_->{fingerprint}
187 } @$data;
188 for my $question (@questions) {
189 $stream->write("\n\n");
190 $stream->write("## Question #$question->{number}\n");
191 $stream->write("> " . encode_utf8 $question->{text});
192 $stream->write("\n");
193 if ($fingerprint and $fingerprint eq $question->{fingerprint}) {
194 $stream->write("This is your question.");
195 $stream->write(" You need to publish or delete it before you can ask another one.")
196 if $question->{status} ne 'published';
197 $stream->write("\n");
198 $stream->write("=> /$oracle_space/question/$question->{number} Manage\n");
199 } elsif ($question->{status} eq 'asked') {
200 if ($fingerprint and any { $fingerprint eq $_->{fingerprint} } @{$question->{answers}}) {
201 $stream->write("This question is still looking for answers, but you already gave your answer.\n");
202 $stream->write("=> /$oracle_space/question/$question->{number} Take a look\n");
203 } else {
204 $stream->write("This question is still looking for answers.\n");
205 $stream->write("=> /$oracle_space/question/$question->{number} Answer\n");
206 }
207 } else {
208 # it's published
209 my $n = grep { $_->{text} } @{$question->{answers}};
210 if ($n == 1) {
211 $stream->write("This question has one answer.\n");
212 } else {
213 $stream->write("This question has $n answers.\n");
214 }
215 $stream->write("=> /$oracle_space/question/$question->{number} Show\n");
216 }
217 }
218 return 1;
219 }
220  
221 sub serve_log {
222 my ($stream, $host) = @_;
223 my $data = load_data($host);
224 success($stream);
225 $log->info("Serving oracle log");
226 $stream->write("# Oracle Log\n");
227 my @questions = grep { $_->{status} ne 'answered' } @$data;
228 for my $question (@questions) {
229 my $text = $question->{text};
230 $text =~ s/\n/ /g;
231 if (length($text) > 50) {
232 $text = substr($text, 0, 50);
233 $text =~ s/\s+\S+$/…/ or $text =~ s/\s+$/…/;
234 }
235 $text = encode_utf8 $text;
236 $stream->write("=> /$oracle_space/question/$question->{number} $question->{date} Question #$question->{number}: $text\n");
237 }
238 return 1;
239 }
240  
241 sub serve_questions {
242 my ($stream, $host) = @_;
243 my $data = load_data($host);
244 my $fingerprint = $stream->handle->get_fingerprint();
245 return result($stream, "60", "You need a client certificate to list your questions") unless $fingerprint;
246 success($stream);
247 $log->info("Serving own questions");
248 $stream->write("# Oracle\n");
249 $stream->write("=> /$oracle_space/ask Ask a question\n");
250 my @questions = grep { $_->{fingerprint} eq $fingerprint } @$data;
251 for my $question (@questions) {
252 $stream->write("\n\n");
253 if ($question->{status} eq 'asked') {
254 $stream->write("## Asked question #$question->{number}\n");
255 } elsif ($question->{status} eq 'answered') {
256 $stream->write("## Answered question #$question->{number}\n");
257 } elsif ($question->{status} eq 'published') {
258 $stream->write("## Published question #$question->{number}\n");
259 }
260 $stream->write(encode_utf8 $question->{text});
261 $stream->write("\n");
262 $stream->write("=> /$oracle_space/question/$question->{number} Show answers\n");
263 }
264 return 1;
265 }
266  
267 sub serve_question {
268 my ($stream, $host, $number) = @_;
269 my $data = load_data($host);
270 my $question = first { $_->{number} eq $number } @$data;
271 if (not $question) {
272 $log->info("Question $number not found");
273 return result($stream, "30", to_url($stream, $host, $oracle_space, ""));
274 }
275 success($stream);
276 my $fingerprint = $stream->handle->get_fingerprint();
277 if ($question->{status} eq 'answered'
278 and (not $fingerprint
279 or $fingerprint ne $question->{fingerprint})) {
280 $log->info("Not the owner requesting question $number");
281 $stream->write("# Question #$question->{number}\n");
282 $stream->write("This question has been answered and it has not been published.\n");
283 $stream->write("You are not the owner of this question, which is why you cannot do anything about it.\n");
284 $stream->write("Switch identity or pick a different client certificate if you think you are the owner of this question\n");
285 $stream->write("=> /$oracle_space/ Back to the oracle\n");
286 return;
287 }
288 $log->info("Serving oracle $question->{number}");
289 $stream->write("# Question #$question->{number}\n");
290 $stream->write("=> /$oracle_space/ Back to the oracle\n");
291 if ($fingerprint) {
292 my $n = grep { $_->{text} } @{$question->{answers}};
293 if ($fingerprint eq $question->{fingerprint}) {
294 $stream->write("=> /$oracle_space/question/$number/delete Delete this question\n");
295 if (($question->{status} eq 'asked' and $n > 0)
296 or $question->{status} eq 'answered') {
297 $stream->write("=> /$oracle_space/question/$number/publish Publish this question\n");
298 }
299 } elsif ($n < $max_answers
300 and none { $fingerprint eq $_->{fingerprint} } @{$question->{answers}}) {
301 # only allow answers if the undeleted answers is below the maximum, and
302 # you haven't answered before (even if it was subsequently deleted
303 $stream->write("=> /$oracle_space/question/$number/answer Answer this question\n");
304 }
305 }
306 if ($question->{status} eq 'asked'
307 and (not $fingerprint
308 or $fingerprint ne $question->{fingerprint})) {
309 # if the question is being asked and you're not the question asker, list the
310 # question but not the answers
311 $stream->write("\n");
312 $stream->write(encode_utf8 $question->{text});
313 $stream->write("\n");
314 # if you haven't answered the question, you may answer it; if you have
315 # answered it, you may delete your answer
316 if ($fingerprint) {
317 my $n = 0;
318 my $answered = 0;
319 for my $answer (@{$question->{answers}}) {
320 $n++;
321 next unless $answer->{fingerprint} eq $fingerprint;
322 $answered = 1;
323 next unless $answer->{text};
324 $answered = 2;
325 $stream->write("\n");
326 $stream->write("## Your answer\n");
327 $stream->write(encode_utf8 $answer->{text});
328 $stream->write("\n");
329 $stream->write("=> /$oracle_space/question/$question->{number}/$n/delete Delete this answer\n");
330 return 1;
331 }
332 if ($answered == 1) {
333 $stream->write("\n");
334 $stream->write("(Your answer was deleted.)\n");
335 return 1;
336 }
337 }
338 } else {
339 $stream->write("\n");
340 $stream->write(encode_utf8 $question->{text});
341 $stream->write("\n");
342 my $n = 0;
343 for my $answer (@{$question->{answers}}) {
344 $n++;
345 next unless $answer->{text};
346 $stream->write("\n");
347 $stream->write("## Answer #$n\n");
348 $stream->write(encode_utf8 $answer->{text});
349 $stream->write("\n");
350 if ($fingerprint
351 and ($fingerprint eq $question->{fingerprint}
352 or $fingerprint eq $answer->{fingerprint})) {
353 $stream->write("=> /$oracle_space/question/$question->{number}/$n/delete Delete this answer\n");
354 }
355 }
356 }
357 return 1;
358 }
359  
360 sub ask_question {
361 my ($stream, $host, $text) = @_;
362 my $fingerprint = $stream->handle->get_fingerprint();
363 return result($stream, "60", "You need a client certificate to ask a question") unless $fingerprint;
364 my $data = load_data($host);
365 my $question = first { $_->{fingerprint} eq $fingerprint and $_->{status} ne 'published' } @$data;
366 if ($question) {
367 $log->info("Asking the oracle a question but there already is one asked question");
368 success($stream);
369 $stream->write("# Ask the oracle a question\n");
370 $stream->write("You already have an unanswered question.\n");
371 $stream->write("=> /$oracle_space/question/$question->{number} Show question\n");
372 } elsif (not $text) {
373 $log->info("Asking the oracle a question");
374 result($stream, "10", "Your question for the oracle");
375 } else {
376 $log->info("Saving a new question for the oracle");
377 $question = {
378 date => strftime("%Y-%m-%d", gmtime),
379 number => new_number($data),
380 text => $text,
381 fingerprint => $fingerprint,
382 status => 'asked',
383 answers => [],
384 };
385 unshift(@$data, $question);
386 save_data($stream, $host, $data);
387 success($stream);
388 $stream->write("# The oracle accepts!\n");
389 $stream->write("You question was submitted to the oracle.\n");
390 $stream->write("=> /$oracle_space/question/$question->{number} Show question\n");
391 }
392 return 1;
393 }
394  
395 sub answer_question {
396 my ($stream, $host, $number, $text) = @_;
397 my $fingerprint = $stream->handle->get_fingerprint();
398 if (not $fingerprint) {
399 $log->info("Answering a question requires a certificate");
400 result($stream, "60", "You need a client certificate to answer a question");
401 return 1;
402 }
403 my $data = load_data($host);
404 my $question = first { $_->{number} eq $number } @$data;
405 if (not $question) {
406 $log->info("Answering a deleted question");
407 success($stream);
408 $stream->write("# Answer a question\n");
409 $stream->write("The question you wanted to answer has been deleted.\n");
410 $stream->write("=> /$oracle_space/ Back to the oracle\n");
411 return 1;
412 } elsif ($fingerprint eq $question->{fingerprint}) {
413 $log->info("The question asker may not answer");
414 result($stream, "40", "You may not answer your own question");
415 return 1;
416 }
417 my $answer = first { $_->{fingerprint} eq $fingerprint } @{$question->{answers}};
418 if ($answer) {
419 $log->info("Answering a question again");
420 success($stream);
421 $stream->write("# Answer a question\n");
422 $stream->write("You already answered this question.\n");
423 $stream->write("=> /$oracle_space/question/$question->{number} Show\n");
424 } elsif ($question->{status} ne 'asked') {
425 $log->info("Answering an answered question");
426 success($stream);
427 $stream->write("# Answer a question\n");
428 $stream->write("This question no longer takes answers.\n");
429 $stream->write("=> /$oracle_space/question/$question->{number} Show\n");
430 } elsif (not $text) {
431 $log->info("Answering a question");
432 result($stream, "10", "Your answer for the oracle");
433 } else {
434 $log->info("Saving a new answer for the oracle");
435 $answer = {
436 text => $text,
437 fingerprint => $fingerprint,
438 };
439 push(@{$question->{answers}}, $answer);
440 my $n = grep { $_->{text} } @{$question->{answers}};
441 $question->{status} = 'answered' if $n >= $max_answers;
442 save_data($stream, $host, $data);
443 result($stream, "30", to_url($stream, $host, $oracle_space, "")) if $question->{status} eq 'answered';
444 result($stream, "30", to_url($stream, $host, $oracle_space, "question/$question->{number}"));
445 }
446 return 1;
447 }
448  
449 sub delete_answer {
450 my ($stream, $host, $question_number, $answer_number) = @_;
451 my $fingerprint = $stream->handle->get_fingerprint();
452 if (not $fingerprint) {
453 $log->info("Deleting an answer requires a certificate");
454 result($stream, "60", "You need a client certificate to delete an answer");
455 return 1;
456 }
457 my $data = load_data($host);
458 my $question = first { $_->{number} eq $question_number } @$data;
459 if (not $question) {
460 $log->info("Deleting an answer of a deleted question");
461 success($stream);
462 $stream->write("# Delete an answer\n");
463 $stream->write("The answer you wanted to answer belongs to a deleted question.\n");
464 $stream->write("=> /$oracle_space/ Back to the oracle\n");
465 return 1;
466 }
467 my $n = 0;
468 my $answer;
469 for (@{$question->{answers}}) {
470 next unless ++$n eq $answer_number;
471 $answer = $_;
472 last;
473 }
474 if (not $answer) {
475 $log->info("Deleting an answer that does not exist (@{$question->{answers}})");
476 success($stream);
477 $stream->write("# Delete an answer\n");
478 $stream->write("The answer you wanted to delete does not exist.\n");
479 $stream->write("=> /$oracle_space/question/$question->{number} Show\n");
480 return 1;
481 } elsif ($fingerprint ne $answer->{fingerprint}
482 and $fingerprint ne $question->{fingerprint}) {
483 $log->info("Deleting an answer not your own");
484 success($stream);
485 $stream->write("# Delete an answer\n");
486 $stream->write("The answer you wanted to delete belongs to somebody else.\n");
487 $stream->write("Switch identity or pick a different client certificate if you think you are the owner of this question or this answer\n");
488 $stream->write("=> /$oracle_space/question/$question->{number} Show\n");
489 return 1;
490 } elsif (not $answer->{text}) {
491 $log->info("Deleting a deleted answer");
492 result($stream, "30", to_url($stream, $host, $oracle_space, "question/$question->{number}"));
493 }
494 $log->info("Deleting an answer");
495 $answer->{text} = undef;
496 if ($question->{status} ne 'asked') {
497 my $n = grep { $_->{text} } @{$question->{answers}};
498 if (not $n) {
499 # answered or published question with no answers gets deleted
500 @$data = grep { $_->{number} ne $question->{number} } @$data;
501 save_data($stream, $host, $data);
502 $stream->write("=> /$oracle_space/ Back to the oracle\n");
503 return 1;
504 }
505 }
506 save_data($stream, $host, $data);
507 result($stream, "30", to_url($stream, $host, $oracle_space, "question/$question->{number}"));
508 return 1;
509 }
510  
511 sub publish_question {
512 my ($stream, $host, $number) = @_;
513 my $fingerprint = $stream->handle->get_fingerprint();
514 if (not $fingerprint) {
515 $log->info("Publishing a question requires a certificate");
516 result($stream, "60", "You need a client certificate to publish a question");
517 return 1;
518 }
519 my $data = load_data($host);
520 my $question = first { $_->{number} eq $number } @$data;
521 if (not $question) {
522 $log->info("Publishing a deleted question");
523 success($stream);
524 $stream->write("# Publish a question\n");
525 $stream->write("The question you wanted to publish has been deleted.\n");
526 $stream->write("=> /$oracle_space/ Back to the oracle\n");
527 return 1;
528 } elsif ($fingerprint ne $question->{fingerprint}) {
529 $log->info("Only the question asker may publish");
530 $stream->write("# Publish a question\n");
531 $stream->write("You are not the owner of this question, which is why you cannot publish it.\n");
532 $stream->write("Switch identity or pick a different client certificate if you think you are the owner of this question\n");
533 $stream->write("=> /$oracle_space/ Back to the oracle\n");
534 return 1;
535 }
536 $log->info("Publishing a question");
537 $question->{status} = 'published';
538 save_data($stream, $host, $data);
539 result($stream, "30", to_url($stream, $host, $oracle_space, "question/$question->{number}"));
540 return 1;
541 }
542  
543 sub delete_question {
544 my ($stream, $host, $number) = @_;
545 my $fingerprint = $stream->handle->get_fingerprint();
546 if (not $fingerprint) {
547 $log->info("Deleting a question requires a certificate");
548 result($stream, "60", "You need a client certificate to delete a question");
549 return 1;
550 }
551 my $data = load_data($host);
552 my $question = first { $_->{number} eq $number } @$data;
553 if (not $question) {
554 $log->info("Deleting a deleted question");
555 result($stream, "30", to_url($stream, $host, $oracle_space, ""));
556 return 1;
557 } elsif ($fingerprint ne $question->{fingerprint}) {
558 $log->info("Only the question asker may delete");
559 success($stream);
560 $stream->write("# Delete a question\n");
561 $stream->write("You are not the owner of this question, which is why you cannot delete it.\n");
562 $stream->write("Switch identity or pick a different client certificate if you think you are the owner of this question\n");
563 $stream->write("=> /$oracle_space/question/$question->{number} Back to the question\n");
564 return 1;
565 }
566 $log->info("Deleting a question");
567 @$data = grep { $_->{number} ne $question->{number} } @$data;
568 save_data($stream, $host, $data);
569 result($stream, "30", to_url($stream, $host, $oracle_space, ""));
570 return 1;
571 }
572  
573 1;