1bf6b042ca8be2f3dc6c46f4fe788b20034ce5be
[org.ibex.mail.git] / src / org / ibex / mail / Address.java
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.
4
5 package org.ibex.mail;
6 import org.ibex.crypto.*;
7 import org.ibex.js.*;
8 import org.ibex.util.*;
9 import org.ibex.mail.protocol.*;
10 import java.util.*;
11 import java.net.*;
12 import java.io.*;
13
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 {
22         String s = s0.trim();
23         if (s.indexOf('<') == -1) {
24             if (s.indexOf(' ') == -1) {
25                 description = "";
26             } else {
27                 description = s.substring(s.indexOf(' ')).trim();
28                 s = s.substring(0, s.indexOf(' '));
29             }
30         } else {
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('>'));
34         }
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);
38     }
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); } }
44
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 */ }
50         }
51         Log.warn(this, "returning false");
52         return false;
53     }
54
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");
63         }
64         return ret;
65     }
66 }