-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusernames.go
More file actions
169 lines (140 loc) · 4.32 KB
/
Copy pathusernames.go
File metadata and controls
169 lines (140 loc) · 4.32 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
// LoadUsernamesFromFile loads a list of usernames from a file
// Each username should be on a separate line
func LoadUsernamesFromFile(filePath string) ([]string, error) {
var usernames []string
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Skip empty lines and comments
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Remove @ symbol if present
line = strings.TrimPrefix(line, "@")
// Normalize the username by removing spaces and converting to lowercase
normalized := normalizeName(line)
// Only add if it's a valid username
if isValidUsername(normalized) {
usernames = append(usernames, normalized)
} else {
log.Printf("WARNING: Invalid username in file: %s", line)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return usernames, nil
}
// SaveUsernameToFile adds a username to the usernames file
func SaveUsernameToFile(filePath string, username string) error {
// Remove @ symbol if present
username = strings.TrimPrefix(username, "@")
// Normalize the username
normalized := normalizeName(username)
// Check if it's valid
if !isValidUsername(normalized) {
return fmt.Errorf("invalid username format: %s", username)
}
// Check if the file exists
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
// Create file with header
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create usernames file: %v", err)
}
defer file.Close()
file.WriteString("# List of usernames to check for similarity\n")
file.WriteString("# Each line is a username, lines starting with # are comments\n\n")
}
// Check if username already exists in the file
existingUsernames, err := LoadUsernamesFromFile(filePath)
if err != nil {
return fmt.Errorf("failed to load existing usernames: %v", err)
}
for _, existing := range existingUsernames {
if existing == normalized {
return fmt.Errorf("username '%s' already exists in the list", username)
}
}
// Append the new username to the file
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open usernames file for appending: %v", err)
}
defer file.Close()
_, err = file.WriteString(normalized + "\n")
if err != nil {
return fmt.Errorf("failed to write username to file: %v", err)
}
log.Printf("Added username '%s' to protected list in %s", normalized, filePath)
return nil
}
// RemoveUsernameFromFile removes a username from the usernames file
func RemoveUsernameFromFile(filePath string, username string) error {
// Remove @ symbol if present
username = strings.TrimPrefix(username, "@")
// Normalize the username
normalized := normalizeName(username)
// Load existing usernames
existingUsernames, err := LoadUsernamesFromFile(filePath)
if err != nil {
return fmt.Errorf("failed to load existing usernames: %v", err)
}
// Check if username exists in the list
found := false
for _, existing := range existingUsernames {
if existing == normalized {
found = true
break
}
}
if !found {
return fmt.Errorf("username '%s' not found in the protected list", username)
}
// Read the entire file
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open file: %v", err)
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
trimmed := strings.TrimSpace(line)
if trimmed == "" || strings.HasPrefix(trimmed, "#") {
// Keep comments and empty lines
lines = append(lines, line)
} else {
// Check if this is the username we want to remove
lineNormalized := normalizeName(strings.TrimPrefix(trimmed, "@"))
if lineNormalized != normalized {
// Keep all other usernames
lines = append(lines, line)
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading file: %v", err)
}
// Write the modified contents back to the file
if err := os.WriteFile(filePath, []byte(strings.Join(lines, "\n")+"\n"), 0644); err != nil {
return fmt.Errorf("failed to write updated file: %v", err)
}
log.Printf("Removed username '%s' from protected list in %s", normalized, filePath)
return nil
}