public CreateBookmarkletAction(Server server, HttpListenerContext listenerContext)
            : base(server, listenerContext)
        {
            string html = Properties.Resources.CreateBookmarklet;

            _responseString.AppendLine(html.Replace("{action}", "http://localhost:" + _server.Port.ToString() + "/"));
        }
        public AutoTypeAction(Server server, HttpListenerContext listenerContext)
            : base(server, listenerContext)
        {
            string url = NormalizeURL(HttpUtility.UrlDecode(_request.QueryString["url"] ?? ""));
            string favoredUserName = HttpUtility.UrlDecode(_request.QueryString["favoredUserName"] ?? "").ToLower();

            string actionResult = "Unknown";
            string actionError = "";

            List<PwEntry> entries = new List<PwEntry>();
            if (_host.Database.IsOpen)
            {
                // Find all entries at this domain
                if (url.Length > 0)
                    FindURL(entries, _host.Database.RootGroup, url.ToLower());

                // If there are more than one, check for favored user name
                if (entries.Count > 1 && favoredUserName != "")
                {

                    List<PwEntry> newEntries = new List<PwEntry>();
                    foreach (PwEntry entry in entries)
                        if (entry.Strings.Get(PwDefs.UserNameField).ReadString().ToLower() == favoredUserName)
                            newEntries.Add(entry);

                    if (newEntries.Count > 0)
                        entries = newEntries;
                }
            }
            else
                actionError = "There's no open KeePass database.";

            if (entries.Count == 1)
            {
                PwEntry entry = entries[0];
                //KeePass.Util.AutoType.PerformIntoCurrentWindow(entry, _host.MainWindow.DocumentManager.SafeFindContainerOf(entry));
                _host.MainWindow.Invoke(new PerformAutoType(DoAutoType), new Object[] { entry, _host.Database });

                actionResult = "AutoType";
            }
            else if (entries.Count == 0)
            {
                actionError = "No entry could be found in KeePass that uses this URL or one close to it.";
            }
            else
            {
                actionError = "There are multiple entries in KeePass that use this URL or one close to it.";
            }

            if (actionError != "")
                actionResult = "Error";

            _response.StatusCode = 200;

            _responseString.AppendLine(@"jsonCallback ({");
            _responseString.AppendLine(@"  ""actionResult"": """ + actionResult + @"""");
            if (actionError != "")
                _responseString.AppendLine(@", ""actionError"": """ + actionError + @"""");
            _responseString.AppendLine(@"});");
        }
 public Action(Server server, HttpListenerContext listenerContext)
 {
     _server = server;
     _host = _server.Host;
     _listenerContext = listenerContext;
     _request = _listenerContext.Request;
     _response = _listenerContext.Response;
     _responseString = new StringBuilder();
 }
        public ViewBookmarkletAction(Server server, HttpListenerContext listenerContext)
            : base(server, listenerContext)
        {
            string html = Properties.Resources.ViewBookmarklet;
            string bookmarklet = Properties.Resources.Bookmarklet;

            string name = HttpUtility.UrlDecode(_request.QueryString["name"]);
            string favoredUsername = HttpUtility.UrlDecode(_request.QueryString["favoredUsername"]);

            bookmarklet = bookmarklet.Replace("{port}", _server.Port.ToString()).Replace("{favoredUsername}", favoredUsername).Replace("\r", "").Replace("\n", " ");
            while (bookmarklet.Contains("  "))
                bookmarklet = bookmarklet.Replace("  ", " ");
            bookmarklet = Uri.EscapeDataString(bookmarklet);

            _responseString.AppendLine(html.Replace("{name}", name).Replace("{bookmarklet}", bookmarklet));
        }
        /// <summary>
        /// The <c>Initialize</c> function is called by KeePass when
        /// you should initialize your plugin (create menu items, etc.).
        /// </summary>
        /// <param name="host">Plugin host interface. By using this
        /// interface, you can access the KeePass main window and the
        /// currently opened database.</param>
        /// <returns>You must return <c>true</c> in order to signal
        /// successful initialization. If you return <c>false</c>,
        /// KeePass unloads your plugin (without calling the
        /// <c>Terminate</c> function of your plugin).</returns>
        public override bool Initialize(IPluginHost host)
        {
            _host = host;
            _port = (int)Settings.Default["Port"];

            _tsmiSep1 = new ToolStripSeparator();

            _tsmiSettings = new ToolStripMenuItem();
            _tsmiSettings.Text = "Bookmarklet Plugin Settings";
            _tsmiSettings.Click += OnSettingsClick;

            _tsmiCreateBookmarklet = new ToolStripMenuItem();
            _tsmiCreateBookmarklet.Text = "Create Bookmarklet";
            _tsmiCreateBookmarklet.Click += OnCreateBookmarkletClick;

            _host.MainWindow.ToolsMenu.DropDownItems.Add(_tsmiSep1);
            _host.MainWindow.ToolsMenu.DropDownItems.Add(_tsmiSettings);
            _host.MainWindow.ToolsMenu.DropDownItems.Add(_tsmiCreateBookmarklet);

            _server = new Server(_port, _host);
            _server.Start();

            return true; // Initialization successful
        }
        public void OnSettingsClick(object sender, EventArgs e)
        {
            FrmSettings frmSettings = new FrmSettings();
            frmSettings.txtPort.Text = _port.ToString();

            if (frmSettings.ShowDialog() == DialogResult.OK)
            {
                _server.Stop();

                _port = int.Parse(frmSettings.txtPort.Text);

                Settings.Default["Port"] = _port;
                Settings.Default.Save();

                _server = new Server(_port, _host);
                _server.Start();
            }
        }
 public UnknownAction(Server server, HttpListenerContext listenerContext)
     : base(server, listenerContext)
 {
     _responseString.AppendLine("Unknown action");
 }