Ejemplo n.º 1
0
 private static void GetSequencesForWindowEnd(SequenceQueriesEventArgs e)
 {
     if (AutoType.SequenceQueriesEnd != null)
     {
         AutoType.SequenceQueriesEnd(null, e);
     }
 }
Ejemplo n.º 2
0
        public static bool PerformIntoCurrentWindow(PwEntry pe)
        {
            string strWindow;

            try
            {
                strWindow = NativeMethods.GetWindowText(
                    NativeMethods.GetForegroundWindow());
            }
            catch (Exception) { strWindow = null; }

            if (!KeePassLib.Native.NativeLib.IsUnix())
            {
                if (strWindow == null)
                {
                    Debug.Assert(false); return(false);
                }
            }
            else
            {
                strWindow = string.Empty;
            }

            Thread.Sleep(100);

            return(AutoType.PerformInternal(pe, strWindow));
        }
Ejemplo n.º 3
0
        private static void OnAutoType(object sender, EventArgs e)
        {
            ToolStripMenuItem tsmi = (sender as ToolStripMenuItem);

            Debug.Assert(tsmi != null); if (tsmi == null)
            {
                return;
            }
            PwEntry pe = (tsmi.Tag as PwEntry);

            Debug.Assert(pe != null); if (pe == null)
            {
                return;
            }

            try
            {
                AutoType.PerformIntoCurrentWindow(pe,
                                                  Program.MainForm.DocumentManager.SafeFindContainerOf(pe));
            }
            catch (Exception ex)
            {
                MessageService.ShowWarning(ex);
            }
        }
Ejemplo n.º 4
0
        public static bool PerformIntoCurrentWindow(PwEntry pe, PwDatabase pdContext,
                                                    string strSeq)
        {
            if (pe == null)
            {
                Debug.Assert(false); return(false);
            }
            if (!pe.GetAutoTypeEnabled())
            {
                return(false);
            }
            if (!AppPolicy.Try(AppPolicyId.AutoTypeWithoutContext))
            {
                return(false);
            }

            Thread.Sleep(TargetActivationDelay);

            IntPtr hWnd;
            string strWindow;

            GetForegroundWindowInfo(out hWnd, out strWindow);

            if (!NativeLib.IsUnix())
            {
                if (strWindow == null)
                {
                    Debug.Assert(false); return(false);
                }
            }
            else
            {
                strWindow = string.Empty;
            }

            if (strSeq == null)
            {
                SequenceQueriesEventArgs evQueries = GetSequencesForWindowBegin(
                    hWnd, strWindow);

                List <string> lSeq = GetSequencesForWindow(pe, hWnd, strWindow,
                                                           pdContext, evQueries.EventID);

                GetSequencesForWindowEnd(evQueries);

                if (lSeq.Count == 0)
                {
                    strSeq = pe.GetAutoTypeSequence();
                }
                else
                {
                    strSeq = lSeq[0];
                }
            }

            AutoTypeCtx ctx = new AutoTypeCtx(strSeq, pe, pdContext);

            return(AutoType.PerformInternal(ctx, strWindow));
        }
Ejemplo n.º 5
0
        public static bool PerformIntoCurrentWindow(PwEntry pe, PwDatabase pdContext)
        {
            if (pe == null)
            {
                Debug.Assert(false); return(false);
            }
            if (!pe.GetAutoTypeEnabled())
            {
                return(false);
            }
            if (!AppPolicy.Try(AppPolicyId.AutoTypeWithoutContext))
            {
                return(false);
            }

            IntPtr hWnd;
            string strWindow;

            try
            {
                NativeMethods.GetForegroundWindowInfo(out hWnd, out strWindow, true);
            }
            catch (Exception) { hWnd = IntPtr.Zero; strWindow = null; }

            if (!KeePassLib.Native.NativeLib.IsUnix())
            {
                if (strWindow == null)
                {
                    Debug.Assert(false); return(false);
                }
            }
            else
            {
                strWindow = string.Empty;
            }

            Thread.Sleep(100);

            SequenceQueriesEventArgs evQueries = GetSequencesForWindowBegin(
                hWnd, strWindow);

            List <string> lSeq = GetSequencesForWindow(pe, hWnd, strWindow,
                                                       pdContext, evQueries.EventID);

            GetSequencesForWindowEnd(evQueries);

            if (lSeq.Count == 0)
            {
                lSeq.Add(pe.GetAutoTypeSequence());
            }

            AutoTypeCtx ctx = new AutoTypeCtx(lSeq[0], pe, pdContext);

            return(AutoType.PerformInternal(ctx, strWindow));
        }
Ejemplo n.º 6
0
        private static SequenceQueriesEventArgs GetSequencesForWindowBegin(
            IntPtr hWnd, string strWindow)
        {
            SequenceQueriesEventArgs e = new SequenceQueriesEventArgs(
                GetNextEventID(), hWnd, strWindow);

            if (AutoType.SequenceQueriesBegin != null)
            {
                AutoType.SequenceQueriesBegin(null, e);
            }

            return(e);
        }
Ejemplo n.º 7
0
        private static bool PerformInternal(AutoTypeCtx ctx, string strWindow)
        {
            if (ctx == null)
            {
                Debug.Assert(false); return(false);
            }

            AutoTypeCtx ctxNew = ctx.Clone();

            if (Program.Config.Integration.AutoTypePrependInitSequenceForIE &&
                WinUtil.IsInternetExplorer7Window(strWindow))
            {
                ctxNew.Sequence = @"{DELAY 50}1{DELAY 50}{BACKSPACE}" +
                                  ctxNew.Sequence;
            }

            return(AutoType.Execute(ctxNew));
        }
Ejemplo n.º 8
0
        public static bool PerformIntoCurrentWindow(PwEntry pe)
        {
            string strWindow;

            try
            {
                strWindow = NativeMethods.GetWindowText(
                    NativeMethods.GetForegroundWindow());
            }
            catch (Exception) { strWindow = null; }

            Debug.Assert(strWindow != null); if (strWindow == null)
            {
                return(false);
            }

            Thread.Sleep(100);

            return(AutoType.PerformInternal(pe, strWindow));
        }
Ejemplo n.º 9
0
        private static void OnAutoType(object sender, EventArgs e)
        {
            ToolStripMenuItem tsmi = sender as ToolStripMenuItem;

            Debug.Assert(tsmi != null); if (tsmi == null)
            {
                return;
            }
            PwEntry pe = tsmi.Tag as PwEntry;

            Debug.Assert(pe != null); if (pe == null)
            {
                return;
            }

            try { AutoType.PerformIntoCurrentWindow(pe); }
            catch (Exception ex)
            {
                MessageService.ShowWarning(ex);
            }
        }
Ejemplo n.º 10
0
        private static bool PerformInternal(PwEntry pwe, string strWindow)
        {
            Debug.Assert(pwe != null); if (pwe == null)
            {
                return(false);
            }

            string strSeq = GetSequenceForWindow(pwe, strWindow, false);

            if ((strSeq == null) || (strSeq.Length == 0))
            {
                return(false);
            }

            if (Program.Config.Integration.AutoTypePrependInitSequenceForIE &&
                WinUtil.IsInternetExplorer7Window(strWindow))
            {
                strSeq = @"{DELAY 50}1{DELAY 50}{BACKSPACE}" + strSeq;
            }

            AutoType.Execute(strSeq, pwe);
            return(true);
        }
Ejemplo n.º 11
0
        internal static bool PerformGlobal(List <PwDatabase> lSources,
                                           ImageList ilIcons, string strSeq)
        {
            if (lSources == null)
            {
                Debug.Assert(false); return(false);
            }

            if (NativeLib.IsUnix())
            {
                if (!NativeMethods.TryXDoTool(true) && !NativeLib.IsWayland())
                {
                    MessageService.ShowWarning(KPRes.AutoTypeXDoToolRequiredGlobalVer);
                    return(false);
                }
            }

            IntPtr hWnd;
            string strWindow;

            try
            {
                // hWnd = NativeMethods.GetForegroundWindowHandle();
                // strWindow = NativeMethods.GetWindowText(hWnd);
                NativeMethods.GetForegroundWindowInfo(out hWnd, out strWindow, true);
            }
            catch (Exception) { Debug.Assert(false); hWnd = IntPtr.Zero; strWindow = null; }

            // if(string.IsNullOrEmpty(strWindow)) return false;
            if (strWindow == null)
            {
                Debug.Assert(false); return(false);
            }
            if (!IsValidAutoTypeWindow(hWnd, true))
            {
                return(false);
            }

            SequenceQueriesEventArgs evQueries = GetSequencesForWindowBegin(
                hWnd, strWindow);

            List <AutoTypeCtx> lCtxs     = new List <AutoTypeCtx>();
            PwDatabase         pdCurrent = null;
            bool     bExpCanMatch        = Program.Config.Integration.AutoTypeExpiredCanMatch;
            DateTime dtNow = DateTime.UtcNow;

            EntryHandler eh = delegate(PwEntry pe)
            {
                if (!bExpCanMatch && pe.Expires && (pe.ExpiryTime <= dtNow))
                {
                    return(true);                    // Ignore expired entries
                }
                List <string> lSeq = GetSequencesForWindow(pe, hWnd, strWindow,
                                                           pdCurrent, evQueries.EventID);

                if (!string.IsNullOrEmpty(strSeq) && (lSeq.Count != 0))
                {
                    lCtxs.Add(new AutoTypeCtx(strSeq, pe, pdCurrent));
                }
                else
                {
                    foreach (string strSeqCand in lSeq)
                    {
                        lCtxs.Add(new AutoTypeCtx(strSeqCand, pe, pdCurrent));
                    }
                }

                return(true);
            };

            foreach (PwDatabase pd in lSources)
            {
                if ((pd == null) || !pd.IsOpen)
                {
                    continue;
                }
                pdCurrent = pd;
                pd.RootGroup.TraverseTree(TraversalMethod.PreOrder, null, eh);
            }

            GetSequencesForWindowEnd(evQueries);

            bool bForceDlg = Program.Config.Integration.AutoTypeAlwaysShowSelDialog;

            if ((lCtxs.Count >= 2) || bForceDlg)
            {
                AutoTypeCtxForm dlg = new AutoTypeCtxForm();
                dlg.InitEx(lCtxs, ilIcons);

                bool        bOK = (dlg.ShowDialog() == DialogResult.OK);
                AutoTypeCtx ctx = (bOK ? dlg.SelectedCtx : null);
                UIUtil.DestroyForm(dlg);

                if (ctx != null)
                {
                    try { NativeMethods.EnsureForegroundWindow(hWnd); }
                    catch (Exception) { Debug.Assert(false); }

                    int    nActDelayMS = TargetActivationDelay;
                    string strWindowT  = strWindow.Trim();

                    // https://sourceforge.net/p/keepass/discussion/329220/thread/3681f343/
                    // This apparently is only required here (after showing the
                    // auto-type entry selection dialog), not when using the
                    // context menu command in the main window
                    if (strWindowT.EndsWith("Microsoft Edge", StrUtil.CaseIgnoreCmp))
                    {
                        // 700 skips the first 1-2 characters,
                        // 750 sometimes skips the first character
                        nActDelayMS = 1000;
                    }

                    // Allow target window to handle its activation
                    // (required by some applications, e.g. Edge)
                    Application.DoEvents();
                    Thread.Sleep(nActDelayMS);
                    Application.DoEvents();

                    AutoType.PerformInternal(ctx, strWindow);
                }
            }
            else if (lCtxs.Count == 1)
            {
                AutoType.PerformInternal(lCtxs[0], strWindow);
            }

            return(true);
        }
Ejemplo n.º 12
0
        private static bool Execute(string strSeq, PwEntry pweData)
        {
            Debug.Assert(strSeq != null); if (strSeq == null)
            {
                return(false);
            }
            Debug.Assert(pweData != null); if (pweData == null)
            {
                return(false);
            }

            if (!pweData.GetAutoTypeEnabled())
            {
                return(false);
            }
            if (!AppPolicy.Try(AppPolicyId.AutoType))
            {
                return(false);
            }

            if (KeePassLib.Native.NativeLib.IsUnix())
            {
                if (!NativeMethods.TryXDoTool())
                {
                    MessageService.ShowWarning(KPRes.AutoTypeXDoToolRequired,
                                               KPRes.PackageInstallHint);
                    return(false);
                }
            }

            PwDatabase pwDatabase = null;

            try { pwDatabase = Program.MainForm.PluginHost.Database; }
            catch (Exception) { }

            bool bObfuscate = (pweData.AutoType.ObfuscationOptions !=
                               AutoTypeObfuscationOptions.None);
            AutoTypeEventArgs args = new AutoTypeEventArgs(strSeq, bObfuscate, pweData);

            if (AutoType.FilterCompilePre != null)
            {
                AutoType.FilterCompilePre(null, args);
            }

            args.Sequence = SprEngine.Compile(args.Sequence, true, pweData,
                                              pwDatabase, true, false);

            string strError = ValidateAutoTypeSequence(args.Sequence);

            if (strError != null)
            {
                MessageService.ShowWarning(strError);
                return(false);
            }

            Application.DoEvents();

            if (AutoType.FilterSendPre != null)
            {
                AutoType.FilterSendPre(null, args);
            }
            if (AutoType.FilterSend != null)
            {
                AutoType.FilterSend(null, args);
            }

            if (args.Sequence.Length > 0)
            {
                try { SendInputEx.SendKeysWait(args.Sequence, args.SendObfuscated); }
                catch (Exception excpAT)
                {
                    MessageService.ShowWarning(excpAT);
                }
            }

            pweData.Touch(false);

            // SprEngine.Compile might have modified the database
            Program.MainForm.UpdateUI(false, null, false, null, false, null, false);

            return(true);
        }
Ejemplo n.º 13
0
        private static bool Execute(AutoTypeCtx ctx)
        {
            if (ctx == null)
            {
                Debug.Assert(false); return(false);
            }

            string  strSeq  = ctx.Sequence;
            PwEntry pweData = ctx.Entry;

            if (pweData == null)
            {
                Debug.Assert(false); return(false);
            }

            if (!pweData.GetAutoTypeEnabled())
            {
                return(false);
            }
            if (!AppPolicy.Try(AppPolicyId.AutoType))
            {
                return(false);
            }

            if (KeePassLib.Native.NativeLib.IsUnix())
            {
                if (!NativeMethods.TryXDoTool())
                {
                    MessageService.ShowWarning(KPRes.AutoTypeXDoToolRequired,
                                               KPRes.PackageInstallHint);
                    return(false);
                }
            }

            PwDatabase pwDatabase = ctx.Database;

            bool bObfuscate = (pweData.AutoType.ObfuscationOptions !=
                               AutoTypeObfuscationOptions.None);
            AutoTypeEventArgs args = new AutoTypeEventArgs(strSeq, bObfuscate,
                                                           pweData, pwDatabase);

            if (AutoType.FilterCompilePre != null)
            {
                AutoType.FilterCompilePre(null, args);
            }

            args.Sequence = SprEngine.Compile(args.Sequence, new SprContext(
                                                  pweData, pwDatabase, SprCompileFlags.All, true, false));

            // string strError = ValidateAutoTypeSequence(args.Sequence);
            // if(!string.IsNullOrEmpty(strError))
            // {
            //	MessageService.ShowWarning(args.Sequence +
            //		MessageService.NewParagraph + strError);
            //	return false;
            // }

            Application.DoEvents();

            if (AutoType.FilterSendPre != null)
            {
                AutoType.FilterSendPre(null, args);
            }
            if (AutoType.FilterSend != null)
            {
                AutoType.FilterSend(null, args);
            }

            if (args.Sequence.Length > 0)
            {
                try { SendInputEx.SendKeysWait(args.Sequence, args.SendObfuscated); }
                catch (Exception excpAT)
                {
                    MessageService.ShowWarning(args.Sequence +
                                               MessageService.NewParagraph + excpAT.Message);
                }
            }

            pweData.Touch(false);
            if (EntryUtil.ExpireTanEntryIfOption(pweData, pwDatabase))
            {
                Program.MainForm.RefreshEntriesList();
            }

            // SprEngine.Compile might have modified the database;
            // pd.Modified is set by SprEngine
            Program.MainForm.UpdateUI(false, null, false, null, false, null, false);

            return(true);
        }
Ejemplo n.º 14
0
        private static bool Execute(AutoTypeCtx ctx)
        {
            if (ctx == null)
            {
                Debug.Assert(false); return(false);
            }

            string  strSeq  = ctx.Sequence;
            PwEntry pweData = ctx.Entry;

            if (pweData == null)
            {
                Debug.Assert(false); return(false);
            }

            if (!pweData.GetAutoTypeEnabled())
            {
                return(false);
            }
            if (!AppPolicy.Try(AppPolicyId.AutoType))
            {
                return(false);
            }

            if (NativeLib.IsUnix())
            {
                if (!NativeMethods.TryXDoTool() && !NativeLib.IsWayland())
                {
                    MessageService.ShowWarning(KPRes.AutoTypeXDoToolRequired,
                                               KPRes.PackageInstallHint);
                    return(false);
                }
            }

            PwDatabase pwDatabase = ctx.Database;

            bool bObfuscate = (pweData.AutoType.ObfuscationOptions !=
                               AutoTypeObfuscationOptions.None);
            AutoTypeEventArgs args = new AutoTypeEventArgs(strSeq, bObfuscate,
                                                           pweData, pwDatabase);

            if (AutoType.FilterCompilePre != null)
            {
                AutoType.FilterCompilePre(null, args);
            }

            args.Sequence = SprEngine.Compile(args.Sequence, new SprContext(
                                                  pweData, pwDatabase, SprCompileFlags.All, true, false));

            // string strError = ValidateAutoTypeSequence(args.Sequence);
            // if(!string.IsNullOrEmpty(strError))
            // {
            //	MessageService.ShowWarning(args.Sequence +
            //		MessageService.NewParagraph + strError);
            //	return false;
            // }

            Application.DoEvents();

            if (AutoType.FilterSendPre != null)
            {
                AutoType.FilterSendPre(null, args);
            }
            if (AutoType.FilterSend != null)
            {
                AutoType.FilterSend(null, args);
            }

            if (args.Sequence.Length > 0)
            {
                string strError = null;
                try { SendInputEx.SendKeysWait(args.Sequence, args.SendObfuscated); }
                catch (SecurityException exSec) { strError = exSec.Message; }
                catch (Exception ex)
                {
                    strError = args.Sequence + MessageService.NewParagraph +
                               ex.Message;
                }

                if (AutoType.SendPost != null)
                {
                    AutoType.SendPost(null, args);
                }

                if (!string.IsNullOrEmpty(strError))
                {
                    try
                    {
                        MainForm mfP = Program.MainForm;
                        if (mfP != null)
                        {
                            mfP.EnsureVisibleForegroundWindow(false, false);
                        }
                    }
                    catch (Exception) { Debug.Assert(false); }

                    MessageService.ShowWarning(strError);
                }
            }

            pweData.Touch(false);
            EntryUtil.ExpireTanEntryIfOption(pweData, pwDatabase);

            MainForm mf = Program.MainForm;

            if (mf != null)
            {
                // Always refresh entry list (e.g. {NEWPASSWORD} might
                // have changed data)
                mf.RefreshEntriesList();

                // SprEngine.Compile might have modified the database;
                // pd.Modified is set by SprEngine
                mf.UpdateUI(false, null, false, null, false, null, false);

                if (Program.Config.MainWindow.MinimizeAfterAutoType &&
                    mf.IsCommandTypeInvokable(null, MainForm.AppCommandType.Window))
                {
                    UIUtil.SetWindowState(mf, FormWindowState.Minimized);
                }
            }

            return(true);
        }
Ejemplo n.º 15
0
        // Multiple calls of this method should be wrapped in
        // GetSequencesForWindowBegin and GetSequencesForWindowEnd
        private static List <string> GetSequencesForWindow(PwEntry pwe,
                                                           IntPtr hWnd, string strWindow, PwDatabase pdContext, int iEventID)
        {
            List <string> l = new List <string>();

            if (pwe == null)
            {
                Debug.Assert(false); return(l);
            }
            if (strWindow == null)
            {
                Debug.Assert(false); return(l);
            }

            if (!pwe.GetAutoTypeEnabled())
            {
                return(l);
            }

            SprContext sprCtx = new SprContext(pwe, pdContext,
                                               SprCompileFlags.NonActive);

            // Specifically defined sequences must match before the title,
            // in order to allow selecting the first item as default one
            foreach (AutoTypeAssociation a in pwe.AutoType.Associations)
            {
                string strWndSpec = a.WindowName;
                if (strWndSpec == null)
                {
                    Debug.Assert(false); continue;
                }

                strWndSpec = SprEngine.Compile(strWndSpec.Trim(), sprCtx);

                if (MatchWindows(strWndSpec, strWindow))
                {
                    string strSeq = a.Sequence;
                    if (string.IsNullOrEmpty(strSeq))
                    {
                        strSeq = pwe.GetAutoTypeSequence();
                    }
                    AddSequence(l, strSeq);
                }
            }

            if (Program.Config.Integration.AutoTypeMatchByTitle)
            {
                string strTitle = SprEngine.Compile(pwe.Strings.ReadSafe(
                                                        PwDefs.TitleField).Trim(), sprCtx);
                if ((strTitle.Length > 0) && (strWindow.IndexOf(strTitle,
                                                                StrUtil.CaseIgnoreCmp) >= 0))
                {
                    AddSequence(l, pwe.GetAutoTypeSequence());
                }
            }

            string strCmpUrl = null;             // To cache compiled URL

            if (Program.Config.Integration.AutoTypeMatchByUrlInTitle)
            {
                strCmpUrl = SprEngine.Compile(pwe.Strings.ReadSafe(
                                                  PwDefs.UrlField).Trim(), sprCtx);
                if ((strCmpUrl.Length > 0) && (strWindow.IndexOf(strCmpUrl,
                                                                 StrUtil.CaseIgnoreCmp) >= 0))
                {
                    AddSequence(l, pwe.GetAutoTypeSequence());
                }
            }

            if (Program.Config.Integration.AutoTypeMatchByUrlHostInTitle)
            {
                if (strCmpUrl == null)
                {
                    strCmpUrl = SprEngine.Compile(pwe.Strings.ReadSafe(
                                                      PwDefs.UrlField).Trim(), sprCtx);
                }

                string strCleanUrl = StrUtil.RemovePlaceholders(strCmpUrl);
                string strHost     = UrlUtil.GetHost(strCleanUrl);

                if (strHost.StartsWith("www.", StrUtil.CaseIgnoreCmp) &&
                    (strCleanUrl.StartsWith("http:", StrUtil.CaseIgnoreCmp) ||
                     strCleanUrl.StartsWith("https:", StrUtil.CaseIgnoreCmp)))
                {
                    strHost = strHost.Substring(4);
                }

                if ((strHost.Length > 0) && (strWindow.IndexOf(strHost,
                                                               StrUtil.CaseIgnoreCmp) >= 0))
                {
                    AddSequence(l, pwe.GetAutoTypeSequence());
                }
            }

            if (Program.Config.Integration.AutoTypeMatchByTagInTitle)
            {
                foreach (string strTag in pwe.Tags)
                {
                    if (string.IsNullOrEmpty(strTag))
                    {
                        Debug.Assert(false); continue;
                    }

                    if (strWindow.IndexOf(strTag, StrUtil.CaseIgnoreCmp) >= 0)
                    {
                        AddSequence(l, pwe.GetAutoTypeSequence());
                        break;
                    }
                }
            }

            if (AutoType.SequenceQuery != null)
            {
                SequenceQueryEventArgs e = new SequenceQueryEventArgs(iEventID,
                                                                      hWnd, strWindow, pwe, pdContext);
                AutoType.SequenceQuery(null, e);

                foreach (string strSeq in e.Sequences)
                {
                    AddSequence(l, strSeq);
                }
            }

            return(l);
        }
Ejemplo n.º 16
0
        public static bool PerformGlobal(List <PwDatabase> vSources,
                                         ImageList ilIcons)
        {
            Debug.Assert(vSources != null); if (vSources == null)
            {
                return(false);
            }

            if (KeePassLib.Native.NativeLib.IsUnix())
            {
                if (!NativeMethods.TryXDoTool(true))
                {
                    MessageService.ShowWarning(KPRes.AutoTypeXDoToolRequiredGlobalVer);
                    return(false);
                }
            }

            IntPtr hWnd;
            string strWindow;

            try
            {
                // hWnd = NativeMethods.GetForegroundWindowHandle();
                // strWindow = NativeMethods.GetWindowText(hWnd);
                NativeMethods.GetForegroundWindowInfo(out hWnd, out strWindow, true);
            }
            catch (Exception) { Debug.Assert(false); hWnd = IntPtr.Zero; strWindow = null; }

            if (string.IsNullOrEmpty(strWindow))
            {
                return(false);
            }
            if (!IsValidAutoTypeWindow(hWnd, true))
            {
                return(false);
            }

            SequenceQueriesEventArgs evQueries = GetSequencesForWindowBegin(
                hWnd, strWindow);

            List <AutoTypeCtx> lCtxs     = new List <AutoTypeCtx>();
            PwDatabase         pdCurrent = null;
            bool     bExpCanMatch        = Program.Config.Integration.AutoTypeExpiredCanMatch;
            DateTime dtNow = DateTime.Now;

            EntryHandler eh = delegate(PwEntry pe)
            {
                if (!bExpCanMatch && pe.Expires && (pe.ExpiryTime < dtNow))
                {
                    return(true);                    // Ignore expired entries
                }
                List <string> lSeq = GetSequencesForWindow(pe, hWnd, strWindow,
                                                           pdCurrent, evQueries.EventID);
                foreach (string strSeq in lSeq)
                {
                    lCtxs.Add(new AutoTypeCtx(strSeq, pe, pdCurrent));
                }

                return(true);
            };

            foreach (PwDatabase pwSource in vSources)
            {
                if (pwSource.IsOpen == false)
                {
                    continue;
                }
                pdCurrent = pwSource;
                pwSource.RootGroup.TraverseTree(TraversalMethod.PreOrder, null, eh);
            }

            GetSequencesForWindowEnd(evQueries);

            bool bForceDlg = Program.Config.Integration.AutoTypeAlwaysShowSelDialog;

            if ((lCtxs.Count >= 2) || bForceDlg)
            {
                AutoTypeCtxForm dlg = new AutoTypeCtxForm();
                dlg.InitEx(lCtxs, ilIcons);

                bool        bOK = (dlg.ShowDialog() == DialogResult.OK);
                AutoTypeCtx ctx = (bOK ? dlg.SelectedCtx : null);
                UIUtil.DestroyForm(dlg);

                if (ctx != null)
                {
                    try { NativeMethods.EnsureForegroundWindow(hWnd); }
                    catch (Exception) { Debug.Assert(false); }

                    // Allow target window to handle its activation;
                    // https://sourceforge.net/p/keepass/discussion/329220/thread/3681f343/
                    Application.DoEvents();
                    Thread.Sleep(TargetActivationDelay);
                    Application.DoEvents();

                    AutoType.PerformInternal(ctx, strWindow);
                }
            }
            else if (lCtxs.Count == 1)
            {
                AutoType.PerformInternal(lCtxs[0], strWindow);
            }

            return(true);
        }
Ejemplo n.º 17
0
        public static bool PerformGlobal(List <PwDatabase> vSources,
                                         ImageList ilIcons)
        {
            Debug.Assert(vSources != null); if (vSources == null)
            {
                return(false);
            }

            if (KeePassLib.Native.NativeLib.IsUnix())
            {
                if (!NativeMethods.TryXDoTool(true))
                {
                    MessageService.ShowWarning(KPRes.AutoTypeXDoToolRequiredGlobalVer);
                    return(false);
                }
            }

            IntPtr hWnd;
            string strWindow;

            try
            {
                // hWnd = NativeMethods.GetForegroundWindowHandle();
                // strWindow = NativeMethods.GetWindowText(hWnd);
                NativeMethods.GetForegroundWindowInfo(out hWnd, out strWindow, true);
            }
            catch (Exception) { Debug.Assert(false); hWnd = IntPtr.Zero; strWindow = null; }

            if (string.IsNullOrEmpty(strWindow))
            {
                return(false);
            }
            if (!IsValidAutoTypeWindow(hWnd, true))
            {
                return(false);
            }

            SequenceQueriesEventArgs evQueries = GetSequencesForWindowBegin(
                hWnd, strWindow);

            List <AutoTypeCtx> lCtxs     = new List <AutoTypeCtx>();
            PwDatabase         pdCurrent = null;
            DateTime           dtNow     = DateTime.Now;

            EntryHandler eh = delegate(PwEntry pe)
            {
                // Ignore expired entries
                if (pe.Expires && (pe.ExpiryTime < dtNow))
                {
                    return(true);
                }

                List <string> lSeq = GetSequencesForWindow(pe, hWnd, strWindow,
                                                           pdCurrent, evQueries.EventID);
                foreach (string strSeq in lSeq)
                {
                    lCtxs.Add(new AutoTypeCtx(strSeq, pe, pdCurrent));
                }

                return(true);
            };

            foreach (PwDatabase pwSource in vSources)
            {
                if (pwSource.IsOpen == false)
                {
                    continue;
                }
                pdCurrent = pwSource;
                pwSource.RootGroup.TraverseTree(TraversalMethod.PreOrder, null, eh);
            }

            GetSequencesForWindowEnd(evQueries);

            if (lCtxs.Count == 1)
            {
                AutoType.PerformInternal(lCtxs[0], strWindow);
            }
            else if (lCtxs.Count > 1)
            {
                AutoTypeCtxForm dlg = new AutoTypeCtxForm();
                dlg.InitEx(lCtxs, ilIcons);

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    try { NativeMethods.EnsureForegroundWindow(hWnd); }
                    catch (Exception) { Debug.Assert(false); }

                    if (dlg.SelectedCtx != null)
                    {
                        AutoType.PerformInternal(dlg.SelectedCtx, strWindow);
                    }
                }
                UIUtil.DestroyForm(dlg);
            }

            return(true);
        }
Ejemplo n.º 18
0
        public static bool PerformGlobal(List <PwDatabase> vSources,
                                         ImageList ilIcons)
        {
            Debug.Assert(vSources != null); if (vSources == null)
            {
                return(false);
            }

            IntPtr hWnd;
            string strWindow;

            try
            {
                hWnd      = NativeMethods.GetForegroundWindow();
                strWindow = NativeMethods.GetWindowText(hWnd);
            }
            catch (Exception) { Debug.Assert(false); hWnd = IntPtr.Zero; strWindow = null; }

            if ((strWindow == null) || (strWindow.Length == 0))
            {
                return(false);
            }

            PwObjectList <PwEntry> m_vList = new PwObjectList <PwEntry>();

            DateTime dtNow = DateTime.Now;

            EntryHandler eh = delegate(PwEntry pe)
            {
                // Ignore expired entries
                if (pe.Expires && (pe.ExpiryTime < dtNow))
                {
                    return(true);
                }

                if (GetSequenceForWindow(pe, strWindow, true) != null)
                {
                    m_vList.Add(pe);
                }

                return(true);
            };

            foreach (PwDatabase pwSource in vSources)
            {
                if (pwSource.IsOpen == false)
                {
                    continue;
                }
                pwSource.RootGroup.TraverseTree(TraversalMethod.PreOrder, null, eh);
            }

            if (m_vList.UCount == 1)
            {
                AutoType.PerformInternal(m_vList.GetAt(0), strWindow);
            }
            else if (m_vList.UCount > 1)
            {
                EntryListForm elf = new EntryListForm();
                elf.InitEx(KPRes.AutoTypeEntrySelection, KPRes.AutoTypeEntrySelectionDescShort,
                           KPRes.AutoTypeEntrySelectionDescLong,
                           Properties.Resources.B48x48_KGPG_Key2, ilIcons, m_vList);
                elf.EnsureForeground = true;

                if (elf.ShowDialog() == DialogResult.OK)
                {
                    try { NativeMethods.EnsureForegroundWindow(hWnd); }
                    catch (Exception) { Debug.Assert(false); }

                    if (elf.SelectedEntry != null)
                    {
                        AutoType.PerformInternal(elf.SelectedEntry, strWindow);
                    }
                }
            }

            return(true);
        }