Skip to content

Commit

Permalink
Added extensibility model for interception of messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl committed Dec 3, 2011
1 parent 5252267 commit ec68d48
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
42 changes: 31 additions & 11 deletions Jabbot/Bot.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Linq;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using Jabbot.MessageHandlers;
using SignalR.Client.Hubs;

namespace Jabbot
Expand Down Expand Up @@ -136,20 +138,31 @@ public void ShutDown()

private void ProcessMessage(dynamic message)
{

string content = message.Content;
string name = message.User.Name;

// Ignore replies from self
if (name.Equals(_name, StringComparison.OrdinalIgnoreCase))
{
return;
}

// We're going to process commands for the bot here
var chatMessage = new ChatMessage(content, name);

if (MessageReceived != null)
{
string content = message.Content;
string name = message.User.Name;
MessageReceived(chatMessage);
}

// Ignore replies from self
if (name.Equals(_name, StringComparison.OrdinalIgnoreCase))
foreach (var handler in GetMessageHandlers())
{
// Stop at the first one that handled the message
if (handler.Handle(chatMessage, this))
{
return;
break;
}

// We're going to process commands for the bot here
var chatMessage = new ChatMessage(content, name);
MessageReceived(chatMessage);
}
}

Expand All @@ -160,7 +173,7 @@ private void OnLeave(dynamic user)

private void OnJoin(dynamic user)
{
AddUser(user);
AddUser(user);
}

private void RemoveUser(dynamic user)
Expand All @@ -181,5 +194,12 @@ private void AddUser(dynamic user)
};
}

private IList<IMessageHandler> GetMessageHandlers()
{
// TODO: Allow passing a directory where we look for commands that get added/removed dynamically
var container = new CompositionContainer(new AssemblyCatalog(typeof(Bot).Assembly));
return container.GetExportedValues<IMessageHandler>().ToList();
}

}
}
2 changes: 2 additions & 0 deletions Jabbot/Jabbot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<HintPath>..\packages\SignalR.Client.0.3.6\lib\net40\SignalR.Client.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -49,6 +50,7 @@
<Compile Include="Bot.cs" />
<Compile Include="ChatMessage.cs" />
<Compile Include="ChatUser.cs" />
<Compile Include="MessageHandlers\IMessageHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions Jabbot/MessageHandlers/IMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.ComponentModel.Composition;

namespace Jabbot.MessageHandlers
{
/// <summary>
/// Extension point for bots. When an incoming message comes in, a message handler
/// will get a change to do something with it.
/// </summary>
[InheritedExport]
public interface IMessageHandler
{
/// <summary>
/// Allows the user to handle a message incoming from the bot's room
/// </summary>
/// <param name="message">Incoming message</param>
/// <param name="bot">bot instance</param>
/// <returns>true if handled, false if not</returns>
bool Handle(ChatMessage message, Bot bot);
}
}

0 comments on commit ec68d48

Please sign in to comment.