1 //
2 // VMime library (http://www.vmime.org)
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // Linking this library statically or dynamically with other modules is making
20 // a combined work based on this library.  Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23 
24 #ifndef VMIME_NET_SMTP_POP3RESPONSE_HPP_INCLUDED
25 #define VMIME_NET_SMTP_POP3RESPONSE_HPP_INCLUDED
26 
27 
28 #include "vmime/config.hpp"
29 
30 
31 #if VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_POP3
32 
33 
34 #include "vmime/object.hpp"
35 #include "vmime/base.hpp"
36 
37 #include "vmime/utility/outputStream.hpp"
38 #include "vmime/utility/progressListener.hpp"
39 
40 #include "vmime/net/socket.hpp"
41 #include "vmime/net/tracer.hpp"
42 
43 
44 namespace vmime {
45 namespace net {
46 
47 
48 class timeoutHandler;
49 
50 
51 namespace pop3 {
52 
53 
54 class POP3Connection;
55 
56 
57 /** A POP3 response, as sent by the server.
58   */
59 class VMIME_EXPORT POP3Response : public object
60 {
61 public:
62 
63 	/** Possible response codes. */
64 	enum ResponseCode
65 	{
66 		CODE_OK = 0,
67 		CODE_READY,
68 		CODE_ERR
69 	};
70 
71 
72 	/** Receive and parse a POP3 response from the
73 	  * specified connection.
74 	  *
75 	  * @param conn connection from which to read
76 	  * @return POP3 response
77 	  * @throws exceptions::operation_timed_out if no data
78 	  * has been received within the granted time
79 	  */
80 	static shared_ptr <POP3Response> readResponse(shared_ptr <POP3Connection> conn);
81 
82 	/** Receive and parse a multiline POP3 response from
83 	  * the specified connection.
84 	  *
85 	  * @param conn connection from which to read
86 	  * @return POP3 response
87 	  * @throws exceptions::operation_timed_out if no data
88 	  * has been received within the granted time
89 	  */
90 	static shared_ptr <POP3Response> readMultilineResponse(shared_ptr <POP3Connection> conn);
91 
92 	/** Receive and parse a large POP3 response (eg. message data)
93 	  * from the specified connection.
94 	  *
95 	  * @param conn connection from which to read
96 	  * @param os output stream to which response data will be written
97 	  * @param progress progress listener (can be NULL)
98 	  * @param predictedSize estimated size of response data (in bytes)
99 	  * @return POP3 response
100 	  * @throws exceptions::operation_timed_out if no data
101 	  * has been received within the granted time
102 	  */
103 	static shared_ptr <POP3Response> readLargeResponse
104 		(shared_ptr <POP3Connection> conn, utility::outputStream& os,
105 		 utility::progressListener* progress, const size_t predictedSize);
106 
107 
108 	/** Returns whether the response is successful ("OK").
109 	  *
110 	  * @return true if the response if successful, false otherwise
111 	  */
112 	bool isSuccess() const;
113 
114 	/** Return the POP3 response code.
115 	  *
116 	  * @return response code
117 	  */
118 	ResponseCode getCode() const;
119 
120 	/** Return the POP3 response text (first line).
121 	  *
122 	  * @return response text
123 	  */
124 	const string getText() const;
125 
126 	/** Return the first POP3 response line.
127 	  *
128 	  * @return first response line
129 	  */
130 	const string getFirstLine() const;
131 
132 	/** Return the response line at the specified position.
133 	  *
134 	  * @param pos line index
135 	  * @return line at the specified index
136 	  */
137 	const string getLineAt(const size_t pos) const;
138 
139 	/** Return the number of lines in the response.
140 	  *
141 	  * @return number of lines in the response
142 	  */
143 	size_t getLineCount() const;
144 
145 private:
146 
147 	POP3Response(shared_ptr <socket> sok, shared_ptr <timeoutHandler> toh, shared_ptr <tracer> tracer);
148 
149 	void readResponseImpl(string& buffer, const bool multiLine);
150 	size_t readResponseImpl
151 		(string& firstLine, utility::outputStream& os,
152 		 utility::progressListener* progress, const size_t predictedSize);
153 
154 
155 	static bool stripFirstLine(const string& buffer, string& result, string* firstLine);
156 
157 	static ResponseCode getResponseCode(const string& buffer);
158 
159 	static void stripResponseCode(const string& buffer, string& result);
160 
161 	static bool checkTerminator(string& buffer, const bool multiLine);
162 	static bool checkOneTerminator(string& buffer, const string& term);
163 
164 
165 	shared_ptr <socket> m_socket;
166 	shared_ptr <timeoutHandler> m_timeoutHandler;
167 	shared_ptr <tracer> m_tracer;
168 
169 	string m_firstLine;
170 	ResponseCode m_code;
171 	string m_text;
172 
173 	std::vector <string> m_lines;
174 };
175 
176 
177 } // pop3
178 } // net
179 } // vmime
180 
181 
182 #endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_POP3
183 
184 #endif // VMIME_NET_SMTP_POP3RESPONSE_HPP_INCLUDED
185