#! /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")

channels = ['#adamcik-test']
bot = Bot('localhost')
listener = Listener()

def listen_parser(line):
    if line.startswith('@'): # FIXME: ValueError on split
        target, message = line[1:].split(' ', 1)
    elif line.startswith('#') or line.startswith('&'):
        target, message = line.split(' ', 1)
    else:
        target = channels[0]
        message = line

    if bot.known_target(target):
        bot.irc.privmsg(target, message)

def privmsg_parser(prefix, command, args):
    user = prefix.split('!')[0]
    target, message = args

    p = subprocess.Popen('./test.sh',
        shell=True,
        bufsize=1024,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE)

    # FIXME this will block
    response = p.communicate(input=message.encode('utf-8'))[0]

    if not response.strip():
        return

    try:
        response = response.decode('utf-8')
    except UnicodeDecodeError:
        response = response.decode('iso-8859-1')

    if target == bot.current_nick:
        for line in response.split('\n'):
            if line.strip():
                bot.irc.privmsg(user, line)
    else:
        for line in response.split('\n'):
            if line.strip():
                bot.irc.privmsg(target, line)

def connect_join(prefix, command, args):
    for channel in channels:
        bot.irc.join(channel)

def invite_rejoin(prefix, command, args):
    if args[-1] in channels:
        bot.irc.join(args[-1])

listener.add(listen_parser)

bot.add('CONNECT', connect_join)
bot.add('INVITE', invite_rejoin)
bot.add('PRIVMSG', privmsg_parser)

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

@@ -428,54 +428,21 @@
 line
-.startswith('@'): # FIXME: ValueError on split
+%5B0%5D in '@#&':
 %0A   
@@ -444,32 +444,33 @@
 :%0A        target
+s
 , message = line
@@ -473,12 +473,8 @@
 line
-%5B1:%5D
 .spl
@@ -492,97 +492,37 @@
     
+ 
-elif line.startswith('#') or line.startswith('&'):%0A        target, message = line
+   targets = targets
 .split('
  ', 
@@ -513,29 +513,26 @@
 gets.split('
- ', 1
+,'
 )%0A    else:%0A
@@ -545,19 +545,21 @@
   target
+s
  = 
+%5B
 channels
@@ -565,33 +565,110 @@
 s%5B0%5D
+%5D
+%0A
 %0A    
-    messa
+for target in targets:%0A        if target.startswith('@'):%0A            tar
 ge
+t
  = 
-line%0A%0A
+target%5B1:%5D%0A%0A    
     
@@ -696,16 +696,20 @@
 arget):%0A
+    
         

Handle comma seperated targets from listener