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 dst, String txt)
121 if (txt == null || txt.length() == 0)
123 if (this.validate() != State.READY)
125 Message msg = new Message(dst, this.name, txt);
126 if (msg.type == Message.Type.JOIN)
127 this.channel = msg.msg;
132 public Message send(String txt)
134 return this.send(this.channel, txt);
137 public Message recv()
140 if (this.validate() != State.SETUP &&
141 this.validate() != State.READY)
143 String line = this.input.readLine();
146 Os.debug("> " + line);
147 Message msg = new Message(line, this.name);
153 catch (SocketTimeoutException e) {
159 this.raw("PING :" + hostname);
163 catch (SocketException e) {
164 this.state = State.INIT;
167 catch (Exception e) {
168 this.state = State.INIT;
169 Os.debug("Client: error in recv", e);
174 /* Private methods */
175 private State validate()
178 if (this.state == State.ABORT) {
179 if (this.socket != null) {
181 this.state = State.INIT;
184 } catch (Exception e) {
185 Os.debug("Client: error closing socket", e);
190 private void process(Message msg)
192 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
193 this.raw("JOIN " + this.channel);
194 this.raw("TOPIC " + this.channel);
195 this.state = State.READY;
197 if (msg.cmd.equals("433")) {
198 this.name = this.nickname + this.mangle;
199 this.mangle = this.mangle + 11;
200 this.raw("NICK " + this.name);
202 if (msg.cmd.equals("PING")) {
203 this.raw("PONG " + msg.msg);
205 if (msg.cmd.equals("PONG")) {
206 this.pinging = false;
210 private void dosasl(Message msg)
214 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
215 Os.debug("Client: sasl - starting auth");
216 this.raw("AUTHENTICATE PLAIN");
218 Os.debug("Client: sasl - Server does not support sasl");
222 if (msg.arg.equals("+")) {
223 Os.debug("Client: sasl - performin authentication");
224 this.raw("AUTHENTICATE " + Os.base64(
225 this.authname + "\0" +
226 this.authname + "\0" +
229 Os.debug("Client: sasl - unexpected authenticate response");
233 Os.debug("Client: SASL Auth Successful");
237 Os.debug("Client: SASL Auth Failed");