Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

Commit

Permalink
Prevent escpaing in discord code ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
Headline committed Aug 17, 2018
1 parent 470d2d8 commit f0043d5
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion IRC-Relay/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;

using Discord.WebSocket;
using System.Text;

namespace IRCRelay
{
Expand Down Expand Up @@ -64,7 +65,54 @@ public static string MentionToUsername(string input, SocketUserMessage message)

public static string Unescape(string input)
{
return Regex.Replace(input, @"\\([^A-Za-z0-9])", "$1");
/* Main StringBuilder for messages that aren't in '`' */
StringBuilder sb = new StringBuilder();

/*
* locations - List of indices where the first '`' lies
* peices - List of strings which live inbetween the '`'s
*/
List<int> locations = new List<int>();
List<StringBuilder> peices = new List<StringBuilder>();
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '`') // we hit a '`'
{
int j;

StringBuilder slice = new StringBuilder(); // used for capturing the str inbetween '`'
slice.Append('`'); // append the '`' for insertion later

/* we'll loop from here until we encounter the next '`',
* appending as we go.
*/
for (j = i+1; j < input.Length && input[j] != '`'; j++) {
slice.Append(input[j]);
}

slice.Append('`'); // append the '`' for insertion later

locations.Add(i); // push the index of the first '`'
peices.Add(slice); // push the captured string

i = j; // advance the outer loop to where our inner one stopped
}
else // we didn't hit a '`', so just append :)
{
sb.Append(input[i]);
}
}

// From here we prep the return string by doing our regex on the input that's not in '`'
string retstr = Regex.Replace(sb.ToString(), @"\\([^A-Za-z0-9])", "$1");

// Now we'll just loop the peices, inserting @ the locations we saved earlier
for (int i = 0; i < peices.Count; i++)
{
retstr = retstr.Insert(locations[i], peices[i].ToString());
}

return retstr; // thank fuck we're done
}

public static string ChannelMentionToName(string input, SocketUserMessage message)
Expand Down

0 comments on commit f0043d5

Please sign in to comment.