Exemplo n.º 1
0
 /// <summary>Execute a SPSL script Async</summary>
 /// <param name="scriptArgs">A <seealso cref="ExecuteScriptEventArgs"/> object containing the script to execute and parameters</param>
 public static void BeginExecuteScript(ExecuteScriptEventArgs scriptArgs)
 {
     string[] scriptlines = scriptArgs.Script.Split('\n');
     if (scriptlines.Length > 0 &&
         scriptArgs.IsSPSL)
     {
         new System.Threading.Thread(delegate()
         {
             foreach (string line in scriptlines)
             {
                 TryParseScriptLine(line, out var command);
                 command?.SendToTerminal(scriptArgs.Handle.ToInt32());
             }
         }).Start();
     }
 }
Exemplo n.º 2
0
        /// <summary>Open a new putty window with its settings being passed in a <seealso cref="SessionData"/> object</summary>
        /// <param name="session">The <seealso cref="SessionData"/> object containing the settings</param>
        public static CtlPuttyPanel OpenPuttySession(SessionData session)
        {
            Log.InfoFormat("Opening putty session, id={0}", session == null ? "" : session.SessionId);
            CtlPuttyPanel panel = null;

            if (session != null)
            {
                String Executable = PuttyStartInfo.GetExecutable(session);
                if (String.IsNullOrEmpty(Executable))
                {
                    MessageBox.Show(
                        string.Format(LocalizedText.SuperPuTTY_Error_trying_to_create_session_Executable_not_set, session, session.Proto),
                        LocalizedText.SuperPuTTY_OpenPuttySession_Failed_to_create_a_session, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(null);
                }
                if (!File.Exists(Executable))
                {
                    MessageBox.Show(string.Format(LocalizedText.SuperPuTTY_Error_trying_to_create_session_Executable_not_found, session, session.Proto, Executable)
                                    , LocalizedText.SuperPuTTY_OpenPuttySession_Failed_to_create_a_session, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(null);
                }

                // This is the callback fired when the panel containing the terminal is closed
                // We use this to save the last docking location and to close the panel
                void ClosedCallback(bool error)
                {
                    if (panel != null)
                    {
                        // save the last dockstate (if it has been changed)
                        if (session.LastDockstate != panel.DockState && panel.DockState != DockState.Unknown && panel.DockState != DockState.Hidden)
                        {
                            session.LastDockstate = panel.DockState;
                            SaveSessions();
                        }

                        if (panel.InvokeRequired)
                        {
                            panel.BeginInvoke((MethodInvoker) delegate { panel.Close(); });
                        }
                        else
                        {
                            panel.Close();
                        }
                    }
                }

                try {
                    panel = new CtlPuttyPanel(session, ClosedCallback);

                    ApplyDockRestrictions(panel);
                    ApplyIconForWindow(panel, session);
                    panel.Show(MainForm.DockPanel, session.LastDockstate);
                    ReportStatus("Opened session: {0} [{1}]", session.SessionId, session.Proto);

                    if (!String.IsNullOrEmpty(session.SPSLFileName) &&
                        File.Exists(session.SPSLFileName))
                    {
                        ExecuteScriptEventArgs scriptArgs = new ExecuteScriptEventArgs()
                        {
                            Script = File.ReadAllText(session.SPSLFileName), Handle = panel.AppPanel.AppWindowHandle
                        };
                        if (!String.IsNullOrEmpty(scriptArgs.Script))
                        {
                            SPSL.BeginExecuteScript(scriptArgs);
                        }
                    }
                } catch (InvalidOperationException ex)
                {
                    MessageBox.Show(
                        string.Format(LocalizedText.SuperPuTTY_Error_trying_to_create_session, ex.Message),
                        LocalizedText.SuperPuTTY_OpenPuttySession_Failed_to_create_session_panel,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            return(panel);
        }