From 8aa516ec62ef86736501e8e06e342def92f7f2a3 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sat, 3 Dec 2011 21:04:20 -0800 Subject: [PATCH] Added methods to bot for managing sprokets. --- Jabbot/Bot.cs | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/Jabbot/Bot.cs b/Jabbot/Bot.cs index f2feacc..e6f1f5a 100644 --- a/Jabbot/Bot.cs +++ b/Jabbot/Bot.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition.Primitives; using System.IO; @@ -21,12 +20,10 @@ public class Bot private readonly string _name; private readonly string _password; private readonly ConcurrentDictionary _users = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + private readonly List _sprokets = new List(); private const string ExtensionsFolder = "Sprokets"; - [ImportMany(AllowRecomposition = true)] - private IEnumerable Sprokets { get; set; } - public Bot(string url, string name, string password) { _name = name; @@ -49,6 +46,30 @@ public event Action Disconnected public event Action MessageReceived; + /// + /// Add a sproket to the bot instance + /// + public void AddSproket(ISproket sproket) + { + _sprokets.Add(sproket); + } + + /// + /// Remove a sproket from the bot instance + /// + public void RemoveSproket(ISproket sproket) + { + _sprokets.Remove(sproket); + } + + /// + /// Remove all sprokets + /// + public void ClearSprokets() + { + _sprokets.Clear(); + } + /// /// Connects to the chat session /// @@ -203,13 +224,8 @@ private void ProcessMessage(dynamic message) MessageReceived(chatMessage); } - // No extensions - if (Sprokets == null) - { - return; - } - - foreach (var handler in Sprokets) + // Loop over the registered sprokets + foreach (var handler in _sprokets) { // Stop at the first one that handled the message if (handler.Handle(chatMessage, this)) @@ -265,7 +281,12 @@ private void InitializeContainer() } var container = new CompositionContainer(catalog); - container.ComposeParts(this); + + // Add all the sprokets to the sproket list + foreach (var sproket in container.GetExportedValues()) + { + AddSproket(sproket); + } } private static string GetExtensionsPath()