2016-07-27 13:31:21 +00:00
|
|
|
// vim:ts=4:sts=4:sw=4:noet:tw=72
|
|
|
|
|
|
|
|
package ircd
|
|
|
|
|
|
|
|
import (
|
2016-08-06 18:08:42 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/tls"
|
2016-07-27 13:31:21 +00:00
|
|
|
"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 * *"
|
2016-08-03 07:21:44 +00:00
|
|
|
|
|
|
|
//var isupport string = "CASEMAPPING=rfc1459 CHANTYPES=# NICKLEN=32 MODES=1 PREFIX=(qaohv)~&@%+"
|
|
|
|
var isupport string = "CASEMAPPING=rfc1459 CHANTYPES=# NICKLEN=32 MODES=1 PREFIX=(ohv)@%+"
|
2016-07-27 13:31:21 +00:00
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
queue chan *irc.Message
|
|
|
|
addq chan Client
|
|
|
|
delq chan Client
|
|
|
|
|
|
|
|
host string
|
|
|
|
info string
|
|
|
|
software string
|
|
|
|
version string
|
|
|
|
created string
|
|
|
|
motd string
|
|
|
|
|
2016-08-07 17:41:07 +00:00
|
|
|
opers map[string]string
|
2016-08-01 22:00:17 +00:00
|
|
|
|
2016-07-27 13:31:21 +00:00
|
|
|
clients map[string]Client
|
2016-07-31 15:24:58 +00:00
|
|
|
clModes map[string]string
|
2016-07-27 13:31:21 +00:00
|
|
|
|
|
|
|
chUsers map[string]map[string]string
|
|
|
|
chTopics map[string]string
|
|
|
|
chModes map[string]map[string]bool
|
|
|
|
|
|
|
|
config *conf.ConfigFile
|
|
|
|
configPath string
|
|
|
|
|
|
|
|
packetsTransferred float64
|
|
|
|
connectionsCurrent float64
|
|
|
|
connectionsCount float64
|
|
|
|
queueLen float64
|
|
|
|
|
|
|
|
authCallback func(name, pass string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new server instance.
|
|
|
|
func NewServer(configPath, software, version string) *Server {
|
|
|
|
sv := &Server{software: software, version: version,
|
|
|
|
created: time.Now().String()}
|
|
|
|
|
|
|
|
sv.queue = make(chan *irc.Message, 1024)
|
|
|
|
sv.addq = make(chan Client, 128)
|
|
|
|
sv.delq = make(chan Client, 128)
|
|
|
|
|
2016-08-07 17:41:07 +00:00
|
|
|
sv.opers = make(map[string]string)
|
2016-08-01 22:00:17 +00:00
|
|
|
|
2016-07-27 13:31:21 +00:00
|
|
|
sv.clients = make(map[string]Client)
|
2016-07-31 15:24:58 +00:00
|
|
|
sv.clModes = make(map[string]string)
|
2016-07-27 13:31:21 +00:00
|
|
|
|
|
|
|
sv.chUsers = make(map[string]map[string]string)
|
|
|
|
sv.chTopics = make(map[string]string)
|
|
|
|
sv.chModes = make(map[string]map[string]bool)
|
|
|
|
|
|
|
|
sv.configPath = configPath
|
|
|
|
sv.loadConfig()
|
|
|
|
|
|
|
|
loglevel, _ := sv.config.GetInt("system", "loglevel")
|
|
|
|
xlog.Init(loglevel)
|
|
|
|
|
|
|
|
sv.host, _ = sv.config.GetString("server", "host")
|
|
|
|
sv.info, _ = sv.config.GetString("server", "info")
|
|
|
|
sv.motd, _ = sv.config.GetString("server", "motd")
|
|
|
|
|
2016-08-02 15:39:32 +00:00
|
|
|
nicks, _ := sv.config.GetString("control", "a")
|
|
|
|
for _, nick := range strings.Split(nicks, ",") {
|
2016-08-07 17:41:07 +00:00
|
|
|
sv.opers[nick] = "a"
|
2016-08-02 15:39:32 +00:00
|
|
|
}
|
|
|
|
nicks, _ = sv.config.GetString("control", "o")
|
|
|
|
for _, nick := range strings.Split(nicks, ",") {
|
2016-08-07 17:41:07 +00:00
|
|
|
sv.opers[nick] = "o"
|
2016-08-01 22:00:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-27 13:31:21 +00:00
|
|
|
sv.packetsTransferred = 0
|
|
|
|
sv.connectionsCurrent = 0
|
|
|
|
sv.connectionsCount = 0
|
|
|
|
sv.queueLen = 0
|
|
|
|
|
|
|
|
return sv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Server) SetAuthCallback(authCB func(name, pass string) bool) {
|
|
|
|
sv.authCallback = authCB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the listening port and start the main server loop.
|
|
|
|
func (sv *Server) Run() {
|
|
|
|
xlog.Info("%s/%s", sv.software, sv.version)
|
|
|
|
go monitoringRun(sv)
|
|
|
|
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-28 22:21:24 +00:00
|
|
|
for {
|
|
|
|
err = sv.dispatcher()
|
|
|
|
if err != nil {
|
|
|
|
xlog.Error("Dispatcher in panic: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2016-07-27 13:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Server) listen(laddr string) {
|
|
|
|
listen, err := net.Listen("tcp", laddr)
|
|
|
|
if err != nil {
|
|
|
|
xlog.Fatal(err.Error())
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
for {
|
2016-08-06 18:08:42 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2016-07-27 13:31:21 +00:00
|
|
|
conn, err := listen.Accept()
|
|
|
|
if err != nil {
|
|
|
|
xlog.Error(err.Error())
|
|
|
|
} else {
|
|
|
|
NewRemoteClient(sv, conn)
|
|
|
|
sv.connectionsCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Server) listenTls(laddr string) {
|
2016-08-06 18:08:42 +00:00
|
|
|
cert, err := tls.LoadX509KeyPair("cert/server.pem", "cert/server.key")
|
|
|
|
if err != nil {
|
|
|
|
xlog.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
cfg := tls.Config{Certificates: []tls.Certificate{cert}}
|
|
|
|
cfg.Rand = rand.Reader
|
|
|
|
listen, err := tls.Listen("tcp", laddr, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
xlog.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
conn, err := listen.Accept()
|
|
|
|
if err != nil {
|
|
|
|
xlog.Error(err.Error())
|
|
|
|
} else {
|
|
|
|
NewRemoteClient(sv, conn)
|
|
|
|
sv.connectionsCount++
|
|
|
|
}
|
|
|
|
}
|
2016-07-27 13:31:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 22:21:24 +00:00
|
|
|
func (sv *Server) dispatcher() (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
var ok bool
|
|
|
|
err, ok = r.(error)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("pkg: %v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-07-27 13:31:21 +00:00
|
|
|
for {
|
|
|
|
sv.queueLen = float64(len(sv.queue))
|
|
|
|
select {
|
|
|
|
case msg := <-sv.queue:
|
|
|
|
sv.recvMsg(msg)
|
|
|
|
sv.packetsTransferred++
|
|
|
|
case cl := <-sv.addq:
|
2016-08-01 22:20:42 +00:00
|
|
|
sv.addClient(cl)
|
2016-07-27 13:31:21 +00:00
|
|
|
case cl := <-sv.delq:
|
2016-08-01 22:20:42 +00:00
|
|
|
sv.delClient(cl)
|
2016-07-27 13:31:21 +00:00
|
|
|
default:
|
|
|
|
time.Sleep(100 * time.Microsecond)
|
|
|
|
}
|
|
|
|
}
|
2016-07-28 22:21:24 +00:00
|
|
|
return
|
2016-07-27 13:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Server) loadConfig() {
|
|
|
|
cfg, err := conf.ReadConfigFile(sv.configPath)
|
|
|
|
if err != nil {
|
|
|
|
xlog.Fatal("Can't read config file (%s)", err.Error())
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
sv.config = cfg
|
|
|
|
}
|
|
|
|
|
2016-08-01 22:20:42 +00:00
|
|
|
func (sv *Server) addClient(cl Client) {
|
|
|
|
clid := strings.ToLower(cl.Name())
|
|
|
|
if _, exists := sv.clients[clid]; exists {
|
|
|
|
cl.Receive(irc.M(sv.host, ERR_NICKNAMEINUSE, cl.Name(), "Nickname is already in use"))
|
|
|
|
go func() {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
cl.Register(false)
|
|
|
|
}()
|
|
|
|
xlog.Info("Client registration failed: '%s' (client exists)", clid)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !sv.authCallback(cl.Name(), cl.Password()) {
|
|
|
|
cl.Receive(irc.M(sv.host, ERR_PASSWDMISMATCH, "", "Password incorrect"))
|
|
|
|
go func() {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
cl.Register(false)
|
|
|
|
}()
|
|
|
|
xlog.Info("Client registration failed: '%s' (wrong password)", clid)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sv.clients[clid] = cl
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Server) delClient(cl Client) {
|
|
|
|
clid := strings.ToLower(cl.Name())
|
|
|
|
cl.Destroy()
|
|
|
|
for chname, ch := range sv.chUsers {
|
|
|
|
if _, exists := ch[clid]; exists {
|
|
|
|
delete(ch, clid)
|
|
|
|
sv.sendMsg(irc.M(cl.Name(), "PART", chname, "quit"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(sv.clients, clid)
|
|
|
|
sv.connectionsCurrent = float64(len(sv.clients))
|
|
|
|
xlog.Info("Client deleted: '%s'", clid)
|
|
|
|
xlog.Info("Server has %d client(s)", len(sv.clients))
|
|
|
|
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
|
|
|
|
}
|
|
|
|
|
2016-07-27 13:31:21 +00:00
|
|
|
func (sv *Server) recvMsg(msg *irc.Message) {
|
|
|
|
cmd := msg.Cmd
|
2016-08-07 17:41:07 +00:00
|
|
|
clid := strings.ToLower(msg.Pre)
|
2016-07-27 13:31:21 +00:00
|
|
|
hook, exists := svCommandHooks[cmd]
|
|
|
|
if !exists {
|
|
|
|
sv.sendReply(msg.Pre, ERR_UNKNOWNCOMMAND, cmd, "Unknown command")
|
|
|
|
return
|
|
|
|
}
|
2016-08-07 17:41:07 +00:00
|
|
|
nm := hook.NeedMode
|
|
|
|
if nm != "" {
|
|
|
|
if m, isoper := sv.opers[clid]; !isoper {
|
|
|
|
sv.sendReply(msg.Pre, ERR_NOPRIVILEGES, cmd, "Permission denied: You're not a server operator")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if strings.Index(nm, m) == -1 {
|
|
|
|
sv.sendReply(msg.Pre, ERR_NOPRIVILEGES, cmd, "Permission denied: You're not a server admin")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-27 13:31:21 +00:00
|
|
|
argc := len(msg.Args)
|
|
|
|
if argc < hook.MinArgs {
|
|
|
|
sv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if hook.NeedTrail && msg.Trail == "" {
|
|
|
|
sv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hook.HookFn(sv, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Server) sendMsg(msg *irc.Message) {
|
|
|
|
if strings.HasPrefix(msg.Args[0], "#") {
|
|
|
|
chid := strings.ToLower(msg.Args[0])
|
|
|
|
if _, exists := sv.chUsers[chid]; !exists {
|
|
|
|
sv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for clid, _ := range sv.chUsers[chid] {
|
|
|
|
if strings.ToLower(msg.Pre) == clid && msg.Cmd == "PRIVMSG" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cl, exists := sv.clients[clid]; exists {
|
|
|
|
cl.Receive(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clid := strings.ToLower(msg.Args[0])
|
|
|
|
if _, exists := sv.clients[clid]; !exists {
|
|
|
|
sv.sendReply(msg.Pre, ERR_NOSUCHNICK, msg.Args[0], "No such nick/channel")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cl := sv.clients[clid]
|
|
|
|
cl.Receive(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Server) sendReply(nick, cmd, args, trail string) {
|
|
|
|
clid := strings.ToLower(nick)
|
|
|
|
if _, exists := sv.clients[clid]; !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cl := sv.clients[clid]
|
|
|
|
if args != "" {
|
|
|
|
args = nick + " " + args
|
|
|
|
} else {
|
|
|
|
args = nick
|
|
|
|
}
|
|
|
|
cl.Receive(irc.M(sv.host, cmd, args, trail))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Server) sendLogon(nick string) {
|
|
|
|
sv.sendReply(nick, RPL_WELCOME, "", "Willkommen!")
|
|
|
|
sv.sendReply(nick, RPL_YOURHOST, "",
|
|
|
|
fmt.Sprintf("Your host is %s, running on %s/%s",
|
|
|
|
sv.host, sv.software, sv.version))
|
|
|
|
sv.sendReply(nick, RPL_CREATED, "",
|
|
|
|
fmt.Sprintf("This server was created %s", sv.created))
|
|
|
|
sv.sendReply(nick, RPL_MYINFO, "",
|
|
|
|
fmt.Sprintf(myinfo, sv.host, sv.software, sv.version))
|
|
|
|
sv.sendReply(nick, RPL_ISUPPORT, "",
|
|
|
|
isupport+" are supported by this server")
|
|
|
|
sv.sendReply(nick, RPL_MOTDSTART, "",
|
|
|
|
fmt.Sprintf("- %s Message of the day -", sv.host))
|
|
|
|
for _, line := range strings.Split(sv.motd, "\n") {
|
|
|
|
sv.sendReply(nick, RPL_MOTD, "", fmt.Sprintf("- %s", line))
|
|
|
|
}
|
|
|
|
sv.sendReply(nick, RPL_ENDOFMOTD, "", "End of MOTD command")
|
2016-08-07 17:41:07 +00:00
|
|
|
if m, isoper := sv.opers[strings.ToLower(nick)]; isoper {
|
|
|
|
switch m {
|
|
|
|
case "a":
|
|
|
|
sv.sendReply(nick, RPL_YOUREOPER, "", "You are now a server admin")
|
|
|
|
case "o":
|
|
|
|
sv.sendReply(nick, RPL_YOUREOPER, "", "You are now a server operator")
|
|
|
|
}
|
|
|
|
}
|
2016-07-27 13:31:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 15:24:58 +00:00
|
|
|
// Send channel name list to client
|
2016-07-27 13:31:21 +00:00
|
|
|
func (sv *Server) channelNames(nick, ch string) {
|
|
|
|
chid := strings.ToLower(ch)
|
|
|
|
if _, exists := sv.chUsers[chid]; !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
names := ""
|
2016-07-31 15:24:58 +00:00
|
|
|
for clid, _ := range sv.chUsers[chid] {
|
2016-07-27 13:31:21 +00:00
|
|
|
name := sv.clients[clid].Name()
|
|
|
|
if names != "" {
|
|
|
|
names += " "
|
|
|
|
}
|
2016-08-03 07:21:44 +00:00
|
|
|
modeFlags := "ohv"
|
|
|
|
modeChars := "@%+"
|
2016-07-31 15:24:58 +00:00
|
|
|
for i, mf := range modeFlags {
|
|
|
|
if _, exists := sv.chModes[chid][string(mf)+" "+clid]; exists {
|
|
|
|
name = string(modeChars[i]) + name
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
names = names + name
|
|
|
|
}
|
|
|
|
chunks := len(strings.Split(names, " "))/8 + 1
|
|
|
|
nameList := strings.SplitN(names, " ", chunks)
|
|
|
|
for _, nameSlice := range nameList {
|
|
|
|
sv.sendReply(nick, RPL_NAMEREPLY, "= "+ch, nameSlice)
|
2016-07-27 13:31:21 +00:00
|
|
|
}
|
|
|
|
sv.sendReply(nick, RPL_ENDOFNAMES, ch, "End of /NAMES list")
|
|
|
|
}
|
|
|
|
|
2016-07-31 15:24:58 +00:00
|
|
|
// Check if mode exists for the specified channel
|
|
|
|
func (sv *Server) channelCheckMode(chid, mode string) bool {
|
|
|
|
if _, exists := sv.chModes[chid]; !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, exists := sv.chModes[chid][mode]; !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-07-31 21:56:25 +00:00
|
|
|
func (sv *Server) channelCheckPerm(chid, clid, modes string) bool {
|
|
|
|
for _, m := range modes {
|
|
|
|
if sv.channelCheckMode(chid, string(m)+" "+clid) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|