Skip to content

Instantly share code, notes, and snippets.

@jtripper
Created March 13, 2013 21:02
Show Gist options
  • Save jtripper/5156171 to your computer and use it in GitHub Desktop.
Save jtripper/5156171 to your computer and use it in GitHub Desktop.
cmd plugin for https://github.com/jtRIPper/plugin-based-irc-bot, provides commands for running python code and bash commands.
# cmd plugin for https://github.com/jtRIPper/plugin-based-irc-bot, provides commands for running python code and bash commands.
from subprocess import Popen, PIPE, STDOUT
import sys
import os
import StringIO
class cmd:
def __init__(self, bot):
self.bot = bot
self.autorun = self.auto
self.allowed_functions = { 'sh':10, 'py':10, 'run':10, 'clear':10, 'help':1 }
self._sh = 0
self._py = 0
self.code = ""
def execute(self, command, to):
p = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
for line in output.split("\n"):
self.bot.msg(to, line)
def sh(self, buffer):
self.bot.msg(buffer.to, "Accepting bash commands.")
self._sh = 1
def py(self, buffer):
self.bot.msg(buffer.to, "Accepting python code.")
self._py = 1
def run(self, buffer):
self.bot.msg(buffer.to, "Executing input..")
if self._sh:
f = open(".tmp_cmds", "w")
f.write(self.code)
f.close()
self.execute("sh .tmp_cmds", buffer.to)
os.remove(".tmp_cmds")
self.code = ""
self._sh = 0
if self._py:
out = sys.stdout
stdout = StringIO.StringIO()
sys.stdout = stdout
try:
exec(self.code)
except:
pass
sys.stdout = out
self.bot.msg(buffer.to, stdout.getvalue())
self.code = ""
self._py = 0
self.bot.msg(buffer.to, "Execution completed.")
def clear(self, buffer):
self.bot.msg(buffer.to, "Clearing input..")
self._py = 0
self._sh = 0
self.code = ""
def auto(self, auth_level, buffer):
if auth_level != 10:
return
if buffer.msg.startswith("!"):
return
if self._sh or self._py:
self.code += buffer.msg + "\n"
def help(self, buffer):
self.bot.msg(buffer.to, "Usage (all level 10, because lol):")
self.bot.msg(buffer.to, " * !cmd.sh -- start accepting bash commands")
self.bot.msg(buffer.to, " * !cmd.py -- accept python code as input")
self.bot.msg(buffer.to, " * !cmd.run -- run input")
self.bot.msg(buffer.to, " * !cmd.clear -- clear input")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment