Advertisement
AquaJD

HostFS

Sep 27th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. --[[
  2.  
  3.     host
  4.     By Dave-ee Jones
  5.    
  6.     Local DNS hosting
  7.     Uses file system instead of RAM
  8.  
  9. --]]
  10.  
  11. local DIR = ".hosts"
  12. if not fs.exists(DIR) then
  13.   fs.makeDir(DIR)
  14. end
  15.  
  16. -- Create a new entry
  17. function new(_NAME,_ID)
  18.     if type(_NAME) == "string" then
  19.         if type(_ID) == "number" then
  20.             local _f = fs.open(DIR.."/".._NAME,"w")
  21.             _f.write(_ID)
  22.             _f.close()
  23.             return true
  24.         else
  25.             error("host: ID needs to be a number",0)
  26.         end
  27.     else
  28.         error("host: NAME needs to be a string",0)
  29.     end
  30. end
  31.  
  32. -- Change the ID of the name given
  33. function setID(_NAME,_ID)
  34.     if type(_NAME) == "string" then
  35.         if type(_ID) == "number" then
  36.             if fs.exists(DIR.."/".._NAME) then
  37.                 local _f = fs.open(DIR.."/".._NAME,"w")
  38.                 _f.write(_ID)
  39.                 _f.close()
  40.                 return true
  41.             else
  42.                 error("host: Record for ".._NAME.." doesn't exist",0)
  43.             end
  44.         else
  45.             error("host: ID needs to be a number",0)
  46.         end
  47.     end
  48.     return false
  49. end
  50.  
  51. -- Change the name of the ID given
  52. function setName(_ID,_NAME)
  53.     if type(_ID) == "number" then
  54.         local b_FOUND = false
  55.         for k,v in pairs(fs.list(DIR)) do
  56.             local _f = fs.open(DIR.."/"..v,"w")
  57.             local _contents = _f.readAll()
  58.             _f.close()
  59.             if tonumber(_contents) == _ID then
  60.                 b_FOUND = true
  61.                 delete(k)
  62.                 new(_NAME,_ID)
  63.             end
  64.         end
  65.         if not b_FOUND then
  66.             error("host: Record for "..tostring(_ID).." doesn't exist",0)
  67.         end
  68.     else
  69.         error("host: NAME needs to be a string",0)
  70.     end
  71. end
  72.  
  73. -- Returns name of ID given or false if there isn't one
  74. function getName(_ID)
  75.     if type(_ID) == "number" then
  76.         for k,v in pairs(fs.list(DIR)) do
  77.             local _f = fs.open(DIR.."/"..v,"r")
  78.             local _contents = _f.readAll()
  79.             _f.close()
  80.             if tonumber(_contents) == _ID then
  81.                 return v
  82.             end
  83.         end
  84.     else
  85.         error("host: ID needs to be a number",0)
  86.     end
  87.     return false
  88. end
  89.  
  90. -- Returns ID of name given or false if there isn't one
  91. function getID(_NAME)
  92.     if type(_NAME) == "string" then
  93.         if fs.exists(DIR.."/".._NAME) then
  94.             local _f = fs.open(DIR.."/".._NAME,"r")
  95.             local _contents = _f.readAll()
  96.             _f.close()
  97.             return tonumber(_contents)
  98.         else
  99.             return false
  100.         end
  101.     else
  102.         error("host: NAME needs to be a string",0)
  103.     end
  104. end
  105.  
  106. -- Deletes a DNS entry
  107. function delete(_NAME)
  108.     if fs.exists(DIR.."/".._NAME) then
  109.         fs.delete(DIR.."/".._NAME)
  110.         return true
  111.     end
  112.     return false
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement