]> jfr.im git - erebus.git/blame - modules/eval.py
eval - make !SYSTEM report stderr too
[erebus.git] / modules / eval.py
CommitLineData
9bd05cf6 1# Erebus IRC bot - Author: Erebus Team
4477123d 2# vim: fileencoding=utf-8
931c88a4 3# !EVAL and !EXEC commands
9bd05cf6 4# This file is released into the public domain; see http://unlicense.org/
931c88a4 5
e4255e70 6# module info
7modinfo = {
9bd05cf6 8 'author': 'Erebus Team',
e4255e70 9 'license': 'public domain',
fa93b933 10 'compatible': [0],
e4255e70 11 'depends': [],
a62d0d18 12 'softdeps': ['help'],
e4255e70 13}
14
db50981b 15# preamble
16import modlib
17lib = modlib.modlib(__name__)
18modstart = lib.modstart
19modstop = lib.modstop
20
e4255e70 21# module code
aad4b934 22import subprocess
db50981b 23import sys
24
aad4b934
JR
25# import these to make life easier using this module
26import os
27import ctlmod
132e8a0e 28
29def module(name):
43540339 30 return lib.mod(name)
132e8a0e 31
889eeb24 32@lib.hook('eval', needchan=False, wantchan=True, glevel=lib.OWNER)
5f03d045 33@lib.help("<python>", "eval")
d431e543 34@lib.argsGE(1)
861f83ef 35def cmd_eval(bot, user, chan, realtarget, *args):
f5aec865 36 if chan is not None: replyto = chan
839d2b35 37 else: replyto = user
38
db50981b 39 try: ret = eval(' '.join(args))
b79721ee 40 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
e5a24146 41 else: bot.msg(replyto, "Done: %r" % (ret,))
db50981b 42
43
889eeb24 44@lib.hook('exec', needchan=False, wantchan=True, glevel=lib.OWNER)
5f03d045 45@lib.help("<python>", "exec")
d431e543 46@lib.argsGE(1)
861f83ef 47def cmd_exec(bot, user, chan, realtarget, *args):
f5aec865 48 if chan is not None: replyto = chan
839d2b35 49 else: replyto = user
50
a28e2ae9 51 try: exec(' '.join(args))
b79721ee 52 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
839d2b35 53 else: bot.msg(replyto, "Done.")
f7628a8c 54
d522e2fd
JR
55@lib.hook(needchan=False, wantchan=True, glevel=lib.OWNER)
56@lib.help('<command line>', 'think os.system')
57@lib.argsGE(1)
58def system(bot, user, chan, realtarget, *args):
59 if chan is not None: replyto = chan
60 else: replyto = user
61
aad4b934
JR
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]))
d522e2fd 66 else:
aad4b934
JR
67 for line in proc.stdout:
68 bot.msg(replyto, line.decode('utf-8', 'surrogateescape'))
d522e2fd 69
ea3cbbdc 70@lib.hook('exception', needchan=False, glevel=lib.OWNER)
5f03d045 71@lib.help(None, "cause an exception")
f7628a8c 72def cmd_exception(*args, **kwargs):
73 raise Exception()
40ee6c8e
JR
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")
77def 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:]))