Browse Source

Added Methods for sending IRC Commands. Restructured class hierarchy.

pull/1/head
Teknikode 8 years ago
parent
commit
d4f0c2b45c
  1. 4
      Bot.cs
  2. 3
      Combot.csproj
  3. 23
      IRC/IRCMessages.cs
  4. 200
      IRC/IRCMethods.cs
  5. 63
      IRC/IRCService.cs
  6. 9
      IRC/IRCTypes.cs
  7. 130
      IRC/Messages.cs

4
Bot.cs

@ -8,9 +8,11 @@ namespace Combot @@ -8,9 +8,11 @@ namespace Combot
{
class Bot
{
public event Action<BotError> ErrorEvent;
public Config Config;
private IRCService _ircService;
internal IRCService _ircService;
internal Bot()
{

3
Combot.csproj

@ -42,9 +42,10 @@ @@ -42,9 +42,10 @@
<Compile Include="Access.cs" />
<Compile Include="Bot.cs" />
<Compile Include="Config.cs" />
<Compile Include="IRC\IRCMethods.cs" />
<Compile Include="IRC\IRCService.cs" />
<Compile Include="IRC\IRCTypes.cs" />
<Compile Include="IRC\Messages.cs" />
<Compile Include="IRC\IRCMessages.cs" />
<Compile Include="Methods.cs" />
<Compile Include="Module.cs" />
<Compile Include="Properties.cs" />

23
IRC/IRCMessages.cs

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Combot
{
internal partial class IRCService
{
public event Action<Message> MessageReceivedEvent;
internal void ParseTCPMessage(string message)
{
Message parsedMessage = new Message();
if (MessageReceivedEvent != null)
{
MessageReceivedEvent(parsedMessage);
}
}
}
}

200
IRC/IRCMethods.cs

@ -0,0 +1,200 @@ @@ -0,0 +1,200 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace Combot
{
internal partial class IRCService
{
/// <summary>
/// Sends a private message to a nick or channel
/// </summary>
/// <param name="nick"></param>
/// <param name="message"></param>
protected void SendPrivMessage(Nick nick, string message)
{
SendTCPMessage(string.Format("PRIVMSG {0} :{1}", nick, message));
}
protected void SendPrivMessage(List<Nick> nicks, string message)
{
string nick_list = string.Empty;
foreach (Nick nick in nicks)
{
nick_list += nick.Nickname + ",";
}
SendTCPMessage(string.Format("PRIVMSG {0} :{1}", nick_list.TrimEnd(','), message));
}
protected void SendPrivMessage(Channel channel, string message)
{
SendTCPMessage(string.Format("PRIVMSG {0} :{1}", channel.Name, message));
}
protected void SendPrivMessage(List<Channel> channels, string message)
{
string channel_list = string.Empty;
foreach (Channel channel in channels)
{
channel_list += channel.Name + ",";
}
SendTCPMessage(string.Format("PRIVMSG {0} :{1}", channel_list.TrimEnd(','), message));
}
/// <summary>
/// Sends a Notice to either a nick or channel
/// </summary>
/// <param name="nick"></param>
/// <param name="message"></param>
protected void SendNotice(Nick nick, string message)
{
SendTCPMessage(string.Format("NOTICE {0} :{1}", nick, message));
}
protected void SendNotice(List<Nick> nicks, string message)
{
string nick_list = string.Empty;
foreach (Nick nick in nicks)
{
nick_list += nick.Nickname + ",";
}
SendTCPMessage(string.Format("NOTICE {0} :{1}", nick_list.TrimEnd(','), message));
}
protected void SendNotice(Channel channel, string message)
{
SendTCPMessage(string.Format("NOTICE {0} :{1}", channel.Name, message));
}
protected void SendNotice(List<Channel> channels, string message)
{
string channel_list = string.Empty;
foreach (Channel channel in channels)
{
channel_list += channel.Name + ",";
}
SendTCPMessage(string.Format("NOTICE {0} :{1}", channel_list.TrimEnd(','), message));
}
/// <summary>
/// Sends the connection password
/// </summary>
/// <param name="password"></param>
protected void SendPassword(string password)
{
SendTCPMessage(password);
}
/// <summary>
/// Sends a Nick command to set the nickname
/// </summary>
/// <param name="nick"></param>
protected void SendNick(string nick)
{
SendTCPMessage(nick);
}
/// <summary>
/// Sends the User command to set a user
/// </summary>
/// <param name="user"></param>
protected void SendUser(string username, string hostname, string servername, string realname)
{
SendTCPMessage(string.Format("USER {0} {1} {2} :{3}", username, hostname, servername, realname));
}
/// <summary>
/// Sends the Oper command to authorize the client as a newtork Oper
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
protected void SendOper(string username, string password)
{
SendTCPMessage(string.Format("OPER {0} {1}", username, password));
}
/// <summary>
/// Sends a Quit command to end the client session
/// </summary>
/// <param name="message"></param>
protected void SendQuit()
{
SendTCPMessage("QUIT");
}
protected void SendQuit(string message)
{
SendTCPMessage(string.Format("QUIT :{0}", message));
}
/// <summary>
/// Sends a Join command to join a channel
/// </summary>
/// <param name="channel"></param>
protected void SendJoin(Channel channel)
{
string message = string.Empty;
message = (channel.Key != string.Empty) ? string.Format("{0}; {1}", channel.Name, channel.Key) : channel.Name;
SendTCPMessage(string.Format("JOIN {0}", message));
}
protected void SendJoin(List<Channel> channels)
{
string message = string.Empty;
string channel_string = string.Empty;
string key_string = string.Empty;
foreach (Channel channel in channels)
{
channel_string += channel.Name + ",";
if (channel.Key != string.Empty)
{
key_string += channel.Key + ",";
}
}
channel_string = channel_string.TrimEnd(',');
key_string = key_string.TrimEnd(',');
message = (key_string != string.Empty) ? string.Format("{0}; {1}", channel_string, key_string) : channel_string;
SendTCPMessage(string.Format("JOIN {0}", message));
}
/// <summary>
/// Sends a Part command to leave a channel
/// </summary>
/// <param name="channel"></param>
protected void SendPart(Channel channel)
{
SendTCPMessage(string.Format("PART {0}", channel.Name));
}
protected void SendPart(List<Channel> channels)
{
string channel_list = string.Empty;
foreach (Channel channel in channels)
{
channel_list += channel.Name + ",";
}
SendTCPMessage(string.Format("PART {0}", channel_list.TrimEnd(',')));
}
/// <summary>
/// Sends a Mode command for either a channel mode or user mode
/// </summary>
/// <param name="channel"></param>
/// <param name="mode"></param>
protected void SendMode(Channel channel, ChannelMode mode)
{
}
}
}

63
IRC/IRCService.cs

@ -2,32 +2,38 @@ @@ -2,32 +2,38 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
namespace Combot
{
internal class IRCService
internal partial class IRCService
{
public Action<BotError> ErrorEvent;
private Bot _bot;
private TCPInterface _tcp;
private Messages _messages;
protected Bot _Bot;
protected TCPInterface _TCP;
protected Thread TCPReader;
protected event Action<string> TCPMessageEvent;
internal IRCService(Bot bot)
{
_bot = bot;
_tcp = new TCPInterface();
_messages = new Messages(_tcp, _bot);
_Bot = bot;
_TCP = new TCPInterface();
TCPMessageEvent += ParseTCPMessage;
}
internal bool Connect(IPAddress IP, int port, int readTimeout, int allowedFailedCount = 0)
{
bool result = false;
if (!_tcp.Connected)
if (!_TCP.Connected)
{
result = _tcp.Connect(IP, port, readTimeout, allowedFailedCount);
result = _TCP.Connect(IP, port, readTimeout, allowedFailedCount);
if (result)
{
TCPReader = new Thread(ReadTCPMessages);
TCPReader.IsBackground = true;
TCPReader.Start();
}
}
return result;
@ -37,12 +43,43 @@ namespace Combot @@ -37,12 +43,43 @@ namespace Combot
{
bool result = false;
if (_tcp.Connected)
if (_TCP.Connected)
{
_tcp.Disconnect();
_TCP.Disconnect();
}
return result;
}
protected void ReadTCPMessages()
{
while (_TCP.Connected)
{
string response = ReadTCPMessage();
if (TCPMessageEvent != null && response != null && response != string.Empty)
{
TCPMessageEvent(response);
}
Thread.Sleep(10);
}
}
protected string ReadTCPMessage()
{
if (_TCP.Connected)
{
return _TCP.Read();
}
return null;
}
protected void SendTCPMessage(string message)
{
if (_TCP.Connected)
{
_TCP.Write(message);
}
}
}
}

9
IRC/IRCTypes.cs

@ -234,6 +234,7 @@ namespace Combot @@ -234,6 +234,7 @@ namespace Combot
{
public string Name { get; set; }
public string Topic { get; set; }
public string Key { get; set; }
public DateTime Registration { get; set; }
public List<ChannelMode> Modes { get; set; }
public List<Nick> Nicks { get; set; }
@ -242,6 +243,7 @@ namespace Combot @@ -242,6 +243,7 @@ namespace Combot
{
Name = string.Empty;
Topic = string.Empty;
Key = string.Empty;
Registration = DateTime.Now;
Modes = new List<ChannelMode>();
Nicks = new List<Nick>();
@ -315,6 +317,11 @@ namespace Combot @@ -315,6 +317,11 @@ namespace Combot
public Nick Sender { get; set; }
public Nick Receiver { get; set; }
public Channel Channel { get; set; }
public string Message { get; set; }
public string Parameters { get; set; }
}
public class Sender
{
}
}

130
IRC/Messages.cs

@ -1,130 +0,0 @@ @@ -1,130 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Combot
{
class Messages
{
public event Action<Message> MessageReceivedEvent;
public event Action<Message> MessageSentEvent;
private TCPInterface _tcp;
private Bot _bot;
internal Messages(TCPInterface tcp, Bot bot)
{
_tcp = tcp;
_bot = bot;
}
private void MessageParser()
{
while(_tcp.Connected)
{
string response = _tcp.Read();
}
}
internal void SendMessage(Message message)
{
if (message.Type == MessageType.Channel || message.Type == MessageType.Query)
{
if (MessageSentEvent != null)
{
MessageSentEvent(message);
}
}
}
internal void SendMessage(Nick nick, string message)
{
Message msg = new Message();
msg.Receiver = nick;
msg.Type = MessageType.Query;
msg.Sender = _bot.Config.Nick;
msg.Message = message;
SendMessage(msg);
}
internal void SendMessage(Channel channel, string message)
{
Message msg = new Message();
msg.Channel = channel;
msg.Type = MessageType.Channel;
msg.Sender = _bot.Config.Nick;
msg.Message = message;
SendMessage(msg);
}
internal void SendNotice(Message message)
{
if (message.Type == MessageType.Notice)
{
if (MessageSentEvent != null)
{
MessageSentEvent(message);
}
}
}
internal void SendNotice(Nick nick, string message)
{
Message msg = new Message();
msg.Receiver = nick;
msg.Type = MessageType.Notice;
msg.Sender = _bot.Config.Nick;
msg.Message = message;
SendNotice(msg);
}
internal void SendNotice(Channel channel, string message)
{
Message msg = new Message();
msg.Channel = channel;
msg.Type = MessageType.Notice;
msg.Sender = _bot.Config.Nick;
msg.Message = message;
SendNotice(msg);
}
internal void SendCTCP(Message message)
{
if (message.Type == MessageType.CTCP)
{
if (MessageSentEvent != null)
{
MessageSentEvent(message);
}
}
}
internal void SendCTCP(Nick nick, string message)
{
Message msg = new Message();
msg.Receiver = nick;
msg.Type = MessageType.CTCP;
msg.Sender = _bot.Config.Nick;
msg.Message = message;
SendCTCP(msg);
}
internal void SendCTCP(Channel channel, string message)
{
Message msg = new Message();
msg.Channel = channel;
msg.Type = MessageType.CTCP;
msg.Sender = _bot.Config.Nick;
msg.Message = message;
SendCTCP(msg);
}
}
}
Loading…
Cancel
Save