File: POPWeasel.java

package info (click to toggle)
airport-utils 2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,024 kB
  • ctags: 5,067
  • sloc: java: 30,844; xml: 571; sh: 553; makefile: 36
file content (360 lines) | stat: -rw-r--r-- 9,054 bytes parent folder | download | duplicates (5)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * IPInspector
 *
 * Copyright (C) 2002, Jonathan Sevy <jsevy@mcs.drexel.edu>
 *
 * 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 airporthostmon;
 
 /* class POPWeasel

Defines object that will communicate with POP and SMTP
servers to get and send e-mail (pardon the name)


Defines methods:

	public Vector getMessages(UserInfo user, String mailHost)
Retrieves messages from specified POP host, using supplied user ID and password.
Return as vector of Strings (one per message)

	public void sendMessages(UserInfo user, Vector messages, String mailHost)
Send the messages corresponding to the Vector supplied of Message objects,
using the specified SMTP host
	

*/


import java.util.*;
import java.net.*;
import java.io.*;



public class POPWeasel
{
	
	public static final int POPPORT = 110;
	public static final int SMTPPORT = 25;
	
	public static final boolean debug = false;
	
	
	
	public POPWeasel()
	{
	
	}
	
	public Vector getMessages(String userName, String password, String mailHost)
	    throws Exception
	{
		Socket mailSocket;
		BufferedReader istream;
		BufferedWriter ostream;
		Vector messageList = new Vector();
		
		
		mailSocket = new Socket(mailHost, POPWeasel.POPPORT);
		istream = new BufferedReader(new InputStreamReader(mailSocket.getInputStream()));
		ostream = new BufferedWriter(new OutputStreamWriter(mailSocket.getOutputStream()));
		
		establishPOPSession(userName, password, istream, ostream);
		messageList = retrieveMessages(istream, ostream);
		closePOPSession(istream, ostream);
		
		istream.close();
		ostream.close();
		mailSocket.close();
		
		
		return messageList;
		
	}
	
	
	
	
	public void sendMessages(String userEmailAddress, String mailHost, Vector messages)
	    throws Exception
	{
		Socket mailSocket;
		BufferedReader istream;
		BufferedWriter ostream;
		
		
		mailSocket = new Socket(mailHost, POPWeasel.SMTPPORT);
		istream = new BufferedReader(new InputStreamReader(mailSocket.getInputStream()));
		ostream = new BufferedWriter(new OutputStreamWriter(mailSocket.getOutputStream()));
		
		establishSMTPSession(mailHost, istream, ostream);
		sendMessages(userEmailAddress, messages, istream, ostream);
		closeSMTPSession(istream, ostream);
		
		istream.close();
		ostream.close();
		mailSocket.close();
			
		
		return;
		
	}
	
	
	// Utility method for sending a single email message
	public void sendMessage(String userEmailAddress, String mailHost, EmailMessage message)
	    throws Exception
	{
	    Vector messageVector = new Vector();
	    messageVector.add(message);
	    this.sendMessages(userEmailAddress, mailHost, messageVector);
	}
	
	
	
	private void checkPOPResponse(String response)
			throws IOException
	// utility method, to see if POP server sends happy response; throws a "fit"
	// (also known as an Exception) if response doesn't begin with "+OK"
	{
		StringTokenizer st;
		
		debugPrintln("Server: " + response);
		
		st = new StringTokenizer(response);
		if (!st.nextToken().equals("+OK"))
			throw new IOException(response);
	}
	
	
	
	private void establishPOPSession(String userName, String password, BufferedReader istream, BufferedWriter ostream)
			throws IOException
	// Send username and password to establish POP session
	{
		checkPOPResponse(istream.readLine());
		
		ostream.write("USER " + userName);
		ostream.newLine();
		ostream.flush();
		debugPrintln("Client: USER " + userName);
		checkPOPResponse(istream.readLine());
		
		ostream.write("PASS " + password);
		ostream.newLine();
		ostream.flush();
		debugPrintln("Client: PASS (hidden)");
		checkPOPResponse(istream.readLine());
		
		return;
	}
	
	
	
	
	private Vector retrieveMessages(BufferedReader istream, BufferedWriter ostream)
			throws IOException, NumberFormatException
	// Get waiting messages, sending STAT and RETR messages to POP host
	{
		Vector retrievedMessages = new Vector();
		StringTokenizer st;
		String nextLine;
		String nextToken;
		int numMessages;
		
		ostream.write("STAT");
		ostream.newLine();
		ostream.flush();
		debugPrintln("Client: STAT");
		
		nextLine = istream.readLine();
		debugPrintln("Server: " + nextLine);
		st = new StringTokenizer(nextLine);
		if (!st.nextToken().equals("+OK"))
			throw new IOException(nextLine);
		nextToken = st.nextToken();	// will be number of messages to retrieve
		numMessages = Integer.parseInt(nextToken);
		
		for (int i=1; i <= numMessages; ++i)
		{
			// get waiting messages
			
			String newMessage = new String();
			
			ostream.write("RETR " + i) ;
			ostream.newLine();
			ostream.flush();
			debugPrintln("Client: RETR " + i);
			
			checkPOPResponse(istream.readLine());
			
			while (!(nextLine = istream.readLine()).equals("."))
			{
				// add each line of message to message string
				newMessage += nextLine + "\n";
			}
			
			// add message to vector to be returned
			retrievedMessages.insertElementAt(newMessage, retrievedMessages.size());
		}
		
		
		return retrievedMessages;
		
	}
	
	
	
	
	private void closePOPSession(BufferedReader istream, BufferedWriter ostream)
			throws IOException
	{
		ostream.write("QUIT");
		ostream.newLine();
		ostream.flush();
		
	}
	
	
	
	
	private void checkSMTPResponse(String response, String expected)
			throws IOException
	// See if SMTP host returns string expected at current stage of communication;
	// if not, throw a fit (exception)
	{
		StringTokenizer st;
		
		debugPrintln("Server: " + response);
		
		st = new StringTokenizer(response);
		if (!st.nextToken().equals(expected))
			throw new IOException(response);
	}
	
	
	
	
	private void establishSMTPSession(String domainName, BufferedReader istream, BufferedWriter ostream)
			throws IOException
	{
		checkSMTPResponse(istream.readLine(), "220");
		
		ostream.write("HELO " + domainName);
		//ostream.newLine();
		ostream.write("\r\n");
		ostream.flush();
		debugPrintln("Client: HELO " + domainName);
		checkSMTPResponse(istream.readLine(), "250");
		
		return;
	}
			
	
	
	
	
	private void sendMessages(String userEmailAddress, Vector messages, BufferedReader istream, BufferedWriter ostream)
			throws IOException
	{
		Enumeration e = messages.elements();
		
		while (e.hasMoreElements())
		{
			// send next message
			EmailMessage m = (EmailMessage)e.nextElement();
			
			//try
			//{
				ostream.write("MAIL FROM:" + "<" + userEmailAddress + ">");
				//ostream.newLine();
				ostream.write("\r\n");
		        ostream.flush();
				debugPrintln("Client: MAIL FROM:" + "<" + userEmailAddress + ">");
				checkSMTPResponse(istream.readLine(), "250");
				
				Enumeration addresses = (m.recipients).elements();
				while(addresses.hasMoreElements())
				{
					String recipient = (String)addresses.nextElement();
					ostream.write("RCPT TO:" + "<" + recipient + ">");
					//ostream.newLine();
				    ostream.write("\r\n");
		            ostream.flush();
					debugPrintln("Client: RCPT TO:" + "<" + recipient + ">");
					checkSMTPResponse(istream.readLine(), "250");
				}
				
				ostream.write("DATA");
				//ostream.newLine();
				ostream.write("\r\n");
		        ostream.flush();
				debugPrintln("Client: DATA");
				checkSMTPResponse(istream.readLine(), "354");
				
				ostream.write(m.text);
				//ostream.newLine();
				ostream.write("\r\n");
		        //ostream.flush();
				debugPrintln("Message text sent");
				ostream.write(".");     // CR-LF  = 0x0D 0x0A = octal 015 012
				//ostream.newLine();
				ostream.write("\r\n");
		        //ostream.newLine();
				ostream.flush();
				//debugPrintln(".");
				checkSMTPResponse(istream.readLine(), "250");
			//}
			//catch (Exception except)
			//{
			//	debugPrintln("Problem sending email message: " + except);
			//}
			
		}
		
		return;
	}
	
	
	
	
	private void closeSMTPSession(BufferedReader istream, BufferedWriter ostream)
			throws IOException
	{
		ostream.write("QUIT");
		//ostream.newLine();
		ostream.write("\r\n");
		ostream.flush();
		debugPrintln("Client: QUIT");
	}
	
	
	
	private void debugPrintln(String message)
	{
	    if (this.debug)
	        System.out.println(message);
	}
			
	
	
	
	
	
}