2015-12-22 19:47:02 +01:00
|
|
|
"""
|
|
|
|
exec.py: Provides commands for executing raw code and debugging PyLink.
|
|
|
|
"""
|
|
|
|
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc import utils, world
|
|
|
|
from pylinkirc.log import log
|
2015-08-26 05:46:47 +02:00
|
|
|
|
2015-12-29 20:03:45 +01:00
|
|
|
# These imports are not strictly necessary, but make the following modules
|
|
|
|
# easier to access through eval and exec.
|
|
|
|
import threading
|
|
|
|
import re
|
|
|
|
import time
|
2016-07-24 20:51:44 +02:00
|
|
|
import pylinkirc
|
|
|
|
import importlib
|
2015-09-27 21:15:10 +02:00
|
|
|
|
2015-08-26 05:46:47 +02:00
|
|
|
def _exec(irc, source, args):
|
|
|
|
"""<code>
|
|
|
|
|
2015-12-22 19:41:42 +01:00
|
|
|
Admin-only. Executes <code> in the current PyLink instance. This command performs backslash escaping of characters, so things like \\n and \\ will work.
|
2015-08-26 05:46:47 +02:00
|
|
|
\x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02"""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2015-12-22 19:41:42 +01:00
|
|
|
|
|
|
|
# Allow using \n in the code, while escaping backslashes correctly otherwise.
|
|
|
|
args = bytes(' '.join(args), 'utf-8').decode("unicode_escape")
|
2015-08-26 05:46:47 +02:00
|
|
|
if not args.strip():
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('No code entered!')
|
2015-08-26 05:46:47 +02:00
|
|
|
return
|
2015-12-22 19:41:42 +01:00
|
|
|
|
|
|
|
log.info('(%s) Executing %r for %s', irc.name, args,
|
2016-05-01 01:44:37 +02:00
|
|
|
irc.getHostmask(source))
|
2015-08-26 05:46:47 +02:00
|
|
|
exec(args, globals(), locals())
|
2015-12-22 19:41:42 +01:00
|
|
|
|
2015-08-26 05:46:47 +02:00
|
|
|
utils.add_cmd(_exec, 'exec')
|
2015-09-19 19:39:05 +02:00
|
|
|
|
|
|
|
def _eval(irc, source, args):
|
|
|
|
"""<Python expression>
|
|
|
|
|
|
|
|
Admin-only. Evaluates the given Python expression and returns the result.
|
|
|
|
\x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02"""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2015-12-22 19:47:02 +01:00
|
|
|
|
2015-09-19 19:39:05 +02:00
|
|
|
args = ' '.join(args)
|
|
|
|
if not args.strip():
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('No code entered!')
|
2015-09-19 19:39:05 +02:00
|
|
|
return
|
2015-12-22 19:47:02 +01:00
|
|
|
|
|
|
|
log.info('(%s) Evaluating %r for %s', irc.name, args,
|
2016-05-01 01:44:37 +02:00
|
|
|
irc.getHostmask(source))
|
2016-08-09 19:23:46 +02:00
|
|
|
irc.reply(repr(eval(args)))
|
2015-09-19 19:39:05 +02:00
|
|
|
utils.add_cmd(_eval, 'eval')
|
2015-12-22 19:47:02 +01:00
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def raw(irc, source, args):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Admin-only. Sends raw text to the uplink IRC server.
|
|
|
|
\x02**WARNING: THIS CAN BREAK YOUR NETWORK IF USED IMPROPERLY!**\x02"""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2015-12-22 19:47:02 +01:00
|
|
|
|
|
|
|
args = ' '.join(args)
|
|
|
|
if not args.strip():
|
|
|
|
irc.reply('No text entered!')
|
|
|
|
return
|
|
|
|
|
|
|
|
log.info('(%s) Sending raw text %r to IRC for %s', irc.name, args,
|
2016-05-01 01:44:37 +02:00
|
|
|
irc.getHostmask(source))
|
2015-12-22 19:47:02 +01:00
|
|
|
irc.send(args)
|
|
|
|
|
|
|
|
irc.reply("Done.")
|
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def inject(irc, source, args):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Admin-only. Injects raw text into the running PyLink protocol module, replying with the hook data returned.
|
|
|
|
\x02**WARNING: THIS CAN BREAK YOUR NETWORK IF USED IMPROPERLY!**\x02"""
|
2016-05-01 01:54:11 +02:00
|
|
|
irc.checkAuthenticated(source, allowOper=False)
|
2015-12-22 19:47:02 +01:00
|
|
|
|
|
|
|
args = ' '.join(args)
|
|
|
|
if not args.strip():
|
|
|
|
irc.reply('No text entered!')
|
|
|
|
return
|
|
|
|
|
|
|
|
log.info('(%s) Injecting raw text %r into protocol module for %s', irc.name,
|
2016-05-01 01:44:37 +02:00
|
|
|
args, irc.getHostmask(source))
|
2015-12-22 19:47:02 +01:00
|
|
|
irc.reply(irc.runline(args))
|