-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added handler when we send ALMOST anything to the server.
- Loading branch information
1 parent
71975cd
commit 07b2386
Showing
9 changed files
with
594 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Net.NetworkInformation; | ||
using System.Text; | ||
using System.Xml; | ||
using System.Xml.Linq; | ||
using SethCS.Exceptions; | ||
using SethCS.Extensions; | ||
|
||
namespace Chaskis.Core | ||
{ | ||
/// <summary> | ||
/// Args that are passed into <see cref="SendEventHandler"/> when | ||
/// *any* command is sent to the server. | ||
/// </summary> | ||
public sealed class SendEventArgs : BaseCoreEventArgs | ||
{ | ||
// ---------------- Fields ---------------- | ||
|
||
internal const string XmlRootName = "chaskis_send_event"; | ||
|
||
// ---------------- Constructor ---------------- | ||
|
||
internal SendEventArgs() : | ||
base() | ||
{ | ||
this.Command = null; | ||
} | ||
|
||
// ---------------- Properties ---------------- | ||
|
||
public IIrcWriter Writer { get; internal set; } | ||
|
||
/// <summary> | ||
/// The IRC command sent to the server. | ||
/// </summary> | ||
public string Command { get; internal set; } | ||
|
||
protected override string XmlElementName => XmlRootName; | ||
|
||
protected override IEnumerable<XElement> ChildToXml() | ||
{ | ||
// It is possible for a character to not be a valid XML | ||
// character since we can send bytes with some commands (such as CTCP Ping). | ||
// So, we need to escape these characters, while still making them | ||
// readable in the log. We will do that by | ||
// replacing them with [hexValue] in the XML, then | ||
// putting them back on the otherside. | ||
bool escaped = false; | ||
StringBuilder builder = new StringBuilder(); | ||
foreach( char c in this.Command ) | ||
{ | ||
if( | ||
( XmlConvert.IsXmlChar( c ) == false ) || | ||
( c == '[' ) || | ||
( c == ']' ) | ||
) | ||
{ | ||
escaped = true; | ||
builder.Append( $"[{Convert.ToInt32( c ):X}]" ); | ||
} | ||
else | ||
{ | ||
builder.Append( c ); | ||
} | ||
} | ||
|
||
XElement commandElement = new XElement( "command", builder ); | ||
commandElement.Add( new XAttribute( "escaped", escaped ) ); | ||
return new List<XElement> | ||
{ | ||
commandElement | ||
}; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Extensions to <see cref="SendEventArgs"/> | ||
/// </summary> | ||
internal static class SendEventArgsExtensions | ||
{ | ||
public static SendEventArgs FromXml( string xmlString, IIrcWriter writer ) | ||
{ | ||
SendEventArgs args = new SendEventArgs | ||
{ | ||
Writer = writer | ||
}; | ||
|
||
XElement root = BaseCoreEventArgs.ParseXml( args, xmlString ); | ||
BaseCoreEventArgs.ParseBaseXml( args, root ); | ||
|
||
foreach( XElement child in root.Elements() ) | ||
{ | ||
if( "command".EqualsIgnoreCase( child.Name.LocalName ) ) | ||
{ | ||
// Assume true, we don't want things to crash if this | ||
// was somehow false. | ||
bool escaped = true; | ||
foreach( XAttribute attr in child.Attributes() ) | ||
{ | ||
if( "escaped".EqualsIgnoreCase( attr.Name.LocalName ) ) | ||
{ | ||
if( bool.TryParse( attr.Value, out escaped ) == false ) | ||
{ | ||
escaped = false; | ||
} | ||
} | ||
} | ||
|
||
// If we did not escape, just assign the value, | ||
// no need to over-complciate things. | ||
if( escaped == false ) | ||
{ | ||
args.Command = child.Value; | ||
} | ||
else | ||
{ | ||
args.Command = ParseCommand( child.Value ); | ||
} | ||
} | ||
} | ||
|
||
if( args.Command == null ) | ||
{ | ||
throw new ValidationException( | ||
$"Could not find all required properties when creating {nameof( SendEventArgs )}" | ||
); | ||
} | ||
|
||
return args; | ||
} | ||
|
||
private static string ParseCommand( string originalCommand ) | ||
{ | ||
StringBuilder builder = new StringBuilder(); | ||
|
||
bool parse = false; | ||
StringBuilder parser = new StringBuilder(); | ||
foreach( char c in originalCommand ) | ||
{ | ||
if( c == '[' ) | ||
{ | ||
parse = true; | ||
} | ||
else if( c == ']' ) | ||
{ | ||
parse = false; | ||
builder.Append( Convert.ToChar( int.Parse( parser.ToString(), NumberStyles.HexNumber ) ) ); | ||
parser.Clear(); | ||
} | ||
else if( parse ) | ||
{ | ||
parser.Append( c ); | ||
} | ||
else | ||
{ | ||
builder.Append( c ); | ||
} | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Chaskis.Core | ||
{ | ||
/// <summary> | ||
/// Event to configure <see cref="SendEventHandler"/> | ||
/// </summary> | ||
public sealed class SendEventConfig : | ||
BaseCoreEvent<SendEventConfig, SendHandlerAction, SendEventArgs> | ||
{ | ||
// ---------------- Constructor ---------------- | ||
|
||
public SendEventConfig() | ||
{ | ||
} | ||
|
||
// ---------------- Functions ---------------- | ||
|
||
public override SendEventConfig Clone() | ||
{ | ||
return (SendEventConfig)this.MemberwiseClone(); | ||
} | ||
|
||
protected override IEnumerable<string> ValidateChild() | ||
{ | ||
// Nothing to validate. | ||
return null; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Copyright Seth Hendrick 2020. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
using System.Text.RegularExpressions; | ||
|
||
namespace Chaskis.Core | ||
{ | ||
public delegate void SendHandlerAction( SendEventArgs args ); | ||
|
||
/// <summary> | ||
/// Event that gets fired when the bot sends ALMOST ANYTHING to the server. | ||
/// This means while you could use this to capture, say us sending a join command to the server, | ||
/// you're better off using <see cref="SendJoinEventHandler"/>. | ||
/// </summary> | ||
public sealed class SendEventHandler : BaseCoreEventHandler<SendEventConfig> | ||
{ | ||
// ---------------- Fields ---------------- | ||
|
||
public static readonly Regex Regex = new Regex( | ||
$@"^<{SendEventArgs.XmlRootName}>.+</{SendEventArgs.XmlRootName}>", | ||
RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.IgnoreCase | ||
); | ||
|
||
// ---------------- Constructor ---------------- | ||
|
||
public SendEventHandler( SendEventConfig config ) : | ||
base( config, Regex ) | ||
{ | ||
} | ||
|
||
// ---------------- Functions ---------------- | ||
|
||
protected override void HandleEventInternal( HandlerArgs args, Match match ) | ||
{ | ||
SendEventArgs connectionArgs = SendEventArgsExtensions.FromXml( args.Line, args.IrcWriter ); | ||
this.config.LineAction( connectionArgs ); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.