Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Enable/Disable hook for sprockets #73

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Extensions/HelpSprocket/Help.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public bool Handle(ChatMessage message, IBot bot)

return false;
}

public bool Enabled { get; set; }
}
}
3 changes: 2 additions & 1 deletion Jabbot.AspNetBotHost/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Jabbot.Sprockets.Core;
using Nancy;
using TinyMessenger;
using System.Linq;

namespace Jabbot.AspNetBotHost
{
Expand All @@ -22,7 +23,7 @@ protected override void ConfigureApplicationContainer(TinyIoC.TinyIoCContainer c

container.Register(mefcontainer.GetExportedValues<IAnnounce>());
container.Register(mefcontainer.GetExportedValues<ISprocketInitializer>());
container.Register(mefcontainer.GetExportedValues<ISprocket>());
container.Register(new SprocketManager(mefcontainer.GetExportedValues<ISprocket>()));
container.Register(new Bot(_serverUrl, _botName, _botPassword));
}

Expand Down
1 change: 1 addition & 0 deletions Jabbot.AspNetBotHost/Jabbot.AspNetBotHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<Compile Include="Bootstrapper.cs" />
<Compile Include="Modules\HttpObject.cs" />
<Compile Include="Modules\TalkMessage.cs" />
<Compile Include="SprocketManager.cs" />
<Compile Include="TinyMessenger.cs" />
<Compile Include="Modules\BotHostModule.cs" />
<Compile Include="Modules\HomeModule.cs" />
Expand Down
22 changes: 19 additions & 3 deletions Jabbot.AspNetBotHost/Modules/BotHostModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISprocket> _sprockets;
private readonly SprocketManager _sprockets;
private readonly IEnumerable<ISprocketInitializer> _sprocketInitializers;
public static Dictionary<Regex, FunctionObject> HubotRespond = new Dictionary<Regex, FunctionObject>();
public static Dictionary<Regex, FunctionObject> HubotListen = new Dictionary<Regex, FunctionObject>();
public BotHostModule(Bot bot, IEnumerable<ISprocket> sprockets, IEnumerable<ISprocketInitializer> sprocketInitializers)
public BotHostModule(Bot bot, SprocketManager sprockets, IEnumerable<ISprocketInitializer> sprocketInitializers)
: base("bot")
{
_bot = bot;
Expand Down Expand Up @@ -99,6 +99,20 @@ public BotHostModule(Bot bot, IEnumerable<ISprocket> sprockets, IEnumerable<ISpr
return Response.AsRedirect("/");
};

Post["/disable/{sprocket}"] = _ =>
{
var sprocket = sprockets[_.Sprocket];
_bot.DisableSprocket(sprocket);
return Response.AsRedirect("/");
};

Post["/enable/{sprocket}"] = _ =>
{
var sprocket = sprockets[_.Sprocket];
_bot.EnableSprocket(sprocket);
return Response.AsRedirect("/");
};

}


Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Jabbot.AspNetBotHost/Modules/HomeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Jabbot.AspNetBotHost.Modules
{
public class HomeModule : NancyModule
{
public HomeModule(IEnumerable<IAnnounce> announcers, IEnumerable<ISprocket> sprockets, Bot bot)
public HomeModule(IEnumerable<IAnnounce> announcers, SprocketManager sprockets, Bot bot)
{
Get["/"] = _ => View["Home/Index", new { Announcers = announcers, Sprockets = sprockets, Bot = bot }];
Get["/Rooms"] = _ => View["Home/Rooms", bot.Rooms];
Expand Down
17 changes: 17 additions & 0 deletions Jabbot.AspNetBotHost/SprocketManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using Jabbot.Sprockets.Core;

namespace Jabbot.AspNetBotHost
{
public class SprocketManager : Dictionary<int, ISprocket>
{
public SprocketManager(IEnumerable<ISprocket> sprockets)
{
var count = 0;
foreach (var sprocket in sprockets)
{
base.Add(count++, sprocket);
}
}
}
}
9 changes: 7 additions & 2 deletions Jabbot.AspNetBotHost/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
}
<h3>Sprockets</h3>
<ul>
@foreach (var m in Model.Sprockets)
@foreach (var kvp in Model.Sprockets)
{
<li>@m</li>
string enabledText = kvp.Value.Enabled ? "Disable" : "Enable";
<form method="POST" action="/bot/@enabledText/@kvp.Key">
<li>@kvp.Value
<input type="submit" value="@enabledText"/>
</li>
</form>
}
</ul>

Expand Down
2 changes: 2 additions & 0 deletions Jabbot.CommandSprockets/CommandSprocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ public virtual bool Handle(ChatMessage message, IBot bot)
}
return false;
}

public bool Enabled { get; set; }
}
}
47 changes: 46 additions & 1 deletion Jabbot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Jabbot.Models;
using Jabbot.Sprockets.Core;
using SignalR.Client.Hubs;
using System.Linq;

namespace Jabbot
{
Expand Down Expand Up @@ -60,6 +61,24 @@ public void RemoveSprocket(ISprocket sprocket)
_sprockets.Remove(sprocket);
}

/// <summary>
/// Enable a specific sprocket
/// </summary>
/// <param name="sprocket">The sprocket to enable</param>
public void EnableSprocket(ISprocket sprocket)
{
SetSprocketEnabled(sprocket,true);
}

/// <summary>
/// Disable a specific sprocket
/// </summary>
/// <param name="sprocket">The sprocket to disable</param>
public void DisableSprocket(ISprocket sprocket)
{
SetSprocketEnabled(sprocket, false);
}

/// <summary>
/// Add a sprocket to the bot instance
/// </summary>
Expand Down Expand Up @@ -112,6 +131,10 @@ public void PowerUp(IEnumerable<ISprocketInitializer> sprocketInitializers = nul

if (sprocketInitializers != null)
IntializeSprockets(sprocketInitializers);
foreach(var sprocket in _sprockets)
{
sprocket.Enabled = true;
}
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -462,7 +493,6 @@ private void ProcessRoomArrival(dynamic message, string room)

}


private void OnLeave(dynamic user)
{

Expand Down Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion Jabbot/Sprockets/Core/ISprocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace Jabbot.Sprockets.Core
[InheritedExport]
public interface ISprocket : IMessageHandler
{

bool Enabled { get; set; }
}
}
2 changes: 2 additions & 0 deletions Jabbot/Sprockets/RegexSprocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}