TheRaven/IRCServerMessage.csharp
2018-07-27 14:01:22 -05:00

66 lines
2.0 KiB
Plaintext

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using AniNIX.Shared;
namespace AniNIX.TheRaven {
//IRC messages are a primitive data type for us to use.
public class IRCServerMessage : IRCMessage {
public string user { get; protected set; }
public string server { get; protected set; }
public string msgCode { get; protected set; }
public string target { get; protected set; }
public string message { get; protected set; }
// Adding whitelist for default Discord-Bridge config
private string _discordRegex = "\\<.+\\>\\s+";
public IRCServerMessage(String serverString) {
incomingIRCString = serverString.Trim();
try {
String[] byColon = incomingIRCString.Split(':');
user = byColon[0];
String[] bySpace = byColon[1].Split(' ');
List<String> messageL = new List<String>(byColon);
messageL.RemoveAt(0);
messageL.RemoveAt(0);
message = String.Join(":",messageL.ToArray());
message = Regex.Replace(message,_discordRegex,"");
if (bySpace[0].Contains("!")) {
String[] byExclamation = bySpace[0].Split('!');
user = byExclamation[0];
server = byExclamation[1];
} else {
user = null;
server = bySpace[0];
}
msgCode = bySpace[1];
target = bySpace[2];
} catch (IndexOutOfRangeException e) {
ReportMessage.Log(Verbosity.Error,String.Format("!!! Can't translate string:\n{0}",serverString,e.ToString()));
user = null;
server = null;
msgCode = null;
target = null;
message = null;
}
}
// There is no outgoing string.
public new string GetOutgoingIRCString() {
return null;
}
public override string ToString() {
if (ReportMessage.verbosity == Verbosity.Explicit) {
return String.Format(">>> {0}\nUser: {1}\nServer: {2}\nmsgCode: {3}\nTarget: {4}\nMessage: {5}\n",incomingIRCString,user,server,msgCode,target,message);
} else {
return String.Format(">>> {0}",incomingIRCString);
}
}
}
}