--// 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
			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])
								
				while string.find(fileName,"\\") ~= nil do
					fileName = string.sub(fileName, string.find(fileName,"\\") + 1)
				end
				
				fileName = string.sub(fileName, 1, -3)
				
				fileStatus = (string.sub(fileStatus,9))
				filePos = string.sub(filePos,15,-6)
				fileLength = string.sub(fileLength,5,-6)

				hub:sendChat("Media Player Classic ("..fileStatus.."): "..fileName.." - "..filePos.."/"..fileLength)
			else
				hub:addLine("You've not got anything open dummy")
			end	
		else
			hub:addLine("Media Player Classic isn't running. Dumbass")
		end
		
		else	 	
			hub:addLine("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 **")