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