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 modconfig
20
21import (
22	"github.com/foxcpp/maddy/framework/config"
23	"github.com/foxcpp/maddy/framework/module"
24)
25
26func MessageCheck(globals map[string]interface{}, args []string, block config.Node) (module.Check, error) {
27	var check module.Check
28	if err := ModuleFromNode("check", args, block, globals, &check); err != nil {
29		return nil, err
30	}
31	return check, nil
32}
33
34// DeliveryDirective is a callback for use in config.Map.Custom.
35//
36// It does all work necessary to create a module instance from the config
37// directive with the following structure:
38//
39//	directive_name mod_name [inst_name] [{
40//	  inline_mod_config
41//	}]
42//
43// Note that if used configuration structure lacks directive_name before mod_name - this function
44// should not be used (call DeliveryTarget directly).
45func DeliveryDirective(m *config.Map, node config.Node) (interface{}, error) {
46	return DeliveryTarget(m.Globals, node.Args, node)
47}
48
49func DeliveryTarget(globals map[string]interface{}, args []string, block config.Node) (module.DeliveryTarget, error) {
50	var target module.DeliveryTarget
51	if err := ModuleFromNode("target", args, block, globals, &target); err != nil {
52		return nil, err
53	}
54	return target, nil
55}
56
57func MsgModifier(globals map[string]interface{}, args []string, block config.Node) (module.Modifier, error) {
58	var check module.Modifier
59	if err := ModuleFromNode("modify", args, block, globals, &check); err != nil {
60		return nil, err
61	}
62	return check, nil
63}
64
65func IMAPFilter(globals map[string]interface{}, args []string, block config.Node) (module.IMAPFilter, error) {
66	var filter module.IMAPFilter
67	if err := ModuleFromNode("imap.filter", args, block, globals, &filter); err != nil {
68		return nil, err
69	}
70	return filter, nil
71}
72
73func StorageDirective(m *config.Map, node config.Node) (interface{}, error) {
74	var backend module.Storage
75	if err := ModuleFromNode("storage", node.Args, node, m.Globals, &backend); err != nil {
76		return nil, err
77	}
78	return backend, nil
79}
80
81// Table is a convenience wrapper for TableDirective.
82//
83//	cfg.Bool(...)
84//	modconfig.Table(cfg, "auth_map", false, false, nil, &mod.authMap)
85//	cfg.Process()
86func Table(cfg *config.Map, name string, inheritGlobal, required bool, defaultVal module.Table, store *module.Table) {
87	cfg.Custom(name, inheritGlobal, required, func() (interface{}, error) {
88		return defaultVal, nil
89	}, TableDirective, store)
90}
91
92func TableDirective(m *config.Map, node config.Node) (interface{}, error) {
93	var tbl module.Table
94	if err := ModuleFromNode("table", node.Args, node, m.Globals, &tbl); err != nil {
95		return nil, err
96	}
97	return tbl, nil
98}