public static void LoadFromXML(string xmlPath) { var now = DateTime.Now; XmlDocument xmlDoc; try { xmlDoc = new XmlDocument(); // Create an XML document object xmlDoc.Load(xmlPath); } catch (Exception ex) { throw new ScheduledTasksException(String.Format("Could not open or parse schedule xml file: {0}", ex.Message)); } var schedule = xmlDoc.GetElementsByTagName("job"); for (var i = 0; i < schedule.Count; i++) { var node = schedule[i]; Command cmd; var interval = 0; var loop = -1; DateTime nextRun; try { cmd = new Command(node["cmd"].InnerText); var start = node["start"].InnerText; try { loop = Convert.ToInt16(node["loop"].InnerText); if (loop > -1) { loop++; } } catch { // ignored } if (Regex.Match(start, "^[0-9]{2}:[0-9]{2}:[0-9]{2}$", RegexOptions.IgnoreCase).Success) { interval = 86400; nextRun = DateTime.Today; nextRun = nextRun.AddHours(Convert.ToDouble(start.Substring(0, 2))); nextRun = nextRun.AddMinutes(Convert.ToDouble(start.Substring(3, 2))); nextRun = nextRun.AddSeconds(Convert.ToDouble(start.Substring(6, 2))); if (now > nextRun) { nextRun = nextRun.AddSeconds(interval); } } else if (Regex.Match(start, "^[0-9]{6}$", RegexOptions.IgnoreCase).Success) { nextRun = now.AddHours(Convert.ToDouble(start.Substring(0, 2))); nextRun = nextRun.AddMinutes(Convert.ToDouble(start.Substring(2, 2))); nextRun = nextRun.AddSeconds(Convert.ToDouble(start.Substring(4, 2))); if (Regex.Match(node["runtime"].InnerText, "^[0-9]{6}$", RegexOptions.IgnoreCase).Success) { var runtime = node["runtime"].InnerText; interval = Convert.ToInt16(runtime.Substring(4, 2)) + (Convert.ToInt16(runtime.Substring(2, 2)) * 60) + (Convert.ToInt16(runtime.Substring(0, 2)) * 60 * 60); } } else { throw new ScheduledTasksException("The time field is formatted incorrectly."); } } catch (Exception ex) { throw new ScheduledTasksException(String.Format("Could not open or parse schedule xml file: {0}", ex.Message)); } try { var task = new Task() { _command = cmd, _nextRun = nextRun, _interval = TimeSpan.FromSeconds(interval), _loop = loop }; _tasks.Add(task); } catch (Exception ex) { throw new ScheduledTasksException(String.Format("Error adding task: {0}", ex.Message)); } } }
public static void LoadTasks(IniParser ini) { var _i = -1; while (true) { var now = DateTime.Now; _i++; var command = ini.GetSetting("Tasks", String.Format("Command{0}", _i)); if (String.IsNullOrWhiteSpace(command)) { if (_i == 0) continue; else { AppConsole.Log("Finished loading tasks", ConsoleColor.DarkMagenta); break; } } Command cmd; DateTime nextRun; var interval = TimeSpan.FromDays(1); var loop = 1; try { cmd = new Command(command); } catch (CommandException ex) { AppConsole.Log(String.Format("Error loading task {0}: {1}", _i, ex.Message), ConsoleColor.Red); continue; } var time = ini.GetSetting("Tasks", String.Format("Time{0}", _i)); if (!String.IsNullOrWhiteSpace(time)) { try { var h = Convert.ToDouble(time.Substring(0, 2)); var m = Convert.ToDouble(time.Substring(2, 2)); nextRun = DateTime.Today; nextRun = nextRun.AddHours(h); nextRun = nextRun.AddMinutes(m); if (now > nextRun) { nextRun = nextRun.Add(interval); } } catch (Exception ex) { AppConsole.Log(String.Format("Error loading task: Bad String Time {0}", ex.Message), ConsoleColor.Red); continue; } } else { try { interval = TimeSpan.FromSeconds(Convert.ToInt32(ini.GetSetting("Tasks", String.Format("Interval{0}", _i)))); } catch (Exception ex) { AppConsole.Log(String.Format("Error loading task: Bad String Interval {0}", ex.Message), ConsoleColor.Red); continue; } TimeSpan delay; try { delay = TimeSpan.FromSeconds(Convert.ToDouble(ini.GetSetting("Tasks", String.Format("Delay{0}", _i), "0"))); } catch { delay = new TimeSpan(); } loop = (ini.GetBoolSetting("Tasks", String.Format("Repeat{0}", _i))) ? -1 : 1; nextRun = now.Add(delay); nextRun = nextRun.Add(interval); } var task = new Task() { _command = cmd, _nextRun = nextRun, _loop = loop, _interval = interval }; _tasks.Add(task); } AppConsole.Log(String.Format("Added {0} tasks", _tasks.Count), ConsoleColor.DarkMagenta); }
private void RunCommand(string cmd) { AppConsole.Log(String.Format("New Command From WebRCON: {0}", cmd), ConsoleColor.Blue); var res = new WSBEMessage() { msgtype = "Log", received = DateTime.Now, message = "", type = 2 }; Command command; try { command = new Command(cmd); } catch (Exception ex) { AppConsole.Log(String.Format("Error parsing BECommand string: {0} - {1}", cmd, ex.Message), ConsoleColor.Red); res.message = "Error parsing command"; Send(res.ToString()); return; } _api.SendCommand(command); switch (command.Type) { case Command.CmdType.Lock: res.message = "Server Locked"; break; case Command.CmdType.Unlock: res.message = "Server Unlocked"; break; case Command.CmdType.MaxPing: res.message = String.Format("Max Ping Set To {0}", command.Parameters); break; case Command.CmdType.loadEvents: res.message = "Reloading Events"; break; case Command.CmdType.LoadScripts: res.message = "Reloading Scripts"; break; case Command.CmdType.LoadBans: res.message = "Reloading Bans"; break; case Command.CmdType.RemoveBan: res.message = "Removed ban"; break; case Command.CmdType.AddBan: res.message = String.Format("Added Ban: {0}", command.Parameters); break; } if (res.message != "") { Send(res.ToString()); } }
public static void LoadFromJSON(string jsonPath) { dynamic tasks; var now = DateTime.Now; try { var json = File.ReadAllText(jsonPath); tasks = JsonConvert.DeserializeObject(json); } catch (Exception ex) { throw new ScheduledTasksException(String.Format("Could not open or parse schedule json file: {0}", ex.Message)); } var i = 0; foreach (var item in tasks) { i++; string start; Command cmd; DateTime nextRun; int interval; int loop; try { start = item.start.ToString(); if (String.IsNullOrWhiteSpace(start)) throw new ScheduledTasksException("Tasks need to have a start defined."); } catch (Exception ex) { throw new ScheduledTasksException(String.Format("Could not parse command: {0}", ex.Message)); } try { cmd = new Command(item.cmd.ToString()); } catch (CommandException ex) { throw new ScheduledTasksException(String.Format("Could not parse command: {0}", ex.Message)); } try { loop = Convert.ToInt16(item.loop.ToString()); } catch { loop = -1; } if (loop == 0) { throw new ScheduledTasksException("Loop can not be 0."); } try { interval = Convert.ToInt16(item.interval.ToString()); } catch { interval = -1; } if (Regex.Match(start, "^[0-9]{2}:[0-9]{2}:[0-9]{2}$", RegexOptions.IgnoreCase).Success) { // hh:mm:ss var h = Convert.ToDouble(start.Substring(0, 2)); var m = Convert.ToDouble(start.Substring(3, 2)); var s = Convert.ToDouble(start.Substring(6, 2)); nextRun = DateTime.Today; nextRun = nextRun.AddHours(h); nextRun = nextRun.AddMinutes(m); nextRun = nextRun.AddSeconds(s); interval = 86400; if (now > nextRun) { nextRun = nextRun.AddSeconds(interval); } } else if (Regex.Match(start, "^[0-9]+$", RegexOptions.IgnoreCase).Success) { // hhmmss nextRun = now.AddSeconds(Convert.ToDouble(start)); } else { throw new ScheduledTasksException("The time field is formatted incorrectly."); } var task = new Task() { _command = cmd, _nextRun = nextRun, _interval = TimeSpan.FromSeconds(interval), _loop = loop }; _tasks.Add(task); } }
void onBEMessageReceivedEvent(Message message) { if (!( message.Type == Message.MessageType.ConnectLegacyGUID || message.Type == Message.MessageType.ConnectGUID || (_checkip && message.Type == Message.MessageType.ConnectIP) )) return; Player player; try { player = new Player(message); } catch (Exception ex) { AppConsole.Log(String.Format("Error paring be message: {0}", ex.Message), ConsoleColor.Red); return; } bool check; try { check = _driver.CheckPlayer(player); } catch (Exception ex) { AppConsole.Log(String.Format("Error checking player: {0}", ex.Message), ConsoleColor.Red); Console.WriteLine(ex.StackTrace); return; } var kick = (check && _mode == Mode.Blacklist) || (!check && _mode == Mode.Whitelist); if (kick) { var cmd = new Command() { Type = Command.CmdType.Kick, Parameters = String.Format("{0} {1}", player.Id, _kickMessage) }; AppConsole.Log(String.Format("Kicking {0}", player.Name)); _api.SendCommand(cmd); } }
private void FilterMonitor() { _modified = new Dictionary<string, DateTime>(); foreach (var f in _filters) { var file = Path.Combine(_api.Settings.BePath, f); if (!File.Exists(file)) { AppConsole.Log(String.Format("Could not find BEFilter: {0}", f)); continue; } var info = new FileInfo(file); _modified.Add(file, info.LastWriteTime); } while (!_workerTerminateSignal) { _waitHandle.WaitOne(_interval); if (_workerTerminateSignal) break; foreach (var key in new List<string>(_modified.Keys)) { var info = new FileInfo(key); if (info.LastWriteTime > _modified[key]) { _modified[key] = info.LastWriteTime; AppConsole.Log(String.Format("Filter {0} has been updated.", info.Name)); Command command; if (info.Name == "scripts") { command = new Command() { Type = Command.CmdType.LoadScripts }; } else { command = new Command() { Type = Command.CmdType.loadEvents }; } _api.SendCommand(command); } } } }
public Message SendCommandRes(Command command) { var id = SendCommand(command); Message response = null; while (response == null) { response = GetCommandResponse(id); Thread.Sleep(100); } return response; }
public int SendCommand(Command command) { return _beclient.SendCommand((BattlEyeCommand)command.Type, command.Parameters); }
void _timer_Elapsed(object sender, ElapsedEventArgs e) { var message = _ini.GetSetting("Messages", _messages[_messagePointer]); _messagePointer++; if (String.IsNullOrWhiteSpace(message)) { return; } message = String.Format("{0} {1}", _prefix, message); AppConsole.Log(String.Format("{0}: {1}", Name, message), ConsoleColor.Magenta); var cmd = new Command() { Type = Command.CmdType.Say, Parameters = String.Format("-1 {0}", message) }; _api.SendCommand(cmd); if (_messagePointer >= _messages.Length) { if (_repeat) { _messagePointer = 0; } else { Kill(); } } }