fe61ff6281fe56ff5fbed59467b6dd21a77111a4
[org.ibex.mail.git] / src / org / ibex / mail / protocol / Connection.java
1 package org.ibex.mail.protocol;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.target.*;
5 import java.util.*;
6 import java.net.*;
7 import java.text.*;
8 import java.io.*;
9
10 /** central place for logging conversations */
11 public abstract class Connection {
12
13     protected Socket conn;
14     protected String vhost;
15     protected PrintWriter pw;
16     protected InputStream is;
17     protected PushbackReader r;
18     protected LineReader lr;
19
20     private static String convdir;
21     static {
22         try {
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);
28         }
29     }
30
31     public Connection(Socket conn, String vhost) throws IOException {
32         this.vhost = vhost;
33         this.conn = conn;
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);
39     }
40
41     protected abstract boolean handleRequest() throws IOException;
42
43     private String cid;
44     private StringBuffer inbound = new StringBuffer();
45     private PrintWriter conversation;
46
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("");
55         conversation.close();
56         return ret;
57     }
58
59     protected void println(String s) throws IOException {
60         conversation.println("S: " + s);
61         pw.print(s);
62         pw.print("\r\n");
63         pw.flush();
64     }
65     protected void flush() throws IOException { pw.flush(); }
66     protected String readln() throws IOException {
67         String line = lr.readLine();
68         conversation.println("C: " + line);
69         return line;
70     }
71     public char getc() throws IOException {
72         int ret = r.read();
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);
76         return (char)ret;
77     }
78     public  char peekc() throws IOException {
79         int ret = r.read();
80         if (ret == -1) throw new EOFException();
81         r.unread(ret);
82         return (char)ret;
83     }
84     public  void fill(byte[] b) throws IOException {
85         int num = 0;
86         while (num < b.length) {
87             int numread = is.read(b, num, b.length - num);
88             if (numread == -1) throw new EOFException();
89             num += numread;
90         }
91     }
92
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);
99             else lastTime = time;
100         }
101         return time;
102     }
103
104 }