]> Pileus Git - ~andy/spades/blob - src/Client.java
Prevent crash with zero-length strings
[~andy/spades] / src / 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 (txt == null || txt.length() == 0)
122                         return null;
123                 if (this.validate() != State.READY)
124                         return null;
125                 Message msg = new Message(this.channel, this.name, txt);
126                 if (msg.type == Message.Type.JOIN)
127                         this.channel = msg.msg;
128                 this.raw(msg.line);
129                 return msg;
130         }
131
132         public Message recv()
133         {
134                 while (true) try {
135                         if (this.validate() != State.SETUP &&
136                             this.validate() != State.READY)
137                                 return null;
138                         String line = this.input.readLine();
139                         if (line == null)
140                                 return null;
141                         Os.debug("> " + line);
142                         Message msg = new Message(line, this.name);
143                         this.process(msg);
144                         if (this.usesasl)
145                                 this.dosasl(msg);
146                         return msg;
147                 }
148                 catch (SocketTimeoutException e) {
149                         if (this.pinging) {
150                                 this.abort();
151                                 return null;
152                         } else {
153                                 this.pinging = true;
154                                 this.raw("PING :" + hostname);
155                                 continue;
156                         }
157                 }
158                 catch (SocketException e) {
159                         this.state = State.INIT;
160                         return null;
161                 }
162                 catch (Exception e) {
163                         this.state = State.INIT;
164                         Os.debug("Client: error in recv", e);
165                         return null;
166                 }
167         }
168
169         /* Private methods */
170         private State validate()
171         {
172                 try {
173                         if (this.state == State.ABORT) {
174                                 if (this.socket != null) {
175                                         this.socket.close();
176                                         this.state = State.INIT;
177                                 }
178                         }
179                 } catch (Exception e) {
180                         Os.debug("Client: error closing socket", e);
181                 }
182                 return this.state;
183         }
184
185         private void process(Message msg)
186         {
187                 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
188                         this.raw("JOIN "  + this.channel);
189                         this.raw("TOPIC " + this.channel);
190                         this.state = State.READY;
191                 }
192                 if (msg.cmd.equals("433")) {
193                         this.name   = this.nickname + this.mangle;
194                         this.mangle = this.mangle + 11;
195                         this.raw("NICK "  + this.name);
196                 }
197                 if (msg.cmd.equals("PING")) {
198                         this.raw("PONG " + msg.msg);
199                 }
200                 if (msg.cmd.equals("PONG")) {
201                         this.pinging = false;
202                 }
203         }
204
205         private void dosasl(Message msg)
206         {
207                 switch (msg.type) {
208                         case CAP:
209                                 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
210                                         Os.debug("Client: sasl - starting auth");
211                                         this.raw("AUTHENTICATE PLAIN");
212                                 } else {
213                                         Os.debug("Client: sasl - Server does not support sasl");
214                                 }
215                                 break;
216                         case AUTH:
217                                 if (msg.arg.equals("+")) {
218                                         Os.debug("Client: sasl - performin authentication");
219                                         this.raw("AUTHENTICATE " + Os.base64(
220                                                                 this.authname + "\0" +
221                                                                 this.authname + "\0" +
222                                                                 this.password));
223                                 } else {
224                                         Os.debug("Client: sasl - unexpected authenticate response");
225                                 }
226                                 break;
227                         case AUTHOK:
228                                 Os.debug("Client: SASL Auth Successful");
229                                 this.raw("CAP END");
230                                 break;
231                         case AUTHFAIL:
232                                 Os.debug("Client: SASL Auth Failed");
233                                 this.raw("CAP END");
234                                 break;
235                 }
236         }
237 }