1 package org.pileus.spades;
9 private String server = null;
10 private int port = 6667;
11 private String nickname = null;
12 private String channel = null;
13 private String username = null;
14 private String hostname = null;
16 private Socket socket = null;
17 private BufferedReader input = null;
18 private PrintWriter output = null;
20 private int mangle = 0;
23 public boolean ready = false;
26 public Client(String username, String hostname)
28 this.username = username;
29 this.hostname = hostname;
30 Os.debug("Client: create");
35 this("user", "localhost");
38 public boolean connect(String server, String nickname, String channel)
40 Os.debug("Client: connect");
43 this.nickname = nickname;
44 this.channel = channel;
47 this.socket = new Socket(this.server, this.port);
48 this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
49 this.output = new PrintWriter(this.socket.getOutputStream());
50 } catch (Exception e) {
51 Os.debug("Client: failed to create connection: " + e);
55 Os.debug("Client: connected");
56 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname);
57 this.raw("NICK "+this.nickname);
62 public boolean abort()
64 Os.debug("Client: abort");
69 } catch (Exception e) {
70 Os.debug("Client: error closing socket", e);
75 public void raw(String line)
78 Os.debug("< " + line);
79 this.output.println(line);
81 } catch (Exception e) {
82 Os.debug("Client: error writing line", e);
86 public Message send(String txt)
88 Message msg = new Message(this.channel, this.nickname, txt);
96 String line = input.readLine();
99 Os.debug("> " + line);
100 Message msg = new Message(line);
103 } catch (SocketException e) {
106 } catch (Exception e) {
108 Os.debug("Client: error in recv", e);
113 /* Private methods */
114 private void process(Message msg)
116 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
117 this.raw("JOIN " + this.channel);
118 this.raw("TOPIC " + this.channel);
121 if (msg.cmd.equals("433")) {
122 this.raw("NICK "+this.nickname+this.mangle);
125 if (msg.cmd.equals("PING")) {
126 this.raw("PING " + msg.msg);