3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00
ergo/src/irc/commands.go

72 lines
1.5 KiB
Go
Raw Normal View History

package irc
2012-04-18 05:24:26 +02:00
type Message interface {
Handle(s *Server, c *Client)
}
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
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)
}
}
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()
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)
}
func tryRegister(s *Server, c *Client) {
2012-04-18 06:13:12 +02:00
if (!c.registered && c.HasNick() && c.HasUser()) {
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)
}
}