package webui import ( "net/http" "strconv" "strings" ) var validConditionFields = map[string]bool{"from": true, "to": true, "subject": true} var validConditionOps = map[string]bool{"contains": true, "equals": true, "starts_with": true} var validActions = map[string]bool{"move_to_folder": true, "delete": true, "mark_read": true} func (a *App) rulesList(w http.ResponseWriter, r *http.Request) { mailbox, ok := a.mailboxWithAccess(w, r) if !ok { return } rules, err := a.DB.ListRulesForMailbox(mailbox.ID) if err != nil { setFlash(w, "error", "Error loading rules") } a.render(w, r, "mailbox_rules.html", M{"active": "mailboxes", "mailbox": mailbox, "rules": rules}) } // addRule mirrors the compact list-CRUD pattern used elsewhere: no separate edit // page, just add/remove — a rule needing changes is removed and re-added. func (a *App) addRule(w http.ResponseWriter, r *http.Request) { mailbox, ok := a.mailboxWithAccess(w, r) if !ok { return } priority, _ := strconv.Atoi(r.FormValue("priority")) field := r.FormValue("condition_field") op := r.FormValue("condition_op") value := strings.TrimSpace(r.FormValue("condition_value")) action := r.FormValue("action") actionValue := strings.TrimSpace(r.FormValue("action_value")) if !validConditionFields[field] || !validConditionOps[op] || value == "" || !validActions[action] { setFlash(w, "error", "Please fill in a valid condition and action") http.Redirect(w, r, Prefix+"/mailboxes/"+idStr(r)+"/rules", http.StatusFound) return } if action == "move_to_folder" && actionValue == "" { setFlash(w, "error", "Please name the folder to move matching mail into") http.Redirect(w, r, Prefix+"/mailboxes/"+idStr(r)+"/rules", http.StatusFound) return } if _, err := a.DB.CreateRule(mailbox.ID, priority, field, op, value, action, actionValue); err != nil { setFlash(w, "error", "Error creating rule") } else { setFlash(w, "success", "Rule added") } http.Redirect(w, r, Prefix+"/mailboxes/"+idStr(r)+"/rules", http.StatusFound) } func (a *App) removeRule(w http.ResponseWriter, r *http.Request) { mailbox, ok := a.mailboxWithAccess(w, r) if !ok { return } ruleID := int64(atoi(r.PathValue("rule_id"))) if err := a.DB.RemoveRule(ruleID, mailbox.ID); err != nil { setFlash(w, "error", "Error removing rule") } else { setFlash(w, "success", "Rule removed") } http.Redirect(w, r, Prefix+"/mailboxes/"+idStr(r)+"/rules", http.StatusFound) }