]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Message.java
331657e890b55ba0550d1e6256a64b587370e0e7
[~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         /* Enumerations */
9         enum Type {
10                 OTHER,
11                 PRIVMSG,
12                 TOPIC,
13                 NAMES,
14         };
15
16         /* Constnats */
17         private final  String  reMsg  = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) +)?(:(.*))";
18         private final  String  reFrom = "([^! ]+)!.*";
19         private final  String  reTo   = "(([^ :,]*)[:,] *)?(.*)";
20
21         private static Pattern ptMsg  = null;
22         private static Pattern ptFrom = null;
23         private static Pattern ptTo   = null;
24
25         /* Public data */
26         public String  line = "";
27
28         public String  src  = "";
29         public String  cmd  = "";
30         public String  dst  = "";
31         public String  arg  = "";
32         public String  msg  = "";
33
34         public Type    type = Type.OTHER;
35         public String  from = "";
36         public String  to   = "";
37         public String  txt  = "";
38
39         /* Private methods */
40         private String notnull(String string)
41         {
42                 return string == null ? "" : string;
43         }
44
45         /* Public Methods */
46         public Message(String dst, String from, String msg)
47         {
48                 this.cmd  = "PRIVMSG";
49                 this.dst  = dst;
50                 this.from = from;
51                 this.msg  = msg;
52                 this.line = this.cmd + " " + this.dst + " :" + this.msg;
53         }
54
55         public Message(String line)
56         {
57                 // Initialize regexes
58                 if (ptMsg  == null) ptMsg  = Pattern.compile(reMsg);
59                 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
60                 if (ptTo   == null) ptTo   = Pattern.compile(reTo);
61
62                 // Cleanup line
63                 line = line.replaceAll("\\s+",       " ");
64                 line = line.replaceAll("^ | $",      "");
65                 line = line.replaceAll("\003[0-9]*", "");
66                 this.line = line;
67
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));
76                 }
77
78                 // Determine friendly parts
79                 Matcher mrFrom = ptFrom.matcher(this.src);
80                 if (mrFrom.matches())
81                         this.from = notnull(mrFrom.group(1));
82
83                 Matcher mrTo = ptTo.matcher(this.msg);
84                 if (mrTo.matches())
85                         this.to   = notnull(mrTo.group(2));
86
87                 if (this.to.equals(""))
88                         this.txt  = notnull(this.msg);
89                 else
90                         this.txt  = notnull(mrTo.group(3));
91
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;
96         }
97
98         public void debug()
99         {
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("---------------------");
111         }
112
113         public String toString()
114         {
115                 return this.from + ": " + this.txt;
116         }
117 }