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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. command.SetClient(client)
  59. checkPass, ok := command.(checkPasswordCommand)
  60. if ok {
  61. checkPass.LoadPassword(client.server)
  62. // Block the client thread while handling a potentially expensive
  63. // password bcrypt operation. Since the server is single-threaded
  64. // for commands, we don't want the server to perform the bcrypt,
  65. // blocking anyone else from sending commands until it
  66. // completes. This could be a form of DoS if handled naively.
  67. checkPass.CheckPassword()
  68. }
  69. client.server.commands <- command
  70. }
  71. }
  72. func (client *Client) connectionTimeout() {
  73. client.commands <- &QuitCommand{
  74. message: "connection timeout",
  75. }
  76. }
  77. //
  78. // idle timer goroutine
  79. //
  80. func (client *Client) connectionIdle() {
  81. client.server.idle <- client
  82. }
  83. //
  84. // server goroutine
  85. //
  86. func (client *Client) Active() {
  87. client.atime = time.Now()
  88. }
  89. func (client *Client) Touch() {
  90. if client.quitTimer != nil {
  91. client.quitTimer.Stop()
  92. }
  93. if client.idleTimer == nil {
  94. client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.connectionIdle)
  95. } else {
  96. client.idleTimer.Reset(IDLE_TIMEOUT)
  97. }
  98. }
  99. func (client *Client) Idle() {
  100. client.Reply(RplPing(client))
  101. if client.quitTimer == nil {
  102. client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
  103. } else {
  104. client.quitTimer.Reset(QUIT_TIMEOUT)
  105. }
  106. }
  107. func (client *Client) Register() {
  108. if client.registered {
  109. return
  110. }
  111. client.registered = true
  112. client.loginTimer.Stop()
  113. client.Touch()
  114. }
  115. func (client *Client) destroy() {
  116. // clean up channels
  117. for channel := range client.channels {
  118. channel.Quit(client)
  119. }
  120. // clean up server
  121. client.server.clients.Remove(client)
  122. // clean up self
  123. if client.loginTimer != nil {
  124. client.loginTimer.Stop()
  125. }
  126. if client.idleTimer != nil {
  127. client.idleTimer.Stop()
  128. }
  129. if client.quitTimer != nil {
  130. client.quitTimer.Stop()
  131. }
  132. client.socket.Close()
  133. Log.debug.Printf("%s: destroyed", client)
  134. }
  135. func (client *Client) IdleTime() time.Duration {
  136. return time.Since(client.atime)
  137. }
  138. func (client *Client) SignonTime() int64 {
  139. return client.ctime.Unix()
  140. }
  141. func (client *Client) IdleSeconds() uint64 {
  142. return uint64(client.IdleTime().Seconds())
  143. }
  144. func (client *Client) HasNick() bool {
  145. return client.nick != ""
  146. }
  147. func (client *Client) HasUsername() bool {
  148. return client.username != ""
  149. }
  150. // <mode>
  151. func (c *Client) ModeString() (str string) {
  152. for flag := range c.flags {
  153. str += flag.String()
  154. }
  155. if len(str) > 0 {
  156. str = "+" + str
  157. }
  158. return
  159. }
  160. func (c *Client) UserHost() Name {
  161. username := "*"
  162. if c.HasUsername() {
  163. username = c.username.String()
  164. }
  165. return Name(fmt.Sprintf("%s!%s@%s", c.Nick(), username, c.hostname))
  166. }
  167. func (c *Client) Nick() Name {
  168. if c.HasNick() {
  169. return c.nick
  170. }
  171. return Name("*")
  172. }
  173. func (c *Client) Id() Name {
  174. return c.UserHost()
  175. }
  176. func (c *Client) String() string {
  177. return c.Id().String()
  178. }
  179. func (client *Client) Friends() ClientSet {
  180. friends := make(ClientSet)
  181. friends.Add(client)
  182. for channel := range client.channels {
  183. for member := range channel.members {
  184. friends.Add(member)
  185. }
  186. }
  187. return friends
  188. }
  189. func (client *Client) SetNickname(nickname Name) {
  190. if client.HasNick() {
  191. Log.error.Printf("%s nickname already set!", client)
  192. return
  193. }
  194. client.nick = nickname
  195. client.server.clients.Add(client)
  196. }
  197. func (client *Client) ChangeNickname(nickname Name) {
  198. // Make reply before changing nick to capture original source id.
  199. reply := RplNick(client, nickname)
  200. client.server.clients.Remove(client)
  201. client.server.whoWas.Append(client)
  202. client.nick = nickname
  203. client.server.clients.Add(client)
  204. for friend := range client.Friends() {
  205. friend.Reply(reply)
  206. }
  207. }
  208. func (client *Client) Reply(reply string, args ...interface{}) {
  209. if len(args) > 0 {
  210. reply = fmt.Sprintf(reply, args...)
  211. }
  212. client.socket.Write(reply)
  213. }
  214. func (client *Client) Quit(message Text) {
  215. if client.hasQuit {
  216. return
  217. }
  218. client.Reply(RplError("connection closed"))
  219. client.hasQuit = true
  220. client.server.whoWas.Append(client)
  221. friends := client.Friends()
  222. friends.Remove(client)
  223. client.destroy()
  224. if len(friends) > 0 {
  225. reply := RplQuit(client, message)
  226. for friend := range friends {
  227. friend.Reply(reply)
  228. }
  229. }
  230. }