1 package org.pileus.spades;
4 import java.util.regex.Pattern;
5 import java.util.regex.Matcher;
11 OTHER, // Unknown message type
12 PRIVMSG, // Private message
13 TOPIC, // Display current topic
14 NAMES, // Display user names
15 ERROR, // Error message from server
16 CAP, // Server capabilities
17 AUTH, // Authentication message
18 AUTHOK, // Authentication succeeded
19 AUTHFAIL, // Authentication failed
23 OTHER, // Unknown message type
24 CHANNEL, // Normal message to a channel
25 MENTION, // User was mentioned in message text
26 DIRECT, // Message directed towards user
27 PRIVMSG, // Private message to user only
28 SENT // Message was sent by the user
32 private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
33 private final String reFrom = "([^! ]+)!.*";
34 private final String reTo = "(([^ :,]*)[:,] *)?(.*)";
36 private static Pattern ptMsg = null;
37 private static Pattern ptFrom = null;
38 private static Pattern ptTo = null;
41 public Date time = null;
43 public String line = "";
45 public String src = "";
46 public String cmd = "";
47 public String dst = "";
48 public String arg = "";
49 public String msg = "";
51 public Type type = Type.OTHER;
52 public How how = How.OTHER;
53 public String from = "";
54 public String to = "";
55 public String txt = "";
58 private String notnull(String string)
60 return string == null ? "" : string;
64 public Message(String dst, String from, String msg)
66 this.time = new Date();
67 this.type = Type.PRIVMSG;
73 this.line = this.cmd + " " + this.dst + " :" + this.msg;
76 public Message(String line, String name)
79 if (ptMsg == null) ptMsg = Pattern.compile(reMsg);
80 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
81 if (ptTo == null) ptTo = Pattern.compile(reTo);
84 this.time = new Date();
87 line = line.replaceAll("\\s+", " ");
88 line = line.replaceAll("^ | $", "");
91 // Split line into parts
92 Matcher mrMsg = ptMsg.matcher(line);
93 if (mrMsg.matches()) {
94 this.src = notnull(mrMsg.group(2));
95 this.cmd = notnull(mrMsg.group(4));
96 this.dst = notnull(mrMsg.group(6));
97 this.arg = notnull(mrMsg.group(8));
98 this.msg = notnull(mrMsg.group(10));
101 // Determine friendly parts
102 Matcher mrFrom = ptFrom.matcher(this.src);
103 if (mrFrom.matches())
104 this.from = notnull(mrFrom.group(1));
106 Matcher mrTo = ptTo.matcher(this.msg);
108 this.to = notnull(mrTo.group(2));
110 if (this.to.equals(""))
111 this.txt = notnull(this.msg);
113 this.txt = notnull(mrTo.group(3));
115 // Parse commands names
116 if (this.cmd.equals("PRIVMSG")) this.type = Type.PRIVMSG;
117 else if (this.cmd.equals("332")) this.type = Type.TOPIC;
118 else if (this.cmd.equals("353")) this.type = Type.NAMES;
119 else if (this.cmd.equals("ERROR")) this.type = Type.ERROR;
120 else if (this.cmd.equals("CAP")) this.type = Type.CAP;
121 else if (this.cmd.equals("AUTHENTICATE")) this.type = Type.AUTH;
122 else if (this.cmd.equals("903")) this.type = Type.AUTHOK;
123 else if (this.cmd.equals("904")) this.type = Type.AUTHFAIL;
124 else if (this.cmd.equals("905")) this.type = Type.AUTHFAIL;
125 else if (this.cmd.equals("906")) this.type = Type.AUTHFAIL;
126 else if (this.cmd.equals("907")) this.type = Type.AUTHFAIL;
129 if (this.dst.equals(name)) this.how = How.PRIVMSG;
130 else if (this.to.equals(name)) this.how = How.DIRECT;
131 else if (this.msg.contains(name)) this.how = How.MENTION;
132 else if (this.type == Type.PRIVMSG) this.how = How.CHANNEL;
137 Os.debug("---------------------");
138 Os.debug("line = [" + line + "]");
139 Os.debug("src = " + this.src);
140 Os.debug("cmd = " + this.cmd);
141 Os.debug("dst = " + this.dst);
142 Os.debug("arg = " + this.arg);
143 Os.debug("msg = " + this.msg);
144 Os.debug("from = " + this.from);
145 Os.debug("to = " + this.to);
146 Os.debug("txt = " + this.txt);
147 Os.debug("---------------------");
150 public String toString()
152 return this.from + ": " + this.txt;