1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
//---PACKAGE DECLARATION--- |
21 |
|
package uk.org.iscream.cms.server.util; |
22 |
|
|
25 |
|
import java.io.*; |
26 |
|
|
27 |
|
/** |
28 |
< |
* The Simple Mail Transfer Protocol class. This class was borrowed |
29 |
< |
* for the GJT, and scaled to do just what we require. |
28 |
> |
* The Simple Mail Transfer Protocol class. This class was inspired |
29 |
> |
* by the GJT. |
30 |
|
* |
31 |
|
* @author $Author$ |
32 |
|
* @version $Id$ |
66 |
|
_socket = new Socket(server, port); |
67 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
68 |
|
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
69 |
< |
_socketIn.readLine(); // get 220 welcome header |
51 |
< |
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
69 |
> |
init(); |
70 |
|
} |
71 |
|
|
72 |
|
//---PUBLIC METHODS--- |
134 |
|
public void sendCommand(String cmd, int reply) throws IOException { |
135 |
|
_socketOut.println(cmd); |
136 |
|
String temp = _socketIn.readLine(); |
137 |
< |
if (!temp.startsWith(new Integer(reply).toString())) { |
137 |
> |
if (temp == null) { |
138 |
> |
throw new IOException ("IO error reading from socket, connection to server died?"); |
139 |
> |
} |
140 |
> |
if (!temp.startsWith(String.valueOf(reply))) { |
141 |
|
throw new IOException ("Expected " + reply + ", got " + temp); |
142 |
|
} |
143 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
//---PRIVATE METHODS--- |
162 |
+ |
|
163 |
+ |
/** |
164 |
+ |
* Check the server sends a 220 message, and |
165 |
+ |
* then send our HELO. |
166 |
+ |
* |
167 |
+ |
* @throws IOException if something goes wrong |
168 |
+ |
*/ |
169 |
+ |
private void init() throws IOException { |
170 |
+ |
String temp = _socketIn.readLine(); |
171 |
+ |
// skip over any 220- lines |
172 |
+ |
while(temp != null && temp.startsWith("220-")) { |
173 |
+ |
temp = _socketIn.readLine(); |
174 |
+ |
} |
175 |
+ |
// check if we got a 220 welcome header |
176 |
+ |
if(temp == null || !temp.startsWith("220 ")) { |
177 |
+ |
throw new IOException ("Server did not initialise with a 220 message"); |
178 |
+ |
} |
179 |
+ |
// server gave a 220, it's ok to do a HELO |
180 |
+ |
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
181 |
+ |
} |
182 |
|
|
183 |
|
//---ACCESSOR/MUTATOR METHODS--- |
184 |
|
|