ircd/server.go

376 lines
9.8 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 * *"
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 Server struct {
2016-07-17 21:52:03 +00:00
Dispatch chan *irc.Message
2016-07-18 19:22:03 +00:00
AddClient chan Client
DelClient chan Client
2014-11-22 13:21:30 +00:00
2016-07-20 22:03:37 +00:00
host string
info string
software string
version string
created string
motd string
clients map[string]Client
chUsers map[string]map[string]string
//chModes map[string][]string
2016-07-19 16:25:39 +00:00
chTopics map[string]string
2016-07-18 14:19:04 +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
2014-11-22 13:21:30 +00:00
}
// Create a new server instance.
func NewServer(configPath, software, version string) *Server {
2016-07-20 16:17:13 +00:00
sv := &Server{software: software, version: version, created: "yes"}
sv.Dispatch = make(chan *irc.Message, 1024)
sv.AddClient = make(chan Client, 1024)
sv.DelClient = make(chan Client, 1024)
sv.clients = make(map[string]Client)
sv.chUsers = make(map[string]map[string]string)
sv.chTopics = make(map[string]string)
sv.configPath = configPath
sv.loadConfig()
loglevel, _ := sv.config.GetInt("system", "loglevel")
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
xlog.Init(loglevel)
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
}
// 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-20 16:17:13 +00:00
sv.dispatch()
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-20 16:17:13 +00:00
func (sv *Server) dispatch() {
2014-11-22 13:21:30 +00:00
for {
2016-07-20 22:03:37 +00:00
time.Sleep(1 * time.Microsecond)
2016-07-20 16:17:13 +00:00
sv.queueLen = float64(len(sv.Dispatch))
2014-11-22 13:21:30 +00:00
select {
2016-07-20 16:17:13 +00:00
case msg := <-sv.Dispatch:
sv.recvMsg(msg)
sv.packetsTransferred++
case cl := <-sv.AddClient:
2016-07-20 22:03:37 +00:00
nick := cl.Name()
2016-07-21 08:30:44 +00:00
if _, exists := sv.clients[nick]; exists {
cl.Register(false)
xlog.Info("Client registration failed: '%s'", nick)
} else {
sv.clients[nick] = cl
sv.sendLogon(cl.Name())
sv.connectionsCurrent = float64(len(sv.clients))
cl.Register(true)
xlog.Info("Client registered: '%s'", nick)
xlog.Info("Server has %d client(s)", len(sv.clients))
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
}
2016-07-20 16:17:13 +00:00
case cl := <-sv.DelClient:
2016-07-20 22:03:37 +00:00
nick := cl.Name()
2016-07-18 19:22:03 +00:00
cl.Destroy()
for chname, ch := range sv.chUsers {
2016-07-20 22:03:37 +00:00
if _, exists := ch[nick]; exists {
delete(ch, nick)
sv.sendMsg(irc.M(nick, "PART", chname, "quit"))
2016-07-20 22:03:37 +00:00
}
}
delete(sv.clients, nick)
sv.connectionsCurrent = float64(len(sv.clients))
xlog.Info("Client deleted: '%s'", nick)
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-20 16:17:13 +00:00
ch := msg.Args[0]
if _, exists := sv.chUsers[ch]; !exists {
sv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
return
}
for nick, _ := range sv.chUsers[ch] {
if msg.Pre == nick && msg.Cmd == "PRIVMSG" {
continue
}
2016-07-20 22:03:37 +00:00
if cl, exists := sv.clients[nick]; exists {
cl.Receive(msg)
}
2016-07-20 16:17:13 +00:00
}
2014-11-22 13:21:30 +00:00
} else {
2016-07-20 16:17:13 +00:00
if _, exists := sv.clients[msg.Args[0]]; !exists {
sv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
return
2016-07-19 16:25:39 +00:00
}
2016-07-20 16:17:13 +00:00
cl := sv.clients[msg.Args[0]]
cl.Receive(msg)
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 (sv *Server) sendReply(tar, cmd, args, trail string) {
if _, exists := sv.clients[tar]; !exists {
2016-07-18 12:26:46 +00:00
return
}
2016-07-20 16:17:13 +00:00
cl := sv.clients[tar]
2016-07-18 14:19:04 +00:00
if args != "" {
2016-07-19 16:25:39 +00:00
args = tar + " " + args
2016-07-18 14:19:04 +00:00
} else {
args = tar
}
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-20 16:17:13 +00:00
fmt.Sprintf("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) channelJoin(nick, ch string) {
if _, exists := sv.chUsers[ch]; !exists {
sv.chUsers[ch] = make(map[string]string)
sv.chTopics[ch] = ""
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
if _, exists := sv.chUsers[ch][nick]; exists {
return
}
sv.chUsers[ch][nick] = ""
sv.sendMsg(irc.M(nick, "JOIN", ch, ""))
sv.sendReply(nick, RPL_TOPIC, ch, sv.chTopics[ch])
sv.channelNames(nick, ch)
2014-11-22 13:21:30 +00:00
}
2016-07-20 16:17:13 +00:00
func (sv *Server) channelPart(nick, ch, reason string) {
if _, exists := sv.chUsers[ch]; !exists {
return
2016-07-18 14:19:04 +00:00
}
2016-07-20 16:17:13 +00:00
if _, exists := sv.chUsers[ch][nick]; !exists {
2016-07-18 14:19:04 +00:00
return
}
2016-07-20 16:17:13 +00:00
sv.sendMsg(irc.M(nick, "PART", ch, reason))
delete(sv.chUsers[ch], nick)
2016-07-18 14:19:04 +00:00
}
2016-07-20 16:17:13 +00:00
func (sv *Server) channelNames(nick, ch string) {
if _, exists := sv.chUsers[ch]; !exists {
2016-07-18 17:02:15 +00:00
return
}
2016-07-20 16:17:13 +00:00
names := ""
for name, mode := range sv.chUsers[ch] {
if names != "" {
names += " "
}
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) {
sv.channelJoin(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) {
sv.channelPart(msg.Pre, msg.Args[0], msg.Trail)
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) {
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
}