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 ctl
20
21import (
22	"fmt"
23
24	imapbackend "github.com/emersion/go-imap/backend"
25	"github.com/foxcpp/maddy/framework/module"
26	"github.com/urfave/cli/v2"
27)
28
29// Copied from go-imap-backend-tests.
30
31// AppendLimitUser is extension for backend.User interface which allows to
32// set append limit value for testing and administration purposes.
33type AppendLimitUser interface {
34	imapbackend.AppendLimitUser
35
36	// SetMessageLimit sets new value for limit.
37	// nil pointer means no limit.
38	SetMessageLimit(val *uint32) error
39}
40
41func imapAcctAppendlimit(be module.Storage, ctx *cli.Context) error {
42	username := ctx.Args().First()
43	if username == "" {
44		return cli.Exit("Error: USERNAME is required", 2)
45	}
46
47	u, err := be.GetIMAPAcct(username)
48	if err != nil {
49		return err
50	}
51	userAL, ok := u.(AppendLimitUser)
52	if !ok {
53		return cli.Exit("Error: module.Storage does not support per-user append limit", 2)
54	}
55
56	if ctx.IsSet("value") {
57		val := ctx.Int("value")
58
59		var err error
60		if val == -1 {
61			err = userAL.SetMessageLimit(nil)
62		} else {
63			val32 := uint32(val)
64			err = userAL.SetMessageLimit(&val32)
65		}
66		if err != nil {
67			return err
68		}
69	} else {
70		lim := userAL.CreateMessageLimit()
71		if lim == nil {
72			fmt.Println("No limit")
73		} else {
74			fmt.Println(*lim)
75		}
76	}
77
78	return nil
79}