Browse Source

Added updating of Channel object automatically. Fixed bugs.

tags/3.0.0
Teknikode 4 years ago
parent
commit
c9027431f9

+ 0
- 3
Combot.sln View File

@@ -31,7 +31,4 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal

+ 5
- 5
Combot/Bot.cs View File

@@ -40,6 +40,11 @@ namespace Combot
}
while (!Connected);

if (Connected)
{
IRC.Login(Config.Server.Name, new Nick() { Nickname = Config.Nick, Host = Dns.GetHostName(), Realname = Config.Realname });
}

return Connected;
}

@@ -51,11 +56,6 @@ namespace Combot
return Connected;
}

public void Login()
{
IRC.Login(Config.Server.Name, new Nick() { Nickname = Config.Nick, Host = Dns.GetHostName(), Realname = Config.Realname });
}

private void HandleDisconnectEvent()
{
Connected = false;

+ 48
- 45
IRCServices/Channel.cs View File

@@ -14,12 +14,11 @@ namespace Combot.IRCServices
public bool AutoJoin { get; set; }
public bool Joined { get; set; }
public DateTime Registration { get; set; }
public List<string> Bans { get; set; }
public List<ChannelMode> Modes { get; set; }
public List<Nick> Nicks { get; set; }

private IRC _IRC;

public Channel(IRC irc)
public Channel()
{
Name = string.Empty;
Topic = string.Empty;
@@ -27,9 +26,9 @@ namespace Combot.IRCServices
AutoJoin = false;
Joined = false;
Registration = DateTime.Now;
Bans = new List<string>();
Modes = new List<ChannelMode>();
Nicks = new List<Nick>();
_IRC = irc;
}

public void AddNick(Nick nick)
@@ -50,6 +49,14 @@ namespace Combot.IRCServices
}
}

public void RemoveNick(string nickname)
{
if (Nicks.Exists(nick => nick.Nickname == nickname))
{
Nicks.Remove(Nicks.Find(nick => nick.Nickname == nickname));
}
}

public void RemoveNicks(List<Nick> nicks)
{
foreach (Nick nick in nicks)
@@ -58,81 +65,77 @@ namespace Combot.IRCServices
}
}

public void AddMode(ChannelMode mode)
public void RemoveNicks(List<string> nicks)
{
if (!Modes.Contains(mode))
foreach (string nick in nicks)
{
ChannelModeInfo modeInfo = new ChannelModeInfo();
modeInfo.Mode = mode;
modeInfo.Set = true;
_IRC.IRCSendMode(Name, modeInfo);
Modes.Add(mode);
RemoveNick(nick);
}
}

public void AddModes(List<ChannelMode> modes)
public Nick GetNick(string nickname)
{
if (!modes.TrueForAll(mode => Modes.Contains(mode)))
Nick foundNick = Nicks.Find(nick => nick.Nickname == nickname);
return foundNick;
}

public List<Nick> GetNicks(List<string> nicknames)
{
List<Nick> foundNicks = new List<Nick>();
foreach (string nickname in nicknames)
{
List<ChannelModeInfo> modeInfos = new List<ChannelModeInfo>();
modes.ForEach(mode => modeInfos.Add(new ChannelModeInfo() { Mode = mode, Set = true }));
_IRC.IRCSendMode(Name, modeInfos);
Modes.AddRange(modes);
Nick foundNick = GetNick(nickname);
if (foundNick != null)
{
foundNicks.Add(foundNick);
}
}
return foundNicks;
}

public void RemoveMode(ChannelMode mode)
public void AddMode(ChannelMode mode)
{
if (Modes.Contains(mode))
if (!Modes.Contains(mode))
{
ChannelModeInfo modeInfo = new ChannelModeInfo();
modeInfo.Mode = mode;
modeInfo.Set = false;
_IRC.IRCSendMode(Name, modeInfo);
Modes.Remove(mode);
Modes.Add(mode);
}
}

public void RemoveModes(List<ChannelMode> modes)
public void AddModes(List<ChannelMode> modes)
{
List<ChannelMode> validModes = Modes.FindAll(mode => mode == modes.Find(m => m == mode));
List<ChannelModeInfo> modeInfos = new List<ChannelModeInfo>();
validModes.ForEach(mode => modeInfos.Add(new ChannelModeInfo() { Mode = mode, Set = true }));
validModes.ForEach(mode => Modes.Remove(mode));
_IRC.IRCSendMode(Name, modeInfos);
foreach (ChannelMode mode in modes)
{
AddMode(mode);
}
}

public void Join()
public void RemoveMode(ChannelMode mode)
{
if (!Joined)
if (Modes.Contains(mode))
{
_IRC.IRCSendJoin(Name, Key);
Joined = true;
Modes.Remove(mode);
}
}

public void Part()
public void RemoveModes(List<ChannelMode> modes)
{
if (Joined)
foreach (ChannelMode mode in modes)
{
_IRC.IRCSendPart(Name);
Joined = false;
RemoveMode(mode);
}
}

public void SetTopic(string topic)
public void AddBan(string mask)
{

if (Joined)
if (!Bans.Contains(mask))
{
_IRC.IRCSendTopic(Name, topic);
Bans.Add(mask);
}
}

public string GetTopic()
public void RemoveBan(string mask)
{

return string.Empty;
Bans.Remove(mask);
}
}
}

+ 262
- 4
IRCServices/IRC.cs View File

@@ -12,23 +12,38 @@ namespace Combot.IRCServices
{
public partial class IRC
{
public bool Connected;
public List<Channel> Channels = new List<Channel>();
public Messages Message;
public event Action DisconnectEvent;
public event Action<TCPError> TCPErrorEvent;
public string Nickname { get; set; }
public Dictionary<string, PrivaledgeMode> PrivaledgeMapping = new Dictionary<string, PrivaledgeMode>() { { "+", PrivaledgeMode.v }, { "%", PrivaledgeMode.h }, { "@", PrivaledgeMode.o }, { "&", PrivaledgeMode.a }, { "~", PrivaledgeMode.q } };

private TCPInterface _TCP;
private Thread TCPReader;
private event Action<string> TCPMessageEvent;
private ReaderWriterLockSlim ChannelRWLock;

public IRC()
{
Connected = false;
_TCP = new TCPInterface();
Message = new Messages(this);
Nickname = string.Empty;
ChannelRWLock = new ReaderWriterLockSlim();

TCPMessageEvent += Message.ParseTCPMessage;
_TCP.TCPConnectionEvent += HandleTCPConnection;
_TCP.TCPErrorEvent += HandleTCPError;
Message.ErrorMessageEvent += HandleErrorMessage;
Message.PingEvent += HandlePing;
Message.ServerReplyEvent += HandleReply;
Message.ChannelModeChangeEvent += HandleChannelModeChange;
Message.UserModeChangeEvent += HandleUserModeChange;
Message.NickChangeEvent += HandleNickChange;
Message.JoinChannelEvent += HandleJoin;
Message.PartChannelEvent += HandlePart;
Message.KickEvent += HandleKick;
Message.QuitEvent += HandleQuit;
}

public bool Connect(IPAddress IP, int port, int readTimeout = 5000, int allowedFailedCount = 0)
@@ -67,6 +82,7 @@ namespace Combot.IRCServices

public void Login(string serverName, Nick nick)
{
Nickname = nick.Nickname;
IRCSendNick(nick.Nickname);
IRCSendUser(nick.Nickname, nick.Host, serverName, nick.Realname);
}
@@ -102,14 +118,256 @@ namespace Combot.IRCServices
}
}

private void HandleErrorMessage(object sender, IRCServices.Messaging.ErrorMessage e)
private void HandleTCPConnection(int e)
{
if (DisconnectEvent != null)
{
DisconnectEvent();
}
}

private void HandleTCPError(TCPError e)
{
if (TCPErrorEvent != null)
{
TCPErrorEvent(e);
}
}

private void HandleErrorMessage(object sender, ErrorMessage e)
{
Disconnect();
}

private void HandlePing(object sender, IRCServices.Messaging.PingInfo e)
private void HandlePing(object sender, PingInfo e)
{
IRCSendPong(e.Message);
}

private void HandleReply(object sender, IReply e)
{
if (e.GetType() == typeof(ServerReplyMessage))
{
ServerReplyMessage msg = (ServerReplyMessage)e;
switch (msg.ReplyCode)
{
case IRCReplyCode.RPL_WHOREPLY:
ChannelRWLock.EnterWriteLock();
string[] msgSplit = msg.Message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string target = msgSplit[0];
if (target.StartsWith("&") || target.StartsWith("#"))
{
if (msgSplit.GetUpperBound(0) >= 7)
{
string nickname = msgSplit[4];
string realname = msgSplit[7];
string username = msgSplit[1];
string host = msgSplit[2];
string modeString = msgSplit[5];
Channel channel = Channels.Find(chan => chan.Name == target);
if (channel != null)
{
Nick nick = channel.GetNick(nickname);
bool nickFound = true;
if (nick == null)
{
nickFound = false;
nick = new Nick();
}
nick.Host = host;
nick.Realname = realname;
nick.Username = username;
nick.Modes = new List<UserMode>();
nick.Privaledges = new List<PrivaledgeMode>();
char[] modeArr = modeString.ToCharArray();
for (int i = 1; i <= modeArr.GetUpperBound(0); i++)
{
if (PrivaledgeMapping.ContainsKey(modeArr[i].ToString()))
{
nick.Privaledges.Add(PrivaledgeMapping[modeArr[i].ToString()]);
}
else if (modeArr[i].ToString() == "*")
{
nick.Modes.Add(UserMode.o);
}
else
{
nick.Modes.Add((UserMode)Enum.Parse(typeof(UserMode), modeArr[i].ToString()));
}
}
if (!nickFound)
{
channel.AddNick(nick);
}
}
}
}
ChannelRWLock.ExitWriteLock();
break;
default:
break;
}
}
else
{
ServerErrorMessage msg = (ServerErrorMessage)e;
}
}

private void HandleChannelModeChange(object sender, ChannelModeChangeInfo e)
{
ChannelRWLock.EnterWriteLock();
Channel channel = Channels.Find(chan => chan.Name == e.Channel);
if (channel != null)
{
foreach (ChannelModeInfo mode in e.Modes)
{
switch (mode.Mode)
{
case ChannelMode.v:
case ChannelMode.h:
case ChannelMode.o:
case ChannelMode.a:
case ChannelMode.q:
Nick changedNick = channel.GetNick(mode.Parameter);
if (mode.Set)
{
changedNick.AddPrivaledge((PrivaledgeMode)Enum.Parse(typeof(PrivaledgeMode), mode.Mode.ToString()));
}
else
{
changedNick.RemovePrivaledge((PrivaledgeMode)Enum.Parse(typeof(PrivaledgeMode), mode.Mode.ToString()));
}
break;
case ChannelMode.b:
if (mode.Set)
{
channel.AddBan(mode.Parameter);
}
else
{
channel.RemoveBan(mode.Parameter);
}
break;
case ChannelMode.k:
if (mode.Set)
{
channel.AddMode(mode.Mode);
channel.Key = mode.Parameter;
}
else
{
channel.RemoveMode(mode.Mode);
channel.Key = string.Empty;
}
break;
default:
if (mode.Set)
{
channel.AddMode(mode.Mode);
}
else
{
channel.RemoveMode(mode.Mode);
}
break;
}
}
}
ChannelRWLock.ExitWriteLock();
}

private void HandleUserModeChange(object sender, UserModeChangeInfo e)
{
ChannelRWLock.EnterWriteLock();
for (int i = 0; i < Channels.Count; i++)
{
Nick changedNick = Channels[i].GetNick(e.Nick.Nickname);
if (changedNick != null)
{
foreach (UserModeInfo mode in e.Modes)
{
if (mode.Set)
{
changedNick.AddMode(mode.Mode);
}
else
{
changedNick.RemoveMode(mode.Mode);
}
}
}
}
ChannelRWLock.ExitWriteLock();
}

private void HandleNickChange(object sender, NickChangeInfo e)
{
ChannelRWLock.EnterWriteLock();
for (int i = 0; i < Channels.Count; i++)
{
Nick newNick = Channels[i].GetNick(e.OldNick.Nickname);
if (newNick != null)
{
newNick.Nickname = e.NewNick.Nickname;
}
}
ChannelRWLock.ExitWriteLock();
}

private void HandleJoin(object sender, JoinChannelInfo e)
{
ChannelRWLock.EnterWriteLock();
Channel channel = Channels.Find(chan => chan.Name == e.Channel);
if (channel != null)
{
channel.AddNick(e.Nick);
}
else
{
Channel newChannel = new Channel();
newChannel.Name = e.Channel;
if (e.Nick.Nickname == Nickname)
{
newChannel.Joined = true;
}
newChannel.Nicks.Add(e.Nick);
Channels.Add(newChannel);
IRCSendWho(newChannel.Name);
}
ChannelRWLock.ExitWriteLock();
}

private void HandlePart(object sender, PartChannelInfo e)
{
ChannelRWLock.EnterWriteLock();
Channel channel = Channels.Find(chan => chan.Name == e.Channel);
if (channel != null)
{
channel.RemoveNick(e.Nick.Nickname);
}
ChannelRWLock.ExitWriteLock();
}

private void HandleKick(object sender, KickInfo e)
{
ChannelRWLock.EnterWriteLock();
Channel channel = Channels.Find(chan => chan.Name == e.Channel);
if (channel != null)
{
channel.RemoveNick(e.Nick.Nickname);
}
ChannelRWLock.ExitWriteLock();
}

private void HandleQuit(object sender, QuitInfo e)
{
ChannelRWLock.EnterWriteLock();
for (int i = 0; i < Channels.Count; i++)
{
Channels[i].RemoveNick(e.Nick.Nickname);
}
ChannelRWLock.ExitWriteLock();
}
}
}

IRCServices/Methods.cs → IRCServices/IRCSend.cs View File


+ 1
- 1
IRCServices/IRCServices.csproj View File

@@ -45,7 +45,7 @@
<Compile Include="Messaging\GetReply.cs" />
<Compile Include="Messaging\Messages.cs" />
<Compile Include="Messaging\MessageTypes.cs" />
<Compile Include="Methods.cs" />
<Compile Include="IRCSend.cs" />
<Compile Include="Nick.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TCP\TCPInterface.cs" />

+ 20
- 7
IRCServices/Messaging/MessageTypes.cs View File

@@ -33,7 +33,7 @@ namespace Combot.IRCServices.Messaging

public class ChannelMessage : IMessage
{
public Channel Channel { get; set; }
public string Channel { get; set; }
public Nick Sender { get; set; }
public string Message { get; set; }
}
@@ -51,7 +51,7 @@ namespace Combot.IRCServices.Messaging

public class ChannelNotice : IMessage
{
public Channel Channel { get; set; }
public string Channel { get; set; }
public Nick Sender { get; set; }
public string Message { get; set; }
}
@@ -64,14 +64,14 @@ namespace Combot.IRCServices.Messaging

public class TopicChangeInfo : IMessage
{
public Channel Channel { get; set; }
public string Channel { get; set; }
public Nick Nick { get; set; }
public string Topic { get; set; }
}

public class ChannelModeChangeInfo : IMessage
{
public Channel Channel { get; set; }
public string Channel { get; set; }
public Nick Nick { get; set; }
public List<ChannelModeInfo> Modes { get; set; }
}
@@ -82,21 +82,34 @@ namespace Combot.IRCServices.Messaging
public List<UserModeInfo> Modes { get; set; }
}

public class NickChangeInfo : IMessage
{
public Nick OldNick { get; set; }
public Nick NewNick { get; set; }
}

public class InviteChannelInfo : IMessage
{
public string Channel { get; set; }
public Nick Requester { get; set; }
public Nick Recipient { get; set; }
}

public class JoinChannelInfo : IMessage
{
public Channel Channel { get; set; }
public string Channel { get; set; }
public Nick Nick { get; set; }
}

public class PartChannelInfo : IMessage
{
public Channel Channel { get; set; }
public string Channel { get; set; }
public Nick Nick { get; set; }
}

public class KickInfo : IMessage
{
public Channel Channel { get; set; }
public string Channel { get; set; }
public Nick Nick { get; set; }
public Nick KickedNick { get; set; }
public string Reason { get; set; }

+ 48
- 45
IRCServices/Messaging/Messages.cs View File

@@ -9,6 +9,7 @@ namespace Combot.IRCServices.Messaging
{
public class Messages
{
public event EventHandler<string> RawMessageEvent;
public event EventHandler<IReply> ServerReplyEvent;
public event EventHandler<ErrorMessage> ErrorMessageEvent;
public event EventHandler<ChannelMessage> ChannelMessageReceivedEvent;
@@ -19,6 +20,8 @@ namespace Combot.IRCServices.Messaging
public event EventHandler<TopicChangeInfo> TopicChangeEvent;
public event EventHandler<ChannelModeChangeInfo> ChannelModeChangeEvent;
public event EventHandler<UserModeChangeInfo> UserModeChangeEvent;
public event EventHandler<NickChangeInfo> NickChangeEvent;
public event EventHandler<InviteChannelInfo> InviteChannelEvent;
public event EventHandler<JoinChannelInfo> JoinChannelEvent;
public event EventHandler<PartChannelInfo> PartChannelEvent;
public event EventHandler<KickInfo> KickEvent;
@@ -36,8 +39,8 @@ namespace Combot.IRCServices.Messaging
internal void ParseTCPMessage(string tcpMessage)
{
DateTime messageTime = DateTime.Now;
Regex messageRegex = new Regex(@"^:(?<Sender>[^\s]+)\s(?<Type>[^\s]+)\s(?<Recipient>[^\s]+)\s(?<Args>.*)", RegexOptions.None);
Regex senderRegex = new Regex(@"^:(?<Nick>[^\s]+)!(?<Realname>[^\s]+)@(?<Host>[^\s]+)", RegexOptions.None);
Regex messageRegex = new Regex(@"^:(?<Sender>[^\s]+)\s(?<Type>[^\s]+)\s(?<Recipient>[^\s]+)\s?(?<Args>.*)", RegexOptions.None);
Regex senderRegex = new Regex(@"^(?<Nick>[^\s]+)!(?<Realname>[^\s]+)@(?<Host>[^\s]+)", RegexOptions.None);
Regex pingRegex = new Regex(@"^PING :(?<Message>.+)", RegexOptions.None);
Regex pongRegex = new Regex(@"^PONG :(?<Message>.+)", RegexOptions.None);
Regex errorRegex = new Regex(@"^ERROR :(?<Message>.+)", RegexOptions.None);
@@ -61,7 +64,7 @@ namespace Combot.IRCServices.Messaging
{
senderNick = senderMatch.Groups["Nick"].Value;
senderRealname = senderMatch.Groups["Realname"].Value;
senderHost = senderMatch.Groups["Nick"].Value;
senderHost = senderMatch.Groups["Host"].Value;
}

int replyCode;
@@ -72,14 +75,14 @@ namespace Combot.IRCServices.Messaging
{
if (ServerReplyEvent != null)
{
ServerReplyEvent(this, new ServerReplyMessage() { TimeStamp = messageTime, ReplyCode = (IRCReplyCode)replyCode, Message = args.Remove(0, 1) });
ServerReplyEvent(this, new ServerReplyMessage() { TimeStamp = messageTime, ReplyCode = (IRCReplyCode)replyCode, Message = args });
}
}
else if (Enum.IsDefined(typeof(IRCErrorCode), replyCode))
{
if (ServerReplyEvent != null)
{
ServerReplyEvent(this, new ServerErrorMessage() { TimeStamp = messageTime, ErrorCode = (IRCErrorCode)replyCode, Message = args.Remove(0, 1) });
ServerReplyEvent(this, new ServerErrorMessage() { TimeStamp = messageTime, ErrorCode = (IRCErrorCode)replyCode, Message = args });
}
}
}
@@ -91,15 +94,8 @@ namespace Combot.IRCServices.Messaging
if (recipient.StartsWith("&") || recipient.StartsWith("#"))
{
ChannelMessage msg = new ChannelMessage();
msg.Channel = _IRC.Channels.Find(channel => channel.Name == recipient);
if (msg.Channel != null && msg.Channel.Modes != null && msg.Channel.Modes.Contains(ChannelMode.n))
{
msg.Sender = msg.Channel.Nicks.Find(nick => nick.Nickname == senderNick);
}
else
{
msg.Sender = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
}
msg.Channel = recipient;
msg.Sender = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
msg.Message = args.Remove(0, 1);

if (ChannelMessageReceivedEvent != null)
@@ -123,15 +119,8 @@ namespace Combot.IRCServices.Messaging
if (recipient.StartsWith("&") || recipient.StartsWith("#"))
{
ChannelNotice msg = new ChannelNotice();
msg.Channel = _IRC.Channels.Find(channel => channel.Name == recipient);
if (msg.Channel != null && msg.Channel.Modes != null && msg.Channel.Modes.Contains(ChannelMode.n))
{
msg.Sender = msg.Channel.Nicks.Find(nick => nick.Nickname == senderNick);
}
else
{
msg.Sender = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
}
msg.Channel = recipient;
msg.Sender = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
msg.Message = args.Remove(0, 1);

if (ChannelNoticeReceivedEvent != null)
@@ -156,15 +145,8 @@ namespace Combot.IRCServices.Messaging
{
ChannelModeChangeInfo modeMsg = new ChannelModeChangeInfo();
modeMsg.Modes = new List<ChannelModeInfo>();
modeMsg.Channel = _IRC.Channels.Find(channel => channel.Name == recipient);
if (modeMsg.Channel != null && modeMsg.Channel.Nicks != null && modeMsg.Channel.Nicks.Exists(nick => nick.Nickname == senderNick))
{
modeMsg.Nick = modeMsg.Channel.Nicks.Find(nick => nick.Nickname == senderNick);
}
else
{
modeMsg.Nick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
}
modeMsg.Channel = recipient;
modeMsg.Nick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };

string[] modeArgs = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
char[] modeInfo = modeArgs[0].TrimStart(':').ToCharArray();
@@ -185,7 +167,7 @@ namespace Combot.IRCServices.Messaging
ChannelModeInfo newMode = new ChannelModeInfo();
newMode.Set = set;
newMode.Mode = (ChannelMode)Enum.Parse(typeof(ChannelMode), mode.ToString());
if (modeArgs.GetUpperBound(0) > argIndex)
if (modeArgs.GetUpperBound(0) >= argIndex)
{
switch (newMode.Mode)
{
@@ -255,25 +237,39 @@ namespace Combot.IRCServices.Messaging
break;
case "TOPIC":
TopicChangeInfo topicMsg = new TopicChangeInfo();
topicMsg.Channel = _IRC.Channels.Find(channel => channel.Name == recipient);
if (topicMsg.Channel != null && topicMsg.Channel.Nicks != null && topicMsg.Channel.Nicks.Exists(nick => nick.Nickname == senderNick))
topicMsg.Channel = recipient;
topicMsg.Nick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
topicMsg.Topic = args.Remove(0, 1);

if (TopicChangeEvent != null)
{
topicMsg.Nick = topicMsg.Channel.Nicks.Find(nick => nick.Nickname == senderNick);
TopicChangeEvent(this, topicMsg);
}
else
break;
case "NICK":
NickChangeInfo nickMsg = new NickChangeInfo();
nickMsg.OldNick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
nickMsg.NewNick = new Nick() { Nickname = recipient.Remove(0, 1) };

if (NickChangeEvent != null)
{
topicMsg.Nick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
NickChangeEvent(this, nickMsg);
}
topicMsg.Topic = args.Remove(0, 1);
break;
case "INVITE":
InviteChannelInfo inviteMsg = new InviteChannelInfo();
inviteMsg.Requester = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
inviteMsg.Recipient = new Nick() { Nickname = recipient };
inviteMsg.Channel = args.Remove(0, 1);

if (TopicChangeEvent != null)
if (InviteChannelEvent != null)
{
TopicChangeEvent(this, topicMsg);
InviteChannelEvent(this, inviteMsg);
}
break;
case "JOIN":
JoinChannelInfo joinMsg = new JoinChannelInfo();
joinMsg.Channel = _IRC.Channels.Find(channel => channel.Name == recipient.TrimStart(':'));
joinMsg.Channel = recipient.TrimStart(':');
joinMsg.Nick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };

if (JoinChannelEvent != null)
@@ -283,7 +279,7 @@ namespace Combot.IRCServices.Messaging
break;
case "PART":
PartChannelInfo partMsg = new PartChannelInfo();
partMsg.Channel = _IRC.Channels.Find(channel => channel.Name == recipient);
partMsg.Channel = recipient;
partMsg.Nick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };

if (PartChannelEvent != null)
@@ -293,10 +289,12 @@ namespace Combot.IRCServices.Messaging
break;
case "KICK":
KickInfo kickMsg = new KickInfo();
kickMsg.Channel = _IRC.Channels.Find(channel => channel.Name == recipient);
kickMsg.Channel = recipient;
kickMsg.Nick = new Nick() { Nickname = senderNick, Realname = senderRealname, Host = senderHost };
string[] argSplit = args.Split(new char[] { ' ' }, StringSplitOptions.None);
kickMsg.KickedNick = kickMsg.Channel.Nicks.Find(nick => nick.Nickname == argSplit[0]);

kickMsg.KickedNick = new Nick() { Nickname = argSplit[0], Realname = argSplit[0], Host = argSplit[0] };

List<string> reasonArgs = argSplit.ToList<string>();
reasonArgs.RemoveAt(0);
kickMsg.Reason = string.Join(" ", reasonArgs.ToArray()).Remove(0, 1);
@@ -354,6 +352,11 @@ namespace Combot.IRCServices.Messaging
ErrorMessageEvent(this, error);
}
}

if (RawMessageEvent != null)
{
RawMessageEvent(this, message);
}
}
}


+ 44
- 13
IRCServices/Nick.cs View File

@@ -8,6 +8,7 @@ namespace Combot.IRCServices
{
public class Nick
{
public string Username { get; set; }
public string Realname { get; set; }
public string Host { get; set; }
public string Nickname { get; set; }
@@ -15,9 +16,11 @@ namespace Combot.IRCServices
public bool Identified { get; set; }
public bool Registered { get; set; }
public List<UserMode> Modes { get; set; }
public List<PrivaledgeMode> Privaledges { get; set; }

public Nick()
{
Username = string.Empty;
Realname = string.Empty;
Host = string.Empty;
Nickname = string.Empty;
@@ -25,27 +28,23 @@ namespace Combot.IRCServices
Identified = false;
Registered = false;
Modes = new List<UserMode>();
}

public Nick(string realname, string host, string nickname, string password, bool identified, bool registered, List<UserMode> modes)
{
Realname = realname;
Host = host;
Nickname = nickname;
Password = password;
Identified = identified;
Registered = registered;
Modes = modes;
Privaledges = new List<PrivaledgeMode>();
}

public void AddMode(UserMode mode)
{
Modes.Add(mode);
if (!Modes.Contains(mode))
{
Modes.Add(mode);
}
}

public void AddModes(List<UserMode> modes)
{
Modes.AddRange(modes);
foreach (UserMode mode in modes)
{
AddMode(mode);
}
}

public void RemoveMode(UserMode mode)
@@ -63,5 +62,37 @@ namespace Combot.IRCServices
RemoveMode(mode);
}
}

public void AddPrivaledge(PrivaledgeMode privaledge)
{
if (!Privaledges.Contains(privaledge))
{
Privaledges.Add(privaledge);
}
}

public void AddPrivaledges(List<PrivaledgeMode> privaledges)
{
foreach (PrivaledgeMode privaledge in privaledges)
{
AddPrivaledge(privaledge);
}
}

public void RemovePrivaledge(PrivaledgeMode privaledge)
{
if (Privaledges.Contains(privaledge))
{
Privaledges.Remove(privaledge);
}
}

public void RemovePrivaledges(List<PrivaledgeMode> privaledges)
{
foreach (PrivaledgeMode privaledge in privaledges)
{
RemovePrivaledge(privaledge);
}
}
}
}

+ 3
- 1
IRCServices/TCP/TCPInterface.cs View File

@@ -9,7 +9,7 @@ using System.Threading;

namespace Combot.IRCServices.TCP
{
class TCPInterface
internal class TCPInterface
{
internal event Action<TCPError> TCPErrorEvent;
internal event Action<int> TCPConnectionEvent;
@@ -94,6 +94,7 @@ namespace Combot.IRCServices.TCP
}
catch (IOException)
{
/*
_currentFailedCount++;
Action<TCPError> localEvent = TCPErrorEvent;
if (localEvent != null && _tcpStream.CanRead)
@@ -102,6 +103,7 @@ namespace Combot.IRCServices.TCP
error.Message = string.Format("Read Timeout, No Response from Server in {0}ms", _readTimeout);
localEvent(error);
}
*/
}
catch (Exception ex)
{

+ 3
- 3
IRCServices/TCP/TCPTypes.cs View File

@@ -6,9 +6,9 @@ using System.Threading.Tasks;

namespace Combot.IRCServices.TCP
{
internal class TCPError
public class TCPError
{
internal int Code { get; set; }
internal string Message { get; set; }
public int Code { get; set; }
public string Message { get; set; }
}
}

+ 14
- 0
IRCServices/Types.cs View File

@@ -185,4 +185,18 @@ namespace Combot.IRCServices
[Description("Server Uptime")]
u
}

public enum PrivaledgeMode
{
[Description("Voice")]
v,
[Description("Half-Operator")]
h,
[Description("Operator")]
o,
[Description("Super Operator")]
a,
[Description("Founder")]
q
}
}

+ 1
- 0
Interface/MainWindow.xaml View File

@@ -7,5 +7,6 @@
<Grid>
<Button x:Name="ToggleConnectionButton" Content="{Binding ToggleConnectionText}" HorizontalAlignment="Left" Margin="10,0,0,10" Width="156" Command="{Binding ToggleConnection}" Height="33" VerticalAlignment="Bottom"/>
<TextBox x:Name="BufferWindow" Margin="10,10,10,48" TextWrapping="Wrap" Text="{Binding CurrentBuffer, Mode=TwoWay}"/>
<Button Content="Join Channel" HorizontalAlignment="Left" Margin="171,0,0,10" Width="121" Command="{Binding JoinChannel}" Height="33" VerticalAlignment="Bottom"/>
</Grid>
</Window>

+ 18
- 4
Interface/ViewModels/MainViewModel.cs View File

@@ -25,6 +25,7 @@ namespace Interface.ViewModels
public string ToggleConnectionText { get { return _ToggleConnectionText; } set { _ToggleConnectionText = value; OnPropertyChanged("ToggleConnectionText"); } }

public DelegateCommand ToggleConnection { get; private set; }
public DelegateCommand JoinChannel { get; private set; }

public MainViewModel()
{
@@ -50,8 +51,15 @@ namespace Interface.ViewModels
Combot.IRC.Message.PrivateNoticeReceivedEvent += PrivateNoticeReceivedHandler;

Combot.IRC.DisconnectEvent += DisconnectHandler;
Combot.IRC.TCPErrorEvent += TCPErrorHandler;

ToggleConnection = new DelegateCommand(ExecuteToggleConnection, CanToggleConnection);
JoinChannel = new DelegateCommand(ExecuteJoinChannel, CanJoinChannel);
}

private void TCPErrorHandler(Combot.IRCServices.TCP.TCPError error)
{
CurrentBuffer += string.Format("[{0}] {1}", error.Code.ToString(), error.Message) + Environment.NewLine;
}

private void ServerReplyHandler(object sender, IReply reply)
@@ -109,15 +117,21 @@ namespace Interface.ViewModels
private void Connect()
{
Connected = Combot.Connect();
if (Connected)
{
Combot.Login();
}
}

private void Disconnect()
{
Connected = Combot.Disconnect();
}

private void ExecuteJoinChannel()
{
if (_Connected)
{
Combot.IRC.IRCSendJoin("#testing");
}
}

private bool CanJoinChannel() { return true; }
}
}

Loading…
Cancel
Save