Changed join/part handling

This commit is contained in:
Andreas Neue 2016-07-24 22:15:52 +02:00
parent 038a3589c4
commit f844505c17
1 changed files with 52 additions and 50 deletions

View File

@ -28,17 +28,22 @@ type Server struct {
queue chan *irc.Message queue chan *irc.Message
addq chan Client addq chan Client
delq chan Client delq chan Client
host string host string
info string info string
software string software string
version string version string
created string created string
motd string motd string
clients map[string]Client clients map[string]Client
chUsers map[string]map[string]string chUsers map[string]map[string]string
chTopics map[string]string chTopics map[string]string
config *conf.ConfigFile config *conf.ConfigFile
configPath string configPath string
packetsTransferred float64 packetsTransferred float64
connectionsCurrent float64 connectionsCurrent float64
connectionsCount float64 connectionsCount float64
@ -47,22 +52,26 @@ type Server struct {
// Create a new server instance. // Create a new server instance.
func NewServer(configPath, software, version string) *Server { func NewServer(configPath, software, version string) *Server {
sv := &Server{software: software, version: version, created: "yes"} sv := &Server{software: software, version: version,
created: time.Now().String()}
sv.queue = make(chan *irc.Message, 1024) sv.queue = make(chan *irc.Message, 1024)
sv.addq = make(chan Client, 1024) sv.addq = make(chan Client, 128)
sv.delq = make(chan Client, 1024) sv.delq = make(chan Client, 128)
sv.clients = make(map[string]Client) sv.clients = make(map[string]Client)
sv.chUsers = make(map[string]map[string]string) sv.chUsers = make(map[string]map[string]string)
sv.chTopics = make(map[string]string) sv.chTopics = make(map[string]string)
sv.configPath = configPath sv.configPath = configPath
sv.loadConfig() sv.loadConfig()
loglevel, _ := sv.config.GetInt("system", "loglevel") loglevel, _ := sv.config.GetInt("system", "loglevel")
xlog.Init(loglevel)
sv.host, _ = sv.config.GetString("server", "host") sv.host, _ = sv.config.GetString("server", "host")
sv.info, _ = sv.config.GetString("server", "info") sv.info, _ = sv.config.GetString("server", "info")
sv.motd, _ = sv.config.GetString("server", "motd") sv.motd, _ = sv.config.GetString("server", "motd")
xlog.Init(loglevel)
sv.packetsTransferred = 0 sv.packetsTransferred = 0
sv.connectionsCurrent = 0 sv.connectionsCurrent = 0
@ -235,7 +244,7 @@ func (sv *Server) sendLogon(nick string) {
fmt.Sprintf("Your host is %s, running on %s/%s", fmt.Sprintf("Your host is %s, running on %s/%s",
sv.host, sv.software, sv.version)) sv.host, sv.software, sv.version))
sv.sendReply(nick, RPL_CREATED, "", sv.sendReply(nick, RPL_CREATED, "",
fmt.Sprintf("Created: %s", sv.created)) fmt.Sprintf("This server was created %s", sv.created))
sv.sendReply(nick, RPL_MYINFO, "", sv.sendReply(nick, RPL_MYINFO, "",
fmt.Sprintf(myinfo, sv.host, sv.software, sv.version)) fmt.Sprintf(myinfo, sv.host, sv.software, sv.version))
sv.sendReply(nick, RPL_ISUPPORT, "", sv.sendReply(nick, RPL_ISUPPORT, "",
@ -248,35 +257,6 @@ func (sv *Server) sendLogon(nick string) {
sv.sendReply(nick, RPL_ENDOFMOTD, "", "End of MOTD command") sv.sendReply(nick, RPL_ENDOFMOTD, "", "End of MOTD command")
} }
func (sv *Server) channelJoin(nick, ch string) {
clid := strings.ToLower(nick)
chid := strings.ToLower(ch)
if _, exists := sv.chUsers[chid]; !exists {
sv.chUsers[chid] = make(map[string]string)
sv.chTopics[chid] = ""
}
if _, exists := sv.chUsers[chid][clid]; exists {
return
}
sv.chUsers[chid][clid] = ""
sv.sendMsg(irc.M(nick, "JOIN", ch, ""))
sv.sendReply(nick, RPL_TOPIC, ch, sv.chTopics[ch])
sv.channelNames(nick, ch)
}
func (sv *Server) channelPart(nick, ch, reason string) {
clid := strings.ToLower(nick)
chid := strings.ToLower(ch)
if _, exists := sv.chUsers[chid]; !exists {
return
}
if _, exists := sv.chUsers[chid][clid]; !exists {
return
}
sv.sendMsg(irc.M(nick, "PART", ch, reason))
delete(sv.chUsers[chid], clid)
}
func (sv *Server) channelNames(nick, ch string) { func (sv *Server) channelNames(nick, ch string) {
chid := strings.ToLower(ch) chid := strings.ToLower(ch)
if _, exists := sv.chUsers[chid]; !exists { if _, exists := sv.chUsers[chid]; !exists {
@ -340,11 +320,33 @@ func handleCmdPrivmsg(sv *Server, msg *irc.Message) {
} }
func handleCmdJoin(sv *Server, msg *irc.Message) { func handleCmdJoin(sv *Server, msg *irc.Message) {
sv.channelJoin(msg.Pre, msg.Args[0]) clid := strings.ToLower(msg.Pre)
chid := strings.ToLower(msg.Args[0])
if _, exists := sv.chUsers[chid]; !exists {
sv.chUsers[chid] = make(map[string]string)
sv.chTopics[chid] = ""
}
if _, exists := sv.chUsers[chid][clid]; exists {
return
}
sv.chUsers[chid][clid] = ""
sv.sendMsg(msg)
sv.sendReply(msg.Pre, RPL_TOPIC, msg.Args[0], sv.chTopics[msg.Args[0]])
sv.channelNames(msg.Pre, msg.Args[0])
} }
func handleCmdPart(sv *Server, msg *irc.Message) { func handleCmdPart(sv *Server, msg *irc.Message) {
sv.channelPart(msg.Pre, msg.Args[0], msg.Trail) clid := strings.ToLower(msg.Pre)
chid := strings.ToLower(msg.Args[0])
if _, exists := sv.chUsers[chid]; !exists {
return
}
if _, exists := sv.chUsers[chid][clid]; !exists {
return
}
sv.sendMsg(msg)
delete(sv.chUsers[chid], clid)
} }
func handleCmdQuit(sv *Server, msg *irc.Message) { func handleCmdQuit(sv *Server, msg *irc.Message) {