]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Client.java
Initial text formatting
[~andy/spades] / src / org / pileus / spades / Client.java
index 693ec551b19fc0f11e30800aecf37d745e190724..3e9e3400b3480daeab2dcac0bdb1b0f05e7bcb8f 100644 (file)
@@ -5,41 +5,58 @@ import java.net.*;
 
 public class Client
 {
-       /* Private data */
-       private String         server   = null;
-       private int            port     = 6667;
-       private String         nickname = null;
-       private String         channel  = null;
-       private String         username = null;
-       private String         hostname = null;
-
-       private Socket         socket   = null;
-       private BufferedReader input    = null;
-       private PrintWriter    output   = null;
+       /* Preference data */
+       public  String         server   = "irc.freenode.net";
+       public  int            port     = 6667;
+       public  String         nickname = "SpadeGuest";
+       public  String         channel  = "#rhnoise";
+       public  boolean        usesasl  = false;
+       public  String         authname = "";
+       public  String         password = "";
+       public  String         username = "user";
+       public  String         hostname = "localhost";
 
        /* Public data */
        public  boolean        ready    = false;
+       public  String         name     = "";
+
+               /* Connection data */
+       private Socket         socket;
+       private BufferedReader input;
+       private PrintWriter    output;
+
+       /* Private data */
+       private int            mangle;
 
        /* Public Methods */
-       public Client(String username, String hostname)
+       public Client()
        {
-               this.username = username;
-               this.hostname = hostname;
                Os.debug("Client: create");
        }
 
-       public Client()
+       public void setServer(String server, int port)
        {
-               this("user", "localhost");
+               this.server = server;
+               this.port   = port;
        }
 
-       public boolean connect(String server, String nickname, String channel)
+       public void setAuth(boolean usesasl, String authname, String password)
        {
-               Os.debug("Client: connect");
+               this.usesasl  = usesasl;
+               this.authname = authname;
+               this.password = password;
+       }
 
-               this.server   = server;
+       public void setUser(String nickname, String channel)
+       {
                this.nickname = nickname;
                this.channel  = channel;
+               this.name     = nickname;
+       }
+
+       public boolean connect()
+       {
+               Os.debug("Client: connect");
 
                try {
                        this.socket = new Socket(this.server, this.port);
@@ -51,8 +68,10 @@ public class Client
                }
 
                Os.debug("Client: connected");
-               this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname);
-               this.raw("NICK "+this.nickname);
+               if (this.usesasl)
+                       this.raw("CAP REQ :sasl");
+               this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.name);
+               this.raw("NICK "+this.name);
 
                return true;
        }
@@ -83,7 +102,7 @@ public class Client
 
        public Message send(String txt)
        {
-               Message msg  = new Message(this.channel, this.nickname, txt);
+               Message msg = new Message(this.channel, this.name, txt);
                this.raw(msg.line);
                return msg;
        }
@@ -95,8 +114,10 @@ public class Client
                        if (line == null)
                                return null;
                        Os.debug("> " + line);
-                       Message msg = new Message(line);
+                       Message msg = new Message(line, this.name);
                        this.process(msg);
+                       if (this.usesasl)
+                               this.dosasl(msg);
                        return msg;
                } catch (SocketException e) {
                        this.ready = false;
@@ -116,8 +137,46 @@ public class Client
                        this.raw("TOPIC " + this.channel);
                        this.ready = true;
                }
+               if (msg.cmd.equals("433")) {
+                       this.name   = this.nickname + this.mangle;
+                       this.mangle = this.mangle + 11;
+                       this.raw("NICK "  + this.name);
+               }
                if (msg.cmd.equals("PING")) {
                        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;
+               }
+       }
 }