// ******************************************
// * $Id$
// *
// *   MailboxCleaner()
// *
// * delete all mails older then -daysToKeepMessages- from all mailboxes in all domains.
// *
// ******************************************

if (WScript.Arguments.length < 2)
{
    WScript.Echo("Provide username and password. Number of days to keep is optional.");
    WScript.Quit(-1);
}

var un = WScript.Arguments(0);
var pw = WScript.Arguments(1);
var days;
if (WScript.Arguments.length > 2)
    days = WScript.Arguments(2);

MailboxCleaner(un, pw, days);
// ******************************************


function MailboxCleaner(hmAdminUsr, hmAdminPwd, daysToKeep)
{
    var return_code = 0;
    var daysToKeepMessages = daysToKeep || 30;

    var trashDate = new Date();
    trashDate.setDate(trashDate.getDate() - daysToKeepMessages);
    WScript.Echo("===============================================================================");
    WScript.Echo("MailboxCleaner started (" + toDateTimeString(new Date()) + ")");
    WScript.Echo("Delete messages before  " + toDateTimeString(trashDate)  +" (days to keep="+daysToKeepMessages+")");
    WScript.Echo("===============================================================================");

    try
    {
        var objHMS = new ActiveXObject("hMailServer.Application");
        objHMS.Authenticate(hmAdminUsr, hmAdminPwd);

        for (cntDomains = 0; cntDomains < objHMS.Domains.Count; cntDomains++)
        {
            var objDomain = objHMS.Domains.Item(cntDomains);
            //var domainName = objDomain.Name;
            //WScript.Echo(domainName);
            for (cntAccounts = 0; cntAccounts < objDomain.Accounts.Count; cntAccounts++)
            {
                var objAccount = objDomain.Accounts.Item(cntAccounts);
                var AccountName = objAccount.Address;
                //WScript.Echo("    "+AccountName);
                for (cntFolders = 0; cntFolders < objAccount.IMAPFolders.Count; cntFolders++)
                {
                    var objFolder = objAccount.IMAPFolders.Item(cntFolders);
                    var objMessages = objFolder.Messages;
                    var FolderName = objFolder.Name;

                    var delcnt_folder = objMessages.Count;
                    var delcnt_messages = 0;
                    for (var cntMessages = objMessages.Count-1; cntMessages >= 0; cntMessages--)
                    {   // Use reverse loop. Deleting a messages invokes a rebuild of Messages-list.
                        var objMessage = objMessages.Item(cntMessages);
                        var mailDate = objMessage.internalDate
                        if (mailDate < trashDate)
                        {
                            //WScript.Echo("D "+mailDate);
                            delcnt_messages++;
                            objMessages.DeleteByDBID(objMessage.ID);
                        }
                        else
                        {
                            // don't delete this message.
                            //WScript.Echo("  "+mailDate);
                        }
                        objMessage = null;
                    }
                    WScript.Echo(AccountName+" "+FolderName+": "+delcnt_messages+"/"+delcnt_folder+" messages deleted.");
                    objFolder = null;
                }
                objAccount = null;
            }
            objDomain = null;
        }
        objHMS = null;
    }
    catch(e)
    {
        WScript.StdErr.WriteLine("Script prematurely terminated...\n" + e.description);
        return_code = -1;
    }
    WScript.Echo("MailboxCleaner ended (" + toDateTimeString(new Date()) + ")");
    WScript.Echo("===============================================================================");
    WScript.Quit(return_code);
}

function padout(number)
{ return (number < 10) ? "0" + number : number;
}
function toDateTimeString(jsDate)
{
    return padout(jsDate.getFullYear()) + "-" + padout(jsDate.getMonth() + 1) + "-" + padout(jsDate.getDate()) + " "
         + padout(jsDate.getHours())    + ":" + padout(jsDate.getMinutes())   + ":" + padout(jsDate.getSeconds());
       //+ " UTC"+(jsDate.getUTCHours()>0?"+":"")+padout(jsDate.getUTCHours()/10);
}
