package org.pileus.spades;
+import java.util.Date;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Message
{
- /* Constnats */
- private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))";
+ /* Enumerations */
+ enum Type {
+ OTHER, // Unknown message type
+ JOIN, // Join channel
+ PART, // Leave channel
+ PRIVMSG, // Private message
+ TOPIC, // Display current topic
+ NAMES, // Display user names
+ ERROR, // Error message from server
+ CAP, // Server capabilities
+ AUTH, // Authentication message
+ AUTHOK, // Authentication succeeded
+ AUTHFAIL, // Authentication failed
+ };
+
+ enum How {
+ OTHER, // Unknown message type
+ CHANNEL, // Normal message to a channel
+ MENTION, // User was mentioned in message text
+ DIRECT, // Message directed towards user
+ PRIVMSG, // Private message to user only
+ SENT // Message was sent by the user
+ };
+
+ /* Constants */
+ private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
private final String reFrom = "([^! ]+)!.*";
private final String reTo = "(([^ :,]*)[:,] *)?(.*)";
+ private final String reCmd = "/([a-z]+)( +(.*))?";
private static Pattern ptMsg = null;
private static Pattern ptFrom = null;
private static Pattern ptTo = null;
+ private static Pattern ptCmd = null;
/* Public data */
+ public Date time = null;
+
public String line = "";
public String src = "";
public String arg = "";
public String msg = "";
+ public Type type = Type.OTHER;
+ public How how = How.OTHER;
public String from = "";
public String to = "";
public String txt = "";
/* Public Methods */
public Message(String dst, String from, String msg)
{
- this.cmd = "PRIVMSG";
- this.dst = dst;
+ this.time = new Date();
+ this.how = How.SENT;
this.from = from;
- this.msg = msg;
- this.line = this.cmd + " " + this.dst + " :" + this.msg;
+
+ if (msg.charAt(0) == '/') {
+ if (ptCmd == null)
+ ptCmd = Pattern.compile(reCmd);
+ Matcher mr = ptCmd.matcher(msg);
+ if (!mr.matches())
+ return;
+
+ String cmd = notnull(mr.group(1));
+ String arg = notnull(mr.group(3));
+
+ if (cmd.matches("join")) {
+ Os.debug("Message: /join");
+ this.type = Type.JOIN;
+ this.cmd = "JOIN";
+ this.msg = arg;
+ this.line = this.cmd + " :" + arg;
+ }
+ if (cmd.matches("part")) {
+ Os.debug("Message: /part");
+ this.type = Type.PART;
+ this.cmd = "PART";
+ this.msg = arg;
+ this.line = this.cmd + " :" + arg;
+ }
+ if (this.line == null) {
+ Os.debug("Message: unknown command");
+ }
+ } else {
+ this.type = Type.PRIVMSG;
+ this.cmd = "PRIVMSG";
+ this.dst = dst;
+ this.msg = msg;
+ this.line = this.cmd + " " + this.dst + " :" + this.msg;
+ }
}
- public Message(String line)
+ public Message(String line, String name)
{
+ // Initialize regexes
if (ptMsg == null) ptMsg = Pattern.compile(reMsg);
if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
if (ptTo == null) ptTo = Pattern.compile(reTo);
+ // Set time stamp
+ this.time = new Date();
+
+ // Cleanup line
line = line.replaceAll("\\s+", " ");
line = line.replaceAll("^ | $", "");
- line = line.replaceAll("\003[0-9]*", "");
this.line = line;
+ // Split line into parts
Matcher mrMsg = ptMsg.matcher(line);
if (mrMsg.matches()) {
this.src = notnull(mrMsg.group(2));
this.msg = notnull(mrMsg.group(10));
}
+ // Determine friendly parts
Matcher mrFrom = ptFrom.matcher(this.src);
if (mrFrom.matches())
this.from = notnull(mrFrom.group(1));
else
this.txt = notnull(mrTo.group(3));
- this.debug();
+ // Parse commands names
+ if (this.cmd.equals("PRIVMSG")) this.type = Type.PRIVMSG;
+ else if (this.cmd.equals("332")) this.type = Type.TOPIC;
+ else if (this.cmd.equals("353")) this.type = Type.NAMES;
+ else if (this.cmd.equals("ERROR")) this.type = Type.ERROR;
+ else if (this.cmd.equals("CAP")) this.type = Type.CAP;
+ else if (this.cmd.equals("AUTHENTICATE")) this.type = Type.AUTH;
+ else if (this.cmd.equals("903")) this.type = Type.AUTHOK;
+ else if (this.cmd.equals("904")) this.type = Type.AUTHFAIL;
+ else if (this.cmd.equals("905")) this.type = Type.AUTHFAIL;
+ else if (this.cmd.equals("906")) this.type = Type.AUTHFAIL;
+ else if (this.cmd.equals("907")) this.type = Type.AUTHFAIL;
+
+ // Set directed
+ if (this.dst.equals(name)) this.how = How.PRIVMSG;
+ else if (this.to.equals(name)) this.how = How.DIRECT;
+ else if (this.msg.contains(name)) this.how = How.MENTION;
+ else if (this.type == Type.PRIVMSG) this.how = How.CHANNEL;
}
public void debug()