Small library for reading mail messages via POP3. Currently doesn\'t return all header fields, and does only plain text authorization.

Dependents:   mmain pop3demo

Committer:
hlipka
Date:
Mon Jan 10 21:11:01 2011 +0000
Revision:
0:ec8a3cef200d
Child:
1:f2b05d1c91be
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 0:ec8a3cef200d 1 #ifndef __POP3_H__
hlipka 0:ec8a3cef200d 2 #define __POP3_H__
hlipka 0:ec8a3cef200d 3
hlipka 0:ec8a3cef200d 4 #include <string>
hlipka 0:ec8a3cef200d 5 #include <list>
hlipka 0:ec8a3cef200d 6
hlipka 0:ec8a3cef200d 7 #include "mbed.h"
hlipka 0:ec8a3cef200d 8 #include "EthernetNetIf.h"
hlipka 0:ec8a3cef200d 9 #include "TCPSocket.h"
hlipka 0:ec8a3cef200d 10
hlipka 0:ec8a3cef200d 11 #define BUFSIZE 256
hlipka 0:ec8a3cef200d 12
hlipka 0:ec8a3cef200d 13 using namespace std;
hlipka 0:ec8a3cef200d 14
hlipka 0:ec8a3cef200d 15 /**
hlipka 0:ec8a3cef200d 16 The class representing a received mail message. All fields are stored in un-altered form, so character set conversion might need to be performed.
hlipka 0:ec8a3cef200d 17 */
hlipka 0:ec8a3cef200d 18 class Pop3Message
hlipka 0:ec8a3cef200d 19 {
hlipka 0:ec8a3cef200d 20 public:
hlipka 0:ec8a3cef200d 21 /**
hlipka 0:ec8a3cef200d 22 the 'From:' header
hlipka 0:ec8a3cef200d 23 */
hlipka 0:ec8a3cef200d 24 string from;
hlipka 0:ec8a3cef200d 25 /**
hlipka 0:ec8a3cef200d 26 the 'Subject:' header
hlipka 0:ec8a3cef200d 27 */
hlipka 0:ec8a3cef200d 28 string subject;
hlipka 0:ec8a3cef200d 29 /**
hlipka 0:ec8a3cef200d 30 the actual mail content, line by line
hlipka 0:ec8a3cef200d 31 */
hlipka 0:ec8a3cef200d 32 list<string> content;
hlipka 0:ec8a3cef200d 33 };
hlipka 0:ec8a3cef200d 34
hlipka 0:ec8a3cef200d 35 class Pop3CommandResponse;
hlipka 0:ec8a3cef200d 36
hlipka 0:ec8a3cef200d 37 /**
hlipka 0:ec8a3cef200d 38 Handling all POP3 related aspects. All operations are synchronous. This class expects a set-up network connection.
hlipka 0:ec8a3cef200d 39
hlipka 0:ec8a3cef200d 40 Needed libraries:
hlipka 0:ec8a3cef200d 41 * DNSResolver
hlipka 0:ec8a3cef200d 42 * NetServices (for TCP socket), or NetServicesMin
hlipka 0:ec8a3cef200d 43 */
hlipka 0:ec8a3cef200d 44 class Pop3
hlipka 0:ec8a3cef200d 45 {
hlipka 0:ec8a3cef200d 46 public:
hlipka 0:ec8a3cef200d 47 /**
hlipka 0:ec8a3cef200d 48 Creates the POP3 handler
hlipka 0:ec8a3cef200d 49
hlipka 0:ec8a3cef200d 50 @param servername the DNS name of the server
hlipka 0:ec8a3cef200d 51 @param username the username
hlipka 0:ec8a3cef200d 52 @param password the users password
hlipka 0:ec8a3cef200d 53 */
hlipka 0:ec8a3cef200d 54 Pop3(const char* servername, const char* username, const char* password);
hlipka 0:ec8a3cef200d 55
hlipka 0:ec8a3cef200d 56 /**
hlipka 0:ec8a3cef200d 57 closes the connection, and cleans up resources
hlipka 0:ec8a3cef200d 58 */
hlipka 0:ec8a3cef200d 59 ~Pop3();
hlipka 0:ec8a3cef200d 60
hlipka 0:ec8a3cef200d 61 /**
hlipka 0:ec8a3cef200d 62 Retrieve a list of all message IDs. This method doesn't care whether the messages have been read already or not.
hlipka 0:ec8a3cef200d 63 The IDs are not sorted, but returned in the order the server delivers them.
hlipka 0:ec8a3cef200d 64
hlipka 0:ec8a3cef200d 65 @return the list of all message IDs stored at the server.
hlipka 0:ec8a3cef200d 66 */
hlipka 0:ec8a3cef200d 67 list<string> *getMessages();
hlipka 0:ec8a3cef200d 68
hlipka 0:ec8a3cef200d 69 /**
hlipka 0:ec8a3cef200d 70 retrieves as single message
hlipka 0:ec8a3cef200d 71
hlipka 0:ec8a3cef200d 72 @param id the message ID to retrieve
hlipka 0:ec8a3cef200d 73 @param getSig when false, the signature will be stripped (separated by the line '-- ')
hlipka 0:ec8a3cef200d 74 @param deleteOnReceive when true the message will be deleted after a sucessful retrieval
hlipka 0:ec8a3cef200d 75 @return the message container
hlipka 0:ec8a3cef200d 76 */
hlipka 0:ec8a3cef200d 77 Pop3Message* getMessage(string id, bool getSig=false, bool deleteOnReceive=false);
hlipka 0:ec8a3cef200d 78
hlipka 0:ec8a3cef200d 79 /**
hlipka 0:ec8a3cef200d 80 @param id the ID of the message to be deleted
hlipka 0:ec8a3cef200d 81 @return true if the deletion was sucessful
hlipka 0:ec8a3cef200d 82 */
hlipka 0:ec8a3cef200d 83 bool deleteMessage(string id);
hlipka 0:ec8a3cef200d 84
hlipka 0:ec8a3cef200d 85 /**
hlipka 0:ec8a3cef200d 86 Connects to the server. Needs the ethernet connection already set up.
hlipka 0:ec8a3cef200d 87
hlipka 0:ec8a3cef200d 88 @return true if the connection has been made (and the user logged in)
hlipka 0:ec8a3cef200d 89 */
hlipka 0:ec8a3cef200d 90 bool init();
hlipka 0:ec8a3cef200d 91
hlipka 0:ec8a3cef200d 92 /**
hlipka 0:ec8a3cef200d 93 closes the connection, and cleans up resources
hlipka 0:ec8a3cef200d 94 */
hlipka 0:ec8a3cef200d 95 void close();
hlipka 0:ec8a3cef200d 96
hlipka 0:ec8a3cef200d 97 private:
hlipka 0:ec8a3cef200d 98 Pop3CommandResponse* sendCommand(string cmd);
hlipka 0:ec8a3cef200d 99 Pop3CommandResponse* sendCommandMultiResponse(string cmd);
hlipka 0:ec8a3cef200d 100
hlipka 0:ec8a3cef200d 101 void onTCPSocketEvent(TCPSocketEvent e);
hlipka 0:ec8a3cef200d 102
hlipka 0:ec8a3cef200d 103 string readResponseLine();
hlipka 0:ec8a3cef200d 104
hlipka 0:ec8a3cef200d 105 bool _connecting;
hlipka 0:ec8a3cef200d 106 bool _connected;
hlipka 0:ec8a3cef200d 107 bool _closed;
hlipka 0:ec8a3cef200d 108 TCPSocket _socket;
hlipka 0:ec8a3cef200d 109
hlipka 0:ec8a3cef200d 110 const char* _username;
hlipka 0:ec8a3cef200d 111 const char* _password;
hlipka 0:ec8a3cef200d 112 const char* _servername;
hlipka 0:ec8a3cef200d 113
hlipka 0:ec8a3cef200d 114 char _readbuf[BUFSIZE];
hlipka 0:ec8a3cef200d 115 int _readpos;
hlipka 0:ec8a3cef200d 116 int _readlen;
hlipka 0:ec8a3cef200d 117
hlipka 0:ec8a3cef200d 118 };
hlipka 0:ec8a3cef200d 119
hlipka 0:ec8a3cef200d 120
hlipka 0:ec8a3cef200d 121 #endif