]> jfr.im git - erebus.git/blob - modules/eval.py
eval - make !SYSTEM report stderr too
[erebus.git] / modules / eval.py
1 # Erebus IRC bot - Author: Erebus Team
2 # vim: fileencoding=utf-8
3 # !EVAL and !EXEC commands
4 # This file is released into the public domain; see http://unlicense.org/
5
6 # module info
7 modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
10 'compatible': [0],
11 'depends': [],
12 'softdeps': ['help'],
13 }
14
15 # preamble
16 import modlib
17 lib = modlib.modlib(__name__)
18 modstart = lib.modstart
19 modstop = lib.modstop
20
21 # module code
22 import subprocess
23 import sys
24
25 # import these to make life easier using this module
26 import os
27 import ctlmod
28
29 def module(name):
30 return lib.mod(name)
31
32 @lib.hook('eval', needchan=False, wantchan=True, glevel=lib.OWNER)
33 @lib.help("<python>", "eval")
34 @lib.argsGE(1)
35 def cmd_eval(bot, user, chan, realtarget, *args):
36 if chan is not None: replyto = chan
37 else: replyto = user
38
39 try: ret = eval(' '.join(args))
40 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
41 else: bot.msg(replyto, "Done: %r" % (ret,))
42
43
44 @lib.hook('exec', needchan=False, wantchan=True, glevel=lib.OWNER)
45 @lib.help("<python>", "exec")
46 @lib.argsGE(1)
47 def cmd_exec(bot, user, chan, realtarget, *args):
48 if chan is not None: replyto = chan
49 else: replyto = user
50
51 try: exec(' '.join(args))
52 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
53 else: bot.msg(replyto, "Done.")
54
55 @lib.hook(needchan=False, wantchan=True, glevel=lib.OWNER)
56 @lib.help('<command line>', 'think os.system')
57 @lib.argsGE(1)
58 def system(bot, user, chan, realtarget, *args):
59 if chan is not None: replyto = chan
60 else: replyto = user
61
62 try:
63 proc = subprocess.Popen(' '.join(args), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
64 except Exception:
65 bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
66 else:
67 for line in proc.stdout:
68 bot.msg(replyto, line.decode('utf-8', 'surrogateescape'))
69
70 @lib.hook('exception', needchan=False, glevel=lib.OWNER)
71 @lib.help(None, "cause an exception")
72 def cmd_exception(*args, **kwargs):
73 raise Exception()
74
75 @lib.hook(needchan=False, wantchan=True, glevel=lib.ADMIN)
76 @lib.help("<nick> <message>", "inject a line as though it came from <nick>", "note that this injects lines, not commands", "ex: INJECT DimeCadmium !WHOAMI")
77 def inject(bot, user, chan, realtarget, *args):
78 targetuser = bot.parent.user(args[0], create=False)
79 if targetuser is None:
80 bot.msg(user, "User is unknown.")
81 return
82 if targetuser.glevel > user.glevel:
83 bot.msg(user, "That user has a higher access level than you.")
84 return
85
86 if chan is not None:
87 bot.parsemsg(bot.parent.user(args[0], create=False), str(chan), ' '.join(args[1:]))
88 else:
89 bot.parsemsg(bot.parent.user(args[0], create=False), str(bot), ' '.join(args[1:]))