1 21 22 package org.armedbear.j.mail; 23 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.net.Socket ; 27 import java.net.SocketException ; 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 private static final int DISCONNECTED = 0; 38 private static final int AUTHORIZATION = 1; 39 private static final int TRANSACTION = 2; 40 41 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 host; 48 private final String user; 49 private String password; 50 private int port; 51 private Socket socket; 52 private MailReader reader; 53 private OutputStreamWriter writer; 54 private String errorText; 55 56 private PopSession(PopURL url, String user, String 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 getHost() 65 { 66 return host; 67 } 68 69 public final int getPort() 70 { 71 return port; 72 } 73 74 public final String getUser() 75 { 76 return user; 77 } 78 79 public final String getPassword() 80 { 81 return password; 82 } 83 84 public final void setPassword(String 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 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 user = url.getUser(); 109 if (user == null) 110 user = System.getProperty("user.name"); 111 String 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 user) 118 { 119 String 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 user, String 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); state = AUTHORIZATION; 144 try { 145 reader = new MailReader(socket.getInputStream()); 146 writer = new OutputStreamWriter (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 e) { 162 Log.error(e); 163 } 164 disconnect(); 166 errorText = "Login failed"; 167 setEcho(false); 168 return false; 169 } 170 171 private synchronized void setTimeout(int ms) 173 { 174 if (socket != null) { 175 try { 176 socket.setSoTimeout(ms); 177 } 178 catch (SocketException 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 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 readLine() 218 { 219 try { 220 String s = reader.readLine(); 221 if (echo && s != null) 222 Log.debug("<== " + s); 223 return s; 224 } 225 catch (IOException e) { 226 Log.error(e); 227 disconnect(); 228 return null; 229 } 230 } 231 232 public synchronized boolean write(String 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 e) { 245 Log.error(e); 246 disconnect(); 247 return false; 248 } 249 } 250 251 public synchronized int getResponse() 252 { 253 while (true) { 254 String 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 |