Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Enable/Disable hook for sprockets.

  • Loading branch information...
commit bc9227788e0e147c57ca245950ca3e7a93729767 1 parent c7b931f
@brooklynDev authored
View
2  Extensions/HelpSprocket/Help.cs
@@ -23,5 +23,7 @@ public bool Handle(ChatMessage message, IBot bot)
return false;
}
+
+ public bool Enabled { get; set; }
}
}
View
12 Jabbot.AspNetBotHost/Modules/BotHostModule.cs
@@ -99,6 +99,18 @@ public BotHostModule(Bot bot, IEnumerable<ISprocket> sprockets, IEnumerable<ISpr
return Response.AsRedirect("/");
};
+ Post["/disable/{sprocket}"] = _ =>
+ {
+ _bot.DisableSprocket(_.Sprocket);
+ return Response.AsRedirect("/");
+ };
+
+ Post["/enable/{sprocket}"] = _ =>
+ {
+ _bot.EnableSprocket(_.Sprocket);
+ return Response.AsRedirect("/");
+ };
+
}
View
7 Jabbot.AspNetBotHost/Views/Home/Index.cshtml
@@ -7,7 +7,12 @@
<ul>
@foreach (var m in Model.Sprockets)
{
- <li>@m</li>
+ string enabledText = m.Enabled ? "Disable" : "Enable";
+ <form method="POST" action="/bot/@enabledText/@m">
+ <li>@m
+ <input type="submit" value="@enabledText"/>
+ </li>
+ </form>
}
</ul>
View
2  Jabbot.CommandSprockets/CommandSprocket.cs
@@ -46,5 +46,7 @@ public virtual bool Handle(ChatMessage message, IBot bot)
}
return false;
}
+
+ public bool Enabled { get; set; }
}
}
View
44 Jabbot/Bot.cs
@@ -8,6 +8,7 @@
using Jabbot.Models;
using Jabbot.Sprockets.Core;
using SignalR.Client.Hubs;
+using System.Linq;
namespace Jabbot
{
@@ -61,6 +62,24 @@ public void RemoveSprocket(ISprocket sprocket)
}
/// <summary>
+ /// Enable a specific sprocket specified by Type Name
+ /// </summary>
+ /// <param name="sprocketTypeName">The Type Name of the sprocket (what's returned by mySprocket.GetType().FullName</param>
+ public void EnableSprocket(string sprocketTypeName)
+ {
+ SetSprocketEnabled(sprocketTypeName,true);
+ }
+
+ /// <summary>
+ /// Disable a specific sprocket specified by Type Name
+ /// </summary>
+ /// <param name="sprocketTypeName">The Type Name of the sprocket (what's returned by mySprocket.GetType().FullName</param>
+ public void DisableSprocket(string sprocketTypeName)
+ {
+ SetSprocketEnabled(sprocketTypeName, false);
+ }
+
+ /// <summary>
/// Add a sprocket to the bot instance
/// </summary>
public void AddUnhandledMessageSprocket(IUnhandledMessageSprocket sprocket)
@@ -112,6 +131,10 @@ public void PowerUp(IEnumerable<ISprocketInitializer> 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;
+ }
+ }
}
}
View
2  Jabbot/Sprockets/Core/ISprocket.cs
@@ -9,6 +9,6 @@ namespace Jabbot.Sprockets.Core
[InheritedExport]
public interface ISprocket : IMessageHandler
{
-
+ bool Enabled { get; set; }
}
}
View
2  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; }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.