almost working
[org.ibex.mail.git] / src / org / ibex / mail / Address.java
diff --git a/src/org/ibex/mail/Address.java b/src/org/ibex/mail/Address.java
new file mode 100644 (file)
index 0000000..92bdbc8
--- /dev/null
@@ -0,0 +1,31 @@
+package org.ibex.mail;
+import org.ibex.crypto.*;
+import org.ibex.js.*;
+import org.ibex.util.*;
+import org.ibex.mail.protocol.*;
+import java.util.*;
+import java.net.*;
+import java.io.*;
+
+public class Address extends JSReflection {
+    public final String user;
+    public final String host;
+    public final String description;
+    public Address(String user, String host, String description) {this.user=user;this.host=host;this.description=description;}
+    public Address(String s) throws Address.Malformed {
+        s = s.trim();
+        if (s.indexOf('<') != -1) {
+            if (s.indexOf('>') == -1) { throw new Malformed("found open-angle-bracket (<) but not close-angle-bracket (>)"); }
+            description = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1);
+            s = s.substring(s.indexOf('<') + 1, s.indexOf('>'));
+        } else {
+            description = "";
+        }
+        if (s.indexOf('@') == -1) { throw new Malformed("no @-sign in email address"); }
+        user = s.substring(0, s.indexOf('@'));
+        host = s.substring(s.indexOf('@')+1);
+    }
+    public String coerceToString() { return toString(); }
+    public String toString() { return description.equals("") ? (user +"@"+ host) : description+" <" + user +"@"+ host + ">"; }
+    public static class Malformed extends MailException.Malformed { public Malformed(String s) { super(s); } }
+}