This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
control.pm
86 lines (72 loc) · 1.69 KB
/
control.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
package BotCtl;
use common::sense;
use POE;
our %ctl_sessions;
sub init {
BotPlugin::add_core_ctl_command('auth', sub {
my ($client, $data, @args) = @_;
if (@args != 1) {
$client->put("error:syntax:This command needs exactly one argument.");
return 1;
}
if (@args == 1 && $args[0] eq $BotIrc::config->{control_pwd}) {
$client->put("ok");
$data->{level} = '!control';
}
# TODO (perhaps): user auth
});
}
sub on_connected {
my $id = $_[HEAP]{client}->ID;
$ctl_sessions{$id} = {
client => $_[HEAP]{client},
level => '!guest',
};
}
sub on_disconnected {
my $id = $_[HEAP]{client}->ID;
delete $ctl_sessions{$id};
}
sub on_input {
my ($heap, $input) = @_[HEAP, ARG0];
my $client = $heap->{client};
my $data = client_data($client);
my ($cmd, @args) = split(/:/, $input);
for (@args) {
s/%([0-9a-f]{2})/chr(hex($1))/eig;
}
$cmd = lc $cmd;
if (!BotPlugin::maybe_ctl_command($client, $data, $cmd, @args)) {
$client->put("error:invalid_command:The given command is not handled by any plugin.");
}
}
sub send {
my $client = shift;
my @args = @_;
for (@args) { s/%/%25/g; s/:/%3A/g; s/\015/%0D/g; s/\012/%0A/g; }
$client->put(join(':', @args));
}
sub client_data {
return $ctl_sessions{shift->ID};
}
sub is_guest { return $_[1]->{level} eq '!guest'; }
sub is_control { return $_[1]->{level} eq '!control'; }
sub get_user {
my $u = $_[1]->{level};
return undef if ($u =~ /^!/);
$u;
}
sub set_level {
$_[0]->{level} = $_[1];
}
sub require_control {
return 1 if &is_control;
$_[0]->put("error:needpriv:Insufficient privileges.");
return 0;
}
sub require_user {
return 1 if &is_control || &get_user;
$_[0]->put("error:needpriv:Insufficient privileges.");
return 0;
}
1;