Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- StarDial+ 1.0
- Made by: MudkipTheEpic and the helpful people on
- MudServ.
- Commands:
- (all are prefixed with "$$")
- dial address OR registeredname (Connects the stargate to that address.)
- disconnect (Disconnects the stargate)
- register name address (Makes you be able yo use commands with that name instead of the address.)
- lockdown (Turns on the ability to disconnect all connections you didn't create.)
- unlock (Undoes lockdown)
- deregister name (Unregisters name)
- whitelist on (Only allows people you select to connect to you.)
- (NOTE: YOU can still dial to anyone.)
- whitelist off (Disables whitelist)
- whitelist add address OR name (Adds them to the whitelist)
- whitelist remove address OR name (Removes them from the whitelist)
- Suggest more to add! Enjoy the program!
- -Mudkip
- --]]
- local runShellInBackground=true --Set to false to have less lag, but no shell prompt.
- local disconnectSelf=false
- local function wrap(typ)
- for k,v in pairs(peripheral.getNames()) do
- if peripheral.getType(v)==typ then
- return v
- end
- end
- return nil
- end
- local glasses=wrap("terminal_glasses_bridge")
- local stargate=wrap("stargate_base")
- if not (glasses and stargate) then
- error("StarDial+ needs a stargate and terminal glasses bridge to function.",0)
- end
- local sg=peripheral.wrap(stargate)
- sg.isConnected=function() return not sg.getDialledAddress()=="" end
- sg.getDialedAddress=sg.getDialedAddress or sg.getDialledAddress
- sg.isInitiator=function() return peripheral.call(stargate,"isInitiator")=="true" end
- local displayExtra
- local _,address=pcall(peripheral.call,stargate,"getHomeAddress")
- local bridge=peripheral.wrap(glasses)
- bridge.clear()
- local l=bridge.addText(12,12,"Say $$dial (address) to dial.",0x000000)
- l.setScale(1.75)
- l.setColor(0x3366FF)
- --print()
- --print(l.getHeight)
- local h=bridge.addBox(1,1,45*6,36,0x999999,0.5)
- h.setZIndex(4)
- l.setZIndex(1)
- local registered={}
- if fs.exists(".register") then
- local l=fs.open(".register","r")
- registered=textutils.unserialize(l.readAll())
- l.close()
- end
- if not registered then fs.delete(".register") registered={} end
- local whitelist={enabled=false}
- if fs.exists(".whitelist") then
- local l=fs.open(".whitelist","r")
- whitelist=textutils.unserialize(l.readAll())
- l.close()
- end
- if not whitelist then fs.delete(".whitelist") whitelist={enabled=false} end
- --local function split(str,pat)
- -- local fpat="(.+)"..pat
- -- if not str:find(pat) then
- -- return {str}
- -- end
- -- local t={}
- -- for c in str:gmatch(fpat) do
- -- table.insert(t,c)
- -- end
- -- return t
- --end
- -- This might be easier
- local function split(str,pat)
- -- local fpat="(.+)"..pat -- lol we seem to have arrived at the exact same function :3 Meh ill leave it
- local tArgs = {}
- for _arg in str:gmatch("%S+") do -- Hmm, well try diff patterns, %w+ I'm sure has worked for me before though, although you can't change the deliminator. Thats easy then
- table.insert(tArgs, _arg)
- end
- return tArgs
- end
- local displayT={}
- local function disconnect(t) t=t or 2 local ok = pcall(peripheral.call,stargate,"disconnect") and t~=0 and displayExtra("Disconnected from gate.",t) end
- local function connect(address) local e= pcall(peripheral.call,stargate,"connect",address) if e then displayExtra("Connected succesfully.") else displayExtra("Could not connect.") end return e end
- local function startsWith(str,pre) return str:sub(1,#pre)==pre end
- local function saveRegister() local l=fs.open(".register","w") l.write(textutils.serialize(registered)) l.close() return true end
- local function getName(address) local l=nil for k,v in pairs(registered) do if v==address then l=k break end end return l end
- local function saveWhitelist() local l=fs.open(".whitelist","w") l.write(textutils.serialize(whitelist)) l.close() return true end
- function displayExtra(text,time)
- table.insert(displayT,{text,time})
- os.queueEvent("display")
- end
- local updateDisplay=displayExtra
- function display(text,time)
- if time and time<0.05 then return end
- time=time or 2
- for i=1,16 do h.setHeight(36+i) sleep(0.05) end
- local extra=bridge.addText(1,42,text,0x00CCFF)
- extra.setScale(1)
- sleep(time)
- extra.delete()
- for i=1,16 do h.setHeight(52-i) sleep(0.05) end
- return true
- end
- local commandTable={
- dial=function(address)
- disconnect(0)
- sleep(1)
- return connect(registered[address] or address)
- end,
- whitelist=function(cmd,opt)
- if opt then opt=registered[opt] or opt end
- if cmd=="on" then
- if whitelist["enabled"] then displayExtra("Already whitelisted.") else displayExtra("Whitelist on.") whitelist.enabled=true end
- elseif cmd=="off" then
- if not whitelist.enabled then displayExtra("Already disabled.") else displayExtra("Unwhitelisting...") whitelist.enabled=false end
- elseif cmd=="add" and opt then
- whitelist[opt]=true
- displayExtra("Added "..opt.." to whitelist.")
- elseif cmd=="remove" and opt then
- whitelist[opt]=false
- displayExtra("Removed "..opt.."from the whitelist.")
- end
- end,
- disconnect=function()
- if sg.getDialledAddress()=="" and not sg.isInitiator() then disconnected=true
- elseif sg.getDialledAddress()=="" then displayExtra("Already disconnected.") return false end
- disconnect(0)
- end,
- register=function(name,address)
- registered[name]=address
- saveRegister()
- displayExtra("Registered "..name.." as "..address..".")
- return name
- end,
- lockdown=function()
- if not locked then
- displayExtra("Locked down.")
- locked=true
- else
- displayExtra("Already locked.")
- end
- end,
- unlock=function()
- if locked then
- displayExtra("Unlocking...")
- locked=false
- else
- displayExtra("Already unlocked.")
- end
- end,
- deregister=function(name)
- registered[name]=nil
- saveRegister()
- displayExtra("Deregistered "..name..".")
- return name
- end,
- address=function(name)
- if not name then return displayExtra(address)
- else
- return displayExtra("Address for "..name.." is "..(registered[name] or "[NONE]"))
- end
- end
- }
- local function doRequests()
- while true do
- local e,command=os.pullEvent("chat_command")
- local rCom
- for k,v in pairs(commandTable) do if k==command or startsWith(command,k.." ") then rCom=v break end end
- if not rCom then displayExtra("Unknown command: "..command)
- else
- --this is what im doing now, dont be scared
- --print(table.concat(split(command),":"))
- --print(rCom)
- local test,err=pcall(rCom,select(2,unpack(split(command," "))))
- if not test then pcall(displayExtra,"Error: "..err) end
- saveRegister()
- saveWhitelist()
- end
- end
- end
- local function doConnections()
- local enabled = "enabled"
- local connected = nil
- while true do
- --//is the dialed address an empty string
- --print("Dialled Address: "..tostring(sg.getDialedAddress() ~= ""))
- --//if you dialed them
- -- print("Initiator: "..tostring(sg.isInitiator()))
- --[[Hope this helps you]]--
- if sg.getDialledAddress() ~= "" and not sg.isInitiator() and connected ~= sg.getDialedAddress() then
- --print("HAI")
- local address = sg.getDialedAddress()
- if locked and not sg.isInitiator() then
- sg.disconnect()
- elseif whitelist[enabled] and not whitelist[sg.getDialedAddress()] and not sg.isInitiator() then
- updateDisplay("Disconnecting unwhitelisted address: "..(getName(address) or sg.getDialedAddress()))
- sg.disconnect()
- elseif whitelist[enabled] and whitelist[sg.getDialedAddress()] and not sg.isInitiator() then
- updateDisplay("Whitelisted address connected: "..(getName(address) or address))
- elseif not sg.isInitiator() then
- updateDisplay("Connected: "..(getName(address) or address))
- end
- connected = address
- elseif sg.getDialledAddress()=="" and connected~="" and not disconnected then if connected then updateDisplay("Disconnected from "..(getName(connected) or connected)) end connected=""
- elseif disconnected then connected="" disconnected=false
- end
- sleep(0.2)
- end
- end
- local function update()
- while true do
- sleep(0.5)
- if #displayT>0 then
- local msg,time=unpack(table.remove(displayT,1))
- display(msg,tme)
- end
- end
- end
- if runShellInBackground then
- shell.exit()
- term.clear()
- term.setCursorPos(1,1)
- print("StarDial+ running.")
- xpcall(function() return parallel.waitForAny(doRequests,doConnections,update,loadfile("rom/programs/shell")) end,printError)
- else
- xpcall(function() return parallel.waitForAny(doRequests,doConnections,update) end,printError)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement