]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Message.java
Add /join and /part commands
[~andy/spades] / src / org / pileus / spades / Message.java
1 package org.pileus.spades;
2
3 import java.util.Date;
4 import java.util.regex.Pattern;
5 import java.util.regex.Matcher;
6
7 public class Message
8 {
9         /* Enumerations */
10         enum Type {
11                 OTHER,    // Unknown message type
12                 JOIN,     // Join channel
13                 PART,     // Leave channel
14                 PRIVMSG,  // Private message
15                 TOPIC,    // Display current topic
16                 NAMES,    // Display user names
17                 ERROR,    // Error message from server
18                 CAP,      // Server capabilities
19                 AUTH,     // Authentication message
20                 AUTHOK,   // Authentication succeeded
21                 AUTHFAIL, // Authentication failed
22         };
23
24         enum How {
25                 OTHER,    // Unknown message type
26                 CHANNEL,  // Normal message to a channel
27                 MENTION,  // User was mentioned in message text
28                 DIRECT,   // Message directed towards user
29                 PRIVMSG,  // Private message to user only
30                 SENT      // Message was sent by the user
31         };
32
33         /* Constants */
34         private final  String  reMsg  = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
35         private final  String  reFrom = "([^! ]+)!.*";
36         private final  String  reTo   = "(([^ :,]*)[:,] *)?(.*)";
37         private final  String  reCmd  = "/([a-z]+)( +(.*))?";
38
39         private static Pattern ptMsg  = null;
40         private static Pattern ptFrom = null;
41         private static Pattern ptTo   = null;
42         private static Pattern ptCmd  = null;
43
44         /* Public data */
45         public Date    time = null;
46
47         public String  line = "";
48
49         public String  src  = "";
50         public String  cmd  = "";
51         public String  dst  = "";
52         public String  arg  = "";
53         public String  msg  = "";
54
55         public Type    type = Type.OTHER;
56         public How     how  = How.OTHER;
57         public String  from = "";
58         public String  to   = "";
59         public String  txt  = "";
60
61         /* Private methods */
62         private String notnull(String string)
63         {
64                 return string == null ? "" : string;
65         }
66
67         /* Public Methods */
68         public Message(String dst, String from, String msg)
69         {
70                 this.time = new Date();
71                 this.how  = How.SENT;
72                 this.from = from;
73
74                 if (msg.charAt(0) == '/') {
75                         if (ptCmd == null)
76                                 ptCmd = Pattern.compile(reCmd);
77                         Matcher mr = ptCmd.matcher(msg);
78                         if (!mr.matches())
79                                 return;
80
81                         String cmd = notnull(mr.group(1));
82                         String arg = notnull(mr.group(3));
83
84                         if (cmd.matches("join")) {
85                                 Os.debug("Message: /join");
86                                 this.type = Type.JOIN;
87                                 this.cmd  = "JOIN";
88                                 this.msg  = arg;
89                                 this.line = this.cmd + " :" + arg;
90                         }
91                         if (cmd.matches("part")) {
92                                 Os.debug("Message: /part");
93                                 this.type = Type.PART;
94                                 this.cmd  = "PART";
95                                 this.msg  = arg;
96                                 this.line = this.cmd + " :" + arg;
97                         }
98                         if (this.line == null) {
99                                 Os.debug("Message: unknown command");
100                         }
101                 } else {
102                         this.type = Type.PRIVMSG;
103                         this.cmd  = "PRIVMSG";
104                         this.dst  = dst;
105                         this.msg  = msg;
106                         this.line = this.cmd + " " + this.dst + " :" + this.msg;
107                 }
108         }
109
110         public Message(String line, String name)
111         {
112                 // Initialize regexes
113                 if (ptMsg  == null) ptMsg  = Pattern.compile(reMsg);
114                 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
115                 if (ptTo   == null) ptTo   = Pattern.compile(reTo);
116
117                 // Set time stamp
118                 this.time = new Date();
119
120                 // Cleanup line
121                 line = line.replaceAll("\\s+",       " ");
122                 line = line.replaceAll("^ | $",      "");
123                 this.line = line;
124
125                 // Split line into parts
126                 Matcher mrMsg = ptMsg.matcher(line);
127                 if (mrMsg.matches()) {
128                         this.src  = notnull(mrMsg.group(2));
129                         this.cmd  = notnull(mrMsg.group(4));
130                         this.dst  = notnull(mrMsg.group(6));
131                         this.arg  = notnull(mrMsg.group(8));
132                         this.msg  = notnull(mrMsg.group(10));
133                 }
134
135                 // Determine friendly parts
136                 Matcher mrFrom = ptFrom.matcher(this.src);
137                 if (mrFrom.matches())
138                         this.from = notnull(mrFrom.group(1));
139
140                 Matcher mrTo = ptTo.matcher(this.msg);
141                 if (mrTo.matches())
142                         this.to   = notnull(mrTo.group(2));
143
144                 if (this.to.equals(""))
145                         this.txt  = notnull(this.msg);
146                 else
147                         this.txt  = notnull(mrTo.group(3));
148
149                 // Parse commands names
150                 if      (this.cmd.equals("PRIVMSG"))       this.type = Type.PRIVMSG;
151                 else if (this.cmd.equals("332"))           this.type = Type.TOPIC;
152                 else if (this.cmd.equals("353"))           this.type = Type.NAMES;
153                 else if (this.cmd.equals("ERROR"))         this.type = Type.ERROR;
154                 else if (this.cmd.equals("CAP"))           this.type = Type.CAP;
155                 else if (this.cmd.equals("AUTHENTICATE"))  this.type = Type.AUTH;
156                 else if (this.cmd.equals("903"))           this.type = Type.AUTHOK;
157                 else if (this.cmd.equals("904"))           this.type = Type.AUTHFAIL;
158                 else if (this.cmd.equals("905"))           this.type = Type.AUTHFAIL;
159                 else if (this.cmd.equals("906"))           this.type = Type.AUTHFAIL;
160                 else if (this.cmd.equals("907"))           this.type = Type.AUTHFAIL;
161
162                 // Set directed
163                 if      (this.dst.equals(name))            this.how  = How.PRIVMSG;
164                 else if (this.to.equals(name))             this.how  = How.DIRECT;
165                 else if (this.msg.contains(name))          this.how  = How.MENTION;
166                 else if (this.type == Type.PRIVMSG)        this.how  = How.CHANNEL;
167         }
168
169         public void debug()
170         {
171                 Os.debug("---------------------");
172                 Os.debug("line = [" + line + "]");
173                 Os.debug("src  = " + this.src);
174                 Os.debug("cmd  = " + this.cmd);
175                 Os.debug("dst  = " + this.dst);
176                 Os.debug("arg  = " + this.arg);
177                 Os.debug("msg  = " + this.msg);
178                 Os.debug("from = " + this.from);
179                 Os.debug("to   = " + this.to);
180                 Os.debug("txt  = " + this.txt);
181                 Os.debug("---------------------");
182         }
183
184         public String toString()
185         {
186                 return this.from + ": " + this.txt;
187         }
188 }