-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.py
More file actions
198 lines (127 loc) · 6.29 KB
/
Commands.py
File metadata and controls
198 lines (127 loc) · 6.29 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!usr/bin/env/python
from threading import Thread
import traceback, sys
from Werewolf.Game import WerewolfException # :oo
owners = ["unaffiliated/incredible"]
class FakeServ:
"When testing offline. "
def print_data(self, *args):
print ", ".join(args)
def __getattr__(self, name):
# Badass
print name, ":",
return self.print_data
def damerau_levenshtein_distance(s1, s2):
d = {}
lenstr1 = len(s1)
lenstr2 = len(s2)
for i in xrange(-1,lenstr1+1):
d[(i,-1)] = i+1
for j in xrange(-1,lenstr2+1):
d[(-1,j)] = j+1
for i in xrange(lenstr1):
for j in xrange(lenstr2):
if s1[i] == s2[j]:
cost = 0
else:
cost = 1
d[(i,j)] = min(
d[(i-1,j)] + 1, # deletion
d[(i,j-1)] + 1, # insertion
d[(i-1,j-1)] + cost, # substitution
)
if i and j and s1[i]==s2[j-1] and s1[i-1] == s2[j]:
d[(i,j)] = min (d[(i,j)], d[i-2,j-2] + cost) # transposition
return d[lenstr1-1,lenstr2-1]
def search_object(list, keyword, verbose=True):
results = {}
keyword = keyword.lower()
for i in list:
results[damerau_levenshtein_distance(keyword, i)] = i
if sorted(results.keys())[0] > 5: #ugh
if verbose:
return "No good matches found for your keyword"
return None
return results[sorted(results.keys())[0]]
class CommandClass:
def __init__(self, channels, serv):
self.channels, self.serv = channels, serv
def _OutputMethod(self, target, text, *args):
if not text:
return
self.OutputMethod(target, text, *args)
def OutputMethod(self, target, text, *args):
self.serv.privmsg(target, args[0].split('!')[0]+': '+str(text))
def get_func(self, channel, author, *args):
cmdname = args[0].lower()
namespace = self.namespace
authorhostmask = author.split('!')[1].split('@')[1]
try:
target = getattr(self.namespace, cmdname)
except AttributeError, SyntaxError:
a = search_object(
self.find_callable_attrs(),
cmdname, verbose=False)
if not a:
return "Attribute not found. "
return "Error: Attribute not found, did you mean "+a+"?"
except WerewolfException as exception:
return str(exception)
if not hasattr(namespace, "admincmds"):
pass
elif ((cmdname in namespace.admincmds and not authorhostmask in namespace.adminlist and not authorhostmask in owners) or
cmdname in namespace.ownercmds and not authorhostmask in owners):
raise WerewolfException("You need a higher security level to use these commands.")
if type(target) == type(self.get_func) or type(target) == type(lambda: 0):
if not target.__doc__:
raise WerewolfException("You are disallowed to call this command. ")
target(*args[1:])
else:
return "OOPS"
def fhelp(self):
pass
def call_func(self, target, author, namespace, cmdtext):
try:
self.namespace = namespace
self.target, self.namespace.target = target, target
self.author, self.namespace.author = author, author
self.namespace.authorname = author
cmdtext = cmdtext.split(' ')
result = self.get_func(target, author, *cmdtext)
if not result: return
self._OutputMethod(target, result, author)
except:
message = str(sys.exc_info()[1])
author = author.split('!')[0]
if type(message) == type(1):
print("TARGET: "+target, " MESSAGE: "+str(message), " AUTHOR: "+author)
self._OutputMethod(target,
"Error: "+str(message), author)
return
traceback.print_exc()
return self._OutputMethod(target,
message, author)
def find_callable_attrs(self):
result = []
for obj in dir(self.namespace):
_type = type(getattr(self.namespace, obj))
if _type == type(self.find_callable_attrs):
result.append(obj)
return result
def commandlist(self, authorname):
"Will print a list of commands IN A QUERY."
result = ""
authorname = authorname.split('!')[0]
for i in dir(self.namespace):
x = getattr(self, i)
if sys.getsizeof(result+i.upper()) > 250:
self.serv.privmsg(authorname, result)
result = ""
if type(x) == type(self.commandlist) and x.__doc__:
result += x.func_name+', '
self.serv.privmsg(authorname, result[:-2])
def help(self, *command):
"Will hopefully find something useful"
data = getattr(self.namespace, command[0]).__doc__
return "Info for %s command: %s" % (command[0].upper(), data)
namespace = None