]> jfr.im git - erebus.git/blobdiff - modules/eval.py
eval - make !SYSTEM report stderr too
[erebus.git] / modules / eval.py
index d2b82c83567e2005d5936e8d714b8139790b72d0..3930b6ddc30f9db1004e0ff983c9495d4800dbfc 100644 (file)
@@ -1,4 +1,5 @@
 # Erebus IRC bot - Author: Erebus Team
+# vim: fileencoding=utf-8
 # !EVAL and !EXEC commands
 # This file is released into the public domain; see http://unlicense.org/
 
@@ -6,7 +7,7 @@
 modinfo = {
        'author': 'Erebus Team',
        'license': 'public domain',
-       'compatible': [1,2],
+       'compatible': [0],
        'depends': [],
        'softdeps': ['help'],
 }
@@ -18,18 +19,21 @@ modstart = lib.modstart
 modstop = lib.modstop
 
 # module code
+import subprocess
 import sys
-import ctlmod
 
+# import these to make life easier using this module
+import os
+import ctlmod
 
 def module(name):
        return lib.mod(name)
 
-@lib.hook('eval', needchan=False, glevel=lib.MANAGER)
+@lib.hook('eval', needchan=False, wantchan=True, glevel=lib.OWNER)
 @lib.help("<python>", "eval")
 @lib.argsGE(1)
 def cmd_eval(bot, user, chan, realtarget, *args):
-       if chan is not None and realtarget == chan.name: replyto = chan
+       if chan is not None: replyto = chan
        else: replyto = user
 
        try: ret = eval(' '.join(args))
@@ -37,18 +41,49 @@ def cmd_eval(bot, user, chan, realtarget, *args):
        else: bot.msg(replyto, "Done: %r" % (ret,))
 
 
-@lib.hook('exec', needchan=False, glevel=lib.MANAGER)
+@lib.hook('exec', needchan=False, wantchan=True, glevel=lib.OWNER)
 @lib.help("<python>", "exec")
 @lib.argsGE(1)
 def cmd_exec(bot, user, chan, realtarget, *args):
-       if chan is not None and realtarget == chan.name: replyto = chan
+       if chan is not None: replyto = chan
        else: replyto = user
 
-       try: exec ' '.join(args)
+       try: exec(' '.join(args))
        except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
        else: bot.msg(replyto, "Done.")
 
+@lib.hook(needchan=False, wantchan=True, glevel=lib.OWNER)
+@lib.help('<command line>', 'think os.system')
+@lib.argsGE(1)
+def system(bot, user, chan, realtarget, *args):
+       if chan is not None: replyto = chan
+       else: replyto = user
+
+       try:
+               proc = subprocess.Popen(' '.join(args), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
+       except Exception:
+               bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
+       else:
+               for line in proc.stdout:
+                       bot.msg(replyto, line.decode('utf-8', 'surrogateescape'))
+
 @lib.hook('exception', needchan=False, glevel=lib.OWNER)
 @lib.help(None, "cause an exception")
 def cmd_exception(*args, **kwargs):
        raise Exception()
+
+@lib.hook(needchan=False, wantchan=True, glevel=lib.ADMIN)
+@lib.help("<nick> <message>", "inject a line as though it came from <nick>", "note that this injects lines, not commands", "ex: INJECT DimeCadmium !WHOAMI")
+def inject(bot, user, chan, realtarget, *args):
+       targetuser = bot.parent.user(args[0], create=False)
+       if targetuser is None:
+               bot.msg(user, "User is unknown.")
+               return
+       if targetuser.glevel > user.glevel:
+               bot.msg(user, "That user has a higher access level than you.")
+               return
+
+       if chan is not None:
+               bot.parsemsg(bot.parent.user(args[0], create=False), str(chan), ' '.join(args[1:]))
+       else:
+               bot.parsemsg(bot.parent.user(args[0], create=False), str(bot), ' '.join(args[1:]))