-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.py
90 lines (80 loc) · 2.29 KB
/
config.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
# -*- coding: utf-8 -*-
# vim:set ts=8 sts=8 sw=8 tw=80 noet cc=80:
from utils import startswith
from op import Operators
class Configuration(object):
def __init__(self, storage, config):
self.storage = storage
self.config = config
self.values = {}
self.handlers = {}
try:
self.values = self.storage['config']
except:
pass
for i in ['intrusive']:
if not i in self.values:
self.values[i] = False
self.op = Operators(storage, config.get("modules", "op"))
self.add_handler('op', self.op.config_handler)
self.add_handler(['show', 'op'], self.op.show_handler)
def add_handler(self, command, handler=None):
info = []
if type(command) == list:
info = command[1:]
command = command[0]
if not command in self.handlers:
self.handlers[command] = []
self.handlers[command] += [{'handler': handler, 'info': info}]
def __call__(self, msg, nick, send_message):
if msg in ['aus!', 'AUS!']:
self.values['intrusive'] = False
self.storage['config'] = self.values
send_message(':(')
return True
elif msg in ['an!', 'fass!']:
self.values['intrusive'] = True
self.storage['config'] = self.values
send_message(':)')
return True
else:
if not self.op.is_op(nick):
return False
return self.passive_config(msg, nick, send_message)
return False
def passive_config(self, msg, nick, send_message):
parts = [ x.strip() for x in msg.strip().split(' ') if
x.strip() != '' ]
if len(parts) == 0:
return False
command = parts[0]
undo = False
data = parts[1:]
args = {'send_message': send_message, 'cmdline': msg.strip(),
'nick': nick }
if command == 'no':
undo = True
command = parts[1]
data = parts[2:]
if not command in self.handlers:
return False
if undo and command == 'show': # "no show" is invalid
return False
if command != 'show':
data = [ undo ] + data
for handler in self.handlers[command]:
if startswith(data, handler['info']):
data = data[len(handler['info']):]
if handler['handler'] is None:
continue
try:
handler['handler'](*data, **args)
return True
except:
pass
def __getattr__(self, name):
return self.values[name]
def get(self, section, name):
return self.config.get(section, name)
def getint(self, section, name):
return self.config.getint(section, name)