Advertisement
CodeCrafter

RDP

Mar 13th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.65 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1, 1)
  3. term.write("Server ID: ")
  4. client = tonumber(read())
  5. local sides = {"top", "bottom", "left", "right", "front", "back"}
  6.  
  7. lastre = {}
  8. onm = false
  9. local stx, sty = term.getSize()
  10.  
  11. function rec()
  12.         repeat
  13.                 event, id, msg = os.pullEvent()
  14.         until id == client and event == "rednet_message"
  15.         local typey = ""
  16.         for i = 1, #msg do
  17.                 if string.sub(msg, i, i) == ":" then
  18.                         typey = string.sub(msg, 1, i-1)
  19.                         break
  20.                 end
  21.         end
  22.         return typey, textutils.unserialize(string.sub(msg, #typey+2))
  23. end
  24.  
  25. function ports()
  26.         for i,v in pairs(sides) do
  27.                 if peripheral.isPresent(v) and peripheral.getType(v) == "modem" then
  28.                         rednet.open(v)
  29.                 end
  30.         end
  31. end
  32.  
  33. function send(bef, msg)
  34.         rednet.send(client, bef..":"..textutils.serialize(msg))
  35. end
  36.  
  37. function check(fl, ms)
  38.         if fl == "blink" then
  39.                 term.setCursorBlink(ms[1])
  40.         elseif fl == "write" then
  41.                 term.write(ms[1])
  42.         elseif fl == "clear" then
  43.                 term.clear()
  44.         elseif fl == "cursorpos" then
  45.                 term.setCursorPos(ms[1], ms[2])
  46.         elseif fl == "textcol" then
  47.                 term.setTextColor(ms[1])
  48.         elseif fl == "backcol" then
  49.                 term.setBackgroundColor(ms[1])
  50.         elseif fl == "scroll" then
  51.                 term.scroll(ms[1])
  52.         elseif fl == "clearline" then
  53.                 term.clearLine()
  54.         elseif fl == "past" then
  55.                 for i,v in pairs(ms) do
  56.                         local typey = ""
  57.                         for i = 1, #v do
  58.                                 if string.sub(v, i, i) == ":" then
  59.                                         typey = string.sub(v, 1, i-1)
  60.                                         break
  61.                                 end
  62.                         end
  63.                         check(typey, textutils.unserialize(string.sub(v, #typey+2)))
  64.                 end
  65.         end
  66. end
  67.  
  68. function start()
  69.         send("new", {})
  70.         while true do
  71.                 if onm == true then break end
  72.                 pcall(function()
  73.                         local fl, ms = rec()
  74.                         check(fl, ms)
  75.                 end)
  76.         end
  77. end
  78.  
  79. function clear()
  80.         term.clear()
  81.         term.setCursorPos(1, 1)
  82. end
  83.  
  84. function drawlist(title, listr, num)
  85.         clear()
  86.         term.setTextColor(colors.lime)
  87.         local list = {}
  88.         print(title.."\n")
  89.         for i,v in pairs(listr) do
  90.                 list[i] = v
  91.         end
  92.         local start = 1
  93.         local last = #list
  94.         if num > sty - 4 then
  95.                 start = num - (sty - 4)
  96.         end
  97.         local cnt = 0
  98.         for i = start, #list do
  99.                 if cnt > sty - 4 then
  100.                         last = last - 1
  101.                         cnt = cnt - 1
  102.                 end
  103.                 cnt = cnt + 1
  104.         end
  105.         for m = start, last do
  106.                 local tor = true
  107.                 if tor == true then
  108.                         if m == num then
  109.                                 term.setTextColor(colors.white)
  110.                                 write("[")
  111.                                 term.setTextColor(colors.lime)
  112.                                 write("-")
  113.                                 term.setTextColor(colors.white)
  114.                                 write("]")
  115.                                 term.setTextColor(colors.lime)
  116.                                 print(" "..list[m])
  117.                         else
  118.                                 term.setTextColor(colors.white)
  119.                                 print("[ ] "..list[m])
  120.                         end
  121.                 end
  122.         end
  123. end
  124.  
  125. function getKey()
  126.         local rt = nil
  127.         function lop()
  128.                 local e, a1 = os.pullEvent("key")
  129.                 if a1 == 200 then
  130.                         rt = "up"
  131.                 elseif a1 == 208 then
  132.                         rt = "down"
  133.                 elseif a1 == 28 then
  134.                         rt = "enter"
  135.                 else
  136.                         lop()
  137.                 end
  138.         end
  139.         lop()
  140.         repeat sleep(0) until rt ~= nil
  141.         return rt
  142. end
  143.  
  144. function ch(title, list)
  145.         clear()
  146.         local ind = 1
  147.         local lcopy = list
  148.         drawlist(title, lcopy, ind)
  149.         local rt = nil
  150.         cycle = function()
  151.                 local ke = getKey()
  152.                 if ke == "up" then
  153.                         if ind > 1 then
  154.                                 ind = ind - 1
  155.                         else
  156.                                 ind = #list
  157.                         end
  158.                         drawlist(title, lcopy, ind)
  159.                         cycle()
  160.                 elseif ke == "down" then
  161.                         if ind < #list then
  162.                                 ind = ind + 1
  163.                         else
  164.                                 ind = 1
  165.                         end
  166.                         drawlist(title, lcopy, ind)
  167.                         cycle()
  168.                 elseif ke == "enter" then
  169.                         rt = ind
  170.                 end
  171.         end
  172.         cycle()
  173.         repeat sleep(0) until rt ~= nil
  174.         return ind
  175. end
  176.  
  177. function getFile()
  178.         local ret = nil
  179.         local curd = ""
  180.         function lp()
  181.                 local tbl = {".."}
  182.                 for i,v in pairs(fs.list(curd)) do
  183.                         print(v)
  184.                         table.insert(tbl, v)
  185.                 end
  186.                 local tcurd = curd
  187.                 if curd == "" then
  188.                         tcurd = "/"
  189.                 end
  190.                 local rut = ch("Directory: "..tcurd, tbl)
  191.                 if rut == 1 then
  192.                         if curd ~= "" then
  193.                                 local newd = curd
  194.                                 local lastr = 1
  195.                                 for i = 1, #newd do
  196.                                         if string.sub(newd, i, i) == "/" then
  197.                                                 lastr = i
  198.                                         end
  199.                                 end
  200.                                 curd = string.sub(newd, 1, lastr-1)
  201.                                 lp()
  202.                         end
  203.                 elseif fs.isDir(curd.."/"..tbl[rut]) then
  204.                         local ecurd = curd.."/"
  205.                         if curd == "" then
  206.                                 ecurd = "/"
  207.                         end
  208.                         curd = ecurd..tbl[rut]
  209.                         lp()
  210.                 elseif not fs.isDir(curd.."/"..tbl[rut]) then
  211.                         local fb = fs.open(curd.."/"..tbl[rut], "r")
  212.                         ret = {tbl[rut], fb.readAll()}
  213.                         fb.close()
  214.                 else
  215.                         lp()
  216.                 end
  217.         end
  218.         lp()
  219.         repeat sleep(0) until ret
  220.         return ret
  221. end
  222.  
  223. function remotelist(tolist)
  224.         send("fslist", {tolist})
  225.         repeat
  226.                 fl, msg = rec()
  227.         until fl == "list"
  228.         return msg
  229. end
  230.  
  231. function remoteid(tolist)
  232.         send("fsisdir", {tolist})
  233.         repeat
  234.                 fl, msg = rec()
  235.         until fl == "isdir"
  236.         return msg
  237. end
  238.  
  239. function remoteget(tolist)
  240.         send("fsopen", {tolist})
  241.         repeat
  242.                 fl, msg = rec()
  243.         until fl == "open"
  244.         return msg
  245. end
  246.  
  247. function getRemoteFile()
  248.         local ret = nil
  249.         local curd = ""
  250.         function lp()
  251.                 local tbl = {".."}
  252.                 for i,v in pairs(remotelist(curd)) do
  253.                         table.insert(tbl, v)
  254.                 end
  255.                 local tcurd = curd
  256.                 if curd == "" then
  257.                         tcurd = "/"
  258.                 end
  259.                 local rut = ch("Directory: "..tcurd, tbl)
  260.                 if rut == 1 then
  261.                         if tcurd ~= "" then
  262.                                 local newd = curd
  263.                                 local lastr = 1
  264.                                 for i = 1, #newd do
  265.                                         if string.sub(newd, i, i) == "/" then
  266.                                                 lastr = i
  267.                                         end
  268.                                 end
  269.                                 curd = string.sub(newd, 1, lastr-1)
  270.                                 if curd == "/" then curd = "" end
  271.                                 lp()
  272.                         end
  273.                 elseif remoteid(curd.."/"..tbl[rut]) then
  274.                         local ecurd = curd.."/"
  275.                         if curd == "" then
  276.                                 ecurd = "/"
  277.                         end
  278.                         curd = ecurd..tbl[rut]
  279.                         if curd == "/" then curd = "" end
  280.                         lp()
  281.                 elseif not remoteid(curd.."/"..tbl[rut]) then
  282.                         local fb = remoteget(curd.."/"..tbl[rut])
  283.                         ret = {tbl[rut], fb}
  284.                 else
  285.                         lp()
  286.                 end
  287.         end
  288.         lp()
  289.         repeat sleep(0) until ret
  290.         return ret
  291. end
  292.  
  293.  
  294.  
  295. function get(_sFilter)
  296.         local event, p1, p2, p3, p4, p5 = os.pullEventRaw(_sFilter)
  297.         if event ~= "rednet_message" then
  298.                 if onm == false then
  299.                         send("event", {event, p1, p2, p3, p4, p5})
  300.                 end
  301.         end
  302. end
  303.  
  304. sleep(0)
  305. ports()
  306. os.pullEvent = get
  307. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement