Timeout inactive accounts

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
Post Reply
philotron
New user
New user
Posts: 1
Joined: 2013-02-10 21:18

Timeout inactive accounts

Post by philotron » 2013-02-10 21:34

A quick script I put together to handle inactive accounts, so that they don't fill up your disk space with spam. Run it on a schedule with "cscript hmail_timeout_accounts.vbs" Please reply if you find it useful, or if you have any suggestions or comments.

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

ozgurerdogan
Senior user
Senior user
Posts: 266
Joined: 2010-11-19 18:50

Re: Timeout inactive accounts

Post by ozgurerdogan » 2016-06-22 16:56

Is this still functional?

User avatar
jimimaseye
Moderator
Moderator
Posts: 8073
Joined: 2011-09-08 17:48

Re: Timeout inactive accounts

Post by jimimaseye » 2016-06-22 17:18

ozgurerdogan wrote:Is this still functional?
Should be, cant see why not.
HMS 5.6.6 B2383 on Win Server 2008 R2 Foundation, + 5.6.7-B2415 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

Post Reply