Browse Source

Moved message delay to TCP send to account for all tcp messages.

Added support in SendMode to group multiple modes together (max of 4).
tags/3.1.0
Teknikode 4 years ago
parent
commit
8c388b2f76

+ 26
- 17
IRCServices/Commanding/Commands.cs View File

@@ -51,15 +51,11 @@ namespace Combot.IRCServices.Commanding

private IRC _IRC;
private int MaxMessageLength;
private int MessageSendDelay;
private DateTime LastMessageSend;

public Commands(IRC irc, int maxMessageLength, int messageSendDelay)
{
_IRC = irc;
LastMessageSend = DateTime.Now;
MaxMessageLength = maxMessageLength;
MessageSendDelay = messageSendDelay;
}

/// <summary>
@@ -69,12 +65,6 @@ namespace Combot.IRCServices.Commanding
/// <param name="message"></param>
public void SendPrivateMessage(string recipient, string message)
{
TimeSpan sinceLastMessage = (DateTime.Now - LastMessageSend);
if (sinceLastMessage.TotalMilliseconds < MessageSendDelay)
{
Thread.Sleep((int)(MessageSendDelay - sinceLastMessage.TotalMilliseconds));
}
LastMessageSend = DateTime.Now;
if (message.Length > MaxMessageLength)
{
List<string> splitMessage = message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
@@ -131,12 +121,6 @@ namespace Combot.IRCServices.Commanding
/// <param name="message"></param>
public void SendNotice(string recipient, string message)
{
TimeSpan sinceLastMessage = (DateTime.Now - LastMessageSend);
if (sinceLastMessage.TotalMilliseconds < MessageSendDelay)
{
Thread.Sleep((int)(MessageSendDelay - sinceLastMessage.TotalMilliseconds));
}
LastMessageSend = DateTime.Now;
if (message.Length > MaxMessageLength)
{
List<string> splitMessage = message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
@@ -413,10 +397,35 @@ namespace Combot.IRCServices.Commanding

public void SendMode(string channel, List<ChannelModeInfo> modeInfos)
{
string modeList = string.Empty;
string setList = string.Empty;
bool lastSet = true;
int modeIndex = 1;
foreach (ChannelModeInfo modeInfo in modeInfos)
{
SendMode(channel, modeInfo);
if ((setList.Length + modeList.Length + channel.Length + modeInfo.Parameter.Length + 8 + ((modeInfo.Set != lastSet) ? 1 : 0)) > MaxMessageLength || modeIndex > 4)
{
_IRC.SendTCPMessage(string.Format("MODE {0} {1} {2}", channel, setList, modeList));
setList = string.Empty;
modeList = string.Empty;
lastSet = true;
modeIndex = 1;
}
if (modeInfo.Set != lastSet)
setList += modeInfo.Set ? "+" : "-";

setList += modeInfo.Mode;
modeList += modeInfo.Parameter + " ";

lastSet = modeInfo.Set;
modeIndex++;
if (ChannelModeCommandEvent != null)
{
ChannelModeCommandEvent(this, new ChannelModeCommand { Channel = channel, Mode = modeInfo });
}
}
if (!string.IsNullOrEmpty(setList) && !string.IsNullOrEmpty(modeList))
_IRC.SendTCPMessage(string.Format("MODE {0} {1} {2}", channel, setList, modeList));
}

public void SendMode(string nick, UserModeInfo modeInfo)

+ 30
- 19
IRCServices/IRC.cs View File

@@ -27,35 +27,40 @@ namespace Combot.IRCServices

private int ReadTimeout;
private int AllowedFailedReads;
private int MessageSendDelay;
private Thread TCPReader;
private Thread KeepAlive;
private DateTime LastMessageSend;
private event Action<string> TCPMessageEvent;
private readonly TCPInterface _TCP;
private readonly ReaderWriterLockSlim ChannelRWLock;

public IRC(int maxMessageLength, int messageSendDelay = 0, int readTimeout = 5000, int allowedFailedReads = 0)
{
_TCP = new TCPInterface();
Nickname = string.Empty;
ChannelRWLock = new ReaderWriterLockSlim();
ReadTimeout = readTimeout;
AllowedFailedReads = allowedFailedReads;
LastMessageSend = DateTime.Now;
MessageSendDelay = messageSendDelay;

_TCP = new TCPInterface();
Message = new Messages(this);
Command = new Commands(this, maxMessageLength, messageSendDelay);
Nickname = string.Empty;
ChannelRWLock = new ReaderWriterLockSlim();
ReadTimeout = readTimeout;
AllowedFailedReads = allowedFailedReads;

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;

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;
}

/// <summary>
@@ -263,6 +268,12 @@ namespace Combot.IRCServices
{
if (_TCP.Connected)
{
TimeSpan sinceLastMessage = (DateTime.Now - LastMessageSend);
if (sinceLastMessage.TotalMilliseconds < MessageSendDelay)
{
Thread.Sleep((int)(MessageSendDelay - sinceLastMessage.TotalMilliseconds));
}
LastMessageSend = DateTime.Now;
string replaceWith = string.Empty;
string parsedMessage = message.Replace("\r\n", replaceWith).Replace("\n", replaceWith).Replace("\r", replaceWith);
_TCP.Write(parsedMessage);

+ 17
- 1
Modules/Moderation/Moderation.cs View File

@@ -216,6 +216,20 @@ namespace Combot.Modules.Plugins
Bot.IRC.Command.SendMode(channel, modeInfo);
}

private void SetMode(bool set, string channel, ChannelMode mode, List<string> nicknames)
{
List<ChannelModeInfo> modeInfos = new List<ChannelModeInfo>();
foreach (var nickname in nicknames)
{
ChannelModeInfo modeInfo = new ChannelModeInfo();
modeInfo.Mode = mode;
modeInfo.Parameter = nickname;
modeInfo.Set = set;
modeInfos.Add(modeInfo);
}
Bot.IRC.Command.SendMode(channel, modeInfos);
}

private void ModifyChannelTopic(Command curCommand, CommandMessage command)
{
string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
@@ -262,6 +276,7 @@ namespace Combot.Modules.Plugins

if (results.Any())
{
List<string> banMasks = new List<string>();
foreach (Dictionary<string, object> result in results)
{
var nickname = result["nickname"].ToString();
@@ -283,8 +298,9 @@ namespace Combot.Modules.Plugins
{
banMask = string.Format("{0}!*@*", nickname);
}
SetMode(set, channel, ChannelMode.b, banMask);
banMasks.Add(banMask);
}
SetMode(set, channel, ChannelMode.b, banMasks);
}
else
{

+ 17
- 1
Modules/Spam Control/Spam_Control.cs View File

@@ -254,6 +254,7 @@ namespace Combot.Modules.Plugins

if (results.Any())
{
List<string> banMasks = new List<string>();
foreach (Dictionary<string, object> result in results)
{
var nickname = result["nickname"].ToString();
@@ -275,8 +276,9 @@ namespace Combot.Modules.Plugins
{
banMask = string.Format("{0}!*@*", nickname);
}
SetMode(set, message.Channel, ChannelMode.b, banMask);
banMasks.Add(banMask);
}
SetMode(set, message.Channel, ChannelMode.b, banMasks);
}
else
{
@@ -293,5 +295,19 @@ namespace Combot.Modules.Plugins
modeInfo.Set = set;
Bot.IRC.Command.SendMode(channel, modeInfo);
}

private void SetMode(bool set, string channel, ChannelMode mode, List<string> nicknames)
{
List<ChannelModeInfo> modeInfos = new List<ChannelModeInfo>();
foreach (var nickname in nicknames)
{
ChannelModeInfo modeInfo = new ChannelModeInfo();
modeInfo.Mode = mode;
modeInfo.Parameter = nickname;
modeInfo.Set = set;
modeInfos.Add(modeInfo);
}
Bot.IRC.Command.SendMode(channel, modeInfos);
}
}
}

Loading…
Cancel
Save