about summary refs log tree commit diff stats
path: root/lyra.pl
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-08-20 22:29:54 +0530
committerAndinus <andinus@nand.sh>2020-08-20 22:29:54 +0530
commitbc2163c379f0fa2b4b35e0bf6c6e54d243aec53c (patch)
treee2045974acda55a91dbad66a085fe9e0fa13633d /lyra.pl
downloadlyra-bc2163c379f0fa2b4b35e0bf6c6e54d243aec53c.tar.gz
Initial commit
Diffstat (limited to 'lyra.pl')
-rwxr-xr-xlyra.pl58
1 files changed, 58 insertions, 0 deletions
diff --git a/lyra.pl b/lyra.pl
new file mode 100755
index 0000000..3aad720
--- /dev/null
+++ b/lyra.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+use Path::Tiny;
+
+use constant is_OpenBSD => $^O eq "openbsd";
+require OpenBSD::Unveil
+    if is_OpenBSD;
+sub unveil {
+    if (is_OpenBSD) {
+        return OpenBSD::Unveil::unveil(@_);
+    } else {
+        return 1;
+    }
+}
+
+# Unveil @INC.
+foreach my $path (@INC) {
+    unveil( $path, 'rx' )
+        or die "Unable to unveil: $!\n";
+}
+
+my %dispatch = (
+    "random" => \&random,
+    "help" => \&HelpMessage,
+);
+
+sub HelpMessage {
+    say qq{Usage:
+    random
+        Print a random fortune.
+    help
+        Show this text.}
+}
+
+# Print a random fortune from $path.
+sub random {
+    my $path = "$ENV{HOME}/quotes.txt";
+    $path = $ARGV[1] if $ARGV[1]; # Use $ARGV[1] as path if it exists.
+    unveil( $path, "r" )
+        or die "Unable to unveil: $!\n"; # Unveil $path as read-only.
+
+    my $file = path($path)->absolute;
+    my @fortunes = split/\n%\n/, $file->slurp;
+
+    say $fortunes[ rand @fortunes ]; # Print random fortune.
+}
+
+if ( $dispatch{ $ARGV[0] } ) {
+    $dispatch{ $ARGV[0] }->();
+} elsif ( scalar @ARGV == 0 ) {
+    HelpMessage();
+} else {
+    say "lyra: no such option";
+}