From 3c30adab5c27c8ff3643c96ebbfd2d388ec00903 Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Fri, 12 Aug 2016 00:13:15 +0200 Subject: [PATCH] Preparations for nats clustering --- handlers.go | 4 ++++ nats.go | 30 ++++++++++++++++++++++++++++++ server.go | 27 +++++++++++++++++++++------ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 nats.go diff --git a/handlers.go b/handlers.go index 38228a0..c8e35b7 100644 --- a/handlers.go +++ b/handlers.go @@ -86,6 +86,10 @@ func handleCmdJoin(sv *Server, msg *irc.Message) { } sv.chUsers[chid][clid] = "" sv.sendMsg(msg) + // dont proceed further if message was sent by remote origin + if !sv.localOrigin(msg) { + return + } sv.sendReply(msg.Pre, RPL_TOPIC, msg.Args[0], sv.chTopics[msg.Args[0]]) sv.channelNames(msg.Pre, msg.Args[0]) m, isoper := sv.opers[clid] diff --git a/nats.go b/nats.go new file mode 100644 index 0000000..8542677 --- /dev/null +++ b/nats.go @@ -0,0 +1,30 @@ +// vim:ts=4:sts=4:sw=4:noet:tw=72 + +package ircd + +import ( + "strings" + + "github.com/nats-io/nats" +) + +type NatsConnector struct { + natsConn *nats.Conn + subscriptions map[string]*nats.Subscription +} + +func NewNatsConnector(servers *string) *NatsConnector { + opts := nats.DefaultOptions + opts.Servers = strings.Split(*servers, ",") + for i, s := range opts.Servers { + opts.Servers[i] = strings.Trim(s, " ") + } + //opts.Secure = *ssl + opts.Secure = false + conn, err := opts.Connect() + if err != nil { + // foo + } + subs := make(map[string]*nats.Subscription) + return &NatsConnector{natsConn: conn, subscriptions: subs} +} diff --git a/server.go b/server.go index 6b0207f..321e895 100644 --- a/server.go +++ b/server.go @@ -248,7 +248,6 @@ func (sv *Server) addClient(cl Client) { return } sv.clients[clid] = cl - sv.clients[clid] = cl sv.sendLogon(cl.Name()) sv.connectionsCurrent = float64(len(sv.clients)) cl.Register(true) @@ -305,8 +304,14 @@ func (sv *Server) recvMsg(msg *irc.Message) { hook.HookFn(sv, msg) } -// Local delivery of an irc message to channel or client +// Forward an irc message to cluster and deliver locally func (sv *Server) sendMsg(msg *irc.Message) { + sv.deliverMsg(msg) + sv.forwardMsg(msg) +} + +// Local delivery of an irc message to channel or client +func (sv *Server) deliverMsg(msg *irc.Message) { if strings.HasPrefix(msg.Args[0], "#") { chid := strings.ToLower(msg.Args[0]) if _, exists := sv.chUsers[chid]; !exists { @@ -332,13 +337,16 @@ func (sv *Server) sendMsg(msg *irc.Message) { } } -// Forward an irc message to cluster and deliver locally +// Forward to cluster func (sv *Server) forwardMsg(msg *irc.Message) { - sv.sendMsg(msg) + if sv.localOrigin(msg) { + // TODO: forward to cluster + println("forward:", msg.String()) + } } -// Send irc reply to local client; drop, if server if client doesnt -// exists locally +// Send irc reply to local client and drop, if client doesnt +// exist locally func (sv *Server) sendReply(nick, cmd, args, trail string) { clid := strings.ToLower(nick) if _, exists := sv.clients[clid]; !exists { @@ -353,6 +361,13 @@ func (sv *Server) sendReply(nick, cmd, args, trail string) { cl.Receive(irc.M(sv.host, cmd, args, trail)) } +// Check if message's origin is local +func (sv *Server) localOrigin(msg *irc.Message) bool { + _, localClient := sv.clients[strings.ToLower(msg.Pre)] + localServer := (msg.Pre == sv.host) + return localClient || localServer +} + func (sv *Server) sendLogon(nick string) { sv.sendReply(nick, RPL_WELCOME, "", "Willkommen!") sv.sendReply(nick, RPL_YOURHOST, "",