From 744ad2ce0bda36ce2b0d18b54b64212fcfde0c98 Mon Sep 17 00:00:00 2001 From: moocow Date: Fri, 20 Apr 2018 22:48:15 +0200 Subject: [PATCH] Stats for LUSERS logic now seperated, fixed params in LUSERS --- irc/client.go | 9 ++++++++ irc/handlers.go | 23 ++++++++------------ irc/modes.go | 14 ++++++++++++ irc/server.go | 5 +++++ irc/stats.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 irc/stats.go diff --git a/irc/client.go b/irc/client.go index 0cafce83..3be6a75b 100644 --- a/irc/client.go +++ b/irc/client.go @@ -724,6 +724,15 @@ func (client *Client) destroy(beingResumed bool) { // send quit messages to friends if !beingResumed { + client.server.stats.ChangeTotal(-1) + if client.HasMode(modes.Invisible) { + client.server.stats.ChangeInvisible(-1) + } + + if client.HasMode(modes.Operator) || client.HasMode(modes.LocalOperator) { + client.server.stats.ChangeOperators(-1) + } + for friend := range friends { if client.quitMessage == "" { client.quitMessage = "Exited" diff --git a/irc/handlers.go b/irc/handlers.go index b54f4332..ac753e80 100644 --- a/irc/handlers.go +++ b/irc/handlers.go @@ -1293,21 +1293,13 @@ func listHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Resp // LUSERS [ []] func lusersHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) bool { //TODO(vegax87) Fix network statistics and additional parameters - var totalcount, invisiblecount, opercount int + totalCount, invisibleCount, operCount := server.stats.GetStats() + + rb.Add(nil, server.name, RPL_LUSERCLIENT, client.nick, fmt.Sprintf(client.t("There are %[1]d users and %[2]d invisible on %[3]d server(s)"), totalCount-invisibleCount, invisibleCount, 1)) + rb.Add(nil, server.name, RPL_LUSEROP, client.nick, strconv.Itoa(operCount), client.t("IRC Operators online")) + rb.Add(nil, server.name, RPL_LUSERCHANNELS, client.nick, strconv.Itoa(server.channels.Len()), client.t("channels formed")) + rb.Add(nil, server.name, RPL_LUSERME, client.nick, fmt.Sprintf(client.t("I have %[1]d clients and %[2]d servers"), totalCount, 1)) - for _, onlineusers := range server.clients.AllClients() { - totalcount++ - if onlineusers.flags[modes.Invisible] { - invisiblecount++ - } - if onlineusers.flags[modes.Operator] { - opercount++ - } - } - rb.Add(nil, server.name, RPL_LUSERCLIENT, client.nick, fmt.Sprintf(client.t("There are %[1]d users and %[2]d invisible on %[3]d server(s)"), totalcount - invisiblecount, invisiblecount, 1)) - rb.Add(nil, server.name, RPL_LUSEROP, client.nick, fmt.Sprintf(client.t("%d IRC Operators online"), opercount)) - rb.Add(nil, server.name, RPL_LUSERCHANNELS, client.nick, fmt.Sprintf(client.t("%d channels formed"), server.channels.Len())) - rb.Add(nil, server.name, RPL_LUSERME, client.nick, fmt.Sprintf(client.t("I have %[1]d clients and %[2]d servers"), totalcount, 1)) return false } @@ -1792,6 +1784,9 @@ func operHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Resp server.snomasks.Send(sno.LocalOpers, fmt.Sprintf(ircfmt.Unescape("Client opered up $c[grey][$r%s$c[grey], $r%s$c[grey]]"), client.nickMaskString, client.operName)) + // increase oper count + server.stats.ChangeOperators(1) + // client may now be unthrottled by the fakelag system client.resetFakelag() diff --git a/irc/modes.go b/irc/modes.go index 375831eb..d8cc95b8 100644 --- a/irc/modes.go +++ b/irc/modes.go @@ -37,6 +37,11 @@ func (client *Client) applyUserModeChanges(force bool, changes modes.ModeChanges if client.flags[change.Mode] { continue } + + if change.Mode == modes.Invisible { + client.server.stats.ChangeInvisible(1) + } + client.flags[change.Mode] = true applied = append(applied, change) @@ -44,6 +49,15 @@ func (client *Client) applyUserModeChanges(force bool, changes modes.ModeChanges if !client.flags[change.Mode] { continue } + + if change.Mode == modes.Invisible { + client.server.stats.ChangeInvisible(-1) + } + + if change.Mode == modes.Operator || change.Mode == modes.LocalOperator { + client.server.stats.ChangeOperators(-1) + } + delete(client.flags, change.Mode) applied = append(applied, change) } diff --git a/irc/server.go b/irc/server.go index fa796b71..1fa9dfb4 100644 --- a/irc/server.go +++ b/irc/server.go @@ -131,6 +131,7 @@ type Server struct { stsEnabled bool webirc []webircConfig whoWas *WhoWasList + stats *Stats } var ( @@ -164,6 +165,7 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) { signals: make(chan os.Signal, len(ServerExitSignals)), snomasks: NewSnoManager(), whoWas: NewWhoWasList(config.Limits.WhowasEntries), + stats: NewStats(), } if err := server.applyConfig(config, true); err != nil { @@ -472,6 +474,9 @@ func (server *Server) tryRegister(c *Client) { return } + // count new user in statistics + server.stats.ChangeTotal(1) + // continue registration server.logger.Debug("localconnect", fmt.Sprintf("Client registered [%s] [u:%s] [r:%s]", c.nick, c.username, c.realname)) server.snomasks.Send(sno.LocalConnects, fmt.Sprintf(ircfmt.Unescape("Client registered $c[grey][$r%s$c[grey]] [u:$r%s$c[grey]] [h:$r%s$c[grey]] [r:$r%s$c[grey]]"), c.nick, c.username, c.rawHostname, c.realname)) diff --git a/irc/stats.go b/irc/stats.go new file mode 100644 index 00000000..65e4a67f --- /dev/null +++ b/irc/stats.go @@ -0,0 +1,57 @@ +package irc + +import ( + "sync" +) + +// Stats contains the numbers of total, invisible and operators on the server +type Stats struct { + sync.RWMutex + + Total int + Invisible int + Operators int +} + +// NewStats creates a new instance of Stats +func NewStats() *Stats { + serverStats := &Stats{ + Total: 0, + Invisible: 0, + Operators: 0, + } + + return serverStats +} + +// ChangeTotal increments the total user count on server +func (s *Stats) ChangeTotal(i int) { + s.Lock() + defer s.Unlock() + + s.Total += i +} + +// ChangeInvisible increments the invisible count +func (s *Stats) ChangeInvisible(i int) { + s.Lock() + defer s.Unlock() + + s.Invisible += i +} + +// ChangeOperators increases the operator count +func (s *Stats) ChangeOperators(i int) { + s.Lock() + defer s.Unlock() + + s.Operators += i +} + +// GetStats retrives total, invisible and oper count +func (s *Stats) GetStats() (int, int, int) { + s.Lock() + defer s.Unlock() + + return s.Total, s.Invisible, s.Operators +}