package mail import ( "bufio" "os/exec" "regexp" "strconv" "strings" ) type Quota struct { Used int64 `json:"used"` // Used storage in bytes Limit int64 `json:"limit"` // Storage limit in bytes (-1 = unlimited) } func GetQuota(email string) (*Quota, error) { cmd := exec.Command("/usr/bin/doveadm", "quota", "get", "-u", email) output, err := cmd.Output() if err != nil { return nil, err } return parseDoveadmQuota(output) } var quotaLineRegex = regexp.MustCompile(`STORAGE\s+(\d+)\s+(\d+)`) func parseDoveadmQuota(output []byte) (*Quota, error) { scanner := bufio.NewScanner(strings.NewReader(string(output))) for scanner.Scan() { line := scanner.Text() matches := quotaLineRegex.FindStringSubmatch(line) if len(matches) == 3 { used, _ := strconv.ParseInt(matches[1], 10, 64) limit, _ := strconv.ParseInt(matches[2], 10, 64) return &Quota{Used: used, Limit: limit}, nil } } return nil, nil }