-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.py
120 lines (98 loc) · 3.58 KB
/
command.py
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
"""Handles commands.
Do not use without Void's permission
"""
class Command:
"""A class representing a command.
This is not the command we receive from IRC
"""
GENERAL = 0
VOICED = 1
OPERATOR = 2
TRUSTED = 3
DEVELOPER = 4
def __init__(self, name, action, prefix='$', restriction=0, enabled=True, help=False):
"""Create a command object."""
self.name = name
self.action = action
self.prefix = prefix
self.restriction = restriction
self.enabled = True
self.help = help
def allowed(self, trust_level):
"""Determine if the supplied trust level is sufficient to run the command.
Also, don't run the command if it was disabled.
"""
return self.enabled and self.restriction <= trust_level
class CommandHandler:
"""A class that handles commands from IRC."""
commands = []
master_commands = []
def __init__(self, event, bot):
"""Create a command handler."""
self.event = event
self.sender = event.source
self.line = event.arguments[0]
self.bot = bot
def find_command(self):
"""Identify the command in the line.
Returns a command object if a command can be found, otherwise False.
"""
try:
prefix = self.line[:1]
word = self.line.split()[0][1:]
for command in (self.master_commands + self.commands):
if command.prefix == prefix and command.name == word:
return command
return False
except Exception:
return False # Empty strings
def perm_level(self):
"""Determine the permission level of the sender."""
if self.sender.host == self.bot.dev:
return Command.DEVELOPER
if self.sender.host in self.bot.trusted.get('trusted', []):
return Command.TRUSTED
if self.event.target[0] == '#':
channel = self.bot.channels[self.event.target]
if channel.is_oper(self.sender.nick) or self.event.target in self.bot.trusted.get('op', {}).get(self.sender.host, []):
return Command.OPERATOR
if channel.is_voiced(self.sender.nick):
return Command.VOICED
return Command.GENERAL
def run(self):
"""Run the command handler."""
command = self.find_command()
if command is not False and command.allowed(self.perm_level()):
command.action(self.bot, self.event)
@classmethod
def enable_command(cls, command_name):
"""Enable the supplied command.
Returns true if command was found, false otherwise.
"""
for command in cls.commands:
if command.name == command_name:
command.enabled = True
return True
return False
@classmethod
def disable_command(cls, command_name):
"""Disable the supplied command.
Returns true if command was found, false otherwise.
"""
for command in cls.commands:
if command.name == command_name:
command.enabled = False
return True
return False
@classmethod
def get_command(cls, name):
"""Find a command matching the :name: parameter."""
for command in (cls.master_commands + cls.commands):
if command.name == name:
return command
return False
@classmethod
def clear_commands(cls):
"""Clear all commands known by the command handler."""
cls.master_commands.clear()
cls.commands.clear()