summaryrefslogtreecommitdiff
path: root/src/depot.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/depot.cc')
-rw-r--r--src/depot.cc200
1 files changed, 88 insertions, 112 deletions
diff --git a/src/depot.cc b/src/depot.cc
index 599bf97..c31c04b 100644
--- a/src/depot.cc
+++ b/src/depot.cc
@@ -2,43 +2,41 @@
* @file depot.cc
* @brief Implementation of the Depot class.
* @author Andreas Aardal Hanssen
- * @date 2002-2005
+ * @date 2002-2005
* ------------------------------------------------------------------ **/
-#include <map>
-#include <string>
-#include <unistd.h>
-#include <errno.h>
-
#include "depot.h"
-#include "mailbox.h"
-#include "status.h"
+
#include "convert.h"
#include "iodevice.h"
#include "iofactory.h"
+#include "mailbox.h"
+#include "status.h"
+
+#include <map>
+#include <string>
+
+#include <errno.h>
+
+#include <unistd.h>
using namespace ::std;
using namespace Binc;
//--------------------------------------------------------------------
-DepotFactory::DepotFactory(void)
-{
-}
+DepotFactory::DepotFactory(void) {}
//--------------------------------------------------------------------
DepotFactory::~DepotFactory(void)
{
- for (vector<Depot *>::iterator i = depots.begin(); i != depots.end();
- ++i)
+ for (vector<Depot *>::iterator i = depots.begin(); i != depots.end(); ++i)
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 (vector<Depot *>::const_iterator i = depots.begin(); i != depots.end(); ++i)
+ if ((*i)->getName() == name) return *i;
return 0;
}
@@ -77,9 +75,7 @@ Depot::Depot(const string &name) : enditerator(0, 0)
}
//--------------------------------------------------------------------
-Depot::~Depot(void)
-{
-}
+Depot::~Depot(void) {}
//--------------------------------------------------------------------
const string &Depot::getLastError(void) const
@@ -96,8 +92,7 @@ void Depot::setLastError(const string &error) const
//--------------------------------------------------------------------
void Depot::assign(Mailbox *m)
{
- for (vector<Mailbox *>::const_iterator i = backends.begin();
- i != backends.end(); ++i)
+ for (vector<Mailbox *>::const_iterator i = backends.begin(); i != backends.end(); ++i)
if (*i == m) break;
backends.push_back(m);
@@ -106,10 +101,8 @@ void Depot::assign(Mailbox *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 (vector<Mailbox *>::const_iterator i = backends.begin(); i != backends.end(); ++i)
+ if ((*i)->isMailbox(mailboxToFilename(s_in))) return *i;
setLastError("No such mailbox " + toImapString(s_in));
return 0;
@@ -118,8 +111,7 @@ Mailbox *Depot::get(const string &s_in) const
//--------------------------------------------------------------------
bool Depot::setSelected(Mailbox *m)
{
- for (vector<Mailbox *>::const_iterator i = backends.begin();
- i != backends.end(); ++i)
+ for (vector<Mailbox *>::const_iterator i = backends.begin(); i != backends.end(); ++i)
if (*i == m) {
selectedmailbox = m;
return true;
@@ -168,8 +160,7 @@ const char Depot::getDelimiter(void) const
//--------------------------------------------------------------------
bool Depot::setDefaultType(const string &name)
{
- for (vector<Mailbox *>::const_iterator i = backends.begin();
- i != backends.end(); ++i)
+ for (vector<Mailbox *>::const_iterator i = backends.begin(); i != backends.end(); ++i)
if ((*i)->getTypeName() == name) {
defaultmailbox = *i;
return true;
@@ -259,15 +250,15 @@ bool Depot::renameMailbox(const string &s_in, const string &t_in) const
sourcename = source;
destname = dest;
- } else if (entry.length() > source.length()
- && entry[source.length()] == '.') {
- sourcename = entry;
- destname = dest + entry.substr(source.length());
- } else continue;
+ } else if (entry.length() > source.length() && entry[source.length()] == '.') {
+ sourcename = entry;
+ destname = dest + entry.substr(source.length());
+ } else
+ continue;
if (rename(sourcename.c_str(), destname.c_str()) != 0) {
- bincWarning << "error renaming " << sourcename << " to "
- << destname << ": " << strerror(errno) << endl;
+ bincWarning << "error renaming " << sourcename << " to " << destname << ": " << strerror(errno)
+ << endl;
} else
nrenamed++;
@@ -280,12 +271,10 @@ bool Depot::renameMailbox(const string &s_in, const string &t_in) const
}
if (nrenamed == 0) {
- setLastError("An error occurred when renaming "
- + toImapString(s_in)
- + " to " + toImapString(t_in)
- + ". Try creating a new mailbox,"
- " then copy over all messages."
- " Finally, delete the original mailbox");
+ setLastError("An error occurred when renaming " + toImapString(s_in) + " to " + toImapString(t_in)
+ + ". Try creating a new mailbox,"
+ " then copy over all messages."
+ " Finally, delete the original mailbox");
return false;
} else
return true;
@@ -299,13 +288,12 @@ bool Depot::getStatus(const std::string &s_in, Status &dest) const
if (m == 0) {
setLastError("Unrecognized mailbox: " + toImapString(s_in));
return false;
-}
-
+ }
+
int statusid = m->getStatusID(mailboxToFilename(mailbox));
if (mailboxstatuses.find(mailbox) != mailboxstatuses.end()) {
dest = mailboxstatuses[mailbox];
- if (dest.getStatusID() == statusid)
- return true;
+ if (dest.getStatusID() == statusid) return true;
}
if (!m->getStatus(mailboxToFilename(mailbox), dest)) {
@@ -327,10 +315,8 @@ vector<string> Depot::getSubscriptions(void) const
//----------------------------------------------------------------------
void Depot::subscribeTo(const std::string mailbox)
{
- for (vector<string>::iterator i = subscribed.begin();
- i != subscribed.end(); ++i) {
- if (*i == mailbox)
- return;
+ for (vector<string>::iterator i = subscribed.begin(); i != subscribed.end(); ++i) {
+ if (*i == mailbox) return;
}
subscribed.push_back(mailbox);
@@ -339,8 +325,7 @@ void Depot::subscribeTo(const std::string mailbox)
//----------------------------------------------------------------------
bool Depot::unsubscribeTo(const std::string mailbox)
{
- for (vector<string>::iterator i = subscribed.begin();
- i != subscribed.end(); ++i) {
+ for (vector<string>::iterator i = subscribed.begin(); i != subscribed.end(); ++i) {
if (*i == mailbox) {
subscribed.erase(i);
return true;
@@ -366,11 +351,9 @@ void Depot::loadSubscribes(void)
while ((c = fgetc(fp)) != EOF) {
if (c == '\n') {
if (current != "") {
- if (current == "INBOX")
- current = ".";
+ if (current == "INBOX") current = ".";
- if (current.substr(0, 5) == "INBOX")
- current = current.substr(5);
+ if (current.substr(0, 5) == "INBOX") current = current.substr(5);
if (addedEntries.find(current) == addedEntries.end()) {
subscribed.push_back(filenameToMailbox(current));
@@ -379,7 +362,7 @@ void Depot::loadSubscribes(void)
current = "";
}
} else
- current += c;
+ current += c;
}
fclose(fp);
@@ -402,39 +385,33 @@ bool Depot::saveSubscribes(void) const
strcpy(ftemplate, tpl.c_str());
int fd = mkstemp(ftemplate);
if (fd == -1) {
- bincWarning << "unable to create temporary file \""
- << tpl << "\"" << endl;
+ bincWarning << "unable to create temporary file \"" << tpl << "\"" << endl;
delete[] ftemplate;
return false;
}
map<string, bool> addedEntries;
- for (vector<string>::const_iterator i = subscribed.begin();
- i != subscribed.end(); ++i) {
+ 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";
- if (write(fd, w.c_str(), w.length()) != (ssize_t) w.length()) {
- bincWarning << "failed to write to " << tpl << ": "
- << strerror(errno) << endl;
+ if (write(fd, w.c_str(), w.length()) != (ssize_t)w.length()) {
+ bincWarning << "failed to write to " << tpl << ": " << strerror(errno) << endl;
break;
}
}
}
-
+
if ((fsync(fd) && (errno != EROFS || errno != EINVAL)) || close(fd)) {
- bincWarning << "failed to close " << ftemplate
- << ": " << strerror(errno) << endl;
- delete[] ftemplate;
- return false;
+ bincWarning << "failed to close " << ftemplate << ": " << strerror(errno) << endl;
+ delete[] ftemplate;
+ return false;
}
if (rename(ftemplate, ".subscribed") != 0) {
- bincWarning << "failed to rename " << ftemplate
- << " to .subscribed: "
- << strerror(errno) << endl;
- delete[] ftemplate;
- return false;
+ bincWarning << "failed to rename " << ftemplate << " to .subscribed: " << strerror(errno) << endl;
+ delete[] ftemplate;
+ return false;
}
delete[] ftemplate;
@@ -462,8 +439,7 @@ Depot::iterator::iterator(DIR *dp, struct dirent *sp)
//--------------------------------------------------------------------
Depot::iterator::iterator(const iterator &copy)
{
- if (*copy.ref != 0)
- ++(*copy.ref);
+ if (*copy.ref != 0) ++(*copy.ref);
ref = copy.ref;
dirp = copy.dirp;
@@ -477,10 +453,9 @@ Depot::iterator::~iterator(void)
}
//--------------------------------------------------------------------
-Depot::iterator &Depot::iterator::operator =(const iterator &copy)
+Depot::iterator &Depot::iterator::operator=(const iterator &copy)
{
- if (*copy.ref != 0)
- ++(*copy.ref);
+ if (*copy.ref != 0) ++(*copy.ref);
deref();
@@ -507,28 +482,27 @@ void Depot::iterator::deref(void)
}
//--------------------------------------------------------------------
-string Depot::iterator::operator * (void) const
+string Depot::iterator::operator*(void) const
{
- if (direntp == 0)
- return "";
+ if (direntp == 0) return "";
return direntp->d_name;
}
//--------------------------------------------------------------------
-void Depot::iterator::operator ++ (void)
+void Depot::iterator::operator++(void)
{
direntp = readdir(dirp);
}
//--------------------------------------------------------------------
-bool Depot::iterator::operator == (Depot::iterator i) const
+bool Depot::iterator::operator==(Depot::iterator i) const
{
return direntp == i.direntp;
}
//--------------------------------------------------------------------
-bool Depot::iterator::operator != (Depot::iterator i) const
+bool Depot::iterator::operator!=(Depot::iterator i) const
{
return direntp != i.direntp;
}
@@ -562,9 +536,7 @@ MaildirPPDepot::MaildirPPDepot(void) : Depot("Maildir++")
}
//--------------------------------------------------------------------
-MaildirPPDepot::~MaildirPPDepot(void)
-{
-}
+MaildirPPDepot::~MaildirPPDepot(void) {}
//--------------------------------------------------------------------
const string &MaildirPPDepot::getPersonalNamespace(void) const
@@ -575,7 +547,8 @@ const string &MaildirPPDepot::getPersonalNamespace(void) const
//--------------------------------------------------------------------
string MaildirPPDepot::mailboxToFilename(const string &m) const
{
- string prefix = "INBOX"; prefix += delimiter;
+ string prefix = "INBOX";
+ prefix += delimiter;
string mm = m;
trim(mm, string(&delimiter, 1));
@@ -584,7 +557,8 @@ string MaildirPPDepot::mailboxToFilename(const string &m) const
if (tmp != "INBOX" && tmp.substr(0, 6) != prefix) {
setLastError("With a Maildir++ depot, you must create all"
" mailboxes under INBOX. Try creating"
- " " + prefix + mm + " .");
+ " "
+ + prefix + mm + " .");
return "";
}
@@ -592,7 +566,8 @@ string MaildirPPDepot::mailboxToFilename(const string &m) const
twodelim += delimiter;
twodelim += delimiter;
- if (mm == "INBOX") return ".";
+ if (mm == "INBOX")
+ return ".";
else if (mm.length() <= 6) {
setLastError("With a Maildir++ depot, you must create all"
" mailboxes under INBOX.");
@@ -602,8 +577,7 @@ string MaildirPPDepot::mailboxToFilename(const string &m) const
" mailboxes under INBOX.");
return "";
} else if (mm.find(twodelim) != string::npos) {
- setLastError("Invalid character combination "
- + twodelim + " in mailbox name");
+ setLastError("Invalid character combination " + twodelim + " in mailbox name");
return "";
} else if (mm != "" && delimiter != '.' && mm.substr(1).find('.') != string::npos) {
setLastError("Invalid character '.' in mailbox name");
@@ -620,26 +594,25 @@ string MaildirPPDepot::mailboxToFilename(const string &m) const
//--------------------------------------------------------------------
string MaildirPPDepot::filenameToMailbox(const string &m) const
{
- if (m == ".") return "INBOX";
- else if (delimiter != '.' && m.find(delimiter) != string::npos) return "";
+ if (m == ".")
+ return "INBOX";
+ else if (delimiter != '.' && m.find(delimiter) != string::npos)
+ return "";
else if (m != "" && m[0] == '.') {
string tmp = m;
for (string::iterator i = tmp.begin(); i != tmp.end(); ++i)
if (*i == '.') *i = delimiter;
return "INBOX" + tmp;
- } else return "";
+ } else
+ return "";
}
//--------------------------------------------------------------------
-IMAPdirDepot::IMAPdirDepot(void) : Depot("IMAPdir")
-{
-}
+IMAPdirDepot::IMAPdirDepot(void) : Depot("IMAPdir") {}
//--------------------------------------------------------------------
-IMAPdirDepot::~IMAPdirDepot(void)
-{
-}
+IMAPdirDepot::~IMAPdirDepot(void) {}
//--------------------------------------------------------------------
string IMAPdirDepot::mailboxToFilename(const string &m) const
@@ -653,8 +626,7 @@ string IMAPdirDepot::mailboxToFilename(const string &m) const
twodelim += delimiter;
if (mm.find(twodelim) != string::npos) {
- setLastError("Invalid character combination "
- + twodelim + " in mailbox name");
+ setLastError("Invalid character combination " + twodelim + " in mailbox name");
return "";
}
@@ -670,7 +642,7 @@ string IMAPdirDepot::mailboxToFilename(const string &m) const
else
tmp += "\\.";
} else {
- tmp += *i;
+ tmp += *i;
}
++i;
@@ -686,24 +658,28 @@ string IMAPdirDepot::filenameToMailbox(const string &m) const
bool escape = false;
// hide the magic "." mailbox.
- if (m == "." || m == "..")
- return "";
+ if (m == "." || m == "..") return "";
string::const_iterator i = m.begin();
while (i != m.end()) {
if (*i == '.') {
- if (i != m.begin() && !escape) tmp += delimiter;
- else if (i == m.begin() || escape) tmp += '.';
+ if (i != m.begin() && !escape)
+ tmp += delimiter;
+ else if (i == m.begin() || escape)
+ tmp += '.';
escape = false;
} else if (*i == '\\') {
- if (!escape) escape = true; else {
+ if (!escape)
+ escape = true;
+ else {
tmp += '\\';
escape = false;
}
} else if (*i == delimiter) {
return "";
} else {
- if (escape) return "";
+ if (escape)
+ return "";
else {
tmp += *i;
escape = false;