From 157e6fc0b6124acc3efb03b0b5726a9ef5ef0381 Mon Sep 17 00:00:00 2001 From: Michael Flaherty Date: Sat, 15 Sep 2018 16:45:11 -0700 Subject: [PATCH] Upload hastebin asyncronously & prevent empty log messages (#22) --- IRC-Relay/Discord.cs | 52 ++++++++++++++++++++--------------------- IRC-Relay/IRC.cs | 2 +- IRC-Relay/LogManager.cs | 3 +++ 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/IRC-Relay/Discord.cs b/IRC-Relay/Discord.cs index 4422a21..bea85a0 100644 --- a/IRC-Relay/Discord.cs +++ b/IRC-Relay/Discord.cs @@ -114,7 +114,7 @@ public async Task OnDiscordMessage(SocketMessage messageParam) } /* Santize discord-specific notation to human readable things */ - string formatted = CodeblockToURL(messageParam.Content, out string url); + string formatted = DoURLMessage(messageParam.Content, message); formatted = MentionToNickname(formatted, message); formatted = EmojiToName(formatted, message); formatted = ChannelMentionToName(formatted, message); @@ -168,19 +168,11 @@ await messageParam.Author.SendMessageAsync("To prevent you from having to re-typ foreach (String part in parts) // we're going to send each line indpependently instead of letting irc clients handle it. { - if (part.Replace(" ", "").Replace("\n", "").Replace("\t", "").Length != 0) // if the string is not empty or just spaces + if (part.Trim().Length != 0) // if the string is not empty or just spaces { session.SendMessage(Session.MessageDestination.IRC, part, username); } } - - if (!url.Equals("")) // hastebin upload is succesfuly if url contains any data - { - if (config.IRCLogMessages) - LogManager.WriteLog(MsgSendType.DiscordToIRC, username, url, "log.txt"); - - session.SendMessage(Session.MessageDestination.IRC, url, username); - } } public static Task Log(LogMessage msg) @@ -195,7 +187,7 @@ public void Dispose() /** Helper methods **/ - public static string CodeblockToURL(string input, out string url) + public string DoURLMessage(string input, SocketUserMessage msg) { string text = "```"; if (input.Contains("```")) @@ -205,32 +197,38 @@ public static string CodeblockToURL(string input, out string url) string code = input.Substring(start + text.Length, (end - start) - text.Length); - url = UploadMarkDown(code); + using (var client = new WebClient()) + { + client.Headers[HttpRequestHeader.ContentType] = "text/plain"; + + client.UploadDataCompleted += Client_UploadDataCompleted; + client.UploadDataAsync(new Uri("https://hastebin.com/documents"), null, Encoding.ASCII.GetBytes(input), msg); + } input = input.Remove(start, (end - start) + text.Length); } - else - { - url = ""; - } return input; } - public static string UploadMarkDown(string input) + + private void Client_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e) { - using (var client = new WebClient()) + if (e.Error != null) { - client.Headers[HttpRequestHeader.ContentType] = "text/plain"; + Log(new LogMessage(LogSeverity.Critical, "HastebinUpload", e.Error.Message)); + return; + } + JObject obj = JObject.Parse(Encoding.UTF8.GetString(e.Result)); - var response = client.UploadString("https://hastebin.com/documents", input); - JObject obj = JObject.Parse(response); + if (obj.HasValues) + { + string key = (string)obj["key"]; + string result = "https://hastebin.com/" + key + ".cs"; - if (!obj.HasValues) - { - return ""; - } + var msg = (SocketUserMessage)e.UserState; + if (config.IRCLogMessages) + LogManager.WriteLog(MsgSendType.DiscordToIRC, msg.Author.Username, result, "log.txt"); - string key = (string)obj["key"]; - return "https://hastebin.com/" + key + ".cs"; + session.SendMessage(Session.MessageDestination.IRC, result, msg.Author.Username); } } public static string MentionToNickname(string input, SocketUserMessage message) diff --git a/IRC-Relay/IRC.cs b/IRC-Relay/IRC.cs index 2fdc30c..25ada70 100644 --- a/IRC-Relay/IRC.cs +++ b/IRC-Relay/IRC.cs @@ -92,7 +92,7 @@ private void OnError(object sender, ErrorEventArgs e) { /* Create a new thread to kill the session. We cannot block * this Disconnect call */ - new System.Threading.Thread(() => { session.Kill(); }).Start(); + new Thread(() => { session.Kill(); }).Start(); Discord.Log(new LogMessage(LogSeverity.Critical, "IRCOnError", e.ErrorMessage)); } diff --git a/IRC-Relay/LogManager.cs b/IRC-Relay/LogManager.cs index 650fc2c..58bcf11 100644 --- a/IRC-Relay/LogManager.cs +++ b/IRC-Relay/LogManager.cs @@ -31,6 +31,9 @@ public class LogManager { public static void WriteLog(MsgSendType type, string name, string message, string filename) { + if (message.Trim().Length == 0) + return; + string prefix; if (type == MsgSendType.DiscordToIRC) {