Advertisement
Redxone

[CC] Tardis Core (Chat edition)

Apr 5th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.72 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. -- "Ready only" definitions
  3. local settings = ".tardiscoresettings"
  4.  
  5. -- Define peripherals.
  6. local cmd = peripheral.find("command")
  7. local chat = peripheral.find("chatbox_admin")
  8.  
  9. -- Define core settings
  10. local default_name = "&eTARDIS"
  11. local core_name = "&9[&eTARDIS&9]"
  12. local core_exe = "tardis:"
  13. local inrun = true
  14. local argdone = ";"
  15. local history = {}
  16. local admins = {
  17.   ["Lewisk3"]=true,
  18. }
  19.  
  20. function execute(command)
  21.     cmd.setCommand(command)
  22.     cmd.runCommand()
  23. end
  24.  
  25. function changeTitle(new)
  26.     core_name = "&9["..new.."&9]"
  27.     saveCoreSettings()
  28. end
  29.  
  30. function tell(text,player)
  31.   if not inrun then print(core_name.." -> &eYou &8:> &f"..text) end
  32.   if(inrun)then
  33.     cmd.setCommand("sudo " ..player.. " ping "..core_name.." -> &eYou &8:> &f"..text)
  34.     cmd.runCommand()
  35.   end
  36. end
  37.  
  38. function say(text)
  39.   if not inrun then print(core_name.." &f"..text) end
  40.   if(inrun)then
  41.     cmd.setCommand("sudo @a ping "..core_name.." &f"..text)
  42.     cmd.runCommand()
  43.   end
  44. end
  45.  
  46. function updateStatus(msg)
  47.   table.insert(history,msg)  
  48.   term.setBackgroundColor(colors.black)
  49.   term.setTextColor(colors.lightBlue)
  50.   print(":> " .. msg)
  51. end
  52.  
  53.  function clear()
  54.    term.setCursorPos(1,1)
  55.    term.setBackgroundColor(colors.black)
  56.    term.setTextColor(colors.lightBlue)
  57.    term.clear()
  58.    term.setBackgroundColor(colors.gray)
  59.    term.clearLine()
  60.    print("Tardis Core :> History.")
  61.    term.setBackgroundColor(colors.black)
  62.    term.setCursorPos(1,3)
  63.  end
  64.  
  65.  
  66. core_commands = {
  67.     ["lua"] = {args=1,run = function(p,var)
  68.             local ok, err = pcall( loadstring(var) )
  69.             if(not ok and err)then
  70.                  tell("&4Execution Error: " .. var, p)
  71.             elseif(ok)then
  72.                tell("&aExecuted lua: " .. var, p)
  73.             end
  74.     end},
  75.     ["say"] = {args=1,run = function(p,var) say(var) end},
  76.     ["cmd"] = {args=1,run = function(p,var) execute(var) end},
  77.     ["nick"] = {args=1,run = function(p,var) changeTitle(var) end},
  78.     ["reset"] = {args=0,run=function(p) changeTitle(default_name) end},
  79.     ["rawsay"] = {args=1,run=function(p,var) execute("sudo @a ping "..var) end},
  80.     ["clear"] = {args=0,run=function(p) clear() tell("Cleared console.",p) end},
  81.     ["cmds"] = {args=0,run=function(p) tell("Available commands: " .. cmdlist,p) end},
  82.     ["whitelist"] = {args=2,run=function(p,mode,user)
  83.         if(mode == "add")then
  84.             admins[user] = true;
  85.             saveCoreSettings()
  86.             tell("You have been added to the whitelist!",user)
  87.             tell("Added user: " .. user .. " to the whitelist.",p)
  88.         elseif(mode == "del")then
  89.             if(user == "Lewisk3")then
  90.               tell("Nope, how about i remove you instead :P", p)
  91.               return
  92.             end
  93.             table.remove(admins,user)
  94.             tell("You have been removed to the whitelist!",user)
  95.             tell("Removed user: " .. user .. " from the whitelist.",p)
  96.             saveCoreSettings()
  97.         else
  98.           tell("Invalid whitelist mode: " .. mode .. " (add, del)",p)
  99.         end
  100.     end},
  101.     ["shutdown"]= {args=0,run=function(p) tell("Shutting down...",p) saveCoreSettings() error("Core shutdown.") end},
  102.     ["cmd_add"] = {args=3,run=function(p,name,ar,val) addCmd(p,name,ar,val) end},
  103.     ["cmd_sub"] = {args=3,run=function(p,name) remCmd(p,name) end},
  104. }
  105. cmdlist = ""
  106. for k, v in pairs(core_commands) do
  107.   cmdlist = cmdlist .. k .. ", "
  108. end
  109.  
  110.  
  111. --tardis:cmd_add;test;0;function(p) print("Test?") end
  112. function addCmd(pl,name,bytes,func)
  113.   if(core_commands[name] ~= nil)then
  114.      tell("&4Existing command: " .. name, pl)
  115.      updateStatus("Tried to add existing command: " .. pl)
  116.   else
  117.     core_commands[name] = {args=bytes,run=function() func() end}
  118.   end
  119.   saveCoreSettings()
  120. end
  121. function remCmd(pl,name)
  122.   if(core_commands[name] == nil)then
  123.      tell("&4Non-Existent command: " .. name, pl)
  124.      updateStatus("Tried to remove non-existent command: " .. pl)
  125.   else
  126.     core_commands[name] = nil
  127.     table.remove(core_commands,name)
  128.   end
  129. end
  130.  
  131. function readCoreSettings()
  132.    if(not fs.exists(settings))then
  133.        saveCoreSettings()
  134.    end
  135.    local f = fs.open(settings,"r")
  136.   -- core_name      = f.readLine()
  137.   -- core_exe       = f.readLine()
  138.   -- argdone        = f.readLine()
  139.    admins         = textutils.unserialize(f.readAll())
  140.   -- core_commands  = textutils.unserialize(f.readLine())
  141. end
  142.  
  143. function saveCoreSettings()
  144.     local f = fs.open(settings,"w")
  145.   --  f.writeLine(core_name)
  146.   --  f.writeLine(core_exe)
  147.   --  f.writeLine(argdone)
  148.     f.write(textutils.serialize(admins))
  149.   --  f.writeLine(core_commands)
  150.     f.close()
  151. end
  152.  
  153.  
  154. clear()
  155. function coreloop(pl,msg)
  156.        if(msg:sub(1,#core_exe) == core_exe)then
  157.         -- Calculate length of command
  158.         msg = msg:sub(#core_exe+1,#msg)
  159.         local com = {}
  160.            for word in string.gmatch(msg,"([^"..argdone.."]+)") do
  161.                if(#msg:gsub("%s+","") > 0)then
  162.                   table.insert(com,word)
  163.                end
  164.            end
  165.         if(#com == 0)then
  166.             tell("&4Instrution must be ended with a - " .. argdone, pl)
  167.             updateStatus("Malformed command: " .. pl)
  168.             return
  169.         end
  170.         com = com[1]
  171.        if(admins[pl])then
  172.          if(core_commands[com])then
  173.             -- Sub out the length of out command
  174.             msg = msg:sub(#com+1,#msg)
  175.             --msg = msg:gsub("%s+","")
  176.    
  177.             local args = {}
  178.             local argsind = 0
  179.             for word in string.gmatch(msg,"([^"..argdone.."]+)") do
  180.                 if(argsind < core_commands[com].args)then
  181.                     table.insert(args,word)
  182.                 end
  183.             end
  184.             if(#args == 0)then
  185.               args[1] = ""
  186.             end
  187.             if(#args < core_commands[com].args)then
  188.                tell("&4Invalid Arguments for: &f" .. msg, pl)
  189.                updateStatus(pl .. " entered invalid arguments for command: " .. com)
  190.             else
  191.              core_commands[com].run(pl,unpack(args))
  192.              updateStatus(pl .. " executed command: " .. com .. "(" .. tostring(unpack(args)) .. ")")
  193.             end
  194.  
  195.           else
  196.              tell("&4Invalid instruction: &f" .. msg, pl)
  197.           end
  198.        else
  199.           tell("&4Unauthorized use of: &f" .. msg, pl)
  200.           updateStatus("Unauthorized user: " .. pl)
  201.        end
  202.     end
  203. end
  204. local targs = { ... }
  205. if(targs[1] == "-n")then
  206.    inrun = false
  207. end
  208.  
  209. print("Loading settings...")
  210. readCoreSettings()
  211. while inrun do
  212.    local ev, sd, pl, msg = os.pullEvent("chatbox_command")
  213.    if(msg ~= nil and pl ~= nil)then
  214.       coreloop(pl,msg)
  215.    end
  216. end
  217.  
  218. while not inrun do
  219.   coreloop("Lewisk3",read())
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement