#! /usr/bin/python

import asyncore
import logging
import subprocess
import sys

from irc import Bot
from listener import Listener

# FIXME figure out async subprocess
# FIXME use optparse and/or configreader
# FIXME reconncet on disconnect

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(message)s")

channel = '#adamcik-test'
nicks= set()
bot = Bot('localhost')
listener = Listener()

def listen_parser(line):
    if line.startswith('@'): # FIXME: ValueError on split
        target, message = line[1:].split(' ', 1)
    else: # FIXME allow for multiple channels
        target = channel
        message =  line

    # FIXME avoid sending msg on fail
    if line.startswith('@') and target not in nicks:
        target = channel
        message = line

    # FIXME parse commands from users?
    bot.irc.privmsg(target, message)

def join(prefix, command, args):
    bot.irc.join(channel)

listener.add(listen_parser)

# FIXME queue events to run after register?
bot.add('CONNECT', join)

try:
    asyncore.loop()
except KeyboardInterrupt:
    bot.irc.quit('Bye :)')
    sys.exit(0)

@@ -843,16 +843,808 @@
 ssage)%0A%0A
+def msg_parser(prefix, command, args):%0A    user = prefix.split('!')%5B0%5D%0A    target, message = args%0A%0A    p = subprocess.Popen('./test.sh',%0A        shell=True,%0A        bufsize=1024,%0A        stdin=subprocess.PIPE,%0A        stdout=subprocess.PIPE)%0A%0A    # FIXME this will block%0A    response = p.communicate(input=message.encode('utf-8'))%5B0%5D%0A%0A    if not response.strip():%0A        return%0A%0A    try:%0A        response = response.decode('utf-8')%0A    except UnicodeDecodeError:%0A        response = response.decode('iso-8859-1')%0A%0A    if target == bot.current_nick:%0A        for line in response.split('%5Cn'):%0A            if line.strip():%0A                bot.irc.privmsg(user, line)%0A    else:%0A        for line in response.split('%5Cn'):%0A            if line.strip():%0A                bot.irc.privmsg(target, line)%0A%0A
 def join
@@ -1792,16 +1792,47 @@
 ', join)
+%0Abot.add('PRIVMSG', msg_parser)
 %0A%0Atry:%0A 

Add basic message parser using blocking Popen