]> jfr.im git - erebus.git/blame - modules/eval.py
eval - add !SYSTEM
[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
d522e2fd 22import os
db50981b 23import sys
24
132e8a0e 25
26def module(name):
43540339 27 return lib.mod(name)
132e8a0e 28
889eeb24 29@lib.hook('eval', needchan=False, wantchan=True, glevel=lib.OWNER)
5f03d045 30@lib.help("<python>", "eval")
d431e543 31@lib.argsGE(1)
861f83ef 32def cmd_eval(bot, user, chan, realtarget, *args):
f5aec865 33 if chan is not None: replyto = chan
839d2b35 34 else: replyto = user
35
db50981b 36 try: ret = eval(' '.join(args))
b79721ee 37 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
e5a24146 38 else: bot.msg(replyto, "Done: %r" % (ret,))
db50981b 39
40
889eeb24 41@lib.hook('exec', needchan=False, wantchan=True, glevel=lib.OWNER)
5f03d045 42@lib.help("<python>", "exec")
d431e543 43@lib.argsGE(1)
861f83ef 44def cmd_exec(bot, user, chan, realtarget, *args):
f5aec865 45 if chan is not None: replyto = chan
839d2b35 46 else: replyto = user
47
a28e2ae9 48 try: exec(' '.join(args))
b79721ee 49 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
839d2b35 50 else: bot.msg(replyto, "Done.")
f7628a8c 51
d522e2fd
JR
52@lib.hook(needchan=False, wantchan=True, glevel=lib.OWNER)
53@lib.help('<command line>', 'think os.system')
54@lib.argsGE(1)
55def system(bot, user, chan, realtarget, *args):
56 if chan is not None: replyto = chan
57 else: replyto = user
58
59 try: prochandle = os.popen(' '.join(args))
60 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
61 else:
62 for line in prochandle:
63 bot.msg(replyto, line)
64
ea3cbbdc 65@lib.hook('exception', needchan=False, glevel=lib.OWNER)
5f03d045 66@lib.help(None, "cause an exception")
f7628a8c 67def cmd_exception(*args, **kwargs):
68 raise Exception()
40ee6c8e
JR
69
70@lib.hook(needchan=False, wantchan=True, glevel=lib.ADMIN)
71@lib.help("<nick> <message>", "inject a line as though it came from <nick>", "note that this injects lines, not commands", "ex: INJECT DimeCadmium !WHOAMI")
72def inject(bot, user, chan, realtarget, *args):
73 targetuser = bot.parent.user(args[0], create=False)
74 if targetuser is None:
75 bot.msg(user, "User is unknown.")
76 return
77 if targetuser.glevel > user.glevel:
78 bot.msg(user, "That user has a higher access level than you.")
79 return
80
81 if chan is not None:
82 bot.parsemsg(bot.parent.user(args[0], create=False), str(chan), ' '.join(args[1:]))
83 else:
84 bot.parsemsg(bot.parent.user(args[0], create=False), str(bot), ' '.join(args[1:]))