X-Chat 2 Shell Plugin

I wrote a X-Chat 2 Python Plugin today that allows you to run commands in a shell.

You can split a command you want to run into multiple lines by ending your input with a \ — just like you would do in a terminal. Newlines can be done using \n though this is currently the only workingescape sequence. Your input is passed to bash in a thread so that X-Chat doesn't freeze. I even got multiple ncat instances connected to a `ncat --chat` inside different channels. See for yourself, here's the sourcecode. If you got questions feel free to ask.

Edit: Fixed the case when there was an empty line the output stops, now it just doesn't print empty lines.

# coding=UTF-8

__module_name__ = "Shell Plugin"
__module_version__ = "1.0"
__module_description__ = "runs everything after ? in bash"

import xchat
import subprocess
import threading

shell_queue = ''

class ShellThread(threading.Thread):
    def __init__(self, shell_command, ctx, tochan):
        self.shell_command = shell_command
        self.tochan = tochan
        self.ctx = ctx
        threading.Thread.__init__(self)
    def run(self):
        self.proc = subprocess.Popen(self.shell_command, shell=True, executable="/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        line = self.proc.stdout.readline()
        while self.proc.poll() == None or len(line) > 0:
            if len(line.strip()) == 0:
                line = self.proc.stdout.readline()
                continue
            if self.tochan == True:
                self.ctx.command('say ' + line.rstrip())
            else:
                self.ctx.prnt(line.rstrip())
            line = self.proc.stdout.readline()
        #xchat.prnt('thread died: ' + self.shell_command + ' retval: ' + str(self.proc.wait()))

def shell(word, word_eol, userdata):
    global shell_queue
    #xchat.prnt(repr(len(word_eol)) + repr(word_eol))
    if word_eol[0][0] == '?' and len(word_eol[0]) > 1:
        ctx = xchat.get_context()
        if word_eol[0][1] == '?':
            tochan = False
            shell = word_eol[0][2:].replace('\\n', '\n')
        else:
            tochan = True
            shell = word_eol[0][1:].replace('\\n', '\n')
        if shell[-1] == '\\':
            shell_queue += shell[:-1]
        else:
            shell_queue += shell
            ShellThread(shell_queue, ctx, tochan).start()
            shell_queue = ''
        return xchat.EAT_ALL
    return xchat.EAT_NONE

xchat.hook_command("", shell)
xchat.prnt('Shell Plugin Loaded')