Last active
August 29, 2015 14:10
-
-
Save DataKinds/4566624b9c450a9410fc to your computer and use it in GitHub Desktop.
Code Runner Cinch IRC Bot
This file contains hidden or 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
require 'cinch' | |
class CodeRunner | |
include Cinch::Plugin | |
match /(ruby|python|haskell|perl|cjam):.+/, use_prefix: false | |
def runCode(code, interpreter) | |
badKeywords = ["\`", "popen", "gets", "STDIN", "interact", "input", "system", "File", "file", "IO", "eval", "exec", "open", "write", "read", "Socket"] | |
malicious = false | |
badKeywords.each do |word| | |
if code.include? word then | |
malicious = true | |
end | |
end | |
if malicious then | |
output = "Hey, calm down there." | |
else | |
output = "" | |
thread = Thread.new {output = `echo #{code} | #{interpreter}`} | |
thread.join 10 | |
if output.length == 0 then | |
output = "Code either took longer than 10 seconds to run or produced no output." | |
elsif output.split(?\n).length > 5 | |
output = output.split(?\n)[0..5].push("...").join(?\n) | |
end | |
end | |
return output | |
end | |
def runRuby(code) | |
return runCode(code, "ruby") | |
end | |
def runPython(code) | |
return runCode(code, "python3") | |
end | |
def runHaskell(code) | |
return runCode(code, "runghc") | |
end | |
def runPerl(code) | |
return runCode(code, "perl") | |
end | |
def runCJam(code) | |
return runCode(code, "java -jar cjam-0.6.2.jar") | |
end | |
def sanitizeCode(code) | |
#badCharacters = '\'\\"|<>=!@#$%^&`*()-[]{}()/?,.:;' | |
#badCharacters.each_char do |char| | |
# code.gsub!(char, "\\#{char}") #add a slash before every bad char | |
#end | |
#return code.squeeze('\\') | |
return code.chars.map{|char| "\\#{char}"}.join #escape every char because screw you | |
end | |
def execute(m) | |
code = m.message | |
code = code.strip | |
codeArray = code.split ':' | |
language = codeArray[0] | |
codeArray.delete_at 0 | |
runnableCode = sanitizeCode(codeArray.join ':') | |
case language | |
when "ruby" | |
m.reply(runRuby(runnableCode)) | |
when "python" | |
m.reply(runPython(runnableCode)) | |
when "haskell" | |
m.reply(runHaskell(runnableCode)) | |
when "perl" | |
m.reply(runPerl(runnableCode)) | |
when "cjam" | |
m.reply(runCJam(runnableCode)) | |
end | |
end | |
end | |
bot = Cinch::Bot.new do | |
configure do |c| | |
c.server = "irc.freenode.org" | |
c.nick = "CrazyB0t" | |
c.channels = ARGV | |
puts "JOINING CHANNELS: #{ARGV}" | |
c.plugins.plugins = [CodeRunner] | |
end | |
end | |
bot.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment