aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluk3yx <luk3yx@users.noreply.github.com>2023-10-20 22:54:34 +1300
committerluk3yx <luk3yx@users.noreply.github.com>2023-10-20 22:54:34 +1300
commit667e58e09e59cbe87afc84c7085792d59040073d (patch)
treeae38b9d52f29b48b4d269d75c2938cc01cd9cfcb
parentFix comments (diff)
downloadlurklite-commands-main.tar.gz
lurklite-commands-main.zip
Add piston.pyHEADmain
-rw-r--r--piston.py117
1 files changed, 117 insertions, 0 deletions
diff --git a/piston.py b/piston.py
new file mode 100644
index 0000000..3b8504d
--- /dev/null
+++ b/piston.py
@@ -0,0 +1,117 @@
+#
+# Piston commands
+#
+# Copyright © 2023 by luk3yx
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+
+import requests
+
+PISTON_URL = 'https://emkc.org/api/v2/piston/execute'
+
+
+@register_command('sh', 'bash')
+def sh(irc, hostmask, is_admin, args):
+ res = requests.post(PISTON_URL, json={
+ 'language': 'bash',
+ 'version': '*',
+ 'files': [{'name': 'main.sh', 'content': args[1]}]
+ })
+ output = res.json()['run']['output'].rstrip('\n')
+ irc.msg(args[0], f'{hostmask[0]}: {output or "(no output)"}')
+
+
+LUA_CODE = r"""
+local f = assert(io.open("code.lua", "rb"))
+local code = f:read("*a")
+f:close()
+local f = load("return " .. code)
+if f then
+ print(f())
+else
+ dofile("code.lua")
+end
+"""
+
+
+@register_command('lua')
+def lua(irc, hostmask, is_admin, args):
+ res = requests.post(PISTON_URL, json={
+ 'language': 'lua',
+ 'version': '*',
+ 'files': [
+ {'name': 'init.lua', 'content': LUA_CODE},
+ {'name': 'code.lua', 'content': args[1]}
+ ]
+ })
+ output = res.json()['run']['output'].rstrip('\n')
+ irc.msg(args[0], f'{hostmask[0]}: {output or "(no output)"}')
+
+
+PYTHON_CODE = r"""
+import ast, builtins
+
+with open('code.py', 'r') as f:
+ code = f.read()
+
+
+# Automatically import libraries where possible
+class _BuiltinsWrapper(dict):
+ __slots__ = ()
+ def __missing__(self, key):
+ try:
+ return __import__(key)
+ except ImportError:
+ raise NameError(f'name {key!r} is not defined')
+
+
+env = {'__builtins__': _BuiltinsWrapper(builtins.__dict__), 'code': code}
+
+
+try:
+ ast.parse(code, mode='eval')
+except SyntaxError:
+ exec(code, env)
+else:
+ print(eval(code, env))
+"""
+
+
+@register_command('py', 'py3', 'python', 'python3')
+def py3(irc, hostmask, is_admin, args):
+ res = requests.post(PISTON_URL, json={
+ 'language': 'python',
+ 'version': '3',
+ 'files': [
+ {'name': 'main.py', 'content': PYTHON_CODE},
+ {'name': 'code.py', 'content': args[1]}
+ ]
+ })
+ run = res.json()['run']
+ output = run['output'].rstrip('\n')
+ if run['code'] == 1:
+ output = output.rsplit('\n', 1)[-1]
+ irc.msg(args[0], f'{hostmask[0]}: {output or "(no output)"}')
+
+
+@register_command('hs', 'haskell')
+def hs(irc, hostmask, is_admin, args):
+ res = requests.post(PISTON_URL, json={
+ 'language': 'haskell',
+ 'version': '*',
+ 'files': [{'name': 'main.hs', 'content': args[1]}]
+ })
+ output = res.json()['run']['output'].rstrip('\n')
+ irc.msg(args[0], f'{hostmask[0]}: {output or "(no output)"}')