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