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;
21 public boolean ready = false;
24 public Client(String username, String hostname)
26 this.username = username;
27 this.hostname = hostname;
28 Os.debug("Client: create");
33 this("user", "localhost");
36 public boolean connect(String server, String nickname, String channel)
38 Os.debug("Client: connect");
41 this.nickname = nickname;
42 this.channel = channel;
45 this.socket = new Socket(this.server, this.port);
46 this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
47 this.output = new PrintWriter(this.socket.getOutputStream());
48 } catch (Exception e) {
49 Os.debug("Client: failed to create connection: " + e);
53 Os.debug("Client: connected");
54 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname);
55 this.raw("NICK "+this.nickname);
60 public boolean abort()
62 Os.debug("Client: abort");
67 } catch (Exception e) {
68 Os.debug("Client: error closing socket", e);
73 public void raw(String line)
76 Os.debug("< " + line);
77 this.output.println(line);
79 } catch (Exception e) {
80 Os.debug("Client: error writing line", e);
84 public Message send(String txt)
86 Message msg = new Message(this.channel, this.nickname, txt);
94 String line = input.readLine();
97 Os.debug("> " + line);
98 Message msg = new Message(line);
101 } catch (SocketException e) {
104 } catch (Exception e) {
106 Os.debug("Client: error in recv", e);
111 /* Private methods */
112 private void process(Message msg)
114 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
115 this.raw("JOIN " + this.channel);
116 this.raw("TOPIC " + this.channel);
119 if (msg.cmd.equals("PING")) {
120 this.raw("PING " + msg.msg);