import irc.bot
import random
import time
import threading
import multiprocessing

global murders, lock
murders = []

def loop(bot):
    while True:
        time.sleep(60)
        c = bot.connection
        c.names(bot.channel)

def murder(channel, c, user, target):
    global murders
    murders.append(user)

    c.privmsg(user, "I'm really glad we have this moment alone.")
    time.sleep(5)
    c.privmsg(user, '[ ' + target.capitalize() + ' has walked uncomfortably close to you. He is standing too close. ]')
    time.sleep(5)
    c.privmsg(user, '[ You feel the cold fingers of ' + target.capitalize() + ' wrap around your throat. ]')
    time.sleep(5)    c.privmsg(user, '[ You you are being strangled by ' + target.capitalize() + '. ]')
    time.sleep(10)
    c.privmsg(user, '[ You can not breath. ]')
    time.sleep(1)
    c.privmsg(user, '[ ** GASP GASP!!! GARGLE!!! ** ]')
    time.sleep(5)
    c.privmsg(user, '[ You are dead. ]')
    time.sleep(5)
    c.notice(channel, user + " has been found dead.  It looks like murder by strangulation.")

    murders.remove(user)

class MurderBot(irc.bot.SingleServerIRCBot):
    def __init__(self, channel, nickname, server, port=6667):
        irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
        self.channel = channel

    def on_welcome(self, c, e):
        print "Bot connected"
        c.join(self.channel)

    def on_privmsg(self, c, e):
        print e.arguments[0]
        global user, murders
        if user in e.source:
            if user not in murders:
                t = threading.Thread(target=murder, args=(self.channel, c, user, e.target))
                t.start()
            elif "stop" in e.arguments[0].lower():
                c.privmsg(user, "No.  I'm not going to stop...until you are dead.")
            elif "no" in e.arguments[0].lower():
                c.privmsg(user, "Just let it happen.")
            elif "ah" in e.arguments[0].lower() or "arg" in e.arguments[0].lower():
                c.privmsg(user, "No one can hear you.  We are alone.")
    def on_pubmsg(self, c, e):
        print e.target
        print e.source
        print e.arguments

    def on_namreply(self, c, e):
        global users, user
        users = e.arguments[2].split()
        user = users[random.randint(0,len(users) - 1)]

        print "Murdering: " + user
        c.privmsg(user, 'Hello! Are you there?')

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description='BashBot')
    parser.add_argument('--host', default="localhost", help='host')
    parser.add_argument('--port', type=int, default=6667, help='port')
    parser.add_argument('channel', help='channel to join')
    parser.add_argument('nickname', help='bot nickname')
    args = parser.parse_args()
    if not args.channel.startswith("#"):
        args.channel = "#"+args.channel

    bot = MurderBot(args.channel, args.nickname, args.host, args.port)

    t = threading.Thread(target=loop, args=(bot,))
    t.start()

   bot.start()