1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
6 import org.ibex.crypto.*;
8 import org.ibex.util.*;
9 import org.ibex.mail.protocol.*;
14 // FIXME this should be more forgiving
15 public class Address extends JSReflection implements Serializable {
16 public final String user;
17 public final String host;
18 public final String description;
19 public static Address parse(String s) { try { return s==null?null:new Address(s); } catch (Malformed _) { return null; } }
20 public Address(String user, String host, String description) {this.user=user;this.host=host;this.description=description;}
21 public Address(String s0) throws Address.Malformed {
25 if (s.indexOf('<') == -1) {
26 if (s.indexOf(' ') == -1) {
29 description = s.substring(s.indexOf(' ')).trim();
30 s = s.substring(0, s.indexOf(' '));
33 if (s.indexOf('>') == -1) { throw new Malformed("found open-angle-bracket (<) but not close-angle-bracket (>)"); }
34 description = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1);
35 s = s.substring(s.indexOf('<') + 1, s.indexOf('>'));
37 if (s.trim().length() == 0) {
41 } else if (s.indexOf('@') == -1) { throw new Malformed("no @-sign in email address \""+s0+"\"");
43 user = s.substring(0, s.indexOf('@'));
44 host = s.substring(s.indexOf('@')+1);
48 if (!good) Log.error(Address.class, "problem parsing: \""+s0+"\"");
51 public String toString() { return (user == null && host == null) ? "" : (user + "@" + host); }
52 public String coerceToString() { return toString(); }
53 public String toString(boolean desc) {
54 return desc && description != null && description.length() > 0 ? (description+" <" + toString() + ">") : toString(); }
55 public static class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
57 /** in case of flaky DNS */
58 private static HashSet<String> local = new HashSet<String>();
59 public boolean isLocal() {
61 if (local.contains(host)) return true;
62 InetAddress[] mx = SMTP.getMailExchangerIPs(host);
63 for(int i=0; i<mx.length; i++) {
64 try { if (NetworkInterface.getByInetAddress(mx[i]) != null) {
67 } } catch (Exception e) { /* DELIBERATE */ }
70 Log.warn(this, "returning false for " + this + ".isLocal()");
74 public static Address[] list(String spec) {
75 if (spec == null) return new Address[] { };
76 StringTokenizer st = new StringTokenizer((String)spec, ",");
77 Address[] ret = new Address[st.countTokens()];
78 for(int i=0; i<ret.length; i++) {
79 String s = st.nextToken();
80 ret[i] = Address.parse(s);
81 if (ret[i] == null) Log.warn(Address.class, "Warning: address " + s + " is unparseable");