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;
21 public String name = "";
24 private Socket socket;
25 private BufferedReader input;
26 private PrintWriter output;
34 Os.debug("Client: create");
37 public void setServer(String server, int port)
43 public void setAuth(boolean usesasl, String authname, String password)
45 this.usesasl = usesasl;
46 this.authname = authname;
47 this.password = password;
50 public void setUser(String nickname, String channel)
52 this.nickname = nickname;
53 this.channel = channel;
57 public boolean connect()
59 Os.debug("Client: connect");
62 this.socket = new Socket(this.server, this.port);
63 this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
64 this.output = new PrintWriter(this.socket.getOutputStream());
65 } catch (Exception e) {
66 Os.debug("Client: failed to create connection: " + e);
70 Os.debug("Client: connected");
72 this.raw("CAP REQ :sasl");
73 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.name);
74 this.raw("NICK "+this.name);
79 public boolean abort()
81 Os.debug("Client: abort");
86 } catch (Exception e) {
87 Os.debug("Client: error closing socket", e);
92 public void raw(String line)
95 Os.debug("< " + line);
96 this.output.println(line);
98 } catch (Exception e) {
99 Os.debug("Client: error writing line", e);
103 public Message send(String txt)
105 Message msg = new Message(this.channel, this.name, txt);
110 public Message recv()
113 String line = input.readLine();
116 Os.debug("> " + line);
117 Message msg = new Message(line, this.name);
122 } catch (SocketException e) {
125 } catch (Exception e) {
127 Os.debug("Client: error in recv", e);
132 /* Private methods */
133 private void process(Message msg)
135 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
136 this.raw("JOIN " + this.channel);
137 this.raw("TOPIC " + this.channel);
140 if (msg.cmd.equals("433")) {
141 this.name = this.nickname + this.mangle;
142 this.mangle = this.mangle + 11;
143 this.raw("NICK " + this.name);
145 if (msg.cmd.equals("PING")) {
146 this.raw("PING " + msg.msg);
150 private void dosasl(Message msg)
154 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
155 Os.debug("Client: sasl - starting auth");
156 this.raw("AUTHENTICATE PLAIN");
158 Os.debug("Client: sasl - Server does not support sasl");
162 if (msg.arg.equals("+")) {
163 Os.debug("Client: sasl - performin authentication");
164 this.raw("AUTHENTICATE " + Os.base64(
165 this.authname + "\0" +
166 this.authname + "\0" +
169 Os.debug("Client: sasl - unexpected authenticate response");
173 Os.debug("Client: SASL Auth Successful");
177 Os.debug("Client: SASL Auth Failed");