public CmdOutput Report(CmdInput ci) { var options = ci.options as Email_Report_Options; try { List <Attachment> attachments = new List <Attachment>(); string prefix = Guid.NewGuid().ToString().Substring(0, 8); string body = DisplayedMessage.GetMessage(new DisplayedMessageOption() { SkipHead = 1, RemoveTail = 1, IncludeStatusImage = false, IncludeTaskName = options.IncludeTaskName, IncludeTimeStamp = options.IncludeTimeStamp, IncludeCmd = options.IncludeCmd }, true, prefix, attachments); List <string> to = options.To.Split(';').ToList(); EmailUtil.SendEmail(to, options.Subject, body, options.STMP, options.Port, options.User, options.Password, options.From, null, "HTML", null, attachments); } catch (Exception ex) { return(new CmdOutput(false, CmdRunState.EXITWITHERROR, ci.MessageId, ex.Message)); } return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, "Sent report successfully")); }
public CmdOutput List(CmdInput ci) { string cmd = ""; if (ci.Args != null && ci.Args.Count == 1) { cmd = ci.Args[0]; } cmd = GlobalSettings.AvailableCmd.Where(a => a.ModuleName.ToLower() == cmd.ToLower()).Select(a => a.ModuleName).FirstOrDefault(); List <string> tmp; if (cmd == null) { tmp = GlobalSettings.AvailableCmd.Where(a => a.Attr != null).Select(a => a.ModuleName).Distinct().ToList(); } else { tmp = GlobalSettings.AvailableCmd.Where(a => a.Attr != null && a.ModuleName == cmd).Select(a => a.CommandName).Distinct().ToList(); } var msg = ""; if (cmd == null) { msg += "Availble module name:<li>"; msg += string.Join("</li><li>", tmp) + "</li>"; } else { msg += $"Availble command for module {cmd}:<li>"; msg += string.Join($"</li><li>", tmp) + "</li>"; } return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, msg)); }
public CmdOutput AddFiles(CmdInput ci) { Zip_AddFiles_Options options = ci.options as Zip_AddFiles_Options; StringBuilder sb = new StringBuilder(); try { var zfiles = Common.CommFuns.ListTmpFiles(options.FileName); if (zfiles.Count == 0) { return(new CmdOutput(false, CmdRunState.FINISHEDWITHWARNING, ci.MessageId, "No file to add")); } byte[] zip = File.ReadAllBytes(options.ZipFile); Dictionary <string, byte[]> data = new Dictionary <string, byte[]>(); foreach (var f in zfiles) { data.Add(Path.GetFileName(f), File.ReadAllBytes(f)); sb.AppendLine($"Added {f} into zipfile<br/>"); } var ret = ZipUtil.AddFilesIntoZip(zip, data); File.WriteAllBytes(options.ZipFile, ret); } catch (Exception e) { sb.AppendLine($"Failed to add." + e.Message); return(new CmdOutput(false, CmdRunState.EXITWITHERROR, ci.MessageId, sb.ToString())); } return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, sb.ToString())); }
public CmdOutput Encrypt(CmdInput ci) { if (ci == null || ci.Args == null || ci.Args.Count != 1) { return(new CmdOutput(false, CmdRunState.EXITWITHERROR, ci.MessageId, "Invalid command")); } return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, "Encrypted string: " + Encryption.Encrypt(ci.Args[0], "jweoir2389kjhlksd920394hf"))); }
public CmdOutput PasswordEncrypt(CmdInput ci) { if (ci == null || ci.Args == null || ci.Args.Count != 1) { return(new CmdOutput(false, CmdRunState.EXITWITHERROR, ci.MessageId, "Invalid command")); } return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, "Encrypted Password: " + Encryption.EncryptPassword(ci.Args[0]))); }
public CmdOutput RunBatch(CmdInput ci) { var options = ci.options as Command_RunBatch_Options; var cmds = Common.CommFuns.ReadAllLinesFromBatchFile(options.BatchFile); if (cmds == null || cmds.Length == 0) { return(new CmdOutput(false, CmdRunState.INVALID, ci.MessageId, "Invalid batch file")); } foreach (var c in cmds) { GlobalSettings.CmdQueue.Enqueue(c); } return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, $"Loaded batch successfully")); }
public CmdOutput AddVar(CmdInput ci) { Env_AddVar_Options options = ci.options as Env_AddVar_Options; var v = options.VarValue; if (options.Encrypted) { try { v = Encryption.Decrypt(v, "jweoir2389kjhlksd920394hf"); } catch { return(new CmdOutput(false, CmdRunState.EXITWITHERROR, ci.MessageId, "Invalid encrypted value")); } } GlobalSettings.UserVariables.AddOrUpdate(options.VarName?.ToLower(), v, (k, o) => v); return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, $"Add variable {options.VarName} successfully")); }
public CmdOutput Unzip(CmdInput ci) { Zip_Unzip_Options options = ci.options as Zip_Unzip_Options; StringBuilder sb = new StringBuilder(); try { var zfiles = Common.CommFuns.ListTmpFiles(options.FileName); if (zfiles.Count == 0) { return(new CmdOutput(false, CmdRunState.FINISHEDWITHWARNING, ci.MessageId, "No file to unzip")); } foreach (var zf in zfiles) { sb.AppendLine($"Unzip {zf}:</br>"); var files = ZipUtil.UnzipFiles(File.ReadAllBytes(zf)); foreach (var f in files) { var d = Path.Combine(options.Destination, f.Key); var fd = Path.GetDirectoryName(d); if (!Directory.Exists(fd)) { Directory.CreateDirectory(fd); } File.WriteAllBytes(d, f.Value); sb.AppendLine($" {d}</br>"); } } } catch (Exception e) { sb.AppendLine($"Failed to unzip." + e.Message); return(new CmdOutput(false, CmdRunState.EXITWITHERROR, ci.MessageId, sb.ToString())); } return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, sb.ToString())); }
public CmdOutput Save(CmdInput ci) { var options = ci.options as Command_Save_Options; string prefix = "cmdhistory"; string suffix = "txt"; if (options.IncludeOutput) { prefix = "cmdwithoutput"; suffix = "html"; } StringBuilder sb = new StringBuilder(); if (options.IncludeOutput) { sb.Append(DisplayedMessage.GetMessage(new DisplayedMessageOption() { RemoveTail = 1 })); } else { foreach (var c in GlobalSettings.CmdHistory) { sb.AppendLine(c); } } var f = Common.CommFuns.SaveStringToFile(sb.ToString(), prefix, suffix); if (options.ViewOutput) { System.Diagnostics.Process.Start(f); } return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, $"Saved as {f}")); }
public CmdOutput ListVar(CmdInput ci) { return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, Common.CommFuns.Dictionary2HTML(GlobalSettings.UserVariables, "Variable Name", "Variable Value"))); }
public CmdOutput ClearTmpFiles(CmdInput ci) { Common.CommFuns.ClearTmpFile(); return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, "Cleared temp files")); }
public CmdOutput Echo(CmdInput ci) { var options = ci.options as Command_Echo_Options; return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, options.EchoString)); }
public CmdOutput Clear(CmdInput ci) { GlobalSettings.CmdHistory.Clear(); GlobalSettings.Message = GlobalSettings.Message.Where(a => a.MessageId == ci.MessageId).Select(a => a).ToList(); return(new CmdOutput(true, CmdRunState.FINISHED, ci.MessageId, $"Cleared")); }
public static void DispatchCmd() { try { while (true) { if (GlobalSettings.CmdQueue.Count == 0) { Thread.Sleep(500); continue; } var cmd = GlobalSettings.CmdQueue.Dequeue(); GlobalSettings.CmdHistory.Add(cmd); var cm = new CmdStructure(cmd); MessageStructure ms = null; bool isContinue = true; if (!GlobalSettings.AppState.ToolInitialized) { GlobalSettings.Message.Add(new DisplayedMessage() { Start = DateTime.Now, Cmd = cmd, MessageId = Guid.NewGuid().ToString(), State = CmdRunState.EXITWITHERROR, MessageList = new List <MessageStructure>() { new MessageStructure("Fail to initialize, please contact administrator", MessageType.ERROR) } }); isContinue = false; } else if ((ms = cm.ReplaceVariable()) != null) { GlobalSettings.Message.Add(new DisplayedMessage() { Start = DateTime.Now, Cmd = cmd, MessageId = Guid.NewGuid().ToString(), State = CmdRunState.INVALID, MessageList = new List <MessageStructure>() { ms } }); isContinue = false; } else if (!cm.ValidCommand()) { var message = ""; if (!cm.ModuleNameIsValid) { message = "Invalid Command,use <font color=green><b>Command List</b></font> to get help"; } else { message = $"Invalid Command,use <font color=green><b>Command List {cm.ModuleName}</b></font> to get help"; } GlobalSettings.Message.Add(new DisplayedMessage() { Start = DateTime.Now, Cmd = cmd, MessageId = Guid.NewGuid().ToString(), State = CmdRunState.INVALID, MessageList = new List <MessageStructure>() { new MessageStructure(message, MessageType.ERROR) } }); isContinue = false; } else if (!cm.CanRun()) { var message = "Cannot run this command. Make sure already logon"; GlobalSettings.Message.Add(new DisplayedMessage() { Start = DateTime.Now, Cmd = cmd, MessageId = Guid.NewGuid().ToString(), State = CmdRunState.INVALID, MessageList = new List <MessageStructure>() { new MessageStructure(message, MessageType.ERROR) } }); isContinue = false; } else if (cm.RequiredHelp()) { GlobalSettings.Message.Add(new DisplayedMessage() { Start = DateTime.Now, Cmd = cmd, MessageId = Guid.NewGuid().ToString(), State = CmdRunState.FINISHED, MessageList = cm.GetHelp() }); isContinue = false; } else if (!cm.ProcessArgs()) { GlobalSettings.Message.Add(new DisplayedMessage() { Start = DateTime.Now, Cmd = cmd, MessageId = Guid.NewGuid().ToString(), State = CmdRunState.EXITWITHERROR, MessageList = cm.GetHelp(true) }); isContinue = false; } if (!isContinue) { GlobalSettings.AppState.isProcessingCommand = false; GlobalSettings.AppState.NotifyObserver(); GlobalSettings.CmdQueue.Clear(); } else { GlobalSettings.AppState.isProcessingCommand = true; var msgid = Guid.NewGuid().ToString(); MessageStructure tmpms = null; if (cm.Paras != null) { tmpms = (cm.Paras as CmdOption).HtmlName; } var mlist = new List <MessageStructure>(); if (tmpms != null) { tmpms.Message = "Working on " + tmpms.Message + ".........."; mlist.Add(tmpms); } GlobalSettings.Message.Add(new DisplayedMessage() { Start = DateTime.Now, Cmd = cmd, MessageId = msgid, State = CmdRunState.STARTING, MessageList = mlist }); GlobalSettings.AppState.NotifyObserver(); var m = cm.Assembly.CreateInstance(GlobalSettings.MouldeNameSpace + "." + cm.ModuleName); Type thisType = m.GetType(); MethodInfo theMethod = thisType.GetMethod(cm.CommandName); CmdInput ci = new CmdInput(MessageHandler, AddorUpdateVariable, null, Common.CommFuns.SaveDataToFile) { Args = cm.Args?.ToList(), options = cm.Paras, MessageId = msgid, ResourceFolder = GlobalSettings.ToolResourceDir, }; var co = theMethod.Invoke(m, new object[] { ci }) as CmdOutput; if (!co.Result) { //GlobalSettings.CmdQueue.Clear(); } MessageHandler(co); } } } catch (ThreadAbortException abortException) { Console.WriteLine((string)abortException.ExceptionState); } }