+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
package org.ibex.mail;
import org.ibex.crypto.*;
import org.ibex.js.*;
import java.io.*;
// FIXME this should be more forgiving
-public class Address extends JSReflection {
+public class Address extends JSReflection implements Serializable {
public final String user;
public final String host;
public final String description;
public static Address parse(String s) { try { return s==null?null:new Address(s); } catch (Malformed _) { return null; } }
public Address(String user, String host, String description) {this.user=user;this.host=host;this.description=description;}
public Address(String s0) throws Address.Malformed {
- String s = s0.trim();
- if (s.indexOf('<') == -1) description = "";
- else {
- 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('>'));
+ boolean good = false;
+ try {
+ String s = s0.trim();
+ if (s.indexOf('<') == -1) {
+ if (s.indexOf(' ') == -1) {
+ description = "";
+ } else {
+ description = s.substring(s.indexOf(' ')).trim();
+ s = s.substring(0, s.indexOf(' '));
+ }
+ } else {
+ 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('>'));
+ }
+ if (s.trim().length() == 0) {
+ user = null;
+ host = null;
+ good = true;
+ } else if (s.indexOf('@') == -1) { throw new Malformed("no @-sign in email address \""+s0+"\"");
+ } else {
+ user = s.substring(0, s.indexOf('@'));
+ host = s.substring(s.indexOf('@')+1);
+ good = true;
+ }
+ } finally {
+ if (!good) Log.error(Address.class, "problem parsing: \""+s0+"\"");
}
- if (s.indexOf('@') == -1) { throw new Malformed("no @-sign in email address \""+s0+"\""); }
- user = s.substring(0, s.indexOf('@'));
- host = s.substring(s.indexOf('@')+1);
}
- public String toString() { return description.equals("") ? (user +"@"+ host) : description+" <" + user +"@"+ host + ">"; }
+ public String toString() { return (user == null && host == null) ? "" : (user + "@" + host); }
public String coerceToString() { return toString(); }
+ public String toString(boolean desc) {
+ return desc && description != null && description.length() > 0 ? (description+" <" + toString() + ">") : toString(); }
public static class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
public boolean isLocal() {