1
0
mirror of https://github.com/deavmi/birchwood synced 2024-09-20 13:43:19 +02:00
- Implemented `hasIllegalCharacters(string)` which checks whether the provided input string contains any invalid characters
- Updated `encode()` to `encode(ChecksMode)`
This commit is contained in:
Tristan B. Velloza Kildaire 2023-06-21 14:22:48 +02:00
parent 06efa23c85
commit 67520c2574

View File

@ -9,6 +9,9 @@ import std.string;
import std.conv : to, ConvException; import std.conv : to, ConvException;
import birchwood.protocol.constants : ReplyType; import birchwood.protocol.constants : ReplyType;
import birchwood.client.exceptions;
import birchwood.config.conninfo : ChecksMode;
// TODO: Before release we should remove this import // TODO: Before release we should remove this import
import std.stdio : writeln; import std.stdio : writeln;
@ -154,17 +157,83 @@ public final class Message
* any invalid characters will be stripped prior * any invalid characters will be stripped prior
* to encoding * to encoding
* *
* Params:
* mode = the `ChecksMode` to use
*
* Throws: * Throws:
* `BirchwoodException` if `ChecksMode` is set to * `BirchwoodException` if `ChecksMode` is set to
* `HARDCORE` and invalid characters are present * `HARDCORE` and invalid characters are present
* Returns: * Returns: the encoded format
*/ */
public string encode() public string encode(ChecksMode mode)
{ {
string fullLine = from~" "~command~" "~params; string fullLine;
/**
* Copy over the values (they might be updated and we
* want to leave the originals intact)
*/
string fFrom = from, fCommand = command, fParams = params;
/**
* If in `HARDCORE` mode then and illegal characters
* are present, throw an exception
*/
if(mode == ChecksMode.HARDCORE && (
hic(fFrom) ||
hic(fCommand) ||
hic(fParams)
))
{
throw new BirchwoodException(ErrorType.ILLEGAL_CHARACTERS, "Invalid characters present");
}
/**
* If in `EASY` mode and illegal characters have
* been found, then fix them up
*/
else
{
// Strip illegal characters from all
fFrom = sic(fFrom);
fCommand = sic(fCommand);
fParams = sic(fParams);
}
/* Combine */
fullLine = fFrom~" "~fCommand~" "~fParams;
return fullLine; return fullLine;
} }
// TODO: comemnt
private alias sic = stripIllegalCharacters;
// TODO: comemnt
private alias hic = hasIllegalCharacters;
/**
* Checks whether the provided input string contains
* any invalid characters
*
* Params:
* input = the string to check
* Returns: `true` if so, `false` otherwise
*/
// TODO: Add unittest
private static bool hasIllegalCharacters(string input)
{
foreach(char character; input)
{
if(character == '\n' || character == '\r')
{
return true;
}
}
return false;
}
/** /**
* Provided an input string this will strip any illegal * Provided an input string this will strip any illegal
* characters present within it * characters present within it
@ -173,6 +242,7 @@ public final class Message
* input = the string to filter * input = the string to filter
* Returns: the filtered string * Returns: the filtered string
*/ */
// TODO: Add unittest
private static string stripIllegalCharacters(string input) private static string stripIllegalCharacters(string input)
{ {
string stripped; string stripped;