ircd/client.go

226 lines
4.0 KiB
Go
Raw Normal View History

2016-07-17 21:52:03 +00:00
// vim:ts=4:sts=4:sw=4:noet:tw=72
package main
2014-11-22 13:21:30 +00:00
import (
"bufio"
"fmt"
"io"
"net"
"strings"
"time"
2016-07-17 21:52:03 +00:00
"code.dnix.de/an/irc"
"code.dnix.de/an/xlog"
)
2014-11-22 13:21:30 +00:00
2016-07-17 21:52:03 +00:00
type Client struct {
2014-11-22 13:21:30 +00:00
server *Server
name string
password string
modes string
isSSL bool
isRegistered bool
isAuthed bool
isClosed bool
2016-07-17 21:52:03 +00:00
receive chan *irc.Message
2014-11-22 13:21:30 +00:00
register chan bool
2016-07-17 21:52:03 +00:00
conn net.Conn
writeq chan string
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func NewClient(srv *Server, conn net.Conn) *Client {
cl := new(Client)
2014-11-22 13:21:30 +00:00
cl.server = srv
cl.name = ""
cl.password = ""
cl.modes = ""
2016-07-17 21:52:03 +00:00
cl.receive = make(chan *irc.Message)
2014-11-22 13:21:30 +00:00
cl.register = make(chan bool)
cl.isRegistered = false
cl.isAuthed = false
cl.isClosed = false
cl.conn = conn
cl.writeq = make(chan string, 256)
go cl.connReader()
go cl.connWriter()
go cl.loop()
2016-07-17 21:52:03 +00:00
xlog.Info("Client connected")
2014-11-22 13:21:30 +00:00
return cl
}
2016-07-17 21:52:03 +00:00
func (cl *Client) Name() string {
2014-11-22 13:21:30 +00:00
return cl.name
}
2016-07-17 21:52:03 +00:00
func (cl *Client) Send(msg *irc.Message) {
msg.Pre = cl.name
cl.server.Dispatch <- msg
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func (cl *Client) Receive(msg *irc.Message) {
2014-11-22 13:21:30 +00:00
cl.receive <- msg
}
2016-07-17 21:52:03 +00:00
func (cl *Client) Register() chan bool {
2014-11-22 13:21:30 +00:00
return cl.register
}
2016-07-17 21:52:03 +00:00
func (cl *Client) AddMode(mode string) {
2014-11-22 13:21:30 +00:00
cl.modes = cl.modes + mode
}
2016-07-17 21:52:03 +00:00
func (cl *Client) DelMode(mode string) {
2014-11-22 13:21:30 +00:00
cl.modes = strings.Replace(cl.modes, mode, "", -1)
}
2016-07-17 21:52:03 +00:00
func (cl *Client) HasMode(mode string) bool {
2014-11-22 13:21:30 +00:00
return strings.IndexRune(cl.modes, rune(mode[0])) != -1
}
2016-07-17 21:52:03 +00:00
func (cl *Client) writeMsg(msg *irc.Message) {
cl.writeLine(msg.String())
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func (cl *Client) loop() {
2014-11-22 13:21:30 +00:00
for {
time.Sleep(1 * time.Millisecond)
if cl.isClosed {
return
}
select {
case msg := <-cl.receive:
cl.writeMsg(msg)
default:
continue
}
}
}
2016-07-18 12:26:46 +00:00
func (cl *Client) destroy() {
2014-11-22 13:21:30 +00:00
if cl.isClosed {
return
}
cl.isClosed = true
close(cl.writeq)
2016-07-18 12:26:46 +00:00
cl.conn.Write([]byte("ERROR :Closing link"))
2014-11-22 13:21:30 +00:00
cl.conn.Close()
if cl.name != "" {
2016-07-18 12:26:46 +00:00
xlog.Info("Client '%s' disconnected", cl.name)
2014-11-22 13:21:30 +00:00
} else {
2016-07-18 12:26:46 +00:00
xlog.Info("Client disconnected")
2014-11-22 13:21:30 +00:00
}
}
2016-07-17 21:52:03 +00:00
func (cl *Client) writeLine(format string, a ...interface{}) {
2014-11-22 13:21:30 +00:00
cl.writeq <- fmt.Sprintf(format, a...)
}
2016-07-17 21:52:03 +00:00
func (cl *Client) connReader() {
2014-11-22 13:21:30 +00:00
input := bufio.NewReader(cl.conn)
for {
2016-07-18 12:26:46 +00:00
if cl.isClosed {
xlog.Debug("connReader: exiting")
return
}
2014-11-22 13:21:30 +00:00
s, err := input.ReadString('\n')
if err == io.EOF {
2016-07-18 12:26:46 +00:00
xlog.Info("connReader: Connection closed by peer")
cl.server.DelClient <- cl
2014-11-22 13:21:30 +00:00
return
}
if err != nil {
2016-07-18 12:26:46 +00:00
xlog.Error("connReader: %s", err.Error())
cl.server.DelClient <- cl
2014-11-22 13:21:30 +00:00
return
}
s = strings.Trim(s, "\r\n")
cl.handleLine(s)
}
}
2016-07-17 21:52:03 +00:00
func (cl *Client) connWriter() {
2014-11-22 13:21:30 +00:00
for line := range cl.writeq {
written := 0
bytes := []byte(line + "\r\n")
for written < len(line) {
n, err := cl.conn.Write(bytes[written:])
if err == io.EOF {
2016-07-18 12:26:46 +00:00
xlog.Info("connWriter: Connection closed by peer")
cl.server.DelClient <- cl
2014-11-22 13:21:30 +00:00
return
} else if err != nil {
2016-07-18 12:26:46 +00:00
xlog.Error("connWriter: %s", err.Error())
cl.server.DelClient <- cl
2014-11-22 13:21:30 +00:00
return
}
written += n
}
}
2016-07-18 12:26:46 +00:00
xlog.Debug("connWriter: exiting")
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
var lineFuncs = map[string]func(*Client, *irc.Message) bool{
"PASS": handleLinePass,
"NICK": handleLineNick,
"USER": handleLineUser,
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func (cl *Client) handleLine(s string) {
2016-07-18 12:26:46 +00:00
xlog.Debug("handleLine: [%s] '%s'", cl.name, s)
msg := irc.Parse(s)
hook, exists := lineFuncs[msg.Cmd]
forward := true
if exists {
forward = hook(cl, msg)
}
if forward {
cl.Send(msg)
}
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func checkAuth(cl *Client) {
2014-11-22 13:21:30 +00:00
if cl.name == "" || cl.password == "" {
return
}
}
2016-07-17 21:52:03 +00:00
func handleLinePass(cl *Client, msg *irc.Message) bool {
2014-11-22 13:21:30 +00:00
cl.password = msg.Args[0]
return false
}
2016-07-17 21:52:03 +00:00
func handleLineNick(cl *Client, msg *irc.Message) bool {
2014-11-22 13:21:30 +00:00
if cl.name != "" {
return false
}
if len(msg.Args) < 1 {
xlog.Warning("Nicksalat!")
return false
}
cl.name = msg.Args[0]
cl.server.AddClient <- cl
if registered := <-cl.register; registered {
cl.isRegistered = true
2016-07-18 12:26:46 +00:00
xlog.Info("User '%s' registered", msg.Args[0])
2014-11-22 13:21:30 +00:00
} else {
2016-07-18 12:26:46 +00:00
xlog.Error("User '%s' already connected", msg.Args[0])
cl.server.DelClient <- cl
2014-11-22 13:21:30 +00:00
}
return false
}
2016-07-17 21:52:03 +00:00
func handleLineUser(cl *Client, msg *irc.Message) bool {
2014-11-22 13:21:30 +00:00
return false
}