--script for nnm-club.org (28/08/2013) only for SimpleTV 0.4.7r4 and above
--id в PasswordManager - nnmclub
--debug_in_file( output_str , filename, setnew )

if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr = m_simpleTV.Control.CurrentAdress

if inAdr == nil then return end

if not string.match(inAdr, 'nnm%-club%.me/forum/viewtopic%.php') then return end

local res, login, password, head = xpcall(
	function()
		require('pm')
		return pm.GetPassword('nnmclub')
	end, err
)

if login == nil then login = '' end
if password == nil then password = '' end

if login == '' or password== '' then
	m_simpleTV.OSD.ShowMessage("nnm-club.me - Login and Password is required", 255, 10)
	return 
end

m_simpleTV.Control.ChangeAdress = 'Yes'
m_simpleTV.Control.CurrentAdress = ''

local session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
if session == nil then return end

--INTERNET_OPTION_CONNECT_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 2, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 6, 5000, 0)


local host, body, header, download_id
header = 
'Host: nnm-club.me\n' ..
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: ' .. inAdr .. '\n' ..
'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\n' ..
'Content-Type: application/x-www-form-urlencoded'

-- геттим по inAdr
local rc, answer
rc, answer = m_simpleTV.WinInet.Get(session, inAdr);

if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Connection error - " .. rc , 255, 10)
	return
end

-- ищем download id
download_id = string.match(answer, '<a href="download.php%?id=(.-)"')

-- если нет - логинимся
if download_id == nil then 
	host = 'http://nnm-club.me/forum/login.php'
	body = 'username=' .. login .. '&password=' .. password .. '&autologin=on&login=%C2%F5%EE%E4'

	rc, answer = m_simpleTV.WinInet.Post(session, host, nil, header, body)

	if rc ~= 200 then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Connection error - " .. rc .. ", or invalid login/password" , 255, 10)
		return
	end
end

-- и снова геттим по inAdr
rc, answer = m_simpleTV.WinInet.Get(session, inAdr);

if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Connection error - " .. rc , 255, 10)
	return
end

-- с нова ищем download id
download_id = string.match(answer, '<a href="download.php%?id=(.-)"')

if download_id == nil then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Downloadable torrent not found!" , 255, 10)
	return
end			

-- забираем по download id
host = 'http://nnm-club.me/forum/download.php?id=' .. download_id
body = ''
local tmpName = m_simpleTV.Common.GetTmpName()

rc, answer = m_simpleTV.WinInet.Post(session, host, nil, header, body, tmpName)

if rc~=200 then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Connection error - " .. rc , 255, 10)
	os.remove(tmpName)
	return
end
 
local retAdr = 'torrent://' .. tmpName

m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr
