Advertisement
Guest User

commandprocessor.lua

a guest
Apr 27th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. --Command Interpreter for authServer v1.0
  2. --Fixed several bugs
  3. --Added "authorize", "createauth", and "removeauth"
  4. --commands.
  5.  
  6. cid, cmd = ...
  7. cid = tonumber(cid)
  8. uname = {}
  9. pword = {}
  10. authLevel = {}
  11. iuID = {}
  12.  
  13. --Function to load table data from any file.
  14. function loadTable(file)
  15.  if fs.exists(file) then
  16.   f = fs.open(file, "r")
  17.   d = f.readAll()
  18.   f.close()
  19.   return textutils.unserialise(d)
  20.  else
  21.   print("Unable to find file.")
  22.   return {}
  23.  end
  24. end
  25.  
  26. --Function to save table data to a file.
  27. function saveTable(file, table)
  28.  f = fs.open(file, "w")
  29.  f.write(textutils.serialise(table))
  30.  f.close()
  31. end
  32.  
  33. --Function to look for a specific value
  34. --from within a table.
  35. function tableLookup(chkv, table)
  36.  for i=1, #table do
  37.   if table[i] == chkv then
  38.    return i
  39.   elseif i > #table then
  40.    return "nomatch"
  41.   end
  42.  end
  43. end
  44.  
  45. --Function to filter rednet responses from
  46. --a specified computer ID
  47. function filterResponse(filterID)
  48.  rid = -1
  49.  while rid ~= filterID do
  50.   rid, msg = rednet.receive()
  51.  end
  52.  return msg
  53. end
  54.  
  55. --Function that does all the command processing.
  56. function processCommand(cmd, id)
  57.  --Code for the command "authorize"
  58.  if cmd == "authorize" then
  59.     --Get user credentials for authroization.
  60.     rednet.send(id, "ready")
  61.     user = filterResponse(id)
  62.     pass = filterResponse(id)
  63.     rAuthLv = filterResponse(id)
  64.     uName = tableLookup(user, uname)
  65.     --Run comparisions for authorization.
  66.     sleep(1)
  67.     if pword[uName] == pass and authLevel[uName] >= rAuthLv then
  68.      rednet.send(id, "auth")
  69.      print("User authorized.")
  70.     else
  71.      print("User authorized.")
  72.      rednet.send(id, "nauth")
  73.     end
  74.    
  75.   --Code for the "create authorization" command
  76.   elseif cmd == "createauth" then
  77.     --Verify user has proper authorization
  78.     --to create another account.
  79.     rednet.send(id, "ready")
  80.     user = filterResponse(id)
  81.     pass = filterResponse(id)
  82.     nUser = filterResponse(id)
  83.     nPass = filterResponse(id)
  84.     nAuth = filterResponse(id)
  85.     uName = tableLookup(user, uname)
  86.     --Check credentials
  87.     if user == uname[uName] and pass == pword[uName] and authLevel[uName] == 5 then
  88.      table.insert(uname, nUser)
  89.      print("Added new username")
  90.      table.insert(pword, nPass)
  91.      print("Added new password")
  92.      table.insert(authLevel, nAuth)
  93.      print("Added new auth level")
  94.      saveTable("uname", uname)
  95.      print("Saved uname table")
  96.      saveTable("pword", pword)
  97.      print("Saved pword table")
  98.      saveTable("authLevel", authLevel)
  99.      print("save authLevel table")
  100.      sleep(0.1)
  101.      rednet.send(id, "done")
  102.    end
  103.    
  104.   elseif cmd == "removeauth" then
  105.    --Verify user has proper authorization to
  106.    --remove another user's authorization.
  107.    rednet.send(id, "ready")
  108.    user = filterResponse(id)
  109.    pass = filterResponse(id)
  110.    user2 = filterResponse(id)
  111.    uName = tableLookup(user, uname)
  112.    authL = authLevel[uName]
  113.    uName2 = tableLookup(user2, uname)
  114.    if uname[uName] == user and pword[uName] == pass and authLevel[uName] == 5 then
  115.     print("User Removed")
  116.     table.remove(uname, uName2)
  117.     table.remove(pword, uName2)
  118.     table.remove(authLevel, uName2)
  119.     saveTable("uname", uname)
  120.     saveTable("pword", pword)
  121.     saveTable("authLevel", authLevel)
  122.     sleep(0.1)
  123.     rednet.send(id, "done")
  124.    else
  125.     print("not authorized")
  126.    end
  127.   end
  128.  end
  129.  
  130. --Get current ID table. Also get user data.
  131. uname = loadTable("uname")
  132. pword = loadTable("pword")
  133. authLevel = loadTable("authLevel")
  134.  
  135. iuID = loadTable("iuID")
  136. processCommand(cmd, cid)
  137.  
  138. --Remove ID from table.
  139. spot = tableLookup(cid, iuID)
  140. table.remove(iuID, spot)
  141. saveTable("iuID", iuID)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement