ircd/server.go

433 lines
11 KiB
Go
Raw Normal View History

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 (
"fmt"
"net"
"os"
2016-07-17 21:52:03 +00:00
"runtime"
2014-11-22 13:21:30 +00:00
"strings"
"time"
2016-07-17 21:52:03 +00:00
"code.dnix.de/an/conf"
"code.dnix.de/an/irc"
"code.dnix.de/an/xlog"
2014-11-22 13:21:30 +00:00
)
const (
CHANLIMIT = 1024
CHANNELLEN = 200
TOPICLEN = 1024
)
var myinfo string = "%s %s/%s * *"
2016-07-22 13:56:50 +00:00
var isupport string = "CASEMAPPING=rfc1459 CHANTYPES=# NICKLEN=32 PREFIX=(aohv)&@%%+"
2014-11-22 13:21:30 +00:00
type Server struct {
2016-07-24 20:15:52 +00:00
queue chan *irc.Message
addq chan Client
delq chan Client
host string
info string
software string
version string
created string
motd string
clients map[string]Client
chUsers map[string]map[string]string
chTopics map[string]string
chModes map[string]map[string]bool
2016-07-24 20:15:52 +00:00
config *conf.ConfigFile
configPath string
2016-07-19 16:25:39 +00:00
packetsTransferred float64
2016-07-20 22:03:37 +00:00
connectionsCurrent float64
connectionsCount float64
2016-07-20 16:17:13 +00:00
queueLen float64
2016-07-25 16:48:41 +00:00
authCallback func(name, pass string) bool
2014-11-22 13:21:30 +00:00
}
// Create a new server instance.
func NewServer(configPath, software, version string) *Server {
2016-07-24 20:15:52 +00:00
sv := &Server{software: software, version: version,
created: time.Now().String()}
2016-07-20 16:17:13 +00:00
2016-07-21 22:00:35 +00:00
sv.queue = make(chan *irc.Message, 1024)
2016-07-24 20:15:52 +00:00
sv.addq = make(chan Client, 128)
sv.delq = make(chan Client, 128)
2016-07-20 16:17:13 +00:00
sv.clients = make(map[string]Client)
sv.chUsers = make(map[string]map[string]string)
sv.chTopics = make(map[string]string)
sv.chModes = make(map[string]map[string]bool)
2016-07-24 20:15:52 +00:00
2016-07-20 16:17:13 +00:00
sv.configPath = configPath
sv.loadConfig()
2016-07-24 20:15:52 +00:00
2016-07-20 16:17:13 +00:00
loglevel, _ := sv.config.GetInt("system", "loglevel")
2016-07-24 20:15:52 +00:00
xlog.Init(loglevel)
2016-07-20 16:17:13 +00:00
sv.host, _ = sv.config.GetString("server", "host")
sv.info, _ = sv.config.GetString("server", "info")
sv.motd, _ = sv.config.GetString("server", "motd")
2014-11-22 13:21:30 +00:00
2016-07-20 16:17:13 +00:00
sv.packetsTransferred = 0
2016-07-20 22:03:37 +00:00
sv.connectionsCurrent = 0
sv.connectionsCount = 0
2016-07-20 16:17:13 +00:00
sv.queueLen = 0
2016-07-19 16:25:39 +00:00
2016-07-20 16:17:13 +00:00
return sv
2014-11-22 13:21:30 +00:00
}
2016-07-25 16:48:41 +00:00
func (sv *Server) SetAuthCallback(authCB func(name, pass string) bool) {
sv.authCallback = authCB
}
2014-11-22 13:21:30 +00:00
// Open the listening port and start the main server loop.
2016-07-20 16:17:13 +00:00
func (sv *Server) Run() {
xlog.Info("%s/%s", sv.software, sv.version)
go monitoringRun(sv)
2016-07-20 21:14:49 +00:00
laddr, err := sv.config.GetString("net", "listen")
if err == nil {
go sv.listen(laddr)
}
laddr, err = sv.config.GetString("net", "listen_tls")
if err == nil {
go sv.listenTls(laddr)
}
2016-07-21 22:00:35 +00:00
sv.dispatcher()
}
func (sv *Server) Dispatch(msg *irc.Message) {
sv.queue <- msg
}
func (sv *Server) AddClient(cl Client) {
sv.addq <- cl
}
func (sv *Server) DelClient(cl Client) {
sv.delq <- cl
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func (sv *Server) listen(laddr string) {
2016-07-19 16:25:39 +00:00
listen, err := net.Listen("tcp", laddr)
2016-07-18 12:26:46 +00:00
if err != nil {
2016-07-19 16:25:39 +00:00
xlog.Fatal(err.Error())
2016-07-18 12:26:46 +00:00
os.Exit(-1)
}
for {
2016-07-19 16:25:39 +00:00
time.Sleep(1 * time.Millisecond)
2016-07-18 12:26:46 +00:00
conn, err := listen.Accept()
if err != nil {
xlog.Error(err.Error())
} else {
2016-07-20 16:17:13 +00:00
NewRemoteClient(sv, conn)
2016-07-20 22:03:37 +00:00
sv.connectionsCount++
2016-07-18 12:26:46 +00:00
}
}
}
2016-07-20 21:14:49 +00:00
func (sv *Server) listenTls(laddr string) {
}
2016-07-21 22:00:35 +00:00
func (sv *Server) dispatcher() {
2014-11-22 13:21:30 +00:00
for {
2016-07-20 22:03:37 +00:00
time.Sleep(1 * time.Microsecond)
2016-07-21 22:00:35 +00:00
sv.queueLen = float64(len(sv.queue))
2014-11-22 13:21:30 +00:00
select {
2016-07-21 22:00:35 +00:00
case msg := <-sv.queue:
2016-07-20 16:17:13 +00:00
sv.recvMsg(msg)
sv.packetsTransferred++
2016-07-21 22:00:35 +00:00
case cl := <-sv.addq:
2016-07-22 13:56:50 +00:00
clid := strings.ToLower(cl.Name())
if _, exists := sv.clients[clid]; exists {
2016-07-21 08:30:44 +00:00
cl.Register(false)
2016-07-25 16:48:41 +00:00
xlog.Info("Client registration failed: '%s' (client exists)", clid)
continue
}
if !sv.authCallback(cl.Name(), cl.Password()) {
cl.Register(false)
xlog.Info("Client registration failed: '%s' (wrong password)", clid)
continue
2016-07-21 08:30:44 +00:00
}
2016-07-25 16:48:41 +00:00
sv.clients[clid] = cl
sv.sendLogon(cl.Name())
sv.connectionsCurrent = float64(len(sv.clients))
cl.Register(true)
xlog.Info("Client registered: '%s'", clid)
xlog.Info("Server has %d client(s)", len(sv.clients))
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
2016-07-21 22:00:35 +00:00
case cl := <-sv.delq:
2016-07-22 13:56:50 +00:00
clid := strings.ToLower(cl.Name())
2016-07-18 19:22:03 +00:00
cl.Destroy()
for chname, ch := range sv.chUsers {
2016-07-22 13:56:50 +00:00
if _, exists := ch[clid]; exists {
delete(ch, clid)
sv.sendMsg(irc.M(cl.Name(), "PART", chname, "quit"))
2016-07-20 22:03:37 +00:00
}
}
2016-07-22 13:56:50 +00:00
delete(sv.clients, clid)
2016-07-20 22:03:37 +00:00
sv.connectionsCurrent = float64(len(sv.clients))
2016-07-22 13:56:50 +00:00
xlog.Info("Client deleted: '%s'", clid)
2016-07-20 16:17:13 +00:00
xlog.Info("Server has %d client(s)", len(sv.clients))
2016-07-17 21:52:03 +00:00
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
2014-11-22 13:21:30 +00:00
default:
}
}
}
2016-07-20 16:17:13 +00:00
func (sv *Server) loadConfig() {
cfg, err := conf.ReadConfigFile(sv.configPath)
2014-11-22 13:21:30 +00:00
if err != nil {
2016-07-17 21:52:03 +00:00
xlog.Fatal("Can't read config file (%s)", err.Error())
2014-11-22 13:21:30 +00:00
os.Exit(-1)
}
2016-07-20 16:17:13 +00:00
sv.config = cfg
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func (sv *Server) recvMsg(msg *irc.Message) {
2014-11-22 13:21:30 +00:00
cmd := msg.Cmd
2016-07-20 16:17:13 +00:00
hook, exists := svCommandHooks[cmd]
2014-11-22 13:21:30 +00:00
if !exists {
2016-07-20 16:17:13 +00:00
sv.sendReply(msg.Pre, ERR_UNKNOWNCOMMAND, cmd, "Unknown command")
2014-11-22 13:21:30 +00:00
return
}
argc := len(msg.Args)
if argc < hook.MinArgs {
2016-07-20 16:17:13 +00:00
sv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
2014-11-22 13:21:30 +00:00
return
}
2016-07-19 16:25:39 +00:00
if hook.NeedTrail && msg.Trail == "" {
2016-07-20 16:17:13 +00:00
sv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
2016-07-19 16:25:39 +00:00
return
}
2016-07-20 16:17:13 +00:00
hook.HookFn(sv, msg)
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func (sv *Server) sendMsg(msg *irc.Message) {
2016-07-17 21:52:03 +00:00
if strings.HasPrefix(msg.Args[0], "#") {
2016-07-22 13:56:50 +00:00
chid := strings.ToLower(msg.Args[0])
if _, exists := sv.chUsers[chid]; !exists {
2016-07-20 16:17:13 +00:00
sv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
return
}
2016-07-22 13:56:50 +00:00
for clid, _ := range sv.chUsers[chid] {
if strings.ToLower(msg.Pre) == clid && msg.Cmd == "PRIVMSG" {
2016-07-20 16:17:13 +00:00
continue
}
2016-07-22 13:56:50 +00:00
if cl, exists := sv.clients[clid]; exists {
2016-07-20 22:03:37 +00:00
cl.Receive(msg)
}
2016-07-20 16:17:13 +00:00
}
2014-11-22 13:21:30 +00:00
} else {
2016-07-22 13:56:50 +00:00
clid := strings.ToLower(msg.Args[0])
if _, exists := sv.clients[clid]; !exists {
2016-07-20 16:17:13 +00:00
sv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
return
2016-07-19 16:25:39 +00:00
}
2016-07-22 13:56:50 +00:00
cl := sv.clients[clid]
2016-07-20 16:17:13 +00:00
cl.Receive(msg)
2016-07-19 16:25:39 +00:00
}
2014-11-22 13:21:30 +00:00
}
2016-07-21 22:00:35 +00:00
func (sv *Server) sendReply(nick, cmd, args, trail string) {
2016-07-22 13:56:50 +00:00
clid := strings.ToLower(nick)
if _, exists := sv.clients[clid]; !exists {
2016-07-18 12:26:46 +00:00
return
}
2016-07-22 13:56:50 +00:00
cl := sv.clients[clid]
2016-07-18 14:19:04 +00:00
if args != "" {
2016-07-21 22:00:35 +00:00
args = nick + " " + args
2016-07-18 14:19:04 +00:00
} else {
2016-07-21 22:00:35 +00:00
args = nick
2016-07-18 14:19:04 +00:00
}
2016-07-20 16:17:13 +00:00
cl.Receive(irc.M(sv.host, cmd, args, trail))
2014-11-22 13:21:30 +00:00
}
2016-07-20 21:14:49 +00:00
func (sv *Server) sendLogon(nick string) {
sv.sendReply(nick, RPL_WELCOME, "", "Willkommen!")
sv.sendReply(nick, RPL_YOURHOST, "",
2016-07-17 21:52:03 +00:00
fmt.Sprintf("Your host is %s, running on %s/%s",
2016-07-20 16:17:13 +00:00
sv.host, sv.software, sv.version))
2016-07-20 21:14:49 +00:00
sv.sendReply(nick, RPL_CREATED, "",
2016-07-24 20:15:52 +00:00
fmt.Sprintf("This server was created %s", sv.created))
2016-07-20 21:14:49 +00:00
sv.sendReply(nick, RPL_MYINFO, "",
2016-07-20 16:17:13 +00:00
fmt.Sprintf(myinfo, sv.host, sv.software, sv.version))
2016-07-20 21:14:49 +00:00
sv.sendReply(nick, RPL_ISUPPORT, "",
2016-07-18 12:26:46 +00:00
isupport+" are supported by this server")
2016-07-20 21:14:49 +00:00
sv.sendReply(nick, RPL_MOTDSTART, "",
2016-07-20 16:17:13 +00:00
fmt.Sprintf("- %s Message of the day -", sv.host))
for _, line := range strings.Split(sv.motd, "\n") {
2016-07-20 21:14:49 +00:00
sv.sendReply(nick, RPL_MOTD, "", fmt.Sprintf("- %s", line))
2016-07-20 16:17:13 +00:00
}
2016-07-20 21:14:49 +00:00
sv.sendReply(nick, RPL_ENDOFMOTD, "", "End of MOTD command")
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func (sv *Server) channelNames(nick, ch string) {
2016-07-22 13:56:50 +00:00
chid := strings.ToLower(ch)
if _, exists := sv.chUsers[chid]; !exists {
2016-07-18 17:02:15 +00:00
return
}
2016-07-20 16:17:13 +00:00
names := ""
2016-07-22 13:56:50 +00:00
for clid, mode := range sv.chUsers[chid] {
name := sv.clients[clid].Name()
2016-07-20 16:17:13 +00:00
if names != "" {
names += " "
}
2016-07-22 13:56:50 +00:00
names = names + mode + name
2016-07-18 17:02:15 +00:00
}
2016-07-20 16:17:13 +00:00
sv.sendReply(nick, RPL_NAMEREPLY, "= "+ch, names)
sv.sendReply(nick, RPL_ENDOFNAMES, ch, "End of /NAMES list")
2016-07-18 14:19:04 +00:00
}
2016-07-20 16:17:13 +00:00
type commandHook struct {
HookFn func(sv *Server, msg *irc.Message)
2016-07-19 16:25:39 +00:00
MinArgs int
NeedTrail bool
NeedOper bool
NeedAuth bool
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
var svCommandHooks = map[string]commandHook{
"PRIVMSG": {handleCmdPrivmsg, 1, true, false, false},
"JOIN": {handleCmdJoin, 1, false, false, false},
"PART": {handleCmdPart, 1, false, false, false},
"QUIT": {handleCmdQuit, 0, false, false, false},
"MODE": {handleCmdMode, 1, false, false, false},
"TOPIC": {handleCmdTopic, 1, false, false, false},
"NAMES": {handleCmdNames, 1, false, false, false},
"WHOIS": {handleCmdWhois, 0, false, false, false},
"PING": {handleCmdPing, 1, false, false, false},
"REHASH": {handleCmdRehash, 0, false, false, false},
2016-07-19 16:25:39 +00:00
/*
2016-07-20 16:17:13 +00:00
"LIST": {handleCmdList, 0, false, false},
"VERSION": {handleCmdVersion, 0, false, false},
"STATS": {handleCmdStats, 0, false, false},
"TIME": {handleCmdTime, 0, false, false},
"OPER": {handleCmdOper, 1, false, false},
"ADMIN": {handleCmdAdmin, 0, false, false},
"INFO": {handleCmdInfo, 0, false, false},
"WHO": {handleCmdWho, 0, false, false},
"WHOWAS": {handleCmdWhowas, 0, false, false},
"KILL": {handleCmdKill, 0, false, false},
"PONG": {handleCmdPong, 0, false, false},
"ERROR": {handleCmdError, 0, false, false},
"AWAY": {handleCmdAway, 0, false, false},
"RESTART": {handleCmdRestart, 0, false, false},
"SUMMON": {handleCmdSummon, 0, false, false},
"USERS": {handleCmdUsers, 0, false, false},
"USERHOST": {handleCmdUserhost, 0, false, false},
"ISON": {handleCmdIson, 0, false, false},
2016-07-19 16:25:39 +00:00
*/
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdPrivmsg(sv *Server, msg *irc.Message) {
sv.sendMsg(msg)
2016-07-18 12:26:46 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdJoin(sv *Server, msg *irc.Message) {
2016-07-24 20:15:52 +00:00
clid := strings.ToLower(msg.Pre)
chid := strings.ToLower(msg.Args[0])
if _, exists := sv.chUsers[chid]; !exists {
sv.chUsers[chid] = make(map[string]string)
sv.chTopics[chid] = ""
2016-07-25 10:09:43 +00:00
sv.chModes[chid] = make(map[string]bool)
2016-07-24 20:15:52 +00:00
}
if _, exists := sv.chUsers[chid][clid]; exists {
return
}
sv.chUsers[chid][clid] = ""
sv.sendMsg(msg)
sv.sendReply(msg.Pre, RPL_TOPIC, msg.Args[0], sv.chTopics[msg.Args[0]])
sv.channelNames(msg.Pre, msg.Args[0])
2016-07-18 12:26:46 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdPart(sv *Server, msg *irc.Message) {
2016-07-24 20:15:52 +00:00
clid := strings.ToLower(msg.Pre)
chid := strings.ToLower(msg.Args[0])
if _, exists := sv.chUsers[chid]; !exists {
return
}
if _, exists := sv.chUsers[chid][clid]; !exists {
return
}
sv.sendMsg(msg)
delete(sv.chUsers[chid], clid)
2016-07-18 12:26:46 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdQuit(sv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdMode(sv *Server, msg *irc.Message) {
if strings.HasPrefix(msg.Args[0], "#") {
chid := strings.ToLower(msg.Args[0])
2016-07-25 10:10:57 +00:00
if _, exists := sv.chUsers[chid]; !exists {
return
}
2016-07-25 10:17:29 +00:00
if len(msg.Args) < 2 {
return
}
modeFlag := strings.ToLower(msg.Args[1])
2016-07-25 10:17:29 +00:00
if len(msg.Args) < 3 {
//modeTar := ""
} else {
2016-07-25 10:17:29 +00:00
//modeTar := strings.ToLower(msg.Args[2])
}
switch modeFlag {
case "+o":
case "-o":
case "+h":
case "-h":
case "+b":
case "-b":
case "+v":
case "-v":
}
sv.sendMsg(msg)
}
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdTopic(sv *Server, msg *irc.Message) {
2016-07-19 16:25:39 +00:00
ch := msg.Args[0]
2016-07-20 16:17:13 +00:00
if _, exists := sv.chUsers[ch]; !exists {
sv.sendReply(msg.Pre, ERR_NOSUCHCHANNEL, ch, "No such channel")
2016-07-19 16:25:39 +00:00
}
if msg.Trail == "" {
2016-07-20 16:17:13 +00:00
sv.sendReply(msg.Pre, RPL_TOPIC, ch, sv.chTopics[ch])
2016-07-19 16:25:39 +00:00
} else {
2016-07-20 16:17:13 +00:00
sv.chTopics[ch] = msg.Trail
sv.sendMsg(msg)
//sv.sendReply(msg.Pre, RPL_TOPIC, ch, msg.Trail)
}
}
func handleCmdNames(sv *Server, msg *irc.Message) {
ch := msg.Args[0]
if _, exists := sv.chUsers[ch]; !exists {
sv.sendReply(msg.Pre, ERR_NOSUCHCHANNEL, ch, "No such channel")
2016-07-19 16:25:39 +00:00
}
2016-07-20 16:17:13 +00:00
sv.channelNames(msg.Pre, ch)
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdWhois(sv *Server, msg *irc.Message) {
sv.sendReply(msg.Pre, RPL_WHOISUSER, "nick user host *", "real name")
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdPing(sv *Server, msg *irc.Message) {
sv.sendReply(msg.Pre, "PONG", msg.Args[0], "")
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func handleCmdRehash(sv *Server, msg *irc.Message) {
sv.loadConfig()
sv.sendReply(msg.Pre, RPL_REHASHING, "", "Rehashing.")
2016-07-18 12:26:46 +00:00
xlog.Info("Rehashing")
2014-11-22 13:21:30 +00:00
}