1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.pop3;
  17. import java.io.BufferedReader;
  18. import java.io.BufferedWriter;
  19. import java.io.EOFException;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.OutputStreamWriter;
  23. import java.lang.StringBuffer;
  24. import java.util.Enumeration;
  25. import java.util.Vector;
  26. import org.apache.commons.net.MalformedServerReplyException;
  27. import org.apache.commons.net.ProtocolCommandListener;
  28. import org.apache.commons.net.ProtocolCommandSupport;
  29. import org.apache.commons.net.SocketClient;
  30. /***
  31. * The POP3 class is not meant to be used by itself and is provided
  32. * only so that you may easily implement your own POP3 client if
  33. * you so desire. If you have no need to perform your own implementation,
  34. * you should use <a href="org.apache.commons.net.pop3.POP3Client.html">POP3Client</a>.
  35. * <p>
  36. * Rather than list it separately for each method, we mention here that
  37. * every method communicating with the server and throwing an IOException
  38. * can also throw a
  39. * <a href="org.apache.commons.net.MalformedServerReplyException.html">
  40. * MalformedServerReplyException </a>, which is a subclass
  41. * of IOException. A MalformedServerReplyException will be thrown when
  42. * the reply received from the server deviates enough from the protocol
  43. * specification that it cannot be interpreted in a useful manner despite
  44. * attempts to be as lenient as possible.
  45. * <p>
  46. * <p>
  47. * @author Daniel F. Savarese
  48. * @see POP3Client
  49. * @see org.apache.commons.net.MalformedServerReplyException
  50. ***/
  51. public class POP3 extends SocketClient
  52. {
  53. /*** The default POP3 port. Set to 110 according to RFC 1288. ***/
  54. public static final int DEFAULT_PORT = 110;
  55. /***
  56. * A constant representing the state where the client is not yet connected
  57. * to a POP3 server.
  58. ***/
  59. public static final int DISCONNECTED_STATE = -1;
  60. /*** A constant representing the POP3 authorization state. ***/
  61. public static final int AUTHORIZATION_STATE = 0;
  62. /*** A constant representing the POP3 transaction state. ***/
  63. public static final int TRANSACTION_STATE = 1;
  64. /*** A constant representing the POP3 update state. ***/
  65. public static final int UPDATE_STATE = 2;
  66. static final String _OK = "+OK";
  67. static final String _ERROR = "-ERR";
  68. // We have to ensure that the protocol communication is in ASCII
  69. // but we use ISO-8859-1 just in case 8-bit characters cross
  70. // the wire.
  71. private static final String __DEFAULT_ENCODING = "ISO-8859-1";
  72. private int __popState;
  73. private BufferedWriter __writer;
  74. private StringBuffer __commandBuffer;
  75. BufferedReader _reader;
  76. int _replyCode;
  77. String _lastReplyLine;
  78. Vector _replyLines;
  79. /***
  80. * A ProtocolCommandSupport object used to manage the registering of
  81. * ProtocolCommandListeners and te firing of ProtocolCommandEvents.
  82. ***/
  83. protected ProtocolCommandSupport _commandSupport_;
  84. /***
  85. * The default POP3Client constructor. Initializes the state
  86. * to <code>DISCONNECTED_STATE</code>.
  87. ***/
  88. public POP3()
  89. {
  90. setDefaultPort(DEFAULT_PORT);
  91. __commandBuffer = new StringBuffer();
  92. __popState = DISCONNECTED_STATE;
  93. _reader = null;
  94. __writer = null;
  95. _replyLines = new Vector();
  96. _commandSupport_ = new ProtocolCommandSupport(this);
  97. }
  98. private void __getReply() throws IOException
  99. {
  100. String line;
  101. _replyLines.setSize(0);
  102. line = _reader.readLine();
  103. if (line == null)
  104. throw new EOFException("Connection closed without indication.");
  105. if (line.startsWith(_OK))
  106. _replyCode = POP3Reply.OK;
  107. else if (line.startsWith(_ERROR))
  108. _replyCode = POP3Reply.ERROR;
  109. else
  110. throw new
  111. MalformedServerReplyException(
  112. "Received invalid POP3 protocol response from server.");
  113. if (_commandSupport_.getListenerCount() > 0)
  114. _commandSupport_.fireReplyReceived(_replyCode, getReplyString());
  115. _replyLines.addElement(line);
  116. _lastReplyLine = line;
  117. }
  118. /***
  119. * Performs connection initialization and sets state to
  120. * <code> AUTHORIZATION_STATE </code>.
  121. ***/
  122. protected void _connectAction_() throws IOException
  123. {
  124. super._connectAction_();
  125. _reader =
  126. new BufferedReader(new InputStreamReader(_input_,
  127. __DEFAULT_ENCODING));
  128. __writer =
  129. new BufferedWriter(new OutputStreamWriter(_output_,
  130. __DEFAULT_ENCODING));
  131. __getReply();
  132. setState(AUTHORIZATION_STATE);
  133. }
  134. /***
  135. * Adds a ProtocolCommandListener. Delegates this task to
  136. * <a href="#_commandSupport_"> _commandSupport_ </a>.
  137. * <p>
  138. * @param listener The ProtocolCommandListener to add.
  139. ***/
  140. public void addProtocolCommandListener(ProtocolCommandListener listener)
  141. {
  142. _commandSupport_.addProtocolCommandListener(listener);
  143. }
  144. /***
  145. * Removes a ProtocolCommandListener. Delegates this task to
  146. * <a href="#_commandSupport_"> _commandSupport_ </a>.
  147. * <p>
  148. * @param listener The ProtocolCommandListener to remove.
  149. ***/
  150. public void removeProtocolCommandistener(ProtocolCommandListener listener)
  151. {
  152. _commandSupport_.removeProtocolCommandListener(listener);
  153. }
  154. /***
  155. * Sets POP3 client state. This must be one of the
  156. * <code>_STATE</code> constants.
  157. * <p>
  158. * @param state The new state.
  159. ***/
  160. public void setState(int state)
  161. {
  162. __popState = state;
  163. }
  164. /***
  165. * Returns the current POP3 client state.
  166. * <p>
  167. * @return The current POP3 client state.
  168. ***/
  169. public int getState()
  170. {
  171. return __popState;
  172. }
  173. /***
  174. * Retrieves the additional lines of a multi-line server reply.
  175. ***/
  176. public void getAdditionalReply() throws IOException
  177. {
  178. String line;
  179. line = _reader.readLine();
  180. while (line != null)
  181. {
  182. _replyLines.addElement(line);
  183. if (line.equals("."))
  184. break;
  185. line = _reader.readLine();
  186. }
  187. }
  188. /***
  189. * Disconnects the client from the server, and sets the state to
  190. * <code> DISCONNECTED_STATE </code>. The reply text information
  191. * from the last issued command is voided to allow garbage collection
  192. * of the memory used to store that information.
  193. * <p>
  194. * @exception IOException If there is an error in disconnecting.
  195. ***/
  196. public void disconnect() throws IOException
  197. {
  198. super.disconnect();
  199. _reader = null;
  200. __writer = null;
  201. _lastReplyLine = null;
  202. _replyLines.setSize(0);
  203. setState(DISCONNECTED_STATE);
  204. }
  205. /***
  206. * Sends a command an arguments to the server and returns the reply code.
  207. * <p>
  208. * @param command The POP3 command to send.
  209. * @param args The command arguments.
  210. * @return The server reply code (either POP3Reply.OK or POP3Reply.ERROR).
  211. ***/
  212. public int sendCommand(String command, String args) throws IOException
  213. {
  214. String message;
  215. __commandBuffer.setLength(0);
  216. __commandBuffer.append(command);
  217. if (args != null)
  218. {
  219. __commandBuffer.append(' ');
  220. __commandBuffer.append(args);
  221. }
  222. __commandBuffer.append(SocketClient.NETASCII_EOL);
  223. __writer.write(message = __commandBuffer.toString());
  224. __writer.flush();
  225. if (_commandSupport_.getListenerCount() > 0)
  226. _commandSupport_.fireCommandSent(command, message);
  227. __getReply();
  228. return _replyCode;
  229. }
  230. /***
  231. * Sends a command with no arguments to the server and returns the
  232. * reply code.
  233. * <p>
  234. * @param command The POP3 command to send.
  235. * @return The server reply code (either POP3Reply.OK or POP3Reply.ERROR).
  236. ***/
  237. public int sendCommand(String command) throws IOException
  238. {
  239. return sendCommand(command, null);
  240. }
  241. /***
  242. * Sends a command an arguments to the server and returns the reply code.
  243. * <p>
  244. * @param command The POP3 command to send
  245. * (one of the POP3Command constants).
  246. * @param args The command arguments.
  247. * @return The server reply code (either POP3Reply.OK or POP3Reply.ERROR).
  248. ***/
  249. public int sendCommand(int command, String args) throws IOException
  250. {
  251. return sendCommand(POP3Command._commands[command], args);
  252. }
  253. /***
  254. * Sends a command with no arguments to the server and returns the
  255. * reply code.
  256. * <p>
  257. * @param command The POP3 command to send
  258. * (one of the POP3Command constants).
  259. * @return The server reply code (either POP3Reply.OK or POP3Reply.ERROR).
  260. ***/
  261. public int sendCommand(int command) throws IOException
  262. {
  263. return sendCommand(POP3Command._commands[command], null);
  264. }
  265. /***
  266. * Returns an array of lines received as a reply to the last command
  267. * sent to the server. The lines have end of lines truncated. If
  268. * the reply is a single line, but its format ndicates it should be
  269. * a multiline reply, then you must call
  270. * <a href="#getAdditionalReply"> getAdditionalReply() </a> to
  271. * fetch the rest of the reply, and then call <code>getReplyStrings</code>
  272. * again. You only have to worry about this if you are implementing
  273. * your own client using the <a href="#sendComand"> sendCommand </a> methods.
  274. * <p>
  275. * @return The last server response.
  276. ***/
  277. public String[] getReplyStrings()
  278. {
  279. String[] lines;
  280. lines = new String[_replyLines.size()];
  281. _replyLines.copyInto(lines);
  282. return lines;
  283. }
  284. /***
  285. * Returns the reply to the last command sent to the server.
  286. * The value is a single string containing all the reply lines including
  287. * newlines. If the reply is a single line, but its format ndicates it
  288. * should be a multiline reply, then you must call
  289. * <a href="#getAdditionalReply"> getAdditionalReply() </a> to
  290. * fetch the rest of the reply, and then call <code>getReplyString</code>
  291. * again. You only have to worry about this if you are implementing
  292. * your own client using the <a href="#sendComand"> sendCommand </a> methods.
  293. * <p>
  294. * @return The last server response.
  295. ***/
  296. public String getReplyString()
  297. {
  298. Enumeration enum;
  299. StringBuffer buffer = new StringBuffer(256);
  300. enum = _replyLines.elements();
  301. while (enum.hasMoreElements())
  302. {
  303. buffer.append((String)enum.nextElement());
  304. buffer.append(SocketClient.NETASCII_EOL);
  305. }
  306. return buffer.toString();
  307. }
  308. }