Advertisement
Xylem_Gaming

Messenger (ServerMethod)

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