342 lines
9.0 KiB
Go
342 lines
9.0 KiB
Go
// vim:ts=4:sts=4:sw=4:noet:tw=72
|
|
|
|
package ircd
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.dnix.de/an/conf"
|
|
"code.dnix.de/an/irc"
|
|
"code.dnix.de/an/xlog"
|
|
)
|
|
|
|
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 {
|
|
Dispatch chan *irc.Message
|
|
AddClient chan Client
|
|
DelClient chan Client
|
|
|
|
host string
|
|
info string
|
|
software string
|
|
version string
|
|
created string
|
|
motd string
|
|
clients map[string]Client
|
|
chSubs map[string]map[string]string
|
|
chTopics map[string]string
|
|
ports map[int]bool
|
|
config *conf.ConfigFile
|
|
configPath string
|
|
|
|
packetsTransferred float64
|
|
clientConnections float64
|
|
}
|
|
|
|
func init() {
|
|
}
|
|
|
|
// Create a new server instance.
|
|
func NewServer(configPath, software, version string) *Server {
|
|
srv := &Server{software: software, version: version, created: "yes"}
|
|
|
|
srv.Dispatch = make(chan *irc.Message, 1024)
|
|
srv.AddClient = make(chan Client, 1024)
|
|
srv.DelClient = make(chan Client, 1024)
|
|
|
|
srv.clients = make(map[string]Client)
|
|
srv.chSubs = make(map[string]map[string]string)
|
|
srv.chTopics = make(map[string]string)
|
|
srv.configPath = configPath
|
|
srv.loadConfig()
|
|
loglevel, _ := srv.config.GetInt("system", "loglevel")
|
|
srv.host, _ = srv.config.GetString("server", "host")
|
|
srv.info, _ = srv.config.GetString("server", "info")
|
|
srv.motd, _ = srv.config.GetString("server", "motd")
|
|
xlog.Init(loglevel)
|
|
|
|
srv.packetsTransferred = 0
|
|
srv.clientConnections = 0
|
|
|
|
return srv
|
|
}
|
|
|
|
// Open the listening port and start the main server loop.
|
|
func (srv *Server) Run() {
|
|
xlog.Info("%s/%s", srv.software, srv.version)
|
|
go monitoringRun(srv)
|
|
laddr, _ := srv.config.GetString("net", "listen_ircd")
|
|
go srv.listen(laddr)
|
|
srv.dispatch()
|
|
}
|
|
|
|
func (srv *Server) listen(laddr string) {
|
|
listen, err := net.Listen("tcp", laddr)
|
|
if err != nil {
|
|
xlog.Fatal(err.Error())
|
|
os.Exit(-1)
|
|
}
|
|
|
|
xlog.Info("Start listening on %s", laddr)
|
|
|
|
for {
|
|
time.Sleep(1 * time.Millisecond)
|
|
conn, err := listen.Accept()
|
|
if err != nil {
|
|
xlog.Error(err.Error())
|
|
} else {
|
|
NewRemoteClient(srv, conn)
|
|
srv.clientConnections++
|
|
}
|
|
}
|
|
}
|
|
|
|
func (srv *Server) dispatch() {
|
|
xlog.Debug("Entering msg dispatcher")
|
|
for {
|
|
time.Sleep(1 * time.Millisecond)
|
|
select {
|
|
case msg := <-srv.Dispatch:
|
|
srv.recvMsg(msg)
|
|
srv.packetsTransferred++
|
|
case cl := <-srv.AddClient:
|
|
name := cl.Name()
|
|
srv.clients[name] = cl
|
|
srv.clientLogon(cl)
|
|
srv.clientMotd(cl)
|
|
xlog.Info("Client registered: '%s'", name)
|
|
xlog.Info("Server has %d client(s)", len(srv.clients))
|
|
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
|
|
case cl := <-srv.DelClient:
|
|
name := cl.Name()
|
|
cl.Destroy()
|
|
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())
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
|
|
func (srv *Server) loadConfig() {
|
|
cfg, err := conf.ReadConfigFile(srv.configPath)
|
|
if err != nil {
|
|
xlog.Fatal("Can't read config file (%s)", err.Error())
|
|
os.Exit(-1)
|
|
}
|
|
srv.config = cfg
|
|
}
|
|
|
|
func (srv *Server) recvMsg(msg *irc.Message) {
|
|
cmd := msg.Cmd
|
|
hook, exists := srvCommandHooks[cmd]
|
|
if !exists {
|
|
srv.sendReply(msg.Pre, ERR_UNKNOWNCOMMAND, cmd, "Unknown command")
|
|
return
|
|
}
|
|
argc := len(msg.Args)
|
|
if argc < hook.MinArgs {
|
|
srv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
|
|
return
|
|
}
|
|
if hook.NeedTrail && msg.Trail == "" {
|
|
srv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
|
|
return
|
|
}
|
|
hook.HookFn(srv, msg)
|
|
}
|
|
|
|
func (srv *Server) sendMsg(msg *irc.Message) {
|
|
if strings.HasPrefix(msg.Args[0], "#") {
|
|
srv.bcChan(msg)
|
|
} else {
|
|
srv.sendClient(msg)
|
|
}
|
|
}
|
|
|
|
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")
|
|
return
|
|
}
|
|
cl := srv.clients[msg.Args[0]]
|
|
cl.Receive(msg)
|
|
}
|
|
|
|
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)
|
|
return
|
|
}
|
|
for nick, _ := range srv.chSubs[ch] {
|
|
if msg.Pre == nick && msg.Cmd == "PRIVMSG" {
|
|
continue
|
|
}
|
|
srv.clients[nick].Receive(msg)
|
|
}
|
|
}
|
|
|
|
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]
|
|
if args != "" {
|
|
args = tar + " " + args
|
|
} else {
|
|
args = tar
|
|
}
|
|
cl.Receive(irc.M(srv.host, cmd, args, trail))
|
|
}
|
|
|
|
func (srv *Server) clientLogon(cl Client) {
|
|
srv.sendReply(cl.Name(), RPL_WELCOME, "", "Willkommen!")
|
|
srv.sendReply(cl.Name(), RPL_YOURHOST, "",
|
|
fmt.Sprintf("Your host is %s, running on %s/%s",
|
|
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")
|
|
}
|
|
|
|
func (srv *Server) clientMotd(cl Client) {
|
|
srv.sendReply(cl.Name(), RPL_MOTDSTART, "",
|
|
fmt.Sprintf("- %s Message of the day -", srv.host))
|
|
for _, line := range strings.Split(srv.motd, "\n") {
|
|
srv.sendReply(cl.Name(), RPL_MOTD, "", fmt.Sprintf("- %s", line))
|
|
}
|
|
srv.sendReply(cl.Name(), RPL_ENDOFMOTD, "", "End of MOTD command")
|
|
}
|
|
|
|
func (srv *Server) channelJoin(nick, ch string) {
|
|
if _, exists := srv.chSubs[ch]; !exists {
|
|
srv.chSubs[ch] = make(map[string]string)
|
|
srv.chTopics[ch] = ""
|
|
}
|
|
if _, exists := srv.chSubs[ch][nick]; exists {
|
|
return
|
|
}
|
|
srv.chSubs[ch][nick] = ""
|
|
srv.bcChan(irc.M(nick, "JOIN", ch, ""))
|
|
srv.sendReply(nick, RPL_TOPIC, ch, srv.chTopics[ch])
|
|
}
|
|
|
|
func (srv *Server) channelPart(nick, ch, reason string) {
|
|
if _, exists := srv.chSubs[ch]; !exists {
|
|
return
|
|
}
|
|
if _, exists := srv.chSubs[ch][nick]; !exists {
|
|
return
|
|
}
|
|
srv.bcChan(irc.M(nick, "PART", ch, reason))
|
|
delete(srv.chSubs[ch], nick)
|
|
}
|
|
|
|
type SrvCommandHook struct {
|
|
HookFn func(srv *Server, msg *irc.Message)
|
|
MinArgs int
|
|
NeedTrail bool
|
|
NeedOper bool
|
|
NeedAuth bool
|
|
}
|
|
|
|
var srvCommandHooks = map[string]SrvCommandHook{
|
|
"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},
|
|
*/
|
|
}
|
|
|
|
func srvHandleCmdPrivmsg(srv *Server, msg *irc.Message) {
|
|
srv.sendMsg(msg)
|
|
}
|
|
|
|
func srvHandleCmdJoin(srv *Server, msg *irc.Message) {
|
|
srv.channelJoin(msg.Pre, msg.Args[0])
|
|
}
|
|
|
|
func srvHandleCmdPart(srv *Server, msg *irc.Message) {
|
|
srv.channelPart(msg.Pre, msg.Args[0], msg.Trail)
|
|
}
|
|
|
|
func srvHandleCmdQuit(srv *Server, msg *irc.Message) {
|
|
|
|
}
|
|
|
|
func srvHandleCmdMode(srv *Server, msg *irc.Message) {
|
|
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func srvHandleCmdWhois(srv *Server, msg *irc.Message) {
|
|
srv.sendReply(msg.Pre, RPL_WHOISUSER, "nick user host *", "real name")
|
|
}
|
|
|
|
func srvHandleCmdPing(srv *Server, msg *irc.Message) {
|
|
srv.sendReply(msg.Pre, "PONG", msg.Args[0], "")
|
|
}
|
|
|
|
func srvHandleCmdRehash(srv *Server, msg *irc.Message) {
|
|
srv.loadConfig()
|
|
srv.sendReply(msg.Pre, RPL_REHASHING, "", "Rehashing.")
|
|
xlog.Info("Rehashing")
|
|
}
|