]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Client.java
Add connection timeout handling
[~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         /* Constnats */
9         enum State {
10                 INIT,
11                 SETUP,
12                 READY,
13                 ABORT,
14         };
15
16         /* Preference data */
17         public  String         server   = "irc.freenode.net";
18         public  int            port     = 6667;
19         public  int            timeout  = 240;
20         public  String         nickname = "SpadeGuest";
21         public  String         channel  = "#rhnoise";
22         public  boolean        usesasl  = false;
23         public  String         authname = "";
24         public  String         password = "";
25         public  String         username = "user";
26         public  String         hostname = "localhost";
27
28         /* Public data */
29         public  State          state    = State.INIT;
30         public  String         name     = "";
31
32         /* Connection data */
33         private boolean        pinging;
34         private Socket         socket;
35         private BufferedReader input;
36         private PrintWriter    output;
37
38         /* Private data */
39         private int            mangle;
40
41         /* Public Methods */
42         public Client()
43         {
44                 Os.debug("Client: create");
45         }
46
47         public void setServer(String server, int port)
48         {
49                 this.server = server;
50                 this.port   = port;
51         }
52
53         public void setAuth(boolean usesasl, String authname, String password)
54         {
55                 this.usesasl  = usesasl;
56                 this.authname = authname;
57                 this.password = password;
58         }
59
60         public void setUser(String nickname, String channel)
61         {
62                 this.nickname = nickname;
63                 this.channel  = channel;
64                 this.name     = nickname;
65         }
66
67         public boolean connect()
68         {
69                 Os.debug("Client: connect");
70
71                 try {
72                         this.state  = State.SETUP;
73                         this.socket = new Socket();
74                         this.socket.setSoTimeout(this.timeout/2 * 1000);
75                         this.socket.connect(new InetSocketAddress(this.server, this.port));
76                         this.input  = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
77                         this.output = new PrintWriter(this.socket.getOutputStream());
78                 } catch (Exception e) {
79                         Os.debug("Client: failed to create connection: " + e);
80                         return false;
81                 }
82
83                 Os.debug("Client: connected");
84                 if (this.usesasl)
85                         this.raw("CAP REQ :sasl");
86                 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.name);
87                 this.raw("NICK "+this.name);
88
89                 return true;
90         }
91
92         public void abort()
93         {
94                 Os.debug("Client: abort");
95                 this.state = State.ABORT;
96                 this.validate();
97         }
98
99         public void reset()
100         {
101                 Os.debug("Client: reset");
102                 this.state = State.INIT;
103         }
104
105         public void raw(String line)
106         {
107                 try {
108                         if (this.validate() != State.SETUP &&
109                             this.validate() != State.READY)
110                                 return;
111                         Os.debug("< " + line);
112                         this.output.println(line);
113                         this.output.flush();
114                 } catch (Exception e) {
115                         Os.debug("Client: error writing line", e);
116                 }
117         }
118
119         public Message send(String txt)
120         {
121                 if (this.validate() != State.READY)
122                         return null;
123                 Message msg = new Message(this.channel, this.name, txt);
124                 if (msg.type == Message.Type.JOIN)
125                         this.channel = msg.msg;
126                 this.raw(msg.line);
127                 return msg;
128         }
129
130         public Message recv()
131         {
132                 while (true) try {
133                         if (this.validate() != State.SETUP &&
134                             this.validate() != State.READY)
135                                 return null;
136                         String line = this.input.readLine();
137                         if (line == null)
138                                 return null;
139                         Os.debug("> " + line);
140                         Message msg = new Message(line, this.name);
141                         this.process(msg);
142                         if (this.usesasl)
143                                 this.dosasl(msg);
144                         return msg;
145                 }
146                 catch (SocketTimeoutException e) {
147                         if (this.pinging) {
148                                 this.abort();
149                                 return null;
150                         } else {
151                                 this.pinging = true;
152                                 this.raw("PING :" + hostname);
153                                 continue;
154                         }
155                 }
156                 catch (SocketException e) {
157                         this.state = State.INIT;
158                         return null;
159                 }
160                 catch (Exception e) {
161                         this.state = State.INIT;
162                         Os.debug("Client: error in recv", e);
163                         return null;
164                 }
165         }
166
167         /* Private methods */
168         private State validate()
169         {
170                 try {
171                         if (this.state == State.ABORT) {
172                                 if (this.socket != null) {
173                                         this.socket.close();
174                                         this.state = State.INIT;
175                                 }
176                         }
177                 } catch (Exception e) {
178                         Os.debug("Client: error closing socket", e);
179                 }
180                 return this.state;
181         }
182
183         private void process(Message msg)
184         {
185                 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
186                         this.raw("JOIN "  + this.channel);
187                         this.raw("TOPIC " + this.channel);
188                         this.state = State.READY;
189                 }
190                 if (msg.cmd.equals("433")) {
191                         this.name   = this.nickname + this.mangle;
192                         this.mangle = this.mangle + 11;
193                         this.raw("NICK "  + this.name);
194                 }
195                 if (msg.cmd.equals("PING")) {
196                         this.raw("PONG " + msg.msg);
197                 }
198                 if (msg.cmd.equals("PONG")) {
199                         this.pinging = false;
200                 }
201         }
202
203         private void dosasl(Message msg)
204         {
205                 switch (msg.type) {
206                         case CAP:
207                                 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
208                                         Os.debug("Client: sasl - starting auth");
209                                         this.raw("AUTHENTICATE PLAIN");
210                                 } else {
211                                         Os.debug("Client: sasl - Server does not support sasl");
212                                 }
213                                 break;
214                         case AUTH:
215                                 if (msg.arg.equals("+")) {
216                                         Os.debug("Client: sasl - performin authentication");
217                                         this.raw("AUTHENTICATE " + Os.base64(
218                                                                 this.authname + "\0" +
219                                                                 this.authname + "\0" +
220                                                                 this.password));
221                                 } else {
222                                         Os.debug("Client: sasl - unexpected authenticate response");
223                                 }
224                                 break;
225                         case AUTHOK:
226                                 Os.debug("Client: SASL Auth Successful");
227                                 this.raw("CAP END");
228                                 break;
229                         case AUTHFAIL:
230                                 Os.debug("Client: SASL Auth Failed");
231                                 this.raw("CAP END");
232                                 break;
233                 }
234         }
235 }