Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---- Check arguments.
- local tArgs = { ... }
- local version = "v0.2"
- local revision = "rev130"
- if #tArgs > 3 or #tArgs < 1 then
- print("Usage: ircserver <servername> <hidden [true/false]>")
- return
- end
- if tArgs[2] ~= "true" and tArgs[2] ~= "false" then
- print("Usage: ircserver <servername> <hidden [true/false]>")
- return
- end
- ---- Variables and functions for startup.
- local susuccess = true
- local monitor
- local modemside
- local monitorside
- local serverpassword = ""
- local servername = tArgs[1]
- local hidden = tArgs[2]
- 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 Server "..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
- 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
- write("Searching for a monitor.")
- textutils.slowWrite("...", 1)
- sleep(1)
- term.setCursorPos(1,6)
- for k,v in ipairs(rs.getSides()) do
- if peripheral.getType(v) == "monitor" then
- local monitor
- monitor = peripheral.wrap(v)
- tx, ty = monitor.getSize()
- if tx < 45 or ty < 11 then
- print("Found a monitor, but is too small.")
- print("Minimum size for a monitor is 4x3.")
- break
- else
- print("Found a monitor. Utilizing for logging.")
- monitorside = v
- break
- end
- end
- end
- if monitorside == nil then
- print("Failed to find a monitor.")
- print("Disabling chat display.")
- end
- print("Enter the password to access the server panel.")
- write(">> ")
- cx,cy = term.getCursorPos()
- serverpassword = read("*")
- term.setCursorPos(1, cy + 1)
- write("Setup complete, finalizing.")
- textutils.slowWrite("...", 1)
- if susuccess == false then
- return
- end
- sleep(1)
- ---- Interior Variables and Functions.
- -- Command registration.
- local commands = {}
- local commandusage = {}
- commands["help"] = "Show all commands or help for one command."
- commands["leave"] = "Leave the chat server."
- commands["online"] = "Show who's online."
- commands["realname"] = "Gets a person's real name."
- commands["nick"] = "Get an alternate name."
- commands["me"] = "Send your message in a 3rd person POV."
- commands["poke"] = "Poke a chatter."
- commands["afk"] = "Alert the server that you're AFK."
- commands["id"] = "Get your computer's, or someone else's Computer ID."
- commands["login"] = "Acknowledge your operator position."
- commands["kick"] = "[A] Kick a person from the server."
- commands["ban"] = "[A] Prevent entry of a computer."
- commands["unban"] = "[A] Allow entry of a banned computer."
- commands["mute"] = "[A] Enable/Disable a chatter's speech."
- commands["op"] = "[PA] Allow another chatter to become a server operator."
- commands["deop"] = "[PA] Remove a chatter's position from operator."
- commands["stop"] = "[PA] Terminate the server."
- commandusage["help"] = "/help [command]"
- commandusage["leave"] = "/leave"
- commandusage["online"] = "/online"
- commandusage["realname"] = "/realname <target's nick>"
- commandusage["nick"] = "/nick <new name>"
- commandusage["me"] = "/me <message>"
- commandusage["poke"] = "/poke <target>"
- commandusage["afk"] = "/afk"
- commandusage["id"] = "/id <target>"
- commandusage["login"] = "/login <password>"
- commandusage["kick"] = "/kick <target>"
- commandusage["ban"] = "/ban <target>"
- commandusage["mute"] = "/mute <target>"
- commandusage["op"] = "/op <target>"
- commandusage["deop"] = "/deop <target>"
- commandusage["stop"] = "/stop"
- --
- local banlist = {}
- local connected = {}
- local current = 0
- local function validateCmd(command)
- local isCorrect = false
- for k,v in pairs(commands) do
- if k == command then
- isCorrect = true
- break
- end
- end
- return isCorrect
- end
- local function process(message)
- local returned = {}
- message:gsub("{(.-)}", function(word)
- --Format: "{actiontype} {input}"
- returned[#returned+1] = word
- end)
- return unpack(returned)
- end
- local function sendAction(target, actiontype, input)
- if connected[target] ~= nil then
- rednet.send(tonumber(connected[target].id), "{"..actiontype.."} {"..input.."}")
- end
- end
- local function broadcastAction(actiontype, input)
- for k,v in pairs(connected) do
- rednet.send(tonumber(v.id), "{"..actiontype.."} {"..input.."}")
- end
- end
- local function broadcast(message)
- for k,v in pairs(connected) do
- rednet.send(tonumber(v.id), "{message} {"..message.."}")
- end
- if monitorside ~= nil then
- print(message)
- end
- end
- local function sendMessage(target, message)
- if connected[target] ~= nil then
- rednet.send(tonumber(connected[target].id), "{message} {"..message.."}")
- end
- end
- local function getChatterData(sName)
- local returned = {}
- for k,v in pairs(connected) do
- if string.lower(v.name) == string.lower(sName) then
- returned = v
- success = true
- break
- end
- end
- return returned;
- end
- local function getCommandType(command)
- local cmdtype = ""
- if command == "help" or command == "leave" or command == "nick" or command == "me" or command == "realname" or command == "poke" or command == "afk" or command == "id" or command == "login" then
- cmdtype = "normal"
- elseif command == "kick" or command == "ban" or command == "unban" or command == "mute" then
- cmdtype = "mod"
- elseif command == "op" or command == "deop" or command == "stop" then
- cmdtype = "pmod"
- end
- return cmdtype
- end
- ---- Interior/Shutdown.
- printHeader("xIRC Server "..version.." "..revision.." - Setup")
- print("Your server is now ready.")
- print("To access the panel, go on the server with a client and use /login <password>, the password being the one you entered during setup.")
- if monitorside ~= nil then
- monitor = peripheral.wrap(monitorside)
- term.redirect(monitor)
- term.clear()
- term.setCursorPos(1,1)
- term.setCursorBlink(true)
- end
- while true do
- event, sender, message = os.pullEventRaw()
- if event == "rednet_message" then
- local ssender = "-"..sender.."-"
- local isOnline = true
- local isNameDuplicate = false
- local isBanned = false
- local actiontype,input = process(message)
- if connected[ssender] == nil then
- isOnline = false
- end
- for k,v in pairs(connected) do
- if actiontype == "join" and input == tostring(v.name) then
- isNameDuplicate = true
- end
- end
- for k,v in pairs(banlist) do
- if k == input then
- isBanned = true
- end
- end
- if connected[ssender] ~= nil then
- if connected[ssender].isAfk == true then
- connected[ssender].isAfk = false
- broadcast("[Sys> "..connected[ssender].nick.." is no longer AFK.")
- end
- end
- if actiontype == "finder" then
- rednet.send(sender, "{"..servername.."} {"..hidden.."}")
- elseif actiontype == "join" then
- if isNameDuplicate == true then
- sendAction(ssender, "result", "nameconflict")
- elseif isOnline == true then
- sendAction(ssender, "result", "idconflict")
- elseif isBanned == true then
- sendAction(ssender, "result", "banned")
- else
- rednet.send(sender, "{result} {j-success}")
- connected[ssender] = {}
- connected[ssender].name = input
- connected[ssender].nick = input
- connected[ssender].id = sender
- connected[ssender].chatop = "none"
- connected[ssender].muted = false
- connected[ssender].isAfk = false
- broadcast("[Sys> "..connected[ssender].name.." joined the chat.")
- local sent = "[Sys> Online: "
- for k,v in pairs(connected) do
- sent = sent..connected[k].name..", "
- end
- sendMessage(ssender, string.sub(sent, 1, -3)..".")
- end
- elseif actiontype == "leave" then
- if isOnline == true then
- broadcast("[Sys> "..connected[ssender].name.." left the chat.")
- connected[ssender] = nil
- end
- elseif actiontype == "message" then
- if isOnline == true and connected[ssender].muted == false then
- broadcast("["..connected[ssender].nick.."> "..input)
- else
- sendMessage(ssender, "[Sys> You are muted, and unable to speak.")
- end
- elseif actiontype == "command" then
- if isOnline == true then
- local command
- local args = {}
- local count = 1
- local isMod = false
- local isPMod = false
- for word in input:gmatch("[%a%d]+") do
- if count == 1 then
- command = tostring(word)
- count = count + 1
- else
- args[count-1] = word
- count = count + 1
- end
- end
- if connected[ssender].chatop == "m" or connected[ssender].chatop == "pm" then
- isMod = true
- end
- if connected[ssender].chatop == "pm" then
- isPMod = true
- end
- local commandtype = getCommandType(command)
- if validateCmd(command) ~= true then
- sendMessage(ssender, "[Sys> That command doesn't exist.")
- end
- if command == "help" then
- if #args == 0 then
- sendMessage(ssender, "[Sys> Possible commands: ")
- sendMessage(ssender, "[Sys> help, leave, nick, me,")
- sendMessage(ssender, "[Sys> realname, poke, afk, id,")
- sendMessage(ssender, "[Sys> login, kick, ban, unban,")
- sendMessage(ssender, "[Sys> online, mute, op, deop, stop")
- sendMessage(ssender, "[Sys> To know how to use these commands, type /help <command>.")
- elseif #args == 1 then
- if validateCmd(args[1]) == true then
- sendMessage(ssender, "[Sys> "..commands[args[1]])
- else
- sendMessage(ssender, "[Sys> That command doesn't exist.")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "nick" then
- if #args == 1 then
- connected[ssender].nick = args[1]
- broadcast("[Sys> "..connected[ssender].name.."'s new display name is "..connected[ssender].nick..".")
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "realname" then
- if #args == 1 then
- if getChatterData(args[1]) ~= nil then
- sendMessage(ssender, "[Sys> "..connected[ssender].nick.."'s real name is "..connected[ssender].name..".")
- else
- sendMessage(ssender, "[Sys> That player doesn't exist, or does not have data.")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "me" then
- local sent = ""
- for k,v in ipairs(args) do
- sent = sent.." "..v
- end
- broadcast("* "..connected[ssender].nick..sent)
- elseif command == "online" then
- local sent = "[Sys> Online: "
- for k,v in pairs(connected) do
- sent = sent..connected[k].name..", "
- end
- sendMessage(ssender, string.sub(sent, 1, -3)..".")
- elseif command == "poke" then
- if #args == 1 then
- if getChatterData(args[1]) ~= nil then
- sendMessage("-"..getChatterData(args[1]).id.."-", "[Sys> "..connected[ssender].nick.." poked you.")
- sendMessage(ssender, "[Sys> You poked "..getChatterData(args[1]).nick..".")
- else
- sendMessage(ssender, "[Sys> That player doesn't exist, or does not have data.")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "afk" then
- if connected[ssender].isAfk == false then
- broadcast("[Sys> "..connected[ssender].nick.." went AFK.")
- connected[ssender].isAfk = true
- else
- broadcast("[Sys> "..connected[ssender].nick.." is no longer AFK.")
- connected[ssender].isAfk = false
- end
- elseif command == "id" then
- if #args < 2 then
- if #args == 1 then
- if getChatterData(args[1]) ~= nil then
- sendMessage(ssender, "[Sys> "..getChatterData(args[1]).nick.."'s computer ID is "..tostring(getChatterData(args[1]).id)..".")
- else
- sendMessage(ssender, "[Sys> That player doesn't exist, or does not have data.")
- end
- else
- sendMessage(ssender, "[Sys> Your computer ID is "..tostring(connected[ssender].id)..".")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "login" then
- if #args == 1 then
- if args[1] == serverpassword then
- broadcast("[Sys> "..connected[ssender].nick.." is now registered as a server primary operator.")
- connected[ssender].chatop = "pm"
- else
- sendMessage(ssender, "[Sys> Incorrect password.")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- end
- if isMod == true then
- if command == "ban" then
- if #args == 1 then
- if getChatterData(args[1]) ~= nil then
- local sent = ""
- for k,v in ipairs(args) do
- if #args < 2 then
- break
- end
- if k ~= 1 then
- sent = sent.." "..v
- end
- end
- if getChatterData(args[1]).chatop ~= "pm" and getChatterData(args[1]).chatop ~= "m" then
- banlist[getChatterData(args[1]).name] = getChatterData(args[1]).id
- sendAction("-"..getChatterData(args[1]).id.."-", "ban", sent)
- broadcast("[Sys> "..getChatterData(args[1]).name.." has been banned from the server.")
- broadcast("[Sys> Reason:"..sent)
- connected["-"..getChatterData(args[1]).id.."-"] = nil
- else
- sendMessage(ssender, "[Sys> You may not ban other operators.")
- end
- else
- sendMessage(ssender, "[Sys> That player doesn't exist, or does not have data.")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "unban" then
- if #args == 1 then
- local success = false
- for k,v in ipairs(banlist) do
- if k == args[1] then
- banlist[k] = nil
- success = true
- end
- end
- if success == true then
- broadcast("[Sys> "..args[1].."'s ban has been lifted.")
- else
- sendMessage(ssender, "[Sys> Unable to find ban data.")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "kick" then
- if #args == 1 then
- if getChatterData(args[1]) ~= nil then
- local sent = ""
- for k,v in ipairs(args) do
- if #args < 2 then
- break
- end
- if k ~= 1 then
- sent = sent.." "..v
- end
- end
- if getChatterData(args[1]).chatop ~= "pm" and getChatterData(args[1]).chatop ~= "m" then
- sendAction("-"..getChatterData(args[1]).id.."-", "kick", sent)
- broadcast("[Sys> "..getChatterData(args[1]).name.." has been kicked from the server.")
- broadcast("[Sys> Reason:"..sent)
- connected["-"..getChatterData(args[1]).id.."-"] = nil
- else
- sendMessage(ssender, "[Sys> You may not kick other operators.")
- end
- else
- sendMessage(ssender, "[Sys> That player doesn't exist, or does not have data.")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "mute" then
- if #args == 1 then
- if getChatterData(args[1]) ~= nil then
- else
- sendMessage(ssender, "[Sys> That player doesn't exist, or does not have data.")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- end
- end
- if isPMod == true then
- if command == "op" then
- if #args == 1 then
- if getChatterData(args[1]) ~= nil then
- if getChatterData(args[1]).chatop == "pm" then
- sendMessage(ssender, "[Sys> A Primary Operator is the highest op rank. You do not need to promote this person.")
- else
- getChatterData(args[1]).chatop = "m"
- sendMessage(ssender, "[Sys> Successfully promoted "..getChatterData(args[1]).name.." permissions.")
- end
- else
- sendMessage(ssender, "[Sys> Failed to promoted permissions. Is the target online?")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "deop" then
- if #args == 1 then
- if getChatterData(args[1]) ~= nil then
- if getChatterData(args[1]).chatop == "pm" then
- sendMessage(ssender, "[Sys> You cannot revoke permissions of a Primary Operator.")
- else
- getChatterData(args[1]).chatop = "none"
- sendMessage(ssender, "[Sys> Successfully revoked "..getChatterData(args[1]).name.." permissions.")
- end
- else
- sendMessage(ssender, "[Sys> Failed to revoke permissions. Is the target online?")
- end
- else
- sendMessage(ssender, "[Sys> Arguments insufficient or overflowing.")
- end
- elseif command == "stop" then
- broadcastAction("terminate", "")
- for k,v in ipairs(rs.getSides()) do
- rednet.close(v)
- end
- term.clear()
- term.restore()
- term.clear()
- term.setCursorPos(1,1)
- print("[Sys> Server terminated.")
- return
- end
- end
- if getCommandType(command) == "mod" then
- if isMod == false then
- sendMessage(ssender, "[Sys> This command requires Operator permissions.")
- end
- elseif getCommandType(command) == "pmod" then
- if isPMod == false and isMod == false then
- sendMessage(ssender, "[Sys> This command requires Primary Operator permissions.")
- end
- end
- end
- end
- elseif event == "terminate" then
- broadcastAction("terminate", "")
- for k,v in ipairs(rs.getSides()) do
- rednet.close(v)
- end
- term.clear()
- term.restore()
- term.clear()
- term.setCursorPos(1,1)
- print("[Sys> Server terminated.")
- return
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement