2013-08-24 06:48:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-03 06:25:27 +00:00
|
|
|
"net"
|
2016-05-03 07:57:55 +00:00
|
|
|
"strings"
|
2013-08-24 06:48:28 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-05-03 06:25:27 +00:00
|
|
|
type Client struct {
|
2016-05-03 07:25:37 +00:00
|
|
|
server *Server
|
2016-05-03 06:25:27 +00:00
|
|
|
connection net.Conn
|
|
|
|
nick string
|
|
|
|
registered bool
|
|
|
|
connected bool
|
|
|
|
operator bool
|
|
|
|
}
|
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
func (c *Client) disconnect() {
|
|
|
|
c.connected = false
|
2016-05-03 07:57:02 +00:00
|
|
|
c.connection.Close()
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 07:25:37 +00:00
|
|
|
func (c *Client) sendGlobalMessage(motd string) {
|
|
|
|
c.reply(rplMOTDStart)
|
|
|
|
|
|
|
|
for len(motd) > 80 {
|
|
|
|
c.reply(rplMOTD, motd[:80])
|
|
|
|
motd = motd[80:]
|
|
|
|
}
|
|
|
|
if len(motd) > 0 {
|
|
|
|
c.reply(rplMOTD, motd)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.reply(rplEndOfMOTD)
|
|
|
|
}
|
|
|
|
|
2013-08-24 06:48:28 +00:00
|
|
|
//Send a reply to a user with the code specified
|
2013-08-24 19:58:38 +00:00
|
|
|
func (c *Client) reply(code replyCode, args ...string) {
|
2013-08-24 06:48:28 +00:00
|
|
|
if c.connected == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch code {
|
|
|
|
case rplWelcome:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 001 %s :Welcome to %s", c.server.name, c.nick, c.server.name))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplJoin:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s JOIN %s", args[0], args[1]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplPart:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s PART %s %s", args[0], args[1], args[2]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplTopic:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 332 %s %s :%s", c.server.name, c.nick, args[0], args[1]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplNoTopic:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 331 %s %s :No topic is set", c.server.name, c.nick, args[0]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplNames:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 353 %s = %s :%s", c.server.name, c.nick, args[0], args[1]))
|
2016-03-09 08:04:04 +00:00
|
|
|
case rplEndOfNames:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 366 %s %s :End of NAMES list", c.server.name, c.nick, args[0]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplNickChange:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s NICK %s", args[0], args[1]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplKill:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s KILL %s A %s", args[0], c.nick, args[1]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplMsg:
|
2016-05-03 07:57:55 +00:00
|
|
|
for _, itm := range strings.Split(args[2], "\n") {
|
|
|
|
c.write(fmt.Sprintf(":%s PRIVMSG %s %s", args[0], args[1], itm))
|
|
|
|
}
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplList:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 322 %s %s", c.server.name, c.nick, args[0]))
|
2016-03-13 09:07:16 +00:00
|
|
|
case rplListEnd:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 323 %s", c.server.name, c.nick))
|
2013-08-24 06:48:28 +00:00
|
|
|
case rplOper:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 381 %s :You are now an operator", c.server.name, c.nick))
|
2013-08-27 18:34:52 +00:00
|
|
|
case rplChannelModeIs:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 324 %s %s %s %s", c.server.name, c.nick, args[0], args[1], args[2]))
|
2013-08-29 20:10:28 +00:00
|
|
|
case rplKick:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s KICK %s %s %s", args[0], args[1], args[2], args[3]))
|
2013-08-30 22:17:54 +00:00
|
|
|
case rplInfo:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 371 %s :%s", c.server.name, c.nick, args[0]))
|
2013-09-02 00:55:19 +00:00
|
|
|
case rplVersion:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 351 %s %s", c.server.name, c.nick, args[0]))
|
2016-03-13 06:51:10 +00:00
|
|
|
case rplMOTDStart:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 375 %s :- Message of the day - ", c.server.name, c.nick))
|
2016-03-13 06:51:10 +00:00
|
|
|
case rplMOTD:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 372 %s :- %s", c.server.name, c.nick, args[0]))
|
2016-03-13 06:51:10 +00:00
|
|
|
case rplEndOfMOTD:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 376 %s :End of MOTD Command", c.server.name, c.nick))
|
2013-10-21 12:34:12 +00:00
|
|
|
case rplPong:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s PONG %s %s", c.server.name, c.nick, c.server.name))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errMoreArgs:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 461 %s :Not enough params", c.server.name, c.nick))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errNoNick:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 431 %s :No nickname given", c.server.name, c.nick))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errInvalidNick:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 432 %s %s :Erronenous nickname", c.server.name, c.nick, args[0]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errNickInUse:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 433 %s %s :Nick already in use", c.server.name, c.nick, args[0]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errAlreadyReg:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 462 :You need a valid nick first", c.server.name))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errNoSuchNick:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 401 %s %s :No such nick/channel", c.server.name, c.nick, args[0]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errUnknownCommand:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 421 %s %s :Unknown command", c.server.name, c.nick, args[0]))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errNotReg:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 451 :You have not registered", c.server.name))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errPassword:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 464 %s :Error, password incorrect", c.server.name, c.nick))
|
2013-08-24 06:48:28 +00:00
|
|
|
case errNoPriv:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 481 %s :Permission denied", c.server.name, c.nick))
|
2013-08-27 18:40:14 +00:00
|
|
|
case errCannotSend:
|
2016-05-03 06:47:24 +00:00
|
|
|
c.write(fmt.Sprintf(":%s 404 %s %s :Cannot send to channel", c.server.name, c.nick, args[0]))
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 06:47:24 +00:00
|
|
|
func (c *Client) write(output string) {
|
|
|
|
c.connection.SetWriteDeadline(time.Now().Add(time.Second * 30))
|
|
|
|
if _, err := fmt.Fprintf(c.connection, "%s\r\n", output); err != nil {
|
|
|
|
c.disconnect()
|
|
|
|
return
|
2013-08-24 06:48:28 +00:00
|
|
|
}
|
|
|
|
}
|