diff options
Diffstat (limited to 'konversation/scripts/fortune')
-rwxr-xr-x | konversation/scripts/fortune | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/konversation/scripts/fortune b/konversation/scripts/fortune new file mode 100755 index 0000000..a3b61d3 --- /dev/null +++ b/konversation/scripts/fortune @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +# Copyright (C) 2004 by İsmail Dönmez +# Licensed under GPL v2 or later at your option + +$PORT = shift; +$SERVER = shift; +$TARGET = shift; + +sub RANDOM_INT ($$) { + my($min, $max) = @_; + return $min if $min == $max; + ($min, $max) = ($max, $min) if $min > $max; + return $min + int rand(1 + $max - $min); +} + +open(FORTUNES,"fortunes.dat") or die("Could not open fortunes file!"); + +while (<FORTUNES>) { + chomp; + ++$TOTAL_LINES; +} + +seek(FORTUNES,0,0); + +srand; +$LINE = RANDOM_INT(0,$TOTAL_LINES - 5); + +$MESSAGE = "4Random Fortune: "; + +while (<FORTUNES>) { + $LINE_COUNT++; + + if ( !$START && $LINE_COUNT >= $LINE ) { + if ( $_ eq "\%\n" ) { + $START = 1; + next; + } + next; + } + + elsif ( $START ) { + if( $_ eq "\%\n" ) { + last; + } + else { + chomp; + s/(\s)+/$1/g; + $MESSAGE .= $_." "; + } + } +} +close(FORTUNES); +exec 'dcop', $PORT, 'default', 'say', $SERVER, $TARGET, $MESSAGE; |