cuber1098

startup.lua or Server Code

Dec 31st, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. os.loadAPI("/LNEX/Server/APIS/ip")
  2.  
  3. function os.pullEvent()
  4.     local cEvent, p1, p2, p3, p4, p5 = os.pullEventRaw()
  5.         if cEvent == "terminate" then
  6.         end
  7.     return cEvent, p1, p2, p3, p4, p5
  8. end
  9.  
  10. serverRun = true
  11.  
  12. dCheck = {}
  13. dCheck[1] = "top"
  14. dCheck[2] = "bottom"
  15. dCheck[3] = "left"
  16. dCheck[4] = "right"
  17. dCheck[5] = "front"
  18. dCheck[6] = "back"
  19.  
  20. for n = 1,#dCheck do
  21.     rednet.open(dCheck[n])
  22. end
  23.  
  24. function checkServer(yn)
  25.     write("Start with maintainence?: ")
  26.     it = read()
  27.     if it == "y" or it == "Y" then
  28.         term.clear()
  29.         return "main"
  30.     end
  31.     if yn == "n" then
  32.         return false
  33.     end
  34.     if fs.exists("/LNEX/Server/CompServe") then
  35.         if fs.exists("/LNEX/Server/APIS") then
  36.             if fs.exists("/LNEX/Server/APIS/WFCI") and fs.exists("/LNEX/Server/APIS/WPCI") and fs.exists("/LNEX/Server/APIS/WAPI") and fs.exists("/LNEX/Server/APIS/WNIL") then
  37.                 if serverRun == true then
  38.                     return true
  39.                 else
  40.                     return false
  41.                 end
  42.             else
  43.                 return false
  44.             end
  45.         else
  46.             return false
  47.         end
  48.     else
  49.         return false
  50.     end
  51. end
  52.  
  53. function placeAcc(pass , compi)
  54.     if fs.exists("/LNEX/Server/CompServe/" .. compi) then
  55.         hpath = "/LNEX/Server/CompServe/" .. compi
  56.         shell.run(hpath)   
  57.         if #passw == 0 then
  58.             file = io.open(hpath , "w")
  59.             local nnum = #passw + 1
  60.             file:write("passw = {}")
  61.             file:write("\npassw[" ..nnum.. "] = '".. pass .."'")
  62.             file:close()
  63.         else
  64.             file = io.open(hpath , "w")
  65.             nnum = #passw + 1
  66.             file:write("passw = {}")
  67.             for i = 1,#passw do
  68.                 local curpass = passw[i]
  69.                 file:write("\npassw[" ..i.. "] = '" .. curpass.."'")
  70.             end
  71.             file:write("\npassw[" ..nnum.. "] = '".. pass .."'")
  72.         end
  73.         file:close()
  74.         shell.run("/LNEX/Server/CompServe/" .. compi)
  75.         if passw[nnum] == pass then
  76.             return true
  77.         else
  78.             return false
  79.         end
  80.     else
  81.         file = io.open("/LNEX/Server/CompServe/" .. compi , "w")
  82.         file:write("passw = {}")
  83.         file:close()
  84.         placeAcc(pass , compi)
  85.     end
  86. end
  87.  
  88. function sendAcc(compi)
  89.     ting = readAccData(compi)
  90.     sv = textutils.serialize(ting)
  91.     assert(type(ting) == "table" , "Error sending table")
  92.    
  93.     rednet.send(compi , sv)
  94. end
  95.  
  96. function acce(compi)
  97.     if fs.exists("/LNEX/Server/CompServe/" .. compi) == true then
  98.         print("Account Read" ..compi)
  99.         shell.run("/LNEX/Server/CompServe/" .. compi)
  100.         if fs.exists("/LNEX/Server/CompServe/" .. compi) == true and #passw >= 5 then
  101.             return true
  102.         else
  103.             return false
  104.         end
  105.     else
  106.         return false
  107.     end
  108. end
  109.  
  110. --[[
  111. function edCount(pass , edvar , compi)
  112.     if edvar == 1 then
  113.         local file = io.open("/LNEX/Server/accounts/" .. compi , "r")
  114.         local cp = file:read()
  115.         file:close()
  116.         if pass == cp then
  117.             return true
  118.         else
  119.             return false
  120.         end
  121.     elseif edvar == 2 then
  122.         local file = io.open("/LNEX/Server/accounts/" .. compi , "w+")
  123.         file:write(pass)
  124.         file:close()
  125.         return true
  126.     end
  127. end]]
  128.  
  129. function readAccData(init)
  130.     shell.run("/LNEX/Server/CompServe/" .. init)
  131.     if #passw >= 1 then
  132.         comp = {}
  133.         for i = 1,#passw do
  134.             comp[i] = passw[i]
  135.             print(comp[i])
  136.         end
  137.         tcomp = textutils.serialize(comp)
  138.         ip.errCheckInv(tcomp , "nil")
  139.         if tcomp == nil then return false end
  140.         print("Account Read: Sending to " ..init)
  141.         return tcomp
  142.     end
  143. end
  144.  
  145. function server()
  146.     id  = os.getComputerID()
  147.     sm = checkServer("y")
  148.         if sm == true then
  149.             state = true
  150.             term.clear()
  151.         elseif sm == "main" then
  152.             state = false
  153.             term.clear()
  154.         else
  155.             state = false
  156.         end
  157.     while serverRun == true do
  158.         term.setTextColor(colors.white)
  159.         output = state and "Open and Servicing" or "Closed for Maintainence or Error"
  160.         textColor = state and colors.green or colors.red
  161.         term.setCursorPos(1,1)
  162.         term.write("Server ID: " ..id)
  163.         print(" ")
  164.         print(" ")
  165.         term.setTextColor(colors.white)
  166.         term.write("Server status: ")
  167.         term.setTextColor(textColor)
  168.         term.write(output)
  169.         if state == false then
  170.             os.pullEvent("key")
  171.             term.clear()
  172.             term.setCursorPos(1,1)
  173.             serverRun = false
  174.             return
  175.         end
  176.         event, p1, p2 = os.pullEvent("rednet_message")
  177.             sendid = p1
  178.             if p2 == "$-*" then
  179.                 rednet.send(sendid , "get")
  180.                 event, p1, p2, p3 = os.pullEvent("rednet_message")
  181.                 newacc = p2
  182.                 sendid = p1
  183.                 tem = tonumber(sendid)
  184.                 if placeAcc(newacc , tem) == true then
  185.                     rednet.send(sendid , "success")
  186.                 else
  187.                     rednet.send(sendid , "failure")
  188.                 end
  189.             elseif p2 == "req" then
  190.                 if acce(p1) == true then --Fixed
  191.                     rednet.send(sendid , "true")
  192.                 else
  193.                     rednet.send(sendid , "false")
  194.                 end
  195.                 repeat
  196.                     p1 , p2 = rednet.receive(2) --or whatever timeout you like
  197.                 until p1 and p1 == sendid
  198.                 sendAcc(p1)  --WIP
  199.             elseif p2 == "is" then
  200.                 if acce(p1) == true then
  201.                     rednet.send(p1 , "true")
  202.                 else
  203.                     rednet.send(p1 , "false")
  204.                 end
  205.             elseif p2 == "edit" then
  206.                 event , p1 , p2 = os.pullEvent("rednet_message")
  207.                 curpass = p2
  208.                 if edCount(curpass , 1 , p1) == true then
  209.                     rednet.send(p1 , "true")
  210.                 else
  211.                     rednet.send(p1 , "false")
  212.                 end
  213.                 event , p1 , p2 = os.pullEvent("rednet_message")
  214.                 npass = p2
  215.                 if edCount(npass , 2 , p1) == true then
  216.                     rednet.send(p1 , "true")
  217.                 end
  218.             end
  219.         sleep(.0000000001)
  220.     end
  221. end
  222.  
  223. if serverRun == false and maintRun == false then
  224.     os.pullEvent("key")
  225.     os.reboot()
  226. end
  227.  
  228. server()
Advertisement
Add Comment
Please, Sign In to add comment