1 package org.pileus.spades;
3 import java.util.regex.Pattern;
4 import java.util.regex.Matcher;
22 private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
23 private final String reFrom = "([^! ]+)!.*";
24 private final String reTo = "(([^ :,]*)[:,] *)?(.*)";
26 private static Pattern ptMsg = null;
27 private static Pattern ptFrom = null;
28 private static Pattern ptTo = null;
31 public String line = "";
33 public String src = "";
34 public String cmd = "";
35 public String dst = "";
36 public String arg = "";
37 public String msg = "";
39 public Type type = Type.OTHER;
40 public String from = "";
41 public String to = "";
42 public String txt = "";
45 private String notnull(String string)
47 return string == null ? "" : string;
51 public Message(String dst, String from, String msg)
57 this.line = this.cmd + " " + this.dst + " :" + this.msg;
60 public Message(String line)
63 if (ptMsg == null) ptMsg = Pattern.compile(reMsg);
64 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
65 if (ptTo == null) ptTo = Pattern.compile(reTo);
68 line = line.replaceAll("\\s+", " ");
69 line = line.replaceAll("^ | $", "");
70 line = line.replaceAll("\003[0-9]*", "");
73 // Split line into parts
74 Matcher mrMsg = ptMsg.matcher(line);
75 if (mrMsg.matches()) {
76 this.src = notnull(mrMsg.group(2));
77 this.cmd = notnull(mrMsg.group(4));
78 this.dst = notnull(mrMsg.group(6));
79 this.arg = notnull(mrMsg.group(8));
80 this.msg = notnull(mrMsg.group(10));
83 // Determine friendly parts
84 Matcher mrFrom = ptFrom.matcher(this.src);
86 this.from = notnull(mrFrom.group(1));
88 Matcher mrTo = ptTo.matcher(this.msg);
90 this.to = notnull(mrTo.group(2));
92 if (this.to.equals(""))
93 this.txt = notnull(this.msg);
95 this.txt = notnull(mrTo.group(3));
97 // Parse commands names
98 if (this.cmd.equals("PRIVMSG")) this.type = Type.PRIVMSG;
99 else if (this.cmd.equals("332")) this.type = Type.TOPIC;
100 else if (this.cmd.equals("353")) this.type = Type.NAMES;
101 else if (this.cmd.equals("ERROR")) this.type = Type.ERROR;
102 else if (this.cmd.equals("CAP")) this.type = Type.CAP;
103 else if (this.cmd.equals("AUTHENTICATE")) this.type = Type.AUTH;
104 else if (this.cmd.equals("903")) this.type = Type.AUTHOK;
105 else if (this.cmd.equals("904")) this.type = Type.AUTHFAIL;
106 else if (this.cmd.equals("905")) this.type = Type.AUTHFAIL;
107 else if (this.cmd.equals("906")) this.type = Type.AUTHFAIL;
108 else if (this.cmd.equals("907")) this.type = Type.AUTHFAIL;
113 Os.debug("---------------------");
114 Os.debug("line = [" + line + "]");
115 Os.debug("src = " + this.src);
116 Os.debug("cmd = " + this.cmd);
117 Os.debug("dst = " + this.dst);
118 Os.debug("arg = " + this.arg);
119 Os.debug("msg = " + this.msg);
120 Os.debug("from = " + this.from);
121 Os.debug("to = " + this.to);
122 Os.debug("txt = " + this.txt);
123 Os.debug("---------------------");
126 public String toString()
128 return this.from + ": " + this.txt;