Code: Select all
' hmail_timeout_accounts.vbs
' Disable inactive email accounts on hmail.
'
' This script is released into the public domain. Do with it what you will.
' Version 1.0 -- 2013-02-10
'------------------------------------------------------------------------------
'---- Configuration -----------------------------------------------------------
' The username and password to log in to hmail
hmailUsername = "foo"
hmailPassword = "bar"
'---- How long until an account is considered inactive ( in days )
InactiveTime = 90
'---- Do we report what would be changed without changing anything? (overrides other settings) ( true or false )
DisplayOnly = false
'---- Do we disable inactive accounts? ( true or false )
SetInactive = false
'---- Do we delete inactive accounts? ( true or false )
DeleteInactive = false
'---- Do we change the quota for inactive accounts? ( true or false )
' I like this option, because it doesn't deactivate the account while still
' limiting it so that inactive accounts don't fill the disk.
SetQuota = true
'---- What should the quota be set to for inactive users? ( in mb )
' (Only if SetQuota is true)
InactiveQuota = 10
'---- What should the quota be set to for inactive users who become active again? ( in mb )
' (Only if SetQuota is true)
ActiveQuota = 10240
'------------------------------------------------------------------------------
'---- The Code ----------------------------------------------------------------
'---- How long until an account is considered inactive ( in days )
' DisplayOnly overrides the other settings
if DisplayOnly then
SetInactive = false
DeleteInactive = false
SetQuota = false
end if
Set StdOut = WScript.StdOut
Dim obBaseApp
Set obBaseApp = CreateObject("hMailServer.Application")
Call obBaseApp.Authenticate(hmailUsername, hmailPassword)
' Inactive time in days
for DomIter=0 to obBaseApp.Domains.Count-1
Set obDomain = obBaseApp.Domains(DomIter)
stdout.write obDomain.name & vbTab & vbTab & vbTab & vbCR
if obDomain.accounts.count>0 then
for AccountIter=0 to obDomain.accounts.count-1
Set obAccount = obDomain.accounts(AccountIter)
CheckUser obAccount
next
end if
next
stdout.write vbTab & vbTab & vbTab & vbTab & vbTab
'------------------------------------------------------------------------------
sub CheckUser(obAccount)
' Check if the account is inactive
if (obAccount.Active=true) _
and (DateDiff("d", obAccount.LastLogonTime, Date) > InactiveTime) _
and (not (obAccount.ForwardEnabled) _
or (obAccount.ForwardKeepOriginal)) then
' Keep track of whether we made any changes.
changed = false
' ----
' If you modify the account, place your code below (and set the changed flag)
' If account is inactive, disable it (if configured as such)
if SetInactive and not obAccount.Active then
obAccount.Active = false
changed = true
end if
' If account is inactive, set the quota (if configured as such)
if SetQuota and (obAccount.MaxSize > InactiveQuota) then
obAccount.MaxSize = InactiveQuota
changed = true
end if
' If you modify the account, place your code above (and set the changed flag)
' ----
' If the record was changed, save the new record.
if changed then
obAccount.Save()
end if
' Generate the report before the account might be invalidated
report = DateDiff("d", obAccount.LastLogonTime, Date) & " days inactive " _
& obAccount.Size & " / " & obAccount.MaxSize & "mb used " _
& obAccount.Address & " deactivated."
' Use this flag so that we don't perform a second action on an invalid account
accountinvalidated = false
' ----
' Place any actions that invalidate the account below (and set the flags)
' If account is inactive, delete it (if configured as such)
if DeleteInactive and not accountinvalidated then
' Not tested. Do we need to call obAccount.Save() here?
obAccount.Delete()
accountinvalidated = true
changed = true
end if
' Report changes (if configured as such)
' Placed here so that we don't save the account unnecessarily
if DisplayOnly then
changed = true
end if
' Place any actions that invalidate the account above (and set the changed flag)
' ----
' After all the checks, write a report if and only if anything was done.
if changed then
' Write inactivity report to stdout
stdout.writeline report
end if
'----
' If the account is active
elseif (obAccount.Active=true) then
' Keep track of whether we made any changes.
changed = false
' ----
' If you modify the account, place your code below (and set the changed flag)
' if a formerly inactive account becomes active again,
' raise the quota (if configured as such)
if SetQuota and (obAccount.MaxSize = InactiveQuota) then
obAccount.MaxSize = ActiveQuota
changed = true
end if
' If you modify the account, place your code above (and set the changed flag)
' ----
' If the record was changed, save the new record.
if changed then
obAccount.Save()
end if
' Generate the report before the account might be invalidated
report = DateDiff("d", obAccount.LastLogonTime, Date) & " days inactive " _
& obAccount.Size & " / " & obAccount.MaxSize & "mb used " _
& obAccount.Address & " reactivated."
' Use this flag so that we don't perform a second action on an invalid account
accountinvalidated = false
' ----
' Place any actions that invalidate the account below (and set the changed flag)
' Place any actions that invalidate the account above (and set the changed flag)
' ----
' After all the checks, write a report if and only if anything was done.
if changed then
' Write reactivation report to stdout
stdout.writeline report
end if
end if
end sub