--- projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java 2001/02/05 20:46:28 1.3 +++ projects/cms/source/util/uk/org/iscream/cms/util/Smtp.java 2002/05/18 18:16:04 1.9 @@ -1,16 +1,35 @@ +/* + * i-scream central monitoring system + * Copyright (C) 2000-2002 i-scream + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.util; +package uk.org.iscream.cms.server.util; //---IMPORTS--- import java.net.*; import java.io.*; /** - * The Simple Mail Transfer Protocol class. This class was borrowed - * for the GJT, and scaled to do just what we require. + * The Simple Mail Transfer Protocol class. This class was inspired + * by the GJT. * * @author $Author: tdb $ - * @version $Id: Smtp.java,v 1.3 2001/02/05 20:46:28 tdb Exp $ + * @version $Id: Smtp.java,v 1.9 2002/05/18 18:16:04 tdb Exp $ */ public class Smtp { @@ -19,7 +38,7 @@ public class Smtp { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.3 $"; + public static final String REVISION = "$Revision: 1.9 $"; //---STATIC METHODS--- @@ -47,8 +66,7 @@ public class Smtp { _socket = new Socket(server, port); _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); _socketOut = new PrintWriter(_socket.getOutputStream(), true); - _socketIn.readLine(); // get 220 welcome header - sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); + init(); } //---PUBLIC METHODS--- @@ -116,7 +134,10 @@ public class Smtp { public void sendCommand(String cmd, int reply) throws IOException { _socketOut.println(cmd); String temp = _socketIn.readLine(); - if (!temp.startsWith(new String(reply))) { + if (temp == null) { + throw new IOException ("IO error reading from socket, connection to server died?"); + } + if (!temp.startsWith(String.valueOf(reply))) { throw new IOException ("Expected " + reply + ", got " + temp); } } @@ -125,7 +146,7 @@ public class Smtp { * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * - * This uses the uk.ac.ukc.iscream.util.FormatName class + * This uses the uk.org.iscream.cms.server.util.FormatName class * to format the toString() * * @return the name of this class and its CVS revision @@ -138,6 +159,26 @@ public class Smtp { } //---PRIVATE METHODS--- + + /** + * Check the server sends a 220 message, and + * then send our HELO. + * + * @throws IOException if something goes wrong + */ + private void init() throws IOException { + String temp = _socketIn.readLine(); + // skip over any 220- lines + while(temp != null && temp.startsWith("220-")) { + temp = _socketIn.readLine(); + } + // check if we got a 220 welcome header + if(temp == null || !temp.startsWith("220 ")) { + throw new IOException ("Server did not initialise with a 220 message"); + } + // server gave a 220, it's ok to do a HELO + sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); + } //---ACCESSOR/MUTATOR METHODS---