1 | // |
---|
2 | // MailStream.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/MailStream.h#1 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: Mail |
---|
8 | // Module: MailStream |
---|
9 | // |
---|
10 | // Definition of the MailStream class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_MailStream_INCLUDED |
---|
40 | #define Net_MailStream_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/UnbufferedStreamBuf.h" |
---|
45 | #include <istream> |
---|
46 | #include <ostream> |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | namespace Net { |
---|
51 | |
---|
52 | |
---|
53 | class Net_API MailStreamBuf: public Poco::UnbufferedStreamBuf |
---|
54 | /// The sole purpose of this stream buffer is to replace |
---|
55 | /// a "\r\n.\r\n" character sequence with a "\r\n..\r\n" sequence for |
---|
56 | /// output streams and vice-versa for input streams. |
---|
57 | /// |
---|
58 | /// This is used when sending mail messages to SMTP servers, or |
---|
59 | /// receiving mail messages from POP servers. |
---|
60 | /// |
---|
61 | /// See RFC 2181 (Simple Mail Transfer Protocol) and RFC 1939 |
---|
62 | /// (Post Office Protocol - Version 3) for more information. |
---|
63 | { |
---|
64 | public: |
---|
65 | MailStreamBuf(std::istream& istr); |
---|
66 | /// Creates the MailStreamBuf and connects it |
---|
67 | /// to the given input stream. |
---|
68 | |
---|
69 | MailStreamBuf(std::ostream& ostr); |
---|
70 | /// Creates the MailStreamBuf and connects it |
---|
71 | /// to the given output stream. |
---|
72 | |
---|
73 | ~MailStreamBuf(); |
---|
74 | /// Destroys the MailStreamBuf. |
---|
75 | |
---|
76 | void close(); |
---|
77 | /// Writes the terminating period, followed by |
---|
78 | /// CR-LF. |
---|
79 | |
---|
80 | protected: |
---|
81 | int readFromDevice(); |
---|
82 | int writeToDevice(char c); |
---|
83 | int readOne(); |
---|
84 | |
---|
85 | private: |
---|
86 | enum State |
---|
87 | { |
---|
88 | ST_DATA, |
---|
89 | ST_CR, |
---|
90 | ST_CR_LF, |
---|
91 | ST_CR_LF_DOT, |
---|
92 | ST_CR_LF_DOT_DOT, |
---|
93 | ST_CR_LF_DOT_CR, |
---|
94 | ST_CR_LF_DOT_CR_LF |
---|
95 | }; |
---|
96 | |
---|
97 | std::istream* _pIstr; |
---|
98 | std::ostream* _pOstr; |
---|
99 | std::string _buffer; |
---|
100 | State _state; |
---|
101 | }; |
---|
102 | |
---|
103 | |
---|
104 | class Net_API MailIOS: public virtual std::ios |
---|
105 | /// The base class for MailInputStream and MailOutputStream. |
---|
106 | /// |
---|
107 | /// This class provides common methods and is also needed to ensure |
---|
108 | /// the correct initialization order of the stream buffer and base classes. |
---|
109 | { |
---|
110 | public: |
---|
111 | MailIOS(std::istream& istr); |
---|
112 | /// Creates the MailIOS and connects it |
---|
113 | /// to the given input stream. |
---|
114 | |
---|
115 | MailIOS(std::ostream& ostr); |
---|
116 | /// Creates the MailIOS and connects it |
---|
117 | /// to the given output stream. |
---|
118 | |
---|
119 | ~MailIOS(); |
---|
120 | /// Destroys the stream. |
---|
121 | |
---|
122 | void close(); |
---|
123 | /// Writes the terminating period, followed by |
---|
124 | /// CR-LF. |
---|
125 | |
---|
126 | MailStreamBuf* rdbuf(); |
---|
127 | /// Returns a pointer to the underlying streambuf. |
---|
128 | |
---|
129 | protected: |
---|
130 | MailStreamBuf _buf; |
---|
131 | }; |
---|
132 | |
---|
133 | |
---|
134 | class Net_API MailInputStream: public MailIOS, public std::istream |
---|
135 | /// This class is used for reading E-Mail messages from a |
---|
136 | /// POP3 server. All occurences of "\r\n..\r\n" are replaced with |
---|
137 | /// "\r\n.\r\n". The first occurence of "\r\n.\r\n" denotes the end |
---|
138 | /// of the stream. |
---|
139 | { |
---|
140 | public: |
---|
141 | MailInputStream(std::istream& istr); |
---|
142 | /// Creates the MailInputStream and connects it |
---|
143 | /// to the given input stream. |
---|
144 | |
---|
145 | ~MailInputStream(); |
---|
146 | /// Destroys the MailInputStream. |
---|
147 | }; |
---|
148 | |
---|
149 | |
---|
150 | class Net_API MailOutputStream: public MailIOS, public std::ostream |
---|
151 | /// This class is used for writing E-Mail messages to a |
---|
152 | /// SMTP server. All occurences of "\r\n.\r\n" are replaced with |
---|
153 | /// "\r\n..\r\n". |
---|
154 | { |
---|
155 | public: |
---|
156 | MailOutputStream(std::ostream& ostr); |
---|
157 | /// Creates the MailOutputStream and connects it |
---|
158 | /// to the given input stream. |
---|
159 | |
---|
160 | ~MailOutputStream(); |
---|
161 | /// Destroys the MailOutputStream. |
---|
162 | }; |
---|
163 | |
---|
164 | |
---|
165 | } } // namespace Poco::Net |
---|
166 | |
---|
167 | |
---|
168 | #endif // Net_MailStream_INCLUDED |
---|