import socket
import time

HOST = "chaos.esper.net" # Server to connect to
HOME_CHANNEL = "#kg6jay" # The home channel for your bot
NICK = "kg_bot" # Your bots nick
PORT = 6667 # Port (it is normally 6667)
SYMBOL = "_" #symbol eg. if set to # commands will be #echo.
blank = ""
###########################
##plugin loading varibles##
###########################
food = True 
def ping(msg):
  s.send("PONG :"+ msg +"\r\n")
def joinchan(channel):
  s.send("PRIVMSG "+ CHANNEL +" :Joining "+ channel +"\r\n")
  s.send("JOIN "+ channel +"\r\n")
def partchan(channel):
  s.send("PRIVMSG "+ CHANNEL +" :Leaving "+ channel +"\r\n")
  s.send("PART "+ channel +"\r\n")
def hello(user):
  s.send("PRIVMSG "+ CHANNEL +" :G'day "+ user +"!\n")
def quitIRC():
  s.send("QUIT "+ CHANNEL +"\n")
def kickme(target, reason):
  s.send("KICK "+ CHANNEL +" "+ target +" "+ reason + "\n")
def fail():
  s.send("PRIVMSG "+ CHANNEL +" :This failed, we dont know why..\n")
  #s.send("PRIVMSG "+ CHANNEL +" :Either you do not have the permission to do that, or that is not a valid command.\n")
def fish(user):
  s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION slaps "+ user +" with a wet sloppy tuna fish.\x01\r\n")
  time.sleep(1)
  s.send("PRIVMSG "+ CHANNEL +" :take that bitch\n")
def sandwich(sender):
  if food == True:
     s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION is making "+ sender +" a sandwich\x01\r\n")
     time.sleep(10)
     s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION has finished making "+ sender +"'s sandwhich\x01\r\n")
     time.sleep(1)
     s.send("PRIVMSG "+ CHANNEL +" :Here you go "+ sender +"! I hope you enjoy it!\r\n")
  else:
     s.send("PRIVMSG "+ CHANNEL +" :Command not loaded\r\n")
def makeitem(nick, item):
  s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION is making "+ nick +" a "+ item +"\x01\r\n")
  time.sleep(10)
  s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION has finished making "+ nick +"'s "+ item +"\x01\r\n")
  time.sleep(1)
  s.send("PRIVMSG "+ CHANNEL +" :Here you go "+ nick +"! I hope you enjoy it!\r\n")
def bomb(user):
  s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION hurls a bomb in "+ user +"'s direction.\x01\r\n")
  time.sleep(0.5)
  s.send("PRIVMSG "+ CHANNEL +" :BOOM!\n")
  s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION bomb explodes.\x01\r\n")
  time.sleep(1.75)
  s.send("PRIVMSG "+ CHANNEL +" :and you thought you could take a bot with such awesomeness! pfft\n")
def cake(sender):
   if food == True: 
     s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION is making "+ sender +" a cake\x01\r\n")
     time.sleep(10)
     s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION has finished making "+ sender +"'s cake\x01\r\n")
     time.sleep(1)
     s.send("PRIVMSG "+ CHANNEL +" :Here you go "+ sender +"! I hope you enjoy it!\r\n")
   else:
     s.send("PRIVMSG "+ CHANNEL +" :Command not loaded\r\n")
def echo(message):
  s.send("PRIVMSG "+ CHANNEL +" :"+ message +"\r\n")
def meow(user):
  s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION mews at "+user+"\n")
def pepsi(user):
  if food == True:
     s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION dispenses a can of Pepsi for "+ user +"\x01\r\n")
  else:
     s.send("PRIVMSG "+ CHANNEL +" :Command not loaded\r\n")
def coke(user):
  if food == True:
     s.send("PRIVMSG "+ CHANNEL +" :\x01ACTION dispenses a can of Coke for "+ user +"\x01\r\n")
  else:
     s.send("PRIVMSG "+ CHANNEL +" :Command not loaded\r\n")
def load(plugin):
  if plugin =="food":
     global food
     food = True
     s.send("PRIVMSG "+ CHANNEL +" :LOADED the FOOD plugin\r\n")
  else:
      s.send("PRIVMSG "+ CHANNEL +" :UNKNOWN plugin\r\n")
     
def unload(plugin):
  if plugin =="food":
     global food
     food = False
     s.send("PRIVMSG "+ CHANNEL +" :UNLOADED the FOOD plugin\r\n")
  else:
     s.send("PRIVMSG "+ CHANNEL +" :UNKNOWN plugin\r\n")
s = socket.socket( )
s.connect((HOST, PORT))
s.send("USER "+ NICK +" "+ NICK +" "+ NICK +" :KGBot\n")
s.send("NICK "+ NICK +"\r\n")
s.send("JOIN "+ HOME_CHANNEL +"\r\n")
while 1:
  line = s.recv(2048)
  line = line.strip("\r\n")
  print line
  stoperror = line.split(" ")
  if ("PING :" in line):
        pingcmd = line.split(":", 1)
        pingmsg = pingcmd[1]
        ping(pingmsg)
  elif "PRIVMSG" in line:
      
      if len(line) < 30:
        print blank
      elif len(stoperror) < 4:
        print blank
      else:
        complete = line.split(":", 2)
        info = complete[1]
        msg = line.split(":", 2)[2] ##the thing that was said
        cmd = msg.split(" ")[0]
        CHANNEL = info.split(" ")[2] ##channel from which it was said
        user = line.split(":")[1].split("!")[0] ## the person that said the thing
        arg = msg.split(" ")
        
        if SYMBOL + "hello" ==cmd:
          hello(user)
          print "recieved hello"
        elif SYMBOL + "hey" ==cmd:
          hello(user)
          print "recieved hello"
        elif SYMBOL + "hi"==cmd:
          hello(user)
          print "recieved hello"
        elif "thanks"== cmd:
          s.send("PRIVMSG "+CHANNEL+" :You're welcome.\n")
        elif SYMBOL + "join"==cmd and len(arg) > 1:
          x = line.split(" ", 4)
          newchannel = x[4]
          joinchan(newchannel)
        elif SYMBOL + "leave"==cmd and len(arg) > 1:
          x = line.split(" ", 4)
          newchannel = x[4]
          partchan(newchannel)
        elif SYMBOL + "quit"==cmd:
          quitIRC()
        elif SYMBOL + "coke"==cmd and len(arg) > 1:
          x = line.split(" ")
          recvr = x[4]
          coke(recvr)
        elif SYMBOL + "sandwich"==cmd and len(arg) > 1:
          x = line.split(" ")
          recvr = x[4]
          sandwich(recvr)
        elif SYMBOL + "pepsi"==cmd and len(arg) > 1:
          x = line.split(" ")
          recvr = x[4]
          pepsi(recvr)
        elif SYMBOL + "fish"==cmd and len(arg) > 1:
          if "Caleb_M" == user:
            fish(user)
          else:
            x = line.split(" ")
            recvr = x[4]
            fish(recvr)
        elif SYMBOL + "bomb"==cmd and len(arg) > 1:
          x = line.split(" ")
          recvr = x[4]
          bomb(recvr)
        elif SYMBOL + "sandwich"==cmd:
          sandwich(user)
        elif SYMBOL + "cake"==cmd and len(arg) > 1:
          x = line.split(" ")
          recvr = x[4]
          cake(recvr)
        elif SYMBOL + "meow"==cmd and len(arg) > 1:
          x = line.split(" ")
          recvr = x[4]
          meow(recvr)
        elif SYMBOL + "echo"==cmd:
          x = msg.split(" ", 1)[1]
          echo(x)
        
        elif line.find(""+ SYMBOL +"load") != -1:
          plugin = msg.split(" ")[1]
          load(plugin)
       
        elif line.find(""+ SYMBOL +"unload") != -1:
          plugin = msg.split(" ")[1]
          unload(plugin)
       
        elif SYMBOL in cmd:
          fail()
