Advertisement
Xylem_Gaming

Server

Feb 18th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1.  
  2. options = {"Log","Exit"}--S1
  3. --shelloptions = {"Kick","Ban","Message","List Clients","Back"}--S2
  4. --Screen Options
  5.  
  6. tsize = table.getn(options)
  7. hid = os.getComputerID()
  8. --tsize2 = table.getn(shelloptions)
  9. --Function variables
  10.  
  11. running = true
  12. srunning = true
  13. subexit = true
  14. --Booleans
  15.  
  16. index = 1
  17. index2 = 1
  18. unindex = 1
  19. --Integers
  20.  
  21. pass = "test" -- Password set here
  22. --Strings
  23.  
  24. os.loadAPI("/screen")
  25. rednet.open("top")
  26. --Initalised Varibles
  27.  
  28. state = nil
  29. logger = {}
  30. --Misc Variables
  31.  
  32. local function addLog(element)
  33.     logger[unindex] = element
  34.     unindex = unindex + 1
  35. end
  36.  
  37. function writeClient(idno)
  38.     file = fs.open(".log",'w')
  39.     if clientid == nil then
  40.         clientid = {}
  41.         clientid[1] = idno
  42.         temp2 = textutils.serialise(clientid)
  43.         file.write(temp2)
  44.     else
  45.         maxn = table.getn(clientid)
  46.         clientid[maxn+1] = idno
  47.         temp2 = textutils.serialise(clientid)
  48.         file.write(temp2)
  49.     end
  50.     file.close()
  51. end
  52.  
  53. function readClient()
  54.     file = fs.open(".log",'r')
  55.     clientid = textutils.unserialise(file.readAll())
  56.     file.close()
  57.     return clientid
  58. end
  59.  
  60.  
  61. function sendAll(senderid, text)
  62.     for i in ipairs(clientid) do
  63.         if senderid ~= clientid[i] then
  64.             rednet.send(clientid[i],text)
  65.         end
  66.     end
  67. end
  68.  
  69. function test(variable)
  70.     if clientid ~= nil then
  71.     for i in ipairs(clientid) do
  72.         if variable == clientid[i] then
  73.             return true
  74.         elseif i == table.maxn(clientid) then
  75.             return false
  76.         end
  77.     end
  78.     else
  79.         return false
  80.     end
  81. end
  82.  
  83. clientid = readClient()
  84.  
  85. if clientid ~= nil then
  86.     addLog("Saved IDs :")
  87.     for i in ipairs(clientid) do
  88.         addLog(clientid[i]..",")
  89.     end
  90. else
  91.     addLog("No saved IDs")
  92. end
  93.  
  94. function handleMsg()
  95.   id, message  = rednet.receive()
  96.   if test(id) == false then
  97.           addLog(test(id))
  98.         if message == "request" then
  99.             addLog("Requested: "..message..", From: "..id)
  100.             sleep(2)
  101.             if state == nil then
  102.           HM = "accepted"
  103.               rednet.send(id,HM)
  104.               writeClient(id)
  105.           atmp2 = nil
  106.         elseif state == "password" then
  107.             rednet.send(id,state)
  108.             HM = "password"
  109.             atmpid, atmp2 = rednet.receive()
  110.           end
  111.         addLog("Sent "..HM)
  112.         if atmp2 == nil then
  113.           if atmp2 == pass then
  114.               rednet.send(atmpid,"accepted")
  115.               addLog("Correct Password")
  116.               writeClient(id)
  117.             else
  118.               addLog("Incorrect Pass, "..atmpid)
  119.           end
  120.           end
  121.         end
  122.     elseif test(id) == true and message == "request" then
  123.         rednet.send(id,"accepted")
  124.         addLog("Computer: "..id.." connected")
  125.     else
  126.     if message ~= "request" then
  127.           addLog(id..": "..message..", was received")
  128.           sendAll(id,id..": "..message)
  129.     end
  130.   end
  131. end
  132.  
  133. function printLog()
  134.     while true do
  135.         term.clear()
  136.         term.setCursorPos(1,1)
  137.         print("> Exit <")
  138.         if logger ~= nil then
  139.             for i in ipairs(logger) do
  140.                 term.setCursorPos(1,i+2)
  141.                 print(logger[i])
  142.             end
  143.         end
  144.         sleep(1)
  145.  
  146.     end
  147. end
  148.  
  149. function uiEvent()
  150.     event, key = os.pullEvent("key")
  151.     if key == 28 then
  152.         subexit = false
  153.         return subexit
  154.     end
  155. end
  156.  
  157. --[[function sinit()
  158.   term.clear()
  159.   screen.drawTab()
  160.   screen.drawTitle("Commands")
  161.   screen.drawMenu(shelloptions)
  162. end]]
  163.  
  164. --[[function shell()
  165.  
  166.   while srunning do
  167.     sinit()
  168.     screen.select(index2,shelloptions[index2])
  169.     event, key = os.pullEvent("key")
  170.     if key == 208 and index2 < tsize2 then--down
  171.         index2 = index2 + 1
  172.     elseif key == 200 and index2 > 1 then--up
  173.         index2 = index2 - 1
  174.     elseif key == 28 then
  175.       if index2 == 1 then -- Kick
  176.        
  177.       elseif index2 == 2 then -- Ban
  178.  
  179.       elseif index2 == 3 then -- Message
  180.        
  181.       elseif index2 == 2 then -- List clients
  182.         shell()
  183.       elseif index2 == 5 then -- Back
  184.         srunning = false
  185.         term.clear()
  186.         index = 1
  187.         init()
  188.       end
  189.     end
  190.   end
  191. end]]
  192.  
  193. function init()
  194.   term.clear()
  195.   screen.drawTab()
  196.   screen.drawTitle("Messenger Server") -- The title you want for your program
  197.   screen.drawMenu(options)
  198.   subexit = true
  199.   clientid = readClient()
  200. end
  201.  
  202. function ui ()
  203.     init()
  204.     screen.select(index,options[index])
  205.     event, key = os.pullEvent("key")
  206.     if key == 208 and index < tsize then--down
  207.         index = index + 1
  208.     elseif key == 200 and index > 1 then--up
  209.         index = index - 1
  210.     elseif key == 28 then
  211.         if index == 1 then
  212.             while subexit do
  213.                 parallel.waitForAny(uiEvent,printLog)
  214.             end
  215.         --elseif index == 2 then
  216.             --shell()
  217.         elseif index == 2 then
  218.             running = false
  219.             term.clear()
  220.             term.setTextColour(colors.red)
  221.             term.setCursorPos(1,1)
  222.             term.write("Server Shutting Down")
  223.             textutils.slowPrint(".................")
  224.             term.setTextColour(colours.white)
  225.         sendAll(hid,"Server is shutdown")
  226.         end
  227.     end
  228. end
  229.  
  230. if fs.exists("/.log") == false then
  231.    file = fs.open("/.log",'w')
  232.   file.close(file)
  233. end
  234.  
  235. if pass ~= "" then
  236.   state = "password"
  237. else
  238.   state = nil
  239. end
  240.  
  241. while running do
  242.   parallel.waitForAny(handleMsg,ui) -- Run the UI and the Logger
  243. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement