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

Commit

Permalink
Upload hastebin asyncronously & prevent empty log messages (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Headline authored Sep 15, 2018
1 parent f76a345 commit 157e6fc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
52 changes: 25 additions & 27 deletions IRC-Relay/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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("```"))
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion IRC-Relay/IRC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
3 changes: 3 additions & 0 deletions IRC-Relay/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 157e6fc

Please sign in to comment.