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 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package irc
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. type Client struct {
  7. conn net.Conn
  8. hostname string
  9. send chan<- Reply
  10. recv <-chan string
  11. username string
  12. realname string
  13. nick string
  14. registered bool
  15. invisible bool
  16. channels ChannelSet
  17. }
  18. type ClientSet map[*Client]bool
  19. func NewClient(conn net.Conn) *Client {
  20. client := &Client{conn: conn, recv: StringReadChan(conn), channels: make(ChannelSet), hostname: LookupHostname(conn.RemoteAddr())}
  21. client.SetReplyToStringChan()
  22. return client
  23. }
  24. func (c *Client) SetReplyToStringChan() {
  25. send := make(chan Reply)
  26. write := StringWriteChan(c.conn)
  27. go func() {
  28. for reply := range send {
  29. write <- reply.String(c)
  30. }
  31. }()
  32. c.send = send
  33. }
  34. // Adapt `chan string` to a `chan Message`.
  35. func (c *Client) Communicate(server *Server) {
  36. for str := range c.recv {
  37. m := ParseMessage(str)
  38. if m != nil {
  39. server.recv <- &ClientMessage{c, m}
  40. }
  41. }
  42. }
  43. func (c *Client) Nick() string {
  44. if c.nick != "" {
  45. return c.nick
  46. }
  47. return "*"
  48. }
  49. func (c *Client) UModeString() string {
  50. if c.invisible {
  51. return "+i"
  52. }
  53. return ""
  54. }
  55. func (c *Client) HasNick() bool {
  56. return c.nick != ""
  57. }
  58. func (c *Client) HasUser() bool {
  59. return c.username != ""
  60. }
  61. func (c *Client) UserHost() string {
  62. return fmt.Sprintf("%s!%s@%s", c.nick, c.username, c.hostname)
  63. }
  64. func (c *Client) Id() string {
  65. return c.UserHost()
  66. }