Handle MODE messages from upstream servers

This commit is contained in:
Simon Ser 2020-02-06 18:07:35 +01:00
parent f2b471259a
commit 343d4cfded
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"strings"
"gopkg.in/irc.v3" "gopkg.in/irc.v3"
) )
@ -14,12 +15,52 @@ const (
rpl_globalusers = "266" rpl_globalusers = "266"
) )
type modeSet string
func (ms modeSet) Has(c byte) bool {
return strings.IndexByte(string(ms), c) >= 0
}
func (ms *modeSet) Add(c byte) {
if !ms.Has(c) {
*ms += modeSet(c)
}
}
func (ms *modeSet) Del(c byte) {
i := strings.IndexByte(string(*ms), c)
if i >= 0 {
*ms = (*ms)[:i] + (*ms)[i+1:]
}
}
func (ms *modeSet) Apply(s string) error {
var plusMinus byte
for i := 0; i < len(s); i++ {
switch c := s[i]; c {
case '+', '-':
plusMinus = c
default:
switch plusMinus {
case '+':
ms.Add(c)
case '-':
ms.Del(c)
default:
return fmt.Errorf("malformed modestring %q: missing plus/minus", s)
}
}
}
return nil
}
type upstreamConn struct { type upstreamConn struct {
upstream *Upstream upstream *Upstream
net net.Conn net net.Conn
irc *irc.Conn irc *irc.Conn
srv *Server srv *Server
registered bool registered bool
modes modeSet
serverName string serverName string
availableUserModes string availableUserModes string
@ -35,6 +76,14 @@ func (c *upstreamConn) handleMessage(msg *irc.Message) error {
Command: "PONG", Command: "PONG",
Params: []string{c.srv.Hostname}, Params: []string{c.srv.Hostname},
}) })
case "MODE":
if len(msg.Params) < 2 {
return newNeedMoreParamsError(msg.Command)
}
if nick := msg.Params[0]; nick != c.upstream.Nick {
return fmt.Errorf("received MODE message for unknow nick %q", nick)
}
return c.modes.Apply(msg.Params[1])
case irc.RPL_WELCOME: case irc.RPL_WELCOME:
c.registered = true c.registered = true
c.srv.Logger.Printf("Connection to %q registered", c.upstream.Addr) c.srv.Logger.Printf("Connection to %q registered", c.upstream.Addr)