using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.Threading; using Meebey.SmartIrc4net; using SpikeLite.AccessControl; using SpikeLite.Modules; namespace SpikeLite { #region Enumerations #region BotStatus public enum BotStatus { Starting, Started, Stopping, Stopped } #endregion #endregion public class SpikeLite { #region Fields private BotStatus e_BotStatus = BotStatus.Stopped; private IrcClient o_IrcClient; private AccessControlManager o_AccessControlManager; #endregion #region Properties #region Status public BotStatus BotStatus { get { return this.e_BotStatus; } } #endregion #endregion #region Constructors public SpikeLite() { this.o_IrcClient = new IrcClient(); this.o_AccessControlManager = new AccessControlManager(this.o_IrcClient); this.RegisterModules(); if(Options.DebugOutputLevel == DebugOutputLevel.Verbose) this.o_IrcClient.OnRawMessage += new IrcEventHandler(o_IrcClient_OnRawMessage); this.o_IrcClient.OnRegistered += new EventHandler(o_IrcClient_OnRegistered); this.o_IrcClient.OnQueryNotice += new IrcEventHandler(o_IrcClient_OnQueryNotice); } void o_IrcClient_OnRawMessage(object sender, IrcEventArgs e) { Console.WriteLine(e.Data.RawMessage); } #endregion #region Methods #region Start public void Start() { if (this.e_BotStatus != BotStatus.Stopped) throw new Exception("Current BotStatus is : '" + this.e_BotStatus.ToString() + "'. To start the bot the BotStatus must be 'Stopped'."); Thread ircThread = new Thread(this.IrcThread); ircThread.Start(); } #endregion #region IrcThread private void IrcThread() { this.e_BotStatus = BotStatus.Starting; this.o_IrcClient.SendDelay = 200; this.o_IrcClient.ActiveChannelSyncing = true; this.o_IrcClient.AutoRejoin = true; this.o_IrcClient.CtcpVersion = "SpikeLite " + Assembly.GetCallingAssembly().GetName().Version.ToString(); this.o_IrcClient.Connect("irc.freenode.net", 6667); this.o_IrcClient.Login(Options.BotNickName, Options.BotRealName); this.e_BotStatus = BotStatus.Started; this.o_IrcClient.Listen(true); } #endregion #region Stop public void Stop() { this.Stop("", false); } public void Stop(string quitMessage) { this.Stop(quitMessage, true); } private void Stop(string quitMessage, bool useQuitMessage) { if (this.e_BotStatus != BotStatus.Started) throw new Exception("Current BotStatus is : '" + this.e_BotStatus.ToString() + "'. To stop the bot the BotStatus must be 'Started'."); this.e_BotStatus = BotStatus.Stopping; if (useQuitMessage) this.o_IrcClient.RfcQuit(quitMessage); else this.o_IrcClient.RfcQuit("Stop() called.", Priority.Critical); this.e_BotStatus = BotStatus.Stopped; } #endregion #region JoinChannels private void JoinChannels() { foreach (string room in Options.Rooms) { this.o_IrcClient.RfcJoin(room); } } #endregion #region RegisterModules private void RegisterModules() { GoogleSearchModule googleSearchModule = new GoogleSearchModule(this.o_IrcClient, this.o_AccessControlManager); GoogleRNSearchModule googleRNSearchModule = new GoogleRNSearchModule(this.o_IrcClient, this.o_AccessControlManager); MSDN1SearchModule msdn1SearchModule = new MSDN1SearchModule(this.o_IrcClient, this.o_AccessControlManager); MSDN2SearchModule msdn2SearchModule = new MSDN2SearchModule(this.o_IrcClient, this.o_AccessControlManager); ConnectSearchModule connectSearchModule = new ConnectSearchModule(this.o_IrcClient, this.o_AccessControlManager); WikipediaSearchModule wikiPediaSearchModule = new WikipediaSearchModule(this.o_IrcClient, this.o_AccessControlManager); CodeProjectSearchModule codeProjectSearchModule = new CodeProjectSearchModule(this.o_IrcClient, this.o_AccessControlManager); PInvokeSearchModule pInvokeSearchModule = new PInvokeSearchModule(this.o_IrcClient, this.o_AccessControlManager); DADSSearchModule dADSSearchModule = new DADSSearchModule(this.o_IrcClient, this.o_AccessControlManager); CodeplexSearchModule codeplexSearchModule = new CodeplexSearchModule(this.o_IrcClient, this.o_AccessControlManager); } #endregion #region Events void o_IrcClient_OnRegistered(object sender, EventArgs e) { this.o_IrcClient.SendMessage(SendType.Message, "nickserv", "identify " + Options.NickServPassword); } void o_IrcClient_OnQueryNotice(object sender, IrcEventArgs e) { if (!string.IsNullOrEmpty(e.Data.Nick) && e.Data.Nick.ToLower() == "nickserv") { if (e.Data.Message == "Password accepted - you are now recognized") { this.JoinChannels(); } } } #endregion #endregion } }