]> Pileus Git - ~andy/spades/commitdiff
Add support for SASL PLAIN authentication
authorAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 07:37:49 +0000 (07:37 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 07:37:49 +0000 (07:37 +0000)
res/xml/prefs.xml
src/org/pileus/spades/Client.java
src/org/pileus/spades/Main.java
src/org/pileus/spades/Message.java
src/org/pileus/spades/Os.java

index dc7eb289b0c7b65db46e7d6616ebbafba2bd04d0..d4fb1d125e379df7d4d85f29c6786314a29a2617 100644 (file)
@@ -39,7 +39,7 @@
                <EditTextPreference
                        android:key="pref_authname"
                        android:summary="Username for SASL Authentication"
-                       android:title="Nickname"
+                       android:title="Authname"
                        android:defaultValue="" />
                <EditTextPreference
                        android:key="pref_password"
index c7f6e1c0f26658679f72b588c861a4208a28faca..b2d6561c45630896ba949662248bd14b8069239a 100644 (file)
@@ -66,6 +66,8 @@ public class Client
                }
 
                Os.debug("Client: connected");
+               if (this.usesasl)
+                       this.raw("CAP REQ :sasl");
                this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname);
                this.raw("NICK "+this.nickname);
 
@@ -112,6 +114,8 @@ public class Client
                        Os.debug("> " + line);
                        Message msg = new Message(line);
                        this.process(msg);
+                       if (this.usesasl)
+                               this.dosasl(msg);
                        return msg;
                } catch (SocketException e) {
                        this.ready = false;
@@ -139,4 +143,37 @@ public class Client
                        this.raw("PING " + msg.msg);
                }
        }
+
+       private void dosasl(Message msg)
+       {
+               switch (msg.type) {
+                       case CAP:
+                               if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
+                                       Os.debug("Client: sasl - starting auth");
+                                       this.raw("AUTHENTICATE PLAIN");
+                               } else {
+                                       Os.debug("Client: sasl - Server does not support sasl");
+                               }
+                               break;
+                       case AUTH:
+                               if (msg.arg.equals("+")) {
+                                       Os.debug("Client: sasl - performin authentication");
+                                       this.raw("AUTHENTICATE " + Os.base64(
+                                                               this.authname + "\0" +
+                                                               this.authname + "\0" +
+                                                               this.password));
+                               } else {
+                                       Os.debug("Client: sasl - unexpected authenticate response");
+                               }
+                               break;
+                       case AUTHOK:
+                               Os.debug("Client: SASL Auth Successful");
+                               this.raw("CAP END");
+                               break;
+                       case AUTHFAIL:
+                               Os.debug("Client: SASL Auth Failed");
+                               this.raw("CAP END");
+                               break;
+               }
+       }
 }
index 05955f1e56376f7078076cf0f7d02ca8089d3f3b..f5118c13f1793f860428616dec2d59cc49390f14 100644 (file)
@@ -47,6 +47,12 @@ public class Main extends Activity
        private ScrollView   lscroll;
        private ScrollView   dscroll;
 
+       /* Private helper methods */
+       private void notice(String text)
+       {
+               this.log.append("*** " + text + "\n");
+       }
+
        /* Private handler methods */
        private void onRegister(Object obj)
        {
@@ -69,14 +75,23 @@ public class Main extends Activity
                                break;
                        case TOPIC:
                                if (!msg.txt.equals(this.topic))
-                                       this.log.append("** Topic for " + msg.arg + ": " + msg.txt + " **\n");
+                                       this.notice("Topic for " + msg.arg + ": " + msg.txt);
                                this.topic = msg.txt;
                                break;
                        case NAMES:
                                if (!msg.txt.equals(this.names))
-                                       this.log.append("** Users in " + msg.arg + ": " + msg.txt + " **\n");
+                                       this.notice("Users in " + msg.arg + ": " + msg.txt);
                                this.names = msg.txt;
                                break;
+                       case ERROR:
+                               this.notice("Error: " + msg.txt);
+                               break;
+                       case AUTHOK:
+                               this.notice("Authentication succeeded: " + msg.txt);
+                               break;
+                       case AUTHFAIL:
+                               this.notice("Authentication failed: " + msg.txt);
+                               break;
                }
                this.lscroll.smoothScrollTo(0, this.log.getBottom());
        }
@@ -84,7 +99,7 @@ public class Main extends Activity
        private void onNotify(String text)
        {
                Os.debug("Main: onNotify - " + text);
-               this.log.append("** " + text + " **\n");
+               this.notice(text);
                this.toast.setText(text);
                this.toast.show();
        }
index 331657e890b55ba0550d1e6256a64b587370e0e7..1f52ebe133443e167c216c0e0e82ea0108e8daef 100644 (file)
@@ -11,10 +11,15 @@ public class Message
                PRIVMSG,
                TOPIC,
                NAMES,
+               ERROR,
+               CAP,
+               AUTH,
+               AUTHOK,
+               AUTHFAIL,
        };
 
        /* Constnats */
-       private final  String  reMsg  = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) +)?(:(.*))";
+       private final  String  reMsg  = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?";
        private final  String  reFrom = "([^! ]+)!.*";
        private final  String  reTo   = "(([^ :,]*)[:,] *)?(.*)";
 
@@ -90,9 +95,17 @@ public class Message
                        this.txt  = notnull(mrTo.group(3));
 
                // Parse commands names
-               if (this.cmd.equals("PRIVMSG")) this.type = Type.PRIVMSG;
-               if (this.cmd.equals("332"))     this.type = Type.TOPIC;
-               if (this.cmd.equals("353"))     this.type = Type.NAMES;
+               if      (this.cmd.equals("PRIVMSG"))       this.type = Type.PRIVMSG;
+               else if (this.cmd.equals("332"))           this.type = Type.TOPIC;
+               else if (this.cmd.equals("353"))           this.type = Type.NAMES;
+               else if (this.cmd.equals("ERROR"))         this.type = Type.ERROR;
+               else if (this.cmd.equals("CAP"))           this.type = Type.CAP;
+               else if (this.cmd.equals("AUTHENTICATE"))  this.type = Type.AUTH;
+               else if (this.cmd.equals("903"))           this.type = Type.AUTHOK;
+               else if (this.cmd.equals("904"))           this.type = Type.AUTHFAIL;
+               else if (this.cmd.equals("905"))           this.type = Type.AUTHFAIL;
+               else if (this.cmd.equals("906"))           this.type = Type.AUTHFAIL;
+               else if (this.cmd.equals("907"))           this.type = Type.AUTHFAIL;
        }
 
        public void debug()
index 767de064b81dc08cfaa2fd73d4ab9a3b10f1c6eb..4f9e0fb4ce0d62d70d2adefd9231f643d5ffcf65 100644 (file)
@@ -1,6 +1,7 @@
 package org.pileus.spades;
 
 import android.util.Log;
+import android.util.Base64;
 
 public class Os
 {
@@ -13,4 +14,10 @@ public class Os
        {
                Log.d("Spades", txt);
        }
+
+       /* Utilities */
+       public static String base64(String txt)
+       {
+               return Base64.encodeToString(txt.getBytes(), 0);
+       }
 }