2016-07-17 21:52:03 +00:00
|
|
|
// vim:ts=4:sts=4:sw=4:noet:tw=72
|
|
|
|
|
2016-07-18 21:33:33 +00:00
|
|
|
package ircd
|
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-18 19:22:03 +00:00
|
|
|
type Client interface {
|
|
|
|
Name() string
|
2016-07-21 08:30:44 +00:00
|
|
|
Register(bool)
|
2016-07-18 19:22:03 +00:00
|
|
|
Send(*irc.Message)
|
|
|
|
Receive(*irc.Message)
|
|
|
|
Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteClient struct {
|
2016-07-21 08:30:44 +00:00
|
|
|
Registered chan bool
|
|
|
|
|
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-21 08:30:44 +00:00
|
|
|
receive chan *irc.Message
|
|
|
|
registered chan bool
|
2014-11-22 13:21:30 +00:00
|
|
|
|
2016-07-17 21:52:03 +00:00
|
|
|
conn net.Conn
|
|
|
|
writeq chan string
|
2014-11-22 13:21:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 16:17:13 +00:00
|
|
|
func NewRemoteClient(sv *Server, conn net.Conn) *RemoteClient {
|
2016-07-18 19:22:03 +00:00
|
|
|
cl := new(RemoteClient)
|
2016-07-21 08:30:44 +00:00
|
|
|
|
2016-07-20 16:17:13 +00:00
|
|
|
cl.server = sv
|
2014-11-22 13:21:30 +00:00
|
|
|
|
|
|
|
cl.name = ""
|
|
|
|
cl.password = ""
|
|
|
|
cl.modes = ""
|
|
|
|
|
2016-07-17 21:52:03 +00:00
|
|
|
cl.receive = make(chan *irc.Message)
|
2016-07-21 08:30:44 +00:00
|
|
|
cl.registered = make(chan bool)
|
2014-11-22 13:21:30 +00:00
|
|
|
|
|
|
|
cl.isRegistered = false
|
|
|
|
cl.isAuthed = false
|
|
|
|
cl.isClosed = false
|
|
|
|
cl.conn = conn
|
|
|
|
cl.writeq = make(chan string, 256)
|
|
|
|
|
|
|
|
go cl.connReader()
|
|
|
|
go cl.connWriter()
|
2016-07-18 19:22:03 +00:00
|
|
|
go cl.dispatch()
|
2014-11-22 13:21:30 +00:00
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
xlog.Info("RemoteClient connected")
|
2014-11-22 13:21:30 +00:00
|
|
|
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) Name() string {
|
2014-11-22 13:21:30 +00:00
|
|
|
return cl.name
|
|
|
|
}
|
|
|
|
|
2016-07-21 08:30:44 +00:00
|
|
|
func (cl *RemoteClient) Register(success bool) {
|
|
|
|
cl.registered <- success
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) Send(msg *irc.Message) {
|
2016-07-17 21:52:03 +00:00
|
|
|
msg.Pre = cl.name
|
|
|
|
cl.server.Dispatch <- msg
|
2014-11-22 13:21:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) Receive(msg *irc.Message) {
|
2014-11-22 13:21:30 +00:00
|
|
|
cl.receive <- msg
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) AddMode(mode string) {
|
2014-11-22 13:21:30 +00:00
|
|
|
cl.modes = cl.modes + mode
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) DelMode(mode string) {
|
2014-11-22 13:21:30 +00:00
|
|
|
cl.modes = strings.Replace(cl.modes, mode, "", -1)
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) HasMode(mode string) bool {
|
2014-11-22 13:21:30 +00:00
|
|
|
return strings.IndexRune(cl.modes, rune(mode[0])) != -1
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) Destroy() {
|
|
|
|
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")
|
|
|
|
}
|
2014-11-22 13:21:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) dispatch() {
|
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)
|
2016-07-21 08:30:44 +00:00
|
|
|
case success := <-cl.registered:
|
|
|
|
if !success {
|
|
|
|
cl.Destroy()
|
|
|
|
xlog.Debug("User registration failed: '%s'", cl.name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
xlog.Debug("User '%s' registered", cl.name)
|
2014-11-22 13:21:30 +00:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) 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")
|
2016-07-20 16:17:13 +00:00
|
|
|
cl.handleCmd(s)
|
2014-11-22 13:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func (cl *RemoteClient) 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-18 19:22:03 +00:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2016-07-20 16:17:13 +00:00
|
|
|
func (cl *RemoteClient) handleCmd(s string) {
|
|
|
|
xlog.Debug("handleCmd: [%s] '%s'", cl.name, s)
|
2016-07-18 12:26:46 +00:00
|
|
|
msg := irc.Parse(s)
|
2016-07-21 08:30:44 +00:00
|
|
|
msg.Cmd = strings.ToUpper(msg.Cmd)
|
2016-07-20 16:17:13 +00:00
|
|
|
if cl.name != "" {
|
2016-07-18 12:26:46 +00:00
|
|
|
cl.Send(msg)
|
2016-07-20 16:17:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// If client name is not set, intercept cmds until we get one
|
|
|
|
switch msg.Cmd {
|
|
|
|
case "PASS":
|
|
|
|
cl.password = msg.Args[0]
|
|
|
|
case "NICK":
|
|
|
|
cl.name = msg.Args[0]
|
|
|
|
cl.server.AddClient <- cl
|
2016-07-21 08:30:44 +00:00
|
|
|
|
2016-07-20 16:17:13 +00:00
|
|
|
case "USER":
|
2016-07-18 12:26:46 +00:00
|
|
|
}
|
2014-11-22 13:21:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 19:22:03 +00:00
|
|
|
func checkAuth(cl *RemoteClient) {
|
2014-11-22 13:21:30 +00:00
|
|
|
if cl.name == "" || cl.password == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|