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.

client.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package irc
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. )
  7. const (
  8. LOGIN_TIMEOUT = time.Minute / 2 // how long the client has to login
  9. IDLE_TIMEOUT = time.Minute // how long before a client is considered idle
  10. QUIT_TIMEOUT = time.Minute // how long after idle before a client is kicked
  11. )
  12. type Client struct {
  13. atime time.Time
  14. authorized bool
  15. awayMessage Text
  16. capabilities CapabilitySet
  17. capState CapState
  18. channels ChannelSet
  19. commands chan Command
  20. ctime time.Time
  21. flags map[UserMode]bool
  22. hasQuit bool
  23. hops uint
  24. hostname Name
  25. idleTimer *time.Timer
  26. loginTimer *time.Timer
  27. nick Name
  28. quitTimer *time.Timer
  29. realname Text
  30. registered bool
  31. server *Server
  32. socket *Socket
  33. username Name
  34. }
  35. func NewClient(server *Server, conn net.Conn) *Client {
  36. now := time.Now()
  37. client := &Client{
  38. atime: now,
  39. authorized: server.password == nil,
  40. capState: CapNone,
  41. capabilities: make(CapabilitySet),
  42. channels: make(ChannelSet),
  43. commands: make(chan Command),
  44. ctime: now,
  45. flags: make(map[UserMode]bool),
  46. server: server,
  47. }
  48. client.socket = NewSocket(conn, client.commands)
  49. client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.connectionTimeout)
  50. go client.run()
  51. return client
  52. }
  53. //
  54. // command goroutine
  55. //
  56. func (client *Client) run() {
  57. for command := range client.commands {
  58. if checkPass, ok := command.(checkPasswordCommand); ok {
  59. checkPass.LoadPassword(client.server)
  60. // Block the client thread while handling a potentially expensive
  61. // password bcrypt operation. Since the server is single-threaded
  62. // for commands, we don't want the server to perform the bcrypt,
  63. // blocking anyone else from sending commands until it
  64. // completes. This could be a form of DoS if handled naively.
  65. checkPass.CheckPassword()
  66. }
  67. command.SetClient(client)
  68. client.server.commands <- command
  69. }
  70. }
  71. func (client *Client) connectionTimeout() {
  72. client.commands <- NewQuitCommand("connection timeout")
  73. }
  74. //
  75. // idle timer goroutine
  76. //
  77. func (client *Client) connectionIdle() {
  78. client.server.idle <- client
  79. }
  80. //
  81. // server goroutine
  82. //
  83. func (client *Client) Active() {
  84. client.atime = time.Now()
  85. }
  86. func (client *Client) Touch() {
  87. if client.quitTimer != nil {
  88. client.quitTimer.Stop()
  89. }
  90. if client.idleTimer == nil {
  91. client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.connectionIdle)
  92. } else {
  93. client.idleTimer.Reset(IDLE_TIMEOUT)
  94. }
  95. }
  96. func (client *Client) Idle() {
  97. client.Reply(RplPing(client))
  98. if client.quitTimer == nil {
  99. client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
  100. } else {
  101. client.quitTimer.Reset(QUIT_TIMEOUT)
  102. }
  103. }
  104. func (client *Client) Register() {
  105. if client.registered {
  106. return
  107. }
  108. client.registered = true
  109. client.loginTimer.Stop()
  110. client.Touch()
  111. }
  112. func (client *Client) destroy() {
  113. // clean up channels
  114. for channel := range client.channels {
  115. channel.Quit(client)
  116. }
  117. // clean up server
  118. client.server.clients.Remove(client)
  119. // clean up self
  120. if client.loginTimer != nil {
  121. client.loginTimer.Stop()
  122. }
  123. if client.idleTimer != nil {
  124. client.idleTimer.Stop()
  125. }
  126. if client.quitTimer != nil {
  127. client.quitTimer.Stop()
  128. }
  129. client.socket.Close()
  130. Log.debug.Printf("%s: destroyed", client)
  131. }
  132. func (client *Client) IdleTime() time.Duration {
  133. return time.Since(client.atime)
  134. }
  135. func (client *Client) SignonTime() int64 {
  136. return client.ctime.Unix()
  137. }
  138. func (client *Client) IdleSeconds() uint64 {
  139. return uint64(client.IdleTime().Seconds())
  140. }
  141. func (client *Client) HasNick() bool {
  142. return client.nick != ""
  143. }
  144. func (client *Client) HasUsername() bool {
  145. return client.username != ""
  146. }
  147. // <mode>
  148. func (c *Client) ModeString() (str string) {
  149. for flag := range c.flags {
  150. str += flag.String()
  151. }
  152. if len(str) > 0 {
  153. str = "+" + str
  154. }
  155. return
  156. }
  157. func (c *Client) UserHost() Name {
  158. username := "*"
  159. if c.HasUsername() {
  160. username = c.username.String()
  161. }
  162. return Name(fmt.Sprintf("%s!%s@%s", c.Nick(), username, c.hostname))
  163. }
  164. func (c *Client) Nick() Name {
  165. if c.HasNick() {
  166. return c.nick
  167. }
  168. return Name("*")
  169. }
  170. func (c *Client) Id() Name {
  171. return c.UserHost()
  172. }
  173. func (c *Client) String() string {
  174. return c.Id().String()
  175. }
  176. func (client *Client) Friends() ClientSet {
  177. friends := make(ClientSet)
  178. friends.Add(client)
  179. for channel := range client.channels {
  180. for member := range channel.members {
  181. friends.Add(member)
  182. }
  183. }
  184. return friends
  185. }
  186. func (client *Client) SetNickname(nickname Name) {
  187. if client.HasNick() {
  188. Log.error.Printf("%s nickname already set!", client)
  189. return
  190. }
  191. client.nick = nickname
  192. client.server.clients.Add(client)
  193. }
  194. func (client *Client) ChangeNickname(nickname Name) {
  195. // Make reply before changing nick to capture original source id.
  196. reply := RplNick(client, nickname)
  197. client.server.clients.Remove(client)
  198. client.server.whoWas.Append(client)
  199. client.nick = nickname
  200. client.server.clients.Add(client)
  201. for friend := range client.Friends() {
  202. friend.Reply(reply)
  203. }
  204. }
  205. func (client *Client) Reply(reply string, args ...interface{}) {
  206. if len(args) > 0 {
  207. reply = fmt.Sprintf(reply, args...)
  208. }
  209. client.socket.Write(reply)
  210. }
  211. func (client *Client) Quit(message Text) {
  212. if client.hasQuit {
  213. return
  214. }
  215. client.hasQuit = true
  216. client.Reply(RplError("quit"))
  217. client.server.whoWas.Append(client)
  218. friends := client.Friends()
  219. friends.Remove(client)
  220. client.destroy()
  221. if len(friends) > 0 {
  222. reply := RplQuit(client, message)
  223. for friend := range friends {
  224. friend.Reply(reply)
  225. }
  226. }
  227. }