]> git.99rst.org Git - irssi-scripts.irssi.org.git/commitdiff
[fortune] change the interface
authorbw1 <redacted>
Thu, 30 May 2019 20:59:25 +0000 (22:59 +0200)
committerbw1 <redacted>
Mon, 10 Jun 2019 20:58:18 +0000 (22:58 +0200)
scripts/fortune.pl

index be2e92e8d61bff3bdd74c40700d4bb7b12e755bd..cb57da96075004ae91828cc808cf4962bdc2e36f 100644 (file)
@@ -1,27 +1,37 @@
 #
 #   fortune
 #
-#   Edited by: Ivo Marino <eim@cpan.org>
-#   $Id: fortune.pl,v 1.3 2004/12/17 19:39:19 eim Exp $
+#   Edited by:
+#   Ivo Marino <eim@cpan.org> 1.3 2004/12/17
+#   bw1 <bw1@aol.at>          1.4 2019/05/30
 #
 #   Required (Debian) packages:
 #
 #       . fortune-mod       The fortune core binaries
 #       . fortunes-min      Basic english fortune cookies
+#
+#   Optional (Debian) packages:
+#
 #       . fortunes-de       German fortune cookies
 #       . fortunes-it       Italian fortune cookies
 #
 #   Usage:
 #
-#       Inside a channel write: /fortune <nick> [lang]
-#       The optional [lang] parameter can be:
+#       Inside Irssi write: /fortune [nick] [-h] [-o options]
+#       The optional [options] parameter can be:
 #
 #           . en            English
 #           . de            German
 #           . it            Italian
+#           or anything else what the fortune command provide
 #
 #       If not defined the fortune script defaults to en.
 #
+#   Settings:
+#
+#       fortune_command
+#       fortune_default_args
+#
 #   TODO:
 #
 #       . Check if specified user exists.
@@ -32,7 +42,8 @@ use strict;
 use vars qw($VERSION %IRSSI);
 
 use Irssi;
-($VERSION) = '$Id: fortune.pl,v 1.3 2004/12/17 19:39:19 eim Exp $' =~ / (\d+\.\d+) /;
+use Getopt::Long qw/GetOptionsFromString/;
+$VERSION = '1.4';
 %IRSSI = (
     authors     => 'Ivo Marino',
     contact     => 'eim@cpan.rg',
@@ -41,42 +52,43 @@ use Irssi;
     license     => 'Public Domain',
 );
 
+my ($nargs, $help);
+my %opts = (
+    'h' => \$help,
+    'o=s' => \$nargs,
+);
+
 sub fortune {
 
     my ($param, $server, $witem) = @_;
     my $return = 0;
     my $cookie = '';
+    my $cmd = Irssi::settings_get_str($IRSSI{name}.'_command');
+    my $args = Irssi::settings_get_str($IRSSI{name}.'_default_args');
+    my ($ret, $arg)= GetOptionsFromString($param, %opts) or $help=1;
+    my $nick = $arg->[0];
 
-    if ($param) {
+    if (!defined $help) {
 
         if ($server || $server->{connected}) {
 
-            (my $nick, my $lang) = split (' ', $param);
-
-            $lang = 'en' unless ($lang eq 'de'|| $lang eq 'it' || $lang eq 'en');
-
-            Irssi::print ("Nick: " . $nick . ", Lang: \"" . $lang . "\"");
-
-            if ($lang eq 'de') {
-
-                $cookie = `fortune -x`;
+            #Irssi::print ("Nick: " . $nick . ", Lang: \"" . $lang . "\"");
 
-            } elsif ($lang eq 'it') {
-
-                $cookie = `fortune -a italia`;
-
-            } else {
-
-                $cookie = `fortune.en -a fortunes literature riddles`;
-            }
+            $args = $nargs if (defined $nargs);
+            $cookie = `$cmd $args`;
 
             $cookie =~ s/\s*\n\s*/ /g;
 
             if ($cookie) {
 
                 if ($witem && ($witem->{type} eq "CHANNEL")) {
-
+                    if (defined $nick) {
                         $witem->command('MSG ' . $witem->{name} . ' ' . $nick . ': ' . $cookie);
+                    } else {
+                        $witem->command('MSG ' . $witem->{name} .' '. $cookie);
+                    }
+                } else {
+                    Irssi::print ($cookie);
                 }
 
             } else {
@@ -93,11 +105,20 @@ sub fortune {
 
     } else {
 
-        Irssi::print ("Usage: /fortune <nick> [language]");
+        Irssi::print ("Usage: /fortune [nick] [-h] [-o options]");
         $return = 1;
     }
 
+    $nick = undef;
+    $nargs= undef;
+    $help = undef;
+
     return $return;
 }
 
+Irssi::settings_add_str($IRSSI{name}, $IRSSI{name}.'_command', 'fortune');
+Irssi::settings_add_str($IRSSI{name}, $IRSSI{name}.'_default_args', '');
+
 Irssi::command_bind ('fortune', \&fortune);
+
+# vim:set expandtab sw=4 ts=4:
git clone https://git.99rst.org/PROJECT