summaryrefslogtreecommitdiff
path: root/src/depot.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/depot.cc')
-rw-r--r--src/depot.cc272
1 files changed, 101 insertions, 171 deletions
diff --git a/src/depot.cc b/src/depot.cc
index c31c04b..86ac5f4 100644
--- a/src/depot.cc
+++ b/src/depot.cc
@@ -1,9 +1,10 @@
-/** --------------------------------------------------------------------
+/**
* @file depot.cc
* @brief Implementation of the Depot class.
* @author Andreas Aardal Hanssen
* @date 2002-2005
- * ------------------------------------------------------------------ **/
+ */
+
#include "depot.h"
#include "convert.h"
@@ -12,6 +13,7 @@
#include "mailbox.h"
#include "status.h"
+#include <cstddef>
#include <map>
#include <string>
@@ -19,224 +21,191 @@
#include <unistd.h>
-using namespace ::std;
using namespace Binc;
+using std::endl;
+using std::string;
-//--------------------------------------------------------------------
DepotFactory::DepotFactory(void) {}
-//--------------------------------------------------------------------
DepotFactory::~DepotFactory(void)
{
- for (vector<Depot *>::iterator i = depots.begin(); i != depots.end(); ++i)
- delete *i;
+ for (auto i : depots)
+ delete i;
}
-//--------------------------------------------------------------------
Depot *DepotFactory::get(const string &name) const
{
- for (vector<Depot *>::const_iterator i = depots.begin(); i != depots.end(); ++i)
- if ((*i)->getName() == name) return *i;
+ for (const auto i : depots)
+ if (i->getName() == name) return i;
- return 0;
+ return nullptr;
}
-//--------------------------------------------------------------------
void DepotFactory::assign(Depot *depot)
{
depots.push_back(depot);
}
-//--------------------------------------------------------------------
DepotFactory &DepotFactory::getInstance(void)
{
static DepotFactory depotfactory;
return depotfactory;
}
-//--------------------------------------------------------------------
-Depot::Depot(void) : enditerator(0, 0)
-{
- defaultmailbox = 0;
- selectedmailbox = 0;
-
- delimiter = '/';
-}
-
-//--------------------------------------------------------------------
-Depot::Depot(const string &name) : enditerator(0, 0)
-{
- defaultmailbox = 0;
- selectedmailbox = 0;
-
- delimiter = '/';
-
- this->name = name;
-}
+Depot::Depot() : Depot("") {}
-//--------------------------------------------------------------------
-Depot::~Depot(void) {}
+Depot::Depot(const std::string &name)
+ : enditerator(nullptr, nullptr)
+ , defaultmailbox(nullptr)
+ , selectedmailbox(nullptr)
+ , name(name)
+ , delimiter('/')
+{}
-//--------------------------------------------------------------------
const string &Depot::getLastError(void) const
{
return lastError;
}
-//--------------------------------------------------------------------
void Depot::setLastError(const string &error) const
{
lastError = error;
}
-//--------------------------------------------------------------------
void Depot::assign(Mailbox *m)
{
- for (vector<Mailbox *>::const_iterator i = backends.begin(); i != backends.end(); ++i)
- if (*i == m) break;
+ for (const auto i : backends)
+ if (i == m) break;
backends.push_back(m);
}
-//--------------------------------------------------------------------
Mailbox *Depot::get(const string &s_in) const
{
- for (vector<Mailbox *>::const_iterator i = backends.begin(); i != backends.end(); ++i)
- if ((*i)->isMailbox(mailboxToFilename(s_in))) return *i;
+ for (const auto i : backends)
+ if (i->isMailbox(mailboxToFilename(s_in))) return i;
setLastError("No such mailbox " + toImapString(s_in));
- return 0;
+ return nullptr;
}
-//--------------------------------------------------------------------
bool Depot::setSelected(Mailbox *m)
{
- for (vector<Mailbox *>::const_iterator i = backends.begin(); i != backends.end(); ++i)
- if (*i == m) {
+ for (const auto i : backends) {
+ if (i == m) {
selectedmailbox = m;
return true;
}
+ }
setLastError("Attempted to select unregistered Mailbox type in Depot");
return false;
}
-//--------------------------------------------------------------------
const string &Depot::getName(void) const
{
return name;
}
-//--------------------------------------------------------------------
const string &Depot::getPersonalNamespace(void) const
{
return personalNamespace;
}
-//--------------------------------------------------------------------
const string &Depot::getOthersNamespace(void) const
{
return othersNamespace;
}
-//--------------------------------------------------------------------
const string &Depot::getSharedNamespace(void) const
{
return sharedNamespace;
}
-//--------------------------------------------------------------------
void Depot::setDelimiter(char c)
{
delimiter = c;
}
-//--------------------------------------------------------------------
-const char Depot::getDelimiter(void) const
+char Depot::getDelimiter(void) const
{
return delimiter;
}
-//--------------------------------------------------------------------
bool Depot::setDefaultType(const string &name)
{
- for (vector<Mailbox *>::const_iterator i = backends.begin(); i != backends.end(); ++i)
- if ((*i)->getTypeName() == name) {
- defaultmailbox = *i;
+ for (const auto i : backends) {
+ if (i->getTypeName() == name) {
+ defaultmailbox = i;
return true;
}
+ }
setLastError("attempt to default to unregistered Mailbox type " + name);
return false;
}
-//--------------------------------------------------------------------
Mailbox *Depot::getSelected(void) const
{
return selectedmailbox;
}
-//--------------------------------------------------------------------
void Depot::resetSelected(void)
{
- selectedmailbox = 0;
+ selectedmailbox = nullptr;
}
-//--------------------------------------------------------------------
Mailbox *Depot::getDefault(void) const
{
return defaultmailbox;
}
-//--------------------------------------------------------------------
bool Depot::createMailbox(const string &s_in) const
{
const string &mailboxname = mailboxToFilename(toCanonMailbox(s_in));
- if (mailboxname == "") {
+ if (mailboxname.empty()) {
setLastError("invalid mailbox name");
return false;
}
Mailbox *mailbox = getDefault();
- if (mailbox == 0) {
+ if (mailbox == nullptr) {
setLastError("no default mailbox defined");
return false;
}
bool result = mailbox->createMailbox(mailboxname, 0777);
- if (result)
+ if (result) {
return true;
- else {
+ } else {
setLastError(mailbox->getLastError());
return false;
}
}
-//--------------------------------------------------------------------
bool Depot::deleteMailbox(const string &s_in) const
{
const string &mailboxname = mailboxToFilename(toCanonMailbox(s_in));
Mailbox *mailbox = get(s_in);
- if (mailbox == 0) {
+ if (mailbox == nullptr) {
setLastError(s_in + ": no such mailbox");
return false;
}
bool result = mailbox->deleteMailbox(mailboxname);
- if (result)
+ if (result) {
return true;
- else {
+ } else {
setLastError(mailbox->getLastError());
return false;
}
}
-//--------------------------------------------------------------------
bool Depot::renameMailbox(const string &s_in, const string &t_in) const
{
- const string &source = mailboxToFilename(s_in).c_str();
- const string &dest = mailboxToFilename(t_in).c_str();
+ const string &source = mailboxToFilename(s_in);
+ const string &dest = mailboxToFilename(t_in);
int nrenamed = 0;
const iterator e = end();
@@ -249,23 +218,24 @@ bool Depot::renameMailbox(const string &s_in, const string &t_in) const
if (entry.length() == source.length()) {
sourcename = source;
destname = dest;
-
} else if (entry.length() > source.length() && entry[source.length()] == '.') {
sourcename = entry;
destname = dest + entry.substr(source.length());
- } else
+ } else {
continue;
+ }
if (rename(sourcename.c_str(), destname.c_str()) != 0) {
bincWarning << "error renaming " << sourcename << " to " << destname << ": " << strerror(errno)
<< endl;
- } else
+ } else {
nrenamed++;
+ }
Mailbox *mailbox;
- if ((mailbox = get(filenameToMailbox(sourcename))) != 0)
+ if ((mailbox = get(filenameToMailbox(sourcename))) != nullptr)
mailbox->bumpUidValidity(filenameToMailbox(sourcename));
- if ((mailbox = get(filenameToMailbox(destname))) != 0)
+ if ((mailbox = get(filenameToMailbox(destname))) != nullptr)
mailbox->bumpUidValidity(filenameToMailbox(destname));
}
}
@@ -276,16 +246,16 @@ bool Depot::renameMailbox(const string &s_in, const string &t_in) const
" then copy over all messages."
" Finally, delete the original mailbox");
return false;
- } else
+ } else {
return true;
+ }
}
-//--------------------------------------------------------------------
bool Depot::getStatus(const std::string &s_in, Status &dest) const
{
const string mailbox = toCanonMailbox(s_in);
Mailbox *m = get(mailbox);
- if (m == 0) {
+ if (m == nullptr) {
setLastError("Unrecognized mailbox: " + toImapString(s_in));
return false;
}
@@ -306,26 +276,22 @@ bool Depot::getStatus(const std::string &s_in, Status &dest) const
return true;
}
-//----------------------------------------------------------------------
-vector<string> Depot::getSubscriptions(void) const
+std::vector<string> Depot::getSubscriptions(void) const
{
return subscribed;
}
-//----------------------------------------------------------------------
-void Depot::subscribeTo(const std::string mailbox)
+void Depot::subscribeTo(const string mailbox)
{
- for (vector<string>::iterator i = subscribed.begin(); i != subscribed.end(); ++i) {
- if (*i == mailbox) return;
- }
+ for (const auto &i : subscribed)
+ if (i == mailbox) return;
subscribed.push_back(mailbox);
}
-//----------------------------------------------------------------------
-bool Depot::unsubscribeTo(const std::string mailbox)
+bool Depot::unsubscribeTo(const string mailbox)
{
- for (vector<string>::iterator i = subscribed.begin(); i != subscribed.end(); ++i) {
+ for (auto i = subscribed.begin(); i != subscribed.end(); ++i) {
if (*i == mailbox) {
subscribed.erase(i);
return true;
@@ -335,7 +301,6 @@ bool Depot::unsubscribeTo(const std::string mailbox)
return false;
}
-//----------------------------------------------------------------------
void Depot::loadSubscribes(void)
{
// drop all existing subscribed folders.
@@ -344,7 +309,7 @@ void Depot::loadSubscribes(void)
// try loading the .subscribed file
bool ok = false;
FILE *fp = fopen(".subscribed", "r");
- map<string, bool> addedEntries;
+ std::map<string, bool> addedEntries;
if (fp) {
int c;
string current;
@@ -361,8 +326,9 @@ void Depot::loadSubscribes(void)
}
current = "";
}
- } else
+ } else {
current += c;
+ }
}
fclose(fp);
@@ -375,28 +341,24 @@ void Depot::loadSubscribes(void)
}
}
-//----------------------------------------------------------------------
bool Depot::saveSubscribes(void) const
{
// create a safe file name
- string tpl = ".subscribed-tmp-XXXXXX";
- char *ftemplate = new char[tpl.length() + 1];
+ string ftemplate = ".subscribed-tmp-XXXXXX";
- strcpy(ftemplate, tpl.c_str());
- int fd = mkstemp(ftemplate);
+ int fd = mkstemp(ftemplate.data());
if (fd == -1) {
- bincWarning << "unable to create temporary file \"" << tpl << "\"" << endl;
- delete[] ftemplate;
+ bincWarning << "unable to create temporary file \"" << ftemplate << "\"" << endl;
return false;
}
- map<string, bool> addedEntries;
- for (vector<string>::const_iterator i = subscribed.begin(); i != subscribed.end(); ++i) {
- if (addedEntries.find(*i) == addedEntries.end()) {
- addedEntries[*i] = true;
- string w = mailboxToFilename(*i) + "\n";
+ std::map<string, bool> addedEntries;
+ for (const auto &i : subscribed) {
+ if (addedEntries.find(i) == addedEntries.end()) {
+ addedEntries[i] = true;
+ string w = mailboxToFilename(i) + "\n";
if (write(fd, w.c_str(), w.length()) != (ssize_t)w.length()) {
- bincWarning << "failed to write to " << tpl << ": " << strerror(errno) << endl;
+ bincWarning << "failed to write to " << ftemplate << ": " << strerror(errno) << endl;
break;
}
}
@@ -404,29 +366,24 @@ bool Depot::saveSubscribes(void) const
if ((fsync(fd) && (errno != EROFS || errno != EINVAL)) || close(fd)) {
bincWarning << "failed to close " << ftemplate << ": " << strerror(errno) << endl;
- delete[] ftemplate;
return false;
}
- if (rename(ftemplate, ".subscribed") != 0) {
+ if (rename(ftemplate.c_str(), ".subscribed") != 0) {
bincWarning << "failed to rename " << ftemplate << " to .subscribed: " << strerror(errno) << endl;
- delete[] ftemplate;
return false;
}
- delete[] ftemplate;
return true;
}
-//--------------------------------------------------------------------
Depot::iterator::iterator(void)
{
- dirp = 0;
+ dirp = nullptr;
ref = new int;
*ref = 1;
}
-//--------------------------------------------------------------------
Depot::iterator::iterator(DIR *dp, struct dirent *sp)
{
dirp = dp;
@@ -436,7 +393,6 @@ Depot::iterator::iterator(DIR *dp, struct dirent *sp)
*ref = 1;
}
-//--------------------------------------------------------------------
Depot::iterator::iterator(const iterator &copy)
{
if (*copy.ref != 0) ++(*copy.ref);
@@ -446,13 +402,11 @@ Depot::iterator::iterator(const iterator &copy)
direntp = copy.direntp;
}
-//--------------------------------------------------------------------
Depot::iterator::~iterator(void)
{
deref();
}
-//--------------------------------------------------------------------
Depot::iterator &Depot::iterator::operator=(const iterator &copy)
{
if (*copy.ref != 0) ++(*copy.ref);
@@ -466,53 +420,47 @@ Depot::iterator &Depot::iterator::operator=(const iterator &copy)
return *this;
}
-//--------------------------------------------------------------------
void Depot::iterator::deref(void)
{
// decrease existing copy ref if there is one
if (*ref != 0 && --(*ref) == 0) {
if (dirp) {
closedir(dirp);
- dirp = 0;
+ dirp = nullptr;
}
delete ref;
- ref = 0;
+ ref = nullptr;
}
}
-//--------------------------------------------------------------------
string Depot::iterator::operator*(void) const
{
- if (direntp == 0) return "";
+ if (direntp == nullptr) return "";
return direntp->d_name;
}
-//--------------------------------------------------------------------
void Depot::iterator::operator++(void)
{
direntp = readdir(dirp);
}
-//--------------------------------------------------------------------
bool Depot::iterator::operator==(Depot::iterator i) const
{
return direntp == i.direntp;
}
-//--------------------------------------------------------------------
bool Depot::iterator::operator!=(Depot::iterator i) const
{
return direntp != i.direntp;
}
-//--------------------------------------------------------------------
Depot::iterator Depot::begin(const string &path) const
{
Depot::iterator i;
- if ((i.dirp = opendir(path.c_str())) == 0) {
+ if ((i.dirp = opendir(path.c_str())) == nullptr) {
bincWarning << "opendir on " << path << " failed" << endl;
setLastError("opendir on " + path + " failed");
return end();
@@ -522,29 +470,24 @@ Depot::iterator Depot::begin(const string &path) const
return i;
}
-//--------------------------------------------------------------------
const Depot::iterator &Depot::end(void) const
{
return enditerator;
}
-//--------------------------------------------------------------------
MaildirPPDepot::MaildirPPDepot(void) : Depot("Maildir++")
{
privateNamespace = "INBOX";
privateNamespace += getDelimiter();
}
-//--------------------------------------------------------------------
MaildirPPDepot::~MaildirPPDepot(void) {}
-//--------------------------------------------------------------------
const string &MaildirPPDepot::getPersonalNamespace(void) const
{
return privateNamespace;
}
-//--------------------------------------------------------------------
string MaildirPPDepot::mailboxToFilename(const string &m) const
{
string prefix = "INBOX";
@@ -566,9 +509,9 @@ string MaildirPPDepot::mailboxToFilename(const string &m) const
twodelim += delimiter;
twodelim += delimiter;
- if (mm == "INBOX")
+ if (mm == "INBOX") {
return ".";
- else if (mm.length() <= 6) {
+ } else if (mm.length() <= 6) {
setLastError("With a Maildir++ depot, you must create all"
" mailboxes under INBOX.");
return "";
@@ -584,37 +527,34 @@ string MaildirPPDepot::mailboxToFilename(const string &m) const
return "";
} else {
string tmp = mm.substr(6);
- for (string::iterator i = tmp.begin(); i != tmp.end(); ++i)
- if (*i == delimiter) *i = '.';
+ for (auto &i : tmp)
+ if (i == delimiter) i = '.';
return "." + tmp;
}
}
-//--------------------------------------------------------------------
string MaildirPPDepot::filenameToMailbox(const string &m) const
{
- if (m == ".")
+ if (m == ".") {
return "INBOX";
- else if (delimiter != '.' && m.find(delimiter) != string::npos)
+ } else if (delimiter != '.' && m.find(delimiter) != string::npos) {
return "";
- else if (m != "" && m[0] == '.') {
+ } else if (m != "" && m[0] == '.') {
string tmp = m;
- for (string::iterator i = tmp.begin(); i != tmp.end(); ++i)
- if (*i == '.') *i = delimiter;
+ for (auto &i : tmp)
+ if (i == '.') i = delimiter;
return "INBOX" + tmp;
- } else
+ } else {
return "";
+ }
}
-//--------------------------------------------------------------------
IMAPdirDepot::IMAPdirDepot(void) : Depot("IMAPdir") {}
-//--------------------------------------------------------------------
IMAPdirDepot::~IMAPdirDepot(void) {}
-//--------------------------------------------------------------------
string IMAPdirDepot::mailboxToFilename(const string &m) const
{
string tmp;
@@ -630,28 +570,23 @@ string IMAPdirDepot::mailboxToFilename(const string &m) const
return "";
}
- string::const_iterator i = mm.begin();
- while (i != mm.end()) {
- if (*i == delimiter) {
+ for (auto i = mm.cbegin(); i != mm.cend(); ++i) {
+ if (*i == delimiter)
tmp += '.';
- } else if (*i == '\\') {
+ else if (*i == '\\')
tmp += "\\\\";
- } else if (*i == '.') {
- if (i == mm.begin())
+ else if (*i == '.')
+ if (i == mm.cbegin())
tmp += ".";
else
tmp += "\\.";
- } else {
+ else
tmp += *i;
- }
-
- ++i;
}
return tmp;
}
-//--------------------------------------------------------------------
string IMAPdirDepot::filenameToMailbox(const string &m) const
{
string tmp;
@@ -660,33 +595,28 @@ string IMAPdirDepot::filenameToMailbox(const string &m) const
// hide the magic "." mailbox.
if (m == "." || m == "..") return "";
- string::const_iterator i = m.begin();
- while (i != m.end()) {
+ for (auto i = m.cbegin(); i != m.cend(); ++i) {
if (*i == '.') {
- if (i != m.begin() && !escape)
+ if (i != m.cbegin() && !escape)
tmp += delimiter;
- else if (i == m.begin() || escape)
+ else if (i == m.cbegin() || escape)
tmp += '.';
escape = false;
} else if (*i == '\\') {
- if (!escape)
+ if (!escape) {
escape = true;
- else {
+ } else {
tmp += '\\';
escape = false;
}
} else if (*i == delimiter) {
return "";
+ } else if (escape) {
+ return "";
} else {
- if (escape)
- return "";
- else {
- tmp += *i;
- escape = false;
- }
+ tmp += *i;
+ escape = false;
}
-
- ++i;
}
return tmp;