-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
185 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const {Command} = require('../src/Yuuko') | ||
const superagent = require('superagent') | ||
|
||
const codeBlockRegExp = /^[\s\n]*```((javascript|js|ruby|rb|python|py)?\n)?([\S\s]*)\n?```[\s\n]*$/ | ||
const inlineCodeRegExp = /^[\s\n]*(``?)(.*)\1[\s\n]*$/ | ||
|
||
function urlPartForLang (lang) { | ||
switch (lang) { | ||
case 'javascript': | ||
case 'js': | ||
return 'https://hook.io/geo1088/js-eval?code=' | ||
case 'ruby': | ||
case 'rb': | ||
return 'https://hook.io/geo1088/ruby-eval?code=' | ||
case 'python': | ||
case 'py': | ||
return 'https://hook.io/geo1088/py-exec-then-eval?code=' | ||
} | ||
} | ||
|
||
module.exports = new Command([ | ||
'eval', | ||
'javascript', 'js', | ||
'ruby', 'rb', | ||
'python', 'py' | ||
], function (msg, args, prefix, commandName) { | ||
let code = args.join(' ') | ||
let codeBlockLang | ||
let match | ||
if ((match = code.match(codeBlockRegExp))) { | ||
code = match[3] | ||
codeBlockLang = match[2] | ||
} else if ((match = code.match(inlineCodeRegExp))) { | ||
code = match[2] | ||
} | ||
|
||
let url = urlPartForLang(commandName) | ||
if (!url) url = urlPartForLang(codeBlockLang) | ||
if (!url) { | ||
msg.channel.createMessage("Couldn't detect language.") | ||
return | ||
} | ||
url += encodeURIComponent(code) | ||
|
||
msg.channel.sendTyping() | ||
superagent.get(url).then(res => { | ||
msg.channel.createMessage(res.text).catch(console.log) | ||
}).catch(err => { | ||
msg.channel.createMessage('' + err).catch(console.log) | ||
}) | ||
}) | ||
module.exports.help = { | ||
desc: `Evaluates arbitrary code in a third-party sandbox. Supports multiple languages; to specify a language, use a language-specific alias or a code block tagged with the desired language. | ||
For example: | ||
\`\`\` | ||
eval \`\u200b\`\u200b\`ruby | ||
puts "hello" | ||
\`\u200b\`\u200b\` | ||
\`\`\` | ||
and | ||
\`\`\` | ||
rb puts "hello" | ||
\`\`\` | ||
are both valid and will evaluate as Ruby code. Supported languages are Ruby (\`ruby\`, \`rb\`), Python (\`python\`, \`py\`), and Javascript (\`javascript\`, \`js\`).` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const util = require('util') | ||
const inspectOptions = {depth: 1} | ||
|
||
module.exports = function (hook) { | ||
// Override console object for logging stuff | ||
console = { | ||
_lines: [], | ||
_logger (...things) { | ||
this._lines.push(...things.join(' ').split('\n')) | ||
}, | ||
_formatLines () { | ||
return this._lines.map(line => line && `//> ${line}\n`).join('') | ||
} | ||
} | ||
console.log = console.error = console.warn = console.info = console._logger | ||
|
||
// Get the eval result | ||
let result | ||
try { | ||
result = eval(hook.params.code || '') // eslint-disable-line no-eval | ||
} catch (e) { | ||
result = e | ||
} | ||
|
||
// Format the result | ||
const message = '```js\n' + console._formatLines() + util.inspect(result, inspectOptions) + '\n```' | ||
hook.res.end(message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import ast | ||
|
||
def comment_print(*things): | ||
print('#=>', *things) | ||
|
||
def exec_then_eval(code): | ||
try: | ||
block = ast.parse(code, mode='exec') | ||
except Exception as e: | ||
print(repr(e)) | ||
return | ||
|
||
try: | ||
last = ast.Expression(block.body.pop().value) | ||
except Exception as e: | ||
last = None | ||
|
||
_globals = {} | ||
_locals = {'print': comment_print} | ||
|
||
try: | ||
exec(compile(block, '<string>', mode='exec'), _globals, _locals) | ||
if last: | ||
print(repr(eval(compile(last, '<string>', mode='eval'), _globals, _locals))) | ||
else: | ||
print('<no returned value>') | ||
except Exception as e: | ||
print(repr(e)) | ||
|
||
try: | ||
code = Hook['params']['code'] | ||
except: | ||
code = '' | ||
|
||
print('```py') | ||
exec_then_eval(code) | ||
print('```') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
alias old_puts puts | ||
def puts(*things) | ||
things.each do |thing| | ||
old_puts "#=> #{thing}" | ||
end | ||
nil | ||
end | ||
|
||
old_puts '```rb' | ||
begin | ||
result = eval Hook['params']['code'] || '' | ||
old_puts result.inspect | ||
rescue Exception => e | ||
old_puts "#{e.class.name}: #{e.message}" | ||
end | ||
old_puts '```' |