Skip to content

Commit

Permalink
Added methods to bot for managing sprokets.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl committed Dec 4, 2011
1 parent c8f77cd commit 8aa516e
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions Jabbot/Bot.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,12 +20,10 @@ public class Bot
private readonly string _name;
private readonly string _password;
private readonly ConcurrentDictionary<string, ChatUser> _users = new ConcurrentDictionary<string, ChatUser>(StringComparer.OrdinalIgnoreCase);
private readonly List<ISproket> _sprokets = new List<ISproket>();

private const string ExtensionsFolder = "Sprokets";

[ImportMany(AllowRecomposition = true)]
private IEnumerable<ISproket> Sprokets { get; set; }

public Bot(string url, string name, string password)
{
_name = name;
Expand All @@ -49,6 +46,30 @@ public event Action Disconnected

public event Action<ChatMessage> MessageReceived;

/// <summary>
/// Add a sproket to the bot instance
/// </summary>
public void AddSproket(ISproket sproket)
{
_sprokets.Add(sproket);
}

/// <summary>
/// Remove a sproket from the bot instance
/// </summary>
public void RemoveSproket(ISproket sproket)
{
_sprokets.Remove(sproket);
}

/// <summary>
/// Remove all sprokets
/// </summary>
public void ClearSprokets()
{
_sprokets.Clear();
}

/// <summary>
/// Connects to the chat session
/// </summary>
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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<ISproket>())
{
AddSproket(sproket);
}
}

private static string GetExtensionsPath()
Expand Down

0 comments on commit 8aa516e

Please sign in to comment.