Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- options = {"Log","Exit"}--S1
- --shelloptions = {"Kick","Ban","Message","List Clients","Back"}--S2
- --Screen Options
- tsize = table.getn(options)
- hid = os.getComputerID()
- --tsize2 = table.getn(shelloptions)
- --Function variables
- running = true
- srunning = true
- subexit = true
- --Booleans
- index = 1
- index2 = 1
- unindex = 1
- --Integers
- pass = "test" -- Password set here
- --Strings
- os.loadAPI("/screen")
- rednet.open("top")
- --Initalised Varibles
- state = nil
- logger = {}
- --Misc Variables
- local function addLog(element)
- logger[unindex] = element
- unindex = unindex + 1
- end
- function writeClient(idno)
- file = fs.open(".log",'w')
- if clientid == nil then
- clientid = {}
- clientid[1] = idno
- temp2 = textutils.serialise(clientid)
- file.write(temp2)
- else
- maxn = table.getn(clientid)
- clientid[maxn+1] = idno
- temp2 = textutils.serialise(clientid)
- file.write(temp2)
- end
- file.close()
- end
- function readClient()
- file = fs.open(".log",'r')
- clientid = textutils.unserialise(file.readAll())
- file.close()
- return clientid
- end
- function sendAll(senderid, text)
- for i in ipairs(clientid) do
- if senderid ~= clientid[i] then
- rednet.send(clientid[i],text)
- end
- end
- end
- function test(variable)
- if clientid ~= nil then
- for i in ipairs(clientid) do
- if variable == clientid[i] then
- return true
- elseif i == table.maxn(clientid) then
- return false
- end
- end
- else
- return false
- end
- end
- clientid = readClient()
- if clientid ~= nil then
- addLog("Saved IDs :")
- for i in ipairs(clientid) do
- addLog(clientid[i]..",")
- end
- else
- addLog("No saved IDs")
- end
- function handleMsg()
- id, message = rednet.receive()
- if test(id) == false then
- addLog(test(id))
- if message == "request" then
- addLog("Requested: "..message..", From: "..id)
- sleep(2)
- if state == nil then
- HM = "accepted"
- rednet.send(id,HM)
- writeClient(id)
- atmp2 = nil
- elseif state == "password" then
- rednet.send(id,state)
- HM = "password"
- atmpid, atmp2 = rednet.receive()
- end
- addLog("Sent "..HM)
- if atmp2 == nil then
- if atmp2 == pass then
- rednet.send(atmpid,"accepted")
- addLog("Correct Password")
- writeClient(id)
- else
- addLog("Incorrect Pass, "..atmpid)
- end
- end
- end
- elseif test(id) == true and message == "request" then
- rednet.send(id,"accepted")
- addLog("Computer: "..id.." connected")
- else
- if message ~= "request" then
- addLog(id..": "..message..", was received")
- sendAll(id,id..": "..message)
- end
- end
- end
- function printLog()
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("> Exit <")
- if logger ~= nil then
- for i in ipairs(logger) do
- term.setCursorPos(1,i+2)
- print(logger[i])
- end
- end
- sleep(1)
- end
- end
- function uiEvent()
- event, key = os.pullEvent("key")
- if key == 28 then
- subexit = false
- return subexit
- end
- end
- --[[function sinit()
- term.clear()
- screen.drawTab()
- screen.drawTitle("Commands")
- screen.drawMenu(shelloptions)
- end]]
- --[[function shell()
- while srunning do
- sinit()
- screen.select(index2,shelloptions[index2])
- event, key = os.pullEvent("key")
- if key == 208 and index2 < tsize2 then--down
- index2 = index2 + 1
- elseif key == 200 and index2 > 1 then--up
- index2 = index2 - 1
- elseif key == 28 then
- if index2 == 1 then -- Kick
- elseif index2 == 2 then -- Ban
- elseif index2 == 3 then -- Message
- elseif index2 == 2 then -- List clients
- shell()
- elseif index2 == 5 then -- Back
- srunning = false
- term.clear()
- index = 1
- init()
- end
- end
- end
- end]]
- function init()
- term.clear()
- screen.drawTab()
- screen.drawTitle("Messenger Server") -- The title you want for your program
- screen.drawMenu(options)
- subexit = true
- clientid = readClient()
- end
- function ui ()
- init()
- screen.select(index,options[index])
- event, key = os.pullEvent("key")
- if key == 208 and index < tsize then--down
- index = index + 1
- elseif key == 200 and index > 1 then--up
- index = index - 1
- elseif key == 28 then
- if index == 1 then
- while subexit do
- parallel.waitForAny(uiEvent,printLog)
- end
- --elseif index == 2 then
- --shell()
- elseif index == 2 then
- running = false
- term.clear()
- term.setTextColour(colors.red)
- term.setCursorPos(1,1)
- term.write("Server Shutting Down")
- textutils.slowPrint(".................")
- term.setTextColour(colours.white)
- sendAll(hid,"Server is shutdown")
- end
- end
- end
- if fs.exists("/.log") == false then
- file = fs.open("/.log",'w')
- file.close(file)
- end
- if pass ~= "" then
- state = "password"
- else
- state = nil
- end
- while running do
- parallel.waitForAny(handleMsg,ui) -- Run the UI and the Logger
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement