Skip to content

Instantly share code, notes, and snippets.

@jazzl0ver
Created May 23, 2016 14:34
Show Gist options
  • Save jazzl0ver/ba619fefbe1a7b48db72c2db6365fb61 to your computer and use it in GitHub Desktop.
Save jazzl0ver/ba619fefbe1a7b48db72c2db6365fb61 to your computer and use it in GitHub Desktop.
Conversion script from FarFTP to NetBox for FAR 3.0.4545 x86 (offsite - http://forum.farmanager.com/viewtopic.php?p=108838#p108838)
local function hex1(hc)
if hc >= 0x30 and hc <= 0x39 then return hc-0x30 end -- 0..9
if hc >= 0x61 and hc <= 0x66 then return hc-0x61+10 end -- a..f
if hc >= 0x41 and hc <= 0x46 then return hc-0x41+10 end -- A..F
return 0;
end
local function hex2(hstr, pos)
return 16*hex1(string.byte(hstr,pos)) + hex1(string.byte(hstr,pos+1))
end
local function decrypt_ftp_password(hpwd)
local spwd, b1, b2, XorMask, v
if #hpwd >= 3*2 then
b1, b2, spwd = hex2(hpwd,1), hex2(hpwd,3), ''
if b1 ~= 0 and b2 ~= 0 then
XorMask = bor(bxor(b1, b2), 80)
for n=2*2+1, #hpwd, 2 do
v = bxor(hex2(hpwd,n), XorMask)
if v ~= 0 and v ~= XorMask then
spwd = spwd .. string.char(v)
else
break
end
end
end
end
return spwd
end
Macro {
area = "Shell"; key = "F5"; description = "Import Ftp session to Netbox"; flags="EnableOutput";
condition = function() return
APanel.Visible and APanel.FilePanel and not APanel.Folder and mf.fsplit(APanel.Current,8) == ".ftp"
and PPanel.Visible and PPanel.Plugin and PPanel.Format == "netbox" -- and PPanel.Root
end;
action = function()
local port,host,dir, user,pass, passive = 21, nil, nil, nil, nil, true
for line in io.lines(APanel.Path..'\\'..APanel.Current) do
local url = line:match('^Url=(.-)%s*$')
local usr = line:match('^User=(.-)%s*$')
local psw = line:match('^Password=hex:(.-)%s*$')
local psv = line:match('^PassiveMode=(.-)%s*$')
if psv == '0' then passive = false end
if psw and psw ~= '' then pass = decrypt_ftp_password(psw) end
if usr and usr ~= '' then user = usr end
if url and url ~= '' then
url = url:gsub('ftp://', '')
local usr, hs1 = url:match('^(.-)@(.-)$') -- user@host
if usr and usr ~= '' then user = usr; url = hs1 end
local hs2, prt = url:match('^(.-):(%d+)$') -- host:port
if prt and prt ~= '' then port = prt; url = hs2 end
local hs3, dr1 = url:match('^(.-)(/.*)$') -- host/dir
if dr1 and dr1 ~= '' then dir = dr1; url = hs3 end
host = url
end
end
assert(host and host ~= '');
local sess = 'ftp(' .. host .. ')'
if dir then sess = sess .. '-' .. dir:gsub('/','_') end
if user then sess = sess .. '@' .. user end
Keys('Tab')
Keys('ShiftF4')
Keys('CtrlDown Down Down Enter') -- protocol FTP
Keys('Tab Tab'); print(host)
Keys('Tab CtrlY'); print(port)
-- Keys('Tab'); if user then Keys('CtrlDown Down Enter') end -- Anonymous or Normal login
Keys('Tab'); if user then Keys('CtrlY'); print(user) end
Keys('Tab'); if pass then Keys('CtrlY'); print(pass) end
Keys('CtrlPgDn CtrlPgDn'); if dir then Keys('AltM Subtract Tab'); print(dir) end
Keys('CtrlPgDn CtrlPgDn AltP'); Keys(passive and 'Add' or 'Subtract')
if user then Keys('Enter') end -- extra Enter to accept plain password
Keys('Enter');
-- local suggest_path = Dlg.GetValue()
-- local sdir = suggest_path:match('^(.+/)[^/]+%s*$') or ''
-- local sdir = ''
-- Keys('CtrlY'); print(sdir..sess);
Keys('Tab')
end;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment