]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Message.java
1f52ebe133443e167c216c0e0e82ea0108e8daef
[~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                 ERROR,
15                 CAP,
16                 AUTH,
17                 AUTHOK,
18                 AUTHFAIL,
19         };
20
21         /* Constnats */
22         private final  String  reMsg  = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
23         private final  String  reFrom = "([^! ]+)!.*";
24         private final  String  reTo   = "(([^ :,]*)[:,] *)?(.*)";
25
26         private static Pattern ptMsg  = null;
27         private static Pattern ptFrom = null;
28         private static Pattern ptTo   = null;
29
30         /* Public data */
31         public String  line = "";
32
33         public String  src  = "";
34         public String  cmd  = "";
35         public String  dst  = "";
36         public String  arg  = "";
37         public String  msg  = "";
38
39         public Type    type = Type.OTHER;
40         public String  from = "";
41         public String  to   = "";
42         public String  txt  = "";
43
44         /* Private methods */
45         private String notnull(String string)
46         {
47                 return string == null ? "" : string;
48         }
49
50         /* Public Methods */
51         public Message(String dst, String from, String msg)
52         {
53                 this.cmd  = "PRIVMSG";
54                 this.dst  = dst;
55                 this.from = from;
56                 this.msg  = msg;
57                 this.line = this.cmd + " " + this.dst + " :" + this.msg;
58         }
59
60         public Message(String line)
61         {
62                 // Initialize regexes
63                 if (ptMsg  == null) ptMsg  = Pattern.compile(reMsg);
64                 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
65                 if (ptTo   == null) ptTo   = Pattern.compile(reTo);
66
67                 // Cleanup line
68                 line = line.replaceAll("\\s+",       " ");
69                 line = line.replaceAll("^ | $",      "");
70                 line = line.replaceAll("\003[0-9]*", "");
71                 this.line = line;
72
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));
81                 }
82
83                 // Determine friendly parts
84                 Matcher mrFrom = ptFrom.matcher(this.src);
85                 if (mrFrom.matches())
86                         this.from = notnull(mrFrom.group(1));
87
88                 Matcher mrTo = ptTo.matcher(this.msg);
89                 if (mrTo.matches())
90                         this.to   = notnull(mrTo.group(2));
91
92                 if (this.to.equals(""))
93                         this.txt  = notnull(this.msg);
94                 else
95                         this.txt  = notnull(mrTo.group(3));
96
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;
109         }
110
111         public void debug()
112         {
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("---------------------");
124         }
125
126         public String toString()
127         {
128                 return this.from + ": " + this.txt;
129         }
130 }