--- /dev/null
+package org.ibex.mail.target;
+
+import org.ibex.mail.*;
+import org.ibex.io.Fountain;
+import org.ibex.io.Stream;
+import java.sql.Timestamp;
+import java.sql.*;
+import java.net.*;
+import java.io.*;
+import java.util.*;
+
+public class SqliteJdbcMailbox extends Mailbox.Default {
+
+ private Connection conn;
+
+ public SqliteJdbcMailbox(String filename) {
+ try {
+ Class.forName("org.sqlite.JDBC");
+ conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
+ conn.prepareStatement("create table if not exists "+
+ "'mail' (messageid_,from_,to_,date_,subject_,headers_,body_,PRIMARY KEY(messageid_))").executeUpdate();
+ }
+ catch (SQLException e) { throw new RuntimeException(e); }
+ catch (ClassNotFoundException e) { throw new RuntimeException(e); }
+ }
+
+ public void add(Message m) {
+ try {
+ PreparedStatement add = conn.prepareStatement("insert into 'mail' values (?,?,?,?,?,?,?)");
+ add.setString(1, m.messageid+"");
+ add.setString(2, m.from+"");
+ add.setString(3, m.to+"");
+ add.setString(4, m.date+"");
+ add.setString(5, m.subject+"");
+ add.setString(6, streamToString(m.headers.getStream()));
+ add.setString(7, streamToString(m.getBody().getStream()));
+ add.executeUpdate();
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+
+ public Mailbox.Iterator iterator() { return new SqliteJdbcIterator(); }
+ public void add(Message message, int flags) { add(message); }
+ public int uidNext() { throw new RuntimeException("not supported"); }
+
+ private class SqliteJdbcIterator extends Mailbox.Default.Iterator {
+ private ResultSet rs;
+ private int count=1;
+ private Message m = null;
+ public SqliteJdbcIterator() {
+ try {
+ PreparedStatement query = conn.prepareStatement("select messageid_ from 'mail'");
+ rs = query.executeQuery();
+ rs.next();
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+ public Message cur() {
+ try {
+ if (m!=null) return m;
+ rs.next();
+ PreparedStatement query = conn.prepareStatement("select headers_,body_ from 'mail' where messageid_=?");
+ query.setString(1, rs.getString(1));
+ ResultSet rs2 = query.executeQuery();
+ if (!rs.next()) return null;
+ m = Message.newMessage(new Fountain.StringFountain(rs.getString(1) + "\r\n\r\n" + rs.getString(2)));
+ return m;
+ } catch (Exception e) { throw new RuntimeException(e); }
+ }
+ public Headers head() { return cur().headers; }
+ public boolean next() { try { m = null; count++; return rs.next(); } catch (Exception e) { throw new RuntimeException(e); } }
+ public int uid() { throw new RuntimeException("not supported"); }
+ public int num() { return count; } // FIXME FIXME
+ public void delete() { throw new RuntimeException("not supported"); }
+ }
+
+ public static String streamToString(Stream stream) throws Exception {
+ StringBuffer b = new StringBuffer();
+ for(String s = stream.readln(); s!=null; s=stream.readln())
+ b.append(s+"\n");
+ return b.toString();
+ }
+
+}
\ No newline at end of file