SHOW:
|
|
- or go back to the newest paste.
| 1 | --[[ | |
| 2 | StarDial+ 1.0 | |
| 3 | Made by: MudkipTheEpic and the helpful people on | |
| 4 | MudServ. | |
| 5 | ||
| 6 | Commands: | |
| 7 | (all are prefixed with "$$") | |
| 8 | dial address OR registeredname (Connects the stargate to that address.) | |
| 9 | disconnect (Disconnects the stargate) | |
| 10 | register name address (Makes you be able yo use commands with that name instead of the address.) | |
| 11 | lockdown (Turns on the ability to disconnect all connections you didn't create.) | |
| 12 | unlock (Undoes lockdown) | |
| 13 | deregister name (Unregisters name) | |
| 14 | whitelist on (Only allows people you select to connect to you.) | |
| 15 | (NOTE: YOU can still dial to anyone.) | |
| 16 | whitelist off (Disables whitelist) | |
| 17 | whitelist add address OR name (Adds them to the whitelist) | |
| 18 | whitelist remove address OR name (Removes them from the whitelist) | |
| 19 | ||
| 20 | Suggest more to add! Enjoy the program! | |
| 21 | -Mudkip | |
| 22 | --]] | |
| 23 | ||
| 24 | - | if _G.isRunning then return false end |
| 24 | + | |
| 25 | local disconnectSelf=false | |
| 26 | local function wrap(typ) | |
| 27 | for k,v in pairs(peripheral.getNames()) do | |
| 28 | if peripheral.getType(v)==typ then | |
| 29 | return v | |
| 30 | end | |
| 31 | end | |
| 32 | return nil | |
| 33 | end | |
| 34 | local glasses=wrap("terminal_glasses_bridge")
| |
| 35 | local stargate=wrap("stargate_base")
| |
| 36 | if not (glasses and stargate) then | |
| 37 | error("StarDial+ needs a stargate and terminal glasses bridge to function.",0)
| |
| 38 | end | |
| 39 | ||
| 40 | ||
| 41 | ||
| 42 | ||
| 43 | ||
| 44 | local sg=peripheral.wrap(stargate) | |
| 45 | sg.isConnected=function() return not sg.getDialledAddress()=="" end | |
| 46 | sg.getDialedAddress=sg.getDialedAddress or sg.getDialledAddress | |
| 47 | sg.isInitiator=function() return peripheral.call(stargate,"isInitiator")=="true" end | |
| 48 | local displayExtra | |
| 49 | local _,address=pcall(peripheral.call,stargate,"getHomeAddress") | |
| 50 | local bridge=peripheral.wrap(glasses) | |
| 51 | bridge.clear() | |
| 52 | local l=bridge.addText(12,12,"Say $$dial (address) to dial.",0x000000) | |
| 53 | l.setScale(1.75) | |
| 54 | l.setColor(0x3366FF) | |
| 55 | --print() | |
| 56 | --print(l.getHeight) | |
| 57 | local h=bridge.addBox(1,1,45*6,36,0x999999,0.5) | |
| 58 | h.setZIndex(4) | |
| 59 | l.setZIndex(1) | |
| 60 | local registered={}
| |
| 61 | if fs.exists(".register") then
| |
| 62 | local l=fs.open(".register","r")
| |
| 63 | registered=textutils.unserialize(l.readAll()) | |
| 64 | l.close() | |
| 65 | end | |
| 66 | if not registered then fs.delete(".register") registered={} end
| |
| 67 | local whitelist={enabled=false}
| |
| 68 | if fs.exists(".whitelist") then
| |
| 69 | local l=fs.open(".whitelist","r")
| |
| 70 | whitelist=textutils.unserialize(l.readAll()) | |
| 71 | l.close() | |
| 72 | end | |
| 73 | if not whitelist then fs.delete(".whitelist") whitelist={enabled=false} end
| |
| 74 | --local function split(str,pat) | |
| 75 | -- local fpat="(.+)"..pat | |
| 76 | -- if not str:find(pat) then | |
| 77 | -- return {str}
| |
| 78 | -- end | |
| 79 | -- local t={}
| |
| 80 | -- for c in str:gmatch(fpat) do | |
| 81 | -- table.insert(t,c) | |
| 82 | -- end | |
| 83 | -- return t | |
| 84 | --end | |
| 85 | -- This might be easier | |
| 86 | local function split(str,pat) | |
| 87 | -- local fpat="(.+)"..pat -- lol we seem to have arrived at the exact same function :3 Meh ill leave it | |
| 88 | local tArgs = {}
| |
| 89 | 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
| |
| 90 | table.insert(tArgs, _arg) | |
| 91 | end | |
| 92 | return tArgs | |
| 93 | end | |
| 94 | ||
| 95 | local displayT={}
| |
| 96 | 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
| |
| 97 | 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
| |
| 98 | - | local function connect(address) local e,err= pcall(peripheral.call,stargate,"connect",address:upper()) if e then displayExtra("Connected succesfully.") else displayExtra("Could not connect: "..tostring(err)) end return e end
|
| 98 | + | |
| 99 | local function saveRegister() local l=fs.open(".register","w") l.write(textutils.serialize(registered)) l.close() return true end
| |
| 100 | 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 | |
| 101 | - | local function getName(address) local l=address:upper() for k,v in pairs(registered) do if v==address then l=k:upper() break end end return l end |
| 101 | + | |
| 102 | function displayExtra(text,time) | |
| 103 | table.insert(displayT,{text,time})
| |
| 104 | os.queueEvent("display")
| |
| 105 | end | |
| 106 | local updateDisplay=displayExtra | |
| 107 | ||
| 108 | function display(text,time) | |
| 109 | if time and time<0.05 then return end | |
| 110 | time=time or 2 | |
| 111 | for i=1,16 do h.setHeight(36+i) sleep(0.05) end | |
| 112 | local extra=bridge.addText(1,42,text,0x00CCFF) | |
| 113 | extra.setScale(1) | |
| 114 | sleep(time) | |
| 115 | extra.delete() | |
| 116 | for i=1,16 do h.setHeight(52-i) sleep(0.05) end | |
| 117 | return true | |
| 118 | end | |
| 119 | ||
| 120 | local commandTable={
| |
| 121 | dial=function(address) | |
| 122 | disconnect(0) | |
| 123 | sleep(1) | |
| 124 | return connect(registered[address] or address) | |
| 125 | - | return connect(registered[address:upper()] or address:upper()) |
| 125 | + | |
| 126 | whitelist=function(cmd,opt) | |
| 127 | if opt then opt=registered[opt] or opt end | |
| 128 | if cmd=="on" then | |
| 129 | if whitelist["enabled"] then displayExtra("Already whitelisted.") else displayExtra("Whitelist on.") whitelist.enabled=true end
| |
| 130 | elseif cmd=="off" then | |
| 131 | if not whitelist.enabled then displayExtra("Already disabled.") else displayExtra("Unwhitelisting...") whitelist.enabled=false end
| |
| 132 | elseif cmd=="add" and opt then | |
| 133 | whitelist[opt]=true | |
| 134 | - | whitelist[opt:upper()]=true |
| 134 | + | |
| 135 | elseif cmd=="remove" and opt then | |
| 136 | whitelist[opt]=false | |
| 137 | - | whitelist[opt:upper()]=false |
| 137 | + | |
| 138 | end | |
| 139 | end, | |
| 140 | disconnect=function() | |
| 141 | if sg.getDialledAddress()=="" and not sg.isInitiator() then disconnected=true | |
| 142 | elseif sg.getDialledAddress()=="" then displayExtra("Already disconnected.") return false end
| |
| 143 | disconnect(0) | |
| 144 | end, | |
| 145 | register=function(name,address) | |
| 146 | registered[name]=address | |
| 147 | - | registered[name]=address:upper() |
| 147 | + | |
| 148 | displayExtra("Registered "..name.." as "..address..".")
| |
| 149 | return name | |
| 150 | end, | |
| 151 | lockdown=function() | |
| 152 | if not locked then | |
| 153 | displayExtra("Locked down.")
| |
| 154 | locked=true | |
| 155 | else | |
| 156 | displayExtra("Already locked.")
| |
| 157 | end | |
| 158 | end, | |
| 159 | unlock=function() | |
| 160 | if locked then | |
| 161 | displayExtra("Unlocking...")
| |
| 162 | locked=false | |
| 163 | else | |
| 164 | displayExtra("Already unlocked.")
| |
| 165 | end | |
| 166 | end, | |
| 167 | deregister=function(name) | |
| 168 | registered[name]=nil | |
| 169 | saveRegister() | |
| 170 | displayExtra("Deregistered "..name..".")
| |
| 171 | return name | |
| 172 | end, | |
| 173 | address=function(name) | |
| 174 | if not name then return displayExtra(address) | |
| 175 | else | |
| 176 | return displayExtra("Address for "..name.." is "..(registered[name] or "[NONE]"))
| |
| 177 | end | |
| 178 | end | |
| 179 | } | |
| 180 | ||
| 181 | local function doRequests() | |
| 182 | while true do | |
| 183 | local e,command=os.pullEvent("chat_command")
| |
| 184 | local rCom | |
| 185 | for k,v in pairs(commandTable) do if k==command or startsWith(command,k.." ") then rCom=v break end end | |
| 186 | if not rCom then displayExtra("Unknown command: "..command)
| |
| 187 | else | |
| 188 | --this is what im doing now, dont be scared | |
| 189 | --print(table.concat(split(command),":")) | |
| 190 | --print(rCom) | |
| 191 | local test,err=pcall(rCom,select(2,unpack(split(command," ")))) | |
| 192 | if not test then pcall(displayExtra,"Error: "..err) end | |
| 193 | saveRegister() | |
| 194 | saveWhitelist() | |
| 195 | end | |
| 196 | end | |
| 197 | end | |
| 198 | ||
| 199 | local function doConnections() | |
| 200 | local enabled = "enabled" | |
| 201 | local connected = nil | |
| 202 | while true do | |
| 203 | ||
| 204 | --//is the dialed address an empty string | |
| 205 | --print("Dialled Address: "..tostring(sg.getDialedAddress() ~= ""))
| |
| 206 | ||
| 207 | --//if you dialed them | |
| 208 | -- print("Initiator: "..tostring(sg.isInitiator()))
| |
| 209 | --[[Hope this helps you]]-- | |
| 210 | ||
| 211 | if sg.getDialledAddress() ~= "" and not sg.isInitiator() and connected ~= sg.getDialedAddress() then | |
| 212 | --print("HAI")
| |
| 213 | local address = sg.getDialedAddress() | |
| 214 | if locked and not sg.isInitiator() then | |
| 215 | sg.disconnect() | |
| 216 | elseif whitelist[enabled] and not whitelist[sg.getDialedAddress()] and not sg.isInitiator() then | |
| 217 | updateDisplay("Disconnecting unwhitelisted address: "..(getName(address) or sg.getDialedAddress()))
| |
| 218 | sg.disconnect() | |
| 219 | elseif whitelist[enabled] and whitelist[sg.getDialedAddress()] and not sg.isInitiator() then | |
| 220 | updateDisplay("Whitelisted address connected: "..(getName(address) or address))
| |
| 221 | elseif not sg.isInitiator() then | |
| 222 | updateDisplay("Connected: "..(getName(address) or address))
| |
| 223 | end | |
| 224 | connected = address | |
| 225 | elseif sg.getDialledAddress()=="" and connected~="" and not disconnected then if connected then updateDisplay("Disconnected from "..(getName(connected) or connected)) end connected=""
| |
| 226 | elseif disconnected then connected="" disconnected=false | |
| 227 | end | |
| 228 | sleep(0.2) | |
| 229 | end | |
| 230 | end | |
| 231 | ||
| 232 | local function update() | |
| 233 | while true do | |
| 234 | sleep(0.5) | |
| 235 | if #displayT>0 then | |
| 236 | local msg,time=unpack(table.remove(displayT,1)) | |
| 237 | display(msg,tme) | |
| 238 | end | |
| 239 | end | |
| 240 | end | |
| 241 | if runShellInBackground then | |
| 242 | shell.exit() | |
| 243 | - | _G.isRunning=true |
| 243 | + | |
| 244 | term.setCursorPos(1,1) | |
| 245 | print("StarDial+ running.")
| |
| 246 | xpcall(function() return parallel.waitForAny(doRequests,doConnections,update,loadfile("rom/programs/shell")) end,printError)
| |
| 247 | else | |
| 248 | xpcall(function() return parallel.waitForAny(doRequests,doConnections,update) end,printError) | |
| 249 | end |