File: SmartClient.java

package info (click to toggle)
subethasmtp 4.0~rc6-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 964 kB
  • sloc: java: 6,330; xml: 322; sh: 12; makefile: 2
file content (277 lines) | stat: -rw-r--r-- 9,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package org.subethamail.smtp.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.subethamail.smtp.client.SMTPClient.Response;

import com.google.common.base.Preconditions;

/**
 * A somewhat smarter abstraction of an SMTP client which doesn't require
 * knowing anything about the nitty gritty of SMTP.
 *
 * @author Jeff Schnitzer
 */
// not final so can mock with Mockito
public class SmartClient {

    private static final Logger log = LoggerFactory.getLogger(SmartClient.class);

    /** The host name which is sent in the HELO and EHLO commands */
    private final String heloHost;
    
    /**
     * SMTP extensions supported by the server, and their parameters as the
     * server specified it in response to the EHLO command. Key is the extension
     * keyword in upper case, like "AUTH", value is the extension parameters
     * string in unparsed form. If the server does not support EHLO, then this
     * map is empty.
     */
    private final Map<String, String> extensions = new HashMap<String, String>();

    /**
     * If supplied (not null), then it will be called after EHLO, to
     * authenticate this client to the server.
     */
    private final Optional<Authenticator> authenticator;

    private final SMTPClient client;

    //mutable state
    
    private int recipientCount;

    /**
     * True if the server sent a 421
     * "Service not available, closing transmission channel" response. In this
     * case the QUIT command should not be sent.
     */
    private boolean serverClosingTransmissionChannel = false;

    
    /**
     * Connects to the specified server and issues the initial HELO command.
     * 
     * @throws UnknownHostException
     *             if problem looking up hostname
     * @throws SMTPException
     *             if problem reported by the server
     * @throws IOException
     *             if problem communicating with host
     */
    private SmartClient(String myHost) throws UnknownHostException, IOException, SMTPException {
        this(Optional.empty(), myHost, Optional.empty());
    }

    /**
     * Constructor.
     * 
     * @param bindpoint
     * @param clientHeloHost
     * @param authenticator
     *            the Authenticator object which will be called after the EHLO
     *            command to authenticate this client to the server. If is null
     *            then no authentication will happen.
     * @throws UnknownHostException
     *             if problem looking up hostname
     * @throws IOException
     *             if problem communicating with host
     * @throws SMTPException
     *             if problem reported by the server
     */
    private SmartClient(Optional<SocketAddress> bindpoint, String clientHeloHost, Optional<Authenticator> authenticator)
            throws UnknownHostException, IOException, SMTPException {
        Preconditions.checkNotNull(bindpoint, "bindpoint cannot be null");
        Preconditions.checkNotNull(clientHeloHost, "clientHeloHost cannot be null");
        Preconditions.checkNotNull(authenticator, "authenticator cannot be null");
        this.client = new SMTPClient(bindpoint, Optional.empty());
        this.heloHost = clientHeloHost;
        this.authenticator = authenticator;
    }

    public final static SmartClient createAndConnect(String host, int port, String clientHeloHost)
            throws UnknownHostException, SMTPException, IOException {
        return createAndConnect(host, port, Optional.empty(), clientHeloHost, Optional.empty());
    }

    public final static SmartClient createAndConnect(String host, int port, Optional<SocketAddress> bindpoint,
            String clientHeloHost, Optional<Authenticator> authenticator)
                    throws UnknownHostException, SMTPException, IOException {
        SmartClient client = new SmartClient(bindpoint, clientHeloHost, authenticator);
        client.connect(host, port);
        return client;
    }

    /**
     * Connects to the specified server and issues the initial HELO command. It
     * gracefully closes the connection if it could be established but
     * subsequently it fails or if the server does not accept messages.
     */
    public void connect(String host, int port)
            throws SMTPException, AuthenticationNotSupportedException, IOException {
        client.connect(host, port);
        try {
            client.receiveAndCheck(); // The server announces itself first
            this.sendHeloOrEhlo();
            if (this.authenticator.isPresent())
                this.authenticator.get().authenticate();
        } catch (SMTPException e) {
            this.quit();
            throw e;
        } catch (AuthenticationNotSupportedException e) {
            this.quit();
            throw e;
        } catch (IOException e) {
            client.close(); // just close the socket, issuing QUIT is hopeless
                            // now
            throw e;
        }
    }

    /**
     * Sends the EHLO command, or HELO if EHLO is not supported, and saves the
     * list of SMTP extensions which are supported by the server.
     */
    protected void sendHeloOrEhlo() throws IOException, SMTPException {
        extensions.clear();
        Response resp = client.sendReceive("EHLO " + heloHost);
        if (resp.isSuccess()) {
            parseEhloResponse(resp);
        } else if (resp.getCode() == 500 || resp.getCode() == 502) {
            // server does not support EHLO, try HELO
            client.sendAndCheck("HELO " + heloHost);
        } else {
            // some serious error
            throw new SMTPException(resp);
        }
    }

    /**
     * Extracts the list of SMTP extensions from the server's response to EHLO,
     * and stores them in {@link #extensions}.
     */
    private void parseEhloResponse(Response resp) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader(resp.getMessage()));
        // first line contains server name and welcome message, skip it
        reader.readLine();
        String line;
        while (null != (line = reader.readLine())) {
            int iFirstSpace = line.indexOf(' ');
            String keyword = iFirstSpace == -1 ? line : line.substring(0, iFirstSpace);
            String parameters = iFirstSpace == -1 ? "" : line.substring(iFirstSpace + 1);
            extensions.put(keyword.toUpperCase(Locale.ENGLISH), parameters);
        }
    }

    /**
     * Returns the server response. It takes note of a 421 response code, so
     * QUIT will not be issued unnecessarily.
     */
    protected Response receive() throws IOException {
        Response response = client.receive();
        if (response.getCode() == 421)
            serverClosingTransmissionChannel = true;
        return response;
    }

    public void from(String from) throws IOException, SMTPException {
        client.sendAndCheck("MAIL FROM: <" + from + ">");
    }

    public void to(String to) throws IOException, SMTPException {
        client.sendAndCheck("RCPT TO: <" + to + ">");
        this.recipientCount++;
    }

    /**
     * Prelude to writing data
     */
    public void dataStart() throws IOException, SMTPException {
        client.sendAndCheck("DATA");
    }

    /**
     * Actually write some data
     */
    public void dataWrite(byte[] data, int numBytes) throws IOException {
        client.dataOutput.write(data, 0, numBytes);
    }

    /**
     * Last step after writing data
     */
    public void dataEnd() throws IOException, SMTPException {
        client.dataOutput.flush();
        client.dotTerminatedOutput.writeTerminatingSequence();
        client.dotTerminatedOutput.flush();

        client.receiveAndCheck();
    }

    /**
     * Quit and close down the connection. Ignore any errors.
     * <p>
     * It still closes the connection, but it does not send the QUIT command if
     * a 421 Service closing transmission channel is received previously. In
     * these cases QUIT would fail anyway.
     * 
     * @see <a href="http://tools.ietf.org/html/rfc5321#section-3.8">RFC 5321
     *      Terminating Sessions and Connections</a>
     */
    public void quit() {
        try {
            if (client.isConnected() && !this.serverClosingTransmissionChannel)
                client.sendAndCheck("QUIT");
        } catch (IOException ex) {
            log.warn("Failed to issue QUIT to " + client.getHostPort());
        }

        client.close();
    }

    /**
     * @return the number of recipients that have been accepted by the server
     */
    public int getRecipientCount() {
        return this.recipientCount;
    }

    /**
     * Returns the SMTP extensions supported by the server.
     * 
     * @return the extension map. Key is the extension keyword in upper case,
     *         value is the unparsed string of extension parameters.
     */
    public Map<String, String> getExtensions() {
        return extensions;
    }

    /**
     * Returns the HELO name of this system.
     */
    public String getHeloHost() {
        return heloHost;
    }

    /**
     * Returns the Authenticator object, which is used to authenticate this
     * client to the server, or null, if no authentication is required.
     */
    public Optional<Authenticator> getAuthenticator() {
        return authenticator;
    }

    public void sendAndCheck(String msg) throws SMTPException, IOException {
        client.sendAndCheck(msg);
    }
}