diff options
Diffstat (limited to '')
-rw-r--r-- | src/net/pop3/POP3Folder.cpp | 172 | ||||
-rw-r--r-- | src/net/pop3/POP3Message.cpp | 34 | ||||
-rw-r--r-- | src/net/pop3/POP3Store.cpp | 9 |
3 files changed, 130 insertions, 85 deletions
diff --git a/src/net/pop3/POP3Folder.cpp b/src/net/pop3/POP3Folder.cpp index 0b177a3e..acffff7d 100644 --- a/src/net/pop3/POP3Folder.cpp +++ b/src/net/pop3/POP3Folder.cpp @@ -30,23 +30,25 @@ namespace net { namespace pop3 { -POP3Folder::POP3Folder(const folder::path& path, POP3Store* store) +POP3Folder::POP3Folder(const folder::path& path, ref <POP3Store> store) : m_store(store), m_path(path), m_name(path.isEmpty() ? folder::path::component("") : path.getLastComponent()), m_mode(-1), m_open(false) { - m_store->registerFolder(this); + store->registerFolder(this); } POP3Folder::~POP3Folder() { - if (m_store) + ref <POP3Store> store = m_store.acquire(); + + if (store) { if (m_open) close(false); - m_store->unregisterFolder(this); + store->unregisterFolder(this); } else if (m_open) { @@ -98,7 +100,9 @@ const folder::path POP3Folder::getFullPath() const void POP3Folder::open(const int mode, bool failIfModeIsNotAvailable) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); if (m_path.isEmpty()) @@ -113,15 +117,15 @@ void POP3Folder::open(const int mode, bool failIfModeIsNotAvailable) } else if (m_path.getSize() == 1 && m_path[0].getBuffer() == "INBOX") { - m_store->sendRequest("STAT"); + store->sendRequest("STAT"); string response; - m_store->readResponse(response, false); + store->readResponse(response, false); - if (!m_store->isSuccessResponse(response)) + if (!store->isSuccessResponse(response)) throw exceptions::command_error("STAT", response); - m_store->stripResponseCode(response, response); + store->stripResponseCode(response, response); std::istringstream iss(response); iss >> m_messageCount; @@ -140,7 +144,9 @@ void POP3Folder::open(const int mode, bool failIfModeIsNotAvailable) void POP3Folder::close(const bool expunge) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); if (!isOpen()) @@ -148,10 +154,10 @@ void POP3Folder::close(const bool expunge) if (!expunge) { - m_store->sendRequest("RSET"); + store->sendRequest("RSET"); string response; - m_store->readResponse(response, false); + store->readResponse(response, false); } m_open = false; @@ -178,7 +184,9 @@ void POP3Folder::create(const int /* type */) const bool POP3Folder::exists() { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); return (m_path.isEmpty() || (m_path.getSize() == 1 && m_path[0].getBuffer() == "INBOX")); @@ -193,22 +201,26 @@ const bool POP3Folder::isOpen() const ref <message> POP3Folder::getMessage(const int num) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); else if (num < 1 || num > m_messageCount) throw exceptions::message_not_found(); - return vmime::create <POP3Message>(this, num); + return vmime::create <POP3Message>(thisRef().dynamicCast <POP3Folder>(), num); } std::vector <ref <message> > POP3Folder::getMessages(const int from, const int to) { + ref <POP3Store> store = m_store.acquire(); + const int to2 = (to == -1 ? m_messageCount : to); - if (!m_store) + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); @@ -216,9 +228,10 @@ std::vector <ref <message> > POP3Folder::getMessages(const int from, const int t throw exceptions::message_not_found(); std::vector <ref <message> > v; + ref <POP3Folder> thisFolder = thisRef().dynamicCast <POP3Folder>(); for (int i = from ; i <= to2 ; ++i) - v.push_back(vmime::create <POP3Message>(this, i)); + v.push_back(vmime::create <POP3Message>(thisFolder, i)); return (v); } @@ -226,19 +239,22 @@ std::vector <ref <message> > POP3Folder::getMessages(const int from, const int t std::vector <ref <message> > POP3Folder::getMessages(const std::vector <int>& nums) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); std::vector <ref <message> > v; + ref <POP3Folder> thisFolder = thisRef().dynamicCast <POP3Folder>(); for (std::vector <int>::const_iterator it = nums.begin() ; it != nums.end() ; ++it) { if (*it < 1|| *it > m_messageCount) throw exceptions::message_not_found(); - v.push_back(vmime::create <POP3Message>(this, *it)); + v.push_back(vmime::create <POP3Message>(thisFolder, *it)); } return (v); @@ -247,7 +263,9 @@ std::vector <ref <message> > POP3Folder::getMessages(const std::vector <int>& nu const int POP3Folder::getMessageCount() { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); @@ -258,22 +276,26 @@ const int POP3Folder::getMessageCount() ref <folder> POP3Folder::getFolder(const folder::path::component& name) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); - return vmime::create <POP3Folder>(m_path / name, m_store); + return vmime::create <POP3Folder>(m_path / name, store); } std::vector <ref <folder> > POP3Folder::getFolders(const bool /* recursive */) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); if (m_path.isEmpty()) { std::vector <ref <folder> > v; - v.push_back(vmime::create <POP3Folder>(folder::path::component("INBOX"), m_store)); + v.push_back(vmime::create <POP3Folder>(folder::path::component("INBOX"), store)); return (v); } else @@ -287,7 +309,9 @@ std::vector <ref <folder> > POP3Folder::getFolders(const bool /* recursive */) void POP3Folder::fetchMessages(std::vector <ref <message> >& msg, const int options, utility::progressListener* progress) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); @@ -301,7 +325,8 @@ void POP3Folder::fetchMessages(std::vector <ref <message> >& msg, const int opti for (std::vector <ref <message> >::iterator it = msg.begin() ; it != msg.end() ; ++it) { - (*it).dynamicCast <POP3Message>()->fetch(this, options); + (*it).dynamicCast <POP3Message>()->fetch + (thisRef().dynamicCast <POP3Folder>(), options); if (progress) progress->progress(++current, total); @@ -313,15 +338,15 @@ void POP3Folder::fetchMessages(std::vector <ref <message> >& msg, const int opti std::ostringstream command; command << "LIST"; - m_store->sendRequest(command.str()); + store->sendRequest(command.str()); // Get the response string response; - m_store->readResponse(response, true, NULL); + store->readResponse(response, true, NULL); - if (m_store->isSuccessResponse(response)) + if (store->isSuccessResponse(response)) { - m_store->stripFirstLine(response, response, NULL); + store->stripFirstLine(response, response, NULL); // C: LIST // S: +OK @@ -358,15 +383,15 @@ void POP3Folder::fetchMessages(std::vector <ref <message> >& msg, const int opti std::ostringstream command; command << "UIDL"; - m_store->sendRequest(command.str()); + store->sendRequest(command.str()); // Get the response string response; - m_store->readResponse(response, true, NULL); + store->readResponse(response, true, NULL); - if (m_store->isSuccessResponse(response)) + if (store->isSuccessResponse(response)) { - m_store->stripFirstLine(response, response, NULL); + store->stripFirstLine(response, response, NULL); // C: UIDL // S: +OK @@ -396,12 +421,15 @@ void POP3Folder::fetchMessages(std::vector <ref <message> >& msg, const int opti void POP3Folder::fetchMessage(ref <message> msg, const int options) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); - msg.dynamicCast <POP3Message>()->fetch(this, options); + msg.dynamicCast <POP3Message>()->fetch + (thisRef().dynamicCast <POP3Folder>(), options); if (options & FETCH_SIZE) { @@ -409,15 +437,15 @@ void POP3Folder::fetchMessage(ref <message> msg, const int options) std::ostringstream command; command << "LIST " << msg->getNumber(); - m_store->sendRequest(command.str()); + store->sendRequest(command.str()); // Get the response string response; - m_store->readResponse(response, false, NULL); + store->readResponse(response, false, NULL); - if (m_store->isSuccessResponse(response)) + if (store->isSuccessResponse(response)) { - m_store->stripResponseCode(response, response); + store->stripResponseCode(response, response); // C: LIST 2 // S: +OK 2 4242 @@ -445,15 +473,15 @@ void POP3Folder::fetchMessage(ref <message> msg, const int options) std::ostringstream command; command << "UIDL " << msg->getNumber(); - m_store->sendRequest(command.str()); + store->sendRequest(command.str()); // Get the response string response; - m_store->readResponse(response, false, NULL); + store->readResponse(response, false, NULL); - if (m_store->isSuccessResponse(response)) + if (store->isSuccessResponse(response)) { - m_store->stripResponseCode(response, response); + store->stripResponseCode(response, response); // C: UIDL 2 // S: +OK 2 QhdPYR:00WBw1Ph7x7 @@ -486,19 +514,19 @@ ref <folder> POP3Folder::getParent() if (m_path.isEmpty()) return NULL; else - return vmime::create <POP3Folder>(m_path.getParent(), m_store); + return vmime::create <POP3Folder>(m_path.getParent(), m_store.acquire()); } -weak_ref <const store> POP3Folder::getStore() const +ref <const store> POP3Folder::getStore() const { - return (m_store); + return m_store.acquire(); } -weak_ref <store> POP3Folder::getStore() +ref <store> POP3Folder::getStore() { - return (m_store); + return m_store.acquire(); } @@ -522,7 +550,9 @@ void POP3Folder::onStoreDisconnected() void POP3Folder::deleteMessage(const int num) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); @@ -530,12 +560,12 @@ void POP3Folder::deleteMessage(const int num) std::ostringstream command; command << "DELE " << num; - m_store->sendRequest(command.str()); + store->sendRequest(command.str()); string response; - m_store->readResponse(response, false); + store->readResponse(response, false); - if (!m_store->isSuccessResponse(response)) + if (!store->isSuccessResponse(response)) throw exceptions::command_error("DELE", response); // Update local flags @@ -562,10 +592,12 @@ void POP3Folder::deleteMessage(const int num) void POP3Folder::deleteMessages(const int from, const int to) { + ref <POP3Store> store = m_store.acquire(); + if (from < 1 || (to < from && to != -1)) throw exceptions::invalid_argument(); - if (!m_store) + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); @@ -577,12 +609,12 @@ void POP3Folder::deleteMessages(const int from, const int to) std::ostringstream command; command << "DELE " << i; - m_store->sendRequest(command.str()); + store->sendRequest(command.str()); string response; - m_store->readResponse(response, false); + store->readResponse(response, false); - if (!m_store->isSuccessResponse(response)) + if (!store->isSuccessResponse(response)) throw exceptions::command_error("DELE", response); } @@ -612,10 +644,12 @@ void POP3Folder::deleteMessages(const int from, const int to) void POP3Folder::deleteMessages(const std::vector <int>& nums) { + ref <POP3Store> store = m_store.acquire(); + if (nums.empty()) throw exceptions::invalid_argument(); - if (!m_store) + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); @@ -626,12 +660,12 @@ void POP3Folder::deleteMessages(const std::vector <int>& nums) std::ostringstream command; command << "DELE " << (*it); - m_store->sendRequest(command.str()); + store->sendRequest(command.str()); string response; - m_store->readResponse(response, false); + store->readResponse(response, false); - if (!m_store->isSuccessResponse(response)) + if (!store->isSuccessResponse(response)) throw exceptions::command_error("DELE", response); } @@ -716,20 +750,22 @@ void POP3Folder::copyMessages(const folder::path& /* dest */, const std::vector void POP3Folder::status(int& count, int& unseen) { - if (!m_store) + ref <POP3Store> store = m_store.acquire(); + + if (!store) throw exceptions::illegal_state("Store disconnected"); else if (!isOpen()) throw exceptions::illegal_state("Folder not open"); - m_store->sendRequest("STAT"); + store->sendRequest("STAT"); string response; - m_store->readResponse(response, false); + store->readResponse(response, false); - if (!m_store->isSuccessResponse(response)) + if (!store->isSuccessResponse(response)) throw exceptions::command_error("STAT", response); - m_store->stripResponseCode(response, response); + store->stripResponseCode(response, response); std::istringstream iss(response); iss >> count; @@ -759,8 +795,8 @@ void POP3Folder::status(int& count, int& unseen) notifyMessageCount(event); // Notify folders with the same path - for (std::list <POP3Folder*>::iterator it = m_store->m_folders.begin() ; - it != m_store->m_folders.end() ; ++it) + for (std::list <POP3Folder*>::iterator it = store->m_folders.begin() ; + it != store->m_folders.end() ; ++it) { if ((*it) != this && (*it)->getFullPath() == m_path) { diff --git a/src/net/pop3/POP3Message.cpp b/src/net/pop3/POP3Message.cpp index c4721dec..59b0406c 100644 --- a/src/net/pop3/POP3Message.cpp +++ b/src/net/pop3/POP3Message.cpp @@ -29,17 +29,19 @@ namespace net { namespace pop3 { -POP3Message::POP3Message(POP3Folder* folder, const int num) +POP3Message::POP3Message(ref <POP3Folder> folder, const int num) : m_folder(folder), m_num(num), m_size(-1), m_deleted(false) { - m_folder->registerMessage(this); + folder->registerMessage(this); } POP3Message::~POP3Message() { - if (m_folder) - m_folder->unregisterMessage(this); + ref <POP3Folder> folder = m_folder.acquire(); + + if (folder) + folder->unregisterMessage(this); } @@ -112,9 +114,11 @@ void POP3Message::extract(utility::outputStream& os, utility::progressListener* progress, const int start, const int length, const bool /* peek */) const { - if (!m_folder) + ref <const POP3Folder> folder = m_folder.acquire(); + + if (!folder) throw exceptions::illegal_state("Folder closed"); - else if (!m_folder->m_store) + else if (!folder->getStore()) throw exceptions::illegal_state("Store disconnected"); if (start != 0 && length != -1) @@ -124,17 +128,17 @@ void POP3Message::extract(utility::outputStream& os, std::ostringstream oss; oss << "RETR " << m_num; - const_cast <POP3Folder*>(m_folder)->m_store->sendRequest(oss.str()); + folder.constCast <POP3Folder>()->m_store.acquire()->sendRequest(oss.str()); try { POP3Folder::MessageMap::const_iterator it = - m_folder->m_messages.find(const_cast <POP3Message*>(this)); + folder->m_messages.find(const_cast <POP3Message*>(this)); - const int totalSize = (it != m_folder->m_messages.end()) + const int totalSize = (it != folder.constCast <POP3Folder>()->m_messages.end()) ? (*it).second : 0; - const_cast <POP3Folder*>(m_folder)->m_store-> + folder.constCast <POP3Folder>()->m_store.acquire()-> readResponse(os, progress, totalSize); } catch (exceptions::command_error& e) @@ -160,9 +164,11 @@ void POP3Message::fetchPartHeader(ref <part> /* p */) } -void POP3Message::fetch(POP3Folder* folder, const int options) +void POP3Message::fetch(ref <POP3Folder> msgFolder, const int options) { - if (m_folder != folder) + ref <POP3Folder> folder = m_folder.acquire(); + + if (folder != msgFolder) throw exceptions::folder_not_found(); // FETCH_STRUCTURE and FETCH_FLAGS are not supported by POP3. @@ -185,12 +191,12 @@ void POP3Message::fetch(POP3Folder* folder, const int options) std::ostringstream oss; oss << "TOP " << m_num << " 0"; - m_folder->m_store->sendRequest(oss.str()); + folder->m_store.acquire()->sendRequest(oss.str()); try { string buffer; - m_folder->m_store->readResponse(buffer, true); + folder->m_store.acquire()->readResponse(buffer, true); m_header = vmime::create <header>(); m_header->parse(buffer); diff --git a/src/net/pop3/POP3Store.cpp b/src/net/pop3/POP3Store.cpp index 67f478cb..24e5b9c3 100644 --- a/src/net/pop3/POP3Store.cpp +++ b/src/net/pop3/POP3Store.cpp @@ -90,7 +90,8 @@ ref <folder> POP3Store::getDefaultFolder() if (!isConnected()) throw exceptions::illegal_state("Not connected"); - return vmime::create <POP3Folder>(folder::path(folder::path::component("INBOX")), this); + return vmime::create <POP3Folder>(folder::path(folder::path::component("INBOX")), + thisRef().dynamicCast <POP3Store>()); } @@ -99,7 +100,8 @@ ref <folder> POP3Store::getRootFolder() if (!isConnected()) throw exceptions::illegal_state("Not connected"); - return vmime::create <POP3Folder>(folder::path(), this); + return vmime::create <POP3Folder>(folder::path(), + thisRef().dynamicCast <POP3Store>()); } @@ -108,7 +110,8 @@ ref <folder> POP3Store::getFolder(const folder::path& path) if (!isConnected()) throw exceptions::illegal_state("Not connected"); - return vmime::create <POP3Folder>(path, this); + return vmime::create <POP3Folder>(path, + thisRef().dynamicCast <POP3Store>()); } |