maddy/cmd/maddyctl/appendlimit.go
fox.cpp ae6decd876
Redesign imapsql-ctl utility (now named maddyctl)
Now it is not tied go-imap-sql details (with the exception of special
features), allowing it to be used with other storage backends that will
be added in the future.

--unsafe flag is removed and now maddyctl explicitly asks for
confirmation in cases where transaction may be unsafe for connected
clients. --yes flag disables that. In the future, maddy can be
extended with IPC interface to push updates so it this restriction
can be lifted altogether.
2019-10-20 01:50:07 +03:00

131 lines
2.7 KiB
Go

package main
import (
"errors"
"fmt"
appendlimit "github.com/emersion/go-imap-appendlimit"
"github.com/urfave/cli"
)
// Copied from go-imap-backend-tests.
// AppendLimitStorage is extension for main backend interface (backend.Storage) which
// allows to set append limit value for testing and administration purposes.
type AppendLimitStorage interface {
appendlimit.Backend
// SetMessageLimit sets new value for limit.
// nil pointer means no limit.
SetMessageLimit(val *uint32) error
}
// AppendLimitUser is extension for backend.User interface which allows to
// set append limit value for testing and administration purposes.
type AppendLimitUser interface {
appendlimit.User
// SetMessageLimit sets new value for limit.
// nil pointer means no limit.
SetMessageLimit(val *uint32) error
}
// AppendLimitMbox is extension for backend.Mailbox interface which allows to
// set append limit value for testing and administration purposes.
type AppendLimitMbox interface {
CreateMessageLimit() *uint32
// SetMessageLimit sets new value for limit.
// nil pointer means no limit.
SetMessageLimit(val *uint32) error
}
func mboxesAppendLimit(be Storage, ctx *cli.Context) error {
username := ctx.Args().First()
if username == "" {
return errors.New("Error: USERNAME is required")
}
name := ctx.Args().Get(1)
if name == "" {
return errors.New("Error:MAILBOX is required")
}
u, err := be.GetUser(username)
if err != nil {
return err
}
mbox, err := u.GetMailbox(name)
if err != nil {
return err
}
mboxAL, ok := mbox.(AppendLimitMbox)
if !ok {
return errors.New("Error: Storage does not support per-mailbox append limit")
}
if ctx.IsSet("value,v") {
val := ctx.Int("value,v")
var err error
if val == -1 {
err = mboxAL.SetMessageLimit(nil)
} else {
val32 := uint32(val)
err = mboxAL.SetMessageLimit(&val32)
}
if err != nil {
return err
}
} else {
lim := mboxAL.CreateMessageLimit()
if lim == nil {
fmt.Println("No limit")
} else {
fmt.Println(*lim)
}
}
return nil
}
func usersAppendlimit(be Storage, ctx *cli.Context) error {
username := ctx.Args().First()
if username == "" {
return errors.New("Error: USERNAME is required")
}
u, err := be.GetUser(username)
if err != nil {
return err
}
userAL, ok := u.(AppendLimitUser)
if !ok {
return errors.New("Error: Storage does not support per-user append limit")
}
if ctx.IsSet("value") {
val := ctx.Int("value")
var err error
if val == -1 {
err = userAL.SetMessageLimit(nil)
} else {
val32 := uint32(val)
err = userAL.SetMessageLimit(&val32)
}
if err != nil {
return err
}
} else {
lim := userAL.CreateMessageLimit()
if lim == nil {
fmt.Println("No limit")
} else {
fmt.Println(*lim)
}
}
return nil
}