Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add back IrcBot source with correct case

  • Loading branch information...
commit 28d7e0b58f676f6c6d2a311032cfa0daee320456 1 parent fec37c6
Daniel Grunwald dgrunwald authored
101 BuildServer/ccnet.IrcPublisher.plugin/ccnet.IrcPublisher.plugin/IrcBot.cs
... ... @@ -0,0 +1,101 @@
  1 +// <file>
  2 +// <copyright see="prj:///doc/copyright.txt"/>
  3 +// <license see="prj:///doc/license.txt"/>
  4 +// <author name="Daniel Grunwald"/>
  5 +// <version>$Revision$</version>
  6 +// </file>
  7 +
  8 +using System;
  9 +using System.Collections.Generic;
  10 +using System.Text;
  11 +using System.Threading;
  12 +
  13 +using Meebey.SmartIrc4net;
  14 +
  15 +namespace ccnet.IrcPublisher.plugin
  16 +{
  17 + public class IrcBot
  18 + {
  19 + static readonly Dictionary<IrcBotSettings, IrcBot> bots = new Dictionary<IrcBotSettings, IrcBot>();
  20 +
  21 + public static IrcBot GetBot(IrcBotSettings settings)
  22 + {
  23 + IrcBot bot;
  24 + lock (bots) {
  25 + if (!bots.TryGetValue(settings, out bot)) {
  26 + bot = new IrcBot(settings);
  27 + bots[settings] = bot;
  28 + }
  29 + }
  30 + return bot;
  31 + }
  32 +
  33 + readonly IrcBotSettings settings;
  34 + readonly IrcClient client = new IrcClient();
  35 + readonly List<string> channels = new List<string>();
  36 +
  37 + private IrcBot(IrcBotSettings settings)
  38 + {
  39 + this.settings = settings;
  40 + client.SendDelay = 750;
  41 + client.OnDisconnected += new EventHandler(client_OnDisconnected);
  42 + client.OnQueryMessage += new IrcEventHandler(client_OnQueryMessage);
  43 + client.OnChannelMessage += new IrcEventHandler(client_OnChannelMessage);
  44 + client.Encoding = new UTF8Encoding(false);
  45 +
  46 + client.Connect(settings.Server, settings.Port);
  47 + client.Login(settings.Nick, settings.RealName);
  48 +
  49 + Thread t = new Thread(new ThreadStart(ListenThread));
  50 + t.IsBackground = true;
  51 + t.Start();
  52 + }
  53 +
  54 + void client_OnChannelMessage(object sender, IrcEventArgs e)
  55 + {
  56 + }
  57 +
  58 + void client_OnQueryMessage(object sender, IrcEventArgs e)
  59 + {
  60 + // process private message sent to the bot
  61 + if (e.Data.Message == "quit") {
  62 + client.RfcQuit();
  63 + }
  64 + }
  65 +
  66 + void client_OnDisconnected(object sender, EventArgs e)
  67 + {
  68 + lock (bots) {
  69 + bots.Remove(settings);
  70 + }
  71 + }
  72 +
  73 + void ListenThread()
  74 + {
  75 + try {
  76 + try {
  77 + client.Listen();
  78 + } finally {
  79 + client.Disconnect();
  80 + }
  81 + } catch (ThreadAbortException) {
  82 + throw;
  83 + } catch (Exception ex) {
  84 + Console.WriteLine(ex.ToString());
  85 + } finally {
  86 + client_OnDisconnected(null, null);
  87 + }
  88 + }
  89 +
  90 + public void SendMessage(string channel, string message)
  91 + {
  92 + lock (channels) {
  93 + if (!channels.Contains(channel)) {
  94 + channels.Add(channel);
  95 + client.RfcJoin(channel);
  96 + }
  97 + }
  98 + client.SendMessage(SendType.Message, channel, message);
  99 + }
  100 + }
  101 +}

0 comments on commit 28d7e0b

Please sign in to comment.
Something went wrong with that request. Please try again.