3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-11 04:29:23 +01:00
PyLink/plugins/exec.py

92 lines
2.7 KiB
Python
Raw Normal View History

"""
exec.py: Provides commands for executing raw code and debugging PyLink.
"""
from pylinkirc import utils, world
from pylinkirc.log import log
2016-12-10 06:42:18 +01:00
from pylinkirc.coremods import permissions
# These imports are not strictly necessary, but make the following modules
# easier to access through eval and exec.
import threading
import re
import time
import pylinkirc
import importlib
def _exec(irc, source, args):
"""<code>
Admin-only. Executes <code> in the current PyLink instance. This command performs backslash escaping of characters, so things like \\n and \\ will work.
2016-12-10 06:42:18 +01:00
\x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02"""
2016-12-10 06:42:18 +01:00
permissions.checkPermissions(irc, source, ['exec.exec'])
# Allow using \n in the code, while escaping backslashes correctly otherwise.
args = bytes(' '.join(args), 'utf-8').decode("unicode_escape")
if not args.strip():
irc.reply('No code entered!')
return
log.info('(%s) Executing %r for %s', irc.name, args,
irc.getHostmask(source))
exec(args, globals(), locals())
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.
2016-12-10 06:42:18 +01:00
2015-09-19 19:39:05 +02:00
\x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02"""
2016-12-10 06:42:18 +01:00
permissions.checkPermissions(irc, source, ['exec.eval'])
2015-09-19 19:39:05 +02:00
args = ' '.join(args)
if not args.strip():
irc.reply('No code entered!')
2015-09-19 19:39:05 +02:00
return
log.info('(%s) Evaluating %r for %s', irc.name, args,
irc.getHostmask(source))
irc.reply(repr(eval(args)))
2015-09-19 19:39:05 +02:00
utils.add_cmd(_eval, 'eval')
@utils.add_cmd
def raw(irc, source, args):
"""<text>
Admin-only. Sends raw text to the uplink IRC server.
2016-12-10 06:42:56 +01:00
\x02**WARNING: THIS CAN BREAK YOUR NETWORK IF USED IMPROPERLY!**\x02"""
2016-12-10 06:42:18 +01:00
permissions.checkPermissions(irc, source, ['exec.raw'])
args = ' '.join(args)
if not args.strip():
irc.reply('No text entered!')
return
log.debug('(%s) Sending raw text %r to IRC for %s', irc.name, args,
irc.getHostmask(source))
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.
2016-12-10 06:42:56 +01:00
\x02**WARNING: THIS CAN BREAK YOUR NETWORK IF USED IMPROPERLY!**\x02"""
2016-12-10 06:42:18 +01:00
permissions.checkPermissions(irc, source, ['exec.inject'])
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,
args, irc.getHostmask(source))
irc.reply(irc.runline(args))