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")));
55 ret = handleRequest();
56 Log.setThreadAnnotation("");
63 protected void println(String s) throws IOException {
64 if (inbound.length() > 0) { conversation.println("C: " + inbound.toString()); inbound.setLength(0); }
65 conversation.println("S: " + s);
71 protected void flush() throws IOException { pw.flush(); }
72 protected String readln() throws IOException {
73 String line = lr.readLine();
74 conversation.println("C: " + line);
78 public char getc() throws IOException {
80 if (ret == -1) throw new EOFException();
82 if (inbound.length() > 0) { conversation.println("C: " + inbound.toString()); inbound.setLength(0); } }
83 else if (ret != '\r') inbound.append((char)ret);
87 public char peekc() throws IOException {
89 if (ret == -1) throw new EOFException();
93 public void fill(byte[] b) throws IOException {
95 while (num < b.length) {
96 int numread = is.read(b, num, b.length - num);
97 if (numread == -1) throw new EOFException();
102 private static String lastTime = null;
103 private static int lastCounter = 0;
104 static String getConversation() {
105 String time = new SimpleDateFormat("yy.MMM.dd-hh:mm:ss").format(new Date());
106 synchronized (SMTP.class) {
107 if (lastTime != null && lastTime.equals(time)) time += "." + (++lastCounter);
108 else lastTime = time;