Author: dbkr
Date: 2006-06-08 18:00:52 +0000 (Thu, 08 Jun 2006)
New Revision: 9090
Added:
trunk/apps/fnmail/src/fnmail/fcp/SSKKeyPair.java
trunk/apps/fnmail/src/fnmail/utils/PropsFile.java
Modified:
trunk/apps/fnmail/build.xml
trunk/apps/fnmail/src/fnmail/AccountManager.java
trunk/apps/fnmail/src/fnmail/FNMail.java
trunk/apps/fnmail/src/fnmail/MessageSender.java
trunk/apps/fnmail/src/fnmail/SingleAccountWatcher.java
trunk/apps/fnmail/src/fnmail/fcp/FCPMessage.java
trunk/apps/fnmail/src/fnmail/fcp/HighLevelFCPClient.java
Log:
Generate and store data that will be inserted to the mailsite. (Also change
compiler to Jikes in the buildfile since using Sun's insanely terse compiler at
this stage of development is ridiculous).
Modified: trunk/apps/fnmail/build.xml
===================================================================
--- trunk/apps/fnmail/build.xml 2006-06-08 11:25:40 UTC (rev 9089)
+++ trunk/apps/fnmail/build.xml 2006-06-08 18:00:52 UTC (rev 9090)
@@ -15,7 +15,7 @@
<tstamp/>
<!-- Create the build directory structure used by compile -->
- <javac srcdir="${src}" destdir="${build}" debug="on"
optimize="on" source="1.4">
+ <javac srcdir="${src}" destdir="${build}" debug="on"
optimize="on" source="1.4" compiler="jikes">
<include name="fnmail/*.java"/>
<include name="fnmail/*/*.java"/>
<include name="thirdparty/*.java"/>
Modified: trunk/apps/fnmail/src/fnmail/AccountManager.java
===================================================================
--- trunk/apps/fnmail/src/fnmail/AccountManager.java 2006-06-08 11:25:40 UTC
(rev 9089)
+++ trunk/apps/fnmail/src/fnmail/AccountManager.java 2006-06-08 18:00:52 UTC
(rev 9090)
@@ -2,18 +2,30 @@
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.BufferedReader;
import java.io.FileNotFoundException;
+import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.util.Random;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import fnmail.fcp.HighLevelFCPClient;
+import fnmail.fcp.SSKKeyPair;
+import fnmail.util.PropsFile;
+
public class AccountManager {
- public static String DATADIR = "data";
+ public static final String DATADIR = "data";
// this really doesn't matter a great deal
- public static String NIMDIR = "nim";
- public static String PASSWDFILE = "passwd";
+ public static final String NIMDIR = "nim";
+
+ public static final String PASSWDFILE = "passwd";
+ private static final String ACCOUNT_FILE = "accprops";
+ private static final int RTS_KEY_LENGTH = 32;
+
public static void Create(String username) throws IOException {
File datadir = new File(DATADIR);
@@ -69,6 +81,64 @@
fos.close();
}
+ public static String getMailsitePubkey(File accdir) {
+ PropsFile accfile = getAccountFile(accdir);
+
+ return accfile.get("mailsite.pubkey");
+ }
+
+ public static String getMailsitePrivkey(File accdir) {
+ PropsFile accfile = getAccountFile(accdir);
+
+ return accfile.get("mailsite.privkey");
+ }
+
+ private static PropsFile getAccountFile(File accdir) {
+ PropsFile accfile = new PropsFile(new File(accdir,
ACCOUNT_FILE));
+
+ if (!accfile.exists()) {
+ initAccFile(accfile);
+ }
+
+ return accfile;
+ }
+
+ private static void initAccFile(PropsFile accfile) {
+ try {
+ System.out.println("Generating mailsite keys...");
+ HighLevelFCPClient fcpcli = new
HighLevelFCPClient(FNMail.getFCPConnection());
+
+ SSKKeyPair keypair = fcpcli.makeSSK();
+
+ // write private key
+ if (!accfile.put("mailsite.privkey", keypair.privkey)) {
+ throw new IOException("Unable to write account
file");
+ }
+
+ // write public key
+ if (!accfile.put("mailsite.pubkey", keypair.pubkey)) {
+ throw new IOException("Unable to write account
file");
+ }
+
+ // initialise RTS/CTS KSK
+ Random rnd = new Random();
+ String rtskey = new String();
+
+ int i;
+ for (i = 0; i < RTS_KEY_LENGTH; i++) {
+ rtskey += (char)(rnd.nextInt(25) + (int)'a');
+ }
+
+ if (!accfile.put("rtskey", rtskey)) {
+ throw new IOException("Unable to write account
file");
+ }
+
+ System.out.println("Mailsite keys generated.");
+ } catch (IOException ioe) {
+ System.out.println("Couldn't create mailsite key file!
"+ioe.getMessage());
+ }
+ }
+
public static boolean authenticate(String username, String password) {
if (!validate_username(username)) return false;
Modified: trunk/apps/fnmail/src/fnmail/FNMail.java
===================================================================
--- trunk/apps/fnmail/src/fnmail/FNMail.java 2006-06-08 11:25:40 UTC (rev
9089)
+++ trunk/apps/fnmail/src/fnmail/FNMail.java 2006-06-08 18:00:52 UTC (rev
9090)
@@ -12,10 +12,15 @@
private static final String TEMPDIRNAME = "temp";
private static File datadir;
private static File tempdir;
+ private static FCPConnection fcpconn;
public static File getTempDir() {
return FNMail.tempdir;
}
+
+ public static FCPConnection getFCPConnection() {
+ return FNMail.fcpconn;
+ }
public static void main(String[] args) {
String fcphost = "localhost";
@@ -74,7 +79,7 @@
FCPContext fcpctx = new FCPContext(fcphost, fcpport);
- FCPConnection fcpconn = new FCPConnection(fcpctx);
+ FNMail.fcpconn = new FCPConnection(fcpctx);
Thread fcpthread = new Thread(fcpconn);
fcpthread.setDaemon(true);
fcpthread.start();
@@ -102,13 +107,13 @@
if (files[i].getName().equals(".") ||
files[i].getName().equals(".."))
continue;
- Thread t = new Thread(new SingleAccountWatcher(fcpconn,
files[i]));
+ Thread t = new Thread(new
SingleAccountWatcher(files[i]));
t.setDaemon(true);
t.start();
}
// and a sender thread
- MessageSender sender = new MessageSender(FNMail.datadir,
fcpconn);
+ MessageSender sender = new MessageSender(FNMail.datadir);
Thread senderthread = new Thread(sender);
senderthread.setDaemon(true);
senderthread.start();
Modified: trunk/apps/fnmail/src/fnmail/MessageSender.java
===================================================================
--- trunk/apps/fnmail/src/fnmail/MessageSender.java 2006-06-08 11:25:40 UTC
(rev 9089)
+++ trunk/apps/fnmail/src/fnmail/MessageSender.java 2006-06-08 18:00:52 UTC
(rev 9090)
@@ -20,12 +20,10 @@
public static final int MIN_RUN_TIME = 60000;
public static final String NIM_KEY_PREFIX = "KSK at fnmail-nim-";
private final File datadir;
- private final FCPConnection fcpconn;
private Thread senderthread;
- public MessageSender(File d, FCPConnection conn) {
+ public MessageSender(File d) {
this.datadir = d;
- this.fcpconn = conn;
}
public void send_message(String from_user, Vector to, File msg) throws
IOException {
@@ -127,8 +125,7 @@
}
private boolean slotinsert(File data, String basekey) {
- // TODO: add date to this too
- HighLevelFCPClient cli = new HighLevelFCPClient(this.fcpconn);
+ HighLevelFCPClient cli = new
HighLevelFCPClient(FNMail.getFCPConnection());
int slot = 1;
boolean carryon = true;
Modified: trunk/apps/fnmail/src/fnmail/SingleAccountWatcher.java
===================================================================
--- trunk/apps/fnmail/src/fnmail/SingleAccountWatcher.java 2006-06-08
11:25:40 UTC (rev 9089)
+++ trunk/apps/fnmail/src/fnmail/SingleAccountWatcher.java 2006-06-08
18:00:52 UTC (rev 9090)
@@ -1,23 +1,30 @@
package fnmail;
import java.io.File;
+import java.io.OutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
import java.lang.InterruptedException;
import fnmail.fcp.FCPConnection;
+import fnmail.fcp.HighLevelFCPClient;
+import fnmail.fcp.SSKKeyPair;
public class SingleAccountWatcher implements Runnable {
public static final String CONTACTS_DIR = "contacts";
private static final int MIN_POLL_DURATION = 60000; // in milliseconds
+ private static final int MAILSITE_UPLOAD_INTERVAL = 60 * 60 * 1000;
private final MessageBank mb;
private final MailFetcher mf;
- private final FCPConnection fcpconn;
- SingleAccountWatcher(FCPConnection fcpc, File accdir) {
- this.fcpconn = fcpc;
+ SingleAccountWatcher(File accdir) {
File contacts_dir = new File(accdir, CONTACTS_DIR);
+ /////////////////
+ AccountManager.getMailsitePrivkey(accdir);
+
this.mb = new MessageBank(accdir.getName());
- this.mf = new MailFetcher(this.mb, contacts_dir, this.fcpconn);
+ this.mf = new MailFetcher(this.mb, contacts_dir,
FNMail.getFCPConnection());
}
public void run() {
Modified: trunk/apps/fnmail/src/fnmail/fcp/FCPMessage.java
===================================================================
--- trunk/apps/fnmail/src/fnmail/fcp/FCPMessage.java 2006-06-08 11:25:40 UTC
(rev 9089)
+++ trunk/apps/fnmail/src/fnmail/fcp/FCPMessage.java 2006-06-08 18:00:52 UTC
(rev 9090)
@@ -39,6 +39,7 @@
String line;
while ( (line = r.readLine(200, 200)) != null) {
+ /***************************************/
//System.out.println(line);
if (this.messagetype == null) {
this.messagetype = line;
Modified: trunk/apps/fnmail/src/fnmail/fcp/HighLevelFCPClient.java
===================================================================
--- trunk/apps/fnmail/src/fnmail/fcp/HighLevelFCPClient.java 2006-06-08
11:25:40 UTC (rev 9089)
+++ trunk/apps/fnmail/src/fnmail/fcp/HighLevelFCPClient.java 2006-06-08
18:00:52 UTC (rev 9090)
@@ -50,6 +50,43 @@
}
}
+ public synchronized SSKKeyPair makeSSK() {
+ FCPMessage msg = this.conn.getMessage("GenerateSSK");
+
+ while (true) {
+ try {
+ this.conn.doRequest(this, msg);
+ break;
+ } catch (NoNodeConnectionException nnce) {
+ try {
+ System.out.println("Warning - no
connection to node. Waiting...");
+ Thread.sleep(5000);
+ } catch (InterruptedException ie) {
+ }
+ } catch (FCPBadFileException bfe) {
+ // won't be thrown since no data
+ }
+ }
+
+ this.donemsg = null;
+ while (this.donemsg == null) {
+ try {
+ this.wait();
+ } catch (InterruptedException ie) {
+ }
+ }
+
+ if (this.donemsg.getType().equalsIgnoreCase("SSKKeypair")) {
+ SSKKeyPair retval = new SSKKeyPair();
+
+ retval.privkey =
(String)this.donemsg.headers.get("InsertURI");
+ retval.pubkey =
(String)this.donemsg.headers.get("RequestURI");
+ return retval;
+ } else {
+ return null;
+ }
+ }
+
public synchronized FCPInsertErrorMessage put(InputStream data, String
key) throws FCPBadFileException {
FCPMessage msg = this.conn.getMessage("ClientPut");
msg.headers.put("URI", key);
Added: trunk/apps/fnmail/src/fnmail/fcp/SSKKeyPair.java
===================================================================
--- trunk/apps/fnmail/src/fnmail/fcp/SSKKeyPair.java 2006-06-08 11:25:40 UTC
(rev 9089)
+++ trunk/apps/fnmail/src/fnmail/fcp/SSKKeyPair.java 2006-06-08 18:00:52 UTC
(rev 9090)
@@ -0,0 +1,6 @@
+package fnmail.fcp;
+
+public class SSKKeyPair {
+ public String pubkey;
+ public String privkey;
+}
Added: trunk/apps/fnmail/src/fnmail/utils/PropsFile.java
===================================================================
--- trunk/apps/fnmail/src/fnmail/utils/PropsFile.java 2006-06-08 11:25:40 UTC
(rev 9089)
+++ trunk/apps/fnmail/src/fnmail/utils/PropsFile.java 2006-06-08 18:00:52 UTC
(rev 9090)
@@ -0,0 +1,81 @@
+package fnmail.util;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileOutputStream;
+import java.io.PrintWriter;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+public class PropsFile {
+ private final File file;
+ private HashMap data;
+
+ public PropsFile(File f) {
+ this.file = f;
+ this.data = null;
+ }
+
+ private void read() throws IOException {
+ this.data = new HashMap();
+
+ BufferedReader br = new BufferedReader(new
FileReader(this.file));
+
+ String line = null;
+ while ( (line = br.readLine()) != null) {
+ String[] parts = line.split("=", 2);
+ if (parts.length < 2) continue;
+ this.data.put(parts[0], parts[1]);
+ }
+
+ br.close();
+ }
+
+ private void write() throws IOException {
+ PrintWriter pw = new PrintWriter(new
FileOutputStream(this.file));
+
+ Iterator i = this.data.entrySet().iterator();
+ while (i.hasNext()) {
+ Map.Entry e = (Map.Entry) i.next();
+ String key = (String)e.getKey();
+ String val = (String)e.getValue();
+
+ pw.println(key+"="+val);
+ }
+
+ pw.close();
+ }
+
+ public String get(String key) {
+ if (this.data == null) {
+ try {
+ this.read();
+ } catch (IOException ioe) {
+ return null;
+ }
+ }
+
+ return (String)this.data.get(key);
+ }
+
+ public boolean put(String key, String val) {
+ if (this.data == null) {
+ this.data = new HashMap();
+ }
+
+ this.data.put(key, val);
+ try {
+ this.write();
+ } catch (IOException ioe) {
+ return false;
+ }
+ return true;
+ }
+
+ public boolean exists() {
+ return this.file.exists();
+ }
+}