/// <summary> /// Disable module /// </summary> public void Exit() { Syslog.Log("Unloading module: " + Name); try { if (!Hook_OnUnload()) { Syslog.Log("Unable to unload module, forcefully removed from memory: " + Name, true); } IsWorking = false; RestartOnModuleCrash = false; if (thread != null) { Syslog.Log("Terminating module: " + Name, true); if (RestartOnModuleCrash) { RestartOnModuleCrash = false; } Core.ThreadManager.KillThread(thread); } ExtensionHandler.UnregisterMod(this); } catch (Exception fail) { Core.HandleException(fail); } }
public override void Load() { try { Core.Help.Register("labs-on", "Turn on the labs tool"); Core.Help.Register("labs-off", "Turn off the labs tool"); Core.Help.Register("labs-resolve", "Retrieve information about object"); Core.Help.Register("labs-info", "Display all known info about the object"); Core.Help.Register("labs-user", "Display information about user"); Core.Help.Register("labs-project-info", "Display information about the project"); Core.Help.Register("labs-project-instances", "List all instances in a project"); Core.Help.Register("labs-project-users", "Display all users in a project"); while (IsWorking) { JSON(); Thread.Sleep(200000); } } catch (ThreadAbortException) { } catch (Exception f) { Core.HandleException(f, "Labs"); } }
public static void deleteInstance(Instance instance) { try { lock (Instances) { if (Instances.Contains(instance)) { Instances.Remove(instance); } } lock (ProjectList) { foreach (Nova project in ProjectList) { if (project.instances.Contains(instance)) { project.instances.Remove(instance); } } } } catch (Exception r) { Core.HandleException(r, "Labs"); } }
/// <summary> /// Intialise module /// </summary> /// <param name="module"></param> public static void InitialiseMod(Module module) { if (string.IsNullOrEmpty(module.Name)) { Syslog.Log("This module has invalid name and was terminated to prevent troubles", true); throw new WmibException("Invalid name"); } module.Date = DateTime.Now; if (Module.Exist(module.Name)) { Syslog.Log("This module is already registered " + module.Name + " this new instance was terminated to prevent troubles", true); throw new WmibException("This module is already registered"); } try { lock (module) { Syslog.Log("Loading module: " + module.Name + " v" + module.Version); Extensions.Add(module); } module.Init(); } catch (Exception fail) { module.IsWorking = false; Syslog.Log("Unable to create instance of " + module.Name); Core.HandleException(fail); } }
public virtual void Launch(CommandParams parameter) { try { if (this.channelOnly && parameter.SourceChannel == null) { return; } if (this.RequiredPermission != null) { if (parameter.SourceChannel != null) { if (!parameter.SourceChannel.SystemUsers.IsApproved(parameter.User, requiredPermission)) { if (!parameter.SourceChannel.SuppressWarnings && !SilentErrors) { IRC.DeliverMessage(messages.Localize("PermissionDenied", parameter.SourceChannel.Language), parameter.SourceChannel); } // user doesn't have permission to run this command return; } } else if (!Security.IsGloballyApproved(parameter.User, RequiredPermission)) { IRC.DeliverMessage(messages.Localize("PermissionDenied"), parameter.User, libirc.Defs.Priority.Low); } } this.action(parameter); } catch (Exception fail) { Core.HandleException(fail); } }
protected override void __evt_JOIN(NetworkChannelEventArgs args) { if (args.ChannelName == Configuration.System.DebugChan && this.instance != Instance.PrimaryInstance) { return; } Channel channel = Core.GetChannel(args.ChannelName); if (channel != null) { foreach (Module module in ExtensionHandler.ExtensionList) { try { if (module.IsWorking) { module.Hook_Join(channel, args.SourceInfo); } } catch (Exception fail) { Syslog.Log("MODULE: exception at Hook_Join in " + module.Name, true); Core.HandleException(fail); } } } }
protected override void __evt_PART(NetworkChannelDataEventArgs args) { if (args.ChannelName == Configuration.System.DebugChan && this.instance != Instance.PrimaryInstance) { return; } Channel channel = Core.GetChannel(args.ChannelName); if (channel != null) { foreach (Module module in ExtensionHandler.ExtensionList) { if (!module.IsWorking) { continue; } try { module.Hook_Part(channel, args.SourceInfo); } catch (Exception fail) { Core.HandleException(fail); } } } }
private static void ExecuteThread() { try { TcpListener server = new TcpListener(IPAddress.Any, Configuration.Network.SystemPort); server.Start(); Online = true; Syslog.WriteNow("Network console is online on port: " + Configuration.Network.SystemPort); while (Core.IsRunning) { TcpClient connection = server.AcceptTcpClient(); Session session = new Session(); lock (SessionList) { SessionList.Add(session); } Thread client = new Thread(session.ThreadExec) { Name = "Telnet:" + connection.Client.RemoteEndPoint }; Core.ThreadManager.RegisterThread(client); client.Start(connection); Thread.Sleep(100); } } catch (Exception fail) { Online = false; Syslog.WriteNow("Network console is down", true); Core.HandleException(fail); } Core.ThreadManager.UnregisterThread(Thread.CurrentThread); }
private void Exec() { while (this.IsActive && Core.IsRunning) { try { this.Disconnect(); this.Connect(); while (!this.IsWorking && !this.Protocol.IsDisconnected) { // we need to wait for the irc handler to connect to irc Thread.Sleep(100); } // now we can finally join all channels Join(); // then we just sleep while (this.Network.IsConnected) { Thread.Sleep(2000); } // in case we got disconnected, we log it and restart the procedure Syslog.WarningLog("Disconnected from irc network on " + Nick); Thread.Sleep(20000); } catch (ThreadAbortException) { Syslog.DebugLog("Terminated primary thread for instance " + Nick); return; } catch (IOException fail) { if (this.IsActive) { Syslog.ErrorLog("Failure of primary thread of instance " + Nick + " attempting to recover"); Core.HandleException(fail); } else { return; } } catch (Exception fail) { Core.HandleException(fail); if (this.IsActive) { Syslog.ErrorLog("Failure of primary thread of instance " + Nick + " attempting to recover"); } else { return; } Thread.Sleep(20000); } } Core.ThreadManager.UnregisterThread(Thread.CurrentThread); }
private void Exec() { try { Thread.Sleep(8000); if (unwritten.PendingRows.Count == 0 && File.Exists(Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml")) { File.Delete(Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml"); } while (Core.IsRunning) { if (unwritten.PendingRows.Count > 0) { int count; Syslog.WarningLog("Performing recovery of " + unwritten.PendingRows.Count + " MySQL rows"); Recovering = true; List <SerializedRow> rows = new List <SerializedRow>(); lock (unwritten.PendingRows) { count = unwritten.PendingRows.Count; rows.AddRange(unwritten.PendingRows); unwritten.PendingRows.Clear(); } int recovered = 0; foreach (SerializedRow row in rows) { if (InsertRow(row.table, row.row)) { recovered++; } else { Syslog.DebugLog("Failed to recover 1 row", 2); } } Syslog.WarningLog("Recovery finished, recovered " + recovered + " of total " + count); Recovering = false; FlushRows(); Thread.Sleep(200000); if (unwritten.PendingRows.Count == 0 && File.Exists(Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml")) { File.Delete(Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml"); } } Thread.Sleep(200); } } catch (Exception fail) { Core.HandleException(fail); Syslog.ErrorLog("Recovery thread for Mysql is down"); } }
/// <summary> /// Set config /// </summary> /// <param name="chan"></param> /// <param name="name"></param> /// <param name="data"></param> public static void SetConfig(Channel chan, string name, string data) { try { if (chan != null) { chan.Extension_SetConfig(name, data); } } catch (Exception fail) { Core.HandleException(fail); } }
/// <summary> /// Insert a line to storage writer /// </summary> /// <param name="File"></param> /// <param name="Text"></param> /// <param name="Delayed"></param> public static void InsertLine(string File, string Text, bool Delayed = true) { try { lock (Data) { Data.Add(new STI(Text, File, Delayed)); } } catch (Exception crashed) { Core.HandleException(crashed); } }
/// <summary> /// Initializes a new instance of the <see cref="wmib.Channel"/> class. /// </summary> /// <param name='name'> /// Name. /// </param> public Channel(string name) { Name = name; Suppress = false; SystemUsers = new Security(this); LoadConfig(); if (DefaultInstance == "any") { PrimaryInstance = Instance.GetInstance(); // we need to save the instance so that next time bot reconnect to bouncer it uses the same instance DefaultInstance = PrimaryInstance.Nick; SaveConfig(); } else { if (!Instance.Instances.ContainsKey(DefaultInstance)) { Syslog.WarningLog("There is no instance " + DefaultInstance + " reassigning channel " + this.Name + " to a different instance"); this.PrimaryInstance = Instance.GetInstance(); Syslog.Log("Reassigned to " + this.PrimaryInstance.Nick); } else { PrimaryInstance = Instance.Instances[DefaultInstance]; } } if (!Directory.Exists(Configuration.WebPages.HtmlPath)) { Directory.CreateDirectory(Configuration.WebPages.HtmlPath); } foreach (Module module in ExtensionHandler.ExtensionList) { try { if (module.IsWorking) { Channel self = this; module.Hook_Channel(self); } } catch (Exception fail) { Syslog.Log("MODULE: exception at Hook_Channel in " + module.Name, true); Core.HandleException(fail); } } }
private static bool Download(string file, string where) { try { ServicePointManager.ServerCertificateValidationCallback = Validator; WebClient _b = new WebClient(); _b.DownloadFile(file, where); return(true); } catch (Exception fail) { Core.HandleException(fail, "Labs"); } return(false); }
private void Exec() { try { Load(); Syslog.Log("Module terminated: " + Name); IsWorking = false; Core.ThreadManager.UnregisterThread(thread); } catch (ThreadAbortException) { Syslog.Log("Module terminated: " + Name); Core.ThreadManager.UnregisterThread(thread); return; } catch (Exception f) { Core.HandleException(f); IsWorking = false; Syslog.Log("Module crashed: " + Name, true); } while (Core.IsRunning && RestartOnModuleCrash) { try { Warning = true; IsWorking = true; Syslog.Log("Restarting the module: " + Name, true); Load(); Syslog.Log("Module terminated: " + Name); IsWorking = false; } catch (ThreadAbortException) { Syslog.Log("Module terminated: " + Name); Core.ThreadManager.UnregisterThread(thread); return; } catch (Exception f) { Core.HandleException(f); IsWorking = false; Syslog.Log("Module crashed: " + Name, true); } } }
private static void SelfAct(string message, Channel channel) { foreach (Module module in ExtensionHandler.ExtensionList) { try { if (module.IsWorking) { module.Hook_OnSelf(channel, new libirc.UserInfo(Configuration.IRC.NickName, Configuration.IRC.Ident, GetSelfHost(channel)), message, true); } } catch (Exception fail) { Core.HandleException(fail, module.Name); } } }
/// <summary> /// Load a binary module /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool LoadAllModulesInLibrary(string path) { try { if (File.Exists(path)) { Assembly library = Assembly.LoadFrom(path); if (library == null) { Syslog.WarningLog("Unable to load " + path + " because the file can't be read"); return(false); } Type[] types = library.GetTypes(); foreach (Type type in types) { if (type.IsSubclassOf(typeof(Module))) { // For recall later _moduleTypes.Add(type); if (ShouldCreateModuleOnStartup(type)) { CreateModule(type); } else { Syslog.DebugLog("Not registering module (type " + type.Name + ") because it's not in a module list"); } } } return(true); } Syslog.Log("Unable to load " + path + " because the file can't be read", true); } catch (Exception fail) { Core.HandleException(fail); } return(false); }
public static void IrcReloadChannelConf(Channel Channel) { foreach (Module module in ExtensionHandler.ExtensionList) { try { if (module.IsWorking) { module.Hook_ReloadConfig(Channel); } } catch (Exception fail) { Syslog.Log("MODULE: exception at Hook_Reload in " + module.Name); Core.HandleException(fail, module.Name); } } }
/// <summary> /// Get an object created by extension of name /// </summary> /// <param name="name"></param> /// <returns></returns> public object RetrieveObject(string name) { try { lock (ExtensionObjects) { if (ExtensionObjects.ContainsKey(name)) { return(ExtensionObjects[name]); } } } catch (Exception er) { Core.HandleException(er); } return(null); }
/// <summary> /// Return true if this module is already loaded /// </summary> /// <param name="Name"></param> /// <returns></returns> public static bool Exist(string Name) { try { foreach (Module x in ExtensionHandler.ExtensionList) { if (x.Name == Name) { return(true); } } } catch (Exception f) { Core.HandleException(f); } return(false); }
protected override void __evt_QUIT(NetworkGenericDataEventArgs args) { foreach (Module module in ExtensionHandler.ExtensionList) { if (!module.IsWorking) { continue; } try { module.Hook_Quit(args.SourceInfo, args.Message); } catch (Exception fail) { Syslog.Log("MODULE: exception at Hook_Quit in " + module.Name, true); Core.HandleException(fail); } } foreach (Channel channel in instance.ChannelList) { if (channel.ContainsUser(args.SourceInfo.Nick)) { foreach (Module module in ExtensionHandler.ExtensionList) { if (!module.IsWorking) { continue; } try { module.Hook_ChannelQuit(channel, args.SourceInfo, args.Message); } catch (Exception fail) { Syslog.Log("MODULE: exception at Hook_ChannelQuit in " + module.Name, true); Core.HandleException(fail); } } } } }
public static void IrcKick(Channel Channel, libirc.UserInfo Source, string Target) { foreach (Module module in ExtensionHandler.ExtensionList) { if (!module.IsWorking) { continue; } try { module.Hook_Kick(Channel, Source, Target); } catch (Exception fail) { Syslog.Log("MODULE: exception at Hook_Kick in " + module.Name, true); Core.HandleException(fail, module.Name); } } }
/// <summary> /// This is used to handle UNIX signals /// </summary> /// <param name='sender'>Sender</param> /// <param name='args'>Arguments</param> protected static void SigInt(object sender, ConsoleCancelEventArgs args) { if (!Core.IsRunning) { // in case that user hit ctrl + c multiple times, we don't want to // call this, once is just enough return; } Syslog.WriteNow("SIGINT - Shutting down", true); try { Core.Kill(); } catch (Exception fail) { Core.HandleException(fail); } Syslog.WriteNow("Terminated (emergency)"); Process.GetCurrentProcess().Kill(); }
/// <summary> /// Remove object from memory /// </summary> /// <param name="Nm">Name of object</param> /// <returns></returns> public bool UnregisterObject(string Nm) { try { lock (ExtensionObjects) { if (!ExtensionObjects.ContainsKey(Nm)) { return(true); } ExtensionObjects.Remove(Nm); return(true); } } catch (Exception er) { Core.HandleException(er); } return(false); }
/// <summary> /// Register a new object in memory /// </summary> /// <param name="Ob">Data</param> /// <param name="Nm">Name</param> /// <returns></returns> public bool RegisterObject(object Ob, string Nm) { try { lock (ExtensionObjects) { if (ExtensionObjects.ContainsKey(Nm)) { return(false); } ExtensionObjects.Add(Nm, Ob); return(true); } } catch (Exception er) { Core.HandleException(er); return(false); } }
/// <summary> /// Get a config /// </summary> /// <param name="chan"></param> /// <param name="name"></param> /// <param name="invalid"></param> /// <returns></returns> public static int GetConfig(Channel chan, string name, int invalid) { try { if (chan != null) { string value = chan.Extension_GetConfig(name); int result; if (int.TryParse(value, out result)) { return(result); } } } catch (Exception fail) { Core.HandleException(fail); } return(invalid); }
/// <summary> /// Get config /// </summary> /// <param name="chan"></param> /// <param name="name"></param> /// <param name="invalid"></param> /// <returns></returns> public static string GetConfig(Channel chan, string name, string invalid) { try { if (chan != null) { string result = chan.Extension_GetConfig(name); if (result == null) { return(invalid); } return(result); } } catch (Exception fail) { Core.HandleException(fail); } return(invalid); }
private void FlushRows() { if (Recovering) { return; } // prevent multiple threads calling this function at same time lock (this) { string file = Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml"; if (File.Exists(file)) { Core.BackupData(file); if (!File.Exists(Configuration.TempName(file))) { Syslog.WarningLog("Unable to create backup file for " + file); return; } } try { File.Delete(file); XmlSerializer xs = new XmlSerializer(typeof(Unwritten)); StreamWriter writer = File.AppendText(file); lock (unwritten) { xs.Serialize(writer, unwritten); } writer.Close(); if (File.Exists(Configuration.TempName(file))) { File.Delete(Configuration.TempName(file)); } } catch (Exception fail) { Core.HandleException(fail); Syslog.WarningLog("Recovering the mysql unwritten dump because of exception to: " + file); Core.RecoverFile(file); } } }
public static bool Trusted(string message, string user, string host) { try { if (message.StartsWith(Configuration.System.CommandPrefix + "trusted ")) { Channel ch = Core.GetChannel(message.Substring("xtrusted ".Length)); if (ch != null) { IRC.DeliverMessage(messages.Localize("TrustedUserList", ch.Language) + ch.SystemUsers.ListAll(), user); return(true); } IRC.DeliverMessage("There is no such a channel I know of", user); return(true); } } catch (Exception fail) { Core.HandleException(fail); } return(false); }
/// <summary> /// Create a thread and load the module /// </summary> public void Init() { try { IsWorking = true; Hook_OnRegister(); if (HasSeparateThreadInstance) { thread = new Thread(Exec) { Name = "Module:" + Name }; Core.ThreadManager.RegisterThread(thread); thread.Start(); } } catch (Exception f) { Core.HandleException(f); } }