Separate original and target account choices

This commit is contained in:
DonVoo 2026-07-06 00:02:44 +02:00
parent f455da54af
commit 167ba9577d

View file

@ -283,6 +283,7 @@ func findForwardedCopy(account, targetAccount string, raw []byte) (forwardedMatc
if id == "" { if id == "" {
return forwardedMatch{}, false return forwardedMatch{}, false
} }
allowedTargets := targetMboxDirs(targetAccount)
entries, err := os.ReadDir(Cfg.MboxRoot) entries, err := os.ReadDir(Cfg.MboxRoot)
if err != nil { if err != nil {
return forwardedMatch{}, false return forwardedMatch{}, false
@ -291,7 +292,7 @@ func findForwardedCopy(account, targetAccount string, raw []byte) (forwardedMatc
if !entry.IsDir() || entry.Name() == account { if !entry.IsDir() || entry.Name() == account {
continue continue
} }
if targetAccount != "" && entry.Name() != targetAccount { if targetAccount != "" && !allowedTargets[entry.Name()] {
continue continue
} }
mboxes, _ := filepath.Glob(filepath.Join(Cfg.MboxRoot, entry.Name(), "*.mbox")) mboxes, _ := filepath.Glob(filepath.Join(Cfg.MboxRoot, entry.Name(), "*.mbox"))
@ -328,10 +329,7 @@ func messageIDHeader(raw []byte) string {
} }
func renderTargetAccountSelect(account, folder, selected string, index int) string { func renderTargetAccountSelect(account, folder, selected string, index int) string {
accounts := viewerAccounts() options := targetAccountOptions()
if selected == account {
selected = ""
}
var b strings.Builder var b strings.Builder
fmt.Fprintf(&b, `<div class="pane-head target-head"><span>Zielkopie</span><form class="target-select-form"`) fmt.Fprintf(&b, `<div class="pane-head target-head"><span>Zielkopie</span><form class="target-select-form"`)
if account != "" && folder != "" && index >= 0 { if account != "" && folder != "" && index >= 0 {
@ -343,15 +341,12 @@ func renderTargetAccountSelect(account, folder, selected string, index int) stri
<input type="hidden" name="index" value="%d"> <input type="hidden" name="index" value="%d">
<select id="target-account-select" name="target_account" class="target-select"> <select id="target-account-select" name="target_account" class="target-select">
<option value="">Automatisch</option>`, html.EscapeString(account), html.EscapeString(folder), index) <option value="">Automatisch</option>`, html.EscapeString(account), html.EscapeString(folder), index)
for _, a := range accounts { for _, opt := range options {
if a.Name == account {
continue
}
attr := "" attr := ""
if a.Name == selected { if opt.Value == selected {
attr = ` selected` attr = ` selected`
} }
fmt.Fprintf(&b, `<option value="%s"%s>%s</option>`, html.EscapeString(a.Name), attr, html.EscapeString(a.TargetLabel)) fmt.Fprintf(&b, `<option value="%s"%s>%s</option>`, html.EscapeString(opt.Value), attr, html.EscapeString(opt.Label))
} }
b.WriteString(`</select></form></div>`) b.WriteString(`</select></form></div>`)
return b.String() return b.String()
@ -360,7 +355,11 @@ func renderTargetAccountSelect(account, folder, selected string, index int) stri
type viewerAccount struct { type viewerAccount struct {
Name string Name string
SourceLabel string SourceLabel string
TargetLabel string }
type targetAccountOption struct {
Value string
Label string
} }
func viewerAccounts() []viewerAccount { func viewerAccounts() []viewerAccount {
@ -380,7 +379,7 @@ func viewerAccounts() []viewerAccount {
} }
a := labels[entry.Name()] a := labels[entry.Name()]
if a.Name == "" { if a.Name == "" {
a = viewerAccount{Name: entry.Name(), SourceLabel: entry.Name(), TargetLabel: entry.Name()} a = viewerAccount{Name: entry.Name(), SourceLabel: entry.Name()}
} }
out = append(out, a) out = append(out, a)
} }
@ -409,14 +408,45 @@ func accountLabels() map[string]viewerAccount {
if source == "" { if source == "" {
source = a.Name source = a.Name
} }
target := a.DstUser out[dir] = viewerAccount{Name: dir, SourceLabel: source}
if target == "" {
target = a.Name
} }
if a.Name != "" { return out
target = target + " (" + a.Name + ")" }
func targetAccountOptions() []targetAccountOption {
if DB == nil {
return nil
}
accounts, err := ListAccounts()
if err != nil {
return nil
}
seen := map[string]bool{}
var out []targetAccountOption
for _, a := range accounts {
target := strings.TrimSpace(a.DstUser)
if target == "" || seen[target] {
continue
}
seen[target] = true
out = append(out, targetAccountOption{Value: target, Label: target})
}
return out
}
func targetMboxDirs(targetUser string) map[string]bool {
out := map[string]bool{}
if targetUser == "" || DB == nil {
return out
}
accounts, err := ListAccounts()
if err != nil {
return out
}
for _, a := range accounts {
if strings.EqualFold(strings.TrimSpace(a.DstUser), targetUser) {
out[accountMboxDir(a)] = true
} }
out[dir] = viewerAccount{Name: dir, SourceLabel: source, TargetLabel: target}
} }
return out return out
} }