mirror of
https://github.com/jhillyerd/inbucket.git
synced 2026-01-07 19:57:06 +00:00
chore: Update BeforeMailAccepted (#547)
* chore: rename BeforeMailAccepted to BeforeMailFromAccepted Signed-off-by: James Hillyerd <james@hillyerd.com> * chore: update BeforeMailAccepted to use SMTPSession Signed-off-by: James Hillyerd <james@hillyerd.com> --------- Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -29,9 +29,9 @@ type InbucketAfterFuncs struct {
|
||||
// InbucketBeforeFuncs holds references to Lua extension functions to be called
|
||||
// before Inbucket handles an event.
|
||||
type InbucketBeforeFuncs struct {
|
||||
MailAccepted *lua.LFunction
|
||||
MessageStored *lua.LFunction
|
||||
RcptToAccepted *lua.LFunction
|
||||
MailFromAccepted *lua.LFunction
|
||||
MessageStored *lua.LFunction
|
||||
RcptToAccepted *lua.LFunction
|
||||
}
|
||||
|
||||
func registerInbucketTypes(ls *lua.LState) {
|
||||
@@ -186,8 +186,8 @@ func inbucketBeforeIndex(ls *lua.LState) int {
|
||||
|
||||
// Push the requested field's value onto the stack.
|
||||
switch field {
|
||||
case "mail_accepted":
|
||||
ls.Push(funcOrNil(before.MailAccepted))
|
||||
case "mail_from_accepted":
|
||||
ls.Push(funcOrNil(before.MailFromAccepted))
|
||||
case "message_stored":
|
||||
ls.Push(funcOrNil(before.MessageStored))
|
||||
case "rcpt_to_accepted":
|
||||
@@ -206,8 +206,8 @@ func inbucketBeforeNewIndex(ls *lua.LState) int {
|
||||
index := ls.CheckString(2)
|
||||
|
||||
switch index {
|
||||
case "mail_accepted":
|
||||
m.MailAccepted = ls.CheckFunction(3)
|
||||
case "mail_from_accepted":
|
||||
m.MailFromAccepted = ls.CheckFunction(3)
|
||||
case "message_stored":
|
||||
m.MessageStored = ls.CheckFunction(3)
|
||||
case "rcpt_to_accepted":
|
||||
|
||||
@@ -62,7 +62,7 @@ func TestInbucketBeforeFuncs(t *testing.T) {
|
||||
assert(inbucket, "inbucket should not be nil")
|
||||
assert(inbucket.before, "inbucket.before should not be nil")
|
||||
|
||||
local fns = { "mail_accepted", "message_stored" }
|
||||
local fns = { "mail_from_accepted", "message_stored", "rcpt_to_accepted" }
|
||||
|
||||
-- Verify functions start off nil.
|
||||
for i, name in ipairs(fns) do
|
||||
|
||||
@@ -106,8 +106,8 @@ func (h *Host) wireFunctions(logger zerolog.Logger, ls *lua.LState) {
|
||||
if ib.After.MessageStored != nil {
|
||||
events.AfterMessageStored.AddListener(listenerName, h.handleAfterMessageStored)
|
||||
}
|
||||
if ib.Before.MailAccepted != nil {
|
||||
events.BeforeMailAccepted.AddListener(listenerName, h.handleBeforeMailAccepted)
|
||||
if ib.Before.MailFromAccepted != nil {
|
||||
events.BeforeMailFromAccepted.AddListener(listenerName, h.handleBeforeMailFromAccepted)
|
||||
}
|
||||
if ib.Before.MessageStored != nil {
|
||||
events.BeforeMessageStored.AddListener(listenerName, h.handleBeforeMessageStored)
|
||||
@@ -151,18 +151,17 @@ func (h *Host) handleAfterMessageStored(msg event.MessageMetadata) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Host) handleBeforeMailAccepted(addr event.AddressParts) *event.SMTPResponse {
|
||||
logger, ls, ib, ok := h.prepareInbucketFuncCall("before.mail_accepted")
|
||||
func (h *Host) handleBeforeMailFromAccepted(session event.SMTPSession) *event.SMTPResponse {
|
||||
logger, ls, ib, ok := h.prepareInbucketFuncCall("before.mail_from_accepted")
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
defer h.pool.putState(ls)
|
||||
|
||||
logger.Debug().Msgf("Calling Lua function with %+v", addr)
|
||||
logger.Debug().Msgf("Calling Lua function with %+v", session)
|
||||
if err := ls.CallByParam(
|
||||
lua.P{Fn: ib.Before.MailAccepted, NRet: 1, Protect: true},
|
||||
lua.LString(addr.Local),
|
||||
lua.LString(addr.Domain),
|
||||
lua.P{Fn: ib.Before.MailFromAccepted, NRet: 1, Protect: true},
|
||||
wrapSMTPSession(ls, &session),
|
||||
); err != nil {
|
||||
logger.Error().Err(err).Msg("Failed to call Lua function")
|
||||
return nil
|
||||
|
||||
@@ -105,11 +105,11 @@ func TestAfterMessageStored(t *testing.T) {
|
||||
test.AssertNotified(t, notify)
|
||||
}
|
||||
|
||||
func TestBeforeMailAccepted(t *testing.T) {
|
||||
func TestBeforeMailFromAccepted(t *testing.T) {
|
||||
// Register lua event listener.
|
||||
script := `
|
||||
function inbucket.before.mail_accepted(localpart, domain)
|
||||
if localpart == "from" and domain == "test" then
|
||||
function inbucket.before.mail_from_accepted(session)
|
||||
if session.from.address == "from@example.com" then
|
||||
logger.info("allowing message", {})
|
||||
return smtp.allow()
|
||||
else
|
||||
@@ -123,22 +123,30 @@ func TestBeforeMailAccepted(t *testing.T) {
|
||||
consoleLogger, extHost, strings.NewReader(test.LuaInit+script), "test.lua")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Send event to be accepted.
|
||||
addr := &event.AddressParts{Local: "from", Domain: "test"}
|
||||
got := extHost.Events.BeforeMailAccepted.Emit(addr)
|
||||
want := event.ActionAllow
|
||||
require.NotNil(t, got, "Expected result from Emit()")
|
||||
if got.Action != want {
|
||||
t.Errorf("Got %v, wanted %v for addr %v", got.Action, want, addr)
|
||||
{
|
||||
// Send event to be accepted.
|
||||
session := event.SMTPSession{
|
||||
From: &mail.Address{Name: "", Address: "from@example.com"},
|
||||
}
|
||||
got := extHost.Events.BeforeMailFromAccepted.Emit(&session)
|
||||
want := event.ActionAllow
|
||||
require.NotNil(t, got, "Expected result from Emit()")
|
||||
if got.Action != want {
|
||||
t.Errorf("Got %v, wanted %v for addr %v", got.Action, want, session.From)
|
||||
}
|
||||
}
|
||||
|
||||
// Send event to be denied.
|
||||
addr = &event.AddressParts{Local: "reject", Domain: "me"}
|
||||
got = extHost.Events.BeforeMailAccepted.Emit(addr)
|
||||
want = event.ActionDeny
|
||||
require.NotNil(t, got, "Expected result from Emit()")
|
||||
if got.Action != want {
|
||||
t.Errorf("Got %v, wanted %v for addr %v", got.Action, want, addr)
|
||||
{
|
||||
// Send event to be denied.
|
||||
session := event.SMTPSession{
|
||||
From: &mail.Address{Name: "", Address: "from@reject.com"},
|
||||
}
|
||||
got := extHost.Events.BeforeMailFromAccepted.Emit(&session)
|
||||
want := event.ActionDeny
|
||||
require.NotNil(t, got, "Expected result from Emit()")
|
||||
if got.Action != want {
|
||||
t.Errorf("Got %v, wanted %v for addr %v", got.Action, want, session.From)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user