Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- version = "v0.2"
- revision = "rev160"
- -- Startup variables/functions.
- tx,ty = term.getSize()
- local cfgname = ""
- local autoafk = false
- local function process(message)
- local returned = {}
- message:gsub("{(.-)}", function(word)
- --Format: "{actiontype} {input}"
- returned[#returned+1] = word
- end)
- return unpack(returned)
- end
- local function setData(sName, bAutoafk)
- h = fs.open("ircsettings", "w")
- h.writeLine("name="..sName)
- h.writeLine("autoafk="..bAutoafk)
- h.close()
- end
- local function getData()
- returned = {}
- h = fs.open("ircsettings", "r")
- returned[#returned + 1] = h.readLine()
- returned[#returned + 1] = h.readLine()
- h.close()
- return unpack(returned)
- end
- local function printHeader(headerstring)
- term.clear()
- term.setCursorPos(1,1)
- local tx,ty = term.getSize()
- print(headerstring)
- write(" ")
- repeat
- cx,cy = term.getCursorPos()
- write("-")
- until cx == tx - 1
- term.setCursorPos(1,3)
- end
- -- Startup.
- printHeader("xIRC Client "..version.." "..revision.." - Setup")
- write("Searching for a modem.")
- textutils.slowWrite("...", 1)
- sleep(1)
- term.setCursorPos(1,4)
- for k,v in ipairs(rs.getSides()) do
- if peripheral.getType(v) == "modem" then
- print("Found a modem. Enabling rednet.")
- modemside = v
- sleep(0.3)
- break
- end
- end
- if modemside == nil then
- print("Failed to find a modem.")
- print("This requires a modem to function correctly.")
- print("Terminating.")
- sleep(3)
- term.clear()
- term.setCursorPos(1,1)
- susuccess = false
- return
- else
- rednet.open(modemside)
- end
- if not fs.exists("ircsettings") then
- printHeader("xIRC Client "..version.." "..revision.." - Setup")
- print("You don't have any user data on this computer.")
- print("Create user data?")
- print("[Enter] = Yes; [Backspace] = No;")
- while true do
- event, key = os.pullEvent("key")
- if key == 28 then --enter
- local afkinput = ""
- local nameinput = ""
- print("Enter your name for this computer.")
- cx,cy = term.getCursorPos()
- write(">> ")
- nameinput = io.read()
- term.setCursorPos(1,cy + 1)
- print("Allow AutoAFK?")
- print("[Enter] = Yes; [Backspace] = No;")
- while true do
- event, key = os.pullEvent("key")
- if key == 28 then
- print("AutoAFK is enabled.")
- afkinput = "true"
- autoafk = true
- break
- elseif key == 14 then
- print("AutoAFK is disabled.")
- afkinput = "false"
- autoafk = true
- break
- end
- end
- cfgname = nameinput
- sleep(0.3)
- setData(nameinput,afkinput)
- break
- elseif key == 14 then --backspace
- print("Skipping data creation.")
- print("You still need a name, though.")
- cx,cy = term.getCursorPos()
- write(">> ")
- cfgname = io.read()
- autoafk = true
- term.setCursorPos(1,cy + 1)
- sleep(0.3)
- break
- end
- end
- else
- name,afk = getData()
- cfgname = string.sub(name, 6)
- if string.sub(afk, 9) == "true" then
- autoafk = true
- else
- autoafk = false
- end
- end
- printHeader("xIRC Client "..version.." "..revision.." - Setup")
- print("Finding you a server...")
- local timeout = os.startTimer(1)
- rednet.broadcast("{finder} {}")
- local servers = {}
- local ids = {}
- while true do
- event, sender, message = os.pullEvent()
- if event == "timer" and sender == timeout then
- break
- elseif event == "rednet_message" then
- actiontype, ainput = process(message)
- ids["["..tostring(sender).."]"] = {}
- ids["["..tostring(sender).."]"].name = actiontype
- ids["["..tostring(sender).."]"].isHidden = ainput
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- local count = 0
- for k,v in pairs(ids) do
- if v.isHidden == "false" then
- count = count + 1
- if count >= ty - 4 then
- term.setCursorPos(tx / 2, (k - ty) + 1)
- write(k.." "..v.name)
- else
- print(k.." "..v.name)
- end
- end
- end
- term.setCursorPos(1,ty - 3)
- print("Enter the ID of the server to join; \"exit\" to leave; Reload by restarting the program.")
- write(">> ")
- while true do
- input2 = read()
- input2 = "["..input2.."]"
- term.setCursorPos(1, ty - 1)
- term.clearLine()
- write(">> ")
- if input2 == "[exit]" then
- print("Exited selection.")
- return
- elseif ids[input2] ~= nil then
- break
- end
- end
- local failed = false
- term.clear()
- term.setCursorPos(1,1)
- write("Attempting to join "..ids[input2].name..".")
- textutils.slowWrite("...", 1)
- sleep(3)
- local input = 0
- input2:gsub("[(.-)]", function(word)
- input = word
- end)
- rednet.send(tonumber(input), "{join} {"..cfgname.."}")
- while true do
- sender,message = rednet.receive(3)
- if sender == nil or sender == "" then
- print("[Sys> Failed to connect. Read timed out.")
- failed = true
- break
- else
- action, ainput = process(message)
- if action == "result" then
- if ainput == "j-success" then
- failed = false
- break
- elseif ainput == "nameconflict" then
- print("[Sys> Someone is online with your name.")
- break
- elseif ainput == "idconflict" then
- print("[Sys> The ID of this computer is already connected.")
- break
- elseif ainput == "banned" then
- print("[Sys> This computer, or this name, is banned from this server.")
- break
- end
- end
- end
- end
- if failed == true then
- return
- end
- local host = tonumber(input)
- -- Interior variables/functions.
- local lastmessage = ""
- local centered = 0
- local entered = ""
- local messages = 1
- local function redrawInput()
- term.setCursorPos(1, ty)
- term.clearLine()
- write(">> "..entered)
- term.setCursorPos(string.len(entered) + 4, ty)
- end
- --[[v1 = 1 + (tx * lines)
- v2 = (tx * lines) + tx]]--
- local function redrawScreen(lastmsg)
- messagelines = string.len(input) / tx
- lines = math.floor(messagelines)
- n1 = string.len(lastmsg) / tx
- n2 = math.floor(n1) + 1
- messages = messages + n2
- cx,cy = term.getCursorPos()
- term.setCursorPos(1,ty)
- term.clearLine()
- term.setCursorPos(1,cy)
- print(lastmsg)
- redrawInput()
- end
- -- Interior.
- term.setCursorBlink(true)
- redrawInput()
- redrawScreen("[Sys> Successfully joined.")
- while true do
- event, p1, p2 = os.pullEventRaw()
- if event == "char" then
- cx,cy = term.getCursorPos()
- if cx ~= tx then
- entered = entered..p1
- centered = centered + 1
- end
- redrawInput()
- elseif event == "key" then
- if p1 == 28 then --enter
- local invalid = false
- local testinput = ""
- for word in string.gmatch(entered, "[^%s]+") do
- testinput = testinput..word
- end
- if testinput == "" or testinput == nil then
- invalid = true
- end
- if invalid == false then
- term.setCursorPos(1, messages + 1)
- term.clearLine()
- if string.sub(entered, 1, 1) == "/" then
- commandinput = string.sub(entered, 2, string.len(entered))
- if string.lower(string.sub(commandinput, 1, 5)) == "leave" then
- rednet.send(tonumber(host), "{leave} {}")
- term.clear()
- term.setCursorPos(1,1)
- print("[Sys> Exited server.")
- return
- end
- success = rednet.send(tonumber(host), "{command} {"..commandinput.."}")
- else
- success = rednet.send(tonumber(host), "{message} {"..entered.."}")
- end
- end
- term.setCursorPos(1, ty)
- term.clearLine()
- write(">> ")
- centered = 0
- entered = ""
- if success == false then
- redrawScreen("[Sys> Lost connection to the server.")
- end
- elseif p1 == 14 then --backspace
- if entered ~= "" then
- centered = centered - 1
- entered = entered:sub(1, string.len(entered) - 1)
- redrawInput()
- end
- end
- elseif event == "rednet_message" then
- local sender = p1
- local message = p2
- if sender == host then
- actiontype,ainput = process(message)
- if actiontype == "message" then
- if lastmessage ~= ainput then
- lastmessage = ainput
- messages = messages + 1
- redrawScreen(ainput)
- end
- elseif actiontype == "kick" then
- redrawScreen("[Sys> You have been kicked out of the server.")
- redrawScreen("[Sys> Reason: "..ainput)
- term.setCursorPos(1,ty)
- term.clearLine()
- return
- elseif actiontype == "ban" then
- redrawScreen("[Sys> You have been banned from the server.")
- redrawScreen("[Sys> Reason: "..ainput)
- term.setCursorPos(1,ty)
- term.clearLine()
- return
- elseif actiontype == "terminate" then
- redrawScreen("[Sys> Server shutting down.")
- sleep(0.3)
- term.clear()
- term.setCursorPos(1,1)
- print("[Sys> Lost connection to the server.")
- rednet.close(modemside)
- return
- end
- end
- elseif event == "terminate" then
- rednet.send(tonumber(host), "{leave} {}")
- rednet.close(modemside)
- term.clear()
- term.setCursorPos(1,1)
- print("[Sys> Exited server.")
- return
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement