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

Commit

Permalink
Prevent codeblock multi-lines from uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
Headline committed Oct 16, 2018
1 parent 1b2c498 commit 9326a15
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions IRC-Relay/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,34 @@ public async Task OnDiscordMessage(SocketMessage messageParam)
}

/* Santize discord-specific notation to human readable things */
string formatted = await DoURLMessage(messageParam.Content, message);
string formatted = RemoveCodeblock(messageParam.Content, message, out string code);
formatted = MentionToNickname(formatted, message);
formatted = EmojiToName(formatted, message);
formatted = ChannelMentionToName(formatted, message);
formatted = Unescape(formatted);

string[] parts = formatted.Split('\n');
if (parts.Length > 3) // don't spam IRC, please.
{
await messageParam.Channel.SendMessageAsync(messageParam.Author.Mention + ": Too many lines! If you're meaning to post" +
" code blocks, please use \\`\\`\\` to open & close the codeblock." +
"\nYour message has been deleted and was not relayed to IRC. Please try again.");
await messageParam.DeleteAsync();

await messageParam.Author.SendMessageAsync("To prevent you from having to re-type your message,"
+ " here's what you tried to send: \n ```"
+ messageParam.Content
+ "```");

return;
}

if (Program.HasMember(config, "StikkedCreateUrlAndKey") && config.StikkedCreateUrlAndKey.Length > 0)
await DoStikkedUpload(code, message);
else
DoHastebinUpload(code, message);


if (Program.HasMember(config, "SpamFilter")) //bcompat for older configurations
{
foreach (string badstr in config.SpamFilter)
Expand All @@ -151,22 +173,6 @@ public async Task OnDiscordMessage(SocketMessage messageParam)
return;
}

string[] parts = formatted.Split('\n');
if (parts.Length > 3) // don't spam IRC, please.
{
await messageParam.Channel.SendMessageAsync(messageParam.Author.Mention + ": Too many lines! If you're meaning to post" +
" code blocks, please use \\`\\`\\` to open & close the codeblock." +
"\nYour message has been deleted and was not relayed to IRC. Please try again.");
await messageParam.DeleteAsync();

await messageParam.Author.SendMessageAsync("To prevent you from having to re-type your message,"
+ " here's what you tried to send: \n ```"
+ messageParam.Content
+ "```");

return;
}

string username = (messageParam.Author as SocketGuildUser)?.Nickname ?? message.Author.Username;
if (config.IRCLogMessages)
LogManager.WriteLog(MsgSendType.DiscordToIRC, username, formatted, "log.txt");
Expand Down Expand Up @@ -200,21 +206,17 @@ public void Dispose()

/** Helper methods **/

public async Task<string> DoURLMessage(string input, SocketUserMessage msg)
public string RemoveCodeblock(string input, SocketUserMessage msg, out string codeout)
{
codeout = "";
string text = "```";
if (input.Contains("```"))
{
int start = input.IndexOf(text, StringComparison.CurrentCulture);
int end = input.IndexOf(text, start + text.Length, StringComparison.CurrentCulture);

string code = input.Substring(start + text.Length, (end - start) - text.Length);

if (Program.HasMember(config, "StikkedCreateUrlAndKey") && config.StikkedCreateUrlAndKey.Length > 0)
await DoStikkedUpload(code, msg);
else
DoHastebinUpload(code, msg);

codeout = code; //await DoStikkedUpload(code, msg);
input = input.Remove(start, (end - start) + text.Length);
}
return input;
Expand Down Expand Up @@ -246,11 +248,6 @@ private async Task DoStikkedUpload(string input, SocketUserMessage msg)
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(config.StikkedCreateUrlAndKey, content); // config.StikkedCreateUrlAndKey
var url = await response.Content.ReadAsStringAsync();

if (config.IRCLogMessages)
LogManager.WriteLog(MsgSendType.DiscordToIRC, username, url, "log.txt");

session.SendMessage(Session.TargetBot.IRC, url, username);
}
}

Expand Down

0 comments on commit 9326a15

Please sign in to comment.