1 package org.pileus.spades;
4 import java.util.regex.Pattern;
5 import java.util.regex.Matcher;
11 OTHER, // Unknown message type
13 PART, // Leave channel
14 PRIVMSG, // Private message
15 TOPIC, // Display current topic
16 NAMES, // Display user names
17 ERROR, // Error message from server
18 CAP, // Server capabilities
19 AUTH, // Authentication message
20 AUTHOK, // Authentication succeeded
21 AUTHFAIL, // Authentication failed
25 OTHER, // Unknown message type
26 CHANNEL, // Normal message to a channel
27 MENTION, // User was mentioned in message text
28 DIRECT, // Message directed towards user
29 PRIVMSG, // Private message to user only
30 SENT // Message was sent by the user
34 private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
35 private final String reFrom = "([^! ]+)!.*";
36 private final String reTo = "(([^ :,]*)[:,] *)?(.*)";
37 private final String reCmd = "/([a-z]+)( +(.*))?";
39 private static Pattern ptMsg = null;
40 private static Pattern ptFrom = null;
41 private static Pattern ptTo = null;
42 private static Pattern ptCmd = null;
45 public Date time = null;
47 public String line = "";
49 public String src = "";
50 public String cmd = "";
51 public String dst = "";
52 public String arg = "";
53 public String msg = "";
55 public Type type = Type.OTHER;
56 public How how = How.OTHER;
57 public String from = "";
58 public String to = "";
59 public String txt = "";
62 private String notnull(String string)
64 return string == null ? "" : string;
68 public Message(String dst, String from, String msg)
70 this.time = new Date();
74 if (msg.charAt(0) == '/') {
76 ptCmd = Pattern.compile(reCmd);
77 Matcher mr = ptCmd.matcher(msg);
81 String cmd = notnull(mr.group(1));
82 String arg = notnull(mr.group(3));
84 if (cmd.matches("join")) {
85 Os.debug("Message: /join");
86 this.type = Type.JOIN;
89 this.line = this.cmd + " :" + arg;
91 if (cmd.matches("part")) {
92 Os.debug("Message: /part");
93 this.type = Type.PART;
96 this.line = this.cmd + " :" + arg;
98 if (this.line == null) {
99 Os.debug("Message: unknown command");
102 this.type = Type.PRIVMSG;
103 this.cmd = "PRIVMSG";
106 this.line = this.cmd + " " + this.dst + " :" + this.msg;
110 public Message(String line, String name)
112 // Initialize regexes
113 if (ptMsg == null) ptMsg = Pattern.compile(reMsg);
114 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
115 if (ptTo == null) ptTo = Pattern.compile(reTo);
118 this.time = new Date();
121 line = line.replaceAll("\\s+", " ");
122 line = line.replaceAll("^ | $", "");
125 // Split line into parts
126 Matcher mrMsg = ptMsg.matcher(line);
127 if (mrMsg.matches()) {
128 this.src = notnull(mrMsg.group(2));
129 this.cmd = notnull(mrMsg.group(4));
130 this.dst = notnull(mrMsg.group(6));
131 this.arg = notnull(mrMsg.group(8));
132 this.msg = notnull(mrMsg.group(10));
135 // Determine friendly parts
136 Matcher mrFrom = ptFrom.matcher(this.src);
137 if (mrFrom.matches())
138 this.from = notnull(mrFrom.group(1));
140 Matcher mrTo = ptTo.matcher(this.msg);
142 this.to = notnull(mrTo.group(2));
144 if (this.to.equals(""))
145 this.txt = notnull(this.msg);
147 this.txt = notnull(mrTo.group(3));
149 // Parse commands names
150 if (this.cmd.equals("PRIVMSG")) this.type = Type.PRIVMSG;
151 else if (this.cmd.equals("332")) this.type = Type.TOPIC;
152 else if (this.cmd.equals("353")) this.type = Type.NAMES;
153 else if (this.cmd.equals("ERROR")) this.type = Type.ERROR;
154 else if (this.cmd.equals("CAP")) this.type = Type.CAP;
155 else if (this.cmd.equals("AUTHENTICATE")) this.type = Type.AUTH;
156 else if (this.cmd.equals("903")) this.type = Type.AUTHOK;
157 else if (this.cmd.equals("904")) this.type = Type.AUTHFAIL;
158 else if (this.cmd.equals("905")) this.type = Type.AUTHFAIL;
159 else if (this.cmd.equals("906")) this.type = Type.AUTHFAIL;
160 else if (this.cmd.equals("907")) this.type = Type.AUTHFAIL;
163 if (this.dst.equals(name)) this.how = How.PRIVMSG;
164 else if (this.to.equals(name)) this.how = How.DIRECT;
165 else if (this.msg.contains(name)) this.how = How.MENTION;
166 else if (this.type == Type.PRIVMSG) this.how = How.CHANNEL;
171 Os.debug("---------------------");
172 Os.debug("line = [" + line + "]");
173 Os.debug("src = " + this.src);
174 Os.debug("cmd = " + this.cmd);
175 Os.debug("dst = " + this.dst);
176 Os.debug("arg = " + this.arg);
177 Os.debug("msg = " + this.msg);
178 Os.debug("from = " + this.from);
179 Os.debug("to = " + this.to);
180 Os.debug("txt = " + this.txt);
181 Os.debug("---------------------");
184 public String toString()
186 return this.from + ": " + this.txt;