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 Client(String username, String hostname)
23 this.username = username;
24 this.hostname = hostname;
25 Os.debug("Client: create");
30 this("user", "localhost");
33 public boolean connect(String server, String nickname, String channel)
35 Os.debug("Client: connect");
38 this.nickname = nickname;
39 this.channel = channel;
42 socket = new Socket(this.server, this.port);
43 input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
44 output = new PrintWriter(socket.getOutputStream());
45 } catch (Exception e) {
46 Os.debug("Client: failed to create connection: " + e);
50 Os.debug("Client: connected");
51 raw("USER "+username+" "+hostname+" "+server+" :"+nickname);
52 raw("NICK "+nickname);
57 public boolean abort()
59 Os.debug("Client: abort");
63 } catch (Exception e) {
64 Os.debug("Client: error closing socket", e);
69 public void raw(String line)
72 Os.debug("< " + line);
75 } catch (Exception e) {
76 Os.debug("Client: error writing line", e);
80 public Message send(String txt)
82 Message msg = new Message(channel, nickname, txt);
90 String line = input.readLine();
93 Os.debug("> " + line);
94 Message msg = new Message(line);
97 } catch (SocketException e) {
99 } catch (Exception e) {
100 Os.debug("Client: error in recv", e);
105 /* Private methods */
106 private void process(Message msg)
108 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
109 raw("JOIN " + channel);
110 raw("TOPIC " + channel);
112 if (msg.cmd.equals("PING")) {
113 raw("PING " + msg.msg);