ircd/server.go

337 lines
8.4 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 (
"fmt"
"net"
"os"
2016-07-17 21:52:03 +00:00
"runtime"
2014-11-22 13:21:30 +00:00
"strconv"
"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 flagConfig string
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 ControlMsg struct {
Opcode int
Client *Client
}
type Server struct {
2016-07-17 21:52:03 +00:00
Dispatch chan *irc.Message
AddClient chan *Client
DelClient chan *Client
2014-11-22 13:21:30 +00:00
Host string
2016-07-17 21:52:03 +00:00
info string
software string
version string
created string
motd string
clients map[string]*Client
subscriptions map[string][]string
ports map[int]bool
config *conf.ConfigFile
configPath string
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)
srv.AddClient = make(chan *Client, 1024)
srv.DelClient = make(chan *Client, 1024)
2014-11-22 13:21:30 +00:00
srv.Host = "dnix.de"
2016-07-17 21:52:03 +00:00
srv.clients = make(map[string]*Client)
srv.subscriptions = make(map[string][]string)
2014-11-22 13:21:30 +00:00
srv.info = ""
srv.motd = `foo bar baz.`
srv.configPath = configPath
srv.loadConfig()
2016-07-17 21:52:03 +00:00
loglevel, _ := srv.config.GetInt("system", "loglevel")
2014-11-22 13:21:30 +00:00
xlog.Init(loglevel)
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-17 21:52:03 +00:00
srv.Host, _ = srv.config.GetString("server", "host")
srv.info, _ = srv.config.GetString("server", "info")
port, _ := srv.config.GetInt("net", "port")
2014-11-22 13:21:30 +00:00
go srv.listen(port)
2016-07-17 21:52:03 +00:00
srv.dispatch()
2014-11-22 13:21:30 +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)
case cl := <-srv.AddClient:
2016-07-17 21:52:03 +00:00
name := cl.Name()
if _, exists := srv.clients[name]; exists {
xlog.Warning("Client registration failed: '%s'", name)
2014-11-22 13:21:30 +00:00
go func() { cl.Register() <- false }()
continue
}
go func() { cl.Register() <- true }()
2016-07-17 21:52:03 +00:00
srv.clients[name] = cl
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
srv.clientLogon(cl)
srv.clientMotd(cl)
case cl := <-srv.DelClient:
2016-07-17 21:52:03 +00:00
name := cl.Name()
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) listen(port int) {
if _, exists := srv.ports[port]; exists {
2016-07-17 21:52:03 +00:00
xlog.Warning("Port %i already opened", port)
2014-11-22 13:21:30 +00:00
}
listen, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
2016-07-17 21:52:03 +00:00
xlog.Fatal("Cannot listen on port %i (%s), exiting",
2014-11-22 13:21:30 +00:00
port, err.Error())
os.Exit(-1)
}
2016-07-17 21:52:03 +00:00
xlog.Info("Start listening on port %d", port)
2014-11-22 13:21:30 +00:00
for {
time.Sleep(1e6)
conn, err := listen.Accept()
if err != nil {
2016-07-17 21:52:03 +00:00
xlog.Error(err.Error())
} else {
NewClient(srv, conn)
2014-11-22 13:21:30 +00:00
}
}
}
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) {
if msg.Args[0] != "" {
2014-11-22 13:21:30 +00:00
srv.sendMsg(msg)
return
}
cmd := msg.Cmd
hook, exists := srvCommandHooks[cmd]
if !exists {
2016-07-17 21:52:03 +00:00
srv.sendCommand(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-17 21:52:03 +00:00
srv.sendCommand(msg.Pre, ERR_NEEDMOREPARAMS, cmd,
"Not enough parameters")
2014-11-22 13:21:30 +00:00
return
}
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], "#") {
2014-11-22 13:21:30 +00:00
srv.sendMsgToChannel(msg)
} else {
srv.sendMsgToClient(msg)
}
}
2016-07-17 21:52:03 +00:00
func (srv *Server) sendMsgToChannel(msg *irc.Message) {
subj := msg.Args[0]
if _, exists := srv.subscriptions[subj]; !exists {
return
}
subs := srv.subscriptions[subj]
for _, sub := range subs {
if _, exists := srv.clients[sub]; !exists {
continue
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
cl := srv.clients[sub]
cl.Receive(msg)
2014-11-22 13:21:30 +00:00
}
}
2016-07-17 21:52:03 +00:00
func (srv *Server) sendMsgToClient(msg *irc.Message) {
if _, exists := srv.clients[msg.Args[0]]; !exists {
xlog.Error("Client '%s' does not exist", msg.Args[0])
2014-11-22 13:21:30 +00:00
return
}
2016-07-17 21:52:03 +00:00
cl := srv.clients[msg.Args[0]]
2014-11-22 13:21:30 +00:00
cl.Receive(msg)
}
2016-07-17 21:52:03 +00:00
func (srv *Server) sendCommand(dst, cmd, args, trail string) {
srv.sendMsg(irc.M(srv.Host, cmd, args, trail))
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func (srv *Server) sendClient(cl *Client, cmd, args, trail string) {
srv.sendMsg(irc.M(srv.Host, cmd, cl.Name()+" "+args, trail))
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func (srv *Server) clientLogon(cl *Client) {
2014-11-22 13:21:30 +00:00
srv.sendClient(cl, RPL_WELCOME, "", "Willkommen!")
srv.sendClient(cl, RPL_YOURHOST, "",
2016-07-17 21:52:03 +00:00
fmt.Sprintf("Your host is %s, running on %s/%s",
2014-11-22 13:21:30 +00:00
srv.Host, srv.software, srv.version))
2016-07-17 21:52:03 +00:00
srv.sendClient(cl, RPL_CREATED, "", fmt.Sprintf("Created: %s", srv.created))
2014-11-22 13:21:30 +00:00
srv.sendClient(cl, RPL_MYINFO,
fmt.Sprintf(myinfo, srv.Host, srv.software, srv.version), "")
2016-07-17 21:52:03 +00:00
srv.sendClient(cl, RPL_ISUPPORT, isupport, "are supported by this server")
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func (srv *Server) clientMotd(cl *Client) {
2014-11-22 13:21:30 +00:00
srv.sendClient(cl, RPL_MOTDSTART, "",
fmt.Sprintf("- %s Message of the day -", srv.Host))
for _, line := range strings.Split(srv.motd, "\n") {
srv.sendClient(cl, RPL_MOTD, "", fmt.Sprintf("- %s", line))
}
2016-07-17 21:52:03 +00:00
srv.sendClient(cl, RPL_ENDOFMOTD, "", "End of MOTD command")
2014-11-22 13:21:30 +00:00
}
type SrvCommandHook struct {
2016-07-17 21:52:03 +00:00
HookFn func(srv *Server, msg *irc.Message)
2014-11-22 13:21:30 +00:00
MinArgs int
NeedOper bool
NeedAuth bool
}
var srvCommandHooks = map[string]SrvCommandHook{
2016-07-17 21:52:03 +00:00
"OPER": {srvHandleCmdOper, 1, false, false},
"QUIT": {srvHandleCmdQuit, 0, false, false},
"MODE": {srvHandleCmdMode, 1, false, false},
"LIST": {srvHandleCmdList, 0, false, false},
"VERSION": {srvHandleCmdVersion, 0, false, false},
"STATS": {srvHandleCmdStats, 0, false, false},
"TIME": {srvHandleCmdTime, 0, false, false},
"ADMIN": {srvHandleCmdAdmin, 0, false, false},
"INFO": {srvHandleCmdInfo, 0, false, false},
"WHO": {srvHandleCmdWho, 0, false, false},
"WHOIS": {srvHandleCmdWhois, 0, false, false},
"WHOWAS": {srvHandleCmdWhowas, 0, false, false},
"KILL": {srvHandleCmdKill, 0, false, false},
"PING": {srvHandleCmdPing, 0, false, false},
"PONG": {srvHandleCmdPong, 0, false, false},
"ERROR": {srvHandleCmdError, 0, false, false},
"AWAY": {srvHandleCmdAway, 0, false, false},
"REHASH": {srvHandleCmdRehash, 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-17 21:52:03 +00:00
func srvHandleCmdOper(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +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-17 21:52:03 +00:00
func srvHandleCmdMode(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdList(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdVersion(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdStats(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdTime(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdAdmin(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdInfo(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdWho(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdWhois(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdWhowas(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdKill(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdPing(srv *Server, msg *irc.Message) {
srv.sendCommand(msg.Pre, "PONG", "", msg.Args[0])
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdPong(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdError(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdAway(srv *Server, msg *irc.Message) {
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-17 21:52:03 +00:00
srv.sendCommand(msg.Pre, RPL_REHASHING, "", "Rehashing.")
2014-11-22 13:21:30 +00:00
xlog.Info("Rehashing.")
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdRestart(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdSummon(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdUsers(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdUserhost(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}
2016-07-17 21:52:03 +00:00
func srvHandleCmdIson(srv *Server, msg *irc.Message) {
2014-11-22 13:21:30 +00:00
}