-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockBotBrain.py
461 lines (384 loc) · 16.3 KB
/
LockBotBrain.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import re
import os
import dumbdbm
import inspect
import Levenshtein
import Logger
DBNAME = 'locks'
def cleannick(nick):
return nick.rstrip('_')
class LockBotException(Exception):
def __init__(self, msg, resourcestr, verb):
super(LockBotException, self).__init__()
self.msg = msg
self.resourcestr = resourcestr
self.verb = verb
class Lock(object):
def __init__(self, db, name, lockstr=''):
self.db = db
self.name = name
self.lockstr = lockstr
owner, waiters = self.fromstr(lockstr)
self._owner = owner
self._waiters = waiters
@property
def owner(self):
return self._owner
@owner.setter
def owner(self, owner):
self._owner = owner
self.sync()
@property
def waiters(self):
return self._waiters
def wait(self, waiter):
if waiter not in self._waiters:
self._waiters.append(waiter)
self.sync()
def popwaiter(self, waiter=None):
if not self._waiters:
return None
if not waiter:
waiter = self._waiters.pop(0)
else:
self._waiters.remove(waiter)
self.sync()
return waiter
def fromstr(self, lockstr):
if not lockstr:
return '', []
flds = lockstr.split(',')
return flds[0], flds[1:]
def tostr(self):
return ','.join([self.owner] + self._waiters)
def sync(self):
self.db[self.name] = self.tostr()
class LockDB(object):
def __init__(self, path):
self.db = dumbdbm.open(path)
def add(self, name):
self[name] = Lock(self.db, name)
def keys(self):
return self.db.keys()
def items(self):
return [(k, Lock(self.db, k, v)) for k, v in self.db.items()]
def __iter__(self):
return self.db.__iter__()
def __len__(self):
return len(self.keys())
def __getitem__(self, name):
return Lock(self.db, name, self.db[name])
def __setitem__(self, name, lock):
self.db[name] = lock.tostr()
def __delitem__(self, name):
del self.db[name]
class LockBotBrain(object):
def __init__(self, nickname, dbdir):
if not os.path.isdir(dbdir):
os.mkdir(dbdir)
dbpath = os.path.join(dbdir, DBNAME)
self.locks = LockDB(dbpath)
self.nickname = nickname
self.rules = self.interpolateRules(nickname)
self.logger = Logger.Logger()
self.verb = None
def interpolateRules(self, nickname):
rules = self.getRules()
irules = []
for rule in rules:
irule = (rule[0].replace('@BOTNAME@', nickname),
rule[1])
irules.append(irule)
return irules
def getErrorMessages(self, nick, channel, exc):
if ',' in exc.resourcestr:
return [(channel, '%s: %s' % (nick, exc.msg)),
(channel, "%s: no resources %s" % (nick, exc.verb))]
else:
return (channel, exc.msg)
def processPrivMsg(self, user, channel, msg):
nick = user.strip().split('!')[0]
self.logger.debug("privmsg: user %s, nick %s, channel %s, msg %s" % \
(user, nick, channel, msg))
# Ignore my own messages
if nick == self.nickname:
return []
# Snub private messages
if channel == self.nickname:
# msg = "It isn't nice to whisper! Play nice with the group."
# return [(nick, msg)]
msg = '%s: %s' % (self.nickname, msg)
channel = nick
for regexp, handler in self.rules:
m = re.match(regexp, msg)
if m:
args = m.groups()
self.verb = handler.__name__ + 'ed'
try:
response = handler(cleannick(nick), channel, *args)
except LockBotException as exc:
response = self.getErrorMessages(nick, channel, exc)
self.verb = None
if type(response) != list:
response = [response]
return response
return []
def getRules(self):
rules = [
('^\s*(?:@BOTNAME@:)?\s*trylock\((.*)\)$', self.lock),
('^\s*(?:@BOTNAME@:)?\s*trylock\s+(.*)$', self.lock),
('^\s*(?:@BOTNAME@:)?\s*unlock\((.*)\)$', self.unlock),
('^\s*(?:@BOTNAME@:)?\s*unlock\s+(.*)$', self.unlock),
('@BOTNAME@:\s*assignlock\((.*?),(.*)\)$', self.assignlock),
('@BOTNAME@:\s*assignlock\s+(.*)\s+(.*)$', self.assignlock),
('@BOTNAME@:\s*freelock\((.*)\)$', self.freelock),
('@BOTNAME@:\s*freelock\s+(.*)$', self.freelock),
('^\s*(?:@BOTNAME@:)?\s*lock\((.*)\)$', self.waitlock),
('^\s*(?:@BOTNAME@:)?\s*lock\s+(.*)$', self.waitlock),
('@BOTNAME@:\s*register\((.*)\)$', self.register),
('@BOTNAME@:\s*register\s+(.*)$', self.register),
('@BOTNAME@:\s*unregister\((.*)\)$', self.unregister),
('@BOTNAME@:\s*unregister\s+(.*)$', self.unregister),
('@BOTNAME@:\s*status\s*$', self.status),
('@BOTNAME@:\s*listlocked\s*$', self.status),
('@BOTNAME@:\s*listfree\s*$', self.listfree),
('@BOTNAME@:\s*list\s*$', self.list),
('@BOTNAME@:\s*help\s*$', self.help),
('@BOTNAME@:.*', self.defaulthandler),
]
return rules
def getlock(self, name):
if name in self.locks:
return name
ratios = [(Levenshtein.ratio(name, l), l) for l in self.locks]
best = max(ratios)
if best[0] < 0.5 or len([c for c in ratios if c[0] == best[0]]) > 1:
return name
return best[1]
def splitResources(self, resourcestr):
results = [i.strip() for i in resourcestr.split(',')]
if '' in results:
raise LockBotException('ERROR: empty resource names are not allowed',
resourcestr, self.verb)
if len(results) != len(set(results)):
raise LockBotException('ERROR: duplicate resource name')
return results, len(results) > 1
def getlocks(self, resourcestr):
resources, multi = self.splitResources(resourcestr)
resources = [self.getlock(r) for r in resources]
for r in resources:
if r not in self.locks:
raise LockBotException('ERROR: unrecognized resource "%s"' % r,
resourcestr, self.verb)
return resources, multi
def _lock(self, caller, assignee, resourcestr, wait=False):
resources, multi = self.getlocks(resourcestr)
waiters = []
# iterate over all resources once to check for errors
for r in resources:
if self.locks[r].owner == assignee:
raise LockBotException("%s already hold the lock for resource %s" %
('you' if self.locks[r].owner == caller else assignee, r),
resourcestr, self.verb)
elif self.locks[r].owner and self.locks[r].owner != assignee:
if wait:
waiters.append(r)
else:
raise LockBotException("DENIED, %s is already locked by %s" %
(r, self.locks[r].owner), resourcestr, self.verb)
# all clear, perform lock
owned = []
for r in resources:
if r in waiters:
self.locks[r].wait(assignee)
else:
self.locks[r].owner = assignee
owned.append(r)
msg = ''
if owned:
if caller == assignee:
own = "you own"
else:
own = assignee + " owns"
msg = "%s: GRANTED, %s %s" % (caller, own, ', '.join(owned))
if waiters:
msg += " (still waiting for %s)" % ', '.join(waiters)
elif waiters:
wmsg = [self.lockstatus(w) for w in waiters]
msg = '%s: WAITING for %s' % (caller, ', '.join(wmsg))
return msg
def lockstatus(self, name):
lock = self.locks[name]
status = ''
if lock.owner:
status = 'owner: ' + lock.owner
if lock.waiters:
status += ' waiters: ' + ','.join(lock.waiters)
if status:
return '%s (%s)' % (name, status)
return self.name
def lock(self, nick, channel, resourcestr):
"""take hold of a lock on a resource"""
return (channel, self._lock(nick, nick, resourcestr))
def register(self, nick, channel, resourcestr):
"""add a new resource to the database"""
resources, multi = self.splitResources(resourcestr)
for r in resources:
if r in self.locks:
raise LockBotException('ERROR, resource "%s" is already registered' % r,
resourcestr, self.verb)
# all clear, register resources
for r in resources:
self.locks.add(r)
return (channel,
"%s: registered resource%s %s" %
(nick,
's' if multi else '',
', '.join(resources)))
def unregister(self, nick, channel, resourcestr):
"""remove a resource from the database"""
resources, multi = self.getlocks(resourcestr)
for r in resources:
if self.locks[r]:
raise LockBotException('ERROR, resource "%s" is locked by %s' %
(r, self.locks[r]),
resourcestr, self.verb)
# all clear, unregister resources
for r in resources:
del self.locks[r]
return (channel,
"%s: removed resource%s %s" %
(nick,
's' if multi else '',
', '.join(resources)))
def unlock(self, nick, channel, resourcestr):
"""release the resource lock"""
resources, multi = self.getlocks(resourcestr)
# iterate over all resources once to check for errors
for r in resources:
l = self.locks[r]
if not l.owner:
raise LockBotException("%s is already free" % r, resourcestr,
self.verb)
elif l.owner != nick and nick not in l.waiters:
raise LockBotException("DENIED, %s holds the lock on %s" %
(self.locks[r].owner, r),
resourcestr, self.verb)
# all clear, perform unlock
owned = [r for r in resources if self.locks[r].owner == nick]
waiters = [r for r in resources if nick in self.locks[r].waiters]
msgs = []
if owned:
multi = len(owned) > 1
msgs = ["%s: RELEASED, resource%s %s %s free" % (nick,
's' if multi else '',
', '.join(owned),
'are' if multi else 'is',
)]
for r in owned:
lock = self.locks[r]
lock.owner = ''
assignee = lock.popwaiter()
if assignee:
msgs += [self._lock(assignee, assignee, r)]
if waiters:
multi = len(owned) > 1
msgs = ["%s: GAVE UP, no longer waiting for resource%s %s" % (nick,
's' if multi else '',
', '.join(waiters)
)]
for r in waiters:
self.locks[r].popwaiter(nick)
return [(channel, msg) for msg in msgs]
def assignlock(self, nick, channel, assignee, resourcestr):
"""assign a resource lock to someone else other than the caller"""
return (channel, self._lock(nick, assignee, resourcestr))
def freelock(self, nick, channel, resourcestr):
"""release a resource lock even if the caller does not hold the lock (USE WITH CAUTION)"""
resources, multi = self.getlocks(resourcestr)
# iterate over all resources once to check for errors
for r in resources:
if not self.locks[r].owner:
raise LockBotException('ERROR: resource %s is already unlocked' % r,
resourcestr, self.verb)
# all clear, perform freelock
msgs = [(channel,
"%s: RELEASED, resource%s %s %s free" %
(nick,
's' if multi else '',
', '.join(resources),
'are' if multi else 'is',
))]
for r in resources:
l = self.locks[r]
lockowner = l.owner
l.owner = ''
msgs += [(channel,
"%s: your lock on %s has been released by %s" %
(lockowner, r, nick))]
assignee = l.popwaiter()
if assignee:
msgs += [(channel, self._lock(nick, assignee, r))]
return msgs
def waitlock(self, nick, channel, resourcestr):
"""try to take the lock, or get on queue if it is currently locked"""
return (channel, self._lock(nick, nick, resourcestr, wait=True))
def status(self, nick, channel):
"""list locked resources and their owners"""
lockeditems = sorted([item[0] for item in self.locks.items() if item[1].owner])
if len(lockeditems) == 0:
return (channel, "There are no locked resources")
else:
messages = []
messages += [(channel, "Status of locked resources:")]
for k in lockeditems:
l = self.locks[k]
msg = " resource: %s owner: %s" % (k, l.owner)
if l.waiters:
msg += " waiters: %s" % ','.join(l.waiters)
messages += [(channel, msg)]
return messages
def listfree(self, nick, channel):
"""list unlocked resources"""
freeitems = [item[0] for item in self.locks.items() if not item[1].owner]
if len(freeitems) == 0:
return (channel, "There are no unlocked resources")
else:
return (channel, "Unlocked resources: " + ', '.join(sorted(freeitems)))
def list(self, nick, channel):
"""list all registered resources"""
if len(self.locks.keys()) == 0:
return (channel, "There are no registered resources")
else:
return (channel, "List of registered resources: %s" %
', '.join(sorted(self.locks.keys())))
def help(self, nick, channel):
"""display this help message"""
def getCmdArguments(handler):
return inspect.getargspec(handler)[0][3:]
helpTuples = []
rules = self.getRules()
processedHandlers = set()
for _, handler in rules:
if handler in processedHandlers:
continue
processedHandlers.add(handler)
helpTuples.append((handler.__name__ + ' ' +
' '.join(["<%s>" % arg
for arg in getCmdArguments(handler)]),
handler.__doc__
))
if handler.__name__ == 'help':
break
padding = max([len(cmdpart) for cmdpart, _ in helpTuples])
messages = ["List of lockbot commands:"]
for cmdpart, description in helpTuples:
message = " %s:%s%s" % (cmdpart,
(padding - len(cmdpart) + 1) * ' ',
description if description else 'N/A')
messages.append(message)
return [(channel, message) for message in messages]
def defaulthandler(self, nick, channel):
messages = [(channel,
"%s: Unrecognized command, try '%s: help'" % (nick, self.nickname))]
return messages