import code
import sys

from util.nick import nickeq, nicklower

class Repl(code.InteractiveConsole):
	def __init__(self, circa, channel):
		code.InteractiveConsole.__init__(self, {"circa": circa})
		self.circa = circa
		self.channel = channel
		self.buf = ""
	
	def write(self, data):
		self.buf += data
	
	def flush(self):
		msg = self.buf.rstrip("\n")
		if len(msg) > 0:
			self.circa.say(self.channel, msg)
		self.buf = ""
	
	def run(self, code):
		sys.stdout = sys.interp = self
		self.push(code)
		sys.stdout = sys.__stdout__
		self.flush()

	def showtraceback(self):
		type, value, lasttb = sys.exc_info()
		self.circa.say(self.channel, "\x02\x034{0}\x03\x02: {1}".format( \
			type.__name__, value))

	def showsyntaxerror(self, filename):
		self.showtraceback()

class ReplModule:
	def __init__(self, circa):
		self.circa = circa
		self.repls = {}

	def onload(self):
		self.circa.add_listener("message", self.handle_repl)

	def onunload(self):
		self.circa.remove_listener("message", self.handle_repl)

	def handle_repl(self, fr, to, text):
		if text.startswith(">>> "):
			self.repl(nicklower(fr), nicklower(fr) if nickeq(to, self.circa.nick) \
					else nicklower(to), text[len(">>> "):])

	def repl(self, fr, to, command):
		if self.circa.is_admin(fr):
			if to not in self.repls:
				self.repls[to] = Repl(self.circa, to)
			self.repls[to].run(command)

module = ReplModule

@@ -855,132 +855,28 @@
 %7D%0A%0A%09
-def onload(self):%0A%09%09self.circa.add_listener(%22message%22, self.handle_repl)%0A%0A%09def onunload(self):%0A%09%09self.circa.remove_listener(
+%09self.events = %7B%0A%09%09%09
 %22mes
@@ -880,18 +880,19 @@
 message%22
-, 
+: %5B
 self.han
@@ -899,17 +899,21 @@
 dle_repl
-)
+%5D%0A%09%09%7D
 %0A%0A%09def h
@@ -941,16 +941,19 @@
 to, text
+, m
 ):%0A%09%09if 
@@ -1095,16 +1095,19 @@
 %3E%3E%3E %22):%5D
+, m
 )%0A%0A%09def 
@@ -1132,16 +1132,19 @@
  command
+, m
 ):%0A%09%09if 
@@ -1163,18 +1163,24 @@
 s_admin(
-fr
+m.prefix
 ):%0A%09%09%09if

fix event handlers