From 1912f1f45edcf517c7de0b21320602e291933764 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Fri, 1 May 2015 21:51:49 -0700 Subject: [PATCH] Added controller to handle multiple bot instances. Changed interface to deal with the controller directly. Added listing of connected servers. --- Combot/Combot.csproj | 1 + Combot/Controller.cs | 51 ++++++++++++++++++++++++++++++++++ Interface/ViewModels/MainViewModel.cs | 37 ++++++++++-------------- Modules/Owner Control/Owner_Control.cs | 15 +++++++++- 4 files changed, 80 insertions(+), 24 deletions(-) create mode 100755 Combot/Controller.cs diff --git a/Combot/Combot.csproj b/Combot/Combot.csproj index 7c9ffe3..4840c4a 100755 --- a/Combot/Combot.csproj +++ b/Combot/Combot.csproj @@ -66,6 +66,7 @@ + diff --git a/Combot/Controller.cs b/Combot/Controller.cs new file mode 100755 index 0000000..db9caf1 --- /dev/null +++ b/Combot/Controller.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Threading; +using Combot.Configurations; + +namespace Combot +{ + public class Controller + { + public static Controller Instance { get { return GetInstance(); } } + + public static Controller GetInstance() { return _controller ?? (_controller = new Controller()); } + + private static Controller _controller; + + private List _bots; + public List Bots { get; private set; } + public readonly Config Config = new Config(); + + private Controller() + { + Load(); + } + + public void Load() + { + Config.LoadServers(); + Bots = new List(); + + foreach (ServerConfig server in Config.Servers) + { + Bot Combot = new Bot(server); + Bots.Add(Combot); + } + } + + public void AutoConnect() + { + Bots.ForEach(bot => + { + if (bot.ServerConfig.AutoConnect) { bot.Connect(); } + }); + } + + public Bot GetBot(string server) + { + return Bots.Find(bot => bot.ServerConfig.Name == server); + } + } +} \ No newline at end of file diff --git a/Interface/ViewModels/MainViewModel.cs b/Interface/ViewModels/MainViewModel.cs index 2ed64ff..131ba51 100755 --- a/Interface/ViewModels/MainViewModel.cs +++ b/Interface/ViewModels/MainViewModel.cs @@ -12,17 +12,12 @@ using System.Windows; using System.Windows.Documents; using Combot; using Combot.IRCServices.Messaging; -using Combot.Configurations; -using Combot.IRCServices; using Combot.IRCServices.Commanding; namespace Interface.ViewModels { public class MainViewModel : ViewModelBase { - public List CombotSessions = new List(); - public Config Config = new Config(); - public string ApplicationTitle { get; set; } private string _CurrentBuffer = string.Empty; @@ -148,15 +143,15 @@ namespace Interface.ViewModels public MainViewModel() { ApplicationTitle = "Combot"; - Config.LoadServers(); ServerList = new ObservableCollection(); LocationList = new ObservableCollection(); BufferLock = new ReaderWriterLockSlim(); - foreach (ServerConfig server in Config.Servers) + Controller.Instance.Load(); + + foreach (Bot Combot in Controller.Instance.Bots) { - ServerList.Add(server.Name); - Bot Combot = new Bot(server); + ServerList.Add(Combot.ServerConfig.Name); Combot.ErrorEvent += e => BotErrorHandler(e, Combot.ServerConfig.Name); @@ -179,15 +174,11 @@ namespace Interface.ViewModels Combot.IRC.DisconnectEvent += () => DisconnectHandler(Combot.ServerConfig.Name); Combot.IRC.TCPErrorEvent += e => TCPErrorHandler(e, Combot.ServerConfig.Name); - CombotSessions.Add(Combot); - SelectedServer = server.Name; - - if (server.AutoConnect) - { - Combot.Connect(); - } + SelectedServer = Combot.ServerConfig.Name; } + Controller.Instance.AutoConnect(); + ToggleConnection = new DelegateCommand(ExecuteToggleConnection, CanToggleConnection); SubmitText = new DelegateCommand(ExecuteSubmitText, CanSubmitText); RemoveLocation = new DelegateCommand(ExecuteRemoveLocation, CanRemoveLocation); @@ -264,7 +255,7 @@ namespace Interface.ViewModels private void PrivateMessageCommandHandler(object sender, PrivateMessageCommand message, string server) { string curNick = string.Empty; - Bot session = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer); + Bot session = Controller.Instance.GetBot(SelectedServer); if (session != null) { curNick = session.IRC.Nickname; @@ -275,7 +266,7 @@ namespace Interface.ViewModels private void PrivateNoticeCommandHandler(object sender, PrivateNoticeCommand message, string server) { string curNick = string.Empty; - Bot session = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer); + Bot session = Controller.Instance.GetBot(SelectedServer); if (session != null) { curNick = session.IRC.Nickname; @@ -321,7 +312,7 @@ namespace Interface.ViewModels private void ExecuteSubmitText() { - Bot botInstance = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer); + Bot botInstance = Controller.Instance.GetBot(SelectedServer); if (botInstance != null && botInstance.Connected) { string message = InputBoxText; @@ -378,7 +369,7 @@ namespace Interface.ViewModels if (location.StartsWith("#") || location.StartsWith("&")) { - Bot botInstance = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer); + Bot botInstance = Controller.Instance.GetBot(SelectedServer); if (botInstance.IRC.Channels.Exists(chan => chan.Name == location)) { botInstance.IRC.Command.SendPart(location); @@ -421,12 +412,12 @@ namespace Interface.ViewModels private void Connect(string server) { - CombotSessions.Find(bot => bot.ServerConfig.Name == server).Connect(); + Controller.Instance.GetBot(server).Connect(); } private void Disconnect(string server) { - CombotSessions.Find(bot => bot.ServerConfig.Name == server).Disconnect(); + Controller.Instance.GetBot(server).Disconnect(); } private void AddToBuffer(string server, string location, string message) @@ -490,7 +481,7 @@ namespace Interface.ViewModels } CurrentBuffer = string.Join(Environment.NewLine, BufferList.Find(buf => buf.Server == SelectedServer && buf.Location == SelectedLocation).Buffer); BufferLock.ExitWriteLock(); - Bot session = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer); + Bot session = Controller.Instance.GetBot(SelectedServer); if (session != null) { Connected = session.Connected; diff --git a/Modules/Owner Control/Owner_Control.cs b/Modules/Owner Control/Owner_Control.cs index 5766778..517883f 100755 --- a/Modules/Owner Control/Owner_Control.cs +++ b/Modules/Owner Control/Owner_Control.cs @@ -143,7 +143,20 @@ namespace Combot.Modules.Plugins SendResponse(command.MessageType, command.Location, command.Nick.Nickname, channelMessage); break; case "servers": - // TODO Add server list + string serverList = string.Empty; + foreach (Bot bot in Controller.Instance.Bots) + { + if (bot.Connected) + { + serverList = string.Join(", ", serverList, bot.ServerConfig.Name); + } + } + if (string.IsNullOrEmpty(serverList)) + { + serverList = "None Connected"; + } + string serverMessage = string.Format("I am connected to the following servers: \u0002{0}\u000F", serverList.TrimStart(',').Trim()); + SendResponse(command.MessageType, command.Location, command.Nick.Nickname, serverMessage); break; case "modules": string moduleList = string.Join(", ", Bot.Modules.Select(module => module.Name));