Page 1 of 1

Accept incoming mail if user recently authenticated

Posted: 2016-05-24 09:52
by Nime
Currently I've deactivated the accounts if they are idle for a year. It's not the best way to save disk space, bandwidth and cpu (virus+spam check).
The user might want to login again and asks for his/her password.

Automatic re-activate would be nice on user authentication but I think it's not possible at the moment.

So I need a script which one checks for last login and then accept the message or deny it.

1. What is the best event to use?
2. What about multiple recipients?
3. Should I first accept the message then check for recent user activity for each recipients then delete mail?
Etc...

I've got 6000 inboxes and many of them are ummm... idle : )

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-24 12:57
by jimimaseye
Just for clarity, what is the reason for your belief that a disabled account takes less resources than a dormant account? If its dormant, and nothing there to process and nothing being received, then how can it be taking any CPU?

Anyway Im not sure you can do what you are suggesting. The earliest 'event' with message details (recipient) is OnSMTPData and I think it will reject the message automatically if the account is disabled before it gets to this stage (a disabled account will not reach the 'OnSMTPData' event).

Just re-reading your options in your post, perhaps the best thing is to stop disabling the accounts (therefore allowing incoming emails), and then do the 'last logoin check' in 'OnSMPTData' event and reject the email at that stage with a 'Result.Value = 2' (https://www.hmailserver.com/documentati ... onsmtpdata). This would avoid the DNSBL, SURBL and spamassasin testing (and your important CPU resouces) and it would also stop your users from hassling you to reactivate when they come back to work in a years time. (Meantime, the account is active but doesnt take resources due to them still being on gardening leave).

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-24 15:44
by Nime
Login or not all active inboxes receive too many messages; Facebook notifications, newsletters, etc. They increase Volume Shadow Copy & Backup disk size, eat bandwidth, consume cpu, etc.

I simply disable the account after a while; daily sql update query does the job. It's a perfect solution except the user won't login again in the future.

I want account to be active and deny all incoming messages that they won't be read because of user inactivity. I could simply accept and then delete away user's messages;
but accepting the user's messages consume cpu+bandwidth+disk.

I think I'm gonna invent car start-stop system of mail server *thinkgreen*

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-24 15:51
by jimimaseye
So my suggestion above will save you hassle and resources.

Enable the (currently disabled) accounts, and do the script in OnSMTPData that checks last login date and does a reject if its beyond your 1 year threshold. No spam checking willo be performed and those inboxes will remain empty ('inactive') whilst the returning user can come along and resume with it if and when they want.

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-25 14:29
by Nime
Ok, here is my intial code, please examine:

Code: Select all



'''''''''''''''''''''''''''''''''''''''''''''
Sub OnSTMPData(oClient, oMessage)
'''''''''''''''''''''''''''''''''''''''''''''

   If oMessage.Recipients.Count = 1 Then 'Ignore if there are multiple recipients
    
      Dim oRecipient = oMessage.Recipients.Item(0)
      
      If oRecipient.IsLocalUser Then
      
        Dim oApp
        Set oApp = CreateObject("hMailServer.Application")
        'Call oApp.Authenticate("Administrator", "testar") ' Is it necessary?
      
        Dim aDomain = Split(oRecipient.Address, "@")
        Dim sDomain = aDomain(1)
        Dim oDomain 
        Set oDomain = oApp.Domains.ItemByName(sDomain)
        
        Dim oAccount
        Set oAccount = oDomain.Accounts.ItemByAddress(oRecipient.Address)
      
        If oAccount.Active And oAccount.ForwardEnabled = False Then  
        
          Dim diffDays = Now - oAccount.LastLogonTime  '  http://www.w3schools.com/asp/func_datediff.asp
          
          If diffDays > 99 Then 'Days
          
            Result.Message = "The recipient " & oRecipient.Address & " is away for " & diffDays & " days."
            Result.Value = 2
           
          End If
           
        End If   
        
      End If
      
   End If

End Sub


Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-25 16:17
by jimimaseye
This version checks all recipients (so not limited to just 1) and only accepts if all pass.

Code: Select all

'''''''''''''''''''''''''''''''''''''''''''''
Sub OnSMTPData(oClient, oMessage)
'''''''''''''''''''''''''''''''''''''''''''''

   Dim j, aUsername, oApp, oDomain, oAccount, oRecipient, aDomain, diffDays
   Set oApp = CreateObject("hMailServer.Application")
   Call oApp.Authenticate("Administrator", "secretpassword")  '  <-- change password
   for j = 0 to oMessage.recipients.count -1
      aUsername = Split(oMessage.Recipients(j).Address,"@")
      If oMessage.Recipients(j).IsLocalUser = true Then
         Set oDomain = oApp.Domains.ItemByName(aUsername(1))
         Set oAccount = oDomain.Accounts.ItemByAddress(oMessage.Recipients(j).Address)
         If oAccount.Active And oAccount.ForwardEnabled = False Then 
            diffDays = DateDiff("d", oAccount.LastLogonTime, Now)  ' get the difference in DAYS
            If diffDays > 99 Then
               Result.Message = "One or more recipients are inactive."
               Result.Value = 2
            End if
         End If
      End If   
   Next
End Sub
(Note, ensure it is OnSMTPData, not OnSTMPData. I wasted 20 minutes getting this thing to work until I saw that. )

BTW, how come it took you so long to get back to this since you first asked in February: viewtopic.php?f=9&t=29167 ?

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-26 08:35
by Nime
Docs say OnSTMPData, I just copied : )
https://www.hmailserver.com/documentati ... onSMTPdata

Yeah I've asked that question in Feb. I was busy, now I have time to implement what I want,
because I am fighting with spams these days. I've installed Sanesecurity & clamd with the help of your
previous posts.

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-26 09:10
by mattg
Have fixed the docs - thanks

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-26 12:07
by jimimaseye
Nime wrote:Docs say OnSTMPData, I just copied : )
https://www.hmailserver.com/documentati ... onSMTPdata

Yeah I've asked that question in Feb. I was busy, now I have time to implement what I want,
because I am fighting with spams these days. I've installed Sanesecurity & clamd with the help of your
previous posts.
Did the script work?

(You know your business but.... isnt 99 days a little quick to 'deactivate' a user/account? Its only just of 3 months).

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-26 15:43
by Nime
Yes it worked! : ) Thanks a lot for the support... I don't deactivate the account, just won't accept new incoming messages.
Delivery to the following recipient failed permanently:

a.gurkan.turker@izsmmmo.com

Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the server for the recipient domain izsmmmo.com by mail.izsmmmo.com. [24.133.83.138].

The error that the other server returned was:
554 The recipient a.gurkan.turker@izsmmmo.com is away for 1321 days.

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-26 16:14
by jimimaseye
You used your version then? Didnt fancy checking all recipients (if more than 1)?

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-26 17:12
by Nime
No I've used your code except the 55x server message.

Needed to check every recipients for active users and if any then accept the message. Multiple recipients are problematic:
Delivery to the following recipient failed permanently:

test@izsmmmo.com

Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the server for the recipient domain izsmmmo.com by mail.izsmmmo.com. [24.133.83.138].

The error that the other server returned was:
554 The recipient a.gurkan.turker@izsmmmo.com is away for 1321 days.
BTW I did decrease the incoming message count again while all the accounts are active.

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-26 17:27
by jimimaseye
Ah so you accept the message if there is at least one valid recipient (ignoring inactive recipients), and only reject if all recipients are inactive?

Re: Accept incoming mail if user recently authenticated

Posted: 2016-05-27 10:44
by Nime
Modified for multiple recipients; it detects active user(s):

Code: Select all

'''''''''''''''''''''''''''''''''''''''''''''
Sub OnSMTPData(oClient, oMessage)
'''''''''''''''''''''''''''''''''''''''''''''

   Dim j, aUsername, oApp, oDomain, oAccount, oRecipient, aDomain, diffDays

   'Dim isAway : isAway = True
   
   Set oApp = CreateObject("hMailServer.Application")
   Call oApp.Authenticate("Administrator", "secretpassword")  '  <-- change password
   for j = 0 to oMessage.recipients.count -1
      aUsername = Split(oMessage.Recipients(j).Address,"@")
      If oMessage.Recipients(j).IsLocalUser = true Then
         Set oDomain = oApp.Domains.ItemByName(aUsername(1))
         Set oAccount = oDomain.Accounts.ItemByAddress(oMessage.Recipients(j).Address)
         If oAccount.Active And oAccount.ForwardEnabled = False Then 
            diffDays = DateDiff("d", oAccount.LastLogonTime, Now)  ' get the difference in DAYS
            If diffDays < 99 Then
            	'isAway = False
            	'Exit For
            	Exit Sub
            End if
         End If
      End If   
   Next
   
   'If isAway Then
         Result.Message = "One or more recipients are inactive."
         Result.Value = 2
   'End If
   
End Sub