maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1/*
  2Maddy Mail Server - Composable all-in-one email server.
  3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  4
  5This program is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10This program is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17*/
 18
 19package testutils
 20
 21import (
 22	"context"
 23
 24	"github.com/emersion/go-message/textproto"
 25	"github.com/foxcpp/maddy/framework/buffer"
 26	"github.com/foxcpp/maddy/framework/config"
 27	"github.com/foxcpp/maddy/framework/module"
 28)
 29
 30type Modifier struct {
 31	InstName string
 32
 33	InitErr     error
 34	MailFromErr error
 35	RcptToErr   error
 36	BodyErr     error
 37
 38	MailFrom map[string]string
 39	RcptTo   map[string][]string
 40	AddHdr   textproto.Header
 41
 42	UnclosedStates int
 43}
 44
 45func (m Modifier) Init(*config.Map) error {
 46	return nil
 47}
 48
 49func (m Modifier) Name() string {
 50	return "test_modifier"
 51}
 52
 53func (m Modifier) InstanceName() string {
 54	return m.InstName
 55}
 56
 57type modifierState struct {
 58	m *Modifier
 59}
 60
 61func (m Modifier) ModStateForMsg(ctx context.Context, msgMeta *module.MsgMetadata) (module.ModifierState, error) {
 62	if m.InitErr != nil {
 63		return nil, m.InitErr
 64	}
 65
 66	m.UnclosedStates++
 67	return modifierState{&m}, nil
 68}
 69
 70func (ms modifierState) RewriteSender(ctx context.Context, mailFrom string) (string, error) {
 71	if ms.m.MailFromErr != nil {
 72		return "", ms.m.MailFromErr
 73	}
 74	if ms.m.MailFrom == nil {
 75		return mailFrom, nil
 76	}
 77
 78	newMailFrom, ok := ms.m.MailFrom[mailFrom]
 79	if ok {
 80		return newMailFrom, nil
 81	}
 82	return mailFrom, nil
 83}
 84
 85func (ms modifierState) RewriteRcpt(ctx context.Context, rcptTo string) ([]string, error) {
 86	if ms.m.RcptToErr != nil {
 87		return []string{""}, ms.m.RcptToErr
 88	}
 89
 90	if ms.m.RcptTo == nil {
 91		return []string{rcptTo}, nil
 92	}
 93
 94	newRcptTo, ok := ms.m.RcptTo[rcptTo]
 95	if ok {
 96		return newRcptTo, nil
 97	}
 98	return []string{rcptTo}, nil
 99}
100
101func (ms modifierState) RewriteBody(ctx context.Context, h *textproto.Header, body buffer.Buffer) error {
102	if ms.m.BodyErr != nil {
103		return ms.m.BodyErr
104	}
105
106	for field := ms.m.AddHdr.Fields(); field.Next(); {
107		h.Add(field.Key(), field.Value())
108	}
109	return nil
110}
111
112func (ms modifierState) Close() error {
113	ms.m.UnclosedStates--
114	return nil
115}
116
117func init() {
118	module.Register("test_modifier", func(_, _ string, _, _ []string) (module.Module, error) {
119		return &Modifier{}, nil
120	})
121	module.RegisterInstance(&Modifier{}, nil)
122}