using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb; using System.Runtime.InteropServices; using Meebey.SmartIrc4net; namespace MooseBot { class Program { // Client Initialization public static IrcClient Client = new IrcClient(); public static List BotCommands = new List(); public static List ChanCommands = new List(); // Connection Settings public static readonly string Address = "irc.freenode.net"; public static readonly int Port = 6667; // Bot Settings public static string Nick = "MooseBot"; public static string[] Channels = { "#chemistry" }; // Window Settigs public static readonly string WindowTitle = "MooseBot v7.0A - Ownerarium Software"; [DllImport("user32.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); // Database Settings public static readonly string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=Database.mdb"; static void Main() { Console.WriteLine(WindowTitle); Console.WriteLine("Library Loaded {0}", Client.VersionString); // Setting up bot commands BotCommands.Add(new Commands.BroadcastCommand()); BotCommands.Add(new Commands.DisconnectCommand()); BotCommands.Add(new Commands.JoinCommand()); BotCommands.Add(new Commands.MessageCommand()); BotCommands.Add(new Commands.PartCommand()); BotCommands.Add(new Commands.QuitCommand()); BotCommands.Add(new Commands.WindowPropertyCommand()); Console.WriteLine("Finished loading bot commands"); // Setting up channel commands ChanCommands.Add(new Commands.MD5Command()); ChanCommands.Add(new Commands.SHA1Command()); ChanCommands.Add(new Commands.Base64Command()); ChanCommands.Add(new Commands.FactoidLookupCommand()); ChanCommands.Add(new Commands.AddFactoidCommand()); ChanCommands.Add(new Commands.DeleteFactoidCommand()); ChanCommands.Add(new Commands.AddUserCommand()); ChanCommands.Add(new Commands.DeleteUserCommand()); ChanCommands.Add(new Commands.UserInfoCommand()); Console.WriteLine("Finished loading channel commands"); Console.WriteLine(); // Loading users from database #region Users Database Access OleDbConnection dbConn = new OleDbConnection(ConnectionString); OleDbCommand dbCommand = new OleDbCommand("SELECT * FROM Users", dbConn); try { dbConn.Open(); } catch (OleDbException ex) { Console.WriteLine("Database failed to load properly!"); ErrorExit("DB", ex.Message); } OleDbDataReader dbReader = dbCommand.ExecuteReader(); while (dbReader.Read()) { string nick = Convert.ToString(dbReader["Nick"]); string host = Convert.ToString(dbReader["Host"]); User.AccessLevel access = User.ParseAccessLevel(Convert.ToString(dbReader["Access"])); DateTime creationDate = DateTime.Parse(Convert.ToString(dbReader["CreationDate"])); User.Users.Add(new User(nick, host, access, creationDate)); Console.WriteLine("Added User: {0}", nick); } dbConn.Close(); Console.WriteLine("{0} users successfully loaded", User.Count); Console.WriteLine(); #endregion // Loading factoids from database #region Factoid Database Access //dbConn = new OleDbConnection(ConnectionString); dbCommand = new OleDbCommand("SELECT * FROM Factoids", dbConn); try { dbConn.Open(); } catch (OleDbException ex) { Console.WriteLine("Database failed to load properly!"); ErrorExit("DB", ex.Message); } dbReader = dbCommand.ExecuteReader(); while (dbReader.Read()) { string name = Convert.ToString(dbReader["Name"]); string response = Convert.ToString(dbReader["Response"]); FactoidHandler.Add(name, response, false); Console.WriteLine("Added Factoid: {0}", name); } dbConn.Close(); Console.WriteLine("{0} factoids successfully loaded", Factoid.Count); Console.WriteLine(); #endregion Client.OnError += new ErrorEventHandler(Client_OnError); Client.OnConnected += new EventHandler(Client_OnConnected); Client.OnJoin += new JoinEventHandler(Client_OnJoin); Client.OnPart += new PartEventHandler(Client_OnPart); Client.OnQueryMessage += new IrcEventHandler(Client_OnQueryMessage); Client.OnChannelMessage += new IrcEventHandler(Client_OnChannelMessage); Client.OnNickChange += new NickChangeEventHandler(Client_OnNickChange); try { Client.Connect(Address, Port); } catch (ConnectionException ex) { ErrorExit("IRC", ex.Message); } catch (Exception ex) { ErrorExit("App", ex.Message); } Console.WriteLine("Listening mode active"); Client.Listen(); Console.WriteLine("[{0}] Listening mode disabled", DateTime.Now.ToShortTimeString()); // Making our window minimized, but visible ShowWindow(FindWindow(null, WindowTitle), 2); Console.ReadLine(); Environment.Exit(0); } static void Client_OnNickChange(object sender, NickChangeEventArgs e) { Console.WriteLine("[{0}] {1} is now known as {2}", DateTime.Now.ToShortTimeString(), e.OldNickname, e.NewNickname); } static void Client_OnChannelMessage(object sender, IrcEventArgs e) { Console.WriteLine("[{0}] [{1}] {2}: {3}", DateTime.Now.ToShortTimeString(), e.Data.Channel, e.Data.Nick, e.Data.Message); string tCmdName = e.Data.MessageArray[0].ToLower(); foreach (Command cmd in ChanCommands) { if (cmd.Name == tCmdName) { string[] args = new string[e.Data.MessageArray.Length - 1]; for (int i = 1; i != e.Data.MessageArray.Length; i++) { args[i - 1] = e.Data.MessageArray[i]; } try { cmd.Execute(Client, new Message(e.Data.Nick + ":" + e.Data.Host, e.Data.Channel, e.Data.Message)); } catch (ArgumentException ex) { Client.RfcNotice(e.Data.Nick, ex.Message); } } } } static void Client_OnQueryMessage(object sender, IrcEventArgs e) { Console.WriteLine("[{0}] [PM] {1}: {2}", DateTime.Now.ToShortTimeString(), e.Data.Nick, e.Data.Message); foreach (Command cmd in BotCommands) { if (cmd.Name.ToLower() == e.Data.MessageArray[0].ToLower()) { string[] args = new string[e.Data.MessageArray.Length - 1]; for (int i = 1; i != e.Data.MessageArray.Length; i++) { args[i - 1] = e.Data.MessageArray[i]; } try { cmd.Execute(Client, new Message(e.Data.Nick, "PM", e.Data.Message)); } catch (ArgumentException ex) { Client.RfcPrivmsg(e.Data.Nick, ex.Message); } } } } static void Client_OnPart(object sender, PartEventArgs e) { Console.WriteLine("[{0}] {1} has parted {2}", DateTime.Now.ToShortTimeString(), e.Data.Nick, e.Channel); } static void Client_OnJoin(object sender, JoinEventArgs e) { Console.WriteLine("[{0}] {1} has joined {2}", DateTime.Now.ToShortTimeString(), e.Data.Nick, e.Channel); } static void Client_OnError(object sender, ErrorEventArgs e) { Console.WriteLine("[{0}] [IRCD] Error: {1}", DateTime.Now.ToShortTimeString(), e.ErrorMessage); } static void Client_OnConnected(object sender, EventArgs e) { Console.WriteLine("[{0}] Connection established", DateTime.Now.ToShortTimeString()); Client.Login(Nick, "MooseBot IRC Services"); Client.RfcJoin(Channels); } static void ErrorExit(string type, string message) { Console.Beep(); Console.WriteLine("[{0}] [{1}] Error: {2}", DateTime.Now.ToShortTimeString(), type, message); Console.Beep(); Console.ReadLine(); Environment.Exit(0); } } }