aboutsummaryrefslogtreecommitdiff
path: root/Plugin/Admin.pm
blob: 437cf07721fafb050f98f3ec66df4be49295b3d6 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package Plugin::Admin;

use strict;
use warnings;

my %config;

sub configure {
	my $self = $_[0];
	my $cmdref = $_[1];
	my $cref = $_[2];
	%config = %$cref;

	$cmdref->("say", sub { $self->say(@_); } );
	$cmdref->("action", sub { $self->action(@_); } );

	$cmdref->("nick", sub { $self->nick(@_); } );
	$cmdref->("join", sub { $self->join_channel(@_); } );
	$cmdref->("part", sub { $self->part(@_); } );
	$cmdref->("mode", sub { $self->mode(@_); } );
	$cmdref->("kick", sub { $self->kick(@_); } );
	$cmdref->("topic", sub { $self->topic(@_); } );
	$cmdref->("reconnect", sub { $self->reconnect(@_); } );

	$cmdref->("ignore", sub { $self->ignore(@_); } );
	$cmdref->("don't ignore", sub { $self->do_not_ignore(@_); } );
	$cmdref->("who are you ignoring?", sub { $self->dump_ignore(@_); } );

	return $self;
}

sub is_admin {
	my $who = shift;
	my $is_admin = grep {$_ eq $who} @{$config{admins}};
	if (!$is_admin) {
		# Uhh log this rather than print
		print "$who isn't an admin, but tried to use a command";
	}
	return $is_admin;
}

sub nick {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: nick <new nick>" unless @arguments == 1;

	$irc->yield(nick => $arguments[0]);
}

sub say {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: say <channel> <msg>" unless @arguments >= 2;

	# Strip nick/channel from message
	$rest =~ s/^(.*?\s)//;

	$irc->yield(privmsg => $arguments[0] => $rest);
}

sub action {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: action <channel> <action text>" unless @arguments >= 2;

	# Strip nick/channel from message
	$rest =~ s/^(.*?\s)//;

	$irc->yield(ctcp => $arguments[0] => "ACTION $rest");
}

sub join_channel {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: join <channel1> [channel2 ...]" unless @arguments >= 1;

	$irc->yield(join => $_) for @arguments;
}

sub part {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: part <channel1> [channel2 ...] [partmsg]" unless @arguments >= 1;

	my $nick = (split /!/, $who)[0];
	my ($chan_str, $reason) = split /\s+(?!#)/, $rest, 2;
	my @channels = split /\s+/, $chan_str;
	$reason = "Commanded by $nick" unless $reason;
	$irc->yield(part => @channels => $reason);
}

sub mode {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: mode <everything>" unless @arguments > 0;

	# FIXME should use $where if it's a channel (?)
	$irc->yield(mode => $rest);
}

sub kick {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: kick <channel> <nick> [reason]" unless @arguments >= 2;

	# FIXME should use $where if it's a channel (?)
	my ($channel, $kickee, undef, $reason) = $rest =~ /^(\S+)\s(\S+)((?:\s)(.*))?$/;
	if ($channel and $kickee) {
		my $nick = (split /!/, $who)[0];
		$reason = "Requested by $nick" unless $reason;
		$irc->yield(kick => $channel => $kickee => $reason);
	}
}

sub topic {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: topic <new topic>" unless @arguments >= 2;

	# Strip nick/channel from message
	$rest =~ s/^(.*?\s)//;

	# FIXME use $where if it's a channel
	$irc->yield(topic => $arguments[0] => $rest);
}

sub reconnect {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);

	my $reason = $rest;
	$irc->yield(quit => $reason);
}

sub ignore {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: ignore <nick>" unless @arguments == 1;

	push @{$config{ignore}}, $arguments[0];

	return "Ignoring $arguments[0]";
}

sub do_not_ignore {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return unless is_admin($who);
	return "Syntax: don't ignore <nick>" unless @arguments == 1;

	my $target = $arguments[0];

	if (grep { $_ eq $target} @{$config{ignore}}) {
		@{$config{ignore}} = grep { $_ ne $target } @{$config{ignore}};
		return "No longer ignoring $target.";
	} else {
		return "I wasn't ignoring $target anyway.";
	}
}

sub dump_ignore {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	return "Syntax: who are you ignoring?" unless @arguments == 0;

	# FIXME special case for empty ignore
	return "I am ignoring: " . join ", ", @{$config{ignore}};
}


1;