/// <summary> /// Join channel /// </summary> /// <param name="chan">Channel</param> /// <param name="user">User</param> /// <param name="host">Host</param> /// <param name="message">Message</param> public static void addChannel(config.channel chan, string user, string host, string message) { try { if (message.StartsWith(config.CommandPrefix + "add")) { if (chan.Users.IsApproved(user, host, "admin")) { while (!core.FinishedJoining) { core.Log("Postponing request to join because bot is still loading", true); Thread.Sleep(2000); } if (message.Contains(" ")) { string channel = message.Substring(message.IndexOf(" ") + 1); if (!validFile(channel) || (channel.Contains("#") == false)) { irc._SlowQueue.DeliverMessage(messages.get("InvalidName", chan.Language), chan); return; } lock (config.channels) { foreach (config.channel cu in config.channels) { if (channel == cu.Name) { irc._SlowQueue.DeliverMessage(messages.get("ChannelIn", chan.Language), chan); return; } } } bool existing = config.channel.channelExist(channel); config.channel xx = new config.channel(channel); lock (config.channels) { config.channels.Add(xx); } config.Save(); xx.instance.irc.SendData("JOIN " + channel); Thread.Sleep(100); config.channel Chan = getChannel(channel); if (!existing) { Chan.Users.addUser("admin", IRCTrust.normalize(user) + "!.*@" + IRCTrust.normalize(host)); } return; } chan.instance.irc.Message(messages.get("InvalidName", chan.Language), chan.Name); return; } irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan); } } catch (Exception b) { handleException(b); } }
private static void Main(string[] args) { try { config.UpTime = DateTime.Now; Thread logger = new Thread(Logging.Exec); core.domain = AppDomain.CurrentDomain; ProcessVerbosity(args); WriteNow(config.Version); WriteNow("Loading..."); logger.Start(); Console.CancelKeyPress += myHandler; messages.LoadLD(); if (config.Load() != 0) { WriteNow("Error while loading the config file, exiting", true); Environment.Exit(-2); return; } Terminal.Init(); core.Help.CreateHelp(); core.WriterThread = new System.Threading.Thread(StorageWriter.Core); core.WriterThread.Start(); if (core.DatabaseServerIsAvailable) { Log("Initializing MySQL"); core.DB = new WMIBMySQL(); } Log("Loading modules"); core.SearchMods(); IRCTrust.Global(); Log("Connecting"); core.Connect(); } catch (Exception fatal) { WriteNow("ERROR: bot crashed, bellow is debugging information"); Console.WriteLine("------------------------------------------------------------------------"); Console.WriteLine("Description: " + fatal.Message); Console.WriteLine("Stack trace: " + fatal.StackTrace); Environment.Exit(-2); } }
/// <summary> /// Remove all refs /// </summary> public void Remove() { if (IsRemoved) { core.DebugLog("Channel is already removed"); return; } Users = null; lock (ExtensionData) { ExtensionData.Clear(); } lock (ExtensionObjects) { ExtensionObjects.Clear(); } shared = null; SharedChans.Clear(); SharedLinkedChan.Clear(); }
/// <summary> /// Constructor /// </summary> /// <param name="name">Channel</param> public channel(string name) { Name = name; conf = ""; //Info = true; Language = "en"; sharedlink = new List<channel>(); shared = ""; suppress = false; //Feed = false; //Logged = false; LoadConfig(); if (!Directory.Exists(config.path_txt)) { Directory.CreateDirectory(config.path_txt); } if (!Directory.Exists(config.path_txt + "/" + Name)) { Directory.CreateDirectory(config.path_txt + "/" + Name); } if (!Directory.Exists(config.path_htm)) { Directory.CreateDirectory(config.path_htm); } if (!Directory.Exists(config.path_htm + Path.DirectorySeparatorChar + Name)) { Directory.CreateDirectory(config.path_htm + Path.DirectorySeparatorChar + Name); } LogDir = Path.DirectorySeparatorChar + Name + Path.DirectorySeparatorChar; Users = new IRCTrust(Name); lock (Module.module) { foreach (Module module in Module.module) { try { if (module.working) { config.channel self = this; module.Hook_Channel(self); } } catch (Exception fail) { core.Log("MODULE: exception at Hook_Channel in " + module.Name, true); core.handleException(fail); } } } }
/// <summary> /// Remove all refs /// </summary> public void Remove() { if (isRemoved) { core.DebugLog("Channel is already removed"); return; } Users = null; lock (ExtensionData) { ExtensionData.Clear(); } lock (ExtensionObjects) { ExtensionObjects.Clear(); } shared = null; SharedChans.Clear(); SharedLinkedChan.Clear(); }
/// <summary> /// Constructor /// </summary> /// <param name="name">Channel</param> public channel(string name) { Name = name; conf = ""; Language = "en"; SharedLinkedChan = new List<channel>(); shared = ""; suppress = false; LoadConfig(); if (DefaultInstance == "any") { instance = core.getInstance(); // we need to save the instance so that next time bot reconnect to bouncer it uses the same instance DefaultInstance = instance.Nick; SaveConfig(); } else { lock (core.Instances) { if (!core.Instances.ContainsKey(DefaultInstance)) { core.DebugLog("There is no instance " + DefaultInstance); instance = core.getInstance(); } else { instance = core.Instances[DefaultInstance]; } } } if (!Directory.Exists(path_txt)) { Directory.CreateDirectory(path_txt); } if (!Directory.Exists(path_txt + "/" + Name)) { Directory.CreateDirectory(path_txt + "/" + Name); } if (!Directory.Exists(path_htm)) { Directory.CreateDirectory(path_htm); } if (!Directory.Exists(path_htm + Path.DirectorySeparatorChar + Name)) { Directory.CreateDirectory(path_htm + Path.DirectorySeparatorChar + Name); } LogDir = Path.DirectorySeparatorChar + Name + Path.DirectorySeparatorChar; Users = new IRCTrust(Name); lock (Module.module) { foreach (Module module in Module.module) { try { if (module.working) { channel self = this; module.Hook_Channel(self); } } catch (Exception fail) { core.Log("MODULE: exception at Hook_Channel in " + module.Name, true); core.handleException(fail); } } } }
/// <summary> /// Constructor /// </summary> /// <param name="name">Channel</param> public channel(string name) { Name = name; conf = ""; Language = "en"; SharedLinkedChan = new List <channel>(); shared = ""; suppress = false; LoadConfig(); if (DefaultInstance == "any") { instance = core.getInstance(); // we need to save the instance so that next time bot reconnect to bouncer it uses the same instance DefaultInstance = instance.Nick; SaveConfig(); } else { lock (core.Instances) { if (!core.Instances.ContainsKey(DefaultInstance)) { core.DebugLog("There is no instance " + DefaultInstance); instance = core.getInstance(); } else { instance = core.Instances[DefaultInstance]; } } } if (!Directory.Exists(path_txt)) { Directory.CreateDirectory(path_txt); } if (!Directory.Exists(path_txt + "/" + Name)) { Directory.CreateDirectory(path_txt + "/" + Name); } if (!Directory.Exists(path_htm)) { Directory.CreateDirectory(path_htm); } if (!Directory.Exists(path_htm + Path.DirectorySeparatorChar + Name)) { Directory.CreateDirectory(path_htm + Path.DirectorySeparatorChar + Name); } LogDir = Path.DirectorySeparatorChar + Name + Path.DirectorySeparatorChar; Users = new IRCTrust(Name); lock (Module.module) { foreach (Module module in Module.module) { try { if (module.working) { channel self = this; module.Hook_Channel(self); } } catch (Exception fail) { core.Log("MODULE: exception at Hook_Channel in " + module.Name, true); core.handleException(fail); } } } }
private static void HandleClient(object data) { try { System.Net.Sockets.TcpClient connection = (System.Net.Sockets.TcpClient)data; core.DebugLog("Incoming connection from: " + connection.Client.RemoteEndPoint.ToString()); Connections++; connection.NoDelay = true; System.Net.Sockets.NetworkStream ns = connection.GetStream(); System.IO.StreamReader Reader = new System.IO.StreamReader(ns); string text; // give the user access to global cache // save the reference to global cache because we might need it in future System.IO.StreamWriter Writer = new System.IO.StreamWriter(ns); // login Writer.WriteLine("Enter username:"******"Enter password:"******"Invalid user or password, bye"); Writer.Flush(); connection.Close(); Connections--; return; } Writer.WriteLine("Successfuly logged in to wm-bot, I have " + Connections.ToString() + " users logged in"); Writer.Flush(); while (connection.Connected && !Reader.EndOfStream) { text = Reader.ReadLine(); string command = text; List <string> list = new List <string>(); string parameters = ""; if (command.Contains(" ")) { parameters = command.Substring(command.IndexOf(" ") + 1); command = command.Substring(0, command.IndexOf(" ")); if (parameters.Contains(" ")) { list.AddRange(parameters.Split(' ')); } } switch (command.ToLower()) { case "exit": Writer.WriteLine("Good bye"); Writer.Flush(); connection.Close(); Connections--; return; case "info": string result = "Uptime: " + core.getUptime() + Environment.NewLine + "Instances:" + Environment.NewLine; lock (core.Instances) { foreach (Instance instance in core.Instances.Values) { result += instance.Nick + " channels: " + instance.ChannelCount.ToString() + " connected: " + instance.IsConnected.ToString() + " working: " + instance.IsWorking.ToString() + "\n"; } } Writer.WriteLine(result); Writer.Flush(); break; case "help": Writer.WriteLine("Commands:\n" + "exit - shutdown connection\n" + "info - print information about system\n" + "halt - shutdown bot\n" + "traffic-on - turn on traffic logs\n" + "traffic-off - turn off traffic logs\n" + "kill [instance] - disconnect selected instance\n" + "conn [instance] - connect instance\n"); Writer.Flush(); break; case "halt": Writer.WriteLine("Shutting down"); Writer.Flush(); core.Kill(); return; case "traffic-on": config.Logging = true; Writer.WriteLine("Dumping traffic"); Writer.Flush(); break; case "traffic-off": config.Logging = false; Writer.WriteLine("Disabled traf"); Writer.Flush(); break; case "kill": if (core.Instances.ContainsKey(parameters)) { core.Instances[parameters].irc.Disconnect(); Writer.WriteLine("Offline: " + parameters); Writer.Flush(); break; } Writer.WriteLine("Unknown instance: " + parameters); Writer.Flush(); break; case "conn": if (core.Instances.ContainsKey(parameters)) { if (core.Instances[parameters].irc.IsConnected) { Writer.WriteLine("Refusing to connect instance which is already connected: " + parameters); Writer.Flush(); break; } core.Instances[parameters].Init(); Writer.WriteLine("Initializing: " + parameters); Writer.Flush(); int curr = 0; while (curr < 10 && !core.Instances[parameters].IsWorking) { curr++; Thread.Sleep(1000); } if (!core.Instances[parameters].IsWorking) { Writer.WriteLine("Failed to initialize instance"); Writer.Flush(); break; } Writer.WriteLine("Joining channels"); Writer.Flush(); core.Instances[parameters].irc.ChannelsJoined = false; core.Instances[parameters].Join(); curr = 0; while (curr < core.Instances[parameters].ChannelCount && !core.Instances[parameters].irc.ChannelsJoined) { curr++; Thread.Sleep(6000); } if (!core.Instances[parameters].irc.ChannelsJoined) { Writer.WriteLine("Failed to rejoin all channels in time"); Writer.Flush(); break; } Writer.WriteLine("Instance is online: " + parameters); Writer.Flush(); break; } Writer.WriteLine("Unknown instance: " + parameters); Writer.Flush(); break; case "send": if (!parameters.Contains(" ")) { Writer.WriteLine("This command requires 2 parameters"); Writer.Flush(); break; } string to = parameters.Substring(0, parameters.IndexOf(" ")); if (core.Instances.ContainsKey(to)) { if (!core.Instances[to].irc.IsConnected) { Writer.WriteLine("Refusing to send data using instance which is not connected: " + to); Writer.Flush(); break; } core.Instances[to].irc.SendData(parameters.Substring(parameters.IndexOf(" ") + 1)); break; } Writer.WriteLine("I have no such instance dude"); Writer.Flush(); break; default: Writer.WriteLine("Unknown command, try help"); Writer.Flush(); break; } } } catch (Exception fail) { core.handleException(fail); } Connections--; }