Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cMailPop3Client.h
Go to the documentation of this file.
1 //
5 //
6 #ifndef _INC_cMailPop3Client_H
7 #define _INC_cMailPop3Client_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../Proto/cMailMessage.h"
13 
14 #if 0
15 namespace GrayLib
16 {
17  class GRAYLIB_LINK cMailPop3Client
18  {
23  //
24  public:
25  cMailPop3Client();
26 
27  //
28  // The destructor.
29  // This does not automatically logout of the server on a nice way (when connected).
30  // However, it makes sure the socket will disconnect from the server. Users should call the Disconnect() method
31  // in order to disconnect from a server though.
32  //
33  ~cMailPop3Client();
34 
35  //
36  // Get the last error in form of a string.
37  // This will contain a reason returned by the POP3 server, when a given operation fails.
38  // @result The string containing the reason for the last error.
39  //
40  const cString& GetLastErrorStr() const;
41 
42  //
43  // Disconnect from the POP3 server according to the POP3 protocol.
44  // You should never just close the connection by closing the socket. So always use this method to disconnect
45  // from the server.
46  //
47  void Disconnect();
48 
49  //
50  // Try to connect to a specified POP3 mail server.
51  // @arg server The string containing the server address, for example "pop.somedomain.com".
52  // @arg portNr The port number to connect to, which is usually 110 for a POP3 server.
53  // @result Returns true when a connection has been established or false when not.
54  //
55  HRESULT Connect(const cString& server, const int portNr=110);
56 
57  //
58  // Try to login to the POP3 mail server. You first have to be connected (using the Connect() method) before
59  // trying to login. You have to specify a username and password.
60  // @arg username The username to login with.
61  // @arg password The password to login with.
62  // @result Returns true when successfully logged in, otherwise false.
63  //
64  bool Login(const cString& username, const cString& password);
65 
66  //
67  // Get the number of messages which are stored on the server.
68  // You have to be connected and logged in before using this method.
69  // Do not put this method inside a for-all-messages loop, because it needs to download some information from the server
70  // everytime it is being called. Fortunately the number of messages cannot change during your session, even if you receive new mail
71  // when you are connecting to the mail server.
72  // @result The number of messages on the server, or -1 when there was an error.
73  //
74  int GetNumMessagesOnServer();
75 
76  //
77  // Download the message header of a given message.
78  // This is the fastest way to find out the details about the messages stored on the server, without actually
79  // downloading the whole message.
80  // @arg outHeader A pointer to the message header you want to fill with the message information.
81  // @arg messageNr The message number, which must be in range of [0..GetNumMessagesFromServer()-1].
82  // @result Returns true when the header has been successfully downloaded, or false when it didn't work out good.
83  //
84  bool DownloadMessageHeader(cMailMessageHeader* outHeader, const int messageNr);
85 
86  //
87  // Download a complete message from the server. This includes the message header as well.
88  // @arg outMessage A pointer to the message object, which you want to fill with the message data.
89  // @arg messageNr The message number, which must be in range of [0..GetNumMessagesFromServer()-1].
90  // @result Returns true when the message has been successfully downloaded, or false when it didn't work out good.
91  //
92  bool DownloadMessage(cMailMessage* outMessage, const int messageNr);
93 
94  //
95  // Delete a given message from the server.
96  // The message will not be deleted immediately, but will only be marked for deletion.
97  // After you successfully logged out from the server (using the Disconnect()) method, the messages which are marked
98  // for deletion will be deleted for real.
99  // @arg messageNr The message number, which must be in range of [0..GetNumMessagesFromServer()-1].
100  // @result Returns true when we successfully marked the message to be deleted, otherwise false is returned.
101  //
102  bool DeleteMessage(const int messageNr);
103 
104  //
105  // Try to download all the messages on the server.
106  // @arg outMessages A pointer to the array to which we this method should add all the downloaded message objects.
107  // @arg delFromServer When set to true, all messages will be marked for deletion, for once you successfully logoff from the server, using the Disconnect() method.
108  // @result Returns true when all messages have been downloaded, otherwise false is returned.
109  //
110  bool DownloadAllMessages(MCore::Array<cMailMessage>* outMessages, bool delFromServer=false);
111 
112  //
113  // Try to download all the message headers on the server.
114  // @arg outMessages A pointer to the array to which we this method should add all the downloaded message header objects.
115  // @result Returns true when all message headers have been downloaded, otherwise false is returned.
116  //
117  bool DownloadAllMessageHeaders(MCore::Array<cMailMessageHeader>* outHeaders);
118 
119  //-----------------------
120 
121  // override virtual methods from the NetworkSystem base class.
122  // they don't do anything though, since we do not use any asynchronous sending and receiving.
123  void OnRead(cNetSocket* socket)
124  {}
125  void OnWrite(cNetSocket* socket)
126  {}
127  void OnClose(cNetSocket* socket)
128  {}
129  void OnAccept(cNetSocket* socket)
130  {}
131 
132  private:
133  cString mLastError; //*< The last error which occurred.
134  cNetSocket* mSocket; //*< The socket we use to communicate with the POP3 server.
135 
136  //
137  // Extract the header from a given string.
138  // This will fill in the members of the cMailMessageHeader class.
139  // @arg messageOrHeader The string containing the message or header as stored on the POP3 server.
140  // @arg outHeader A pointer to the header object of which this method should write the extracted information to.
141  //
142  void ExtractHeader(const cString& messageOrHeader, cMailMessageHeader* outHeader);
143 
144  //
145  // Read incoming data from the mail server.
146  // @result The string containing the received data.
147  //
148  cString Read();
149 
150  //
151  // Check if the last sent command to the server has been executed successfully or not.
152  // @arg input The input, which is the received responds from the server, after we sent the command.
153  // @result Returns true when the command has been successfully executed, otherwise false is returned.
154  //
155  bool IsOK(const cString& input) const;
156 
157  //
158  // Send a command to the server.
159  // @arg command The command to send to the server.
160  // @result Returns true when we have successfully sent the command to the server.
161  //
162  bool Send(const cString& command);
163 
164  //
165  // Wait for the responds of the server after we have sent a command to it.
166  // In case the respond indicates something went wrong, we extract the returned reason/error message, which is
167  // then stored inside the string which is returned by GetLastError().
168  // @arg Returns true when the command is successfully executed, otherwise false is returned.
169  //
170  bool WaitForOK();
171 
172  //
173  // Extracts the error message from a server responds when something went wrong.
174  // @arg input The input string, which is the responds from the server.
175  // @result A string containing the error message, which has been extracted from the server responds.
176  //
177  cString ExtractError(const cString& input);
178  };
179 };
180 #endif
181 
182 #endif // _INC_cMailPop3Client_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cMesh.h:22
cStringT< GChar_t > cString
Definition: cString.h:636