1 package org.pileus.spades;
17 public String server = "irc.freenode.net";
18 public int port = 6667;
19 public String nickname = "SpadeGuest";
20 public String channel = "#rhnoise";
21 public boolean usesasl = false;
22 public String authname = "";
23 public String password = "";
24 public String username = "user";
25 public String hostname = "localhost";
28 public State state = State.INIT;
29 public String name = "";
32 private Socket socket;
33 private BufferedReader input;
34 private PrintWriter output;
42 Os.debug("Client: create");
45 public void setServer(String server, int port)
51 public void setAuth(boolean usesasl, String authname, String password)
53 this.usesasl = usesasl;
54 this.authname = authname;
55 this.password = password;
58 public void setUser(String nickname, String channel)
60 this.nickname = nickname;
61 this.channel = channel;
65 public boolean connect()
67 Os.debug("Client: connect");
70 this.state = State.SETUP;
71 this.socket = new Socket();
72 this.socket.connect(new InetSocketAddress(this.server, this.port));
73 this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
74 this.output = new PrintWriter(this.socket.getOutputStream());
75 } catch (Exception e) {
76 Os.debug("Client: failed to create connection: " + e);
80 Os.debug("Client: connected");
82 this.raw("CAP REQ :sasl");
83 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.name);
84 this.raw("NICK "+this.name);
91 Os.debug("Client: abort");
92 this.state = State.ABORT;
98 Os.debug("Client: reset");
99 this.state = State.INIT;
102 public void raw(String line)
105 if (this.validate() != State.SETUP &&
106 this.validate() != State.READY)
108 Os.debug("< " + line);
109 this.output.println(line);
111 } catch (Exception e) {
112 Os.debug("Client: error writing line", e);
116 public Message send(String txt)
118 if (this.validate() != State.READY)
120 Message msg = new Message(this.channel, this.name, txt);
121 if (msg.type == Message.Type.JOIN)
122 this.channel = msg.msg;
127 public Message recv()
130 if (this.validate() != State.SETUP &&
131 this.validate() != State.READY)
133 String line = this.input.readLine();
136 Os.debug("> " + line);
137 Message msg = new Message(line, this.name);
142 } catch (SocketException e) {
143 this.state = State.INIT;
145 } catch (Exception e) {
146 this.state = State.INIT;
147 Os.debug("Client: error in recv", e);
152 /* Private methods */
153 private State validate()
156 if (this.state == State.ABORT) {
157 if (this.socket != null) {
159 this.state = State.INIT;
162 } catch (Exception e) {
163 Os.debug("Client: error closing socket", e);
168 private void process(Message msg)
170 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
171 this.raw("JOIN " + this.channel);
172 this.raw("TOPIC " + this.channel);
173 this.state = State.READY;
175 if (msg.cmd.equals("433")) {
176 this.name = this.nickname + this.mangle;
177 this.mangle = this.mangle + 11;
178 this.raw("NICK " + this.name);
180 if (msg.cmd.equals("PING")) {
181 this.raw("PING " + msg.msg);
185 private void dosasl(Message msg)
189 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
190 Os.debug("Client: sasl - starting auth");
191 this.raw("AUTHENTICATE PLAIN");
193 Os.debug("Client: sasl - Server does not support sasl");
197 if (msg.arg.equals("+")) {
198 Os.debug("Client: sasl - performin authentication");
199 this.raw("AUTHENTICATE " + Os.base64(
200 this.authname + "\0" +
201 this.authname + "\0" +
204 Os.debug("Client: sasl - unexpected authenticate response");
208 Os.debug("Client: SASL Auth Successful");
212 Os.debug("Client: SASL Auth Failed");