#include #include #include using namespace std; #define SERVER #define USER #define PASS #define PORT 143 #define MAILBOX "INBOX.test" #define ROOTCERTS "/etc/ssl/certs" class myTracer : public vmime::net::tracer { public: myTracer( const vmime::shared_ptr & serv, const int connectionId ) : m_service(serv), m_connectionId(connectionId) { } void traceSend(const vmime::string& line) { cout << "[" << m_service->getProtocolName() << ":" << m_connectionId << "] C: " << line << std::endl; } void traceReceive(const vmime::string& line) { cout << "[" << m_service->getProtocolName() << ":" << m_connectionId << "] S: " << line << std::endl; } private: vmime::shared_ptr m_service; const int m_connectionId; }; class myTracerFactory : public vmime::net::tracerFactory { public: myTracerFactory(){ } // virtual shared_ptr create(shared_ptr serv, const int connectionId) = 0; shared_ptr create( const shared_ptr serv, const int connectionId ) { return vmime::make_shared (serv, connectionId); } private: }; class rootCertificateVerifier : public vmime::security::cert::defaultCertificateVerifier { public: rootCertificateVerifier() { if(!m_rootCerts.empty()) { setX509RootCAs(m_rootCerts); return; } auto factory = vmime::platform::getHandler()->getFileSystemFactory(); auto path = vmime::utility::path::fromString(ROOTCERTS, "/", vmime::charsets::UTF_8); auto dir = factory->create(path); auto it = dir->getFiles(); while(it->hasMoreElements()) { auto file = it->nextElement(); if(!file->isFile()) continue; auto cert = vmime::security::cert::X509Certificate::import(*file->getFileReader()->getInputStream()); if(cert) m_rootCerts.push_back(cert); } setX509RootCAs(m_rootCerts); } private: static std::vector > m_rootCerts; }; std::vector > rootCertificateVerifier::m_rootCerts; void Run() { vmime::shared_ptr session = vmime::net::session::create(); vmime::utility::url url(SERVER); url.setUsername(USER); url.setPassword(PASS); url.setPort(PORT); vmime::shared_ptr store = session->getStore(url); store->setProperty("connection.tls", true); store->setCertificateVerifier(vmime::make_shared ()); store->setTracerFactory(vmime::make_shared ()); store->connect(); while(1) { auto path = vmime::utility::path::fromString(MAILBOX, ".", vmime::charsets::UTF_8); auto folder = store->getFolder(path); folder->open (vmime::net::folder::MODE_READ_WRITE); if(!folder->isOpen()) { throw std::runtime_error("Could not open IMAP folder " + path.toString(".", vmime::charsets::UTF_8)); } int msgc = folder->getStatus()->getMessageCount(); if(msgc == 0) { sleep(10); continue; } auto msg = folder->getMessage(1); folder->fetchMessage(msg, vmime::net::fetchAttributes::STRUCTURE ); } } int main(int argc, char* argv[]) { while(1) { try { Run(); }catch(std::exception & exc) { cout << "Exception: " << exc.what() << endl; } } }