1 package org.pileus.spades;
3 import java.util.regex.Pattern;
4 import java.util.regex.Matcher;
17 private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) +)?(:(.*))";
18 private final String reFrom = "([^! ]+)!.*";
19 private final String reTo = "(([^ :,]*)[:,] *)?(.*)";
21 private static Pattern ptMsg = null;
22 private static Pattern ptFrom = null;
23 private static Pattern ptTo = null;
26 public String line = "";
28 public String src = "";
29 public String cmd = "";
30 public String dst = "";
31 public String arg = "";
32 public String msg = "";
34 public Type type = Type.OTHER;
35 public String from = "";
36 public String to = "";
37 public String txt = "";
40 private String notnull(String string)
42 return string == null ? "" : string;
46 public Message(String dst, String from, String msg)
52 this.line = this.cmd + " " + this.dst + " :" + this.msg;
55 public Message(String line)
58 if (ptMsg == null) ptMsg = Pattern.compile(reMsg);
59 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
60 if (ptTo == null) ptTo = Pattern.compile(reTo);
63 line = line.replaceAll("\\s+", " ");
64 line = line.replaceAll("^ | $", "");
65 line = line.replaceAll("\003[0-9]*", "");
68 // Split line into parts
69 Matcher mrMsg = ptMsg.matcher(line);
70 if (mrMsg.matches()) {
71 this.src = notnull(mrMsg.group(2));
72 this.cmd = notnull(mrMsg.group(4));
73 this.dst = notnull(mrMsg.group(6));
74 this.arg = notnull(mrMsg.group(8));
75 this.msg = notnull(mrMsg.group(10));
78 // Determine friendly parts
79 Matcher mrFrom = ptFrom.matcher(this.src);
81 this.from = notnull(mrFrom.group(1));
83 Matcher mrTo = ptTo.matcher(this.msg);
85 this.to = notnull(mrTo.group(2));
87 if (this.to.equals(""))
88 this.txt = notnull(this.msg);
90 this.txt = notnull(mrTo.group(3));
92 // Parse commands names
93 if (this.cmd.equals("PRIVMSG")) this.type = Type.PRIVMSG;
94 if (this.cmd.equals("332")) this.type = Type.TOPIC;
95 if (this.cmd.equals("353")) this.type = Type.NAMES;
100 Os.debug("---------------------");
101 Os.debug("line = [" + line + "]");
102 Os.debug("src = " + this.src);
103 Os.debug("cmd = " + this.cmd);
104 Os.debug("dst = " + this.dst);
105 Os.debug("arg = " + this.arg);
106 Os.debug("msg = " + this.msg);
107 Os.debug("from = " + this.from);
108 Os.debug("to = " + this.to);
109 Os.debug("txt = " + this.txt);
110 Os.debug("---------------------");
113 public String toString()
115 return this.from + ": " + this.txt;