KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > PopSession


1 /*
2  * PopSession.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: PopSession.java,v 1.1.1.1 2002/09/24 16:09:55 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.SocketException JavaDoc;
28 import org.armedbear.j.Log;
29 import org.armedbear.j.Netrc;
30 import org.armedbear.j.SocketConnection;
31
32 public final class PopSession
33 {
34     private static final int DEFAULT_PORT = 110;
35
36     // States.
37
private static final int DISCONNECTED = 0;
38     private static final int AUTHORIZATION = 1;
39     private static final int TRANSACTION = 2;
40
41     // Responses.
42
public static final int OK = 0;
43     public static final int ERR = 1;
44
45     private int state;
46     private boolean echo = false;
47     private String JavaDoc host;
48     private final String JavaDoc user;
49     private String JavaDoc password;
50     private int port;
51     private Socket JavaDoc socket;
52     private MailReader reader;
53     private OutputStreamWriter JavaDoc writer;
54     private String JavaDoc errorText;
55
56     private PopSession(PopURL url, String JavaDoc user, String JavaDoc password)
57     {
58         this.host = url.getHost();
59         this.port = url.getPort();
60         this.user = user;
61         this.password = password;
62     }
63
64     public final String JavaDoc getHost()
65     {
66         return host;
67     }
68
69     public final int getPort()
70     {
71         return port;
72     }
73
74     public final String JavaDoc getUser()
75     {
76         return user;
77     }
78
79     public final String JavaDoc getPassword()
80     {
81         return password;
82     }
83
84     public final void setPassword(String JavaDoc password)
85     {
86         this.password = password;
87     }
88
89     public final void setEcho(boolean b)
90     {
91         echo = b;
92     }
93
94     public final boolean getEcho()
95     {
96         return echo;
97     }
98
99     public final String JavaDoc getErrorText()
100     {
101         return errorText;
102     }
103
104     public static PopSession getSession(PopURL url)
105     {
106         if (url.getHost() == null)
107             return null;
108         String JavaDoc user = url.getUser();
109         if (user == null)
110             user = System.getProperty("user.name");
111         String JavaDoc password = Netrc.getPassword(url.getHost(), user);
112         if (password == null)
113             return null;
114         return new PopSession(url, user, password);
115     }
116
117     public static PopSession getSession(PopURL url, String JavaDoc user)
118     {
119         String JavaDoc password = Netrc.getPassword(url.getHost(), user);
120         if (password == null)
121             return null;
122         return new PopSession(url, user, password);
123     }
124
125     public static PopSession getSession(PopURL url, String JavaDoc user, String JavaDoc password)
126     {
127         return new PopSession(url, user, password);
128     }
129
130     public boolean connect()
131     {
132         setEcho(true);
133         socket = null;
134         errorText = null;
135         SocketConnection sc =
136             new SocketConnection(host, port, 30000, 200, null);
137         socket = sc.connect();
138         if (socket == null) {
139             errorText = sc.getErrorText();
140             return false;
141         }
142         setTimeout(30000); // 30-second timeout
143
state = AUTHORIZATION;
144         try {
145             reader = new MailReader(socket.getInputStream());
146             writer = new OutputStreamWriter JavaDoc(socket.getOutputStream());
147             if (readLine() != null) {
148                 if (write("user " + user)) {
149                     if (getResponse() == OK) {
150                         if (write("pass " + password)) {
151                             if (getResponse() == OK) {
152                                 state = TRANSACTION;
153                                 setEcho(false);
154                                 return true;
155                             }
156                         }
157                     }
158                 }
159             }
160         }
161         catch (IOException JavaDoc e) {
162             Log.error(e);
163         }
164         // Something went astray.
165
disconnect();
166         errorText = "Login failed";
167         setEcho(false);
168         return false;
169     }
170
171     // Set specified timeout (in milliseconds).
172
private synchronized void setTimeout(int ms)
173     {
174         if (socket != null) {
175             try {
176                 socket.setSoTimeout(ms);
177             }
178             catch (SocketException JavaDoc e) {
179                 Log.error(e);
180             }
181         } else
182             Log.debug("PopSession.setTimeout socket is null");
183     }
184
185     public synchronized boolean logout()
186     {
187         Log.debug("PopSession.logout");
188         boolean succeeded = false;
189         if (state > DISCONNECTED) {
190             setEcho(true);
191             write("quit");
192             if (getResponse() == OK)
193                 succeeded = true;
194         }
195         disconnect();
196         return succeeded;
197     }
198
199     public void disconnect()
200     {
201         if (socket != null) {
202             try {
203                 socket.close();
204             }
205             catch (IOException JavaDoc e) {
206                 Log.error(e);
207             }
208         }
209         state = DISCONNECTED;
210     }
211
212     protected void finalize()
213     {
214         Log.debug("PopSession.finalize");
215     }
216
217     public synchronized String JavaDoc readLine()
218     {
219         try {
220             String JavaDoc s = reader.readLine();
221             if (echo && s != null)
222                 Log.debug("<== " + s);
223             return s;
224         }
225         catch (IOException JavaDoc e) {
226             Log.error(e);
227             disconnect();
228             return null;
229         }
230     }
231
232     public synchronized boolean write(String JavaDoc s)
233     {
234         if (writer == null)
235             return false;
236         if (echo)
237             Log.debug("==> " + (s.startsWith("pass ") ? "pass" : s));
238         s += "\r\n";
239         try {
240             writer.write(s);
241             writer.flush();
242             return true;
243         }
244         catch (IOException JavaDoc e) {
245             Log.error(e);
246             disconnect();
247             return false;
248         }
249     }
250
251     public synchronized int getResponse()
252     {
253         while (true) {
254             String JavaDoc s = readLine();
255             if (s == null) {
256                 state = DISCONNECTED;
257                 return ERR;
258             }
259             if (s.startsWith("+OK"))
260                 return OK;
261             if (s.startsWith("-ERR"))
262                 return ERR;
263         }
264     }
265 }
266
Popular Tags