]> jfr.im git - erebus.git/blob - modules/eval.py
eval - add !SYSTEM
[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 os
23 import sys
24
25
26 def module(name):
27 return lib.mod(name)
28
29 @lib.hook('eval', needchan=False, wantchan=True, glevel=lib.OWNER)
30 @lib.help("<python>", "eval")
31 @lib.argsGE(1)
32 def cmd_eval(bot, user, chan, realtarget, *args):
33 if chan is not None: replyto = chan
34 else: replyto = user
35
36 try: ret = eval(' '.join(args))
37 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
38 else: bot.msg(replyto, "Done: %r" % (ret,))
39
40
41 @lib.hook('exec', needchan=False, wantchan=True, glevel=lib.OWNER)
42 @lib.help("<python>", "exec")
43 @lib.argsGE(1)
44 def cmd_exec(bot, user, chan, realtarget, *args):
45 if chan is not None: replyto = chan
46 else: replyto = user
47
48 try: exec(' '.join(args))
49 except Exception: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
50 else: bot.msg(replyto, "Done.")
51
52 @lib.hook(needchan=False, wantchan=True, glevel=lib.OWNER)
53 @lib.help('<command line>', 'think os.system')
54 @lib.argsGE(1)
55 def 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
65 @lib.hook('exception', needchan=False, glevel=lib.OWNER)
66 @lib.help(None, "cause an exception")
67 def cmd_exception(*args, **kwargs):
68 raise Exception()
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")
72 def 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:]))