1 package org.pileus.spades;
17 public String server = "irc.freenode.net";
18 public int port = 6667;
19 public int timeout = 240;
20 public String nickname = "SpadeGuest";
21 public String channel = "#rhnoise";
22 public boolean usesasl = false;
23 public String authname = "";
24 public String password = "";
25 public String username = "user";
26 public String hostname = "localhost";
29 public State state = State.INIT;
30 public String name = "";
33 private boolean pinging;
34 private Socket socket;
35 private BufferedReader input;
36 private PrintWriter output;
44 Os.debug("Client: create");
47 public void setServer(String server, int port)
53 public void setAuth(boolean usesasl, String authname, String password)
55 this.usesasl = usesasl;
56 this.authname = authname;
57 this.password = password;
60 public void setUser(String nickname, String channel)
62 this.nickname = nickname;
63 this.channel = channel;
67 public boolean connect()
69 Os.debug("Client: connect");
72 this.state = State.SETUP;
73 this.socket = new Socket();
74 this.socket.setSoTimeout(this.timeout/2 * 1000);
75 this.socket.connect(new InetSocketAddress(this.server, this.port));
76 this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
77 this.output = new PrintWriter(this.socket.getOutputStream());
78 } catch (Exception e) {
79 Os.debug("Client: failed to create connection: " + e);
83 Os.debug("Client: connected");
85 this.raw("CAP REQ :sasl");
86 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.name);
87 this.raw("NICK "+this.name);
94 Os.debug("Client: abort");
95 this.state = State.ABORT;
101 Os.debug("Client: reset");
102 this.state = State.INIT;
105 public void raw(String line)
108 if (this.validate() != State.SETUP &&
109 this.validate() != State.READY)
111 Os.debug("< " + line);
112 this.output.println(line);
114 } catch (Exception e) {
115 Os.debug("Client: error writing line", e);
119 public Message send(String txt)
121 if (this.validate() != State.READY)
123 Message msg = new Message(this.channel, this.name, txt);
124 if (msg.type == Message.Type.JOIN)
125 this.channel = msg.msg;
130 public Message recv()
133 if (this.validate() != State.SETUP &&
134 this.validate() != State.READY)
136 String line = this.input.readLine();
139 Os.debug("> " + line);
140 Message msg = new Message(line, this.name);
146 catch (SocketTimeoutException e) {
152 this.raw("PING :" + hostname);
156 catch (SocketException e) {
157 this.state = State.INIT;
160 catch (Exception e) {
161 this.state = State.INIT;
162 Os.debug("Client: error in recv", e);
167 /* Private methods */
168 private State validate()
171 if (this.state == State.ABORT) {
172 if (this.socket != null) {
174 this.state = State.INIT;
177 } catch (Exception e) {
178 Os.debug("Client: error closing socket", e);
183 private void process(Message msg)
185 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
186 this.raw("JOIN " + this.channel);
187 this.raw("TOPIC " + this.channel);
188 this.state = State.READY;
190 if (msg.cmd.equals("433")) {
191 this.name = this.nickname + this.mangle;
192 this.mangle = this.mangle + 11;
193 this.raw("NICK " + this.name);
195 if (msg.cmd.equals("PING")) {
196 this.raw("PONG " + msg.msg);
198 if (msg.cmd.equals("PONG")) {
199 this.pinging = false;
203 private void dosasl(Message msg)
207 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
208 Os.debug("Client: sasl - starting auth");
209 this.raw("AUTHENTICATE PLAIN");
211 Os.debug("Client: sasl - Server does not support sasl");
215 if (msg.arg.equals("+")) {
216 Os.debug("Client: sasl - performin authentication");
217 this.raw("AUTHENTICATE " + Os.base64(
218 this.authname + "\0" +
219 this.authname + "\0" +
222 Os.debug("Client: sasl - unexpected authenticate response");
226 Os.debug("Client: SASL Auth Successful");
230 Os.debug("Client: SASL Auth Failed");