Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package irc
  2. import (
  3. "regexp"
  4. "strings"
  5. "golang.org/x/text/unicode/norm"
  6. )
  7. var (
  8. // regexps
  9. ChannelNameExpr = regexp.MustCompile(`^[&!#+][\pL\pN]{1,63}$`)
  10. NicknameExpr = regexp.MustCompile("^[\\pL\\pN\\pP\\pS]{1,32}$")
  11. )
  12. // Names are normalized and canonicalized to remove formatting marks
  13. // and simplify usage. They are things like hostnames and usermasks.
  14. type Name string
  15. func NewName(str string) Name {
  16. return Name(norm.NFKC.String(str))
  17. }
  18. func NewNames(strs []string) []Name {
  19. names := make([]Name, len(strs))
  20. for index, str := range strs {
  21. names[index] = NewName(str)
  22. }
  23. return names
  24. }
  25. // tests
  26. func (name Name) IsChannel() bool {
  27. return ChannelNameExpr.MatchString(name.String())
  28. }
  29. func (name Name) IsNickname() bool {
  30. namestr := name.String()
  31. // * is used for unregistered clients
  32. // , is used as a separator by the protocol
  33. // # is a channel prefix
  34. // @+ are channel membership prefixes
  35. if namestr == "*" || strings.Contains(namestr, ",") || strings.Contains("#@+", string(namestr[0])) {
  36. return false
  37. }
  38. return NicknameExpr.MatchString(namestr)
  39. }
  40. // conversions
  41. func (name Name) String() string {
  42. return string(name)
  43. }
  44. func (name Name) ToLower() Name {
  45. return Name(strings.ToLower(name.String()))
  46. }
  47. // It's safe to coerce a Name to Text. Name is a strict subset of Text.
  48. func (name Name) Text() Text {
  49. return Text(name)
  50. }
  51. // Text is PRIVMSG, NOTICE, or TOPIC data. It's canonicalized UTF8
  52. // data to simplify but keeps all formatting.
  53. type Text string
  54. func NewText(str string) Text {
  55. return Text(norm.NFC.String(str))
  56. }
  57. func (text Text) String() string {
  58. return string(text)
  59. }
  60. // CTCPText is text suitably escaped for CTCP.
  61. type CTCPText string
  62. var ctcpEscaper = strings.NewReplacer("\x00", "\x200", "\n", "\x20n", "\r", "\x20r")
  63. func NewCTCPText(str string) CTCPText {
  64. return CTCPText(ctcpEscaper.Replace(str))
  65. }