Script: fortune.pl

Sends a random fortune cookie.
Author: ptitlouis — Version: 0.2 — License: Public-domain
For WeeChat ≥ 0.3.0, requires: fortune.
Tags: fortune
Added: 2006-02-12 — Updated: 2009-05-03

Download GitHub Repository

 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
# This script is a port from the original fortune.pl irssi script written by
# Ivo Marino <eim@cpan.org>. This script is in the public domain
# 
# Original WeeChat Author: Julien Louis <ptitlouis@sysif.net>
#
# Port to WeeChat 0.3.0: Sid Vicious (aka Trashlord) <dornenreich666@gmail.com>

weechat::register("fortune", "ptitlouis", "0.2", "Public domain", "Send a random fortune cookie to a specified nick", "", "");

weechat::hook_command("fortune", "Send a random fortune cookie to a specified nick",
	"<nick> [lang]", 
	"<nick> The nickname to send the fortune cookie\n" .
	" [lang] The cookie language (Default: en)\n",
	"", "fortune", "");

sub fortune {

    my ($data, $buffer, $param) = @_;
    my $rc = weechat::WEECHAT_RC_OK;
    my $cookie = '';
    if ($param) {
        (my $nick, my $lang) = split (' ', $param);
        $lang = 'en' unless ($lang eq 'de'|| $lang eq 'it' || $lang eq
                             'en' || $lang eq 'fr' );
        weechat::print($buffer, "Nick: " . $nick . ", Lang: \"" . $lang . "\"");
        
        if ($lang eq 'de') {
            $cookie = `fortune -x`;
        } 
        elsif ($lang eq 'it') {
            $cookie = `fortune -a italia`;
        } 
        else {
            $cookie = `fortune -a fortunes literature riddles`;
        }
        
        $cookie =~ s/\s*\n\s*/ /g;
        if ($cookie) {
            weechat::command($buffer, $nick . ": " . $cookie, $channel);
        }
        
        else {
            weechat::print($buffer, "No cookie.");
            $rc = weechat::WEECHAT_RC_ERROR;
        }
    }
    else {
        weechat::print ($buffer, "Usage: /fortune <nick> [language]");
        $rc = weechat::WEECHAT_RC_ERROR;
    }
    
    return $rc;
}