001: /*
002: * PopURL.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: PopURL.java,v 1.2 2002/12/27 03:53:18 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.net.MalformedURLException;
025: import org.armedbear.j.FastStringBuffer;
026:
027: public final class PopURL extends MailboxURL {
028: private static final int DEFAULT_PORT = 110;
029:
030: // RFC 2384 pop://user@host:port
031: public PopURL(String s) throws MalformedURLException {
032: if (!s.startsWith("pop://"))
033: throw new MalformedURLException();
034: s = s.substring(6);
035: port = DEFAULT_PORT;
036: // The user name may be enclosed in quotes.
037: if (s.length() > 0 && s.charAt(0) == '"') {
038: int index = s.indexOf('"', 1);
039: if (index >= 0) {
040: user = s.substring(1, index);
041: s = s.substring(index + 1);
042: } else
043: throw new MalformedURLException();
044: // We've got the user name.
045: if (s.length() == 0) {
046: // No host specified.
047: host = "127.0.0.1";
048: return;
049: }
050: if (s.charAt(0) != '@')
051: throw new MalformedURLException();
052: s = s.substring(1); // Skip '@'.
053: index = s.indexOf(':');
054: if (index >= 0) {
055: try {
056: port = Integer.parseInt(s.substring(index + 1));
057: } catch (Exception e) {
058: throw new MalformedURLException();
059: }
060: s = s.substring(0, index);
061: }
062: // What's left is the host name.
063: host = s;
064: } else {
065: int index = s.indexOf(':');
066: if (index >= 0) {
067: try {
068: port = Integer.parseInt(s.substring(index + 1));
069: } catch (Exception e) {
070: throw new MalformedURLException();
071: }
072: s = s.substring(0, index);
073: }
074: index = s.indexOf('@');
075: if (index >= 0) {
076: user = s.substring(0, index);
077: host = s.substring(index + 1);
078: } else {
079: user = s;
080: host = "127.0.0.1";
081: }
082: }
083: }
084:
085: public boolean equals(Object object) {
086: if (!(object instanceof PopURL))
087: return false;
088: PopURL url = (PopURL) object;
089: if (host != url.host) {
090: if (host == null)
091: return false;
092: if (!host.equals(url.host))
093: return false;
094: }
095: if (user != url.user) {
096: if (user == null)
097: return false;
098: if (!user.equals(url.user))
099: return false;
100: }
101: if (port != url.port)
102: return false;
103: return true;
104: }
105:
106: public String toString() {
107: FastStringBuffer sb = new FastStringBuffer("pop://");
108: if (user != null) {
109: if (user.indexOf('@') >= 0) {
110: sb.append('"');
111: sb.append(user);
112: sb.append('"');
113: } else
114: sb.append(user);
115: sb.append('@');
116: }
117: sb.append(host);
118: if (port != DEFAULT_PORT) {
119: sb.append(':');
120: sb.append(port);
121: }
122: return sb.toString();
123: }
124:
125: public String getCanonicalName() {
126: FastStringBuffer sb = new FastStringBuffer("pop://");
127: String s = user != null ? user : System
128: .getProperty("user.name");
129: if (s.indexOf('@') >= 0) {
130: sb.append('"');
131: sb.append(s);
132: sb.append('"');
133: } else
134: sb.append(s);
135: sb.append('@');
136: sb.append(host);
137: sb.append(':');
138: sb.append(port);
139: return sb.toString();
140: }
141: }
|