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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package irc
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "time"
  7. )
  8. type Client struct {
  9. atime time.Time
  10. away bool
  11. channels ChannelSet
  12. conn net.Conn
  13. hostname string
  14. nick string
  15. realname string
  16. registered bool
  17. replies chan<- Reply
  18. server *Server
  19. serverPass bool
  20. username string
  21. }
  22. type ClientSet map[*Client]bool
  23. func NewClient(server *Server, conn net.Conn) *Client {
  24. read := StringReadChan(conn)
  25. write := StringWriteChan(conn)
  26. replies := make(chan Reply)
  27. client := &Client{
  28. channels: make(ChannelSet),
  29. conn: conn,
  30. hostname: LookupHostname(conn.RemoteAddr()),
  31. replies: replies,
  32. server: server,
  33. }
  34. go client.readConn(read)
  35. go client.writeConn(write, replies)
  36. return client
  37. }
  38. func (c *Client) readConn(recv <-chan string) {
  39. for str := range recv {
  40. m, err := ParseCommand(str)
  41. if err != nil {
  42. if err == NotEnoughArgsError {
  43. c.replies <- ErrNeedMoreParams(c.server, str)
  44. } else {
  45. c.replies <- ErrUnknownCommand(c.server, str)
  46. }
  47. continue
  48. }
  49. m.SetBase(c)
  50. c.server.commands <- m
  51. }
  52. }
  53. func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
  54. for reply := range replies {
  55. if DEBUG_CLIENT {
  56. log.Printf("%s ← %s : %s", c, reply.Source(), reply)
  57. }
  58. reply.Format(c, write)
  59. }
  60. }
  61. func (c *Client) Replies() chan<- Reply {
  62. return c.replies
  63. }
  64. func (c *Client) Server() *Server {
  65. return c.server
  66. }
  67. func (c *Client) Nick() string {
  68. if c.HasNick() {
  69. return c.nick
  70. }
  71. return "guest"
  72. }
  73. func (c *Client) UModeString() string {
  74. return ""
  75. }
  76. func (c *Client) HasNick() bool {
  77. return c.nick != ""
  78. }
  79. func (c *Client) HasUsername() bool {
  80. return c.username != ""
  81. }
  82. func (c *Client) Username() string {
  83. if c.HasUsername() {
  84. return c.username
  85. }
  86. return "guest"
  87. }
  88. func (c *Client) UserHost() string {
  89. return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Username(), c.hostname)
  90. }
  91. func (c *Client) Id() string {
  92. return c.UserHost()
  93. }
  94. func (c *Client) String() string {
  95. return c.UserHost()
  96. }
  97. func (c *Client) PublicId() string {
  98. return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Nick(), c.server.Id())
  99. }