__module_name__ = "WhatImListening"
__module_version__ = "1.0"
__module_description__ = "Displays annoying messages with music being played \
in MOC or AMAROK"

from subprocess import Popen,PIPE
import xchat

#available players: moc, amarok
player="moc"

#change it to your favorite channel
#canal = "#example"
canal = ""

#change it to 1 if you want to start writing messages
#just after loading the script (without /STARTWIL)
#default channel should be specified also
autostart=0

myhook = None
artista = ""
cancion = ""
oldartista = ""
oldcancion = ""

def parar_cb(word, word_eol, userdata):
    global myhook
    if myhook is not None:
        xchat.unhook(myhook)
        myhook = None
        print "Script stopped! Restart it with /STARTWIL"
   	xchat.hook_command("STARTWIL", comenzar_cb)
            
def timeout_cb(userdata):
    global oldartista, oldcancion
   
    if player == "amarok":
   	artista = Popen(["dcop", "amarok", "player", "artist"]\
             ,stdout=PIPE).communicate()[0].strip()
             
   	cancion = Popen(["dcop", "amarok", "player", "title"]\
             ,stdout=PIPE).communicate()[0].strip()
   	if (artista != oldartista or cancion != oldcancion) and \
   	artista != "" and cancion != "" and artista != "call failed" and \
	cancion != "call failed" and artista[:6] != "ERROR:" and \
	cancion[:6] != "ERROR:":
	   cnc = xchat.find_context(channel=canal)
	   if cnc:
		   #      cnc.command("me listens \"" + cancion + "\" by " + artista)
		   cnc.command("me listens \"" + cancion + "\" by " + artista)
		   oldartista = artista
		   oldcancion = cancion
   	return 1
   
    elif player == "moc":
	    for b in Popen(["mocp", "--info"],stdout=PIPE).stdout.readlines():
		    temp = b.split()
		    if temp[0] == "State:" and temp[1] == "STOP":
			    break
		    if temp[0] == "Artist:":
			    artista = b[8:].strip()
		    elif temp[0] == "SongTitle:":
			    cancion = b[11:].strip()
	    if temp[1] != "STOP" and (artista != oldartista or \
      			cancion != oldcancion) and artista != "" and cancion != "":
       			cnc = xchat.find_context(channel=canal)
       			if cnc:
				cnc.command("me listens \"" + cancion + "\" by " + artista)
				oldartista = artista
				oldcancion = cancion   
            return 1

    else:
	    print player + " not supported"

def comenzar_cb(word, word_eol, userdata): 
    global myhook, canal, player
    if myhook is None:
	if canal == "":
		print "Set a channel with /CHANNELWIL #channel"
	
	else:
		myhook = xchat.hook_timer(5000, timeout_cb)
		xchat.hook_command("STOPWIL", parar_cb)
		print "Script working for " + player + " in " + canal

def channel_cb(word, word_eol, userdata): 
    global myhook, canal, player,autostart
    if len(word) > 1:
	    canal = word[1]
	    print "Script configured for channel " + canal
    else:
	    print "Usage: /CHANNELWIL #channel"

myhook = None
xchat.hook_command("CHANNELWIL", channel_cb)

if autostart == 0:
	xchat.hook_command("STARTWIL", comenzar_cb)
	print "Script loaded. Start it with /STARTWIL"
else:
	myhook = xchat.hook_timer(5000, timeout_cb)
	xchat.hook_command("STOPWIL", parar_cb)
	print "Script started. Stop it with /STOPWIL or change channel with /CHANNELWIL #channel"
