Implemented Client as an Interface

This commit is contained in:
Andreas Neue 2016-07-18 21:22:03 +02:00
parent c4b4e47cdf
commit 60506584cb
2 changed files with 65 additions and 62 deletions

View File

@ -14,7 +14,15 @@ import (
"code.dnix.de/an/xlog" "code.dnix.de/an/xlog"
) )
type Client struct { type Client interface {
Name() string
Send(*irc.Message)
Receive(*irc.Message)
Register() chan bool
Destroy()
}
type RemoteClient struct {
server *Server server *Server
name string name string
@ -33,8 +41,8 @@ type Client struct {
writeq chan string writeq chan string
} }
func NewClient(srv *Server, conn net.Conn) *Client { func NewRemoteClient(srv *Server, conn net.Conn) *RemoteClient {
cl := new(Client) cl := new(RemoteClient)
cl.server = srv cl.server = srv
cl.name = "" cl.name = ""
@ -52,47 +60,58 @@ func NewClient(srv *Server, conn net.Conn) *Client {
go cl.connReader() go cl.connReader()
go cl.connWriter() go cl.connWriter()
go cl.loop() go cl.dispatch()
xlog.Info("Client connected") xlog.Info("RemoteClient connected")
return cl return cl
} }
func (cl *Client) Name() string { func (cl *RemoteClient) Name() string {
return cl.name return cl.name
} }
func (cl *Client) Send(msg *irc.Message) { func (cl *RemoteClient) Send(msg *irc.Message) {
msg.Pre = cl.name msg.Pre = cl.name
cl.server.Dispatch <- msg cl.server.Dispatch <- msg
} }
func (cl *Client) Receive(msg *irc.Message) { func (cl *RemoteClient) Receive(msg *irc.Message) {
cl.receive <- msg cl.receive <- msg
} }
func (cl *Client) Register() chan bool { func (cl *RemoteClient) Register() chan bool {
return cl.register return cl.register
} }
func (cl *Client) AddMode(mode string) { func (cl *RemoteClient) AddMode(mode string) {
cl.modes = cl.modes + mode cl.modes = cl.modes + mode
} }
func (cl *Client) DelMode(mode string) { func (cl *RemoteClient) DelMode(mode string) {
cl.modes = strings.Replace(cl.modes, mode, "", -1) cl.modes = strings.Replace(cl.modes, mode, "", -1)
} }
func (cl *Client) HasMode(mode string) bool { func (cl *RemoteClient) HasMode(mode string) bool {
return strings.IndexRune(cl.modes, rune(mode[0])) != -1 return strings.IndexRune(cl.modes, rune(mode[0])) != -1
} }
func (cl *Client) writeMsg(msg *irc.Message) { func (cl *RemoteClient) Destroy() {
cl.writeLine(msg.String()) if cl.isClosed {
return
}
cl.isClosed = true
close(cl.writeq)
cl.conn.Write([]byte("ERROR :Closing link"))
cl.conn.Close()
if cl.name != "" {
xlog.Info("RemoteClient '%s' disconnected", cl.name)
} else {
xlog.Info("RemoteClient disconnected")
}
} }
func (cl *Client) loop() { func (cl *RemoteClient) dispatch() {
for { for {
time.Sleep(1 * time.Millisecond) time.Sleep(1 * time.Millisecond)
if cl.isClosed { if cl.isClosed {
@ -107,26 +126,7 @@ func (cl *Client) loop() {
} }
} }
func (cl *Client) destroy() { func (cl *RemoteClient) connReader() {
if cl.isClosed {
return
}
cl.isClosed = true
close(cl.writeq)
cl.conn.Write([]byte("ERROR :Closing link"))
cl.conn.Close()
if cl.name != "" {
xlog.Info("Client '%s' disconnected", cl.name)
} else {
xlog.Info("Client disconnected")
}
}
func (cl *Client) writeLine(format string, a ...interface{}) {
cl.writeq <- fmt.Sprintf(format, a...)
}
func (cl *Client) connReader() {
input := bufio.NewReader(cl.conn) input := bufio.NewReader(cl.conn)
for { for {
if cl.isClosed { if cl.isClosed {
@ -149,7 +149,7 @@ func (cl *Client) connReader() {
} }
} }
func (cl *Client) connWriter() { func (cl *RemoteClient) connWriter() {
for line := range cl.writeq { for line := range cl.writeq {
written := 0 written := 0
bytes := []byte(line + "\r\n") bytes := []byte(line + "\r\n")
@ -170,13 +170,21 @@ func (cl *Client) connWriter() {
xlog.Debug("connWriter: exiting") xlog.Debug("connWriter: exiting")
} }
var lineFuncs = map[string]func(*Client, *irc.Message) bool{ func (cl *RemoteClient) writeMsg(msg *irc.Message) {
cl.writeLine(msg.String())
}
func (cl *RemoteClient) writeLine(format string, a ...interface{}) {
cl.writeq <- fmt.Sprintf(format, a...)
}
var lineFuncs = map[string]func(*RemoteClient, *irc.Message) bool{
"PASS": handleLinePass, "PASS": handleLinePass,
"NICK": handleLineNick, "NICK": handleLineNick,
"USER": handleLineUser, "USER": handleLineUser,
} }
func (cl *Client) handleLine(s string) { func (cl *RemoteClient) handleLine(s string) {
xlog.Debug("handleLine: [%s] '%s'", cl.name, s) xlog.Debug("handleLine: [%s] '%s'", cl.name, s)
msg := irc.Parse(s) msg := irc.Parse(s)
hook, exists := lineFuncs[msg.Cmd] hook, exists := lineFuncs[msg.Cmd]
@ -189,18 +197,18 @@ func (cl *Client) handleLine(s string) {
} }
} }
func checkAuth(cl *Client) { func checkAuth(cl *RemoteClient) {
if cl.name == "" || cl.password == "" { if cl.name == "" || cl.password == "" {
return return
} }
} }
func handleLinePass(cl *Client, msg *irc.Message) bool { func handleLinePass(cl *RemoteClient, msg *irc.Message) bool {
cl.password = msg.Args[0] cl.password = msg.Args[0]
return false return false
} }
func handleLineNick(cl *Client, msg *irc.Message) bool { func handleLineNick(cl *RemoteClient, msg *irc.Message) bool {
if cl.name != "" { if cl.name != "" {
return false return false
} }
@ -220,6 +228,6 @@ func handleLineNick(cl *Client, msg *irc.Message) bool {
return false return false
} }
func handleLineUser(cl *Client, msg *irc.Message) bool { func handleLineUser(cl *RemoteClient, msg *irc.Message) bool {
return false return false
} }

View File

@ -27,15 +27,10 @@ var flagConfig string
var myinfo string = "%s %s/%s * *" var myinfo string = "%s %s/%s * *"
var isupport string = "ALIAS FRIEND UNFRIEND CASEMAPPING=rfc1459 CHANLIMIT=#:1024 CHANMODES=b,k,l,imnpst CHANNELLEN=200 CHANTYPES=# EXCEPTS=e KICKLEN MAXLIST=b:50,e:50 MODES=1 NETWORK=dnix.de NICKLEN=32 PREFIX=(aohv)&@%%+ SAFELIST STATUSMSG=&@%%+ TOPICLEN" var isupport string = "ALIAS FRIEND UNFRIEND CASEMAPPING=rfc1459 CHANLIMIT=#:1024 CHANMODES=b,k,l,imnpst CHANNELLEN=200 CHANTYPES=# EXCEPTS=e KICKLEN MAXLIST=b:50,e:50 MODES=1 NETWORK=dnix.de NICKLEN=32 PREFIX=(aohv)&@%%+ SAFELIST STATUSMSG=&@%%+ TOPICLEN"
type ControlMsg struct {
Opcode int
Client *Client
}
type Server struct { type Server struct {
Dispatch chan *irc.Message Dispatch chan *irc.Message
AddClient chan *Client AddClient chan Client
DelClient chan *Client DelClient chan Client
host string host string
info string info string
@ -43,7 +38,7 @@ type Server struct {
version string version string
created string created string
motd string motd string
clients map[string]*Client clients map[string]Client
channels map[string]map[string]string channels map[string]map[string]string
ports map[int]bool ports map[int]bool
config *conf.ConfigFile config *conf.ConfigFile
@ -58,10 +53,10 @@ func NewServer(configPath, software, version string) *Server {
srv := &Server{software: software, version: version, created: "yes"} srv := &Server{software: software, version: version, created: "yes"}
srv.Dispatch = make(chan *irc.Message, 1024) srv.Dispatch = make(chan *irc.Message, 1024)
srv.AddClient = make(chan *Client, 1024) srv.AddClient = make(chan Client, 1024)
srv.DelClient = make(chan *Client, 1024) srv.DelClient = make(chan Client, 1024)
srv.clients = make(map[string]*Client) srv.clients = make(map[string]Client)
srv.channels = make(map[string]map[string]string) srv.channels = make(map[string]map[string]string)
srv.configPath = configPath srv.configPath = configPath
srv.loadConfig() srv.loadConfig()
@ -102,7 +97,7 @@ func (srv *Server) listen(address string, port int) {
if err != nil { if err != nil {
xlog.Error(err.Error()) xlog.Error(err.Error())
} else { } else {
NewClient(srv, conn) NewRemoteClient(srv, conn)
} }
} }
} }
@ -130,7 +125,7 @@ func (srv *Server) dispatch() {
srv.clientMotd(cl) srv.clientMotd(cl)
case cl := <-srv.DelClient: case cl := <-srv.DelClient:
name := cl.Name() name := cl.Name()
cl.destroy() cl.Destroy()
//go func() { //go func() {
// time.Sleep(10 * time.Second) // time.Sleep(10 * time.Second)
delete(srv.clients, name) delete(srv.clients, name)
@ -169,7 +164,7 @@ func (srv *Server) recvMsg(msg *irc.Message) {
func (srv *Server) sendMsg(msg *irc.Message) { func (srv *Server) sendMsg(msg *irc.Message) {
if strings.HasPrefix(msg.Args[0], "#") { if strings.HasPrefix(msg.Args[0], "#") {
srv.sendMsgToChannel(msg) srv.channelBroadcast(msg.Args[0], msg)
} else { } else {
srv.sendMsgToClient(msg) srv.sendMsgToClient(msg)
} }
@ -213,7 +208,7 @@ func (srv *Server) sendReply(tar, cmd, args, trail string) {
cl.Receive(irc.M(srv.host, cmd, args, trail)) cl.Receive(irc.M(srv.host, cmd, args, trail))
} }
func (srv *Server) clientLogon(cl *Client) { func (srv *Server) clientLogon(cl Client) {
srv.sendReply(cl.Name(), RPL_WELCOME, "", "Willkommen!") srv.sendReply(cl.Name(), RPL_WELCOME, "", "Willkommen!")
srv.sendReply(cl.Name(), RPL_YOURHOST, "", srv.sendReply(cl.Name(), RPL_YOURHOST, "",
fmt.Sprintf("Your host is %s, running on %s/%s", fmt.Sprintf("Your host is %s, running on %s/%s",
@ -226,7 +221,7 @@ func (srv *Server) clientLogon(cl *Client) {
isupport+" are supported by this server") isupport+" are supported by this server")
} }
func (srv *Server) clientMotd(cl *Client) { func (srv *Server) clientMotd(cl Client) {
srv.sendReply(cl.Name(), RPL_MOTDSTART, "", srv.sendReply(cl.Name(), RPL_MOTDSTART, "",
fmt.Sprintf("- %s Message of the day -", srv.host)) fmt.Sprintf("- %s Message of the day -", srv.host))
for _, line := range strings.Split(srv.motd, "\n") { for _, line := range strings.Split(srv.motd, "\n") {
@ -237,7 +232,7 @@ func (srv *Server) clientMotd(cl *Client) {
func (srv *Server) channelBroadcast(ch string, msg *irc.Message) { func (srv *Server) channelBroadcast(ch string, msg *irc.Message) {
for nick, _ := range srv.channels[ch] { for nick, _ := range srv.channels[ch] {
if msg.Pre == nick { if msg.Pre == nick && msg.Cmd == "PRIVMSG" {
continue continue
} }
srv.clients[nick].Receive(msg) srv.clients[nick].Receive(msg)
@ -245,6 +240,7 @@ func (srv *Server) channelBroadcast(ch string, msg *irc.Message) {
} }
func (srv *Server) channelJoin(nick, ch string) { func (srv *Server) channelJoin(nick, ch string) {
println("join")
if _, exists := srv.channels[ch]; !exists { if _, exists := srv.channels[ch]; !exists {
srv.channels[ch] = make(map[string]string) srv.channels[ch] = make(map[string]string)
} }
@ -263,7 +259,7 @@ func (srv *Server) channelPart(nick, ch, reason string) {
return return
} }
srv.channelBroadcast(ch, irc.M(nick, "PART", ch, reason)) srv.channelBroadcast(ch, irc.M(nick, "PART", ch, reason))
delete(srv.channels[ch][nick]) delete(srv.channels[ch], nick)
} }
type SrvCommandHook struct { type SrvCommandHook struct {
@ -308,11 +304,10 @@ func srvHandleCmdPrivmsg(srv *Server, msg *irc.Message) {
func srvHandleCmdJoin(srv *Server, msg *irc.Message) { func srvHandleCmdJoin(srv *Server, msg *irc.Message) {
srv.channelJoin(msg.Pre, msg.Args[0]) srv.channelJoin(msg.Pre, msg.Args[0])
} }
func srvHandleCmdPart(srv *Server, msg *irc.Message) { func srvHandleCmdPart(srv *Server, msg *irc.Message) {
srv.channelPart(msg.Pre, msg.Args[0], msg.Trailing) srv.channelPart(msg.Pre, msg.Args[0], msg.Trail)
} }
func srvHandleCmdOper(srv *Server, msg *irc.Message) { func srvHandleCmdOper(srv *Server, msg *irc.Message) {