-
Notifications
You must be signed in to change notification settings - Fork 0
/
tr0l.pm
93 lines (75 loc) · 2.58 KB
/
tr0l.pm
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package tr0l;
use strict;
use warnings;
use Irssi;
my $vfile = "VERSION";
open( FILE, '<', $vfile ) or die 'Could not open file: ' . $!;
our $VERSION = <FILE>;
our @EXPORT = qw(respond command_set_handler);
our (@CHANNELS,
%COMMANDS,
%HELP,
$DEFAULT
) = ( );
sub respond {
my ($server, $msg, $target, $nick) = @_;
my (@command, $cmd, $m1, $m2, $m3, @arguments, $chans, $output) = 0;
# test that message came from a joined chan
$chans = join("|", @tr0l::CHANNELS);
return unless $target =~ /^#($chans)/;
@command = split(' ', $msg);
# account for possible nick prefix
if($command[0] =~ /tr0l:?/) {
shift(@command);
}
$cmd = $command[0];
shift(@command);
@arguments = @command;
return unless $cmd;
return if $msg eq $cmd;
# invoke handler
my $responder = $COMMANDS{$cmd} // $COMMANDS{$DEFAULT};
$output = $responder->($server, $target, $nick, @arguments);
if($tr0l::output) {
# respond with what is assumed to be a returned string
$server->command("msg $target $tr0l::output");
}
}
sub command_set_handler {
my ($cmd, $doc, $handler) = @_;
$COMMANDS{$cmd} = $handler;
$HELP{$cmd} = $doc;
}
sub command_set_default {
my ($cmd, $hander) = @_;
$DEFAULT = $cmd;
$COMMANDS{$cmd} = $hander;
}
BEGIN {
command_set_default("_DEFAULT_",
sub{
my ($server, $chan, $nick, @args) = @_;
Irssi::print("Failed command:");
Irssi::print(" \$target " . $chan);
Irssi::print(" \$nick " . $nick);
Irssi::print(" \$args " . @args);
return "no intiendo."
});
command_set_handler("!help", "Prints this help message",
sub {
return "Commands: " .
join(",\n", map("$_ : $COMMANDS{$_}",
keys %COMMANDS));
});
command_set_handler("!slap", "!slap <user> slaps a ho",
sub{
my ($server, $chan, $nick, @args) = @_;
return "$args[0] got slapped around a bit with a large trout.";
});
command_set_handler("!version", "!version prints tr0l's version number.",
sub{return $VERSION;});
# split out command suites where reasonable
require "tr0l/karma.pl";
require "tr0l/alias.pl";
}
1;