almost working
[org.ibex.mail.git] / src / org / ibex / mail / Address.java
1 package org.ibex.mail;
2 import org.ibex.crypto.*;
3 import org.ibex.js.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.protocol.*;
6 import java.util.*;
7 import java.net.*;
8 import java.io.*;
9
10 public class Address extends JSReflection {
11     public final String user;
12     public final String host;
13     public final String description;
14     public Address(String user, String host, String description) {this.user=user;this.host=host;this.description=description;}
15     public Address(String s) throws Address.Malformed {
16         s = s.trim();
17         if (s.indexOf('<') != -1) {
18             if (s.indexOf('>') == -1) { throw new Malformed("found open-angle-bracket (<) but not close-angle-bracket (>)"); }
19             description = s.substring(0, s.indexOf('<')) + s.substring(s.indexOf('>') + 1);
20             s = s.substring(s.indexOf('<') + 1, s.indexOf('>'));
21         } else {
22             description = "";
23         }
24         if (s.indexOf('@') == -1) { throw new Malformed("no @-sign in email address"); }
25         user = s.substring(0, s.indexOf('@'));
26         host = s.substring(s.indexOf('@')+1);
27     }
28     public String coerceToString() { return toString(); }
29     public String toString() { return description.equals("") ? (user +"@"+ host) : description+" <" + user +"@"+ host + ">"; }
30     public static class Malformed extends MailException.Malformed { public Malformed(String s) { super(s); } }
31 }