File: fortune

package info (click to toggle)
konversation 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,928 kB
  • sloc: cpp: 46,913; xml: 787; python: 556; sh: 96; perl: 86; makefile: 7
file content (55 lines) | stat: -rwxr-xr-x 1,032 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env perl
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# SPDX-FileCopyrightText: 2004 İsmail Dönmez <ismail@kde.org>

$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);
my $dbus_cmd = $ENV{'KONVERSATION_DBUS_BIN'} || 'qdbus';
exec $dbus_cmd, 'org.kde.konversation', '/irc', 'say', $SERVER, $TARGET, $MESSAGE;