Advertisement
osmarks

P2P Tunnel Manager

Nov 30th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. local data
  2. local file = "./p2p.tbl"
  3.  
  4. local function save()
  5.     local f = fs.open(file, "w")
  6.     f.write(textutils.serialise(data))
  7.     f.close()
  8. end
  9.  
  10. local function load()
  11.     if fs.exists(file) then
  12.         local f = fs.open(file, "r")
  13.         local x = textutils.unserialise(f.readAll())
  14.         f.close()
  15.         return x
  16.     end
  17. end
  18.  
  19. local function split(str)
  20.     local t = {}
  21.     for w in str:gmatch("%S+") do table.insert(t, w) end
  22.     return t
  23. end
  24.  
  25. data = load() or {}
  26.  
  27. local case_insensitive = {
  28.     __index = function( table, key )
  29.         local value = rawget( table, key )
  30.         if value ~= nil then
  31.             return value
  32.         end
  33.         if type(key) == "string" then
  34.             local value = rawget( table, string.lower(key) )
  35.             if value ~= nil then
  36.                 return value
  37.             end                    
  38.         end
  39.         return nil
  40.     end
  41. }
  42.  
  43. setmetatable(data, case_insensitive)
  44.  
  45. local function tunnel_info(name)
  46.     local d = data[name]
  47.     return ("%s: %d %s"):format(name, d.channels, d.description)
  48. end
  49.  
  50. local commands = {
  51.     list = function()
  52.         for t in pairs(data) do print(tunnel_info(t)) end
  53.     end,
  54.     info = function(name)
  55.         print(tunnel_info(name))
  56.     end,
  57.     describe = function(name)
  58.         local t = data[name]
  59.         print("Description:", t.description)
  60.         write "New description: "
  61.         t.description = read()
  62.     end,
  63.     add = function(name)
  64.         data[name] = {
  65.             channels = 0,
  66.             description = "None set."
  67.         }
  68.     end,
  69.     channels = function(name, by)
  70.         local by = tonumber(by)
  71.         if not by then error "Invalid number!" end
  72.         local t = data[name]
  73.         print("Channels:", t.channels)
  74.         print("Increasing by:", by)
  75.         t.channels = t.channels + by
  76.         print("New channels:", t.channels)
  77.     end,
  78.     delete = function(name)
  79.         data[name] = nil
  80.     end
  81. }
  82.  
  83. setmetatable(commands, case_insensitive)
  84.  
  85. local hist = {}
  86.  
  87. while true do
  88.     write "> "
  89.     local text = read(nil, hist)
  90.     table.insert(hist, text)
  91.     local tokens = split(text)
  92.     local command = table.remove(tokens, 1)
  93.  
  94.     local ok, err = pcall(commands[command], unpack(tokens))
  95.     save()
  96.     if not ok then printError(err) end
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement