basic channel handling

This commit is contained in:
Andreas Neue 2016-07-18 16:19:04 +02:00
parent a1274b131a
commit 39816874d2
1 changed files with 50 additions and 20 deletions

View File

@ -44,7 +44,7 @@ type Server struct {
created string
motd string
clients map[string]*Client
subscriptions map[string][]string
channels map[string]map[string]string
ports map[int]bool
config *conf.ConfigFile
configPath string
@ -62,7 +62,7 @@ func NewServer(configPath, software, version string) *Server {
srv.DelClient = make(chan *Client, 1024)
srv.clients = make(map[string]*Client)
srv.subscriptions = make(map[string][]string)
srv.channels = make(map[string]map[string]string)
srv.configPath = configPath
srv.loadConfig()
loglevel, _ := srv.config.GetInt("system", "loglevel")
@ -176,12 +176,12 @@ func (srv *Server) sendMsg(msg *irc.Message) {
}
func (srv *Server) sendMsgToChannel(msg *irc.Message) {
subj := msg.Args[0]
if _, exists := srv.subscriptions[subj]; !exists {
cname := msg.Args[0]
if _, exists := srv.channels[cname]; !exists {
return
}
subs := srv.subscriptions[subj]
for _, sub := range subs {
subs := srv.channels[cname]
for sub, _ := range subs {
if _, exists := srv.clients[sub]; !exists {
continue
}
@ -205,7 +205,12 @@ func (srv *Server) sendReply(tar, cmd, args, trail string) {
return
}
cl := srv.clients[tar]
cl.Receive(irc.M(srv.host, cmd, tar+" "+args, trail))
if args != "" {
args = tar + args
} else {
args = tar
}
cl.Receive(irc.M(srv.host, cmd, args, trail))
}
func (srv *Server) clientLogon(cl *Client) {
@ -230,6 +235,29 @@ func (srv *Server) clientMotd(cl *Client) {
srv.sendReply(cl.Name(), RPL_ENDOFMOTD, "", "End of MOTD command")
}
func (srv *Server) channelBroadcast(ch string, msg *irc.Message) {
for nick, _ := range srv.channels[ch] {
if msg.Pre == nick {
continue
}
srv.clients[nick].Receive(msg)
}
}
func (srv *Server) channelJoin(nick, ch string) {
if _, exists := srv.channels[ch]; !exists {
srv.channels[ch] = make(map[string]string)
}
if _, exists := srv.channels[ch][nick]; exists {
return
}
srv.channels[ch][nick] = ""
srv.channelBroadcast(ch, irc.M(nick, "JOIN", ch, ""))
}
func (srv *Server) channelPart(nick, ch string) {
}
type SrvCommandHook struct {
HookFn func(srv *Server, msg *irc.Message)
MinArgs int
@ -239,8 +267,8 @@ type SrvCommandHook struct {
var srvCommandHooks = map[string]SrvCommandHook{
"PRIVMSG": {srvHandleCmdPrivmsg, 1, false, false},
"JOIN": {srvHandleCmdPrivmsg, 1, false, false},
"PART": {srvHandleCmdPrivmsg, 1, false, false},
"JOIN": {srvHandleCmdJoin, 1, false, false},
"PART": {srvHandleCmdPart, 1, false, false},
"QUIT": {srvHandleCmdQuit, 0, false, false},
"MODE": {srvHandleCmdMode, 1, false, false},
"LIST": {srvHandleCmdList, 0, false, false},
@ -271,9 +299,12 @@ func srvHandleCmdPrivmsg(srv *Server, msg *irc.Message) {
}
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])
}
func srvHandleCmdOper(srv *Server, msg *irc.Message) {
@ -307,7 +338,6 @@ func srvHandleCmdWho(srv *Server, msg *irc.Message) {
}
func srvHandleCmdWhois(srv *Server, msg *irc.Message) {
println("in whois")
srv.sendReply(msg.Pre, RPL_WHOISUSER, "nick user host *", "real name")
}