Can't delete message by calling function from ACCOUNT rule

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
User avatar
katip
Senior user
Senior user
Posts: 673
Joined: 2006-12-22 07:58
Location: Istanbul

Can't delete message by calling function from ACCOUNT rule

Post by katip » 2019-05-14 01:26

my chronic headache.
i found this here : https://www.hmailserver.com/forum/viewt ... 57#p119141
10 years ago the guy suffered from the same. didn't tell it so but very likely a "Subscript out of range" failure.
once again in short:
account rule
message size > 0 calls a function
function does some checks
message either passes this checks, returns from function and lands to inbox or should be deleted by the same function (such as ...Messages.DeleteByDBID(oMessage.ID) )
has anyone achieved this?

no, "add a header, save message, next rule deletes it" isn't the solution. that way it indeed gets deleted BUT leaves that famous confusing "The mail server could not deliver the message to you since the file .... does not exist on the server" message behind and user is upset.
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

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

Re: Can't delete message by calling function from ACCOUNT rule

Post by jimimaseye » 2019-05-14 09:05

Have you tried/are you able to try and replicate the Account Rule within a script (the conditions etc) and calling the script in AcceptMessage (or somewhere similar)?

ie

OnAccept Message:

Code: Select all

if message oMessage.to = the AccountOfWhereTheRuleWouldBeSat@domain.com and [other criteria] then
   reject (with code 2)
endif
(or maybe at OnDeliverMessage?)

Just a thought.


EDIT:

OR.....

split the function's actions down and put the conditions for calling it in the rule:

ie,
if messagesize > 0 and
condition 2 and
condition 3

then

action1 (do a function)
action2 (do another function)
delete message
where the combination of the conditions and the actions match the script
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

User avatar
SorenR
Senior user
Senior user
Posts: 3169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Can't delete message by calling function from ACCOUNT rule

Post by SorenR » 2019-05-14 10:31

In the ideal world you would use something like:

Code: Select all

   Dim oApp : Set oApp = CreateObject("hMailServer.Application")
   Call oApp.Authenticate(ADMIN, PASSWORD)

   Dim oMessages : Set oMessages = CreateObject("hMailServer.Messages")
   Call oMessages.DeleteByDBID(oMessage.ID)

   Set oMessages = Nothing
   Set oApp = Nothing
"Set oMessages = CreateObject("hMailServer.Messages")" give me this ...

Code: Select all

"ERROR"	3604		"2019-05-14 10:08:35.175"	"Script Error: Source: hMailServer COM library - Error: 800403E9 - Description: You do not have access to this property / method. Ensure that hMailServer.Application.Authenticate() is called with proper login credentials. - Line: 1484 Column: 3 - Code: (null)"
SørenR.

The quantum rule of insecurity which states that the act of observing how vulnerable a host or service is changes the insecurity level of the service.

User avatar
katip
Senior user
Senior user
Posts: 673
Joined: 2006-12-22 07:58
Location: Istanbul

Re: Can't delete message by calling function from ACCOUNT rule

Post by katip » 2019-05-14 16:55

my problem is "Subscript out of range".
it seems ID isn't written in table unless file is saved on disk. and then it means message has already landed in inbox. too late for anything.
am i right? if yes, no need to waste further time. i already have workarounds but it's a shame that calling a "delete" from an account rule is not possible (or clean).
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

User avatar
SorenR
Senior user
Senior user
Posts: 3169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Can't delete message by calling function from ACCOUNT rule

Post by SorenR » 2019-05-14 18:53

katip wrote:
2019-05-14 16:55
my problem is "Subscript out of range".
it seems ID isn't written in table unless file is saved on disk. and then it means message has already landed in inbox. too late for anything.
am i right? if yes, no need to waste further time. i already have workarounds but it's a shame that calling a "delete" from an account rule is not possible (or clean).
ID is written in table long before message hits inbox. You should be able to find oMessage.ID already in OnDeliveryStart(). .eml file is in .\hMailServer\Data at this stage.

Code: Select all

Option Explicit

Sub OnDeliveryStart(oMessage)
   EventLog.Write( "Sub OnDeliveryStart(oMessage)       ID: " & CLng(oMessage.ID) )
   EventLog.Write( "Sub OnDeliveryStart(oMessage) Filename: " & oMessage.Filename )
End Sub

Sub OnDeliverMessage(oMessage)
   EventLog.Write( "Sub OnDeliverMessage(oMessage)       ID: " & CLng(oMessage.ID) )
   EventLog.Write( "Sub OnDeliverMessage(oMessage) Filename: " & oMessage.Filename )
End Sub

Sub OnDelivered(oMessage) ' <=== ACCOUNT RULE!
   EventLog.Write( "Sub OnDelivered(oMessage)       ID: " & CLng(oMessage.ID) )
   EventLog.Write( "Sub OnDelivered(oMessage) Filename: " & oMessage.Filename )
End Sub

Code: Select all

3604		"2019-05-14 18:47:33.268"	"Sub OnDeliveryStart(oMessage)       ID: 1042113"
3604		"2019-05-14 18:47:33.268"	"Sub OnDeliveryStart(oMessage) Filename: C:\hMailServer\Data\{92F2C5C9-3DED-46F2-95CF-DFFCE062C6D6}.eml"
3604		"2019-05-14 18:47:33.284"	"Sub OnDeliverMessage(oMessage)       ID: 1042113"
3604		"2019-05-14 18:47:33.284"	"Sub OnDeliverMessage(oMessage) Filename: C:\hMailServer\Data\{92F2C5C9-3DED-46F2-95CF-DFFCE062C6D6}.eml"
3604		"2019-05-14 18:47:33.299"	"Sub OnDelivered(oMessage)       ID: 1042113"
3604		"2019-05-14 18:47:33.299"	"Sub OnDelivered(oMessage) Filename: C:\hMailServer\Data\mydomain.tld\postmaster\92\{92F2C5C9-3DED-46F2-95CF-DFFCE062C6D6}.eml"
SørenR.

The quantum rule of insecurity which states that the act of observing how vulnerable a host or service is changes the insecurity level of the service.

User avatar
katip
Senior user
Senior user
Posts: 673
Joined: 2006-12-22 07:58
Location: Istanbul

Re: Can't delete message by calling function from ACCOUNT rule

Post by katip » 2019-05-14 20:30

SorenR wrote:
2019-05-14 18:53
ID is written in table long before message hits inbox. You should be able to find oMessage.ID already in OnDeliveryStart(). .eml file is in .\hMailServer\Data at this stage.
hmm, my approach was:

Code: Select all

...
strLocalSender = oMessage.Recipients(0).Address '<--- account from which the function is called by account rule
...
For i = 0 To oAccounts.Count -1
    If oAccounts(i).Address = strLocalSender Then
        oAccounts(i).Messages.DeleteByDBID(oMessage.ID)
        'oAccounts(i).IMAPFolders.ItemByName("Inbox").Messages.DeleteByDBID(oMessage.ID) '<-- IMAP version, doesn't work either
    End If
Next
this returns "Subscription..." error.
ok, i can retrieve Message.ID at an earlier stage, but i must know that strLocalSender is a particular user. i don't consider to manipulate the message in one lot with entire recipientlist. (it's again that redundant duplicates elimination. some recipients have already received their copy and a second copy (i.e. by group, alias or forwards) should be abondoned.)
what do you think?
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

User avatar
SorenR
Senior user
Senior user
Posts: 3169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Can't delete message by calling function from ACCOUNT rule

Post by SorenR » 2019-05-14 21:38

katip wrote:
2019-05-14 20:30
SorenR wrote:
2019-05-14 18:53
ID is written in table long before message hits inbox. You should be able to find oMessage.ID already in OnDeliveryStart(). .eml file is in .\hMailServer\Data at this stage.
hmm, my approach was:

Code: Select all

...
strLocalSender = oMessage.Recipients(0).Address '<--- account from which the function is called by account rule
...
For i = 0 To oAccounts.Count -1
    If oAccounts(i).Address = strLocalSender Then
        oAccounts(i).Messages.DeleteByDBID(oMessage.ID)
        'oAccounts(i).IMAPFolders.ItemByName("Inbox").Messages.DeleteByDBID(oMessage.ID) '<-- IMAP version, doesn't work either
    End If
Next
this returns "Subscription..." error.
ok, i can retrieve Message.ID at an earlier stage, but i must know that strLocalSender is a particular user. i don't consider to manipulate the message in one lot with entire recipientlist. (it's again that redundant duplicates elimination. some recipients have already received their copy and a second copy (i.e. by group, alias or forwards) should be abondoned.)
what do you think?
I've lost track of this issue, can you link to previous threads?

It's something about avoiding double messaging if "Commercial vs Community Google Mail" and including "the recipient they forgot" no?

Sender is _always_ external?
Recipient _list_ are local users?
SørenR.

The quantum rule of insecurity which states that the act of observing how vulnerable a host or service is changes the insecurity level of the service.

User avatar
SorenR
Senior user
Senior user
Posts: 3169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Can't delete message by calling function from ACCOUNT rule

Post by SorenR » 2019-05-14 23:28

Been experimenting...

Reading the DB Table hm_messages from the Account Rule (Run Function) and it shows that the ID of the message (oMessage.ID) do exist BUT (!) "messageaccountid" and "messagefolderid" are 0, so it appears the message is owned by "nobody" in folder "nowhere" and this is probably why you are getting "Subscript out of range".

Since the "messages" object can not be used standalone there is no immediate solution.
SørenR.

The quantum rule of insecurity which states that the act of observing how vulnerable a host or service is changes the insecurity level of the service.

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

Re: Can't delete message by calling function from ACCOUNT rule

Post by jimimaseye » 2019-05-14 23:31

It's almost as if we could do with the software author still being around to make code changes, isn't it. :roll:

[Entered by mobile. Excuse my spelling.]
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

User avatar
katip
Senior user
Senior user
Posts: 673
Joined: 2006-12-22 07:58
Location: Istanbul

Re: Can't delete message by calling function from ACCOUNT rule

Post by katip » 2019-05-15 02:41

SorenR wrote:
2019-05-14 21:38
I've lost track of this issue, can you link to previous threads?

It's something about avoiding double messaging if "Commercial vs Community Google Mail" and including "the recipient they forgot" no?

Sender is _always_ external?
Recipient _list_ are local users?
yes, that was it.
sender may be both external or local. recipient is always local.
initially we have discussed it here : viewtopic.php?f=7&t=33744
and here : viewtopic.php?f=7&t=33791
finally here with your elegant cloning solution : viewtopic.php?f=9&t=33764
Reading the DB Table hm_messages from the Account Rule (Run Function) and it shows that the ID of the message (oMessage.ID) do exist BUT (!) "messageaccountid" and "messagefolderid" are 0, so it appears the message is owned by "nobody" in folder "nowhere" and this is probably why you are getting "Subscript out of range".
that's clear enough now.

my workaround: tag the message with a header -> rule action -> move to public folder which is kinda trash bin and should be emptied periodically. that works fine! problem is that my public folder feature is broken, messages don't show up there but this is another issue, i can live with it though..
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

Post Reply