Created
May 29, 2013 17:01
-
-
Save pjmagee/5671906 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Threading; | |
| using IrcDotNet; | |
| using IrcDotNet.Ctcp; | |
| namespace IRCEXAMPLES.Messaging | |
| { | |
| /// <summary> | |
| /// Contains the connection between an irc client and server. | |
| /// </summary> | |
| public class IrcClientHandler | |
| { | |
| public EventHandler<IrcMessageEventArgs> OnMessageReceived; | |
| /// <summary> | |
| /// The irc client connected to the server | |
| /// </summary> | |
| private IrcClient _ircClient; | |
| /// <summary> | |
| /// The ctcp client used to send CTCP actions | |
| /// </summary> | |
| private CtcpClient _ctcpClient; | |
| /// <summary> | |
| /// Used to reconnect to the server if the user did not specify a disconnect | |
| /// </summary> | |
| private bool _initiatedDisconnect; | |
| /// <summary> | |
| /// The network this client is connected to | |
| /// </summary> | |
| private Network _network; | |
| /// <summary> | |
| /// The connected server for this network | |
| /// </summary> | |
| private Server _server; | |
| public bool IsConnected | |
| { | |
| get { return _ircClient != null && _ircClient.IsConnected; } | |
| } | |
| public void Connect(Network network) | |
| { | |
| // Set the network for this client wrapper | |
| _network = network; | |
| // order servers by weight | |
| foreach (var server in network.Servers.OrderBy(x => x.Weight)) | |
| { | |
| // initalize irc client | |
| var ircClient = new IrcClient(); | |
| ircClient.FloodPreventer = new IrcStandardFloodPreventer(4, 2000); | |
| ircClient.Connected += ClientOnConnected; | |
| ircClient.Registered += ClientOnRegistered; | |
| ircClient.Disconnected += ClientOnDisconnected; | |
| ircClient.ChannelListReceived += ClientOnChannelListReceived; | |
| using (var connectedEvent = new ManualResetEventSlim(false)) | |
| { | |
| Console.WriteLine("Attempting to connect to '{0}'.", server.Host); | |
| ircClient.Connected += (sender, args) => connectedEvent.Set(); | |
| ircClient.Connect(server.Host, server.Port.GetValueOrDefault(6667), server.Secure, new IrcUserRegistrationInfo() { NickName = "testbot1", RealName = "testbot1", UserName = "testbot1" }); | |
| // wait for the client to connect | |
| if (connectedEvent.Wait(10000)) | |
| { | |
| connectedEvent.Reset(); | |
| ircClient.Registered += (sender, args) => connectedEvent.Set(); | |
| connectedEvent.Wait(); | |
| ircClient.Channels.Join(network.Channels.Select(c => c.Name)); | |
| _server = server; | |
| _ircClient = ircClient; | |
| _ctcpClient = new CtcpClient(_ircClient); | |
| return; | |
| } | |
| Console.WriteLine("Connection to '{0}' timed out.", server.Host); | |
| ircClient.Dispose(); | |
| } | |
| } | |
| } | |
| #region Client Events | |
| private void ClientOnConnected(object sender, EventArgs e) | |
| { | |
| Console.WriteLine("Client connected."); | |
| } | |
| private void ClientOnDisconnected(object sender, EventArgs e) | |
| { | |
| var client = (IrcClient)sender; | |
| Console.WriteLine("Client disconnected."); | |
| } | |
| private void ClientOnChannelListReceived(object sender, IrcChannelListReceivedEventArgs e) | |
| { | |
| var client = (IrcClient)sender; | |
| Console.WriteLine("Channel listing received."); | |
| Console.WriteLine("Channels: " + String.Join(",", e.Channels.Select(c => c.Name))); | |
| } | |
| private void ClientOnRegistered(object sender, EventArgs eventArgs) | |
| { | |
| var client = (IrcClient)sender; | |
| client.LocalUser.NoticeReceived += LocalUserOnNoticeReceived; | |
| client.LocalUser.MessageReceived += LocalUserOnMessageReceived; | |
| client.LocalUser.JoinedChannel += LocalUserOnJoinedChannel; | |
| client.LocalUser.LeftChannel += LocalUserOnLeftChannel; | |
| Console.Beep(); | |
| Console.WriteLine("Client registered."); | |
| } | |
| #endregion | |
| #region Local User Events | |
| private void LocalUserOnLeftChannel(object sender, IrcChannelEventArgs e) | |
| { | |
| var localUser = (IrcLocalUser)sender; | |
| e.Channel.UserJoined -= ChannelOnUserJoined; | |
| e.Channel.UserLeft -= ChannelOnUserLeft; | |
| e.Channel.MessageReceived -= ChannelOnMessageReceived; | |
| e.Channel.NoticeReceived -= ChannelOnNoticeReceived; | |
| Console.WriteLine("Left channel '{0}'", e.Channel.Name); | |
| } | |
| private void LocalUserOnJoinedChannel(object sender, IrcChannelEventArgs e) | |
| { | |
| var localUser = (IrcLocalUser)sender; | |
| e.Channel.UserJoined += ChannelOnUserJoined; | |
| e.Channel.UserLeft += ChannelOnUserLeft; | |
| e.Channel.MessageReceived += ChannelOnMessageReceived; | |
| e.Channel.NoticeReceived += ChannelOnNoticeReceived; | |
| Console.WriteLine("Joined channel '{0}'", e.Channel.Name); | |
| } | |
| private void LocalUserOnMessageReceived(object sender, IrcMessageEventArgs e) | |
| { | |
| var localUser = (IrcLocalUser)sender; | |
| Console.WriteLine("Message from '{0}'", e.Source.Name); | |
| Console.WriteLine("Message contents '{0}'", e.Text); | |
| if (OnMessageReceived != null) | |
| OnMessageReceived(sender, e); | |
| } | |
| private void LocalUserOnNoticeReceived(object sender, IrcMessageEventArgs e) | |
| { | |
| var localUser = (IrcLocalUser)sender; | |
| if (OnMessageReceived != null) | |
| OnMessageReceived(sender, e); | |
| } | |
| #endregion | |
| # region Channel Events | |
| private void ChannelOnNoticeReceived(object sender, IrcMessageEventArgs e) | |
| { | |
| var channel = (IrcChannel)sender; | |
| if (OnMessageReceived != null) | |
| OnMessageReceived(sender, e); | |
| } | |
| private void ChannelOnMessageReceived(object sender, IrcMessageEventArgs e) | |
| { | |
| var channel = (IrcChannel)sender; | |
| if (OnMessageReceived != null) | |
| OnMessageReceived(sender, e); | |
| } | |
| private void ChannelOnUserLeft(object sender, IrcChannelUserEventArgs e) | |
| { | |
| var channel = (IrcChannel)sender; | |
| } | |
| private void ChannelOnUserJoined(object sender, IrcChannelUserEventArgs e) | |
| { | |
| var channel = (IrcChannel)sender; | |
| } | |
| #endregion | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment