Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

feat: implement mailbox quota management and alert system

lotk2eb06ab6b07e
diff --git a/Dockerfiles/dovecot/dovecot.sh b/Dockerfiles/dovecot/dovecot.sh
index cd193379..f646d335 100644
--- a/Dockerfiles/dovecot/dovecot.sh
+++ b/Dockerfiles/dovecot/dovecot.sh
@@ -22,7 +22,7 @@ connect = host=pgsql dbname=${DBNAME} user=${DBUSER} password=${DBPASS}
default_pass_scheme = MD5-CRYPT
-user_query = SELECT '/var/vmail/%d/%n' as home, 'maildir:/var/vmail/%d/%n' as mail, 150 AS uid, 8 AS gid, CONCAT('dirsize:storage=', quota) AS quota FROM mailbox WHERE username = '%u' AND active = 1
+user_query = SELECT '/var/vmail/%d/%n' as home, 'maildir:/var/vmail/%d/%n' as mail, 150 AS uid, 8 AS gid, 'maildir:storage=' || quota AS quota FROM mailbox WHERE username = '%u' AND active = 1
password_query = SELECT username as user, password, '/var/vmail/%d/%n' as userdb_home, 'maildir:/var/vmail/%d/%n' as userdb_mail, 150 as userdb_uid, 8 as userdb_gid FROM mailbox WHERE username = '%u' AND active = 1
diff --git a/core/api/domains/v1/domains.go b/core/api/domains/v1/domains.go
index d8201278..165ca2c4 100644
--- a/core/api/domains/v1/domains.go
+++ b/core/api/domains/v1/domains.go
@@ -35,22 +35,22 @@ type CertInfo struct {
// Domain defines the domain entity
type Domain struct {
- Domain string `json:"domain" dc:"Domain name"`
- ARecord string `json:"a_record" dc:"A record"`
- Mailboxes int `json:"mailboxes" dc:"Number of mailboxes created"`
- MailboxQuota int64 `json:"mailbox_quota" dc:"Default mailbox space size"`
- Quota int64 `json:"quota" dc:"Domain quota"`
- RateLimit int `json:"rate_limit" dc:"Rate limit for sending emails per second"`
- CreateTime int64 `json:"create_time" dc:"Creation time"`
- Active int `json:"active" dc:"Status: 1-enabled, 0-disabled"`
- DNSRecords DNSRecords `json:"dns_records" dc:"DNS records"`
- CertInfo CertInfo `json:"cert_info" dc:"Certificate information"`
- Catchall string `json:"email" dc:"Cache all DNS records, used for domain verification"`
- Default int `json:"default" dc:"Default sender domain, 1-yes, 0-no"`
- Urls []string `json:"urls" dc:"Additional URLs associated with the domain"`
- HasBrandInfo int `json:"hasbrandinfo" dc:"Brand information : 1-exist, 0-not exist"`
- // 补充专属ip
+ Domain string `json:"domain" dc:"Domain name"`
+ ARecord string `json:"a_record" dc:"A record"`
+ Mailboxes int `json:"mailboxes" dc:"Number of mailboxes created"`
+ MailboxQuota int64 `json:"mailbox_quota" dc:"Default mailbox space size"`
+ Quota int64 `json:"quota" dc:"Domain quota"`
+ RateLimit int `json:"rate_limit" dc:"Rate limit for sending emails per second"`
+ CreateTime int64 `json:"create_time" dc:"Creation time"`
+ Active int `json:"active" dc:"Status: 1-enabled, 0-disabled"`
+ DNSRecords DNSRecords `json:"dns_records" dc:"DNS records"`
+ CertInfo CertInfo `json:"cert_info" dc:"Certificate information"`
+ Catchall string `json:"email" dc:"Cache all DNS records, used for domain verification"`
+ Default int `json:"default" dc:"Default sender domain, 1-yes, 0-no"`
+ Urls []string `json:"urls" dc:"Additional URLs associated with the domain"`
+ HasBrandInfo int `json:"hasbrandinfo" dc:"Brand information : 1-exist, 0-not exist"`
MultiIPDomains *MultiIPDomain `json:"multi_ip_domains" dc:"Multiple IP domains"`
+ CurrentUsage int64 `json:"current_usage" dc:"Domain Current usage"`
}
type AddDomainReq struct {
@@ -65,7 +65,7 @@ type AddDomainReq struct {
Catchall string `json:"email" v:"email" dc:"Catch all email address, used for domain verification"`
Urls []string `json:"urls" dc:"Additional URLs associated with the domain"`
HasBrandInfo int `json:"hasbrandinfo" dc:"Brand information : 1-exist, 0-not exist"`
- OutboundIp string `json:"outbound_ip" v:"ipv4" dc:"Exclusive IP address for the domain, used for sending emails"`
+ OutboundIp string `json:"outbound_ip" v:"ipv4" dc:"Exclusive IP address for the domain, used for sending emails"`
}
type AddDomainRes struct {
@@ -85,7 +85,7 @@ type UpdateDomainReq struct {
Catchall string `json:"email" v:"email" dc:"Catch all email address, used for domain verification"`
Urls []string `json:"urls" dc:"Additional URLs associated with the domain"`
HasBrandInfo int `json:"hasbrandinfo" dc:"Brand information : 1-exist, 0-not exist"`
- OutboundIp string `json:"outbound_ip" v:"ipv4" dc:"Exclusive IP address for the domain, used for sending emails"`
+ OutboundIp string `json:"outbound_ip" v:"ipv4" dc:"Exclusive IP address for the domain, used for sending emails"`
}
type UpdateDomainRes struct {
diff --git a/core/api/mail_boxes/v1/mailboxes.go b/core/api/mail_boxes/v1/mailboxes.go
index a09fe1ed..9cfe864e 100644
--- a/core/api/mail_boxes/v1/mailboxes.go
+++ b/core/api/mail_boxes/v1/mailboxes.go
@@ -19,6 +19,8 @@ type Mailbox struct {
CreateTime int64 `json:"create_time" dc:"Creation time"`
UpdateTime int64 `json:"update_time" dc:"Update time"`
Active int `json:"active" dc:"Status: 1-enabled, 0-disabled"`
+ UsedQuota int64 `json:"used_quota" dc:"Used Mailbox quota"`
+ QuotaActive int `json:"quota_active" dc:"Quota switch 1: On 0: Off"`
}
type AddMailboxReq struct {
@@ -30,7 +32,8 @@ type AddMailboxReq struct {
Password string `json:"password" v:"required|min-length:8" dc:"Password"`
Active int `json:"active" v:"required" dc:"Status" d:"1"`
IsAdmin int `json:"isAdmin" v:"required" dc:"IsAdmin" d:"0"`
- Quota int `json:"quota" v:"required|min:1" dc:"Quota" d:"5242880"`
+ Quota int `json:"quota" dc:"Quota" d:"5242880"`
+ QuotaActive int `json:"quota_active" v:"in:0,1" dc:"Quota switch 1: On 0: Off" d:"1"`
}
type AddMailboxRes struct {
@@ -41,10 +44,10 @@ type BatchAddMailboxReq struct {
g.Meta `path:"/mailbox/batch_create" tags:"MailBox" method:"post" summary:"Batch create mailbox" in:"body"`
Authorization string `json:"authorization" dc:"Authorization" in:"header"`
Domain string `json:"domain" v:"required|domain" dc:"Domain"`
-
- Quota int `json:"quota" v:"required|min:1" dc:"Quota" d:"5242880"`
+ Quota int `json:"quota" dc:"Quota" d:"5242880"`
Count int `json:"count" v:"required|min:2" dc:"Count" d:"10"`
Prefix string `json:"prefix" v:"regex:[\\w-]{0,}" dc:"Email name prefix, optional" d:"user"`
+ QuotaActive int `json:"quota_active" v:"in:0,1" dc:"Quota switch 1: On 0: Off" d:"1"`
}
type BatchAddMailboxRes struct {
@@ -60,7 +63,8 @@ type UpdateMailboxReq struct {
Password string `json:"password" v:"required|min-length:8" dc:"Password"`
Active int `json:"active" v:"required" dc:"Status" d:"1"`
IsAdmin int `json:"isAdmin" v:"required" dc:"IsAdmin" d:"0"`
- Quota int `json:"quota" v:"required|min:1" dc:"Quota" d:"5242880"`
+ Quota int `json:"quota" dc:"Quota" d:"5242880"`
+ QuotaActive int `json:"quota_active" v:"in:0,1" dc:"Quota switch 1: On 0: Off" d:"1"`
}
type UpdateMailboxRes struct {
diff --git a/core/internal/controller/mail_boxes/mail_boxes_v1_add_mailbox.go b/core/internal/controller/mail_boxes/mail_boxes_v1_add_mailbox.go
index aafe9ecf..e5da1780 100644
--- a/core/internal/controller/mail_boxes/mail_boxes_v1_add_mailbox.go
+++ b/core/internal/controller/mail_boxes/mail_boxes_v1_add_mailbox.go
@@ -24,6 +24,7 @@ func (c *ControllerV1) AddMailbox(ctx context.Context, req *v1.AddMailboxReq) (r
LocalPart: req.LocalPart,
Domain: req.Domain,
Active: req.Active,
+ QuotaActive: req.QuotaActive,
}
if mailbox.FullName == "" {
diff --git a/core/internal/controller/mail_boxes/mail_boxes_v1_batch_add_mailbox.go b/core/internal/controller/mail_boxes/mail_boxes_v1_batch_add_mailbox.go
index cc868570..feb90f67 100644
--- a/core/internal/controller/mail_boxes/mail_boxes_v1_batch_add_mailbox.go
+++ b/core/internal/controller/mail_boxes/mail_boxes_v1_batch_add_mailbox.go
@@ -18,7 +18,7 @@ func (c *ControllerV1) BatchAddMailbox(ctx context.Context, req *v1.BatchAddMail
return nil, fmt.Errorf("You cannot create more than 5000 batches")
}
- createdEmails, err := mail_boxes.BatchAdd(ctx, req.Domain, req.Quota, req.Count, req.Prefix)
+ createdEmails, err := mail_boxes.BatchAdd(ctx, req.Domain, req.Quota, req.Count, req.Prefix, req.QuotaActive)
if err != nil {
return nil, err
}
diff --git a/core/internal/controller/mail_boxes/mail_boxes_v1_export_mailbox.go b/core/internal/controller/mail_boxes/mail_boxes_v1_export_mailbox.go
index 04e64fdf..9e40b9d4 100644
--- a/core/internal/controller/mail_boxes/mail_boxes_v1_export_mailbox.go
+++ b/core/internal/controller/mail_boxes/mail_boxes_v1_export_mailbox.go
@@ -47,13 +47,14 @@ func (c *ControllerV1) ExportMailbox(ctx context.Context, req *v1.ExportMailboxR
var b strings.Builder
w := csv.NewWriter(&b)
- _ = w.Write([]string{"username", "full_name", "is_admin", "quota", "local_part", "domain", "active", "password", "password_encode"})
+ _ = w.Write([]string{"username", "full_name", "is_admin", "quota", "quota_active", "local_part", "domain", "active", "password", "password_encode"})
for _, m := range mailboxes {
_ = w.Write([]string{
m.Username,
m.FullName,
fmt.Sprintf("%d", m.IsAdmin),
fmt.Sprintf("%d", m.Quota),
+ fmt.Sprintf("%d", m.QuotaActive),
m.LocalPart,
m.Domain,
fmt.Sprintf("%d", m.Active),
diff --git a/core/internal/controller/mail_boxes/mail_boxes_v1_import_mailbox.go b/core/internal/controller/mail_boxes/mail_boxes_v1_import_mailbox.go
index af4a3ff7..7ddc842d 100644
--- a/core/internal/controller/mail_boxes/mail_boxes_v1_import_mailbox.go
+++ b/core/internal/controller/mail_boxes/mail_boxes_v1_import_mailbox.go
@@ -63,6 +63,8 @@ func (c *ControllerV1) ImportMailbox(ctx context.Context, req *v1.ImportMailboxR
m.IsAdmin = parseInt(row[i])
case "quota":
m.Quota = int64(parseInt(row[i]))
+ case "quota_active":
+ m.QuotaActive = parseInt(row[i])
case "local_part":
m.LocalPart = row[i]
case "domain":
@@ -128,6 +130,8 @@ func (c *ControllerV1) ImportMailbox(ctx context.Context, req *v1.ImportMailboxR
if mailboxes[i].Active == 0 {
mailboxes[i].Active = 1
}
+
+ mailboxes[i].QuotaActive = 1
}
var failed []string
diff --git a/core/internal/controller/mail_boxes/mail_boxes_v1_update_mailbox.go b/core/internal/controller/mail_boxes/mail_boxes_v1_update_mailbox.go
index feaba7a2..dd901eec 100644
--- a/core/internal/controller/mail_boxes/mail_boxes_v1_update_mailbox.go
+++ b/core/internal/controller/mail_boxes/mail_boxes_v1_update_mailbox.go
@@ -22,6 +22,7 @@ func (c *ControllerV1) UpdateMailbox(ctx context.Context, req *v1.UpdateMailboxR
LocalPart: req.LocalPart,
Domain: req.Domain,
Active: req.Active,
+ QuotaActive: req.QuotaActive,
}
if mailbox.FullName == "" {
mailbox.FullName = req.LocalPart
diff --git a/core/internal/service/database_initialization/mail_serv.go b/core/internal/service/database_initialization/mail_serv.go
index f80ee7d1..4a44833c 100644
--- a/core/internal/service/database_initialization/mail_serv.go
+++ b/core/internal/service/database_initialization/mail_serv.go
@@ -20,6 +20,7 @@ func init() {
active SMALLINT NOT NULL DEFAULT 1,
urls TEXT[] NOT NULL DEFAULT '{}'::TEXT[],
hasbrandinfo SMALLINT NOT NULL DEFAULT 0,
+ current_usage BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (domain)
)`,
@@ -37,6 +38,8 @@ func init() {
create_time int NOT NULL default 0,
update_time int NOT NULL default 0,
active SMALLINT NOT NULL DEFAULT 1,
+ used_quota BIGINT NOT NULL DEFAULT 0,
+ quota_active SMALLINT NOT NULL DEFAULT 1,
PRIMARY KEY (username)
)`,
@@ -119,5 +122,12 @@ func init() {
// domain
_ = AddColumnIfNotExists("domain", "urls", "TEXT[]", "'{}'::TEXT[]", false)
_ = AddColumnIfNotExists("domain", "hasbrandinfo", "SMALLINT", "0", false)
+ _ = AddColumnIfNotExists("domain", "current_usage", "BIGINT", "0", true)
+
+
+ // mailbox used quota column
+ _ = AddColumnIfNotExists("mailbox", "used_quota", "BIGINT", "0", true)
+ _ = AddColumnIfNotExists("mailbox", "quota_active", "SMALLINT", "1", true)
+
})
}
diff --git a/core/internal/service/mail_boxes/check_quota_alerts.go b/core/internal/service/mail_boxes/check_quota_alerts.go
new file mode 100644
index 00000000..d0ec3fdf
--- /dev/null
+++ b/core/internal/service/mail_boxes/check_quota_alerts.go
@@ -0,0 +1,247 @@
+package mail_boxes
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "strings"
+ "sync"
+ "time"
+
+ "billionmail-core/internal/service/mail_service"
+ "github.com/gogf/gf/v2/frame/g"
+)
+
+// -----------------------------
+// Check the email domain and mailbox quota usage and send alerts. Scheduled task
+// Rule: quota_active = 1 and quota > 0, usage rate >= 90% or 95% triggers an alert (priority is given to the higher threshold)
+// De-duplication: Do not repeat alerts for the same object and the same threshold within 24 hours
+// -----------------------------
+
+const (
+ quotaAlertThreshold90 = 90
+ quotaAlertThreshold95 = 95
+ quotaAlertHoldHours = 24
+)
+
+type quotaAlertTarget struct {
+ Kind string `json:"kind"` // domain/mailbox
+ Target string `json:"target"`
+ Percent float64 `json:"percent"`
+ Used int64 `json:"used"`
+ Quota int64 `json:"quota"`
+ Threshold int `json:"threshold"`
+}
+
+var (
+ // key: kind|target|threshold -> lastTime
+ quotaAlertCache = sync.Map{} // map[string]time.Time
+)
+
+// CheckMailboxesQuotaAlerts 域名/邮箱配额告警
+func CheckMailboxesQuotaAlerts(ctx context.Context) {
+
+ domainAlerts := collectDomainQuotaAlerts(ctx)
+ mailboxAlerts := collectMailboxQuotaAlerts(ctx)
+
+ if len(domainAlerts) == 0 && len(mailboxAlerts) == 0 {
+ return
+ }
+
+ all := append(domainAlerts, mailboxAlerts...)
+ for _, a := range all {
+
+ if shouldSkipAlert(a) {
+ continue
+ }
+
+ //recordQuotaAlert(ctx, a)
+
+ markAlertSent(a)
+
+ // send email asynchronously
+ go func(alert quotaAlertTarget) {
+ if err := sendQuotaAlertEmail(ctx, alert); err != nil {
+ g.Log().Warning(ctx, "send quota alert email failed", err)
+ }
+ }(a)
+ }
+}
+
+func collectDomainQuotaAlerts(ctx context.Context) []quotaAlertTarget {
+ type dRow struct {
+ Domain string
+ Quota int64
+ Used int64
+ }
+ var rows []dRow
+
+ if err := g.DB().Model("domain").
+ Fields("domain, quota, COALESCE(current_usage,0) as used").
+ Where("quota > 0").
+ Scan(&rows); err != nil && err != sql.ErrNoRows {
+ g.Log().Warning(ctx, "collect domain quota failed", err)
+ return nil
+ }
+ alerts := make([]quotaAlertTarget, 0)
+ for _, r := range rows {
+ if r.Quota <= 0 {
+ continue
+ }
+ pct := float64(r.Used) / float64(r.Quota) * 100
+ var threshold int
+ if pct >= quotaAlertThreshold95 {
+ threshold = quotaAlertThreshold95
+ } else if pct >= quotaAlertThreshold90 {
+ threshold = quotaAlertThreshold90
+ } else {
+ continue
+ }
+ alerts = append(alerts, quotaAlertTarget{Kind: "domain", Target: r.Domain, Percent: pct, Used: r.Used, Quota: r.Quota, Threshold: threshold})
+ }
+ return alerts
+}
+
+func collectMailboxQuotaAlerts(ctx context.Context) []quotaAlertTarget {
+ type mRow struct {
+ Username string
+ Quota int64
+ UsedQuota int64
+ QuotaActive int
+ }
+ var rows []mRow
+ if err := g.DB().Model("mailbox").
+ Fields("username, quota, COALESCE(used_quota,0) used_quota, COALESCE(quota_active,1) quota_active").
+ Where("active", 1).
+ Where("quota_active", 1).
+ Where("quota > ?", 0).
+ Scan(&rows); err != nil && err != sql.ErrNoRows {
+ g.Log().Warning(ctx, "collect mailbox quota failed", err)
+ return nil
+ }
+
+ if len(rows) == 0 {
+ return nil
+ }
+
+ alerts := make([]quotaAlertTarget, 0)
+ for _, r := range rows {
+
+ pct := float64(r.UsedQuota) / float64(r.Quota) * 100
+ var threshold int
+ if pct >= quotaAlertThreshold95 {
+ threshold = quotaAlertThreshold95
+ } else if pct >= quotaAlertThreshold90 {
+ threshold = quotaAlertThreshold90
+ } else {
+ continue
+ }
+ alerts = append(alerts, quotaAlertTarget{Kind: "mailbox", Target: r.Username, Percent: pct, Used: r.UsedQuota, Quota: r.Quota, Threshold: threshold})
+ }
+ return alerts
+}
+
+// skip if alerted within 24 hours for the same object and threshold
+func shouldSkipAlert(a quotaAlertTarget) bool {
+ key := fmt.Sprintf("%s|%s|%d", a.Kind, a.Target, a.Threshold)
+ if v, ok := quotaAlertCache.Load(key); ok {
+ if ts, ok2 := v.(time.Time); ok2 {
+ if time.Since(ts) < quotaAlertHoldHours*time.Hour {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+func markAlertSent(a quotaAlertTarget) {
+ key := fmt.Sprintf("%s|%s|%d", a.Kind, a.Target, a.Threshold)
+ quotaAlertCache.Store(key, time.Now())
+}
+
+//func recordQuotaAlert(ctx context.Context, a quotaAlertTarget) {
+// //data, _ := json.Marshal(a)
+// msg := fmt.Sprintf("Quota alert %s=%s threshold=%d used=%d quota=%d percent=%.2f%%", a.Kind, a.Target, a.Threshold, a.Used, a.Quota, a.Percent)
+//}
+
+func sendQuotaAlertEmail(ctx context.Context, a quotaAlertTarget) error {
+ domain := ""
+ if a.Kind == "mailbox" {
+ parts := strings.Split(a.Target, "@")
+ if len(parts) == 2 {
+ domain = parts[1]
+ }
+ } else if a.Kind == "domain" {
+ domain = a.Target
+ }
+ if domain == "" {
+ d, _ := getDefaultSendDomain()
+ domain = d
+ }
+
+ fromAddress := fmt.Sprintf("noreply@%s", domain)
+ toAddress := fromAddress
+ if a.Kind == "mailbox" {
+ toAddress = a.Target
+ }
+
+ usedStr := formatSize(a.Used)
+ quotaStr := formatSize(a.Quota)
+ subject := fmt.Sprintf("[Quota %d%% Alert] %s usage %.1f%%", a.Threshold, a.Target, a.Percent)
+ if a.Percent >= 100 {
+ subject = fmt.Sprintf("[Quota EXCEEDED] %s %.1f%%", a.Target, a.Percent)
+ }
+
+ bodyBuilder := &strings.Builder{}
+ fmt.Fprintf(bodyBuilder, "Hello,\n\n")
+ if a.Kind == "mailbox" {
+ fmt.Fprintf(bodyBuilder, "Mailbox: %s\n", a.Target)
+ } else {
+ fmt.Fprintf(bodyBuilder, "Domain: %s\n", a.Target)
+ }
+ fmt.Fprintf(bodyBuilder, "Quota: %s\nUsed: %s (%.2f%%)\nThreshold: %d%%\n\n", quotaStr, usedStr, a.Percent, a.Threshold)
+ if a.Percent >= 100 {
+ bodyBuilder.WriteString("Status: EXCEEDED LIMIT, new mails may be rejected.\n\n")
+ } else {
+ bodyBuilder.WriteString("Status: Approaching limit.\n\n")
+ }
+ bodyBuilder.WriteString("Suggested actions:\n - Delete large or old mails\n - Empty Trash/Spam folders\n - Request quota increase if necessary\n\nThis is an automated notification.\n")
+ content := bodyBuilder.String()
+
+ sender, err := mail_service.NewEmailSenderWithLocal(fromAddress)
+ if err != nil {
+ return err
+ }
+ defer sender.Close()
+ msg := mail_service.NewMessage(subject, content)
+ msg.SetRealName("Quota Monitor")
+
+ return sender.Send(msg, []string{toAddress})
+}
+
+func getDefaultSendDomain() (string, error) {
+
+ var defaultDomain string
+ val, err := g.DB().Model("bm_options").Where("name", "default_sender_domain").Value("value")
+ if err == nil && val != nil {
+ defaultDomain = val.String()
+ }
+
+ return defaultDomain, nil
+}
+
+func formatSize(v int64) string {
+ if v < 1024 {
+ return fmt.Sprintf("%dB", v)
+ }
+ kb := float64(v) / 1024
+ if kb < 1024 {
+ return fmt.Sprintf("%.2fKB", kb)
+ }
+ mb := kb / 1024
+ if mb < 1024 {
+ return fmt.Sprintf("%.2fMB", mb)
+ }
+ gb := mb / 1024
+ return fmt.Sprintf("%.2fGB", gb)
+}
diff --git a/core/internal/service/mail_boxes/init_quota_plugin.go b/core/internal/service/mail_boxes/init_quota_plugin.go
new file mode 100644
index 00000000..1e54b4ac
--- /dev/null
+++ b/core/internal/service/mail_boxes/init_quota_plugin.go
@@ -0,0 +1,308 @@
+package mail_boxes
+
+import (
+ "billionmail-core/internal/consts"
+ "billionmail-core/internal/service/dockerapi"
+ "billionmail-core/internal/service/public"
+ "context"
+ "errors"
+ "fmt"
+ "github.com/gogf/gf/v2/frame/g"
+ "github.com/gogf/gf/v2/os/gfile"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "regexp"
+ "strings"
+ "time"
+)
+
+// Initialize quota plugin. If not installed, install it first.
+// Modify dovecot.conf, 20-pop3.conf, and 90-quota.conf.
+// Rebuild dovecot-sql.conf.ext
+// Check all mailboxes and add the maildirsize file (make sure the file permissions are correct)
+
+func InitQuotaPluginAndUpdateUsedSpace(ctx context.Context) error {
+ if public.HostWorkDir == "" {
+ return errors.New("HostWorkDir not set")
+ }
+
+ markPath := public.AbsPath("../core/data/quota_init_done.mark")
+
+ if gfile.Exists(markPath) {
+ return nil
+ }
+
+ confRoot := public.AbsPath("../conf/dovecot")
+
+ if _, err := os.Stat(confRoot); os.IsNotExist(err) {
+ return fmt.Errorf("dovecot conf dir not found: %s", confRoot)
+ }
+
+ // 1. Modify dovecot.conf
+ if err := ensureDovecotConf(confRoot); err != nil {
+ return err
+ }
+
+ // 2. Modify 20-pop3.conf
+ if err := ensurePop3Conf(confRoot); err != nil {
+ g.Log().Debug(ctx, " Modify 20-pop3.conf err:", err)
+ return err
+ }
+
+ // 3. /conf.d/90-quota.conf
+ if err := ensureQuotaConf(confRoot); err != nil {
+ g.Log().Debug(ctx, " Modify /conf.d/90-quota.conf err:", err)
+ return err
+ }
+
+ // 4. Rebuild dovecot-sql.conf.ext
+ if err := recreateSqlConf(confRoot); err != nil {
+ g.Log().Debug(ctx, " Rebuild dovecot-sql.conf.ext err:", err)
+ return err
+ }
+
+ // 5. maildirsize
+ if err := AddMaildirsizeFileForAllMailboxes(ctx); err != nil {
+ return err
+ }
+
+ // 6. mark & reload dovecot (best-effort)
+ if err := gfile.PutContents(markPath, fmt.Sprintf("First sync completed at %s", time.Now().Format("2006-01-02 15:04:05"))); err != nil {
+ g.Log().Warningf(ctx, "Failed to create the quota marker file: %v", err)
+ }
+
+ if err := reloadDovecot(ctx); err != nil {
+ g.Log().Warning(ctx, "reload dovecot failed", err)
+ }
+
+ g.Log().Info(ctx, "Quota plugin initialization completed")
+ return nil
+}
+
+func ensureDovecotConf(confRoot string) error {
+ path := filepath.Join(confRoot, "dovecot.conf")
+ data, err := ioutil.ReadFile(path)
+ if err != nil {
+ return fmt.Errorf("read dovecot.conf failed: %w", err)
+ }
+ content := string(data)
+
+ // Search for the "mail_plugins" line
+ mailPluginsRe := regexp.MustCompile(`(?m)^\s*mail_plugins\s*=.*$`)
+ if mailPluginsRe.MatchString(content) {
+ // If it exists but there is no quota, add a quota.
+ newContent := mailPluginsRe.ReplaceAllStringFunc(content, func(line string) string {
+ if strings.Contains(line, "quota") {
+ return line
+ }
+ return line + " quota"
+ })
+ content = newContent
+ } else {
+ // Insert before the first !include
+ includeIdx := strings.Index(content, "!include")
+ if includeIdx > -1 {
+ content = content[:includeIdx] + "mail_plugins = quota\n" + content[includeIdx:]
+ } else {
+ content = "mail_plugins = quota\n" + content
+ }
+ }
+ return ioutil.WriteFile(path, []byte(content), 0644)
+}
+
+func ensurePop3Conf(confRoot string) error {
+ path := filepath.Join(confRoot, "conf.d", "20-pop3.conf")
+
+ data, err := ioutil.ReadFile(path)
+ if os.IsNotExist(err) {
+ tpl := "protocol pop3 {\n mail_plugins = $mail_plugins\n}\n"
+ return ioutil.WriteFile(path, []byte(tpl), 0644)
+ }
+ if err != nil {
+ return err
+ }
+
+ content := string(data)
+
+ if !strings.Contains(content, "\n mail_plugins = $mail_plugins") &&
+ !strings.Contains(content, "\n\tmail_plugins = $mail_plugins") &&
+ !strings.Contains(content, "^mail_plugins = \\$mail_plugins$") {
+
+ content = strings.ReplaceAll(content, "#mail_plugins = $mail_plugins", " mail_plugins = $mail_plugins")
+
+ if !strings.Contains(content, "mail_plugins = $mail_plugins") {
+ content = strings.Replace(content, "protocol pop3 {", "protocol pop3 {\n mail_plugins = $mail_plugins", 1)
+ }
+ }
+
+ return ioutil.WriteFile(path, []byte(content), 0644)
+}
+
+func ensureQuotaConf(confRoot string) error {
+ path := filepath.Join(confRoot, "conf.d", "90-quota.conf")
+
+ if _, err := os.Stat(path); os.IsNotExist(err) {
+ return ioutil.WriteFile(path, []byte("plugin {\n quota = maildir\n}\n"), 0644)
+ }
+
+ data, err := ioutil.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ content := string(data)
+
+ if strings.Contains(content, "quota = maildir") &&
+ !strings.Contains(content, "#quota = maildir") &&
+ !strings.Contains(content, "/*quota = maildir") {
+ return nil // 已正确启用
+ }
+
+ if strings.Contains(content, "plugin {") && !strings.Contains(content, "quota = maildir") {
+ content = strings.Replace(content, "plugin {", "plugin {\n quota = maildir", 1)
+ } else {
+
+ content += "\nplugin {\n quota = maildir\n}\n"
+ }
+
+ return ioutil.WriteFile(path, []byte(content), 0644)
+}
+
+func recreateSqlConf(confRoot string) error {
+ path := filepath.Join(confRoot, "conf.d", "dovecot-sql.conf.ext")
+
+ //data, err := ioutil.ReadFile(path)
+ //if err != nil {
+ // return fmt.Errorf("read back dovecot-sql.conf.ext failed: %w", err)
+ //}
+
+ dbPass, _ := public.DockerEnv("DBPASS")
+ dbName, _ := public.DockerEnv("DBNAME")
+ dbUser, _ := public.DockerEnv("DBUSER")
+
+ content := fmt.Sprintf(`driver = pgsql
+connect = host=pgsql dbname=%s user=%s password=%s
+
+default_pass_scheme = MD5-CRYPT
+
+user_query = SELECT '/var/vmail/%%d/%%n' as home, 'maildir:/var/vmail/%%d/%%n' as mail, 150 AS uid, 8 AS gid, 'maildir:storage=' || quota AS quota FROM mailbox WHERE username = '%%u' AND active = 1
+
+password_query = SELECT username as user, password, '/var/vmail/%%d/%%n' as userdb_home, 'maildir:/var/vmail/%%d/%%n' as userdb_mail, 150 as userdb_uid, 8 as userdb_gid FROM mailbox WHERE username = '%%u' AND active = 1
+`, dbName, dbUser, dbPass)
+
+ err := ioutil.WriteFile(path, []byte(content), 0644)
+ if err != nil {
+ return fmt.Errorf("333333write new dovecot-sql.conf.ext failed: %w", err)
+ }
+
+ return nil
+}
+
+func AddMaildirsizeFileForAllMailboxes(ctx context.Context) error {
+
+ if _, err := g.DB().Exec(ctx, "ALTER TABLE mailbox ADD COLUMN IF NOT EXISTS quota_active SMALLINT NOT NULL DEFAULT 1"); err != nil {
+ g.Log().Warning(ctx, "ensure quota_active column failed", err)
+ }
+
+ type Row struct {
+ Username string
+ LocalPart string
+ Domain string
+ Quota int64
+ QuotaActive int
+ }
+ var rows []Row
+ if err := g.DB().Model("mailbox").Fields("username, local_part, domain, quota, COALESCE(quota_active,1) as quota_active").Scan(&rows); err != nil {
+
+ return err
+ }
+ if len(rows) == 0 {
+
+ return nil
+ }
+
+ vmailRoot := public.AbsPath("../vmail-data")
+
+ for _, r := range rows {
+ userDir := filepath.Join(vmailRoot, r.Domain, r.LocalPart)
+ maildirsizePath := filepath.Join(userDir, "maildirsize")
+
+ //if !public.IsDir(userDir) {
+ // continue
+ //}
+
+ if err := os.MkdirAll(userDir, 0755); err != nil {
+ g.Log().Warning(ctx, "create userDir failed", r.Username, err)
+ continue
+ }
+
+ // userDir (uid=150, gid=8)
+ if err := public.ChownDovecot(userDir); err != nil {
+ g.Log().Warning(ctx, "chown userDir failed", r.Username, err)
+
+ }
+
+ // maildirsize
+ if gfile.Exists(maildirsizePath) {
+
+ if err := public.ChownDovecot(userDir); err != nil {
+ g.Log().Warning(ctx, "chown maildirsize failed", r.Username, err)
+ }
+ //g.Log().Debug(ctx, "maildirsize already exists, skipped", r.Username, maildirsizePath)
+ continue
+ } else {
+ // not exist, create
+ firstLineQuota := int64(0)
+ if r.QuotaActive == 1 && r.Quota > 0 {
+ firstLineQuota = r.Quota
+ }
+ content := fmt.Sprintf("%dS\n0 0\n", firstLineQuota)
+
+ // 使用 gfile.PutContents(自动创建目录,原子写入)
+ if err := gfile.PutContents(maildirsizePath, content); err != nil {
+ g.Log().Warning(ctx, "write maildirsize failed", r.Username, err)
+ continue
+ }
+
+ if err := public.ChownDovecot(userDir); err != nil {
+ g.Log().Warning(ctx, "chown maildirsize failed after write", r.Username, err)
+ }
+ g.Log().Debug(ctx, "created maildirsize", r.Username, maildirsizePath)
+ }
+
+ }
+
+ dk, err := docker.NewDockerAPI()
+ if err != nil {
+ g.Log().Warning(ctx, "docker api init failed", err)
+ return err
+ }
+ defer dk.Close()
+ for _, r := range rows {
+ if r.QuotaActive == 1 && r.Quota > 0 {
+
+ cmd := []string{"doveadm", "quota", "recalc", "-u", r.Username}
+ _, err = dk.ExecCommandByName(context.Background(), consts.SERVICES.Dovecot, cmd, "root")
+
+ if err != nil {
+ g.Log().Warning(ctx, "doveadm recalc failed", r.Username, err)
+ }
+
+ }
+ }
+
+ return nil
+}
+
+func reloadDovecot(ctx context.Context) error {
+ dk, err := docker.NewDockerAPI()
+ if err != nil {
+ return err
+ }
+ defer dk.Close()
+ // Try systemctl reload, fallback to sending SIGHUP via doveadm
+ if _, err := dk.ExecCommandByName(ctx, consts.SERVICES.Dovecot, []string{"dovecot", "reload"}, "root"); err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/core/internal/service/mail_boxes/mail_boxes.go b/core/internal/service/mail_boxes/mail_boxes.go
index aa757473..6b1347e6 100644
--- a/core/internal/service/mail_boxes/mail_boxes.go
+++ b/core/internal/service/mail_boxes/mail_boxes.go
@@ -2,6 +2,9 @@ package mail_boxes
import (
v1 "billionmail-core/api/mail_boxes/v1"
+ "billionmail-core/internal/consts"
+ "billionmail-core/internal/service/dockerapi"
+ "billionmail-core/internal/service/public"
"context"
"encoding/base64"
"encoding/hex"
@@ -11,6 +14,8 @@ import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"math/rand"
+ "os"
+ "path/filepath"
"regexp"
"strings"
"time"
@@ -39,7 +44,14 @@ func Add(ctx context.Context, mailbox *v1.Mailbox) (err error) {
mailbox.Maildir = fmt.Sprintf("%s@%s/", mailbox.LocalPart, mailbox.Domain)
_, err = g.DB().Model("mailbox").Ctx(ctx).InsertIgnore(mailbox)
- return err
+ if err != nil {
+ return err
+ }
+ // maildirsize
+ if e2 := ensureMaildirAndQuotaFile(ctx, mailbox); e2 != nil {
+ g.Log().Warning(ctx, "ensureMaildirAndQuotaFile failed", e2)
+ }
+ return nil
}
func Update(ctx context.Context, mailbox *v1.Mailbox) (err error) {
@@ -61,12 +73,28 @@ func Update(ctx context.Context, mailbox *v1.Mailbox) (err error) {
m := gconv.Map(mailbox)
delete(m, "create_time")
+ delete(m, "used_quota")
+
+ var mb v1.Mailbox
+ err = g.DB().Model("mailbox").Where("username", mailbox.Username).Scan(&mb)
_, err = g.DB().Model("mailbox").
Ctx(ctx).
Where("username", mailbox.Username).
Update(m)
- return err
+ if err != nil {
+
+ return err
+ }
+
+ if mailbox.QuotaActive == 1 && mailbox.Quota != 0 {
+ if mailbox.Quota != mb.Quota {
+ if e2 := updateMaildirQuotaHeader(ctx, mailbox); e2 != nil {
+ g.Log().Warning(ctx, "updateMaildirQuotaHeader failed", e2)
+ }
+ }
+ }
+ return nil
}
func Delete(ctx context.Context, email string) error {
@@ -208,7 +236,7 @@ func generateRandomPassword(charset string, length int) string {
}
return string(password)
}
-func BatchAdd(ctx context.Context, domain string, quota int, count int, prefix string) ([]string, error) {
+func BatchAdd(ctx context.Context, domain string, quota int, count int, prefix string, quotaActive int) ([]string, error) {
if prefix == "" {
randomPre := make([]byte, 4)
@@ -235,9 +263,9 @@ func BatchAdd(ctx context.Context, domain string, quota int, count int, prefix s
//passwordEncoded := PasswdEncode(ctx, password)
//passwordCrypted, err := PasswdMD5Crypt(ctx, password)
- if err != nil {
- return nil, fmt.Errorf("Generate password md5-crypt failed: %w", err)
- }
+ //if err != nil {
+ // return nil, fmt.Errorf("Generate password md5-crypt failed: %w", err)
+ //}
mailboxes := make([]v1.Mailbox, 0, count)
emailList := make([]string, 0, count)
@@ -252,7 +280,7 @@ func BatchAdd(ctx context.Context, domain string, quota int, count int, prefix s
localPart := fmt.Sprintf("%s%d%s", prefix, i, string(randomChars))
username := localPart + "@" + domain
- password := generateRandomPassword("", 8) // todo
+ password := generateRandomPassword("", 8)
passwordEncoded := PasswdEncode(ctx, password)
passwordCrypted, _ := PasswdMD5Crypt(ctx, password)
@@ -270,6 +298,7 @@ func BatchAdd(ctx context.Context, domain string, quota int, count int, prefix s
UpdateTime: timestamp,
Active: 1,
Maildir: fmt.Sprintf("%s@%s/", localPart, domain),
+ QuotaActive: quotaActive,
}
mailboxes = append(mailboxes, mailbox)
@@ -306,6 +335,27 @@ func BatchAdd(ctx context.Context, domain string, quota int, count int, prefix s
}
+ // If the limit is enabled, asynchronous will initialize the maildir and maildirsize for batch-created email accounts, and limit the concurrency
+ if quotaActive != 1 || quota <= 0 {
+ return nil
+ }
+
+ go func(mbs []v1.Mailbox) {
+ sem := make(chan struct{}, 10)
+ for i := range mbs {
+ mb := mbs[i]
+ sem <- struct{}{}
+ go func(mb v1.Mailbox) {
+ defer func() { <-sem }()
+ _ = ensureMaildirAndQuotaFile(context.Background(), &mb)
+ }(mb)
+ }
+
+ for i := 0; i < cap(sem); i++ {
+ sem <- struct{}{}
+ }
+ }(mailboxes)
+
return nil
})
@@ -314,11 +364,11 @@ func BatchAdd(ctx context.Context, domain string, quota int, count int, prefix s
return nil, fmt.Errorf("Failed to create email: %w", err)
}
- if len(createdEmails) == 0 {
+ if len(emailList) == 0 {
return nil, fmt.Errorf("Failed to create any mailbox")
}
- return createdEmails, nil
+ return emailList, nil
}
// AddImport
@@ -351,6 +401,11 @@ func AddImport(ctx context.Context, mailbox *v1.Mailbox) (err error) {
}
mailbox.Username = strings.ToLower(mailbox.Username)
+ if mailbox.LocalPart == "" && mailbox.Username != "" {
+ if parts := strings.Split(mailbox.Username, "@"); len(parts) == 2 {
+ mailbox.LocalPart = parts[0]
+ }
+ }
mailbox.LocalPart = strings.ToLower(mailbox.LocalPart)
mailbox.Domain = strings.ToLower(mailbox.Domain)
@@ -358,10 +413,18 @@ func AddImport(ctx context.Context, mailbox *v1.Mailbox) (err error) {
mailbox.CreateTime = now
mailbox.UpdateTime = now
mailbox.Active = 1
+ mailbox.QuotaActive = 1
mailbox.Maildir = fmt.Sprintf("%s@%s/", mailbox.LocalPart, mailbox.Domain)
_, err = g.DB().Model("mailbox").Ctx(ctx).InsertIgnore(mailbox)
- return err
+ if err != nil {
+ return err
+ }
+
+ if e2 := ensureMaildirAndQuotaFile(ctx, mailbox); e2 != nil {
+ g.Log().Warning(ctx, "AddImport ensureMaildirAndQuotaFile failed", e2)
+ }
+ return nil
}
// NormalizeMailboxes normalizes mailbox usernames, local parts, domains, and maildirs to lowercase.
@@ -377,31 +440,110 @@ func NormalizeMailboxes() (err error) {
return
}
-// TestPasswordHandling 测试密码处理逻辑
-func TestPasswordHandling(ctx context.Context, email string) error {
- // 查询邮箱信息
- var mailbox v1.Mailbox
- err := g.DB().Model("mailbox").Where("username", email).Scan(&mailbox)
- if err != nil {
- return fmt.Errorf("Failed to query mailbox: %w", err)
+func maildirRoot(m *v1.Mailbox) string {
+ vmailRoot := public.AbsPath("../vmail-data")
+ return filepath.Join(vmailRoot, m.Domain, m.LocalPart)
+}
+
+func atomicWriteMaildirsize(path string, content []byte) error {
+ tmp := path + ".tmp"
+ if err := os.WriteFile(tmp, content, 0644); err != nil {
+ return err
}
+ _ = os.Chown(tmp, 150, 8)
+ return os.Rename(tmp, path)
+}
- // 尝试解密密码
- decodedPassword, err := PasswdDecode(ctx, mailbox.PasswordEncode)
- if err != nil {
- return fmt.Errorf("Failed to decode password: %w", err)
+func ensureMaildirAndQuotaFile(ctx context.Context, m *v1.Mailbox) error {
+ root := maildirRoot(m)
+ if err := os.MkdirAll(root, 0755); err != nil {
+ return err
}
- // 重新加密
- reEncryptedPassword, err := PasswdMD5Crypt(ctx, decodedPassword)
- if err != nil {
- return fmt.Errorf("Failed to re-encrypt password: %w", err)
+ fp := filepath.Join(root, "maildirsize")
+
+ quotaVal := int64(0)
+ if m.QuotaActive == 1 && m.Quota > 0 {
+ quotaVal = m.Quota
+ }
+ content := fmt.Sprintf("%dS\n0 0\n", quotaVal)
+ if _, err := os.Stat(fp); err == nil {
+
+ return updateMaildirQuotaHeader(ctx, m)
+ }
+ if err := atomicWriteMaildirsize(fp, []byte(content)); err != nil {
+
+ return err
}
+ recalcQuotaAsync(ctx, m.Username)
+ return nil
+}
- // 比较是否一致
- if mailbox.Password != reEncryptedPassword {
- return fmt.Errorf("Password mismatch: stored=%s, re-encrypted=%s", mailbox.Password, reEncryptedPassword)
+func updateMaildirQuotaHeader(ctx context.Context, m *v1.Mailbox) error {
+ root := maildirRoot(m)
+ if err := os.MkdirAll(root, 0755); err != nil {
+ return err
+ }
+ fp := filepath.Join(root, "maildirsize")
+ quotaVal := int64(0)
+ if m.QuotaActive == 1 && m.Quota > 0 {
+ quotaVal = m.Quota
}
+ header := fmt.Sprintf("%dS\n", quotaVal)
+ data, err := os.ReadFile(fp)
+ if err != nil {
+ if os.IsNotExist(err) {
+ if err2 := atomicWriteMaildirsize(fp, []byte(header+"0 0\n")); err2 != nil {
+ return err2
+ }
+ recalcQuotaAsync(ctx, m.Username)
+ return nil
+ }
+ return err
+ }
+ lines := strings.Split(string(data), "\n")
+ if len(lines) == 0 || (len(lines) == 1 && strings.TrimSpace(lines[0]) == "") {
+ if err2 := atomicWriteMaildirsize(fp, []byte(header+"0 0\n")); err2 != nil {
+ return err2
+ }
+ recalcQuotaAsync(ctx, m.Username)
+ return nil
+ }
+ usageLines := make([]string, 0, len(lines))
+ for i := 1; i < len(lines); i++ {
+ l := strings.TrimSpace(lines[i])
+ if l != "" {
+ // "<bytes> <count>"
+ parts := strings.Fields(l)
+ if len(parts) >= 1 {
+ usageLines = append(usageLines, l)
+ }
+ }
+ }
+ if len(usageLines) == 0 {
+ usageLines = append(usageLines, "0 0")
+ }
+ newContent := header + strings.Join(usageLines, "\n") + "\n"
+ if err := atomicWriteMaildirsize(fp, []byte(newContent)); err != nil {
+ return err
+ }
+ recalcQuotaAsync(ctx, m.Username)
return nil
}
+
+// doveadm quota recalc -u <user>
+func recalcQuotaAsync(ctx context.Context, username string) {
+ if username == "" {
+ return
+ }
+ go func(u string) {
+ dk, err := docker.NewDockerAPI()
+ if err != nil {
+ return
+ }
+ defer dk.Close()
+
+ _, _ = dk.ExecCommandByName(ctx, consts.SERVICES.Dovecot, []string{"doveadm", "quota", "recalc", "-u", u}, "root")
+ }(username)
+}
diff --git a/core/internal/service/mail_boxes/update_used_space.go b/core/internal/service/mail_boxes/update_used_space.go
new file mode 100644
index 00000000..504ceb68
--- /dev/null
+++ b/core/internal/service/mail_boxes/update_used_space.go
@@ -0,0 +1,267 @@
+package mail_boxes
+
+import (
+ "billionmail-core/internal/consts"
+ "billionmail-core/internal/service/dockerapi"
+ "billionmail-core/internal/service/public"
+ "bufio"
+ "context"
+ "fmt"
+ "github.com/gogf/gf/v2/frame/g"
+ "os"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "sync"
+ "sync/atomic"
+)
+
+var domainUsageColumnEnsured int32 // 0-Unverified, 1-Verified
+
+// -----------------------------
+// Scheduled check for update of domain email quota usage - used_quota
+// -----------------------------
+
+type mailboxUsage struct {
+ user string
+ used int64
+}
+
+func UpdateMailboxesUsedSpace() {
+ ctx := context.Background()
+
+ type row struct {
+ Username string
+ LocalPart string
+ Domain string
+ UsedQuota int64
+ }
+ var list []row
+ if err := g.DB().Model("mailbox").
+ Fields("username,local_part,domain,COALESCE(used_quota,0) used_quota").
+ Where("active=1 AND COALESCE(quota_active,1)=1").
+ Scan(&list); err != nil {
+ g.Log().Error(ctx, "query mailbox list failed", err)
+ return
+ }
+ if len(list) == 0 {
+
+ return
+ }
+
+ root := public.AbsPath("../vmail-data")
+
+ dk, _ := docker.NewDockerAPI()
+ defer func() {
+ if dk != nil {
+ dk.Close()
+ }
+ }()
+
+ workerCount := 8
+ if c := g.Cfg().MustGet(ctx, "quota.worker").Int(); c > 0 && c <= 64 {
+ workerCount = c
+ }
+
+ jobs := make(chan row, len(list))
+ var wg sync.WaitGroup
+ results := make(chan mailboxUsage, len(list))
+
+ // maildirsize
+ workerFn := func() {
+ defer wg.Done()
+ for mb := range jobs {
+ userDir := filepath.Join(root, mb.Domain, mb.LocalPart)
+
+ size, err := readMaildirsize(userDir)
+
+ if err != nil {
+ if dk != nil && (err == nil || err.Error() == "file not exists") {
+ if _, e2 := dk.ExecCommandByName(ctx, consts.SERVICES.Dovecot, []string{"doveadm", "quota", "recalc", "-u", mb.Username}, "root"); e2 == nil {
+ size, err = readMaildirsize(userDir)
+ }
+ }
+ }
+ if err != nil {
+ continue
+ }
+ results <- mailboxUsage{user: mb.Username, used: size}
+ }
+ }
+
+ wg.Add(workerCount)
+ for i := 0; i < workerCount; i++ {
+ go workerFn()
+ }
+
+ for _, mb := range list {
+ jobs <- mb
+ }
+ close(jobs)
+ wg.Wait()
+ close(results)
+
+ var updates []mailboxUsage
+ for r := range results {
+
+ var oldUsed int64
+ for _, mb := range list {
+ if mb.Username == r.user {
+ oldUsed = mb.UsedQuota
+ break
+ }
+ }
+ if r.used != oldUsed {
+ updates = append(updates, r)
+ }
+ }
+
+ if len(updates) == 0 {
+
+ return
+ }
+
+ updateInTransaction(ctx, updates)
+
+ aggregateDomainUsage(ctx)
+
+ //g.Log().Debug(ctx, "update completed", "scanned", len(list), "changed", len(updates), "elapsed", time.Since(start))
+}
+
+func updateInTransaction(ctx context.Context, updates []mailboxUsage) {
+ const batchSize = 300
+ successCount := 0
+ failCount := 0
+
+ for i := 0; i < len(updates); i += batchSize {
+ end := i + batchSize
+ if end > len(updates) {
+ end = len(updates)
+ }
+ batch := updates[i:end]
+
+ tx, err := g.DB().Begin(ctx)
+ if err != nil {
+
+ failCount += len(batch)
+ continue
+ }
+
+ batchSuccess := 0
+ batchFailed := 0
+
+ for _, u := range batch {
+ result, err := tx.Exec(
+ `UPDATE mailbox
+ SET used_quota = ?
+ WHERE username = ?`,
+ u.used, u.user,
+ )
+ if err != nil {
+ g.Log().Warning(ctx, "Single update failed", "user", u.user, "used", u.used, "err", err)
+ batchFailed++
+ } else {
+ affected, _ := result.RowsAffected()
+ if affected == 0 {
+ g.Log().Debug(ctx, "User not found: ", u.user)
+ }
+ batchSuccess++
+ }
+ }
+
+ if batchFailed > 0 {
+ tx.Rollback()
+ failCount += batchFailed
+ successCount += batchSuccess
+ } else {
+ if err := tx.Commit(); err != nil {
+
+ failCount += len(batch)
+ } else {
+ successCount += batchSuccess
+ }
+ }
+ }
+
+ //g.Log().Debug(ctx, "Batch update completed", "success", successCount, "failed", failCount)
+}
+
+func readMaildirsize(userDir string) (int64, error) {
+ path := filepath.Join(userDir, "maildirsize")
+
+ f, err := os.Open(path)
+ if err != nil {
+ return 0, fmt.Errorf("open maildirsize failed: %w", err)
+ }
+ defer f.Close()
+
+ scanner := bufio.NewScanner(f)
+ var total int64
+ var lineNo int
+
+ for scanner.Scan() {
+ lineNo++
+ line := strings.TrimSpace(scanner.Text())
+
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+
+ parts := strings.Fields(line)
+ if len(parts) == 0 {
+ continue
+ }
+
+ if lineNo == 1 {
+
+ continue
+ }
+
+ // <size> <mtime> 或 <size> <mtime> <flags>
+ sizeStr := parts[0]
+
+ sizeStr = strings.TrimSuffix(sizeStr, "S")
+
+ v, err := strconv.ParseInt(sizeStr, 10, 64)
+ if err != nil {
+ return 0, fmt.Errorf("parse maildirsize line %d: invalid size '%s': %w", lineNo, sizeStr, err)
+ }
+ total += v
+ }
+
+ if err := scanner.Err(); err != nil {
+ return 0, fmt.Errorf("scan maildirsize failed: %w", err)
+ }
+
+ return total, nil
+}
+
+// domain.current_usage
+func aggregateDomainUsage(ctx context.Context) {
+
+ if atomic.LoadInt32(&domainUsageColumnEnsured) == 0 {
+ if _, err := g.DB().Exec(ctx, "ALTER TABLE domain ADD COLUMN IF NOT EXISTS current_usage BIGINT NOT NULL DEFAULT 0"); err != nil {
+ g.Log().Warning(ctx, "ensure domain.current_usage column failed", err)
+ }
+ atomic.StoreInt32(&domainUsageColumnEnsured, 1)
+ }
+
+ type dRow struct {
+ Domain string
+ Usage int64
+ }
+ var rows []dRow
+ if err := g.DB().Model("mailbox").Fields("domain as domain, SUM(used_quota) as usage").Where("active=1").Group("domain").Scan(&rows); err != nil {
+ g.Log().Warning(ctx, "aggregate domain usage query failed", err)
+ return
+ }
+ if len(rows) == 0 {
+ return
+ }
+
+ for _, r := range rows {
+ if _, err := g.DB().Model("domain").Where("domain", r.Domain).Update(g.Map{"current_usage": r.Usage}); err != nil {
+ g.Log().Warning(ctx, "update domain current_usage failed", r.Domain, err)
+ }
+ }
+}
diff --git a/core/internal/service/mail_service/sending.go b/core/internal/service/mail_service/sending.go
index cee05060..bb75a0bc 100644
--- a/core/internal/service/mail_service/sending.go
+++ b/core/internal/service/mail_service/sending.go
@@ -1,11 +1,11 @@
package mail_service
import (
- "billionmail-core/internal/service/mail_boxes"
"billionmail-core/internal/service/public"
"bytes"
"context"
"crypto/tls"
+ "encoding/base64"
"encoding/hex"
"errors"
"fmt"
@@ -136,7 +136,7 @@ func NewEmailSenderWithLocal(email string) (es *EmailSender, err error) {
es.Email = email
es.UserName = email
- es.Password, err = mail_boxes.PasswordByEmail(context.Background(), email)
+ es.Password, err = PasswordPlainByEmail(context.Background(), email)
if err != nil {
return
@@ -150,6 +150,26 @@ func NewEmailSenderWithLocal(email string) (es *EmailSender, err error) {
return
}
+// PasswordPlainByEmail
+func PasswordPlainByEmail(ctx context.Context, email string) (string, error) {
+ val, err := g.DB().Model("mailbox").Where("username", email).Value("password_encode")
+ if err != nil {
+ return "", fmt.Errorf("query password failed: %w", err)
+ }
+ if val.IsEmpty() {
+ return "", fmt.Errorf("password not found for %s", email)
+ }
+ rawHex, err := hex.DecodeString(val.String())
+ if err != nil {
+ return "", fmt.Errorf("hex decode failed: %w", err)
+ }
+ plainBytes, err := base64.StdEncoding.DecodeString(string(rawHex))
+ if err != nil {
+ return "", fmt.Errorf("base64 decode failed: %w", err)
+ }
+ return string(plainBytes), nil
+}
+
// Close closes the SMTP connection
func (e *EmailSender) Close() {
_ = e.Disconnect()
diff --git a/core/internal/service/public/common.go b/core/internal/service/public/common.go
index 187457e2..d8baf3a3 100644
--- a/core/internal/service/public/common.go
+++ b/core/internal/service/public/common.go
@@ -2774,3 +2774,7 @@ func ValidateARecord(record v1.DNSRecord) bool {
return false
}
+
+func ChownDovecot(path string) error {
+ return os.Chown(path, 150, 8)
+}
diff --git a/core/internal/service/timers/timers.go b/core/internal/service/timers/timers.go
index a168bb53..0e2c1296 100644
--- a/core/internal/service/timers/timers.go
+++ b/core/internal/service/timers/timers.go
@@ -152,6 +152,33 @@ func Start(ctx context.Context) (err error) {
multi_ip_domain.ReapplyAllIptablesRules(ctx)
})
+ // Updated: Used space in the mailbox
+ gtimer.AddOnce(3*time.Second, func() {
+ mail_boxes.UpdateMailboxesUsedSpace()
+ })
+
+ gtimer.Add(60*time.Minute, func() {
+ mail_boxes.UpdateMailboxesUsedSpace()
+ })
+
+ // Check the email quota and the alert for exceeding the quota
+ gtimer.AddOnce(4*time.Second, func() {
+ mail_boxes.CheckMailboxesQuotaAlerts(ctx)
+ })
+
+ gtimer.Add(60*time.Minute, func() {
+ mail_boxes.CheckMailboxesQuotaAlerts(ctx)
+ })
+
+ // Initialize the quota plugin and update the quota usage status of the domain name and email
+ gtimer.AddOnce(3*time.Second, func() {
+ err := mail_boxes.InitQuotaPluginAndUpdateUsedSpace(ctx)
+ if err != nil {
+ g.Log().Warning(ctx, "Initialize the quota plugin and update the quota usage status of the domain name and email, failed: ", err)
+ err = nil
+ }
+ })
+
g.Log().Debug(ctx, "All timers started successfully")
return nil
}
diff --git a/docker-compose.yml b/docker-compose.yml
index 52cb8d94..7822bd02 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -193,6 +193,7 @@ services:
- ./logs/core:/opt/billionmail/core/logs
- ./core-data:/opt/billionmail/core/data
- /var/run/docker.sock:/var/run/docker.sock:ro
+ - ./vmail-data:/opt/billionmail/vmail-data
environment:
- TZ=${TZ}
- FAIL2BAN_INIT=${FAIL2BAN_INIT:-y}