ircd/server.go

342 lines
9.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
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-18 14:19:04 +00:00
host string
info string
software string
version string
created string
motd string
2016-07-18 19:22:03 +00:00
clients map[string]Client
2016-07-19 16:25:39 +00:00
chSubs map[string]map[string]string
chTopics map[string]string
2016-07-18 14:19:04 +00:00
ports map[int]bool
config *conf.ConfigFile
configPath string
2016-07-19 16:25:39 +00:00
packetsTransferred float64
clientConnections float64
2014-11-22 13:21:30 +00:00
}
func init() {
}
// Create a new server instance.
func NewServer(configPath, software, version string) *Server {
srv := &Server{software: software, version: version, created: "yes"}
2016-07-17 21:52:03 +00:00
srv.Dispatch = make(chan *irc.Message, 1024)
2016-07-18 19:22:03 +00:00
srv.AddClient = make(chan Client, 1024)
srv.DelClient = make(chan Client, 1024)
2014-11-22 13:21:30 +00:00
2016-07-18 19:22:03 +00:00
srv.clients = make(map[string]Client)
2016-07-19 16:25:39 +00:00
srv.chSubs = make(map[string]map[string]string)
srv.chTopics = make(map[string]string)
2014-11-22 13:21:30 +00:00
srv.configPath = configPath
srv.loadConfig()
2016-07-17 21:52:03 +00:00
loglevel, _ := srv.config.GetInt("system", "loglevel")
2016-07-18 12:26:46 +00:00
srv.host, _ = srv.config.GetString("server", "host")
srv.info, _ = srv.config.GetString("server", "info")
srv.motd, _ = srv.config.GetString("server", "motd")
2014-11-22 13:21:30 +00:00
xlog.Init(loglevel)
2016-07-19 16:25:39 +00:00
srv.packetsTransferred = 0
srv.clientConnections = 0
2014-11-22 13:21:30 +00:00
return srv
}
// Open the listening port and start the main server loop.
func (srv *Server) Run() {
xlog.Info("%s/%s", srv.software, srv.version)
2016-07-19 16:25:39 +00:00
go monitoringRun(srv)
laddr, _ := srv.config.GetString("net", "listen_ircd")
go srv.listen(laddr)
2016-07-17 21:52:03 +00:00
srv.dispatch()
2014-11-22 13:21:30 +00:00
}
2016-07-19 16:25:39 +00:00
func (srv *Server) listen(laddr string) {
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)
}
2016-07-19 16:25:39 +00:00
xlog.Info("Start listening on %s", laddr)
2016-07-18 12:26:46 +00:00
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-18 19:22:03 +00:00
NewRemoteClient(srv, conn)
2016-07-19 16:25:39 +00:00
srv.clientConnections++
2016-07-18 12:26:46 +00:00
}
}
}
2016-07-17 21:52:03 +00:00
func (srv *Server) dispatch() {
xlog.Debug("Entering msg dispatcher")
2014-11-22 13:21:30 +00:00
for {
time.Sleep(1 * time.Millisecond)
select {
2016-07-17 21:52:03 +00:00
case msg := <-srv.Dispatch:
2014-11-22 13:21:30 +00:00
srv.recvMsg(msg)
2016-07-19 16:25:39 +00:00
srv.packetsTransferred++
2014-11-22 13:21:30 +00:00
case cl := <-srv.AddClient:
2016-07-17 21:52:03 +00:00
name := cl.Name()
srv.clients[name] = cl
2016-07-19 16:25:39 +00:00
srv.clientLogon(cl)
srv.clientMotd(cl)
2016-07-17 21:52:03 +00:00
xlog.Info("Client registered: '%s'", name)
xlog.Info("Server has %d client(s)", len(srv.clients))
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
2014-11-22 13:21:30 +00:00
case cl := <-srv.DelClient:
2016-07-17 21:52:03 +00:00
name := cl.Name()
2016-07-18 19:22:03 +00:00
cl.Destroy()
2016-07-17 21:52:03 +00:00
delete(srv.clients, name)
xlog.Info("Client deleted: '%s'", name)
xlog.Info("Server has %d client(s)", len(srv.clients))
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
2014-11-22 13:21:30 +00:00
default:
}
}
}
func (srv *Server) loadConfig() {
2016-07-17 21:52:03 +00:00
cfg, err := conf.ReadConfigFile(srv.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-17 21:52:03 +00:00
srv.config = cfg
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func (srv *Server) recvMsg(msg *irc.Message) {
2014-11-22 13:21:30 +00:00
cmd := msg.Cmd
hook, exists := srvCommandHooks[cmd]
if !exists {
2016-07-18 12:26:46 +00:00
srv.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-18 12:26:46 +00:00
srv.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 == "" {
srv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
return
}
2014-11-22 13:21:30 +00:00
hook.HookFn(srv, msg)
}
2016-07-17 21:52:03 +00:00
func (srv *Server) sendMsg(msg *irc.Message) {
if strings.HasPrefix(msg.Args[0], "#") {
2016-07-19 16:25:39 +00:00
srv.bcChan(msg)
2014-11-22 13:21:30 +00:00
} else {
2016-07-19 16:25:39 +00:00
srv.sendClient(msg)
2014-11-22 13:21:30 +00:00
}
}
2016-07-19 16:25:39 +00:00
func (srv *Server) sendClient(msg *irc.Message) {
if _, exists := srv.clients[msg.Args[0]]; !exists {
xlog.Debug("sendClient: Client '%s' does not exist", msg.Args[0])
srv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
2016-07-17 21:52:03 +00:00
return
}
2016-07-19 16:25:39 +00:00
cl := srv.clients[msg.Args[0]]
cl.Receive(msg)
2014-11-22 13:21:30 +00:00
}
2016-07-19 16:25:39 +00:00
func (srv *Server) bcChan(msg *irc.Message) {
ch := msg.Args[0]
if _, exists := srv.chSubs[ch]; !exists {
xlog.Error("bcChan: Channel '%s' does not exist", ch)
2014-11-22 13:21:30 +00:00
return
}
2016-07-19 16:25:39 +00:00
for nick, _ := range srv.chSubs[ch] {
if msg.Pre == nick && msg.Cmd == "PRIVMSG" {
continue
}
srv.clients[nick].Receive(msg)
}
2014-11-22 13:21:30 +00:00
}
2016-07-18 12:26:46 +00:00
func (srv *Server) sendReply(tar, cmd, args, trail string) {
if _, exists := srv.clients[tar]; !exists {
xlog.Error("sendReply: Client '%s' does not exist", tar)
return
}
cl := srv.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
}
cl.Receive(irc.M(srv.host, cmd, args, trail))
2014-11-22 13:21:30 +00:00
}
2016-07-18 19:22:03 +00:00
func (srv *Server) clientLogon(cl Client) {
2016-07-18 12:26:46 +00:00
srv.sendReply(cl.Name(), RPL_WELCOME, "", "Willkommen!")
srv.sendReply(cl.Name(), RPL_YOURHOST, "",
2016-07-17 21:52:03 +00:00
fmt.Sprintf("Your host is %s, running on %s/%s",
2016-07-18 12:26:46 +00:00
srv.host, srv.software, srv.version))
srv.sendReply(cl.Name(), RPL_CREATED, "",
fmt.Sprintf("Created: %s", srv.created))
srv.sendReply(cl.Name(), RPL_MYINFO, "",
fmt.Sprintf(myinfo, srv.host, srv.software, srv.version))
srv.sendReply(cl.Name(), RPL_ISUPPORT, "",
isupport+" are supported by this server")
2014-11-22 13:21:30 +00:00
}
2016-07-18 19:22:03 +00:00
func (srv *Server) clientMotd(cl Client) {
2016-07-18 12:26:46 +00:00
srv.sendReply(cl.Name(), RPL_MOTDSTART, "",
fmt.Sprintf("- %s Message of the day -", srv.host))
2014-11-22 13:21:30 +00:00
for _, line := range strings.Split(srv.motd, "\n") {
2016-07-18 12:26:46 +00:00
srv.sendReply(cl.Name(), RPL_MOTD, "", fmt.Sprintf("- %s", line))
2014-11-22 13:21:30 +00:00
}
2016-07-18 12:26:46 +00:00
srv.sendReply(cl.Name(), RPL_ENDOFMOTD, "", "End of MOTD command")
2014-11-22 13:21:30 +00:00
}
2016-07-18 14:19:04 +00:00
func (srv *Server) channelJoin(nick, ch string) {
2016-07-19 16:25:39 +00:00
if _, exists := srv.chSubs[ch]; !exists {
srv.chSubs[ch] = make(map[string]string)
srv.chTopics[ch] = ""
2016-07-18 14:19:04 +00:00
}
2016-07-19 16:25:39 +00:00
if _, exists := srv.chSubs[ch][nick]; exists {
2016-07-18 14:19:04 +00:00
return
}
2016-07-19 16:25:39 +00:00
srv.chSubs[ch][nick] = ""
srv.bcChan(irc.M(nick, "JOIN", ch, ""))
srv.sendReply(nick, RPL_TOPIC, ch, srv.chTopics[ch])
2016-07-18 14:19:04 +00:00
}
2016-07-18 17:02:15 +00:00
func (srv *Server) channelPart(nick, ch, reason string) {
2016-07-19 16:25:39 +00:00
if _, exists := srv.chSubs[ch]; !exists {
2016-07-18 17:02:15 +00:00
return
}
2016-07-19 16:25:39 +00:00
if _, exists := srv.chSubs[ch][nick]; !exists {
2016-07-18 17:02:15 +00:00
return
}
2016-07-19 16:25:39 +00:00
srv.bcChan(irc.M(nick, "PART", ch, reason))
delete(srv.chSubs[ch], nick)
2016-07-18 14:19:04 +00:00
}
2014-11-22 13:21:30 +00:00
type SrvCommandHook struct {
2016-07-19 16:25:39 +00:00
HookFn func(srv *Server, msg *irc.Message)
MinArgs int
NeedTrail bool
NeedOper bool
NeedAuth bool
2014-11-22 13:21:30 +00:00
}
var srvCommandHooks = map[string]SrvCommandHook{
2016-07-19 16:25:39 +00:00
"PRIVMSG": {srvHandleCmdPrivmsg, 1, true, false, false},
"JOIN": {srvHandleCmdJoin, 1, false, false, false},
"PART": {srvHandleCmdPart, 1, false, false, false},
"QUIT": {srvHandleCmdQuit, 0, false, false, false},
"MODE": {srvHandleCmdMode, 1, false, false, false},
"TOPIC": {srvHandleCmdTopic, 1, false, false, false},
"WHOIS": {srvHandleCmdWhois, 0, false, false, false},
"PING": {srvHandleCmdPing, 1, false, false, false},
"REHASH": {srvHandleCmdRehash, 0, false, false, false},
/*
"LIST": {srvHandleCmdList, 0, false, false},
"VERSION": {srvHandleCmdVersion, 0, false, false},
"STATS": {srvHandleCmdStats, 0, false, false},
"TIME": {srvHandleCmdTime, 0, false, false},
"OPER": {srvHandleCmdOper, 1, false, false},
"ADMIN": {srvHandleCmdAdmin, 0, false, false},
"INFO": {srvHandleCmdInfo, 0, false, false},
"WHO": {srvHandleCmdWho, 0, false, false},
"WHOWAS": {srvHandleCmdWhowas, 0, false, false},
"KILL": {srvHandleCmdKill, 0, false, false},
"PONG": {srvHandleCmdPong, 0, false, false},
"ERROR": {srvHandleCmdError, 0, false, false},
"AWAY": {srvHandleCmdAway, 0, false, false},
"RESTART": {srvHandleCmdRestart, 0, false, false},
"SUMMON": {srvHandleCmdSummon, 0, false, false},
"USERS": {srvHandleCmdUsers, 0, false, false},
"USERHOST": {srvHandleCmdUserhost, 0, false, false},
"ISON": {srvHandleCmdIson, 0, false, false},
*/
2014-11-22 13:21:30 +00:00
}
2016-07-18 12:26:46 +00:00
func srvHandleCmdPrivmsg(srv *Server, msg *irc.Message) {
srv.sendMsg(msg)
}
func srvHandleCmdJoin(srv *Server, msg *irc.Message) {
2016-07-18 14:19:04 +00:00
srv.channelJoin(msg.Pre, msg.Args[0])
2016-07-18 12:26:46 +00:00
}
func srvHandleCmdPart(srv *Server, msg *irc.Message) {
2016-07-18 19:22:03 +00:00
srv.channelPart(msg.Pre, msg.Args[0], msg.Trail)
2016-07-18 12:26:46 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdQuit(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-19 16:25:39 +00:00
func srvHandleCmdMode(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-19 16:25:39 +00:00
func srvHandleCmdTopic(srv *Server, msg *irc.Message) {
ch := msg.Args[0]
if _, exists := srv.chSubs[ch]; !exists {
srv.sendReply(msg.Pre, ERR_NOSUCHCHANNEL, ch, "No such channel")
}
if msg.Trail == "" {
srv.sendReply(msg.Pre, RPL_TOPIC, ch, srv.chTopics[ch])
} else {
srv.chTopics[ch] = msg.Trail
srv.bcChan(msg)
//srv.sendReply(msg.Pre, RPL_TOPIC, ch, msg.Trail)
}
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdWhois(srv *Server, msg *irc.Message) {
2016-07-18 12:26:46 +00:00
srv.sendReply(msg.Pre, RPL_WHOISUSER, "nick user host *", "real name")
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdPing(srv *Server, msg *irc.Message) {
2016-07-18 12:26:46 +00:00
srv.sendReply(msg.Pre, "PONG", msg.Args[0], "")
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdRehash(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
srv.loadConfig()
2016-07-18 12:26:46 +00:00
srv.sendReply(msg.Pre, RPL_REHASHING, "", "Rehashing.")
xlog.Info("Rehashing")
2014-11-22 13:21:30 +00:00
}