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