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/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 0981c67..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; @@ -99,6 +99,20 @@ public BotHostModule(Bot bot, IEnumerable sprockets, IEnumerable + { + var sprocket = sprockets[_.Sprocket]; + _bot.DisableSprocket(sprocket); + return Response.AsRedirect("/"); + }; + + Post["/enable/{sprocket}"] = _ => + { + var sprocket = sprockets[_.Sprocket]; + _bot.EnableSprocket(sprocket); + return Response.AsRedirect("/"); + }; + } @@ -120,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 1634e78..2d59b8c 100644 --- a/Jabbot.AspNetBotHost/Views/Home/Index.cshtml +++ b/Jabbot.AspNetBotHost/Views/Home/Index.cshtml @@ -5,9 +5,14 @@ }

Sprockets

    - @foreach (var m in Model.Sprockets) + @foreach (var kvp in Model.Sprockets) { -
  • @m
  • + string enabledText = kvp.Value.Enabled ? "Disable" : "Enable"; +
    +
  • @kvp.Value + +
  • +
    }
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..486fb35 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 + /// + /// The sprocket to enable + public void EnableSprocket(ISprocket sprocket) + { + SetSprocketEnabled(sprocket,true); + } + + /// + /// Disable a specific sprocket + /// + /// The sprocket to disable + public void DisableSprocket(ISprocket sprocket) + { + SetSprocketEnabled(sprocket, 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,20 @@ private void Send(string command) _chat.Invoke("send", command).Wait(); } + private void SetSprocketEnabled(ISprocket sprocket, bool enabled) + { + if (sprocket == null) + { + return; + } + + var sprocketToChange = _sprockets.FirstOrDefault(s => s == sprocket); + if(sprocketToChange == null) + { + return; + } + + sprocketToChange.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; } } }