001: /*
002: *
003: * Jsmtpd, Java SMTP daemon
004: * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package org.jsmtpd.core.mail;
022:
023: import java.io.Serializable;
024:
025: /**
026: * Helper class to store an email address
027: * @author Jean-Francois POUX
028: *
029: */
030: public class EmailAddress implements Serializable {
031:
032: private String user = "";
033: private String host = "";
034: private String authContext = "";
035:
036: /**
037: * Creates an instance by parsing a string user@domain, or <>
038: * @param input the string
039: * @return instance
040: * @throws InvalidAddress when parsing error
041: */
042: public static EmailAddress parseAddress(String input)
043: throws InvalidAddress {
044: if ((input == null) || input.equals(""))
045: throw new InvalidAddress();
046:
047: if (input.equals("<>")) {
048: EmailAddress e = new EmailAddress();
049: e.setUser("<>");
050: return e;
051: }
052: EmailAddress e = new EmailAddress();
053: String mailPart;
054: String tmp = input.toLowerCase().trim();
055: if (tmp.indexOf(" auth=") != -1) {
056: String[] res = input.trim().split(" ");
057: e.setAuthContext(res[1].trim());
058: mailPart = res[0].trim();
059: } else
060: mailPart = input.trim();
061:
062: if (mailPart.indexOf('@') == -1)
063: throw new InvalidAddress();
064: String[] res = mailPart.split("@");
065:
066: if ((res[0].length() > 512) || (res[1].length() > 512))
067: throw new InvalidAddress();
068:
069: e.setUser(res[0]);
070: e.setHost(res[1].toLowerCase());
071: return e;
072: }
073:
074: /**
075: * builds a user@domain string
076: */
077: public String toString() {
078: if (user.equals("<>"))
079: return user;
080:
081: return (user + "@" + host);
082: }
083:
084: public String getHost() {
085: return host;
086: }
087:
088: public void setHost(String host) {
089: this .host = host;
090: }
091:
092: public String getUser() {
093: return user;
094: }
095:
096: public void setUser(String user) {
097: this .user = user;
098: }
099:
100: /**
101: * test if to instances are equals. matches user and domain, or * as user @ domain
102: * @param in
103: * @return true if equals
104: */
105: public boolean isEqual(EmailAddress in) {
106: if (in == this )
107: return true;
108:
109: if (in.getHost().equals(this .host)
110: && in.getUser().equals(this .user))
111: return true;
112:
113: if (in.getHost().equals(this .host) && in.getUser().equals("*"))
114: return true;
115:
116: return false;
117:
118: }
119:
120: public int hashCode() {
121: String tmp = host;
122: return tmp.hashCode();
123: }
124:
125: public boolean equals(Object obj) {
126: if (!(obj instanceof EmailAddress))
127: return false;
128: return isEqual((EmailAddress) obj);
129: }
130:
131: public EmailAddress() {
132:
133: }
134:
135: public EmailAddress(EmailAddress original) {
136: setHost(new String(original.getHost()));
137: setUser(new String(original.getUser()));
138: }
139:
140: public String getAuthContext() {
141: return authContext;
142: }
143:
144: public void setAuthContext(String authContext) {
145: this.authContext = authContext;
146: }
147: }
|