]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Message.java
Add IRC Service and update UI
[~andy/spades] / src / org / pileus / spades / Message.java
1 package org.pileus.spades;
2
3 import java.util.regex.Pattern;
4 import java.util.regex.Matcher;
5
6 public class Message
7 {
8         /* Constnats */
9         private final  String  reMsg  = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))";
10         private final  String  reFrom = "([^! ]+)!.*";
11         private final  String  reTo   = "(([^ :,]*)[:,] *)?(.*)";
12
13         private static Pattern ptMsg  = null;
14         private static Pattern ptFrom = null;
15         private static Pattern ptTo   = null;
16
17         /* Public data */
18         public String  line = "";
19
20         public String  src  = "";
21         public String  cmd  = "";
22         public String  dst  = "";
23         public String  arg  = "";
24         public String  msg  = "";
25
26         public String  from = "";
27         public String  to   = "";
28         public String  txt  = "";
29
30         /* Private methods */
31         private String notnull(String string)
32         {
33                 return string == null ? "" : string;
34         }
35
36         /* Public Methods */
37         public Message(String dst, String from, String msg)
38         {
39                 this.cmd  = "PRIVMSG";
40                 this.dst  = dst;
41                 this.from = from;
42                 this.msg  = msg;
43                 this.line = this.cmd + " " + this.dst + " :" + this.msg;
44         }
45
46         public Message(String line)
47         {
48                 if (ptMsg  == null) ptMsg  = Pattern.compile(reMsg);
49                 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
50                 if (ptTo   == null) ptTo   = Pattern.compile(reTo);
51
52                 line = line.replaceAll("\\s+",       " ");
53                 line = line.replaceAll("^ | $",      "");
54                 line = line.replaceAll("\003[0-9]*", "");
55                 this.line = line;
56
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));
64                 }
65
66                 Matcher mrFrom = ptFrom.matcher(this.src);
67                 if (mrFrom.matches())
68                         this.from = notnull(mrFrom.group(1));
69
70                 Matcher mrTo = ptTo.matcher(this.msg);
71                 if (mrTo.matches())
72                         this.to   = notnull(mrTo.group(2));
73
74                 if (this.to.equals(""))
75                         this.txt  = notnull(this.msg);
76                 else
77                         this.txt  = notnull(mrTo.group(3));
78
79                 this.debug();
80         }
81
82         public void debug()
83         {
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("---------------------");
95         }
96
97         public String toString()
98         {
99                 return this.from + ": " + this.txt;
100         }
101 }