From bb0e30ac189c35f75c9780c143bba360cd82fbe5 Mon Sep 17 00:00:00 2001 From: teru Date: Wed, 10 Aug 2016 19:45:23 +0900 Subject: [PATCH] Implement set/unset a CatchAllUser --- catchalluser.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/catchalluser.go b/catchalluser.go index 8ca2aba..7fa2c9a 100644 --- a/catchalluser.go +++ b/catchalluser.go @@ -2,6 +2,7 @@ package mailfull import ( "bufio" + "fmt" "os" "path/filepath" ) @@ -65,3 +66,45 @@ func (r *Repository) CatchAllUser(domainName string) (*CatchAllUser, error) { return catchAllUser, nil } + +// CatchAllUserSet sets a CatchAllUser to the input Domain. +func (r *Repository) CatchAllUserSet(domainName string, catchAllUser *CatchAllUser) error { + existUser, err := r.User(domainName, catchAllUser.Name()) + if err != nil { + return err + } + if existUser == nil { + return ErrUserNotExist + } + + file, err := os.OpenFile(filepath.Join(r.DirMailDataPath, domainName, FileNameCatchAllUser), os.O_RDWR|os.O_TRUNC, 0666) + if err != nil { + return err + } + defer file.Close() + + if _, err := fmt.Fprintf(file, "%s\n", catchAllUser.Name()); err != nil { + return err + } + + return nil +} + +// CatchAllUserUnset removes a CatchAllUser from the input Domain. +func (r *Repository) CatchAllUserUnset(domainName string) error { + existDomain, err := r.Domain(domainName) + if err != nil { + return err + } + if existDomain == nil { + return ErrDomainNotExist + } + + file, err := os.OpenFile(filepath.Join(r.DirMailDataPath, domainName, FileNameCatchAllUser), os.O_RDWR|os.O_TRUNC, 0666) + if err != nil { + return err + } + file.Close() + + return nil +}