KeeAgent.KeeAgentExt.AddEntry C# (CSharp) Method

AddEntry() public method

public AddEntry ( PwEntry entry, ICollection constraints ) : ISshKey
entry PwEntry
constraints ICollection
return ISshKey
        public ISshKey AddEntry(PwEntry entry,
            ICollection<Agent.KeyConstraint> constraints)
        {
            var settings = entry.GetKeeAgentSettings();
              try {
            var key = entry.GetSshKey();
            string db_name = "<Unknown database>";
            try {
              var database = pluginHost.MainWindow.DocumentManager.GetOpenDatabases()
            .Where((db) => db.RootGroup.FindEntry(entry.Uuid, true) != null).Single();
              db_name = database.Name;
            } catch (Exception) {
              Debug.Fail("Duplicate UUIDs?");
            }
            key.Source = string.Format("{0}: {1}", db_name, entry.GetFullPath());
            if (agent is PageantClient) {
              // Pageant errors if you try to add a key that is already loaded
              // so try to remove the key first so that it behaves like other agents
              try {
            agent.RemoveKey(key);
              } catch (Exception) {
            // ignore failure
              }
            } else {
              // also, Pageant does not support constraints
              if (constraints != null) {
            foreach (var constraint in constraints) {
              key.AddConstraint(constraint);
            }
              } else {
            if (settings.UseConfirmConstraintWhenAdding) {
              key.addConfirmConstraint();
            }
            if (settings.UseLifetimeConstraintWhenAdding) {
              key.addLifetimeConstraint(settings.LifetimeConstraintDuration);
            }
              }
              if (Options.AlwaysConfirm &&
              !key.HasConstraint(Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM))
              {
            key.addConfirmConstraint();
              }
            }
            agent.AddKey(key);
            if (settings.Location.SelectedType == EntrySettings.LocationType.Attachment
              && settings.Location.SaveAttachmentToTempFile)
            {
              try {
            var data = entry.Binaries.Get(settings.Location.AttachmentName).ReadData();
            var tempPath = Path.Combine(UrlUtil.GetTempPath(), "KeeAgent");
            if (!Directory.Exists(tempPath))
              Directory.CreateDirectory(tempPath);
            var fileName = Path.Combine(tempPath, settings.Location.AttachmentName);
            File.WriteAllBytes(fileName, data);
            keyFileMap[key.GetMD5Fingerprint().ToHexString()] = new KeyFileInfo(fileName, true);
              } catch (Exception ex) {
            Debug.Fail(ex.Message, ex.StackTrace);
              }
            }
            if (settings.Location.SelectedType == EntrySettings.LocationType.File) {
              keyFileMap[key.GetMD5Fingerprint().ToHexString()] =
            new KeyFileInfo(settings.Location.FileName, false);
            }
            return key;
              } catch (Exception ex) {
            var firstLine = string.Format("KeeAgent: Error while loading key from entry '{0}'",
              entry.GetFullPath());
            if (ex is NoAttachmentException) {
              MessageService.ShowWarning(new string[] {
            firstLine,
             "No attachment specified in KeePass entry"
               });
            } else if (ex is FileNotFoundException || ex is DirectoryNotFoundException) {

            if (!Options.IgnoreMissingExternalKeyFiles) {
                MessageService.ShowWarning(new string[] {
                  firstLine,
                  "Could not find file",
                  settings.Location.FileName
                 });
            }

            } else if (ex is KeyFormatterException || ex is PpkFormatterException) {
              MessageService.ShowWarning(new string[] {
            firstLine,
            string.Format ("Could not load file {0}",
              settings.Location.SelectedType == EntrySettings.LocationType.File
                ? string.Format("'{0}'", settings.Location.FileName)
                : string.Format("from attachment '{0}'", settings.Location.AttachmentName)),
            "Possible causes:",
            "- Passphrase was entered incorrectly",
            "- File is corrupt or has been tampered"
               });
            } else if (ex is AgentFailureException) {
              MessageService.ShowWarning(new string[] {
            firstLine,
            "Agent Failure",
            "Possible causes:",
            "- Key is already loaded in agent",
            "- Agent is locked"
              });
            } else if (ex is AgentNotRunningException) {
              MessageService.ShowWarning(new string[] {
            firstLine,
            "Could not add key because no SSH agent was found.",
            "Please make sure your SSH agent program is running (e.g. Pageant)."
              });
            } else {
              MessageService.ShowWarning(new string[] {
            firstLine,
            "Unexpected error",
            ex.ToString()
              });
              Debug.Fail(ex.ToString());
            }
            throw;
              }
        }