Created
August 16, 2016 15:58
-
-
Save jmurth1234/125397a2c9fd11027754bc29d2e82806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rymate.irc.parser; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* Port of the WinIRC parser to Java | |
* | |
* Created by rymate1234 on 15/08/2016. | |
*/ | |
public class IrcMessage { | |
private final Prefix prefix; | |
private final Trailing trailing; | |
private final Command command; | |
private String originalMessage; | |
private String serverTime; | |
public IrcMessage(String message) { | |
// remove colours first | |
String coloursRegex = "[\\x02\\x1f\\x16\\x0f]|\\x03\\d{0,2}(?:,\\d{0,2})?"; | |
message = message.replaceFirst(coloursRegex, ""); | |
List<String> lineSplit = Arrays.asList(message.split(" ")); | |
if (message.startsWith("@")) { | |
String timeVar = lineSplit.get(0); | |
serverTime = timeVar.replace("@time=", ""); | |
lineSplit.remove(0); | |
} | |
message = String.join(" ", lineSplit); | |
this.originalMessage = message; | |
this.prefix = new Prefix(message); | |
this.trailing = new Trailing(message); | |
this.command = new Command(message, this.getPrefix(), this.getTrailing()); | |
} | |
public String getOriginalMessage() { | |
return originalMessage; | |
} | |
public String getServerTime() { | |
return serverTime; | |
} | |
public Prefix getPrefix() { | |
return prefix; | |
} | |
public Trailing getTrailing() { | |
return trailing; | |
} | |
public Command getCommand() { | |
return command; | |
} | |
public class Prefix { | |
/** | |
* The original message | |
*/ | |
private String originalMessage; | |
private String prefix; | |
private String nickname; | |
private String username; | |
private String hostname; | |
private boolean user; | |
private int index; | |
/** | |
* Initializes a new instance of the | |
* {@link #Prefix} | |
* class. | |
* | |
* @param message The message. | |
*/ | |
public Prefix(String message) { | |
this.originalMessage = message; | |
if (!this.getIsPrefixed()) { | |
return; | |
} | |
index = message.indexOf(" "); | |
this.setPrefix(message.substring(1, index - 1)); | |
String[] userHostArray = this.getPrefix().split("(!)|(@)"); | |
if (userHostArray.length > 1) { | |
this.user = true; | |
this.nickname = userHostArray[0]; | |
this.username = userHostArray[1]; | |
this.hostname = userHostArray[2]; | |
} | |
} | |
public boolean getIsPrefixed() { | |
return this.originalMessage.startsWith(":"); | |
} | |
public String getPrefix() { | |
return prefix; | |
} | |
public void setPrefix(String prefix) { | |
this.prefix = prefix; | |
} | |
public String getNickname() { | |
return nickname; | |
} | |
public void setNickname(String nickname) { | |
this.nickname = nickname; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getHostname() { | |
return hostname; | |
} | |
public void setHostname(String hostname) { | |
this.hostname = hostname; | |
} | |
public void setUser(boolean user) { | |
this.user = user; | |
} | |
public boolean isUser() { | |
return user; | |
} | |
public int getIndex() { | |
return index; | |
} | |
} | |
public class Trailing { | |
private final boolean hasTrail; | |
private int trailIndex; | |
private String content; | |
public Trailing(String message) { | |
String regex = "[\\x00-\\x07\\x0B\\x0C\\x0E-\\x1F\\x26]"; | |
message = message.replaceAll(regex, ""); | |
trailIndex = message.indexOf(" :"); | |
if (trailIndex > 0) { | |
this.content = message.substring(trailIndex + 2); | |
} else { | |
trailIndex = message.length(); | |
} | |
this.hasTrail = (trailIndex > 0 && trailIndex < message.length()); | |
} | |
public String getContent() { | |
return content; | |
} | |
public boolean isHasTrail() { | |
return hasTrail; | |
} | |
public int getTrailIndex() { | |
return trailIndex; | |
} | |
} | |
public class Command { | |
private ArrayList<String> parameters; | |
private String command; | |
public Command(String message, Prefix prefix, Trailing trailing) { | |
int endIndex = Math.min(prefix.getIndex() + 1 + trailing.getTrailIndex() - prefix.getIndex() - 1, message.length()); | |
String[] commandAndParameters = message.substring(prefix.getIndex() + 1, endIndex).split(" "); | |
this.command = commandAndParameters[0]; | |
// If the command as args, then they compose the rest of the collection. | |
if (commandAndParameters.length > 1) { | |
this.parameters = new ArrayList<String>(Arrays.asList(commandAndParameters)); | |
this.parameters.remove(1); | |
} | |
} | |
public String getCommand() { | |
return command; | |
} | |
public boolean is(String other) { | |
return command.equals(other); | |
} | |
public List<String> getParameters() { | |
return parameters; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment