From 1978c49bea5b439d993067c055cec47e70db8fd6 Mon Sep 17 00:00:00 2001 From: "Jannis M. Hoffmann" Date: Sat, 7 Oct 2023 22:33:50 +0200 Subject: minor refactoring --- src/maildirmessage.cc | 192 +++++++++++++++----------------------------------- 1 file changed, 58 insertions(+), 134 deletions(-) (limited to 'src/maildirmessage.cc') diff --git a/src/maildirmessage.cc b/src/maildirmessage.cc index ee1d889..56a75c4 100644 --- a/src/maildirmessage.cc +++ b/src/maildirmessage.cc @@ -1,9 +1,10 @@ -/** -------------------------------------------------------------------- +/** * @file maildirmessage.cc * @brief Implementation of the MaildirMessage class. * @author Andreas Aardal Hanssen * @date Copyright 2002-2005 - * ----------------------------------------------------------------- **/ + */ + #include "maildirmessage.h" #include "convert.h" @@ -25,14 +26,14 @@ #include #include -using namespace ::std; using namespace Binc; +using std::string; +using std::vector; string Message::lastError; string MaildirMessage::storage; namespace { - //---------------------------------------------------------------------- void printOneHeader(IODevice &io, const MimePart *message, const string &s_in, @@ -44,11 +45,11 @@ namespace { if (message->h.getFirstHeader(s_in, hitem)) { tmp = hitem.getValue(); io << toImapString(unfold(tmp, removecomments)); - } else + } else { io << "NIL"; + } } - //---------------------------------------------------------------------- void printOneAddressList(IODevice &io, const MimePart *message, const string &s_in, @@ -63,17 +64,17 @@ namespace { splitAddr(unfold(tmp, removecomments), addr); if (addr.size() != 0) { io << "("; - for (vector::const_iterator i = addr.begin(); i != addr.end(); ++i) - - io << Address(*i).toParenList(); + for (const auto &i : addr) + io << Address(i).toParenList(); io << ")"; - } else + } else { io << "NIL"; - } else + } + } else { io << "NIL"; + } } - //---------------------------------------------------------------------- void envelope(IODevice &io, const MimePart *message) { HeaderItem hitem; @@ -106,16 +107,14 @@ namespace { io << ")"; } - //---------------------------------------------------------------------- void bodyStructure(IODevice &io, const MimePart *message, bool extended) { HeaderItem hitem; if (message->isMultipart() && message->members.size() > 0) { io << "("; - for (vector::const_iterator i = message->members.begin(); i != message->members.end(); - ++i) - bodyStructure(io, &(*i), extended); + for (const auto &i : message->members) + bodyStructure(io, &i, extended); io << " "; io << toImapString(message->getSubType()); @@ -137,8 +136,7 @@ namespace { vector v; split(tmp, ";", v); - for (vector::const_iterator i = v.begin(); i != v.end(); ++i) { - string element = *i; + for (string element : v) { trim(element); if (element.find('=') != string::npos) { string::size_type pos = element.find('='); @@ -153,15 +151,17 @@ namespace { if (parameters.size() != 0) { io << "("; - for (vector::const_iterator i = parameters.begin(); i != parameters.end(); ++i) { - if (i != parameters.begin()) io << " "; + for (auto i = parameters.cbegin(); i != parameters.cend(); ++i) { + if (i != parameters.cbegin()) io << " "; io << toImapString(*i); } io << ")"; - } else + } else { io << "NIL"; - } else + } + } else { io << "NIL"; + } // CONTENT-DISPOSITION io << " "; @@ -207,13 +207,16 @@ namespace { ++i; } io << ")"; - } else + } else { io << "NIL"; + } io << ")"; - } else + } else { io << "NIL"; - } else + } + } else { io << "NIL"; + } // CONTENT-LANGUAGE io << " "; @@ -251,8 +254,8 @@ namespace { subtype = "plain"; } - for (vector::const_iterator i = v.begin(); i != v.end(); ++i) { - if (i == v.begin()) continue; + for (auto i = v.cbegin(); i != v.cend(); ++i) { + if (i == v.cbegin()) continue; string element = *i; trim(element); if (element.find('=') != string::npos) { @@ -278,13 +281,14 @@ namespace { io << " "; if (parameters.size() != 0) { io << "("; - for (vector::const_iterator i = parameters.begin(); i != parameters.end(); ++i) { - if (i != parameters.begin()) io << " "; + for (auto i = parameters.cbegin(); i != parameters.cend(); ++i) { + if (i != parameters.cbegin()) io << " "; io << toImapString(*i); } io << ")"; - } else + } else { io << "NIL"; + } // CONTENT-ID io << " "; @@ -301,8 +305,9 @@ namespace { tmp = hitem.getValue(); trim(tmp); io << toImapString(tmp); - } else + } else { io << "\"7bit\""; + } io << " "; // Size of body in octets @@ -366,13 +371,16 @@ namespace { ++i; } io << ")"; - } else + } else { io << "NIL"; + } io << ")"; - } else + } else { io << "NIL"; - } else + } + } else { io << "NIL"; + } // CONTENT-LANGUAGE io << " "; @@ -388,10 +396,9 @@ namespace { } } -//------------------------------------------------------------------------ MaildirMessage::MaildirMessage(Maildir &hom) : fd(-1) - , doc(0) + , doc(nullptr) , internalFlags(None) , stdflags(F_NONE) , uid(0) @@ -400,10 +407,9 @@ MaildirMessage::MaildirMessage(Maildir &hom) , safeName("") , internaldate(0) , home(hom) - , customFlags(0) + , customFlags(nullptr) {} -//------------------------------------------------------------------------ MaildirMessage::MaildirMessage(const MaildirMessage ©) : fd(copy.fd) , doc(copy.doc) @@ -424,43 +430,16 @@ MaildirMessage::MaildirMessage(const MaildirMessage ©) } } -//------------------------------------------------------------------------ MaildirMessage::~MaildirMessage(void) { delete customFlags; } -//------------------------------------------------------------------------ -MaildirMessage &MaildirMessage::operator=(const MaildirMessage ©) -{ - fd = copy.fd; - doc = copy.doc; - internalFlags = copy.internalFlags; - stdflags = copy.stdflags; - uid = copy.uid; - size = copy.size; - unique = copy.unique; - safeName = copy.safeName; - internaldate = copy.internaldate; - home = copy.home; - - if (copy.customFlags) { - customFlags = new vector; - *customFlags = *copy.customFlags; - } else { - customFlags = 0; - } - - return *this; -} - -//------------------------------------------------------------------------ bool MaildirMessage::operator<(const MaildirMessage &a) const { return uid < a.uid; } -//------------------------------------------------------------------------ void MaildirMessage::close(void) { if (fd != -1) { @@ -482,7 +461,7 @@ void MaildirMessage::close(void) struct utimbuf tim = {internaldate, internaldate}; utime(safeName.c_str(), &tim); } else { - time_t t = time(0); + time_t t = time(nullptr); struct utimbuf tim = {t, t}; utime(safeName.c_str(), &tim); } @@ -490,57 +469,48 @@ void MaildirMessage::close(void) internalFlags &= ~WasWrittenTo; } - if (doc) { doc->clear(); delete doc; - doc = 0; + doc = nullptr; } } -//------------------------------------------------------------------------ void MaildirMessage::setExpunged(void) { internalFlags |= Expunged; } -//------------------------------------------------------------------------ void MaildirMessage::setUnExpunged(void) { internalFlags &= ~Expunged; } -//------------------------------------------------------------------------ void MaildirMessage::setFlagsUnchanged(void) { internalFlags &= ~FlagsChanged; } -//------------------------------------------------------------------------ bool MaildirMessage::hasFlagsChanged(void) const { return (internalFlags & FlagsChanged) != 0; } -//------------------------------------------------------------------------ unsigned char MaildirMessage::getStdFlags(void) const { return stdflags; } -//------------------------------------------------------------------------ bool MaildirMessage::isExpunged(void) const { return (internalFlags & Expunged) != 0; } -//------------------------------------------------------------------------ unsigned int MaildirMessage::getUID(void) const { return uid; } -//------------------------------------------------------------------------ unsigned int MaildirMessage::getSize(bool render) const { if (size == 0 && render) { @@ -551,57 +521,48 @@ unsigned int MaildirMessage::getSize(bool render) const return size; } -//------------------------------------------------------------------------ const string &MaildirMessage::getUnique(void) const { return unique; } -//------------------------------------------------------------------------ time_t MaildirMessage::getInternalDate(void) const { return internaldate; } -//------------------------------------------------------------------------ void MaildirMessage::setInternalDate(time_t t) { internaldate = t; } -//------------------------------------------------------------------------ void MaildirMessage::setStdFlag(unsigned char f_in) { internalFlags |= FlagsChanged; stdflags |= f_in; } -//------------------------------------------------------------------------ void MaildirMessage::resetStdFlags(void) { internalFlags |= FlagsChanged; stdflags = F_NONE; } -//------------------------------------------------------------------------ void MaildirMessage::setUID(unsigned int i_in) { uid = i_in; } -//------------------------------------------------------------------------ void MaildirMessage::setSize(unsigned int i_in) { size = i_in; } -//------------------------------------------------------------------------ void MaildirMessage::setUnique(const string &s_in) { unique = s_in; } -//------------------------------------------------------------------------ int MaildirMessage::getFile(void) const { if (fd != -1) return fd; @@ -619,16 +580,16 @@ int MaildirMessage::getFile(void) const if (errno == ENOENT) { struct stat st; if (lstat(fpath.c_str(), &st) != -1) { - bincWarning << "dangling symlink: " << fpath << endl; + bincWarning << "dangling symlink: " << fpath << std::endl; return -1; } } else { - bincWarning << "unable to open " << fpath << ": " << strerror(errno) << endl; + bincWarning << "unable to open " << fpath << ": " << strerror(errno) << std::endl; return -1; } home.scanFileNames(); - if ((item = home.index.find(id)) == 0) + if ((item = home.index.find(id)) == nullptr) break; else fpath = home.path + "/cur/" + item->fileName; @@ -643,38 +604,33 @@ int MaildirMessage::getFile(void) const return -1; } -//------------------------------------------------------------------------ void MaildirMessage::setFile(int fd) { this->fd = fd; } -//------------------------------------------------------------------------ void MaildirMessage::setSafeName(const string &name) { safeName = name; } -//------------------------------------------------------------------------ const string &MaildirMessage::getSafeName(void) const { return safeName; } -//------------------------------------------------------------------------ string MaildirMessage::getFileName(void) const { MaildirIndexItem *item = home.index.find(getUnique()); if (!item) { home.scanFileNames(); - if ((item = home.index.find(getUnique())) == 0) return ""; + if ((item = home.index.find(getUnique())) == nullptr) return ""; } return item->fileName; } -//------------------------------------------------------------------------ int MaildirMessage::readChunk(string &chunk) { if (fd == -1) { @@ -692,7 +648,6 @@ int MaildirMessage::readChunk(string &chunk) return readBytes; } -//------------------------------------------------------------------------ bool MaildirMessage::appendChunk(const string &chunk) { if (fd == -1) { @@ -725,7 +680,6 @@ bool MaildirMessage::appendChunk(const string &chunk) return false; } -//------------------------------------------------------------------------ bool MaildirMessage::parseFull(void) const { MaildirMessageCache &cache = MaildirMessageCache::getInstance(); @@ -744,7 +698,6 @@ bool MaildirMessage::parseFull(void) const return true; } -//------------------------------------------------------------------------ bool MaildirMessage::parseHeaders(void) const { MaildirMessageCache &cache = MaildirMessageCache::getInstance(); @@ -764,7 +717,6 @@ bool MaildirMessage::parseHeaders(void) const return true; } -//------------------------------------------------------------------------ bool MaildirMessage::printBodyStructure(bool extended) const { if (!parseFull()) return false; @@ -773,7 +725,6 @@ bool MaildirMessage::printBodyStructure(bool extended) const return true; } -//------------------------------------------------------------------------ bool MaildirMessage::printEnvelope(void) const { if (!parseFull()) return false; @@ -782,7 +733,6 @@ bool MaildirMessage::printEnvelope(void) const return true; } -//------------------------------------------------------------------------ bool MaildirMessage::printHeader(const std::string §ion, std::vector headers, bool includeHeaders, @@ -795,7 +745,6 @@ bool MaildirMessage::printHeader(const std::string §ion, return true; } -//------------------------------------------------------------------------ unsigned int MaildirMessage::getHeaderSize(const std::string §ion, std::vector headers, bool includeHeaders, @@ -805,8 +754,9 @@ unsigned int MaildirMessage::getHeaderSize(const std::string §ion, { if (section == "") { if (!parseHeaders()) return 0; - } else if (!parseFull()) + } else if (!parseFull()) { return 0; + } const MimePart *part = doc->getPart(section, "", mime ? MimePart::FetchMime : MimePart::FetchHeader); if (!part) { @@ -823,7 +773,6 @@ unsigned int MaildirMessage::getHeaderSize(const std::string §ion, return storage.size(); } -//------------------------------------------------------------------------ bool MaildirMessage::printBody(const std::string §ion, unsigned int startOffset, unsigned int length) const @@ -833,7 +782,7 @@ bool MaildirMessage::printBody(const std::string §ion, const MimePart *part = doc->getPart(section, ""); if (!part) { storage = ""; - return 0; + return false; } int fd = getFile(); @@ -844,7 +793,6 @@ bool MaildirMessage::printBody(const std::string §ion, return true; } -//------------------------------------------------------------------------ unsigned int MaildirMessage::getBodySize(const std::string §ion, unsigned int startOffset, unsigned int length) const @@ -863,7 +811,6 @@ unsigned int MaildirMessage::getBodySize(const std::string §ion, return s < length ? s : length; } -//------------------------------------------------------------------------ bool MaildirMessage::printDoc(unsigned int startOffset, unsigned int length, bool onlyText) const { if (!parseFull()) return false; @@ -878,7 +825,6 @@ bool MaildirMessage::printDoc(unsigned int startOffset, unsigned int length, boo return true; } -//------------------------------------------------------------------------ unsigned int MaildirMessage::getDocSize(unsigned int startOffset, unsigned int length, bool onlyText) const @@ -894,7 +840,6 @@ unsigned int MaildirMessage::getDocSize(unsigned int startOffset, return s < length ? s : length; } -//------------------------------------------------------------------------ bool MaildirMessage::headerContains(const std::string &header, const std::string &text) { if (!parseHeaders()) return false; @@ -909,7 +854,6 @@ bool MaildirMessage::headerContains(const std::string &header, const std::string return (tmp.find(tmp2) != string::npos); } -//------------------------------------------------------------------------ bool MaildirMessage::bodyContains(const std::string &text) { if (!parseFull()) return false; @@ -944,7 +888,6 @@ bool MaildirMessage::bodyContains(const std::string &text) return false; } -//------------------------------------------------------------------------ bool MaildirMessage::textContains(const std::string &text) { // search the body part of the message.. @@ -973,7 +916,6 @@ bool MaildirMessage::textContains(const std::string &text) return false; } -//------------------------------------------------------------------------ const std::string &MaildirMessage::getHeader(const std::string &header) { static string NIL = ""; @@ -986,23 +928,19 @@ const std::string &MaildirMessage::getHeader(const std::string &header) return hitem.getValue(); } -//------------------------------------------------------------------------ MaildirMessageCache::MaildirMessageCache(void) {} -//------------------------------------------------------------------------ MaildirMessageCache::~MaildirMessageCache(void) { clear(); } -//------------------------------------------------------------------------ MaildirMessageCache &MaildirMessageCache::getInstance(void) { static MaildirMessageCache cache; return cache; } -//------------------------------------------------------------------------ void MaildirMessageCache::addStatus(const MaildirMessage *m, ParseStatus s) { if (statuses.find(m) == statuses.end()) { @@ -1019,7 +957,6 @@ void MaildirMessageCache::addStatus(const MaildirMessage *m, ParseStatus s) statuses[m] = s; } -//------------------------------------------------------------------------ MaildirMessageCache::ParseStatus MaildirMessageCache::getStatus(const MaildirMessage *m) const { if (statuses.find(m) == statuses.end()) return NotParsed; @@ -1027,24 +964,19 @@ MaildirMessageCache::ParseStatus MaildirMessageCache::getStatus(const MaildirMes return statuses[m]; } -//------------------------------------------------------------------------ void MaildirMessageCache::clear(void) { - for (deque::iterator i = parsed.begin(); i != parsed.end(); ++i) - const_cast(*i)->close(); - + for (std::deque::iterator i = parsed.begin(); i != parsed.end(); ++i) parsed.clear(); - statuses.clear(); } -//------------------------------------------------------------------------ void MaildirMessageCache::removeStatus(const MaildirMessage *m) { if (statuses.find(m) == statuses.end()) return; statuses.erase(statuses.find(m)); - for (deque::iterator i = parsed.begin(); i != parsed.end(); ++i) { + for (std::deque::iterator i = parsed.begin(); i != parsed.end(); ++i) { if (*i == m) { const_cast(*i)->close(); parsed.erase(i); @@ -1053,25 +985,21 @@ void MaildirMessageCache::removeStatus(const MaildirMessage *m) } } -//------------------------------------------------------------------------ void MaildirMessage::setInternalFlag(unsigned char f) { internalFlags |= f; } -//------------------------------------------------------------------------ unsigned char MaildirMessage::getInternalFlags(void) const { return internalFlags; } -//------------------------------------------------------------------------ void MaildirMessage::clearInternalFlag(unsigned char f) { internalFlags &= ~f; } -//------------------------------------------------------------------------ void MaildirMessage::setCustomFlag(const string &flag) { if (!customFlags) { @@ -1079,15 +1007,13 @@ void MaildirMessage::setCustomFlag(const string &flag) customFlags = new vector; } - for (vector::const_iterator it = customFlags->begin(); it != customFlags->end(); ++it) { - if (*it == flag) return; - } + for (const auto &it : *customFlags) + if (it == flag) return; internalFlags |= FlagsChanged | CustomFlagsChanged; customFlags->push_back(flag); } -//------------------------------------------------------------------------ void MaildirMessage::removeCustomFlag(const string &flag) { internalFlags |= FlagsChanged | CustomFlagsChanged; @@ -1102,16 +1028,14 @@ void MaildirMessage::removeCustomFlag(const string &flag) } } -//------------------------------------------------------------------------ void MaildirMessage::resetCustomFlags(void) { internalFlags |= FlagsChanged | CustomFlagsChanged; delete customFlags; - customFlags = 0; + customFlags = nullptr; } -//------------------------------------------------------------------------ vector MaildirMessage::getCustomFlags(void) const { if (!customFlags) return vector(); -- cgit v1.2.3