| 1 package com.quadcap.pop3.client; 2 3 40 41 import java.util.Enumeration ; 42 import java.util.Vector ; 43 44 import java.net.Socket ; 45 import java.net.UnknownHostException ; 46 47 import java.io.BufferedInputStream ; 48 import java.io.BufferedOutputStream ; 49 import java.io.ByteArrayOutputStream ; 50 import java.io.FileOutputStream ; 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 import java.io.OutputStream ; 54 55 import com.quadcap.util.Debug; 56 import com.quadcap.util.Util; 57 58 import com.quadcap.io.DotStuffInputStream; 59 import com.quadcap.io.LogInputStream; 60 import com.quadcap.io.LogOutputStream; 61 62 63 69 public class Session { 70 73 String host = null; 74 77 int port = -1; 78 79 82 Socket socket = null; 83 84 87 InputStream in = null; 88 89 92 OutputStream out = null; 93 94 97 ByteArrayOutputStream resp = new ByteArrayOutputStream (); 98 99 102 String response = null; 103 104 112 public Session(String host, int port) { 113 this.host = host; 114 this.port = port; 115 } 116 117 121 public int connect() throws IOException , UnknownHostException { 122 socket = new Socket (host, port); 123 in = new BufferedInputStream (socket.getInputStream()); 125 out = new BufferedOutputStream (socket.getOutputStream()); 126 127 if (true) { 128 FileOutputStream log = new FileOutputStream ("pop3.log", true); 129 in = new LogInputStream(in, log, "S: "); 130 out = new LogOutputStream(out, log, "C: "); 131 } 132 int ret = getResponse(); 133 return ret; 134 } 135 136 137 public static final int CR = '\r'; 138 139 public static final int LF = '\n'; 140 141 142 public static final int OK = 0; 143 144 public static final int ERR = 1; 145 146 152 public int getResponse() throws IOException { 153 out.flush(); 154 resp.reset(); 155 int state = 0; 156 ByteArrayOutputStream bo = new ByteArrayOutputStream (); 157 while (state < 2) { 158 int c = in.read(); 159 if (c < 0) { 160 throw new IOException ("End of file in getResponse()"); 161 } 162 bo.write(c); 163 resp.write(c); 164 switch(state) { 165 case 0: 166 if (c == CR) state = 1; 167 break; 168 case 1: 169 if (c == LF) state = 2; 170 break; 171 } 172 } 173 response = resp.toString(); 174 if (response.indexOf("+OK") == 0) { 175 return OK; 176 } else if (response.indexOf("-ERR") == 0) { 177 return ERR; 178 } else { 179 Debug.println("getResponse: ???"); 180 return ERR; 181 } 182 } 183 184 188 public InputStream getResponse(boolean get) throws IOException { 189 int ret = getResponse(); 190 return (get && ret == OK) ? new DotStuffInputStream(in) : null; 191 } 192 193 194 private static final byte[] CRLF = { CR, LF }; 195 196 201 void writeCmd(byte[]cmd, String val) throws IOException { 202 out.write(cmd); 203 out.write(Util.bytes(val)); 204 out.write(CRLF); 205 } 206 207 210 int simpleCmd(byte[] cmd, String val) throws IOException { 211 writeCmd(cmd, val); 212 return getResponse(); 213 } 214 215 218 InputStream responseCmd(byte[] cmd, String val) throws IOException { 219 writeCmd(cmd, val); 220 return getResponse(true); 221 } 222 223 226 Vector vectorCmd(byte[] cmd, String val) throws IOException { 227 writeCmd(cmd, val); 228 int ret = getResponse(); 229 return Util.split(response, ' '); 230 } 231 232 233 private static final byte[] userBytes = {(byte)'U',(byte)'S', 234 (byte)'E',(byte)'R', 235 (byte)' '}; 236 public int user(String name) throws IOException { 237 return simpleCmd(userBytes, name); 238 } 239 240 241 private static final byte[] passBytes = {(byte)'P',(byte)'A', 242 (byte)'S',(byte)'S', 243 (byte)' '}; 244 public int pass(String pass) throws IOException { 245 return simpleCmd(passBytes, pass); 246 } 247 248 249 private static final byte[] listBytes = {(byte)'L',(byte)'I', 250 (byte)'S',(byte)'T', 251 (byte)' '}; 252 public InputStream list() throws IOException { 253 return responseCmd(listBytes, ""); 254 } 255 public Vector list(String msg) throws IOException { 256 return vectorCmd(listBytes, msg); 257 } 258 259 260 private static final byte[] statBytes = {(byte)'S',(byte)'T', 261 (byte)'A',(byte)'T', 262 (byte)' '}; 263 public Vector stat() throws IOException { 264 return vectorCmd(statBytes, ""); 265 } 266 267 268 private static final byte[] uidlBytes = {(byte)'U',(byte)'I', 269 (byte)'D',(byte)'L', 270 (byte)' '}; 271 public InputStream uidl() throws IOException { 272 return responseCmd(uidlBytes, ""); 273 } 274 public Vector uidl(String msg) throws IOException { 275 return vectorCmd(uidlBytes, msg); 276 } 277 278 279 private static final byte[] retrBytes = {(byte)'R',(byte)'E', 280 (byte)'T',(byte)'R', 281 (byte)' '}; 282 public InputStream retr(String msg) throws IOException { 283 return responseCmd(retrBytes, msg); 284 } 285 public InputStream retr(int i) throws IOException { return retr("" + i); } 286 287 288 private static final byte[] deleBytes = {(byte)'D',(byte)'E', 289 (byte)'L',(byte)'E', 290 (byte)' '}; 291 public int dele(String msg) throws IOException { 292 return simpleCmd(deleBytes, msg); 293 } 294 public int dele(int i) throws IOException { return dele("" + i); } 295 296 297 private static final byte[] topBytes = {(byte)'T',(byte)'O', 298 (byte)'P',(byte)' '}; 299 public InputStream top(String msg, int lines) throws IOException { 300 return responseCmd(topBytes, msg + " " + lines); 301 } 302 303 304 private static final byte[] rsetBytes = {(byte)'R',(byte)'S', 305 (byte)'E',(byte)'T', 306 (byte)' '}; 307 public int rset() throws IOException { 308 return simpleCmd(rsetBytes, ""); 309 } 310 311 312 private static final byte[] quitBytes = {(byte)'Q', (byte)'U', 313 (byte)'I', (byte)'T', 314 (byte)' '}; 315 public int quit() throws IOException { 316 try { 317 simpleCmd(quitBytes, ""); 318 } finally { 319 socket.close(); 320 } 321 return OK; 322 } 323 324 325 private static final byte[] noopBytes = {(byte)'N', (byte)'O', 326 (byte)'O', (byte)'P', 327 (byte)' '}; 328 329 330 private static final byte[] apopBytes = {(byte)'A', (byte)'P', 331 (byte)'O', (byte)'P', 332 (byte)' '}; 333 334 } 335 | Popular Tags |