]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Client.java
693ec551b19fc0f11e30800aecf37d745e190724
[~andy/spades] / src / org / pileus / spades / Client.java
1 package org.pileus.spades;
2
3 import java.io.*;
4 import java.net.*;
5
6 public class Client
7 {
8         /* Private data */
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;
15
16         private Socket         socket   = null;
17         private BufferedReader input    = null;
18         private PrintWriter    output   = null;
19
20         /* Public data */
21         public  boolean        ready    = false;
22
23         /* Public Methods */
24         public Client(String username, String hostname)
25         {
26                 this.username = username;
27                 this.hostname = hostname;
28                 Os.debug("Client: create");
29         }
30
31         public Client()
32         {
33                 this("user", "localhost");
34         }
35
36         public boolean connect(String server, String nickname, String channel)
37         {
38                 Os.debug("Client: connect");
39
40                 this.server   = server;
41                 this.nickname = nickname;
42                 this.channel  = channel;
43
44                 try {
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);
50                         return false;
51                 }
52
53                 Os.debug("Client: connected");
54                 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname);
55                 this.raw("NICK "+this.nickname);
56
57                 return true;
58         }
59
60         public boolean abort()
61         {
62                 Os.debug("Client: abort");
63                 try {
64                         this.socket.close();
65                         this.ready = false;
66                         return true;
67                 } catch (Exception e) {
68                         Os.debug("Client: error closing socket", e);
69                         return false;
70                 }
71         }
72
73         public void raw(String line)
74         {
75                 try {
76                         Os.debug("< " + line);
77                         this.output.println(line);
78                         this.output.flush();
79                 } catch (Exception e) {
80                         Os.debug("Client: error writing line", e);
81                 }
82         }
83
84         public Message send(String txt)
85         {
86                 Message msg  = new Message(this.channel, this.nickname, txt);
87                 this.raw(msg.line);
88                 return msg;
89         }
90
91         public Message recv()
92         {
93                 try {
94                         String line = input.readLine();
95                         if (line == null)
96                                 return null;
97                         Os.debug("> " + line);
98                         Message msg = new Message(line);
99                         this.process(msg);
100                         return msg;
101                 } catch (SocketException e) {
102                         this.ready = false;
103                         return null;
104                 } catch (Exception e) {
105                         this.ready = false;
106                         Os.debug("Client: error in recv", e);
107                         return null;
108                 }
109         }
110
111         /* Private methods */
112         private void process(Message msg)
113         {
114                 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
115                         this.raw("JOIN "  + this.channel);
116                         this.raw("TOPIC " + this.channel);
117                         this.ready = true;
118                 }
119                 if (msg.cmd.equals("PING")) {
120                         this.raw("PING " + msg.msg);
121                 }
122         }
123 }