Advertisement
MC403

SJNOS-WFM (Network)

Nov 24th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.46 KB | None | 0 0
  1. --SJNOS by MC403. wfm.exe (8JdUff3c)
  2.  
  3. local tArgs = {...} --wfm.exe <USERNAME> <DOMAIN> <WFM_USER> <WFM_PASS>
  4.  
  5. local COLOR_TEXT = colors.orange
  6. local COLOR_BG = colors.gray
  7. local COLOR_HEAD_TEXT = colors.orange
  8. local COLOR_HEAD_BG = colors.gray
  9.  
  10. local function start()
  11.  
  12.  
  13. if (not term.isColor()) then
  14.     return true
  15. elseif (#tArgs[1] < 4) then
  16.     error("Invalid Parameters.")
  17. end
  18.  
  19. os.loadAPI("SJNOS/system/SJNOS/sjn")
  20.  
  21. local cpos, clear, tcolor, bcolor, getTime = sjn.getStandardFunctions()
  22. local width, height = term.getSize()
  23.  
  24. local USERNAME = tArgs[1]
  25. local USERRANK = sjn.getConfigFileContent("SJNOS/users/"..USERNAME.."/config/.config").d4
  26.  
  27. local REDNET, REDNETSIDE = sjn.getRednet(true)
  28.  
  29. local DNS_SERVER = tonumber(sjn.getConfigFileContent("SJNOS/settings/net.set").dns)
  30.  
  31. local REFRESH_COUNTER = 100
  32.  
  33. local wfm_user = tostring(tArgs[3])
  34. local wfm_pass = tostring(tArgs[4])
  35. local wfm_id = -1
  36.  
  37. local KEY = -1
  38. local SERVER = -1
  39.  
  40. local selection = 1
  41.  
  42. local FILES_homePath = 'SJNOS/users/' .. USERNAME .. "/home"
  43. local FILES_userPath = ''
  44. local FILES_serverPath = ''
  45.  
  46. local FILES_serverData = {}
  47.  
  48. local FILES_elements = {['y']={}, ['c']={}}
  49.  
  50. local last_refresh_string = ''
  51.  
  52. local function warning(header, text)
  53.     local array = sjn.splitStringWithNumber('', text, 30, 18)
  54.     sjn.showDialog(header, array, colors.lightGray, colors.white, colors.white, colors.lightGray, {{colors.lime, colors.white, "Okay"}})
  55. end
  56. local function showInputDialog(header, texts)
  57.     local success, input = sjn.showInputDialog(header, texts, colors.lightGray, colors.white, colors.white, colors.lightGray, colors.orange, colors.gray, {{colors.lime, colors.white, "Okay"},{colors.red, colors.white, "Cancel"}}, nil, nil, true)
  58.     return (success == 1), input
  59. end
  60.  
  61. local function checkArgs()
  62.     if (tonumber(tArgs[2]) == nil) then
  63.         local target = tArgs[2]
  64.  
  65.         rednet.send(DNS_SERVER, "~SJN@SWEBCLIENT:GETIDOF#" .. target)
  66.  
  67.         local responded, success, result = sjn.receive(DNS_SERVER, 2, function(msg)
  68.             if (string.find(msg, "~SJN@SWEBDNS:IDIS#")) then
  69.                 local p1, p2 = string.find(msg, "~SJN@SWEBDNS:IDIS#")
  70.                 if (#msg > p2) then
  71.                     local result = string.sub(msg, p2 + 1)
  72.                     if (tonumber(result)) then
  73.                         return true, true, math.floor(math.abs(tonumber(result)))
  74.                     end
  75.                 end
  76.             elseif (string.find(msg, "~SJN@SWEBDNS:SERVER_NOT_REGISTERED")) then
  77.                 return true, false
  78.             end
  79.         end)
  80.  
  81.         if (responded) then
  82.             if (success) then
  83.                 wfm_id = result
  84.             else
  85.                 warning('WFM Message', "This domain is not registered on your connected DNS (#"..DNS_SERVER..").")
  86.             end
  87.         else
  88.             warning('WFM Message', "No responce by the DNS. Are you sure this DNS exists and isn't busy?")
  89.         end
  90.     else
  91.         wfm_id = math.floor(math.abs(tonumber(tArgs[2])))
  92.     end
  93.  
  94.     if (wfm_id >= 0) then
  95.         rednet.send(wfm_id, "~SJN@WFMCLIENT:LOGIN#" .. wfm_user .. '#' .. wfm_pass)
  96.         local responded, success, key = sjn.receive(wfm_id, 2, function(msg)
  97.             if (string.find(msg, "~SJN@WFMSERVER:LOGIN_SUCCESS#")) then
  98.                 local p1, p2 = string.find(msg, "~SJN@WFMSERVER:LOGIN_SUCCESS#")
  99.                 if (#msg > p2) then
  100.                     return true, true, string.sub(msg, p2 + 1)
  101.                 end
  102.             elseif (string.find(msg, "~SJN@WFMSERVER:LOGIN_WRONG")) then
  103.                 return true, false
  104.             end
  105.         end)
  106.  
  107.         if (responded) then
  108.             if (success) then
  109.                 KEY = key
  110.                 SERVER = wfm_id
  111.                 return true
  112.             else
  113.                 sjn.showDialog('WFM Message', {"Username or Password wrong!"}, colors.orange, colors.gray, colors.gray, colors.orange, {{colors.lime, colors.gray, "Okay"}})
  114.             end
  115.         else
  116.             sjn.showDialog('WFM Message', {"The computer doesn't respond."}, colors.orange, colors.gray, colors.gray, colors.orange, {{colors.lime, colors.gray, "Okay"}})
  117.         end
  118.     end
  119.     return false
  120. end
  121.  
  122. local r1, r2, r3 = checkArgs()
  123.  
  124. local ADMIN = r2 and r3 == 'true'
  125.  
  126. if (not r1 or KEY == -1 or SERVER == -1) then
  127.     return true --True heißt hier, dass das Programm beendet wird.
  128. end
  129.  
  130.  
  131. local function sendToServer(cmd, data, data2, data3)
  132.     local pack = {
  133.         ['data'] = data,
  134.         ['data2'] = data2,
  135.         ['data3'] = data3,
  136.         ['user'] = wfm_user,
  137.         ['password'] = wfm_pass
  138.     }
  139.     rednet.send(SERVER, '~SJN@WFMCLIENT:' .. cmd .. '#' .. textutils.serialize(pack))
  140. end
  141.  
  142. local function receiveFromServer(cmd, time)
  143.     return sjn.receive(SERVER, time or 1, function (msg)
  144.         local _, p = string.find(msg, '~SJN@WFMSERVER:' .. cmd ..'#')
  145.         if (p and #msg > p) then
  146.             return true, string.sub(msg, p + 1)
  147.         end
  148.     end)
  149. end
  150.  
  151. local function refresh()
  152.     REDNET, REDNETSIDE = sjn.getRednet(true)
  153.  
  154.     if (not REDNET) then
  155.         error('Please attach a rednet modem!')
  156.     end
  157.  
  158.     sendToServer('GETDATA', FILES_serverPath)
  159.     local success, raw = receiveFromServer('DATA', 2)
  160.  
  161.     last_refresh_string = sjn.getTime()
  162.  
  163.     if (success) then
  164.         local data = textutils.unserialize(raw)
  165.         FILES_serverData = data
  166.         --[[
  167.         data = {
  168.             ['list'] = {
  169.                 {['isDir']=false, ['name']='test.stp'}
  170.             }
  171.             ['msg'] = 'Dieser Pfad existiert niacht!'
  172.         }
  173.         ]]
  174.         if
  175.             false
  176.             then
  177.             error('Received invalid data from your WFM-Server.')
  178.         end
  179.     else
  180.         error('The synchronisazion failed because your WFM-Server stopped responding.')
  181.     end
  182. end
  183.  
  184. local function serverPathDown(name)
  185.     if (FILES_serverPath == "") then
  186.         FILES_serverPath = name
  187.     else
  188.         FILES_serverPath = FILES_serverPath .. "/" .. name
  189.     end
  190.     refresh()
  191. end
  192.  
  193. local function serverPathUp()
  194.     local nonsense = string.reverse(FILES_serverPath)
  195.     local p = string.find(nonsense,"/")
  196.  
  197.     if (p) then
  198.         local sense = string.reverse(string.sub(nonsense, p + 1))
  199.         FILES_serverPath = sense
  200.     else
  201.         FILES_serverPath = ""
  202.     end
  203.     refresh()
  204. end
  205.  
  206. local function userPathDown(name)
  207.     if (FILES_userPath == "") then
  208.         FILES_userPath = name
  209.     else
  210.         FILES_userPath = FILES_userPath .. "/" .. name
  211.     end
  212.     refresh()
  213. end
  214.  
  215. local function userPathUp()
  216.     if (FILES_userPath == '') then
  217.         return
  218.     end
  219.  
  220.     local nonsense = string.reverse(FILES_userPath)
  221.     local p = string.find(nonsense,"/")
  222.  
  223.     if (p) then
  224.         local sense = string.reverse(string.sub(nonsense, p + 1))
  225.         FILES_userPath = sense
  226.     else
  227.         FILES_userPath = ""
  228.     end
  229. end
  230.  
  231. local function drawMenu()
  232.     cpos(1,1)
  233.     tcolor(COLOR_HEAD_TEXT)
  234.     bcolor(COLOR_HEAD_BG)
  235.     term.clearLine()
  236.     cpos(1, 1)
  237.     write("WFM #"..SERVER)
  238.  
  239.     tcolor(colors.lightGray)
  240.     if (selection == 1) then
  241.         tcolor(colors.lime)
  242.     end
  243.     cpos(36, 1)
  244.     write("Terminal")
  245.  
  246.     tcolor(colors.lightGray)
  247.     if (selection == 2) then
  248.         tcolor(colors.lime)
  249.     end
  250.     cpos(45, 1)
  251.     write("Files")
  252.  
  253.     cpos(51, 1)
  254.     tcolor(colors.red)
  255.     write("X")
  256. end
  257.  
  258. local function drawMain()
  259.     tcolor(COLOR_TEXT)
  260.     bcolor(COLOR_BG)
  261.     if (selection == 1) then
  262.         --Terminal
  263.         cpos(1, 19)
  264.         tcolor(colors.lightGray)
  265.         write("Click anywhere to enter a command.")
  266.     elseif (selection == 2) then
  267.         --Files
  268.         local width_left = 25
  269.         local pos_right = width_left + 2
  270.         local width_right = width - pos_right
  271.  
  272.         tcolor(colors.white)
  273.         bcolor(colors.gray)
  274.         for y=2, height do
  275.             cpos(1, y)
  276.             write(string.rep(" ", width_left))
  277.         end
  278.         local temp = 'Your PC'
  279.         cpos((width_left - #temp) / 2 + 1, 2)
  280.         write(temp)
  281.  
  282.         cpos(1, 3)
  283.         if (FILES_userPath == '') then
  284.             write("H:/")
  285.         else
  286.             write(string.sub("H:/" .. FILES_userPath, 1, width_left - 1))
  287.         end
  288.         cpos(width_left, 3)
  289.         write("^")
  290.  
  291.         FILES_elements.y = {}
  292.         local y_elements = {["dir"]={}, ["file"]={}}
  293.         local list = fs.list(FILES_homePath .. "/" .. FILES_userPath)
  294.  
  295.         for i=1, #list do
  296.             if (fs.isDir(FILES_homePath .. "/" .. FILES_userPath .. "/" .. list[i])) then
  297.                 table.insert(y_elements.dir, list[i])
  298.             else
  299.                 table.insert(y_elements.file, list[i])
  300.             end
  301.         end
  302.  
  303.         for i=1, #y_elements.dir do
  304.             cpos(1, i + 3)
  305.             write("[=] " .. y_elements.dir[i])
  306.             table.insert(FILES_elements.y, {['name'] = y_elements.dir[i], ['isDir'] = true})
  307.         end
  308.         for i=1, #y_elements.file do
  309.             cpos(1, i + 3 + #y_elements.dir)
  310.             write("-~- " .. y_elements.file[i])
  311.             table.insert(FILES_elements.y, {['name'] = y_elements.file[i], ['isDir'] = false})
  312.         end
  313.  
  314.  
  315.         tcolor(colors.yellow)
  316.         bcolor(colors.gray)
  317.         for y=2, height do
  318.             cpos(pos_right, y)
  319.             write(string.rep(" ", width_right))
  320.         end
  321.         local temp = 'Connected PC'
  322.         cpos((width_right - #temp) / 2 + pos_right, 2)
  323.         write(temp)
  324.  
  325.         cpos(pos_right, 3)
  326.         write(string.sub("S:/" .. FILES_serverPath, 1, width_right - 1))
  327.         cpos(pos_right + width_right, 3)
  328.         write("^")
  329.  
  330.  
  331.         local data = FILES_serverData
  332.  
  333.         if (data.msg) then
  334.             local array = sjn.splitStringWithNumber('', data.msg, width_right - 2, 16)
  335.             for i=1, #array do
  336.                 cpos(pos_right + 1, i + 3)
  337.                 write(array[i])
  338.             end
  339.         else
  340.             FILES_elements.c = {}
  341.             local c_elements = {["d"]={}, ["f"]={}}
  342.  
  343.             for i=1, #data.list do
  344.                 if (data.list[i].isDir) then
  345.                     table.insert(c_elements.d, data.list[i])
  346.                 else
  347.                     table.insert(c_elements.f, data.list[i])
  348.                 end
  349.             end
  350.  
  351.             for i=1, #c_elements.d do
  352.                 cpos(pos_right, i + 3)
  353.                 write("[=] " .. c_elements.d[i].name)
  354.                 table.insert(FILES_elements.c, c_elements.d[i])
  355.             end
  356.             for i=1, #c_elements.f do
  357.                 cpos(pos_right, i + 3 + #c_elements.d)
  358.                 write("-~- " .. c_elements.f[i].name)
  359.                 table.insert(FILES_elements.c, c_elements.f[i])
  360.             end
  361.         end
  362.  
  363.  
  364.         cpos(1, 19)
  365.         tcolor(colors.lightGray)
  366.         write("Last update: " ..last_refresh_string)
  367.     end
  368. end
  369.  
  370. local function draw()
  371.     bcolor(COLOR_BG)
  372.     clear()
  373.     drawMenu()
  374.     drawMain()
  375. end
  376.  
  377. refresh()
  378. draw()
  379.  
  380. local running = true
  381.  
  382. local timer = os.startTimer(1)
  383.  
  384. while running do
  385.     if (selection == 2) then
  386.         draw()
  387.     end
  388.  
  389.     local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  390.     if (event=="mouse_click") then
  391.         local btn, x, y = p1, p2, p3
  392.         if (x == 51 and y == 1) then
  393.             --Quit
  394.             break
  395.         elseif (x>=36 and x<=43 and y==1 and selection ~= 1) then
  396.             --Terminal
  397.             selection = 1
  398.         elseif (x>=45 and x<=50 and y==1 and selection ~= 2) then
  399.             --Files
  400.             selection = 2
  401.         elseif (selection == 1) then
  402.             --Terminal Inner
  403.             if btn == 1 then
  404.                 term.scroll(1)
  405.                 drawMenu()
  406.                 cpos(1, 19)
  407.                 tcolor(colors.orange)
  408.                 bcolor(colors.gray)
  409.                 write("$ ")
  410.                 local input = read()
  411.  
  412.                 tcolor(colors.lightGray)
  413.                 if (input == 'help') then
  414.                     print("$update      Updates your Server.                  ")
  415.                   --print("                                                   ")
  416.                 elseif (input == 'update') then
  417.                     print('Sending request...')
  418.                     sendToServer('UPDATE')
  419.                     local success, raw = receiveFromServer('RESULT')
  420.                     if (success) then
  421.                         tcolor(colors.lime)
  422.                         print('Server: ' .. (textutils.unserialize(raw).result or '%'))
  423.                         tcolor(colors.yellow)
  424.                         print('This PC will idle for 10 seconds now. Please do nothing!')
  425.                         receiveFromServer('RESULT', 10)
  426.  
  427.                         refresh()
  428.                         tcolor(colors.lime)
  429.                         print('\nUpdated!')
  430.                     else
  431.                         tcolor(colors.red)
  432.                         print('No response. Maybe the update has failed.')
  433.                     end
  434.                 end
  435.  
  436.                 drawMenu()
  437.             end
  438.         elseif (selection == 2) then
  439.             --Files Inner
  440.             if (x < 26) then
  441.                 --Your PC
  442.                 if (y == 3) then
  443.                     if (x == 25) then
  444.                         -- Pfad einen höher
  445.                         userPathUp()
  446.                     end
  447.                 elseif (y > 3 and y <= #FILES_elements.y + 3) then
  448.                     local element = FILES_elements.y[y - 3]
  449.                     if (element.isDir) then
  450.                         if (btn == 1) then
  451.                             userPathDown(element.name)
  452.                         end
  453.                     else
  454.                         if (btn == 1) then
  455.  
  456.                         elseif (btn == 2) then
  457.                             local menu = {" Upload     "," Edit       "," Rename     "," Delete     "," Execute    "}
  458.                             bcolor(colors.orange)
  459.                             tcolor(colors.white)
  460.                             cpos(5, y)
  461.                             local t = element.name
  462.                             write(t .. string.rep(" ", 25 - 5 - #t))
  463.  
  464.                             local xa = x
  465.                             local ya = y
  466.  
  467.                             if (x > 25 - #menu[1]) then xa = 25 - #menu[1] end
  468.                             if (x < 5) then xa = 5 end
  469.                             if (y > 19 - #menu) then ya = 19 - #menu end
  470.  
  471.                             bcolor(colors.white)
  472.                             tcolor(colors.orange)
  473.  
  474.                             for i=1, #menu do
  475.                                 cpos(xa, ya + i)
  476.                                 write(menu[i])
  477.                             end
  478.  
  479.                             local active = true
  480.  
  481.                             while active do
  482.                                 local event, btn, x2, y2 = os.pullEventRaw()
  483.                                 if (event=="mouse_click") then
  484.                                     if (x2 >= xa and x2 <= xa + 12) then
  485.                                         if (y2 > ya and y2 <= ya + #menu) then
  486.                                             if (y2 == ya + 1) then
  487.                                                 --Upload
  488.                                                 local content = sjn.getFileAll(FILES_homePath .. "/" .. FILES_userPath .. '/' .. element.name)
  489.  
  490.                                                 sendToServer('UPLOAD', FILES_serverPath .. "/" .. element.name, content)
  491.                                                 local success, raw = receiveFromServer('RESULT')
  492.                                                 if (success) then
  493.                                                     warning("Upload", 'Server: ' .. textutils.unserialize(raw).msg)
  494.                                                     refresh()
  495.                                                 else
  496.                                                     warning("Upload", 'Upload failed.')
  497.                                                 end
  498.                                             elseif (y2 == ya + 2) then
  499.                                                 --Edit
  500.                                                 shell.run("edit", FILES_homePath .. "/" .. FILES_userPath .. "/" .. element.name)
  501.                                             elseif (y2 == ya + 3) then
  502.                                                 --Rename
  503.                                                 draw()
  504.                                                 cpos(5, y)
  505.                                                 tcolor(colors.white)
  506.                                                 bcolor(colors.gray)
  507.                                                 write(string.rep(" ", 25-5))
  508.                                                 cpos(5, y)
  509.                                                 local input = read()
  510.  
  511.                                                 if input and input ~= '' then
  512.                                                     if not string.find(input, '%.%.') then
  513.                                                         if not (string.find(input, '/') or string.find(input, '\\')) then
  514.                                                             local old_path = FILES_homePath .. '/' .. FILES_userPath .. '/' .. element.name
  515.                                                             local path = FILES_homePath .. '/' .. FILES_userPath .. '/' .. input
  516.                                                             if not fs.exists(path) then
  517.                                                                 fs.move(old_path, path)
  518.                                                             else
  519.                                                                 warning("Rename", "File exists!")
  520.                                                             end
  521.                                                         else
  522.                                                             warning("Rename", "Please don't use '/' and '\\'!")
  523.                                                         end
  524.                                                     else
  525.                                                         warning("Rename", "Please don't use '..'!")
  526.                                                     end
  527.                                                 end
  528.                                             elseif (y2 == ya + 4) then
  529.                                                 --Delete
  530.                                                 fs.delete(FILES_homePath .. "/" .. FILES_userPath .. "/" .. element.name)
  531.                                             elseif (y2 == ya + 5) then
  532.                                                 --Execute
  533.                                                 sjn.consoleStart('$ run H:/' .. FILES_userPath .. '/' .. element.name)
  534.  
  535.                                                 shell.run(FILES_homePath .. "/" .. FILES_userPath .. "/" .. element.name)
  536.  
  537.                                                 sjn.consoleEnd()
  538.                                             end
  539.                                             active = false
  540.                                         else
  541.                                             active = false
  542.                                         end
  543.                                     else
  544.                                         active = false
  545.                                     end
  546.                                 end
  547.                             end
  548.                         end
  549.                     end
  550.                 else
  551.                     if btn == 2 then
  552.                         local menu = {" New File   "," New Folder "}
  553.                         bcolor(colors.orange)
  554.                         tcolor(colors.white)
  555.                         cpos(5, y)
  556.  
  557.                         local xa = x
  558.                         local ya = y - 1
  559.  
  560.                         if (x > 25 - #menu[1]) then xa = 25 - #menu[1] end
  561.                         if (x < 5) then xa = 5 end
  562.                         if (y > 19 - #menu) then ya = 19 - #menu end
  563.  
  564.                         bcolor(colors.white)
  565.                         tcolor(colors.orange)
  566.  
  567.                         for i=1, #menu do
  568.                             cpos(xa, ya + i)
  569.                             write(menu[i])
  570.                         end
  571.  
  572.                         local active = true
  573.  
  574.                         while active do
  575.                             local event, btn, x2, y2 = os.pullEventRaw()
  576.                             if (event=="mouse_click") then
  577.                                 if (x2 >= xa and x2 <= xa + 12) then
  578.                                     if (y2 > ya and y2 <= ya + #menu) then
  579.                                         if (y2 == ya + 1) then
  580.                                             --New File
  581.                                             local success, input = showInputDialog('New File', {'Create a new file:'})
  582.  
  583.                                             if success and input ~= '' then
  584.                                                 if not (string.find(input, '%.%.') or string.find(input, '/') or string.find(input, '\\')) then
  585.                                                     local path = FILES_homePath .. '/' .. FILES_userPath .. '/' .. input
  586.                                                     if not fs.exists(path) then
  587.                                                         local f = fs.open(path, 'w')
  588.                                                         f.close()
  589.                                                     else
  590.                                                         warning("New File", "File exists!")
  591.                                                     end
  592.                                                 else
  593.                                                     warning("New File", "Please don't use '..' and '/' and '\\'!")
  594.                                                 end
  595.                                             end
  596.                                         elseif (y2 == ya + 2) then
  597.                                             --New Folder
  598.                                             local success, input = showInputDialog('New Folder', {'Create a new folder:'})
  599.                                             if success and input and input ~= '' then
  600.                                                 if not (string.find(input, '%.%.') or string.find(input, '/') or string.find(input, '\\')) then
  601.                                                     local path = FILES_homePath .. '/' .. FILES_userPath .. '/' .. input
  602.                                                     if not fs.exists(path) then
  603.                                                         fs.makeDir(path)
  604.                                                     else
  605.                                                         warning("New Folder", "File exists!")
  606.                                                     end
  607.                                                 else
  608.                                                     warning("New Folder", "Please don't use '..' and '/' and '\\'!")
  609.                                                 end
  610.                                             end
  611.                                         end
  612.                                         active = false
  613.                                     else
  614.                                         active = false
  615.                                     end
  616.                                 else
  617.                                     active = false
  618.                                 end
  619.                             end
  620.                         end
  621.                     end
  622.                 end
  623.             elseif (x > 26) then
  624.                 --Connected PC
  625.                 if (y == 3) then
  626.                     if (x == 51) then
  627.                         -- Pfad einen höher
  628.                         serverPathUp()
  629.                     end
  630.                 elseif (not FILES_serverData.msg) then
  631.                     if (y > 3 and y <= #FILES_elements.c + 3) then
  632.                         local element = FILES_elements.c[y - 3]
  633.                         if (element.isDir) then
  634.                             if (btn == 1) then
  635.                                 serverPathDown(element.name)
  636.                             end
  637.                         else
  638.                             if (btn == 1) then
  639.                             elseif (btn == 2) then
  640.                                 local menu = {" Download   "," Edit       "," Rename     "," Delete     "," Execute    "}
  641.                                 bcolor(colors.orange)
  642.                                 tcolor(colors.white)
  643.                                 cpos(27 + 4, y)
  644.                                 local t = element.name
  645.                                 write(t .. string.rep(" ", 51 - 27 - 4 - #t))
  646.  
  647.                                 local xa = x + 0
  648.                                 local ya = y + 0
  649.  
  650.                                 if (x > 51 - #menu[1]) then xa = 51 - #menu[1] end
  651.                                 if (x < 27 + 4) then xa = 27 + 4 end
  652.                                 if (y > 19 - #menu) then ya = 19 - #menu end
  653.  
  654.                                 bcolor(colors.white)
  655.                                 tcolor(colors.orange)
  656.  
  657.                                 for i=1, #menu do
  658.                                     cpos(xa, ya + i)
  659.                                     write(menu[i])
  660.                                 end
  661.  
  662.                                 local active = true
  663.  
  664.                                 while active do
  665.                                     local event, btn, x2, y2 = os.pullEventRaw()
  666.                                     if (event=="mouse_click") then
  667.                                         if (x2 >= xa and x2 <= xa + 12) then
  668.                                             if (y2 > ya and y2 <= ya + #menu) then
  669.                                                 if (y2 == ya + 1) then
  670.                                                     --Download
  671.                                                     sendToServer('DOWNLOAD', FILES_serverPath .. '/' .. element.name)
  672.                                                     local success, raw = receiveFromServer('CONTENT')
  673.                                                     if (success) then
  674.                                                         local download_path = FILES_homePath .. '/' .. FILES_userPath .. '/' .. element.name
  675.  
  676.                                                         if (not fs.isReadOnly(download_path)) then
  677.                                                             download_path = sjn.getAltFileName(download_path)
  678.  
  679.                                                             local f = fs.open(download_path, "w")
  680.                                                             f.write(textutils.unserialize(raw).content)
  681.                                                             f.close()
  682.                                                         else
  683.                                                             warning("Download", 'Can\'t download in a read-only path. Please select a path that isn\'t in \'C:/rom/\'')
  684.                                                         end
  685.                                                     else
  686.                                                         warning("Download", 'Download failed. Try again.')
  687.                                                     end
  688.                                                 elseif (y2 == ya + 2) then
  689.                                                     --Edit
  690.                                                     sendToServer('DOWNLOAD', FILES_serverPath .. '/' .. element.name)
  691.                                                     local success, raw = receiveFromServer('CONTENT')
  692.                                                     if (success) then
  693.                                                         local download_path = 'SJNOS/users/' .. USERNAME ..'/temp/.wfm_download'
  694.  
  695.                                                         local f = fs.open(download_path, "w")
  696.                                                         f.write(textutils.unserialize(raw).content)
  697.                                                         f.close()
  698.                                                         if (shell.run("edit", download_path)) then
  699.                                                             local f = fs.open(download_path, "r")
  700.                                                             local c = f.readAll()
  701.                                                             f.close()
  702.  
  703.                                                             if (c ~= textutils.unserialize(raw).content) then
  704.                                                                 --Es wurde etwas geändert.
  705.                                                                 sendToServer('UPLOAD', FILES_serverPath .. '/' .. element.name, c)
  706.                                                                 local success, raw = receiveFromServer('RESULT')
  707.                                                                 if (success) then
  708.                                                                     warning("Edit", 'Server: ' .. textutils.unserialize(raw).msg)
  709.                                                                 else
  710.                                                                     warning("Edit", 'Upload failed.')
  711.                                                                 end
  712.                                                             end
  713.                                                         end
  714.                                                     else
  715.                                                         warning("Edit", 'Can\'t edit because the download failed. Try again.')
  716.                                                     end
  717.                                                 elseif (y2 == ya + 3) then
  718.                                                     --Rename
  719.                                                     draw()
  720.                                                     cpos(31, y)
  721.                                                     tcolor(colors.yellow)
  722.                                                     bcolor(colors.gray)
  723.                                                     write(string.rep(" ", 51-31))
  724.                                                     cpos(31, y)
  725.                                                     local input = read()
  726.  
  727.                                                     if input and input ~= '' then
  728.                                                         if not string.find(input, '%.%.') then
  729.                                                             if not (string.find(input, '/') or string.find(input, '\\')) then
  730.                                                                 local path = FILES_serverPath .. '/' .. input
  731.  
  732.                                                                 sendToServer('MOVE', FILES_serverPath .. '/' .. element.name, FILES_serverPath .. '/' .. input)
  733.                                                                 local success, raw = receiveFromServer('RESULT')
  734.                                                                 if (success) then
  735.                                                                     if (textutils.unserialize(raw).result) then
  736.                                                                         warning("Rename", 'Server: ' .. textutils.unserialize(raw).result)
  737.                                                                         refresh()
  738.                                                                     else
  739.                                                                         warning("Rename", 'Invalid response.')
  740.                                                                     end
  741.                                                                 else
  742.                                                                     warning("Rename", "No response.")
  743.                                                                 end
  744.                                                             else
  745.                                                                 warning("Rename", "Please don't use '/' and '\\'!")
  746.                                                             end
  747.                                                         else
  748.                                                             warning("Rename", "Please don't use '..'!")
  749.                                                         end
  750.                                                     end
  751.                                                 elseif (y2 == ya + 4) then
  752.                                                     --Delete
  753.                                                     sendToServer('DELETE', FILES_serverPath .. '/' .. element.name)
  754.                                                     local success, raw = receiveFromServer('RESULT')
  755.                                                     if (success) then
  756.                                                         if (textutils.unserialize(raw).msg) then
  757.                                                             warning("Delete", 'Server: ' .. textutils.unserialize(raw).msg)
  758.                                                         end
  759.                                                     else
  760.                                                         warning("Delete", "No response.")
  761.                                                     end
  762.                                                 elseif (y2 == ya + 5) then
  763.                                                     --Execute
  764.                                                     if sjn.showConfirmDialog('Execute', {"Do you really want to run", "'"..element.name.."'?", "It may damage your server!", "Maybe you will lose this connection."}, colors.lightGray, colors.white, colors.white, colors.lightGray, 2) then
  765.                                                         sendToServer('EXECUTE', FILES_serverPath .. '/' .. element.name)
  766.                                                         local success, raw = receiveFromServer('RESULT')
  767.                                                         if (success) then
  768.                                                             if (textutils.unserialize(raw).result) then
  769.                                                                 warning("Execute", 'Server: ' .. textutils.unserialize(raw).result)
  770.                                                             else
  771.                                                                 warning("Execute", 'Invalid response.')
  772.                                                             end
  773.                                                         else
  774.                                                             warning("Execute", "No response.")
  775.                                                         end
  776.                                                     end
  777.                                                 end
  778.                                                 active = false
  779.                                             else
  780.                                                 active = false
  781.                                             end
  782.                                         else
  783.                                             active = false
  784.                                         end
  785.                                     end
  786.                                 end
  787.                             end
  788.                         end
  789.                     end
  790.                 end
  791.                 timer = os.startTimer(1)
  792.             end
  793.         end
  794.     elseif (event == 'modem_message') then
  795.         local side, channel, id, msg, dis = p1, p2, p3, p4, p5
  796.         if (id == SERVER) then
  797.  
  798.         end
  799.     elseif (event == 'timer' and timer == p1) then
  800.         timer = os.startTimer(1)
  801.         refresh()
  802.     end
  803. end
  804.  
  805. return true
  806. end
  807.  
  808. local ok, err = pcall(start)
  809. if not ok then
  810.     sjn.drawErrorScreen2(err)
  811. end
  812.  
  813. if (term.isColor) then
  814.     term.setTextColor(colors.white)
  815.     term.setBackgroundColor(colors.black)
  816. end
  817. term.clear()
  818. term.setCursorPos(1, 1)
  819.  
  820. os.unloadAPI("SJNOS/system/SJNOS/sjn")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement