private bool GetExecutionApproval(ref XMLRecord xrec)
 {
     if (xrec.CommandIsSafe != "true")
     {
         // TODO: add note to the message that the command might receive all lines through the pipe
         if (cBNotifyExecution.IsChecked.Value)
         {
             if (xrec.ShellExecute == "true")
             {
                 // TODO: Fix/replace the string placeholder with the actual string on the clipboard
                 MessageBoxResult result = MessageBox.Show("Do you want to pass the following string to the shell for execution:" + Environment.NewLine + Environment.NewLine + xrec.AllArguments, "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                 if (result == MessageBoxResult.No)
                 {
                     return(false);
                 }
             }
             else if (xrec.IsDll == "true")
             {
                 MessageBoxResult result = MessageBox.Show("Do you want to load/run the following library to process the string(s) from the clipboard textbox:" + Environment.NewLine + Environment.NewLine + xrec.Path + @"\" + xrec.Executable, "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                 if (result == MessageBoxResult.No)
                 {
                     return(false);
                 }
             }
             else
             {
                 MessageBoxResult result = MessageBox.Show("Do you want to run the following external command:" + Environment.NewLine + Environment.NewLine + xrec.Path + @"\" + xrec.Executable + " [string from the clipboard textbox]", "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                 if (result == MessageBoxResult.No)
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
Ejemplo n.º 2
0
        public OptionalArguments(string sXmlBatFile)
        {
            InitializeComponent();


            XMLRecord xrec    = new XMLRecord(sXmlBatFile);
            bool      XmlFlag = true;

            // Add the static text
            oaStackPanel.Children.Add(new TextBlock {
                Text = "Click \"Add\" to populate the Optional Arguments field." + Environment.NewLine, FontSize = 14.667, TextWrapping = TextWrapping.Wrap, FontWeight = FontWeights.Bold
            });


            foreach (Tuple <string, string> tup in xrec.Options)
            {
                XmlFlag = false;
                if (tup.Item1 != "")
                {
                    DockPanel newPanel = new DockPanel();
                    newPanel.Margin        = new Thickness(0, 5, 0, 0);
                    newPanel.LastChildFill = true;

                    TextBox tbOptionsString = new TextBox {
                        Text = tup.Item1, FontSize = 14.667, Height = 22, VerticalAlignment = VerticalAlignment.Bottom
                    };
                    TextBlock tBlockInfo = new TextBlock {
                        Text = tup.Item2, FontSize = 14.667, TextWrapping = TextWrapping.Wrap, FontWeight = FontWeights.Bold
                    };

                    Button bAdd = new Button {
                        Content = "Add", FontSize = 14.667, Height = 22, Tag = tbOptionsString
                    };
                    bAdd.Click += new RoutedEventHandler(bAddOption_Click);
                    DockPanel.SetDock(bAdd, Dock.Right);
                    DockPanel.SetDock(tBlockInfo, Dock.Top);


                    newPanel.Children.Add(tBlockInfo);
                    newPanel.Children.Add(bAdd);
                    newPanel.Children.Add(tbOptionsString);



                    oaStackPanel.Children.Add(newPanel);
                    oaStackPanel.Children.Add(new Separator {
                        Visibility = Visibility.Visible, Margin = new Thickness(0, 10, 0, 10)
                    });
                }
            }

            if (XmlFlag)
            {
                DockPanel newPanel = new DockPanel();
                newPanel.Margin        = new Thickness(0, 5, 0, 0);
                newPanel.LastChildFill = true;

                TextBlock tBlockNoOptions = new TextBlock {
                    Text = "No options defined in file\n" + sXmlBatFile, FontSize = 14.667, VerticalAlignment = VerticalAlignment.Center, TextWrapping = TextWrapping.Wrap, TextAlignment = TextAlignment.Center
                };

                newPanel.Children.Add(tBlockNoOptions);

                oaStackPanel.Children.Add(newPanel);
            }

            Button bDone = new Button {
                Content = "Done", FontSize = 14.667, Height = 22
            };

            bDone.Click += bDone_Click;
            oaStackPanel.Children.Add(bDone);
        }
        private void RunXmlCommand(ref string[] saLinesToExecute, XMLRecord xrec)
        {
            Logger.WriteLog("Data from XML file: \nPath: " + xrec.Path + "\nExecutable: " + xrec.Executable + "\nClass: " + xrec.Class + "\nDescription: " + xrec.Description + "\nStaticArguments: " + xrec.AllArguments);

            // TODO: change the below logic to inform the user that "isdll" and "usepipe" cant be both true at the same time
            // e.g. handle this in the XMLRecord class to prevent both set to true
            if (xrec.IsDll == "true" && xrec.UsePipe == "true")
            {
                MessageBox.Show("The isdll and usepipe properties in the XML file cant be both true at the same time.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }


            // TODO: change the below logic to inform the user that "ShellExecute" and "usepipe" cant be both true at the same time
            // e.g. handle this in the XMLRecord class to prevent both set to true
            if (xrec.ShellExecute == "true" && xrec.UsePipe == "true")
            {
                MessageBox.Show("The ShellExecute and usepipe properties in the XML file cant be both true at the same time.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // TODO: change the below logic to inform the user that "ShellExecute" and "IsDll" cant be both true at the same time
            // e.g. handle this in the XMLRecord class to prevent both set to true
            if (xrec.ShellExecute == "true" && xrec.IsDll == "true")
            {
                MessageBox.Show("The ShellExecute and IsDll properties in the XML file cant be both true at the same time.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }


            if (xrec.UsePipe == "true")
            {
                string sAllArguments = "";

                // Create a temporary pipe name
                string sPipeName = Guid.NewGuid().ToString();


                // Place the optional argument into the arguments string
                // Todo: the below optional argument must be shown in the messagebox which informs the user about the executed command, e.g. set the messagebox call before the Process.Start call and put the sAllArguments variable into the messagebox
                sAllArguments = xrec.AllArguments.Replace(OptionalArgumentString, comboOptArg.Text);

                // Add the pipe name to the command line
                sAllArguments = sAllArguments.Replace(PipeNameString, sPipeName);


                // Get user approval to run the external command
                if (GetExecutionApproval(ref xrec) == false)
                {
                    return;
                }
                ;

                /* To delete
                 * if (xrec.CommandIsSafe != "true")
                 * {
                 *  if (cBNotifyExecution.IsChecked.Value)
                 *  {
                 *      // TODO: add note to the message that the command might receive all lines through the pipe
                 *      MessageBoxResult result = MessageBox.Show("Do you want to run the following external command:" + Environment.NewLine + Environment.NewLine + xrec.Path + @"\" + xrec.Executable + " " + sAllArguments, "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                 *      // + saLinesToExecute[0] + Environment.NewLine + "[+ following lines in the clipboard window]"
                 *      if (result == MessageBoxResult.No) { return; }
                 *  }
                 * } */


                // Start the pipe server thread
                Logger.WriteLog("Staring pipe server thread. Pipe name: " + sPipeName);
                StartServer(sPipeName, saLinesToExecute);


                // Setup the startup info object
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.CreateNoWindow  = false;
                startInfo.UseShellExecute = false;
                startInfo.WindowStyle     = ProcessWindowStyle.Normal;
                startInfo.FileName        = xrec.Path + @"\" + xrec.Executable;
                startInfo.Arguments       = sAllArguments;

                StartExternalProgram(startInfo);


                // Re-enable the "Execute first line only" checkbox
                if (Properties.Settings.Default.bEnableFirstLineOnly)
                {
                    cBFirstLineOnly.IsChecked = true;
                }
            }
            else
            {
                // Check if a DLL should be called instead of an executable
                if (xrec.IsDll == "true")
                {
                    // Get user approval to run the external command
                    if (GetExecutionApproval(ref xrec) == false)
                    {
                        return;
                    }
                    ;

                    Logger.WriteLog("Loading DLL: " + xrec.Path + @"\" + xrec.Executable + "\r\nDllNameSpaceName.DllClassname: " + xrec.DllNamespaceName + "." + xrec.DllClassName + "\r\nDllMethodName: " + xrec.DllMethodName);

                    try
                    {
                        // Source: https://stackoverflow.com/questions/18362368/loading-dlls-at-runtime-in-c-sharp
                        var DLL = Assembly.LoadFile(xrec.Path + @"\" + xrec.Executable);

                        var theType = DLL.GetType(xrec.DllNamespaceName + "." + xrec.DllClassName);
                        var c       = Activator.CreateInstance(theType);
                        var method  = theType.GetMethod(xrec.DllMethodName);
                        method.Invoke(c, new object[] { saLinesToExecute, xrec.DllConfigFileName });
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Failed to load DLL: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        Logger.WriteLog("Failed to load DLL: " + ex.Message);
                    }

                    // Re-enable the "Execute first line only" checkbox
                    if (Properties.Settings.Default.bEnableFirstLineOnly)
                    {
                        cBFirstLineOnly.IsChecked = true;
                    }

                    return;
                }


                // Check if the shell should handle the execution of the command
                if (xrec.ShellExecute == "true")
                {
                    foreach (string sClipboardLine in saLinesToExecute)
                    {
                        // Get user approval to run the external command
                        if (GetExecutionApproval(ref xrec) == false)
                        {
                            return;
                        }
                        ;

                        Logger.WriteLog("Preparing ShellExecute for: " + sClipboardLine);

                        try
                        {
                            string sAllArguments = "";

                            // Place the main argument into the arguments string
                            // Todo: Add this logic (and similar ones) to the XMLRecord class
                            sAllArguments = xrec.AllArguments.Replace(ClipboardArgumentString, sClipboardLine);

                            // Use the shell / default handler to run the process
                            // Todo: Inform user that this might be dangerous
                            System.Diagnostics.Process.Start(sAllArguments);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("ShellExecute failed: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            Logger.WriteLog("ShellExecute failed: " + ex.Message);
                        }

                        // Re-enable the "Execute first line only" checkbox
                        if (Properties.Settings.Default.bEnableFirstLineOnly)
                        {
                            cBFirstLineOnly.IsChecked = true;
                        }
                    }

                    return;
                }


                foreach (string sClipboardLine in saLinesToExecute)
                {
                    // Get user approval to run the external command
                    if (GetExecutionApproval(ref xrec) == false)
                    {
                        return;
                    }
                    ;

                    /* To delete
                     * if (xrec.CommandIsSafe != "true")
                     * {
                     *  if (cBNotifyExecution.IsChecked.Value)
                     *  {
                     *      MessageBoxResult result = MessageBox.Show("Do you want to run the external command with the following parameter:" + Environment.NewLine + Environment.NewLine + sClipboardLine, "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                     *      if (result == MessageBoxResult.No) { return; }
                     *  }
                     * }  */

                    string sAllArguments = "";

                    // Place the main argument into the arguments string
                    sAllArguments = xrec.AllArguments.Replace(ClipboardArgumentString, sClipboardLine);

                    // Place the optional argument into the arguments string
                    // Todo: the below optional argument must be shown in the messagebox which informs the user about the executed command, e.g. set the messagebox call before the Process.Start call and put the sAllArguments variable into the messagebox
                    sAllArguments = sAllArguments.Replace(OptionalArgumentString, comboOptArg.Text);


                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.CreateNoWindow  = false;
                    startInfo.UseShellExecute = false;
                    startInfo.WindowStyle     = ProcessWindowStyle.Normal;
                    startInfo.FileName        = xrec.Path + @"\" + xrec.Executable;
                    startInfo.Arguments       = sAllArguments;

                    StartExternalProgram(startInfo);


                    // Re-enable the "Execute first line only" checkbox
                    if (Properties.Settings.Default.bEnableFirstLineOnly)
                    {
                        cBFirstLineOnly.IsChecked = true;
                    }
                }
            }
        }
        private void listBoxCommands_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            // Do nothing if no ListBox item was selected
            if (listBoxCommands.SelectedItem == null)
            {
                return;
            }


            // Do nothing if clipboard window is empty
            if (tbClipboardContent.Text == "")
            {
                return;
            }

            // Split the content in the clipboard window into seperate strings, delimiter = new line. Remove lines with no text.
            string[] saClipboardLines = tbClipboardContent.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);


            switch ((listBoxCommands.SelectedItem as FileItem).FileExt.ToLower())
            {
            case ".xml":
                // Get the path to the selected file
                string sXmlBatFile = AppDomain.CurrentDomain.BaseDirectory + @"Tools\" + (listBoxCommands.SelectedItem as FileItem).FileName + (listBoxCommands.SelectedItem as FileItem).FileExt;

                XMLRecord xrec = new XMLRecord(sXmlBatFile);

                if (cBFirstLineOnly.IsChecked.Value)
                {
                    string[] saTheFirstLine = new string[] { saClipboardLines[0] };
                    RunXmlCommand(ref saTheFirstLine, xrec);
                }
                else
                {
                    // Check if more than N lines and show a warning message
                    if (tbClipboardContent.LineCount >= Properties.Settings.Default.uiExecutionWarningCount)
                    {
                        MessageBoxResult result = MessageBox.Show("You are about to execute the selected command " + tbClipboardContent.LineCount.ToString() + " times." + Environment.NewLine + Environment.NewLine + "Click Yes to continue.", "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.No)
                        {
                            return;
                        }
                    }

                    RunXmlCommand(ref saClipboardLines, xrec);
                }
                break;


            case ".bat":
            case ".cmd":
            case ".ps1":
                if (cBFirstLineOnly.IsChecked.Value)
                {
                    RunCommand(saClipboardLines[0]);
                }
                else
                {
                    // Check if more than N lines and show a warning message
                    if (tbClipboardContent.LineCount >= Properties.Settings.Default.uiExecutionWarningCount)
                    {
                        MessageBoxResult result = MessageBox.Show("You are about to execute the selected command " + tbClipboardContent.LineCount.ToString() + " times." + Environment.NewLine + Environment.NewLine + "Click Yes to continue.", "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.No)
                        {
                            return;
                        }
                    }
                    foreach (string line in saClipboardLines)
                    {
                        RunCommand(line);
                    }
                }
                break;


            default:
                MessageBox.Show("File type not supported.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
Ejemplo n.º 5
0
        private void RunXmlCommand(ref string[] saLinesToExecute, XMLRecord xrec)
        {
            // Check if more than N lines and show a warning message
            if (tbClipboardContent.LineCount >= Properties.Settings.Default.uiExecutionWarningCount)
            {
                MessageBoxResult result = MessageBox.Show("You are about to execute the selected command " + tbClipboardContent.LineCount.ToString() + " times." + Environment.NewLine + Environment.NewLine + "Click Yes to continue.", "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.No)
                {
                    return;
                }
            }


            Logger.WriteLog("Data from XML file: \nPath: " + xrec.Path + "\nExecutable: " + xrec.Executable + "\nClass: " + xrec.Class + "\nDescription: " + xrec.Description + "\nStaticArguments: " + xrec.AllArguments);

            if (xrec.UsePipe == "true")
            {
                string sAllArguments = "";

                // Create a temporary pipe name
                string sPipeName = Guid.NewGuid().ToString();


                // Place the optional argument into the arguments string
                // Todo: the below optional argument must be shown in the messagebox which informs the user about the executed command, e.g. set the messagebox call before the Process.Start call and put the sAllArguments variable into the messagebox
                sAllArguments = xrec.AllArguments.Replace(OptionalArgumentString, comboOptArg.Text);

                // Add the pipe name to the command line
                sAllArguments = sAllArguments.Replace(PipeNameString, sPipeName);


                // Get user approval to run the external command
                if (xrec.CommandIsSafe != "true")
                {
                    if (cBNotifyExecution.IsChecked.Value)
                    {
                        // TODO: add note to the message that the command might receive all lines through the pipe
                        MessageBoxResult result = MessageBox.Show("Do you want to run the following external command:" + Environment.NewLine + Environment.NewLine + xrec.Path + @"\" + xrec.Executable + " " + sAllArguments, "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        // + saLinesToExecute[0] + Environment.NewLine + "[+ following lines in the clipboard window]"
                        if (result == MessageBoxResult.No)
                        {
                            return;
                        }
                    }
                }


                // Start the pipe server thread
                Logger.WriteLog("Staring pipe server thread. Pipe name: " + sPipeName);
                StartServer(sPipeName, saLinesToExecute);


                // Setup the startup info object
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.CreateNoWindow  = false;
                startInfo.UseShellExecute = false;
                startInfo.WindowStyle     = ProcessWindowStyle.Normal;
                startInfo.FileName        = xrec.Path + @"\" + xrec.Executable;
                startInfo.Arguments       = sAllArguments;

                StartExternalProgram(startInfo);


                // Re-enable the "Execute first line only" checkbox
                //if (cbEnableFirstLineOnly.IsChecked.Value) { cBFirstLineOnly.IsChecked = true; }
                if (Properties.Settings.Default.bEnableFirstLineOnly)
                {
                    cBFirstLineOnly.IsChecked = true;
                }
            }
            else
            {
                foreach (string sClipboardLine in saLinesToExecute)
                {
                    if (xrec.CommandIsSafe != "true")
                    {
                        if (cBNotifyExecution.IsChecked.Value)
                        {
                            MessageBoxResult result = MessageBox.Show("Do you want to run the external command with the following parameter:" + Environment.NewLine + Environment.NewLine + sClipboardLine, "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                            if (result == MessageBoxResult.No)
                            {
                                return;
                            }
                        }
                    }

                    string sAllArguments = "";

                    // Place the main argument into the arguments string
                    sAllArguments = xrec.AllArguments.Replace(ClipboardArgumentString, sClipboardLine);

                    // Place the optional argument into the arguments string
                    // Todo: the below optional argument must be shown in the messagebox which informs the user about the executed command, e.g. set the messagebox call before the Process.Start call and put the sAllArguments variable into the messagebox
                    sAllArguments = sAllArguments.Replace(OptionalArgumentString, comboOptArg.Text);


                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.CreateNoWindow  = false;
                    startInfo.UseShellExecute = false;
                    startInfo.WindowStyle     = ProcessWindowStyle.Normal;
                    startInfo.FileName        = xrec.Path + @"\" + xrec.Executable;
                    startInfo.Arguments       = sAllArguments;

                    StartExternalProgram(startInfo);


                    // Re-enable the "Execute first line only" checkbox
                    //if (cbEnableFirstLineOnly.IsChecked.Value) { cBFirstLineOnly.IsChecked = true; }
                    if (Properties.Settings.Default.bEnableFirstLineOnly)
                    {
                        cBFirstLineOnly.IsChecked = true;
                    }
                }
            }
        }