1 package org.pileus.spades;
3 import java.util.regex.Pattern;
4 import java.util.regex.Matcher;
9 private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))";
10 private final String reFrom = "([^! ]+)!.*";
11 private final String reTo = "(([^ :,]*)[:,] *)?(.*)";
13 private static Pattern ptMsg = null;
14 private static Pattern ptFrom = null;
15 private static Pattern ptTo = null;
18 public String line = "";
20 public String src = "";
21 public String cmd = "";
22 public String dst = "";
23 public String arg = "";
24 public String msg = "";
26 public String from = "";
27 public String to = "";
28 public String txt = "";
31 private String notnull(String string)
33 return string == null ? "" : string;
37 public Message(String dst, String from, String msg)
43 this.line = this.cmd + " " + this.dst + " :" + this.msg;
46 public Message(String line)
48 if (ptMsg == null) ptMsg = Pattern.compile(reMsg);
49 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
50 if (ptTo == null) ptTo = Pattern.compile(reTo);
52 line = line.replaceAll("\\s+", " ");
53 line = line.replaceAll("^ | $", "");
54 line = line.replaceAll("\003[0-9]*", "");
57 Matcher mrMsg = ptMsg.matcher(line);
58 if (mrMsg.matches()) {
59 this.src = notnull(mrMsg.group(2));
60 this.cmd = notnull(mrMsg.group(4));
61 this.dst = notnull(mrMsg.group(6));
62 this.arg = notnull(mrMsg.group(8));
63 this.msg = notnull(mrMsg.group(10));
66 Matcher mrFrom = ptFrom.matcher(this.src);
68 this.from = notnull(mrFrom.group(1));
70 Matcher mrTo = ptTo.matcher(this.msg);
72 this.to = notnull(mrTo.group(2));
74 if (this.to.equals(""))
75 this.txt = notnull(this.msg);
77 this.txt = notnull(mrTo.group(3));
84 Os.debug("---------------------");
85 Os.debug("line = [" + line + "]");
86 Os.debug("src = " + this.src);
87 Os.debug("cmd = " + this.cmd);
88 Os.debug("dst = " + this.dst);
89 Os.debug("arg = " + this.arg);
90 Os.debug("msg = " + this.msg);
91 Os.debug("from = " + this.from);
92 Os.debug("to = " + this.to);
93 Os.debug("txt = " + this.txt);
94 Os.debug("---------------------");
97 public String toString()
99 return this.from + ": " + this.txt;