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
31 private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
32 private final String reFrom = "([^! ]+)!.*";
33 private final String reTo = "(([^ :,]*)[:,] *)?(.*)";
35 private static Pattern ptMsg = null;
36 private static Pattern ptFrom = null;
37 private static Pattern ptTo = null;
40 public Date time = null;
42 public String line = "";
44 public String src = "";
45 public String cmd = "";
46 public String dst = "";
47 public String arg = "";
48 public String msg = "";
50 public Type type = Type.OTHER;
51 public How how = How.OTHER;
52 public String from = "";
53 public String to = "";
54 public String txt = "";
57 private String notnull(String string)
59 return string == null ? "" : string;
63 public Message(String dst, String from, String msg)
69 this.line = this.cmd + " " + this.dst + " :" + this.msg;
72 public Message(String line, String name)
75 if (ptMsg == null) ptMsg = Pattern.compile(reMsg);
76 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
77 if (ptTo == null) ptTo = Pattern.compile(reTo);
80 this.time = new Date();
83 line = line.replaceAll("\\s+", " ");
84 line = line.replaceAll("^ | $", "");
87 // Split line into parts
88 Matcher mrMsg = ptMsg.matcher(line);
89 if (mrMsg.matches()) {
90 this.src = notnull(mrMsg.group(2));
91 this.cmd = notnull(mrMsg.group(4));
92 this.dst = notnull(mrMsg.group(6));
93 this.arg = notnull(mrMsg.group(8));
94 this.msg = notnull(mrMsg.group(10));
97 // Determine friendly parts
98 Matcher mrFrom = ptFrom.matcher(this.src);
100 this.from = notnull(mrFrom.group(1));
102 Matcher mrTo = ptTo.matcher(this.msg);
104 this.to = notnull(mrTo.group(2));
106 if (this.to.equals(""))
107 this.txt = notnull(this.msg);
109 this.txt = notnull(mrTo.group(3));
111 // Parse commands names
112 if (this.cmd.equals("PRIVMSG")) this.type = Type.PRIVMSG;
113 else if (this.cmd.equals("332")) this.type = Type.TOPIC;
114 else if (this.cmd.equals("353")) this.type = Type.NAMES;
115 else if (this.cmd.equals("ERROR")) this.type = Type.ERROR;
116 else if (this.cmd.equals("CAP")) this.type = Type.CAP;
117 else if (this.cmd.equals("AUTHENTICATE")) this.type = Type.AUTH;
118 else if (this.cmd.equals("903")) this.type = Type.AUTHOK;
119 else if (this.cmd.equals("904")) this.type = Type.AUTHFAIL;
120 else if (this.cmd.equals("905")) this.type = Type.AUTHFAIL;
121 else if (this.cmd.equals("906")) this.type = Type.AUTHFAIL;
122 else if (this.cmd.equals("907")) this.type = Type.AUTHFAIL;
125 if (this.dst.equals(name)) this.how = How.PRIVMSG;
126 else if (this.to.equals(name)) this.how = How.DIRECT;
127 else if (this.msg.contains(name)) this.how = How.MENTION;
128 else if (this.type == Type.PRIVMSG) this.how = How.CHANNEL;
133 Os.debug("---------------------");
134 Os.debug("line = [" + line + "]");
135 Os.debug("src = " + this.src);
136 Os.debug("cmd = " + this.cmd);
137 Os.debug("dst = " + this.dst);
138 Os.debug("arg = " + this.arg);
139 Os.debug("msg = " + this.msg);
140 Os.debug("from = " + this.from);
141 Os.debug("to = " + this.to);
142 Os.debug("txt = " + this.txt);
143 Os.debug("---------------------");
146 public String toString()
148 return this.from + ": " + this.txt;