switch to a better python eval API
This commit is contained in:
parent
1231067012
commit
f8083ae7b4
1 changed files with 10 additions and 56 deletions
|
@ -1,72 +1,26 @@
|
||||||
#--depends-on commands
|
#--depends-on commands
|
||||||
|
|
||||||
import html, json, socket
|
import urllib.parse
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils
|
||||||
|
|
||||||
EVAL_TEMPLATE = """
|
EVAL_URL = "http://dotpy3.herokuapp.com/"
|
||||||
import io, json, sys
|
|
||||||
|
|
||||||
old_stdout = sys.stdout
|
|
||||||
def fail(s):
|
|
||||||
old_stdout.write(json.dumps({"success": False, "out": str(e)}))
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
try:
|
|
||||||
compiled = compile(sys.stdin.read(), "code", "single")
|
|
||||||
except SyntaxError as e:
|
|
||||||
fail(str(e))
|
|
||||||
|
|
||||||
stdout = io.StringIO()
|
|
||||||
sys.stdout = stdout
|
|
||||||
|
|
||||||
try:
|
|
||||||
result = eval(compiled)
|
|
||||||
except Exception as e:
|
|
||||||
fail(str(e))
|
|
||||||
|
|
||||||
stdout.write("\\n")
|
|
||||||
if not result == None:
|
|
||||||
stdout.write(str(result)+"\\n")
|
|
||||||
old_stdout.write(json.dumps({"success": True, "out": stdout.getvalue()}))
|
|
||||||
"""
|
|
||||||
|
|
||||||
EVAL_URL = "https://tpcg.tutorialspoint.com/tpcg.php"
|
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
_name = "Python"
|
_name = "Python"
|
||||||
|
|
||||||
def _eval(self, lang, event):
|
@utils.hook("received.command.py", alias_of="python")
|
||||||
|
@utils.hook("received.command.python")
|
||||||
|
def _eval(self, event):
|
||||||
|
url = "%s?%s" % (EVAL_URL, urllib.parse.quote(event["args"]))
|
||||||
|
|
||||||
page = None
|
page = None
|
||||||
try:
|
try:
|
||||||
page = utils.http.request(EVAL_URL,
|
page = utils.http.request(url)
|
||||||
post_data={
|
|
||||||
"lang": lang,
|
|
||||||
"code": EVAL_TEMPLATE,
|
|
||||||
"execute": "%s main.py" % lang,
|
|
||||||
"mainfile": "main.py",
|
|
||||||
"stdinput": event["args"]
|
|
||||||
},
|
|
||||||
method="POST")
|
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if page and page.data:
|
if page and page.data:
|
||||||
out = page.data.split("</b></span><br>", 1)[1]
|
event["stdout"].write("%s: %s" % (event["user"].nickname,
|
||||||
out = html.unescape(out)
|
page.data.rstrip("\n")))
|
||||||
out = json.loads(out)
|
|
||||||
|
|
||||||
event["stdout" if out["success"] else "stderr"].write(
|
|
||||||
"%s: %s" % (event["user"].nickname, out["out"].strip("\n")))
|
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("%s: failed to eval" % event["user"].nickname)
|
event["stderr"].write("%s: failed to eval" % event["user"].nickname)
|
||||||
|
|
||||||
@utils.hook("received.command.py2", alias_of="python2")
|
|
||||||
@utils.hook("received.command.python2", min_args=1)
|
|
||||||
def eval(self, event):
|
|
||||||
self._eval("python", event)
|
|
||||||
|
|
||||||
@utils.hook("received.command.py", alias_of="python")
|
|
||||||
@utils.hook("received.command.py3", alias_of="python")
|
|
||||||
@utils.hook("received.command.python", alias_of="python3")
|
|
||||||
@utils.hook("received.command.python3", min_args=1)
|
|
||||||
def eval3(self, event):
|
|
||||||
self._eval("python3", event)
|
|
||||||
|
|
Loading…
Reference in a new issue