1 package org.pileus.spades;
9 public String server = "irc.freenode.net";
10 public int port = 6667;
11 public String nickname = "SpadeGuest";
12 public String channel = "#rhnoise";
13 public boolean usesasl = false;
14 public String authname = "";
15 public String password = "";
16 public String username = "user";
17 public String hostname = "localhost";
20 public boolean ready = false;
23 private Socket socket;
24 private BufferedReader input;
25 private PrintWriter output;
33 Os.debug("Client: create");
36 public void setServer(String server, int port)
42 public void setAuth(boolean usesasl, String authname, String password)
44 this.usesasl = usesasl;
45 this.authname = authname;
46 this.password = password;
49 public void setUser(String nickname, String channel)
51 this.nickname = nickname;
52 this.channel = channel;
55 public boolean connect()
57 Os.debug("Client: connect");
60 this.socket = new Socket(this.server, this.port);
61 this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
62 this.output = new PrintWriter(this.socket.getOutputStream());
63 } catch (Exception e) {
64 Os.debug("Client: failed to create connection: " + e);
68 Os.debug("Client: connected");
70 this.raw("CAP REQ :sasl");
71 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname);
72 this.raw("NICK "+this.nickname);
77 public boolean abort()
79 Os.debug("Client: abort");
84 } catch (Exception e) {
85 Os.debug("Client: error closing socket", e);
90 public void raw(String line)
93 Os.debug("< " + line);
94 this.output.println(line);
96 } catch (Exception e) {
97 Os.debug("Client: error writing line", e);
101 public Message send(String txt)
103 Message msg = new Message(this.channel, this.nickname, txt);
108 public Message recv()
111 String line = input.readLine();
114 Os.debug("> " + line);
115 Message msg = new Message(line);
120 } catch (SocketException e) {
123 } catch (Exception e) {
125 Os.debug("Client: error in recv", e);
130 /* Private methods */
131 private void process(Message msg)
133 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
134 this.raw("JOIN " + this.channel);
135 this.raw("TOPIC " + this.channel);
138 if (msg.cmd.equals("433")) {
139 this.raw("NICK "+this.nickname+this.mangle);
142 if (msg.cmd.equals("PING")) {
143 this.raw("PING " + msg.msg);
147 private void dosasl(Message msg)
151 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
152 Os.debug("Client: sasl - starting auth");
153 this.raw("AUTHENTICATE PLAIN");
155 Os.debug("Client: sasl - Server does not support sasl");
159 if (msg.arg.equals("+")) {
160 Os.debug("Client: sasl - performin authentication");
161 this.raw("AUTHENTICATE " + Os.base64(
162 this.authname + "\0" +
163 this.authname + "\0" +
166 Os.debug("Client: sasl - unexpected authenticate response");
170 Os.debug("Client: SASL Auth Successful");
174 Os.debug("Client: SASL Auth Failed");