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

Dependents:   mmain pop3demo

Files at this revision

API Documentation at this revision

Comitter:
hlipka
Date:
Sat Jan 29 23:27:26 2011 +0000
Parent:
1:f2b05d1c91be
Child:
3:37570217ae90
Commit message:
changed to use the TcpLineStream library - makes it simples and faster

Changed in this revision

pop3.cpp Show annotated file Show diff for this revision Revisions of this file
pop3.h Show annotated file Show diff for this revision Revisions of this file
--- a/pop3.cpp	Tue Jan 11 21:35:35 2011 +0000
+++ b/pop3.cpp	Sat Jan 29 23:27:26 2011 +0000
@@ -20,45 +20,17 @@
     _username=username;
     _password=password;
     _servername=servername;
-
-    _readpos=0;
-    _readlen=0;
-
+    
     _closed=false;
 
-    _socket.setOnEvent(this,&Pop3::onTCPSocketEvent);
+    _stream=new TCPLineStream(servername,110,"\r\n");
+
 }
 
 Pop3::~Pop3() {
     close();
 }
 
-void Pop3::onTCPSocketEvent(TCPSocketEvent e) {
-//    printf("New TCPSocketEvent: %d\n",e);
-    switch (e) {
-        case TCPSOCKET_CONNECTED:
-            _connected = true;
-            _connecting=false;
-            break;
-
-        case TCPSOCKET_READABLE:
-            break;
-        case TCPSOCKET_WRITEABLE:
-            break;
-
-        case TCPSOCKET_CONTIMEOUT:
-        case TCPSOCKET_CONRST:
-        case TCPSOCKET_CONABRT:
-        case TCPSOCKET_ERROR:
-        case TCPSOCKET_DISCONNECTED: {
-            _socket.close();
-            _connected = false;
-            _connecting=false;
-        }
-        break;
-    }
-}
-
 list<string> *Pop3::getMessages() {
     Pop3CommandResponse* pcr=sendCommandMultiResponse("LIST");
     if (!pcr->success) {
@@ -123,27 +95,17 @@
 
 Pop3CommandResponse* Pop3::sendCommand(string cmd) {
 //    printf("send [%s]\n",cmd.c_str());
-    if (!_connected) {
-        printf("not connected\n");
-        return NULL;
-    }
-    int err=_socket.send(cmd.append("\r\n").c_str(),cmd.length()+2);
-    Net::poll();
-    string r=readResponseLine();
+    _stream->sendLine(cmd);
+    string r=_stream->readLine();
     return new Pop3CommandResponse(r);
 }
 
 Pop3CommandResponse* Pop3::sendCommandMultiResponse(string cmd) {
     // first, send command
 //    printf("send [%s]\n",cmd.c_str());
-    if (!_connected) {
-        printf("not connected\n");
-        return NULL;
-    }
-    int err=_socket.send(cmd.append("\r\n").c_str(),cmd.length()+2);
-    Net::poll();
+    _stream->sendLine(cmd);
     // read first response line -> contains status
-    string r=readResponseLine();
+    string r=_stream->readLine();
 //    printf("response=[%s]\n",r.c_str());
     // create response object to collect remaining lines
     Pop3CommandResponse *pcr=new Pop3CommandResponse(r);
@@ -151,7 +113,7 @@
         return pcr;
     // read other lines, stop when marker reached
     while (true) {
-        r=readResponseLine();
+        r=_stream->readLine();
         if (pcr->addLine(r))
             break;
     }
@@ -159,48 +121,11 @@
 }
 
 bool Pop3::init() {
-    // retrieve IP address for hostname
-    DNSResolver *dnsr=new DNSResolver();
-    IpAddr addr=dnsr->resolveName(_servername);
-    delete dnsr;
-
-    if (addr.isNull()) {
-        printf("cannot resolve server name %s\n",_servername);
-        return false;
-    }
-
-//    printf("server ip=%i.%i.%i.%i\n",addr[0],addr[1],addr[2],addr[3]);
-
-    _connecting=true;
-
-    // create tcp socket to server
-    Host host(addr, 110);
-
-    TCPSocketErr bindErr = _socket.connect(host);
-
-    if (bindErr != 0) {
-        printf("connection error %i\n", bindErr);
-        return false;
-    }
-
-    // wait for connection established
-    Timer tmr;
-    tmr.start();
-
-    while (_connecting) {
-        Net::poll();
-        wait(0.1);
-        if (tmr.read()>10)
-            break;
-    }
-
-    if (!_connected) {
-        printf("error - could not connect (timeout)\n");
-        return false;
-    }
+    if (!_stream->open())
+        return NULL;
 
     // read server banner
-    string banner=readResponseLine();
+    string banner=_stream->readLine();
 //    printf("banner=[%s]\n",banner.c_str());
 
     Pop3CommandResponse pcr(banner);
@@ -239,65 +164,15 @@
     Pop3CommandResponse *pcr=sendCommand("CLOSE");
     delete pcr;
 
-    while (_connected) {
-        Net::poll();
-        // read remaining data
-        int err=_socket.recv(_readbuf,BUFSIZE-1);
-        if (err<=0)
-            break;
+    if (NULL!=_stream) {
+        _stream->close();
+        delete _stream;
+        _stream=NULL;
     }
 
-    _socket.resetOnEvent();
-    _socket.close();
-
     _closed=true;
 }
 
-string Pop3::readResponseLine() {
-    string r;
-    bool got_r=false;
-
-    if (!_connected)
-        return r;
-
-    while (true) {
-        Net::poll();
-        if (_readlen>_readpos) {
-            while (_readbuf[_readpos]!=0 && _readpos<_readlen) {
-                char c=_readbuf[_readpos++];
-                if (!got_r) {
-                    if (c=='\r') {
-                        got_r=true;
-                    } else {
-                        r.push_back(c);
-                    }
-                } else {
-                    if (c=='\n') {
-                        return r;
-                    } else {
-                        r.push_back('\r'); // push missed \r also, so push it to string
-                        r.push_back(c);
-                        got_r=false;
-                    }
-                }
-            }
-        } else {
-            int err=_socket.recv(_readbuf,BUFSIZE-1);
-            if (err < 0) {
-                printf("error while receiving data: %i!\n",err);
-            } else if (err>0) {
-                _readbuf[err]=0;
-                _readlen=err;
-                _readpos=0;
-            }
-        }
-        wait(0.1);
-        if (_connected==false) {
-            return r;
-        }
-    }
-}
-
 Pop3CommandResponse::Pop3CommandResponse(string line) {
     if (0==line.find("+OK")) {
         success=true;
--- a/pop3.h	Tue Jan 11 21:35:35 2011 +0000
+++ b/pop3.h	Sat Jan 29 23:27:26 2011 +0000
@@ -4,9 +4,7 @@
 #include <string>
 #include <list>
 
-#include "TCPSocket.h"
-
-#define BUFSIZE 256
+#include "tcplinestream.h"
 
 using namespace std;
 
@@ -96,23 +94,13 @@
         Pop3CommandResponse* sendCommand(string cmd);
         Pop3CommandResponse* sendCommandMultiResponse(string cmd);
         
-        void onTCPSocketEvent(TCPSocketEvent e);
-        
-        string readResponseLine();
-        
-        bool _connecting;
-        bool _connected;
-        bool _closed;
-        TCPSocket _socket;
+        TCPLineStream *_stream;
         
         const char* _username;
         const char* _password;
         const char* _servername;
         
-        char _readbuf[BUFSIZE];
-        int _readpos;
-        int _readlen;
-        
+        bool _closed;
 };