commit a87210a1926cf2c2c216d1121d2bba84e1e0840f from: Oliver Lowe date: Thu Apr 14 01:09:14 2022 UTC exclude known derogatory words from usernames commit - 63d6f8291650351e5067cd50373f3f6d202b65df commit + a87210a1926cf2c2c216d1121d2bba84e1e0840f blob - 7a3c4393c7ed79653bd98da9527be675ab1e44d5 blob + 31ae18d54a51af4ab1cf4d13fad88ec1a394e0ab --- random.go +++ random.go @@ -46,6 +46,18 @@ func randomLine(f *os.File) (string, error) { return line, nil } +// /usr/share/dict/words contains derogatory words +// which we don't think should be used in public usernames. +func derogatory(s string) bool { + if s == "bitch" { + return true + } + if strings.HasPrefix(s, "nigger") { + return true + } + return false +} + // RandomUsername generates a random username from the dictionary file at dictpath. // The dictionary file should be a newline delimited text file, one word per line. // On Unix systems, the file /usr/share/dic/words is a suitable file. @@ -56,16 +68,26 @@ func RandomUsername(dictpath string) (string, error) { } defer f.Close() - first, err := randomLine(f) - if err != nil { - return "", fmt.Errorf("first random word: %w", err) + var first, second string + for { + first, err = randomLine(f) + if err != nil { + return "", fmt.Errorf("first random word: %w", err) + } + first = strings.ToLower(first) + if !derogatory(first) { + break + } } - first = strings.ToLower(first) - second, err := randomLine(f) - if err != nil { - return "", fmt.Errorf("second random word: %w", err) + for { + second, err = randomLine(f) + if err != nil { + return "", fmt.Errorf("second random word: %w", err) + } + second = strings.ToLower(second) + if !derogatory(second) { + break + } } - second = strings.ToLower(second) - return fmt.Sprintf("%s_%s%02d", first, second, rand.Intn(99)), nil }