You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

types.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package irc
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. //
  7. // simple types
  8. //
  9. // a string with wildcards
  10. type Mask string
  11. // add, remove, list modes
  12. type ModeOp rune
  13. // user mode flags
  14. type UserMode rune
  15. func (mode UserMode) String() string {
  16. return fmt.Sprintf("%c", mode)
  17. }
  18. type Phase uint
  19. type Numeric uint
  20. func (code Numeric) String() string {
  21. return fmt.Sprintf("%03d", code)
  22. }
  23. // channel mode flags
  24. type ChannelMode rune
  25. func (mode ChannelMode) String() string {
  26. return fmt.Sprintf("%c", mode)
  27. }
  28. // user-channel mode flags
  29. type UserChannelMode rune
  30. type ChannelNameMap map[string]*Channel
  31. func (channels ChannelNameMap) Add(channel *Channel) error {
  32. if channels[channel.name] != nil {
  33. return fmt.Errorf("%s: already set", channel.name)
  34. }
  35. channels[channel.name] = channel
  36. return nil
  37. }
  38. func (channels ChannelNameMap) Remove(channel *Channel) error {
  39. if channel != channels[channel.name] {
  40. return fmt.Errorf("%s: mismatch", channel.name)
  41. }
  42. delete(channels, channel.name)
  43. return nil
  44. }
  45. type ClientNameMap map[string]*Client
  46. var (
  47. ErrNickMissing = errors.New("nick missing")
  48. ErrNicknameInUse = errors.New("nickname in use")
  49. )
  50. func (clients ClientNameMap) Add(client *Client) error {
  51. if !client.HasNick() {
  52. return ErrNickMissing
  53. }
  54. if clients[client.nick] != nil {
  55. return ErrNicknameInUse
  56. }
  57. clients[client.nick] = client
  58. return nil
  59. }
  60. func (clients ClientNameMap) Remove(client *Client) error {
  61. if clients[client.nick] != client {
  62. return fmt.Errorf("%s: mismatch", client.nick)
  63. }
  64. delete(clients, client.nick)
  65. return nil
  66. }
  67. type ClientSet map[*Client]bool
  68. func (clients ClientSet) Add(client *Client) {
  69. clients[client] = true
  70. }
  71. func (clients ClientSet) Remove(client *Client) {
  72. delete(clients, client)
  73. }
  74. func (clients ClientSet) Has(client *Client) bool {
  75. return clients[client]
  76. }
  77. type ChannelSet map[*Channel]bool
  78. func (channels ChannelSet) Add(channel *Channel) {
  79. channels[channel] = true
  80. }
  81. func (channels ChannelSet) Remove(channel *Channel) {
  82. delete(channels, channel)
  83. }
  84. func (channels ChannelSet) First() *Channel {
  85. for channel := range channels {
  86. return channel
  87. }
  88. return nil
  89. }
  90. //
  91. // interfaces
  92. //
  93. type Identifier interface {
  94. Id() string
  95. Nick() string
  96. }
  97. type Replier interface {
  98. Reply(...Reply)
  99. }
  100. type Reply interface {
  101. Format(*Client) []string
  102. Source() Identifier
  103. }
  104. type Command interface {
  105. Name() string
  106. Client() *Client
  107. Source() Identifier
  108. Reply(Reply)
  109. }
  110. type ServerCommand interface {
  111. Command
  112. HandleServer(*Server)
  113. }
  114. type AuthServerCommand interface {
  115. Command
  116. HandleAuthServer(*Server)
  117. }
  118. type RegServerCommand interface {
  119. Command
  120. HandleRegServer(*Server)
  121. }
  122. type ChannelCommand interface {
  123. Command
  124. HandleChannel(channel *Channel)
  125. }
  126. //
  127. // structs
  128. //
  129. type UserMask struct {
  130. nickname Mask
  131. username Mask
  132. hostname Mask
  133. }
  134. func (mask *UserMask) String() string {
  135. return fmt.Sprintf("%s!%s@%s", mask.nickname, mask.username, mask.hostname)
  136. }