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)
107 Message msg = new Message(this.channel, this.name, txt);
108 if (msg.type == Message.Type.JOIN)
109 this.channel = msg.msg;
114 public Message recv()
117 String line = input.readLine();
120 Os.debug("> " + line);
121 Message msg = new Message(line, this.name);
126 } catch (SocketException e) {
129 } catch (Exception e) {
131 Os.debug("Client: error in recv", e);
136 /* Private methods */
137 private void process(Message msg)
139 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
140 this.raw("JOIN " + this.channel);
141 this.raw("TOPIC " + this.channel);
144 if (msg.cmd.equals("433")) {
145 this.name = this.nickname + this.mangle;
146 this.mangle = this.mangle + 11;
147 this.raw("NICK " + this.name);
149 if (msg.cmd.equals("PING")) {
150 this.raw("PING " + msg.msg);
154 private void dosasl(Message msg)
158 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
159 Os.debug("Client: sasl - starting auth");
160 this.raw("AUTHENTICATE PLAIN");
162 Os.debug("Client: sasl - Server does not support sasl");
166 if (msg.arg.equals("+")) {
167 Os.debug("Client: sasl - performin authentication");
168 this.raw("AUTHENTICATE " + Os.base64(
169 this.authname + "\0" +
170 this.authname + "\0" +
173 Os.debug("Client: sasl - unexpected authenticate response");
177 Os.debug("Client: SASL Auth Successful");
181 Os.debug("Client: SASL Auth Failed");