Advertisement
MudkipTheEpic

StarDial+ 1.2

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