Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }