From bc9227788e0e147c57ca245950ca3e7a93729767 Mon Sep 17 00:00:00 2001 From: Alex Friedman Date: Tue, 17 Jan 2012 00:22:54 -0500 Subject: [PATCH 1/2] Added Enable/Disable hook for sprockets. --- Extensions/HelpSprocket/Help.cs | 2 + Jabbot.AspNetBotHost/Modules/BotHostModule.cs | 12 +++++ Jabbot.AspNetBotHost/Views/Home/Index.cshtml | 7 ++- Jabbot.CommandSprockets/CommandSprocket.cs | 2 + Jabbot/Bot.cs | 44 ++++++++++++++++++- Jabbot/Sprockets/Core/ISprocket.cs | 2 +- Jabbot/Sprockets/RegexSprocket.cs | 2 + 7 files changed, 68 insertions(+), 3 deletions(-) diff --git a/Extensions/HelpSprocket/Help.cs b/Extensions/HelpSprocket/Help.cs index 97ada37..4869601 100644 --- a/Extensions/HelpSprocket/Help.cs +++ b/Extensions/HelpSprocket/Help.cs @@ -23,5 +23,7 @@ public bool Handle(ChatMessage message, IBot bot) return false; } + + public bool Enabled { get; set; } } } diff --git a/Jabbot.AspNetBotHost/Modules/BotHostModule.cs b/Jabbot.AspNetBotHost/Modules/BotHostModule.cs index 0981c67..bfb9324 100644 --- a/Jabbot.AspNetBotHost/Modules/BotHostModule.cs +++ b/Jabbot.AspNetBotHost/Modules/BotHostModule.cs @@ -99,6 +99,18 @@ public BotHostModule(Bot bot, IEnumerable sprockets, IEnumerable + { + _bot.DisableSprocket(_.Sprocket); + return Response.AsRedirect("/"); + }; + + Post["/enable/{sprocket}"] = _ => + { + _bot.EnableSprocket(_.Sprocket); + return Response.AsRedirect("/"); + }; + } diff --git a/Jabbot.AspNetBotHost/Views/Home/Index.cshtml b/Jabbot.AspNetBotHost/Views/Home/Index.cshtml index 1634e78..60aaa4a 100644 --- a/Jabbot.AspNetBotHost/Views/Home/Index.cshtml +++ b/Jabbot.AspNetBotHost/Views/Home/Index.cshtml @@ -7,7 +7,12 @@
    @foreach (var m in Model.Sprockets) { -
  • @m
  • + string enabledText = m.Enabled ? "Disable" : "Enable"; +
    +
  • @m + +
  • +
    }
diff --git a/Jabbot.CommandSprockets/CommandSprocket.cs b/Jabbot.CommandSprockets/CommandSprocket.cs index a31fb94..39d4492 100644 --- a/Jabbot.CommandSprockets/CommandSprocket.cs +++ b/Jabbot.CommandSprockets/CommandSprocket.cs @@ -46,5 +46,7 @@ public virtual bool Handle(ChatMessage message, IBot bot) } return false; } + + public bool Enabled { get; set; } } } diff --git a/Jabbot/Bot.cs b/Jabbot/Bot.cs index 0326252..1acd9b2 100644 --- a/Jabbot/Bot.cs +++ b/Jabbot/Bot.cs @@ -8,6 +8,7 @@ using Jabbot.Models; using Jabbot.Sprockets.Core; using SignalR.Client.Hubs; +using System.Linq; namespace Jabbot { @@ -60,6 +61,24 @@ public void RemoveSprocket(ISprocket sprocket) _sprockets.Remove(sprocket); } + /// + /// Enable a specific sprocket specified by Type Name + /// + /// The Type Name of the sprocket (what's returned by mySprocket.GetType().FullName + public void EnableSprocket(string sprocketTypeName) + { + SetSprocketEnabled(sprocketTypeName,true); + } + + /// + /// Disable a specific sprocket specified by Type Name + /// + /// The Type Name of the sprocket (what's returned by mySprocket.GetType().FullName + public void DisableSprocket(string sprocketTypeName) + { + SetSprocketEnabled(sprocketTypeName, false); + } + /// /// Add a sprocket to the bot instance /// @@ -112,6 +131,10 @@ public void PowerUp(IEnumerable sprocketInitializers = nul if (sprocketInitializers != null) IntializeSprockets(sprocketInitializers); + foreach(var sprocket in _sprockets) + { + sprocket.Enabled = true; + } } } } @@ -387,6 +410,10 @@ private void ProcessChatMessages(ChatMessage message) foreach (var handler in _sprockets) { + if(!handler.Enabled) + { + continue; + } if (handler.Handle(message, this)) { handled = true; @@ -430,6 +457,10 @@ private void ProcessRoomArrival(dynamic message, string room) foreach (var handler in _sprockets) { + if(!handler.Enabled) + { + continue; + } if (handler.Handle(new ChatMessage("[JABBR] - " + name + " just entered " + room, name, room), this)) { handled = true; @@ -462,7 +493,6 @@ private void ProcessRoomArrival(dynamic message, string room) } - private void OnLeave(dynamic user) { @@ -502,5 +532,17 @@ private void Send(string command) _chat.Invoke("send", command).Wait(); } + private void SetSprocketEnabled(string sprocketTypeName, bool enabled) + { + if (String.IsNullOrEmpty(sprocketTypeName)) + { + return; + } + + foreach (var sprocket in _sprockets.Where(s => s.GetType().FullName == sprocketTypeName)) + { + sprocket.Enabled = enabled; + } + } } } diff --git a/Jabbot/Sprockets/Core/ISprocket.cs b/Jabbot/Sprockets/Core/ISprocket.cs index f1560c3..5ebe004 100644 --- a/Jabbot/Sprockets/Core/ISprocket.cs +++ b/Jabbot/Sprockets/Core/ISprocket.cs @@ -9,6 +9,6 @@ namespace Jabbot.Sprockets.Core [InheritedExport] public interface ISprocket : IMessageHandler { - + bool Enabled { get; set; } } } diff --git a/Jabbot/Sprockets/RegexSprocket.cs b/Jabbot/Sprockets/RegexSprocket.cs index 022359d..9d908f3 100644 --- a/Jabbot/Sprockets/RegexSprocket.cs +++ b/Jabbot/Sprockets/RegexSprocket.cs @@ -27,5 +27,7 @@ public bool Handle(ChatMessage message, IBot bot) } protected abstract void ProcessMatch(Match match, ChatMessage message, IBot bot); + + public bool Enabled { get; set; } } } From 1980ca6d386feef2a029d5e438ae1689b9aafb58 Mon Sep 17 00:00:00 2001 From: Alex Friedman Date: Tue, 17 Jan 2012 21:06:29 -0500 Subject: [PATCH 2/2] Refactored Bot, Enable/Disable now take ISprocket. Sprocket pairing with ID logic moved to AspNetBotHost --- Jabbot.AspNetBotHost/Bootstrapper.cs | 3 ++- .../Jabbot.AspNetBotHost.csproj | 1 + Jabbot.AspNetBotHost/Modules/BotHostModule.cs | 14 ++++++---- Jabbot.AspNetBotHost/Modules/HomeModule.cs | 2 +- Jabbot.AspNetBotHost/SprocketManager.cs | 17 ++++++++++++ Jabbot.AspNetBotHost/Views/Home/Index.cshtml | 8 +++--- Jabbot/Bot.cs | 27 ++++++++++--------- 7 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 Jabbot.AspNetBotHost/SprocketManager.cs diff --git a/Jabbot.AspNetBotHost/Bootstrapper.cs b/Jabbot.AspNetBotHost/Bootstrapper.cs index 31301a1..4f4259d 100644 --- a/Jabbot.AspNetBotHost/Bootstrapper.cs +++ b/Jabbot.AspNetBotHost/Bootstrapper.cs @@ -6,6 +6,7 @@ using Jabbot.Sprockets.Core; using Nancy; using TinyMessenger; +using System.Linq; namespace Jabbot.AspNetBotHost { @@ -22,7 +23,7 @@ protected override void ConfigureApplicationContainer(TinyIoC.TinyIoCContainer c container.Register(mefcontainer.GetExportedValues()); container.Register(mefcontainer.GetExportedValues()); - container.Register(mefcontainer.GetExportedValues()); + container.Register(new SprocketManager(mefcontainer.GetExportedValues())); container.Register(new Bot(_serverUrl, _botName, _botPassword)); } diff --git a/Jabbot.AspNetBotHost/Jabbot.AspNetBotHost.csproj b/Jabbot.AspNetBotHost/Jabbot.AspNetBotHost.csproj index dc71126..ef3e9d2 100644 --- a/Jabbot.AspNetBotHost/Jabbot.AspNetBotHost.csproj +++ b/Jabbot.AspNetBotHost/Jabbot.AspNetBotHost.csproj @@ -129,6 +129,7 @@ + diff --git a/Jabbot.AspNetBotHost/Modules/BotHostModule.cs b/Jabbot.AspNetBotHost/Modules/BotHostModule.cs index bfb9324..5d22ec5 100644 --- a/Jabbot.AspNetBotHost/Modules/BotHostModule.cs +++ b/Jabbot.AspNetBotHost/Modules/BotHostModule.cs @@ -23,11 +23,11 @@ public class BotHostModule : NancyModule private static readonly string _botRooms = ConfigurationManager.AppSettings["Bot.RoomList"]; private static readonly string _momentApiKey = ConfigurationManager.AppSettings["Moment.ApiKey"]; public static Bot _bot; - private readonly IEnumerable _sprockets; + private readonly SprocketManager _sprockets; private readonly IEnumerable _sprocketInitializers; public static Dictionary HubotRespond = new Dictionary(); public static Dictionary HubotListen = new Dictionary(); - public BotHostModule(Bot bot, IEnumerable sprockets, IEnumerable sprocketInitializers) + public BotHostModule(Bot bot, SprocketManager sprockets, IEnumerable sprocketInitializers) : base("bot") { _bot = bot; @@ -101,13 +101,15 @@ public BotHostModule(Bot bot, IEnumerable sprockets, IEnumerable { - _bot.DisableSprocket(_.Sprocket); + var sprocket = sprockets[_.Sprocket]; + _bot.DisableSprocket(sprocket); return Response.AsRedirect("/"); }; Post["/enable/{sprocket}"] = _ => { - _bot.EnableSprocket(_.Sprocket); + var sprocket = sprockets[_.Sprocket]; + _bot.EnableSprocket(sprocket); return Response.AsRedirect("/"); }; @@ -132,7 +134,9 @@ private void StartBot() ScheduleKeepAlive(_hostBaseUrl + "/keepalive"); } foreach (var sprocket in _sprockets) - _bot.AddSprocket(sprocket); + { + _bot.AddSprocket(sprocket.Value); + } _bot.PowerUp(_sprocketInitializers); JoinRooms(_bot); diff --git a/Jabbot.AspNetBotHost/Modules/HomeModule.cs b/Jabbot.AspNetBotHost/Modules/HomeModule.cs index 7619dd8..e7bd6cd 100644 --- a/Jabbot.AspNetBotHost/Modules/HomeModule.cs +++ b/Jabbot.AspNetBotHost/Modules/HomeModule.cs @@ -6,7 +6,7 @@ namespace Jabbot.AspNetBotHost.Modules { public class HomeModule : NancyModule { - public HomeModule(IEnumerable announcers, IEnumerable sprockets, Bot bot) + public HomeModule(IEnumerable announcers, SprocketManager sprockets, Bot bot) { Get["/"] = _ => View["Home/Index", new { Announcers = announcers, Sprockets = sprockets, Bot = bot }]; Get["/Rooms"] = _ => View["Home/Rooms", bot.Rooms]; diff --git a/Jabbot.AspNetBotHost/SprocketManager.cs b/Jabbot.AspNetBotHost/SprocketManager.cs new file mode 100644 index 0000000..b9ea017 --- /dev/null +++ b/Jabbot.AspNetBotHost/SprocketManager.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using Jabbot.Sprockets.Core; + +namespace Jabbot.AspNetBotHost +{ + public class SprocketManager : Dictionary + { + public SprocketManager(IEnumerable sprockets) + { + var count = 0; + foreach (var sprocket in sprockets) + { + base.Add(count++, sprocket); + } + } + } +} \ No newline at end of file diff --git a/Jabbot.AspNetBotHost/Views/Home/Index.cshtml b/Jabbot.AspNetBotHost/Views/Home/Index.cshtml index 60aaa4a..2d59b8c 100644 --- a/Jabbot.AspNetBotHost/Views/Home/Index.cshtml +++ b/Jabbot.AspNetBotHost/Views/Home/Index.cshtml @@ -5,11 +5,11 @@ }

Sprockets

    - @foreach (var m in Model.Sprockets) + @foreach (var kvp in Model.Sprockets) { - string enabledText = m.Enabled ? "Disable" : "Enable"; -
    -
  • @m + string enabledText = kvp.Value.Enabled ? "Disable" : "Enable"; + +
  • @kvp.Value
  • diff --git a/Jabbot/Bot.cs b/Jabbot/Bot.cs index 1acd9b2..486fb35 100644 --- a/Jabbot/Bot.cs +++ b/Jabbot/Bot.cs @@ -62,21 +62,21 @@ public void RemoveSprocket(ISprocket sprocket) } /// - /// Enable a specific sprocket specified by Type Name + /// Enable a specific sprocket /// - /// The Type Name of the sprocket (what's returned by mySprocket.GetType().FullName - public void EnableSprocket(string sprocketTypeName) + /// The sprocket to enable + public void EnableSprocket(ISprocket sprocket) { - SetSprocketEnabled(sprocketTypeName,true); + SetSprocketEnabled(sprocket,true); } /// - /// Disable a specific sprocket specified by Type Name + /// Disable a specific sprocket /// - /// The Type Name of the sprocket (what's returned by mySprocket.GetType().FullName - public void DisableSprocket(string sprocketTypeName) + /// The sprocket to disable + public void DisableSprocket(ISprocket sprocket) { - SetSprocketEnabled(sprocketTypeName, false); + SetSprocketEnabled(sprocket, false); } /// @@ -532,17 +532,20 @@ private void Send(string command) _chat.Invoke("send", command).Wait(); } - private void SetSprocketEnabled(string sprocketTypeName, bool enabled) + private void SetSprocketEnabled(ISprocket sprocket, bool enabled) { - if (String.IsNullOrEmpty(sprocketTypeName)) + if (sprocket == null) { return; } - foreach (var sprocket in _sprockets.Where(s => s.GetType().FullName == sprocketTypeName)) + var sprocketToChange = _sprockets.FirstOrDefault(s => s == sprocket); + if(sprocketToChange == null) { - sprocket.Enabled = enabled; + return; } + + sprocketToChange.Enabled = enabled; } } }