Refactored events and event handling
This commit is contained in:
parent
d569334204
commit
10e3a6a6e3
@ -269,7 +269,7 @@ func (c *Client) readThread(signalChan chan signalCode) {
|
|||||||
lines := bytes.Split(rawLines, []byte("\r\n"))
|
lines := bytes.Split(rawLines, []byte("\r\n"))
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
if len(line) > 0 {
|
if len(line) > 0 {
|
||||||
c.server.eventChan <- Event{client: c, input: string(line)}
|
c.server.eventChan <- Event{client: c, event: command, input: string(line)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,9 +28,18 @@ type Client struct {
|
|||||||
channelMap map[string]*Channel
|
channelMap map[string]*Channel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type eventType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
connected eventType = iota
|
||||||
|
disconnected
|
||||||
|
command
|
||||||
|
)
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
client *Client
|
client *Client
|
||||||
input string
|
input string
|
||||||
|
event eventType
|
||||||
}
|
}
|
||||||
|
|
||||||
type Channel struct {
|
type Channel struct {
|
||||||
|
204
server.go
204
server.go
@ -48,8 +48,15 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}(e)
|
}(e)
|
||||||
fields := strings.Fields(e.input)
|
|
||||||
|
|
||||||
|
switch e.event {
|
||||||
|
case connected:
|
||||||
|
//Client connected
|
||||||
|
case disconnected:
|
||||||
|
//Client disconnected
|
||||||
|
case command:
|
||||||
|
//Client send a command
|
||||||
|
fields := strings.Fields(e.input)
|
||||||
if len(fields) < 1 {
|
if len(fields) < 1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -57,18 +64,23 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
if strings.HasPrefix(fields[0], ":") {
|
if strings.HasPrefix(fields[0], ":") {
|
||||||
fields = fields[1:]
|
fields = fields[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
command := strings.ToUpper(fields[0])
|
command := strings.ToUpper(fields[0])
|
||||||
args := fields[1:]
|
args := fields[1:]
|
||||||
|
|
||||||
switch {
|
s.handleCommand(e.client, command, args)
|
||||||
case command == "INFO":
|
}
|
||||||
e.client.reply(rplInfo, "Rosella IRCD github.com/eXeC64/Rosella")
|
}
|
||||||
case command == "VERSION":
|
|
||||||
e.client.reply(rplVersion, VERSION)
|
func (s *Server) handleCommand(client *Client, command string, args []string) {
|
||||||
case command == "NICK":
|
|
||||||
|
switch command {
|
||||||
|
case "INFO":
|
||||||
|
client.reply(rplInfo, "Rosella IRCD github.com/eXeC64/Rosella")
|
||||||
|
case "VERSION":
|
||||||
|
client.reply(rplVersion, VERSION)
|
||||||
|
case "NICK":
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
e.client.reply(errNoNick)
|
client.reply(errNoNick)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,47 +88,47 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
|
|
||||||
//Check newNick is of valid formatting (regex)
|
//Check newNick is of valid formatting (regex)
|
||||||
if nickRegexp.MatchString(newNick) == false {
|
if nickRegexp.MatchString(newNick) == false {
|
||||||
e.client.reply(errInvalidNick, newNick)
|
client.reply(errInvalidNick, newNick)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, exists := s.clientMap[strings.ToLower(newNick)]; exists {
|
if _, exists := s.clientMap[strings.ToLower(newNick)]; exists {
|
||||||
e.client.reply(errNickInUse, newNick)
|
client.reply(errNickInUse, newNick)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//Protect the server name from being used
|
//Protect the server name from being used
|
||||||
if strings.ToLower(newNick) == strings.ToLower(s.name) {
|
if strings.ToLower(newNick) == strings.ToLower(s.name) {
|
||||||
e.client.reply(errNickInUse, newNick)
|
client.reply(errNickInUse, newNick)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e.client.setNick(newNick)
|
client.setNick(newNick)
|
||||||
|
|
||||||
case command == "USER":
|
case "USER":
|
||||||
if e.client.nick == "" {
|
if client.nick == "" {
|
||||||
e.client.reply(rplKill, "Your nickname is already being used")
|
client.reply(rplKill, "Your nickname is already being used")
|
||||||
e.client.disconnect()
|
client.disconnect()
|
||||||
} else {
|
} else {
|
||||||
e.client.reply(rplWelcome)
|
client.reply(rplWelcome)
|
||||||
e.client.registered = true
|
client.registered = true
|
||||||
}
|
}
|
||||||
|
|
||||||
case command == "JOIN":
|
case "JOIN":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
e.client.reply(errMoreArgs)
|
client.reply(errMoreArgs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if args[0] == "0" {
|
if args[0] == "0" {
|
||||||
//Quit all channels
|
//Quit all channels
|
||||||
for channel := range e.client.channelMap {
|
for channel := range client.channelMap {
|
||||||
e.client.partChannel(channel, "Disconnecting")
|
client.partChannel(channel, "Disconnecting")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -125,18 +137,18 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
for _, channel := range channels {
|
for _, channel := range channels {
|
||||||
//Join the channel if it's valid
|
//Join the channel if it's valid
|
||||||
if channelRegexp.Match([]byte(channel)) {
|
if channelRegexp.Match([]byte(channel)) {
|
||||||
e.client.joinChannel(channel)
|
client.joinChannel(channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case command == "PART":
|
case "PART":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
e.client.reply(errMoreArgs)
|
client.reply(errMoreArgs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,18 +158,18 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
for _, channel := range channels {
|
for _, channel := range channels {
|
||||||
//Part the channel if it's valid
|
//Part the channel if it's valid
|
||||||
if channelRegexp.Match([]byte(channel)) {
|
if channelRegexp.Match([]byte(channel)) {
|
||||||
e.client.partChannel(channel, reason)
|
client.partChannel(channel, reason)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case command == "PRIVMSG":
|
case "PRIVMSG":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
e.client.reply(errMoreArgs)
|
client.reply(errMoreArgs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,64 +180,64 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
|
|
||||||
if chanExists {
|
if chanExists {
|
||||||
if channel.mode.noExternal {
|
if channel.mode.noExternal {
|
||||||
if _, inChannel := channel.clientMap[strings.ToLower(e.client.nick)]; !inChannel {
|
if _, inChannel := channel.clientMap[strings.ToLower(client.nick)]; !inChannel {
|
||||||
//Not in channel, not allowed to send
|
//Not in channel, not allowed to send
|
||||||
e.client.reply(errCannotSend, args[0])
|
client.reply(errCannotSend, args[0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if channel.mode.moderated {
|
if channel.mode.moderated {
|
||||||
clientMode := channel.modeMap[strings.ToLower(e.client.nick)]
|
clientMode := channel.modeMap[strings.ToLower(client.nick)]
|
||||||
if !clientMode.operator && !clientMode.voice {
|
if !clientMode.operator && !clientMode.voice {
|
||||||
//It's moderated and we're not +v or +o, do nothing
|
//It's moderated and we're not +v or +o, do nothing
|
||||||
e.client.reply(errCannotSend, args[0])
|
client.reply(errCannotSend, args[0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, c := range channel.clientMap {
|
for _, c := range channel.clientMap {
|
||||||
if c != e.client {
|
if c != client {
|
||||||
c.reply(rplMsg, e.client.nick, args[0], message)
|
c.reply(rplMsg, client.nick, args[0], message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if clientExists {
|
} else if clientExists {
|
||||||
client.reply(rplMsg, e.client.nick, client.nick, message)
|
client.reply(rplMsg, client.nick, client.nick, message)
|
||||||
} else {
|
} else {
|
||||||
e.client.reply(errNoSuchNick, args[0])
|
client.reply(errNoSuchNick, args[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
case command == "QUIT":
|
case "QUIT":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e.client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
case command == "TOPIC":
|
case "TOPIC":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
e.client.reply(errMoreArgs)
|
client.reply(errMoreArgs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
channel, exists := s.channelMap[strings.ToLower(args[0])]
|
channel, exists := s.channelMap[strings.ToLower(args[0])]
|
||||||
if exists == false {
|
if exists == false {
|
||||||
e.client.reply(errNoSuchNick, args[0])
|
client.reply(errNoSuchNick, args[0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
e.client.reply(rplTopic, channel.name, channel.topic)
|
client.reply(rplTopic, channel.name, channel.topic)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
clientMode := channel.modeMap[strings.ToLower(e.client.nick)]
|
clientMode := channel.modeMap[strings.ToLower(client.nick)]
|
||||||
if channel.mode.topicLocked && !clientMode.operator {
|
if channel.mode.topicLocked && !clientMode.operator {
|
||||||
e.client.reply(errNoPriv)
|
client.reply(errNoPriv)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,9 +256,9 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case command == "LIST":
|
case "LIST":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,7 +267,7 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
|
|
||||||
for channelName, channel := range s.channelMap {
|
for channelName, channel := range s.channelMap {
|
||||||
if channel.mode.secret {
|
if channel.mode.secret {
|
||||||
if _, inChannel := channel.clientMap[strings.ToLower(e.client.nick)]; !inChannel {
|
if _, inChannel := channel.clientMap[strings.ToLower(client.nick)]; !inChannel {
|
||||||
//Not in the channel, skip
|
//Not in the channel, skip
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -264,7 +276,7 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
chanList = append(chanList, listItem)
|
chanList = append(chanList, listItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.client.reply(rplList, chanList...)
|
client.reply(rplList, chanList...)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
channels := strings.Split(args[0], ",")
|
channels := strings.Split(args[0], ",")
|
||||||
@ -277,16 +289,16 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
e.client.reply(rplList, chanList...)
|
client.reply(rplList, chanList...)
|
||||||
}
|
}
|
||||||
case command == "OPER":
|
case "OPER":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
e.client.reply(errMoreArgs)
|
client.reply(errMoreArgs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,26 +310,26 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
io.WriteString(h, password)
|
io.WriteString(h, password)
|
||||||
pass := fmt.Sprintf("%x", h.Sum(nil))
|
pass := fmt.Sprintf("%x", h.Sum(nil))
|
||||||
if hashedPassword == pass {
|
if hashedPassword == pass {
|
||||||
e.client.operator = true
|
client.operator = true
|
||||||
e.client.reply(rplOper)
|
client.reply(rplOper)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
e.client.reply(errPassword)
|
client.reply(errPassword)
|
||||||
|
|
||||||
case command == "KILL":
|
case "KILL":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.client.operator == false {
|
if client.operator == false {
|
||||||
e.client.reply(errNoPriv)
|
client.reply(errNoPriv)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
e.client.reply(errMoreArgs)
|
client.reply(errMoreArgs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,21 +339,21 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
|
|
||||||
client, exists := s.clientMap[strings.ToLower(nick)]
|
client, exists := s.clientMap[strings.ToLower(nick)]
|
||||||
if !exists {
|
if !exists {
|
||||||
e.client.reply(errNoSuchNick, nick)
|
client.reply(errNoSuchNick, nick)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client.reply(rplKill, e.client.nick, reason)
|
client.reply(rplKill, client.nick, reason)
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
case command == "KICK":
|
case "KICK":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
e.client.reply(errMoreArgs)
|
client.reply(errMoreArgs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,19 +362,19 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
|
|
||||||
channel, channelExists := s.channelMap[channelKey]
|
channel, channelExists := s.channelMap[channelKey]
|
||||||
if !channelExists {
|
if !channelExists {
|
||||||
e.client.reply(errNoSuchNick, args[0])
|
client.reply(errNoSuchNick, args[0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
target, targetExists := channel.clientMap[targetKey]
|
target, targetExists := channel.clientMap[targetKey]
|
||||||
if !targetExists {
|
if !targetExists {
|
||||||
e.client.reply(errNoSuchNick, args[1])
|
client.reply(errNoSuchNick, args[1])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
clientMode := channel.modeMap[e.client.key]
|
clientMode := channel.modeMap[client.key]
|
||||||
if !clientMode.operator && !e.client.operator {
|
if !clientMode.operator && !client.operator {
|
||||||
e.client.reply(errNoPriv)
|
client.reply(errNoPriv)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -370,21 +382,21 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
|
|
||||||
//It worked
|
//It worked
|
||||||
for _, client := range channel.clientMap {
|
for _, client := range channel.clientMap {
|
||||||
client.reply(rplKick, e.client.nick, channel.name, target.nick, reason)
|
client.reply(rplKick, client.nick, channel.name, target.nick, reason)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(channel.clientMap, targetKey)
|
delete(channel.clientMap, targetKey)
|
||||||
delete(channel.modeMap, targetKey)
|
delete(channel.modeMap, targetKey)
|
||||||
delete(target.channelMap, channelKey)
|
delete(target.channelMap, channelKey)
|
||||||
|
|
||||||
case command == "MODE":
|
case "MODE":
|
||||||
if e.client.registered == false {
|
if client.registered == false {
|
||||||
e.client.reply(errNotReg)
|
client.reply(errNotReg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
e.client.reply(errMoreArgs)
|
client.reply(errMoreArgs)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,23 +404,23 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
|
|
||||||
channel, channelExists := s.channelMap[channelKey]
|
channel, channelExists := s.channelMap[channelKey]
|
||||||
if !channelExists {
|
if !channelExists {
|
||||||
e.client.reply(errNoSuchNick, args[0])
|
client.reply(errNoSuchNick, args[0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mode := channel.mode
|
mode := channel.mode
|
||||||
|
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
//No more args, they just want the mode
|
//No more args, they just want the mode
|
||||||
e.client.reply(rplChannelModeIs, args[0], mode.String(), "")
|
client.reply(rplChannelModeIs, args[0], mode.String(), "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if cm, ok := channel.modeMap[strings.ToLower(e.client.nick)]; !ok || !cm.operator {
|
if cm, ok := channel.modeMap[strings.ToLower(client.nick)]; !ok || !cm.operator {
|
||||||
//Not a channel operator.
|
//Not a channel operator.
|
||||||
|
|
||||||
//If they're not an irc operator either, they'll fail
|
//If they're not an irc operator either, they'll fail
|
||||||
if !e.client.operator {
|
if !client.operator {
|
||||||
e.client.reply(errNoPriv)
|
client.reply(errNoPriv)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -485,6 +497,6 @@ func (s *Server) handleEvent(e Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
e.client.reply(errUnknownCommand, command)
|
client.reply(errUnknownCommand, command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user