2012-04-18 03:16:57 +02:00
|
|
|
package irc
|
|
|
|
|
2012-04-18 05:24:26 +02:00
|
|
|
type Message interface {
|
|
|
|
Handle(s *Server, c *Client)
|
|
|
|
}
|
|
|
|
|
2012-04-18 03:16:57 +02:00
|
|
|
func (m *NickMessage) Handle(s *Server, c *Client) {
|
|
|
|
if s.nicks[m.nickname] != nil {
|
|
|
|
c.send <- ErrNickNameInUse(m.nickname)
|
|
|
|
return
|
|
|
|
}
|
2012-04-18 06:13:12 +02:00
|
|
|
oldNick := c.nick
|
2012-04-18 03:16:57 +02:00
|
|
|
if c.nick != "" {
|
|
|
|
delete(s.nicks, c.nick)
|
|
|
|
}
|
|
|
|
c.nick = m.nickname
|
|
|
|
s.nicks[c.nick] = c
|
2012-04-18 06:13:12 +02:00
|
|
|
if c.registered {
|
|
|
|
c.send <- ReplyNick(oldNick, c)
|
|
|
|
} else {
|
|
|
|
tryRegister(s, c)
|
|
|
|
}
|
2012-04-18 03:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UserMessage) Handle(s *Server, c *Client) {
|
|
|
|
if c.username != "" {
|
|
|
|
c.send <- ErrAlreadyRegistered(c.Nick())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.username, c.realname = m.user, m.realname
|
|
|
|
tryRegister(s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *QuitMessage) Handle(s *Server, c *Client) {
|
|
|
|
c.send <- MessageError()
|
2012-04-18 05:24:26 +02:00
|
|
|
c.conn.Close()
|
2012-04-18 03:16:57 +02:00
|
|
|
delete(s.nicks, c.nick)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UnknownMessage) Handle(s *Server, c *Client) {
|
|
|
|
c.send <- ErrUnknownCommand(c.Nick(), m.command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *PingMessage) Handle(s *Server, c *Client) {
|
|
|
|
c.send <- MessagePong()
|
|
|
|
}
|
|
|
|
|
2012-04-18 05:24:26 +02:00
|
|
|
func (m *ModeMessage) Handle(s *Server, c *Client) {
|
|
|
|
if m.nickname != c.nick {
|
|
|
|
c.send <- ErrUsersDontMatch(c.Nick())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, mode := range m.modes {
|
|
|
|
if mode == "+i" {
|
|
|
|
c.invisible = true
|
|
|
|
} else if mode == "-i" {
|
|
|
|
c.invisible = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.send <- ReplyUModeIs(c)
|
|
|
|
}
|
|
|
|
|
2012-04-18 03:16:57 +02:00
|
|
|
func tryRegister(s *Server, c *Client) {
|
2012-04-18 06:13:12 +02:00
|
|
|
if (!c.registered && c.HasNick() && c.HasUser()) {
|
2012-04-18 03:16:57 +02:00
|
|
|
c.registered = true
|
2012-04-18 06:13:12 +02:00
|
|
|
c.send <- ReplyWelcome(c)
|
2012-04-18 07:11:35 +02:00
|
|
|
c.send <- ReplyYourHost(c.Nick(), s.name)
|
2012-04-18 07:21:41 +02:00
|
|
|
c.send <- ReplyCreated(c.Nick(), s.ctime)
|
2012-04-18 07:11:35 +02:00
|
|
|
c.send <- ReplyMyInfo(c.Nick(), s.name)
|
2012-04-18 03:16:57 +02:00
|
|
|
}
|
|
|
|
}
|