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

@ -37,17 +37,17 @@ type Server struct {
AddClient chan *Client AddClient chan *Client
DelClient chan *Client DelClient 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
subscriptions map[string][]string channels map[string]map[string]string
ports map[int]bool ports map[int]bool
config *conf.ConfigFile config *conf.ConfigFile
configPath string configPath string
} }
func init() { func init() {
@ -62,7 +62,7 @@ func NewServer(configPath, software, version string) *Server {
srv.DelClient = make(chan *Client, 1024) srv.DelClient = make(chan *Client, 1024)
srv.clients = make(map[string]*Client) srv.clients = make(map[string]*Client)
srv.subscriptions = make(map[string][]string) srv.channels = make(map[string]map[string]string)
srv.configPath = configPath srv.configPath = configPath
srv.loadConfig() srv.loadConfig()
loglevel, _ := srv.config.GetInt("system", "loglevel") loglevel, _ := srv.config.GetInt("system", "loglevel")
@ -176,12 +176,12 @@ func (srv *Server) sendMsg(msg *irc.Message) {
} }
func (srv *Server) sendMsgToChannel(msg *irc.Message) { func (srv *Server) sendMsgToChannel(msg *irc.Message) {
subj := msg.Args[0] cname := msg.Args[0]
if _, exists := srv.subscriptions[subj]; !exists { if _, exists := srv.channels[cname]; !exists {
return return
} }
subs := srv.subscriptions[subj] subs := srv.channels[cname]
for _, sub := range subs { for sub, _ := range subs {
if _, exists := srv.clients[sub]; !exists { if _, exists := srv.clients[sub]; !exists {
continue continue
} }
@ -205,7 +205,12 @@ func (srv *Server) sendReply(tar, cmd, args, trail string) {
return return
} }
cl := srv.clients[tar] 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) { 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") 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 { type SrvCommandHook struct {
HookFn func(srv *Server, msg *irc.Message) HookFn func(srv *Server, msg *irc.Message)
MinArgs int MinArgs int
@ -239,8 +267,8 @@ type SrvCommandHook struct {
var srvCommandHooks = map[string]SrvCommandHook{ var srvCommandHooks = map[string]SrvCommandHook{
"PRIVMSG": {srvHandleCmdPrivmsg, 1, false, false}, "PRIVMSG": {srvHandleCmdPrivmsg, 1, false, false},
"JOIN": {srvHandleCmdPrivmsg, 1, false, false}, "JOIN": {srvHandleCmdJoin, 1, false, false},
"PART": {srvHandleCmdPrivmsg, 1, false, false}, "PART": {srvHandleCmdPart, 1, false, false},
"QUIT": {srvHandleCmdQuit, 0, false, false}, "QUIT": {srvHandleCmdQuit, 0, false, false},
"MODE": {srvHandleCmdMode, 1, false, false}, "MODE": {srvHandleCmdMode, 1, false, false},
"LIST": {srvHandleCmdList, 0, 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) { func srvHandleCmdJoin(srv *Server, msg *irc.Message) {
srv.channelJoin(msg.Pre, msg.Args[0])
} }
func srvHandleCmdPart(srv *Server, msg *irc.Message) { func srvHandleCmdPart(srv *Server, msg *irc.Message) {
srv.channelPart(msg.Pre, msg.Args[0])
} }
func srvHandleCmdOper(srv *Server, msg *irc.Message) { 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) { func srvHandleCmdWhois(srv *Server, msg *irc.Message) {
println("in whois")
srv.sendReply(msg.Pre, RPL_WHOISUSER, "nick user host *", "real name") srv.sendReply(msg.Pre, RPL_WHOISUSER, "nick user host *", "real name")
} }