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 {
23 if (s.indexOf('<') == -1) description = "";
25 if (s.indexOf('>') == -1) { throw new Malformed("found open-angle-bracket (<) but not close-angle-bracket (>)"); }
26 description = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1);
27 s = s.substring(s.indexOf('<') + 1, s.indexOf('>'));
29 if (s.indexOf('@') == -1) { throw new Malformed("no @-sign in email address \""+s0+"\""); }
30 user = s.substring(0, s.indexOf('@'));
31 host = s.substring(s.indexOf('@')+1);
33 public String toString() { return user + "@" + host; }
34 public String toString(boolean desc) {
35 return desc && description != null && description.length() > 0 ? (description+" <" + toString() + ">") : toString(); }
36 public String coerceToString() { return toString(); }
37 public static class Malformed extends Message.Malformed { public Malformed(String s) { super(s); } }
39 public boolean isLocal() {
40 InetAddress[] mx = SMTP.getMailExchangerIPs(host);
41 for(int i=0; i<mx.length; i++) {
42 try { if (NetworkInterface.getByInetAddress(mx[i]) != null) { return true; } }
43 catch (Exception e) { /* DELIBERATE */ }
45 Log.warn(this, "returning false");
49 public static Address[] list(String spec) {
50 if (spec == null) return new Address[] { };
51 StringTokenizer st = new StringTokenizer((String)spec, ",");
52 Address[] ret = new Address[st.countTokens()];
53 for(int i=0; i<ret.length; i++) {
54 String s = st.nextToken();
55 ret[i] = Address.parse(s);
56 if (ret[i] == null) Log.warn(Address.class, "Warning: address " + s + " is unparseable");