1 package org.ibex.mail.protocol;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.target.*;
10 /** central place for logging conversations */
11 public abstract class Connection {
13 protected Socket conn;
14 protected String vhost;
15 protected PrintWriter pw;
16 protected InputStream is;
17 protected PushbackReader r;
18 protected LineReader lr;
20 private static String convdir;
23 convdir = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
24 convdir += File.separatorChar + "conversation";
25 new File(convdir).mkdirs();
26 } catch (Exception e) {
27 Log.warn(Connection.class, e);
31 public Connection(Socket conn, String vhost) throws IOException {
34 this.pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
35 this.is = conn.getInputStream();
36 Reader isr = new InputStreamReader(is);
37 this.r = new PushbackReader(isr);
38 this.lr = new LineReader(isr);
41 protected abstract boolean handleRequest() throws IOException;
44 private StringBuffer inbound = new StringBuffer();
45 private PrintWriter conversation;
47 public final boolean handle() throws IOException {
48 cid = getConversation();
49 Log.setThreadAnnotation("[conversation " + cid + "] ");
50 InetSocketAddress remote = (InetSocketAddress)conn.getRemoteSocketAddress();
51 Log.info(this, "connection from "+remote.getHostName()+":"+remote.getPort()+" ("+remote.getAddress()+")");
52 conversation = new PrintWriter(new OutputStreamWriter(new FileOutputStream(convdir + File.separatorChar + cid + ".txt")));
53 boolean ret = handleRequest();
54 Log.setThreadAnnotation("");
59 protected void println(String s) throws IOException {
60 conversation.println("S: " + s);
65 protected void flush() throws IOException { pw.flush(); }
66 protected String readln() throws IOException {
67 String line = lr.readLine();
68 conversation.println("C: " + line);
71 public char getc() throws IOException {
73 if (ret == -1) throw new EOFException();
74 if (ret == '\n') { if (inbound.length() > 0) { conversation.println("C: " + inbound.toString()); inbound.setLength(0); } }
75 else if (ret != '\r') inbound.append((char)ret);
78 public char peekc() throws IOException {
80 if (ret == -1) throw new EOFException();
84 public void fill(byte[] b) throws IOException {
86 while (num < b.length) {
87 int numread = is.read(b, num, b.length - num);
88 if (numread == -1) throw new EOFException();
93 private static String lastTime = null;
94 private static int lastCounter = 0;
95 static String getConversation() {
96 String time = new SimpleDateFormat("yy.MMM.dd-hh:mm:ss").format(new Date());
97 synchronized (SMTP.class) {
98 if (lastTime != null && lastTime.equals(time)) time += "." + (++lastCounter);