Advertisement
Bilal_Bassam

Enter_Manager

Jun 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.20 KB | None | 0 0
  1. -- This program are not finished ^_^
  2. -- I use this program for my computer only
  3. -- If you need you can get this program and edited to be right for you, enjoy!! ^_^
  4.  
  5.  
  6. -- If the "LuaXML" library not found then exit
  7. if not require("LuaXML") then print("\nLuaXML library are required\n") return end
  8. function main()
  9.     local version = "5.0"
  10.     local filepath = "./Enters.xml"
  11.     -- If xml file not found then create one
  12.     if not io.open(filepath, "r+") then
  13.         -- Include the basics
  14.         local f = xml.new("Enters")
  15.         f:append("password")[1] = nil
  16.         f:append("entersNo")[1] = "0"
  17.         xml.save(f, filepath)
  18.     end
  19.     -- Loads the XML file into a lua variable
  20.     local logsFile = xml.load(filepath)
  21.     -- Loads the pass
  22.     local pass = logsFile:find("password")[1] or ""
  23.     -- Loads the same computer time
  24.     local date = os.date("%x %I:%M:%S %p")
  25.     -- Loads how many log there are
  26.     local numOfIds = tonumber(logsFile:find("entersNo")[1])
  27.    
  28.    
  29.     local Acces = false
  30.     local isDenieded = false
  31.     -- The last log object (LuaXML object)
  32.     local append
  33.    
  34.    
  35.     local function debgPrint(text)
  36.         if not text then text = "" end
  37.         io.write(text,"\n\n")
  38.     end
  39.     -- TODO: find another way to clear the command line
  40.     local function clear()
  41.         io.write("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
  42.     end
  43.    
  44.     local function getValue(tag, attrb, value)
  45.         if not logsFile:find(tag) then error("Programming Error: \"tag\" not found,\nat function \"getValue\".\nPlease report this error at programmer email\nbelal2453@gmail.com", 2) end
  46.        
  47.         for i, n in pairs(logsFile) do
  48.             if string.find(xml.str(n), "<".. tag) and string.find(xml.str(n), attrb.. "=".. "\"".. value) then
  49.                 return logsFile[i][1], n
  50.             end
  51.         end
  52.     end
  53.    
  54.     local function WriteEnter(date)
  55.         if not date then date = os.date("%x %I:%M:%S %p") end
  56.         append = xml.append(logsFile, "log")
  57.         append[1] = tostring(date)
  58.         append["id"] = numOfIds + 1
  59.         append["isConfiemed"] = "false"
  60.         numOfIds = numOfIds + 1
  61.         logsFile:find("entersNo")[1] = numOfIds
  62.         xml.save(logsFile, filepath)
  63.     end
  64.    
  65.     local function SeeifthePassIscorect()
  66.         local inpass = io.read()
  67.         io.write("\n")
  68.         if inpass == pass then
  69.             append["isConfiemed"] = "true"
  70.             xml.save(logsFile, filepath)
  71.             return true
  72.         else
  73.             return false
  74.         end
  75.     end
  76.    
  77.     local function ShowunConfirmedEnters()
  78.         local denieds = {}
  79.         for i, n in pairs(logsFile) do
  80.             if string.find(xml.str(n), "isConfiemed=\"false\"") then
  81.                 table.insert(denieds, n["id"])
  82.                 isDenieded = true
  83.             end
  84.         end
  85.         return denieds
  86.     end
  87.    
  88.     local function welcome()
  89.         local denieds = ShowunConfirmedEnters()
  90.         debgPrint("Welcome, Mr. Bilal.")
  91.         if isDenieded == true and (pass ~= "" and pass) then
  92.             debgPrint("There are some Denieded Enters, Do you want to see Them? Y\\N")
  93.             if io.read() == "y" then
  94.                 io.write("\n")
  95.                 for i, l in pairs(denieds) do io.write("At line number: ", l, "\n", "At date: ", getValue("log", "id", tostring(l)), "\n\n") end
  96.             end
  97.         end
  98.         if pass == "" or not pass then
  99.             io.write("There are no password sets on programe;\nPlease write your new password here: \n")
  100.             repeat
  101.                 local retypedPass
  102.                 local oy = false
  103.                 local newPass = io.read()
  104.                 if not newPass or newPass == "" then
  105.                     io.write("Sorry, you may enter a new password\nTo work on this programe\n\n")
  106.                     io.write("Enter your new password: \n")
  107.                 else
  108.                     io.write("\nNow please retype your new password: \n")
  109.                     retypedPass = io.read()
  110.                     if (not retypedPass or retypedPass == "") or (newPass ~= retypedPass) then
  111.                         io.write("\n\nInvaild retyped password.\nSorry you may Re enter the password\n")
  112.                     elseif newPass == retypedPass then
  113.                         io.write("Your new password sets TO: \n\n".. newPass.. "\n\n")
  114.                         logsFile:find("password")[1] = tostring(newPass)
  115.                         xml.save(logsFile, filepath)
  116.                         oy = true
  117.                     end
  118.                 end
  119.             until oy == true
  120.         end
  121.     end
  122.    
  123.     local commands =
  124.     {
  125.         ["version"] = "The version is: ".. version,
  126.         ["confirm"] = {
  127.             line = function(...)
  128.                 line = {}
  129.                 for i, n in pairs(...) do table.insert(line, n) end
  130.                
  131.                 local denieds = ShowunConfirmedEnters()
  132.                 local fnd = false
  133.                
  134.                 if #denieds <= 0 then debgPrint("There are no denieded enters.") return false end
  135.                
  136.                 for i, n in pairs(logsFile) do
  137.                     for v, j in pairs(line) do
  138.                         if string.find(xml.str(n), "<log ") and string.find(xml.str(n), "id=\"".. j) then
  139.                             logsFile[i]["isConfiemed"] = "true"
  140.                             xml.save(logsFile, filepath)
  141.                             debgPrint("Line number ".. logsFile[i]["id"].. " has confirmed.")
  142.                             fnd = true
  143.                             break
  144.                         end
  145.                     end
  146.                 end
  147.                 if fnd ~= true then debgPrint("Line not found!!") return false end
  148.             end,
  149.            
  150.             all = function()
  151.                 local denieds = ShowunConfirmedEnters()
  152.                 local count = 0
  153.                
  154.                 if #denieds == 0 then debgPrint("There are no denieded enters.") return false end
  155.                
  156.                 for i, n in pairs(logsFile) do
  157.                     if string.find(xml.str(n), "<log ") and string.find(xml.str(n), "isConfiemed=\"false") then
  158.                         debgPrint("Line number ".. logsFile[i]["id"].. " has confirmed.")
  159.                         logsFile[i]["isConfiemed"] = "true"
  160.                         xml.save(logsFile, filepath)
  161.                     end
  162.                     count = count + 1
  163.                 end
  164.                 debgPrint("\nAll Enters are confirmed.")
  165.                 isDenieded = false
  166.                 return true
  167.             end,
  168.             usage = "Syntix: Confirm [ all, line \"number of line\" ]\nUsage: This Command is to confirms Enters."
  169.         },
  170.         ["show"] = {
  171.             denieded = function()
  172.                 local denieds = ShowunConfirmedEnters()
  173.                 local fnd = false
  174.                 for i, n in pairs(denieds) do if n then fnd = true end end
  175.                 if isDenieded == false or fnd == false then
  176.                     debgPrint("There are no denieded Enters!!")
  177.                     return false
  178.                 end
  179.                 for i, l in pairs(denieds) do
  180.                     io.write("At line number: ", l, "\n", "At date: ", getValue("log", "id", l), "\n\n")
  181.                 end
  182.             end,
  183.             all = function()
  184.                 local fnd = false
  185.                 for i, v in pairs(logsFile) do
  186.                     if string.find(xml.str(v), "<log ") then
  187.                         fnd = true
  188.                         io.write("At line number ".. v["id"].. ":\n".. v[1].. " ")
  189.                         if v["isConfiemed"] == "true" then
  190.                             io.write("Confirmed\n\n")
  191.                         else
  192.                             io.write("\n\n")
  193.                         end
  194.                     end
  195.                 end
  196.                 if fnd == false then debgPrint("There are no enters to show it.") end
  197.             end,
  198.             usage = "Syntix: Show [ all, denieded ]\nUsage: This command is to show the Enters"
  199.         },
  200.         ["delete"] = {
  201.             line = function(...)
  202.                 local count = 1
  203.                 local fnd = false
  204.                
  205.                 if not ... then debgPrint("Please enter line number to delete it.") return false end
  206.                 -- for i, n in pairs(...) do if not n or not tonumber(n) then debgPrint("Please enter line number to delete it.") return false end end
  207.                 -- for i, n in pairs(...) do if tonumber(n) > numOfIds or tonumber(n) <= 0 then debgPrint("There are no line like that.") return false end end
  208.                
  209.                 for i, n in ipairs(logsFile) do
  210.                     for v, l in ipairs(...) do
  211.                         if string.find(xml.str(n), "<log ")
  212.                             and string.find(xml.str(n), "id=\"".. l.. "\"") then
  213.                            
  214.                             logsFile[i] = ""
  215.                             debgPrint("Line number: ".. n["id"].. " is deleted.\n")
  216.                             fnd = true
  217.                             break
  218.                         end
  219.                     end
  220.                 end
  221.                 if fnd ~= true then debgPrint("There are no line like that.") return false end
  222.                
  223.                 for i, n in pairs(logsFile) do
  224.                     if string.find(xml.str(n), "<log ")
  225.                      and n["id"] ~= count then
  226.                         logsFile[i]["id"] = count
  227.                     end
  228.                     if string.find(xml.str(n), "<log ") then count = count + 1 end
  229.                 end
  230.                
  231.                 numOfIds = count - 1
  232.                 logsFile:find("entersNo")[1] = count - 1
  233.                 xml.save(logsFile, filepath)
  234.                 return true
  235.             end,
  236.             all = function()
  237.                 debgPrint("Are you sure to delete all enters? Y\\N\n")
  238.                 local ins = io.read()
  239.                 io.write("\n")
  240.                 if ins ~= "y" and ins ~= "Y" then return false end
  241.                
  242.                 for i, n in pairs(logsFile) do
  243.                     if string.find(xml.str(n), "<log") then
  244.                         logsFile[i] = ""
  245.                     end
  246.                 end
  247.                 logsFile:find("entersNo")[1] = "0"
  248.                 numOfIds = 0
  249.                 xml.save(logsFile, filepath)
  250.                 debgPrint("All enters are deleted.")
  251.                
  252.                 return true
  253.             end,
  254.             denieded = function()
  255.                 local count = 1
  256.                 local denieds = ShowunConfirmedEnters()
  257.                 local fnd = false
  258.                
  259.                 for i, n in pairs(denieds) do if n then fnd = true end end
  260.                 if fnd ~= true then debgPrint("There are no denieds enters!!") return false end
  261.                
  262.                 for i, n in pairs(logsFile) do
  263.                     for y, x in pairs(denieds) do
  264.                         if string.find(xml.str(n), "<log ")
  265.                          and string.find(xml.str(n), "id=\"".. x.."\"") then
  266.                             debgPrint("Line number ".. logsFile[i]["id"].. " was deleted.")
  267.                             logsFile[i] = ""
  268.                             break
  269.                         end
  270.                     end
  271.                 end
  272.                
  273.                 for i, n in pairs(logsFile) do
  274.                     if string.find(xml.str(n), "<log ")
  275.                      and n["id"] ~= count then
  276.                         logsFile[i]["id"] = count
  277.                     end
  278.                     if string.find(xml.str(n), "<log ") then count = count + 1 end
  279.                 end
  280.                
  281.                 numOfIds = count - #denieds
  282.                 logsFile:find("entersNo")[1] = count - #denieds
  283.                 xml.save(logsFile, filepath)
  284.                 return true
  285.             end,
  286.             usage = "Syntix: Delete [ all, denieded, line \"line number\" ]\nUsage: This command is to delete Enters."
  287.         },
  288.         ["enter"] = function() io.write("Enter password: ") WriteEnter() SeeifthePassIscorect() end,
  289.         ["clear"] = function() clear() end,
  290.         -- ["backup"] = function(...) if ... == nil then backup() else backup({...}) end debgPrint("Backup Done.") end,
  291.         --
  292.         help =
  293. [[
  294. version     Display vesion of this programs.
  295.         Syntix: version
  296.        
  297. confirm     Confirm any denieded Enter.
  298.         Syntix: confirm [ all, line "line number" ]
  299.        
  300. show        Display Enters.
  301.         Syntix: show [ all, denieded ]
  302.        
  303. delete      Delete Enters.
  304.         Syntix: delete [ all, denieded, line "line number"]
  305.        
  306. enter       reLogin with password.
  307.         Syntix: enter (Password needed)]]
  308.     }
  309.    
  310.     local function pCommand()
  311.         io.write("Commander> ")
  312.         local command = io.read()
  313.         io.write("\n")
  314.         local returnedCommand = nil
  315.         local commandArray = {}
  316.        
  317.         command = tostring( command )
  318.        
  319.         for i in string.gmatch(command, "%S+") do
  320.           table.insert(commandArray, i)
  321.         end
  322.        
  323.         for v in pairs(commands) do
  324.             if v == commandArray[1] then returnedCommand = commands[commandArray[1]] end
  325.         end
  326.         return commandArray, returnedCommand
  327.     end
  328.    
  329.     local function dCommand()
  330.         logsFile = xml.load(filepath)
  331.         local commandArray, command = pCommand()
  332.         local params = {}
  333.         for i, n in pairs(commandArray) do
  334.             table.insert(params, n)
  335.         end
  336.         table.remove(params, 1)
  337.         table.remove(params, 1)
  338.        
  339.         -- If type of command is table then call the params
  340.         if (type(command) == "table") then
  341.             local found = false
  342.             if (#commandArray <= 1) then debgPrint(command.usage or commands.help) return end
  343.             for i in pairs(command) do
  344.                 if i == commandArray[2] then
  345.                     if type(command[commandArray[2]]) == "function" then command[commandArray[2]](params)
  346.                     elseif type(command[commandArray[2]]) == "string" or type(command[commandArray[2]]) == "number" then debgPrint(command[commandArray[2]])
  347.                     else debgPrint("Command not found!!", "\n") end
  348.                     found = true
  349.                 end
  350.             end
  351.             if found ~= true then debgPrint(command.usage or command.help) return end
  352.        
  353.         -- If type of command is function then call it
  354.         elseif type(command) == "function" then
  355.             command(commandArray[2], commandArray[3], commandArray[4], commandArray[5])
  356.         -- If type of command is strin then debug it
  357.         elseif type(command) == "string" or type(command) == "number" then
  358.             debgPrint(command)
  359.         -- and finally if the command was exit then quit
  360.         elseif commandArray[1] == "exit" then
  361.             return true
  362.         -- If command not found then return false ( to commandsloop function )
  363.         else
  364.             return false
  365.         end
  366.     end
  367.    
  368.     local function commandsLoop()
  369.         repeat
  370.             local status = dCommand()
  371.             if status == false then debgPrint("Command not found!!", "\n") end
  372.         until status == true
  373.     end
  374.    
  375.     WriteEnter(date)
  376.     if pass == "" or not pass then
  377.         Acces = true
  378.         append["isConfiemed"] = "true"
  379.     else
  380.         Acces = SeeifthePassIscorect()
  381.     end
  382.    
  383.     if Acces == true then
  384.         welcome()
  385.         commandsLoop()
  386.     end
  387. end
  388. -- if arg and #arg >= 3 then for i, n in pairs(arg) do print(i, n) end end
  389.  
  390. local err, messg = pcall(main)
  391. if not err then print(messg,"\n\n\n","Press enter key to exit\n") io.read() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement