Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --MESSENGER CLIENT
- bexit = false
- id = os.getComputerID()
- x,y = term.getSize()
- i = 3
- local function openRednet()
- for _,side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == 'modem' then
- rednet.open(side)
- return(side)
- end
- end
- print("No modem present!")
- end
- function getid()
- if fs.exists("/.serverid") == true then
- file = fs.open("/.serverid")
- serverid = file.readAll()
- file.close()
- return true
- end
- return false
- end
- function slow(text,h)
- w = math.floor((x-string.len(text))/2)
- term.setCursorPos(w,h)
- textutils.slowPrint(text)
- end
- function drawTab()
- term.setTextColor(colors.yellow)
- --clear()
- term.setCursorPos(1,1)
- term.clearLine()
- print("Computer ID "..id)
- term.setCursorPos(1,2)
- term.clearLine()
- print("__________________________________________________")
- term.setCursorPos(1,y-3)
- term.clearLine()
- print("__________________________________________________")
- term.setTextColor(colors.white)
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function input()
- term.setTextColor(colors.white)
- term.setCursorPos(1,y-1)
- local message2 = read()
- if message2 == "exit" then
- --bexit = true
- clear()
- term.setTextColour(colours.orange)
- print("Thanks for using messenger by Xylem")
- else
- term.setCursorPos(1,y-1)
- term.clearLine()
- win("Me: "..message2)
- rednet.send(serverid,message2)
- end
- end
- function listen()
- id2,message = rednet.receive()
- if id2 == sid and message == "Server is shutdown" then
- --bexit = true
- clear()
- print("Server "..sid.." has shutdown been by host")
- else
- win(message)
- end
- end
- function win(text)
- if i > y-5 or i==y-5 then
- term.setCursorPos(1,y-3)
- term.clearLine()
- term.setCursorPos(1,4)
- term.clearLine()
- term.scroll(1)
- drawTab()
- term.setCursorPos(1,y-5)
- print(text)
- else
- term.setCursorPos(1,1+i)
- print(text)
- i = i+1
- end
- end
- modemSide = openRednet()
- if modemSide == nil then
- print("No modem connected")
- else
- print("Opened modem on "..modemSide.."!")
- end
- --Authenticator
- if serverid == nil then
- bexit = false
- term.clear()
- --Intro
- term.setCursorPos(1,1)
- term.setTextColor(colours.yellow)
- term.write("Enter server ID: ")
- term.setTextColor(colours.white)
- --Input from intro to show the server id
- sinput = tonumber(read())
- rednet.send(tonumber(sinput),"request")
- --Recieve authentication
- sid, request = rednet.receive()
- --SID is temporary to see if authentication is fullfilled
- if request == "accepted" then -- No password set
- term.clear()
- term.setTextColor(colours.green)
- term.setCursorPos(1,1)
- print("Connected")
- sleep(1)
- rednet.send(tonumber(sinput),"connected")
- serverid = sid -- serverid is now established
- elseif request == "password" then -- Password set
- term.setTextColour(colours.yellow)
- term.write("Enter Password: ")
- term.setTextColour(colours.white)
- passatmp = read("*")
- rednet.send(sid,passatmp)
- id3,request2 = rednet.receive()
- if request2 == "denied" then -- Rejected request
- term.clear()
- .sapi.printColour(red)
- print("Incorrect Password")
- sleep(1)
- bexit = true -- Cancel program
- elseif request2 == "accepted" then -- Accepted request
- term.clear()
- .sapi.printColour(green,"Correct Password",1,1)
- sleep(1)
- serverid = sid
- end
- elseif request == nil then
- term.clear()
- .sapi.printColour(orange,"Error",1,1)
- bexit = true -- Cancel program
- end
- end
- --Main method
- if modemSide ~= nil then
- sid, request = rednet.send(serverid,"rejoin",10)
- --[[if sid == nil and request == nil then
- bexit = true
- clear()
- print("Server is currently down")
- end]]
- clear()
- drawTab()
- while bexit == false do
- parallel.waitForAny(input,listen)
- end
- else
- print("No modem was found. Please place a modem ")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement