]> Pileus Git - ~andy/spades/commitdiff
Add /join and /part commands
authorAndy Spencer <andy753421@gmail.com>
Mon, 22 Apr 2013 02:09:42 +0000 (02:09 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 22 Apr 2013 02:09:42 +0000 (02:09 +0000)
src/org/pileus/spades/Client.java
src/org/pileus/spades/Message.java

index 3e9e3400b3480daeab2dcac0bdb1b0f05e7bcb8f..f1657840c02924a06864a4b85e9ba885f4b69e72 100644 (file)
@@ -102,7 +102,11 @@ public class Client
 
        public Message send(String txt)
        {
+               if (!this.ready)
+                       return null;
                Message msg = new Message(this.channel, this.name, txt);
+               if (msg.type == Message.Type.JOIN)
+                       this.channel = msg.msg;
                this.raw(msg.line);
                return msg;
        }
index 77e657b9ff0d1ed2069244e2d674e05cf4ccfb4f..d13ea1ceaee1fe2dbe7a3ded2170227804e06982 100644 (file)
@@ -9,6 +9,8 @@ public class Message
        /* Enumerations */
        enum Type {
                OTHER,    // Unknown message type
+               JOIN,     // Join channel
+               PART,     // Leave channel
                PRIVMSG,  // Private message
                TOPIC,    // Display current topic
                NAMES,    // Display user names
@@ -32,10 +34,12 @@ public class Message
        private final  String  reMsg  = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
        private final  String  reFrom = "([^! ]+)!.*";
        private final  String  reTo   = "(([^ :,]*)[:,] *)?(.*)";
+       private final  String  reCmd  = "/([a-z]+)( +(.*))?";
 
        private static Pattern ptMsg  = null;
        private static Pattern ptFrom = null;
        private static Pattern ptTo   = null;
+       private static Pattern ptCmd  = null;
 
        /* Public data */
        public Date    time = null;
@@ -64,13 +68,43 @@ public class Message
        public Message(String dst, String from, String msg)
        {
                this.time = new Date();
-               this.type = Type.PRIVMSG;
-               this.cmd  = "PRIVMSG";
                this.how  = How.SENT;
-               this.dst  = dst;
                this.from = from;
-               this.msg  = msg;
-               this.line = this.cmd + " " + this.dst + " :" + this.msg;
+
+               if (msg.charAt(0) == '/') {
+                       if (ptCmd == null)
+                               ptCmd = Pattern.compile(reCmd);
+                       Matcher mr = ptCmd.matcher(msg);
+                       if (!mr.matches())
+                               return;
+
+                       String cmd = notnull(mr.group(1));
+                       String arg = notnull(mr.group(3));
+
+                       if (cmd.matches("join")) {
+                               Os.debug("Message: /join");
+                               this.type = Type.JOIN;
+                               this.cmd  = "JOIN";
+                               this.msg  = arg;
+                               this.line = this.cmd + " :" + arg;
+                       }
+                       if (cmd.matches("part")) {
+                               Os.debug("Message: /part");
+                               this.type = Type.PART;
+                               this.cmd  = "PART";
+                               this.msg  = arg;
+                               this.line = this.cmd + " :" + arg;
+                       }
+                       if (this.line == null) {
+                               Os.debug("Message: unknown command");
+                       }
+               } else {
+                       this.type = Type.PRIVMSG;
+                       this.cmd  = "PRIVMSG";
+                       this.dst  = dst;
+                       this.msg  = msg;
+                       this.line = this.cmd + " " + this.dst + " :" + this.msg;
+               }
        }
 
        public Message(String line, String name)