Skip to content

Commit

Permalink
Merge pull request #32 from deavmi/bugfix/stripper
Browse files Browse the repository at this point in the history
Bugfix/stripper
  • Loading branch information
deavmi committed Jul 6, 2023
2 parents 98e5866 + f2b0c34 commit 624db02
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 22 deletions.
82 changes: 63 additions & 19 deletions source/birchwood/client/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public class Client : Thread
public void nick(string nickname)
{
/* Ensure no illegal characters in nick name */
if(isValidText(nickname))
if(textPass(nickname))
{
// TODO: We could investigate this later if we want to be safer
ulong maxNickLen = connInfo.getDB!(ulong)("MAXNICKLEN");
Expand Down Expand Up @@ -276,7 +276,7 @@ public class Client : Thread
public void joinChannel(string channel)
{
/* Ensure no illegal characters in channel name */
if(isValidText(channel))
if(textPass(channel))
{
/* Channel name must start with a `#` */
if(channel[0] == '#')
Expand All @@ -296,6 +296,46 @@ public class Client : Thread
}
}


/**
* Provided with a reference to a string
* this will check to see if it contains
* any illegal characters and then if so
* it will strip them if the `ChecksMode`
* is set to `EASY` (and return `true`)
* else it will return `false` if set to
* `HARDCORE` whilst illegal characters
* are present.
*
* Params:
* text = the ref'd `string`
* Returns: `true` if validated, `false`
* otherwise
*/
private bool textPass(ref string text)
{
/* If there are any invalid characters */
if(Message.hasIllegalCharacters(text))
{
import birchwood.config.conninfo : ChecksMode;
if(connInfo.getMode() == ChecksMode.EASY)
{
// Filter the text and update it in-place
text = Message.stripIllegalCharacters(text);
return true;
}
else
{
return false;
}
}
/* If there are no invalid characters prewsent */
else
{
return true;
}
}

/**
* Joins the requested channels
*
Expand All @@ -319,7 +359,7 @@ public class Client : Thread
string channelLine = channels[0];

/* Ensure valid characters in first channel */
if(isValidText(channelLine))
if(textPass(channelLine))
{
//TODO: Add check for #

Expand All @@ -331,7 +371,7 @@ public class Client : Thread
string currentChannel = channels[i];

/* Ensure the character channel is valid */
if(isValidText(currentChannel))
if(textPass(currentChannel))
{
//TODO: Add check for #

Expand Down Expand Up @@ -391,7 +431,7 @@ public class Client : Thread
string channelLine = channels[0];

/* Ensure valid characters in first channel */
if(isValidText(channelLine))
if(textPass(channelLine))
{
//TODO: Add check for #

Expand All @@ -403,7 +443,7 @@ public class Client : Thread
string currentChannel = channels[i];

/* Ensure the character channel is valid */
if(isValidText(currentChannel))
if(textPass(currentChannel))
{
//TODO: Add check for #

Expand Down Expand Up @@ -450,7 +490,7 @@ public class Client : Thread
public void leaveChannel(string channel)
{
/* Ensure the channel name contains only valid characters */
if(isValidText(channel))
if(textPass(channel))
{
/* Leave the channel */
Message leaveMessage = new Message("", "PART", channel);
Expand Down Expand Up @@ -485,12 +525,12 @@ public class Client : Thread
else if(recipients.length > 1)
{
/* Ensure message is valid */
if(isValidText(message))
if(textPass(message))
{
string recipientLine = recipients[0];

/* Ensure valid characters in first recipient */
if(isValidText(recipientLine))
if(textPass(recipientLine))
{
/* Append on a trailing `,` */
recipientLine ~= ",";
Expand All @@ -500,7 +540,7 @@ public class Client : Thread
string currentRecipient = recipients[i];

/* Ensure valid characters in the current recipient */
if(isValidText(currentRecipient))
if(textPass(currentRecipient))
{
if(i == recipients.length-1)
{
Expand Down Expand Up @@ -551,7 +591,7 @@ public class Client : Thread
public void directMessage(string message, string recipient)
{
/* Ensure the message and recipient are valid text */
if(isValidText(message) && isValidText(recipient))
if(textPass(message) && textPass(recipient))
{
/* Ensure the recipient does NOT start with a # (as that is reserved for channels) */
if(recipient[0] != '#')
Expand Down Expand Up @@ -592,12 +632,12 @@ public class Client : Thread
else if(channels.length > 1)
{
/* Ensure message is valid */
if(isValidText(message))
if(textPass(message))
{
string channelLine = channels[0];

/* Ensure valid characters in first channel */
if(isValidText(channelLine))
if(textPass(channelLine))
{
/* Append on a trailing `,` */
channelLine ~= ",";
Expand All @@ -607,7 +647,7 @@ public class Client : Thread
string currentChannel = channels[i];

/* Ensure valid characters in current channel */
if(isValidText(currentChannel))
if(textPass(currentChannel))
{
if(i == channels.length-1)
{
Expand Down Expand Up @@ -659,7 +699,7 @@ public class Client : Thread
{
//TODO: Add check on recipient
//TODO: Add emptiness check
if(isValidText(message) && isValidText(channel))
if(textPass(message) && textPass(channel))
{
if(channel[0] == '#')
{
Expand Down Expand Up @@ -917,7 +957,7 @@ public class Client : Thread
{
// TODO: Implement me properly with all required checks

if(isValidText(username) && isValidText(hostname) && isValidText(servername) && isValidText(realname))
if(textPass(username) && textPass(hostname) && textPass(servername) && textPass(realname))
{
/* User message */
Message userMessage = new Message("", "USER", username~" "~hostname~" "~servername~" "~":"~realname);
Expand Down Expand Up @@ -946,18 +986,22 @@ public class Client : Thread
* Sends a message to the server by enqueuing it on
* the client-side send queue.
*
* Any invalid characters will be stripped prior
* to encoding IF `ChecksMode` is set to `EASY` (default)
*
* Params:
* message = the message to send
* Throws:
* `BirchwoodException` if the message's length
* exceeds 512 bytes
* A `BirchwoodException` is thrown if the messages
* final length exceeds 512 bytes of if `ChecksMode`
* is set to `HARDCORE`
*/
private void sendMessage(Message message)
{
// TODO: Do message splits here

/* Encode the message */
ubyte[] encodedMessage = encodeMessage(message.encode());
ubyte[] encodedMessage = encodeMessage(message.encode(connInfo.getMode()));

/* If the message is 512 bytes or less then send */
if(encodedMessage.length <= 512)
Expand Down
36 changes: 36 additions & 0 deletions source/birchwood/config/conninfo.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ import std.socket : SocketException, Address, getAddress;
import birchwood.client.exceptions;
import std.conv : to, ConvException;

/**
* The mode describes how birchwood will act
* when encounterin invalid characters that
* were provided BY the user TO birchwood
*/
public enum ChecksMode
{
/**
* In this mode any invalid characters
* will be automatically stripped
*/
EASY,

/**
* In this mode any invalid characters
* will result in the throwing of a
* `BirchwoodException`
*/
HARDCORE
}

/**
* Represents the connection details for a server
* to connect to
Expand Down Expand Up @@ -59,6 +80,8 @@ public shared struct ConnectionInfo

/* TODO: before publishing change this bulk size */

private ChecksMode mode;

/**
* Constructs a new ConnectionInfo instance with the
* provided details
Expand All @@ -81,6 +104,19 @@ public shared struct ConnectionInfo

// Set the default fakelag to 1
this.fakeLag = 1;

// Set the validity mode to easy
this.mode = ChecksMode.EASY;
}

public ChecksMode getMode()
{
return this.mode;
}

public void setMode(ChecksMode mode)
{
this.mode = mode;
}

/**
Expand Down
Loading

0 comments on commit 624db02

Please sign in to comment.