private void EditAccountCommand(List <Shell.ParseResult> result)
        {
            if (repository == null)
            {
                Console.WriteLine("Password repository has not been opened.");
                return;
            }
            if (result.Count < 2)
            {
                Console.WriteLine("Missing <account> argument.");
                return;
            }
            var pwditem = FindAccount(result[1].Input);

            if (pwditem != null)
            {
                var cr = new ConsoleReader();
                cr.Prefix = "Name: ";
                string newname;
                while (true)
                {
                    newname = cr.Read(pwditem.Name);
                    if (newname == pwditem.Name || FindAccount(newname) == null)
                    {
                        break;
                    }
                    Console.WriteLine("The account already exists.");
                }
                cr.Prefix = "URL: ";
                var url = cr.Read(pwditem.Url);
                cr.Prefix = "Login: "******"Password: "******"Description: ";
                var desc = cr.Read(pwditem.Description);
                if (AskYesNoQuestion("Do you want to update the account?") == Answer.Yes)
                {
                    if (pwd.Length > 0)
                    {
                        pwditem.SecurePassword = pwd;
                    }
                    if (pwditem.Name != newname)
                    {
                        pwditem.Name = newname;
                    }
                    pwditem.Url         = url;
                    pwditem.Login       = login;
                    pwditem.Description = desc;
                    repository.Update(pwditem);
                    Console.WriteLine("Account updated.");
                }
            }
        }
        private void ChangeMasterPasswordCommand(List <Shell.ParseResult> result)
        {
            if (repository == null)
            {
                Console.WriteLine("Password repository has not been opened.");
                return;
            }
            if (!CheckSaveChanges())
            {
                Console.WriteLine("Aborted.");
                return;
            }
            var cr = new ConsoleReader();

            cr.Prefix = "Current Master Password: "******"Access denied.");
                return;
            }
            cr.Prefix = "New Master Password: "******"Aborted.");
                return;
            }
            while (true)
            {
                cr.Prefix = "Confirm New Master Password: "******"Aborted.");
                    return;
                }
                if (confirm.IsEqualTo(newrepositoryPassword))
                {
                    break;
                }
                Console.WriteLine("Passwords do not match.");
            }
            if (AskYesNoQuestion("Do you want to change the master password?") == Answer.Yes)
            {
                repository.ChangeMasterPassword(repositoryFileName, keyDirectory, newrepositoryPassword);
                repositoryPassword = newrepositoryPassword;
                Console.WriteLine("Master password changed.");
            }
        }
        private void AddAccountCommand(List <Shell.ParseResult> result)
        {
            if (repository == null)
            {
                Console.WriteLine("Password repository has not been opened.");
                return;
            }
            var cr = new ConsoleReader();

            cr.Prefix = "Name: ";
            string name;

            while (true)
            {
                name = cr.Read();
                if (FindAccount(name) == null)
                {
                    break;
                }
                Console.WriteLine("The account already exists.");
            }
            cr.Prefix = "URL: ";
            var url = cr.Read();

            cr.Prefix = "Login: "******"Password: "******"Description: ";
            var desc = cr.Read();

            if (AskYesNoQuestion("Do you want to add the account?") == Answer.Yes)
            {
                var pwditem = new Password
                {
                    Name           = name,
                    Description    = desc,
                    Url            = url,
                    Login          = login,
                    SecurePassword = pwd
                };
                repository.Add(pwditem);
                Console.WriteLine("Account added.");
            }
        }
        private void OpenRepositoryCommand(List <Shell.ParseResult> result)
        {
            if (result.Count < 2)
            {
                Console.WriteLine("Missing <file> argument.");
                return;
            }
            if (!File.Exists(result[1].Input))
            {
                Console.WriteLine("Password repository file does not exist.");
                return;
            }
            if (repository != null)
            {
                Console.WriteLine("Repository has not been closed.");
                return;
            }
            repositoryFileName = Path.GetFullPath(result[1].Input);
            if (result.Count > 2)
            {
                keyDirectory = Path.GetFullPath(result[2].Input);
                if (!Directory.Exists(keyDirectory))
                {
                    Console.WriteLine("Key directory does not exist.");
                    return;
                }
            }
            else
            {
                keyDirectory = Path.GetDirectoryName(repositoryFileName);
            }
            var cs = new ConsoleReader();

            cs.Prefix          = "Master Password: "******"Repository opened.");
            }
            catch
            {
                Console.WriteLine("Access denied.");
            }
        }
        private void EditRepositoryCommand(List <Shell.ParseResult> result)
        {
            if (repository == null)
            {
                Console.WriteLine("Password repository has not been opened.");
                return;
            }
            var cr = new ConsoleReader();

            cr.Prefix = "Name: ";
            var name = cr.Read(repository.Name);

            cr.Prefix = "Description: ";
            var desc = cr.Read(repository.Description);

            if (AskYesNoQuestion("Do you want to update the repository?") == Answer.Yes)
            {
                repository.Name        = name;
                repository.Description = desc;
                Console.WriteLine("Repository updated.");
            }
        }
        public void Run()
        {
            ShowHelpCommand();
            var consoleReader = new ConsoleReader();

            consoleReader.Background = ConsoleColor.DarkBlue;
            consoleReader.Foreground = ConsoleColor.Yellow;
            consoleReader.Expand     = Expand;
            while (true)
            {
                try
                {
                    if (!DispatchCommand(consoleReader))
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to dispatch command. Reason: {0}", ex.Message);
                }
            }
        }
        private Answer AskYesNoQuestion(string txt, Answer?dft = null)
        {
            var cr = new ConsoleReader();

            cr.Prefix = $"{txt} (y)es / (n)o: ";
            while (true)
            {
                string dftinput = "";
                if (dft.HasValue)
                {
                    dftinput = dft.Value == Answer.Yes ? "y" : "n";
                }
                var input = cr.Read(dftinput).ToLowerInvariant();
                if (input == "y" || input == "yes")
                {
                    return(Answer.Yes);
                }
                if (input == "n" || input == "no")
                {
                    return(Answer.No);
                }
            }
        }
        private void NewRepositoryCommand(List <Shell.ParseResult> result)
        {
            if (result.Count < 2)
            {
                Console.WriteLine("Missing <file> argument.");
                return;
            }
            if (File.Exists(result[1].Input))
            {
                Console.WriteLine("Password repository file already exists.");
                return;
            }
            if (repository != null)
            {
                Console.WriteLine("Repository has not been closed.");
                return;
            }
            repositoryFileName = Path.GetFullPath(result[1].Input);
            if (result.Count > 2)
            {
                keyDirectory = Path.GetFullPath(result[2].Input);
                if (!Directory.Exists(keyDirectory))
                {
                    Console.WriteLine("Key directory does not exist.");
                    return;
                }
            }
            else
            {
                keyDirectory = Path.GetDirectoryName(repositoryFileName);
            }
            var cr = new ConsoleReader();

            cr.Prefix = "Name: ";
            var name = cr.Read(Path.GetFileNameWithoutExtension(repositoryFileName));

            cr.Prefix = "Description: ";
            var desc = cr.Read();

            cr.Prefix          = "Master Password: "******"Aborted.");
                return;
            }
            while (true)
            {
                cr.Prefix = "Confirm Master Password: "******"Aborted.");
                    return;
                }
                if (confirm.IsEqualTo(repositoryPassword))
                {
                    break;
                }
                Console.WriteLine("Passwords do not match.");
            }
            if (AskYesNoQuestion("Do you want to create the repository?") == Answer.Yes)
            {
                repository             = new PasswordRepository();
                repository.Name        = name;
                repository.Description = desc;
                repository.Save(repositoryFileName, keyDirectory, repositoryPassword);
                Console.WriteLine("Repository created.");
            }
        }
        private bool DispatchCommand(ConsoleReader consoleReader)
        {
            var ret         = true;
            var parseResult = Shell.Parse(consoleReader.Read());

            if (parseResult.Count > 0 && parseResult[0].InputToken == Shell.Token.ID)
            {
                switch (parseResult[0].Input.ToLowerInvariant())
                {
                case "new-repository":
                    NewRepositoryCommand(parseResult);
                    break;

                case "open-repository":
                    OpenRepositoryCommand(parseResult);
                    break;

                case "show-repository":
                    ShowRepositoryCommand(parseResult);
                    break;

                case "edit-repository":
                    EditRepositoryCommand(parseResult);
                    break;

                case "save-repository":
                    SaveRepositoryCommand(parseResult);
                    break;

                case "close-repository":
                    CloseRepositoryCommand(parseResult);
                    break;

                case "change-masterpassword":
                    ChangeMasterPasswordCommand(parseResult);
                    break;

                case "list-account":
                    ListAccountCommand(parseResult);
                    break;

                case "add-account":
                    AddAccountCommand(parseResult);
                    break;

                case "delete-account":
                    DeleteAccountCommand(parseResult);
                    break;

                case "edit-account":
                    EditAccountCommand(parseResult);
                    break;

                case "show-account":
                    ShowAccountCommand(parseResult);
                    break;

                case "open-url":
                    OpenURLCommand(parseResult);
                    break;

                case "exit-console":
                    if (CheckSaveChanges())
                    {
                        ret = false;
                    }
                    break;

                case "clear-console":
                    ClearConsoleCommand();
                    break;

                case "show-help":
                    ShowHelpCommand();
                    break;

                case "show-license":
                    ShowLicenseCommand();
                    break;

                default:
                    Console.WriteLine("Invalid command. Type Show-Help.");
                    break;
                }
            }
            return(ret);
        }