1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package logic

import (
	"bufio"
	"os"
	"strings"
)

// isRequesterAllowed reports whether the given fediverse moniker (@user@host)
// is permitted to request new feeds.
//
// Policy:
//   - If restrict is false, everyone is allowed (allowlisting disabled).
//   - If restrict is true, the request is closed by default: only monikers
//     listed in allowedFile (one per line) are allowed. An unreadable or empty
//     list therefore denies everyone.
//
// Matching is case-insensitive. Blank lines and lines starting with '#' are
// ignored. A leading '@' on list entries is optional.
func isRequesterAllowed(restrict bool, allowedFile, moniker string) bool {
	if !restrict {
		return true
	}
	f, err := os.Open(allowedFile)
	if err != nil {
		// Restriction is on but the list is unreadable → deny (closed).
		return false
	}
	defer f.Close()

	want := normaliseMoniker(moniker)
	sc := bufio.NewScanner(f)
	sc.Split(bufio.ScanLines)
	for sc.Scan() {
		line := strings.TrimSpace(sc.Text())
		if line == "" || strings.HasPrefix(line, "#") {
			continue
		}
		if normaliseMoniker(line) == want {
			return true
		}
	}
	// Restriction is on and the requester was not listed → deny (closed).
	return false
}

func normaliseMoniker(s string) string {
	s = strings.ToLower(strings.TrimSpace(s))
	s = strings.TrimPrefix(s, "@")
	return s
}