Skip to content

Instantly share code, notes, and snippets.

@kylef
Last active September 25, 2015 01:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kylef/840763 to your computer and use it in GitHub Desktop.
Save kylef/840763 to your computer and use it in GitHub Desktop.
Python Evaluate for ZNC modpython
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.locals['network'] = self.GetNetwork()
self.indent = re.compile(r'^>+')
return True
def OnModCommand(self, line):
self.locals['client'] = self.GetClient()
# 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']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment