--// mpc.lua Spams Media Player Classic info to DC!
--// Shamelessly plagiarising astro's VLC script... ZeeClone presents the following fix for Media Player Classic.
--// Requires wget.exe to be in the scripts folder and media player classic web interface to be set to port 13579.

local spamfile = "C:\\controls.html"

dcpp:setListener("ownChatOut","mplayer",function(hub,message,ret)
   if string.find(message,"^/mplayer%s*") then
                os.execute ("\""..DC():GetAppPath().."\scripts\\wget.exe\" -t 1 -O  C:\\controls.html http://localhost:13579/controls.html")
		mpc(hub)
		os.remove ( "C:\\controls.html" )
      return 1
   end
end)



function mpc(hub)
   local fileContents = {}
   file = io.open(spamfile,"r")
   

	if file then
		for line in file:lines() do table.insert(fileContents,line) end
		file:close()
		if table.getn(fileContents) ~= 0 then
			
			fileLength = fileContents[33];
			filePos = fileContents[31];
			fileStatus = fileContents[27];
			--local fileLength = trim(fileContents[248])
			--local filePos = trim(fileContents[246])
			--local fileStatus = trim(fileContents[241])
			if fileStatus ~= "Status: n/a" then
				--local fileName = trim(fileContents[9])
				local fileName = fileContents[10];
							
				while string.find(fileName,"\\") ~= nil do
					fileName = string.sub(fileName, string.find(fileName,"\\")+1)
				end
				 				
				dotpos = string.find(fileName,"%.")-1
				fileName = string.sub(fileName, 0, dotpos)
				fileStatus = (string.sub(fileStatus,16,-6))
				filePos = string.sub(filePos,21,28)
				fileLength = string.sub(fileLength,11,18)
				

				hub:sendChat("Media Player Classic ("..fileStatus.."): "..fileName.." - "..filePos.."/"..fileLength)
			else
				DC():PrintDebug("You've not got anything open dummy")
			end	
		else
			DC():PrintDebug("Media Player Classic isn't running. Dumbass")
		end
		
		else	 	
			DC():PrintDebug("Cannot find controls.html")
		end	--end if file

end  --end function

function trim(s)
  -- from PiL2 20.4
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end

DC():PrintDebug("  ** Loaded mplayer.lua **")