bool
   PreSaveLimitationsCheck::CheckLimitations(boost::shared_ptr<DistributionListRecipient> recipient, String &resultDescription)
   {
      if (recipient->GetAddress().GetLength() == 0)
      {
         resultDescription = "The recipient address is empty";
         return false;
      }

      return true;
   }
   bool 
   PersistentTCPIPPort::SaveObject(boost::shared_ptr<TCPIPPort> pObject)
   {
      SQLStatement oStatement;
      oStatement.SetTable("hm_tcpipports");
      
      if (pObject->GetID() == 0)
      {
         oStatement.SetStatementType(SQLStatement::STInsert);
         oStatement.SetIdentityColumn("portid");
      }
      else
      {
         oStatement.SetStatementType(SQLStatement::STUpdate);
         String sWhere;
         sWhere.Format(_T("portid = %I64d"), pObject->GetID());
         oStatement.SetWhereClause(sWhere);
         
      }

      __int64 iAddress = 0;

      IPAddressSQLHelper helper;
      

      oStatement.AddColumn("portprotocol", pObject->GetProtocol());
      oStatement.AddColumn("portnumber", pObject->GetPortNumber());
      oStatement.AddColumnInt64("portsslcertificateid", pObject->GetSSLCertificateID());
      helper.AppendStatement(oStatement, pObject->GetAddress(), "portaddress1", "portaddress2");
      oStatement.AddColumn("portusessl", pObject->GetUseSSL());
      
      bool bNewObject = pObject->GetID() == 0;

      // Save and fetch ID
      __int64 iDBID = 0;
      bool bRetVal = Application::Instance()->GetDBManager()->Execute(oStatement, bNewObject ? &iDBID : 0);
      if (bRetVal && bNewObject)
         pObject->SetID((int) iDBID);


      return true;
   }
   bool
   PreSaveLimitationsCheck::CheckLimitations(boost::shared_ptr<DistributionList> list, String &resultDescription)
   {
      boost::shared_ptr<Domain> domain = GetDomain(list->GetDomainID());

      if (GetDuplicateExist(domain, TypeList,list->GetID(), list->GetAddress()))      
         return DuplicateError(resultDescription);

      if (list->GetID() == 0)
      {
         if (domain->GetMaxNoOfDistributionLists()  && 
            domain->GetDistributionLists()->GetCount() >= domain->GetMaxNoOfDistributionLists())
         {
            resultDescription = "The maximum number of distribution lists have been created.";
            return false;
         }
      }

      return true;
   }
   bool
   PreSaveLimitationsCheck::CheckLimitations(boost::shared_ptr<Account> account, String &resultDescription)
   {
      if (account->GetVacationMessage().GetLength() > 1000)
      {
         resultDescription = "The auto reply message length exceeds the 1000 character limit.";
         return false;
      }

      if (account->GetAddress().FindOneOf(_T("?*|\\/<>")) >= 0)
      {
         resultDescription = "The characters ?*|\\/<> are not permitted in hMailServer account addresses.";
         return false;
      }


      boost::shared_ptr<Domain> domain = GetDomain(account->GetDomainID());

      if (GetDuplicateExist(domain, TypeAccount, account->GetID(), account->GetAddress()))      
         return DuplicateError(resultDescription);

      if (account->GetID() == 0)
      {
         if (domain->GetMaxNoOfAccountsEnabled() && 
             domain->GetAccounts()->GetCount() >= domain->GetMaxNoOfAccounts())
         {
            resultDescription = "The maximum number of accounts have been created.";
            return false;
         }
      }

      if (domain->GetMaxAccountSize() != 0)
      {
         if (account->GetAccountMaxSize() > domain->GetMaxAccountSize())
         {
            resultDescription = "The account is larger than the maximum account size specified in the domain settings.";
            return false;
         }

         if (account->GetAccountMaxSize() == 0)
         {
            resultDescription = "The domain has a maximum account size set. When this is the case, all accounts in the domains must have an account size set.";
            return false;
         }
      }

      if (domain->GetMaxSizeMB() > 0)
      {

         if (account->GetAccountMaxSize() == 0)
         {
            resultDescription = "The domain has a maximum account size set. When this is the case, all accounts in the domains must have an account size set.";
            return false;
         }

         String sError = "Account could not be saved. The total size of all accounts in the domain would exceed the maximum size for the domain.";
         
         __int64 currentSize = PersistentDomain::GetAllocatedSize(domain);

         if (account->GetID() == 0)
         {
            if (currentSize + account->GetAccountMaxSize() > domain->GetMaxSizeMB())
            {
               resultDescription = sError;
               return false;
            }
         }
         else
         {
            boost::shared_ptr<Account> currentAccountSettings = boost::shared_ptr<Account>(new Account);
            PersistentAccount::ReadObject(currentAccountSettings, account->GetID());

            if (currentSize - currentAccountSettings->GetAccountMaxSize() + account->GetAccountMaxSize() > domain->GetMaxSizeMB())
            {
               resultDescription = sError;
               return false;
            }
         }
      }

      String address = account->GetAddress().ToLower();
      if (address.Find(_T("\\")) >= 0 || address.Find(_T("/")) >= 0)
      {
         resultDescription = "An account name may not contain the characters \\ or /.";
         return false;
      }


      return true;
   }
bool
PersistentAccount::DeleteObject(boost::shared_ptr<Account> pAccount)
{
    __int64 iID = pAccount->GetID();
    assert(iID);

    if (iID <= 0)
        return false;

    // Delete messages connected to this account.
    DeleteMessages(pAccount);

    // Force delete the inbox as well. DeleteMessages above does not delete it.
    boost::shared_ptr<IMAPFolder> inbox = pAccount->GetFolders()->GetFolderByName("Inbox");
    if (inbox)
        PersistentIMAPFolder::DeleteObject(inbox, true);

    pAccount->GetRules()->DeleteAll();

    // Delete fetch accounts connected to this account.
    PersistentFetchAccount::DeleteByAccountID(iID);

    // Delete references from groups...
    PersistentGroupMember::DeleteByAccount(iID);

    PersistentACLPermission::DeleteOwnedByAccount(iID);

    SQLCommand deleteCommand("delete from hm_accounts where accountid = @ACCOUNTID");
    deleteCommand.AddParameter("@ACCOUNTID", iID);

    bool bRet = Application::Instance()->GetDBManager()->Execute(deleteCommand);

    // Delete folder from data directory
    String sAccountFolder = IniFileSettings::Instance()->GetDataDirectory() + "\\" + StringParser::ExtractDomain(pAccount->GetAddress()) + "\\" + StringParser::ExtractAddress(pAccount->GetAddress());
    FileUtilities::DeleteDirectory(sAccountFolder);

    // Refresh caches.
    Cache<Account, PersistentAccount>::Instance()->RemoveObject(pAccount);

    return bRet;
}
bool
PersistentAccount::SaveObject(boost::shared_ptr<Account> pAccount, String &sErrorMessage, bool createInbox)
{
    if (!PreSaveLimitationsCheck::CheckLimitations(pAccount, sErrorMessage))
        return false;

    __int64 iID = pAccount->GetID();
    if (iID > 0)
    {
        // First read the domain to see if we've changed its name.
        boost::shared_ptr<Account> tempAccount = boost::shared_ptr<Account>(new Account());
        if (!PersistentAccount::ReadObject(tempAccount, iID))
            return false;

        if (tempAccount->GetAddress().CompareNoCase(pAccount->GetAddress()) != 0)
        {
            // Check if this account contains messages with full path. If so, the user cannot
            // rename it at this point.
            // Disallow change, if not all files are partial in the database.
            if (!PersistentMessage::GetAllMessageFilesArePartialNames())
            {
                sErrorMessage = "As of hMailServer 5.4, partial file names are stored in the database, rather than the full path\r\n"
                                "To be able to rename the account, you must first migrate your database to the new partial paths scheme. To do\r\n"
                                "this, run Data Directory Synchronizer.";
                return false;
            }

            // Name has been changed. Rename all sub objects first.
            NameChanger nameChanger;
            if (!nameChanger.RenameAccount(tempAccount->GetAddress(), pAccount, sErrorMessage))
                return false;

            // Remove the old account from the cache.
            Cache<Account, PersistentAccount>::Instance()->RemoveObject(tempAccount->GetAddress());
        }
    }

    SQLStatement oStatement;
    oStatement.AddColumnInt64("accountdomainid", pAccount->GetDomainID());
    oStatement.AddColumn("accountaddress", pAccount->GetAddress());
    oStatement.AddColumn("accountpassword", pAccount->GetPassword());
    oStatement.AddColumn("accountactive", pAccount->GetActive());
    oStatement.AddColumn("accountisad", pAccount->GetIsAD());
    oStatement.AddColumn("accountaddomain", pAccount->GetADDomain());
    oStatement.AddColumn("accountadusername", pAccount->GetADUsername());
    oStatement.AddColumn("accountmaxsize", pAccount->GetAccountMaxSize());

    oStatement.AddColumn("accountvacationmessageon", pAccount->GetVacationMessageIsOn());
    oStatement.AddColumn("accountvacationmessage", pAccount->GetVacationMessage());
    oStatement.AddColumn("accountvacationsubject", pAccount->GetVacationSubject());
    oStatement.AddColumn("accountvacationexpires", pAccount->GetVacationExpires());
    oStatement.AddColumn("accountvacationexpiredate", pAccount->GetVacationExpiresDate());

    oStatement.AddColumn("accountpwencryption", pAccount->GetPasswordEncryption());
    oStatement.AddColumn("accountadminlevel", pAccount->GetAdminLevel());

    oStatement.AddColumn("accountforwardenabled", pAccount->GetForwardEnabled());
    oStatement.AddColumn("accountforwardaddress", pAccount->GetForwardAddress());
    oStatement.AddColumn("accountforwardkeeporiginal", pAccount->GetForwardKeepOriginal());

    oStatement.AddColumn("accountenablesignature", pAccount->GetEnableSignature());
    oStatement.AddColumn("accountsignatureplaintext", pAccount->GetSignaturePlainText());
    oStatement.AddColumn("accountsignaturehtml", pAccount->GetSignatureHTML());

    oStatement.AddColumn("accountlastlogontime", pAccount->GetLastLogonTime());

    oStatement.AddColumn("accountpersonfirstname", pAccount->GetPersonFirstName());
    oStatement.AddColumn("accountpersonlastname", pAccount->GetPersonLastName());

    oStatement.SetTable("hm_accounts");


    if (pAccount->GetID() == 0)
    {
        oStatement.SetStatementType(SQLStatement::STInsert);
        oStatement.SetIdentityColumn("accountid");
    }
    else
    {
        oStatement.SetStatementType(SQLStatement::STUpdate);

        String sWhere;
        sWhere.Format(_T("accountid = %I64d"), pAccount->GetID());

        oStatement.SetWhereClause(sWhere);

    }

    bool bNewObject = pAccount->GetID() == 0;

    // Save and fetch ID
    __int64 iDBID = 0;
    bool bRetVal = Application::Instance()->GetDBManager()->Execute(oStatement, bNewObject ? &iDBID : 0);
    if (bRetVal && bNewObject)
    {
        pAccount->SetID((long) iDBID);

        if (createInbox)
        {
            if (!CreateInbox(*pAccount))
            {
                PersistentAccount::DeleteObject(pAccount);
            }
        }
    }

    if (!bNewObject)
        Cache<Account, PersistentAccount>::Instance()->RemoveObject(pAccount);

    return bRetVal;
}