-
Notifications
You must be signed in to change notification settings - Fork 7
/
pyeval.py
62 lines (49 loc) · 1.69 KB
/
pyeval.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
import sys
import re
from code import InteractiveInterpreter
import znc
class pyeval(znc.Module, InteractiveInterpreter):
module_types = [znc.CModInfo.UserModule, znc.CModInfo.NetworkModule]
description = 'Evaluates python code'
def write(self, data):
for line in data.split('\n'):
if len(line):
self.PutModule(line)
def resetbuffer(self):
"""Reset the input buffer."""
self.buffer = []
def push(self, line):
self.buffer.append(line)
source = "\n".join(self.buffer)
more = self.runsource(source, self.filename)
if not more:
self.resetbuffer()
return more
def OnLoad(self, args, message):
if not self.GetUser().IsAdmin():
message.s = 'You must have admin privileges to load this module.'
return False
self.filename = "<console>"
self.resetbuffer()
self.locals['znc'] = znc
self.locals['module'] = self
self.locals['user'] = self.GetUser()
self.indent = re.compile(r'^>+')
return True
def OnModCommand(self, line):
self.locals['client'] = self.GetClient()
self.locals['network'] = self.GetNetwork()
# Hijack sys.stdout.write
stdout_write = sys.stdout.write
sys.stdout.write = self.write
m = self.indent.match(line)
if m:
self.push((' ' * len(m.group())) + line[len(m.group()):])
elif line == ' ' or line == '<':
self.push('')
else:
self.push(line)
# Revert sys.stdout.write
sys.stdout.write = stdout_write
del self.locals['client']
del self.locals['network']