Browse Source

Stats for LUSERS logic now seperated, fixed params in LUSERS

tags/v0.12.0
moocow 6 years ago
parent
commit
744ad2ce0b
5 changed files with 94 additions and 14 deletions
  1. 9
    0
      irc/client.go
  2. 9
    14
      irc/handlers.go
  3. 14
    0
      irc/modes.go
  4. 5
    0
      irc/server.go
  5. 57
    0
      irc/stats.go

+ 9
- 0
irc/client.go View File

@@ -724,6 +724,15 @@ func (client *Client) destroy(beingResumed bool) {
724 724
 
725 725
 	// send quit messages to friends
726 726
 	if !beingResumed {
727
+		client.server.stats.ChangeTotal(-1)
728
+		if client.HasMode(modes.Invisible) {
729
+			client.server.stats.ChangeInvisible(-1)
730
+		}
731
+
732
+		if client.HasMode(modes.Operator) || client.HasMode(modes.LocalOperator) {
733
+			client.server.stats.ChangeOperators(-1)
734
+		}
735
+
727 736
 		for friend := range friends {
728 737
 			if client.quitMessage == "" {
729 738
 				client.quitMessage = "Exited"

+ 9
- 14
irc/handlers.go View File

@@ -1293,21 +1293,13 @@ func listHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Resp
1293 1293
 // LUSERS [<mask> [<server>]]
1294 1294
 func lusersHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) bool {
1295 1295
 	//TODO(vegax87) Fix network statistics and additional parameters
1296
-	var totalcount, invisiblecount, opercount int
1296
+	totalCount, invisibleCount, operCount := server.stats.GetStats()
1297
+
1298
+	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))
1299
+	rb.Add(nil, server.name, RPL_LUSEROP, client.nick, strconv.Itoa(operCount), client.t("IRC Operators online"))
1300
+	rb.Add(nil, server.name, RPL_LUSERCHANNELS, client.nick, strconv.Itoa(server.channels.Len()), client.t("channels formed"))
1301
+	rb.Add(nil, server.name, RPL_LUSERME, client.nick, fmt.Sprintf(client.t("I have %[1]d clients and %[2]d servers"), totalCount, 1))
1297 1302
 
1298
-	for _, onlineusers := range server.clients.AllClients() {
1299
-		totalcount++
1300
-		if onlineusers.flags[modes.Invisible] {
1301
-			invisiblecount++
1302
-		}
1303
-		if onlineusers.flags[modes.Operator] {
1304
-			opercount++
1305
-		}
1306
-	}
1307
-	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))
1308
-	rb.Add(nil, server.name, RPL_LUSEROP, client.nick, fmt.Sprintf(client.t("%d IRC Operators online"), opercount))
1309
-	rb.Add(nil, server.name, RPL_LUSERCHANNELS, client.nick, fmt.Sprintf(client.t("%d channels formed"), server.channels.Len()))
1310
-	rb.Add(nil, server.name, RPL_LUSERME, client.nick, fmt.Sprintf(client.t("I have %[1]d clients and %[2]d servers"), totalcount, 1))
1311 1303
 	return false
1312 1304
 }
1313 1305
 
@@ -1792,6 +1784,9 @@ func operHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Resp
1792 1784
 
1793 1785
 	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))
1794 1786
 
1787
+	// increase oper count
1788
+	server.stats.ChangeOperators(1)
1789
+
1795 1790
 	// client may now be unthrottled by the fakelag system
1796 1791
 	client.resetFakelag()
1797 1792
 

+ 14
- 0
irc/modes.go View File

@@ -37,6 +37,11 @@ func (client *Client) applyUserModeChanges(force bool, changes modes.ModeChanges
37 37
 				if client.flags[change.Mode] {
38 38
 					continue
39 39
 				}
40
+
41
+				if change.Mode == modes.Invisible {
42
+					client.server.stats.ChangeInvisible(1)
43
+				}
44
+
40 45
 				client.flags[change.Mode] = true
41 46
 				applied = append(applied, change)
42 47
 
@@ -44,6 +49,15 @@ func (client *Client) applyUserModeChanges(force bool, changes modes.ModeChanges
44 49
 				if !client.flags[change.Mode] {
45 50
 					continue
46 51
 				}
52
+
53
+				if change.Mode == modes.Invisible {
54
+					client.server.stats.ChangeInvisible(-1)
55
+				}
56
+
57
+				if change.Mode == modes.Operator || change.Mode == modes.LocalOperator {
58
+					client.server.stats.ChangeOperators(-1)
59
+				}
60
+
47 61
 				delete(client.flags, change.Mode)
48 62
 				applied = append(applied, change)
49 63
 			}

+ 5
- 0
irc/server.go View File

@@ -131,6 +131,7 @@ type Server struct {
131 131
 	stsEnabled                 bool
132 132
 	webirc                     []webircConfig
133 133
 	whoWas                     *WhoWasList
134
+	stats                      *Stats
134 135
 }
135 136
 
136 137
 var (
@@ -164,6 +165,7 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
164 165
 		signals:             make(chan os.Signal, len(ServerExitSignals)),
165 166
 		snomasks:            NewSnoManager(),
166 167
 		whoWas:              NewWhoWasList(config.Limits.WhowasEntries),
168
+		stats:               NewStats(),
167 169
 	}
168 170
 
169 171
 	if err := server.applyConfig(config, true); err != nil {
@@ -472,6 +474,9 @@ func (server *Server) tryRegister(c *Client) {
472 474
 		return
473 475
 	}
474 476
 
477
+	// count new user in statistics
478
+	server.stats.ChangeTotal(1)
479
+
475 480
 	// continue registration
476 481
 	server.logger.Debug("localconnect", fmt.Sprintf("Client registered [%s] [u:%s] [r:%s]", c.nick, c.username, c.realname))
477 482
 	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))

+ 57
- 0
irc/stats.go View File

@@ -0,0 +1,57 @@
1
+package irc
2
+
3
+import (
4
+	"sync"
5
+)
6
+
7
+// Stats contains the numbers of total, invisible and operators on the server
8
+type Stats struct {
9
+	sync.RWMutex
10
+
11
+	Total     int
12
+	Invisible int
13
+	Operators int
14
+}
15
+
16
+// NewStats creates a new instance of Stats
17
+func NewStats() *Stats {
18
+	serverStats := &Stats{
19
+		Total:     0,
20
+		Invisible: 0,
21
+		Operators: 0,
22
+	}
23
+
24
+	return serverStats
25
+}
26
+
27
+// ChangeTotal increments the total user count on server
28
+func (s *Stats) ChangeTotal(i int) {
29
+	s.Lock()
30
+	defer s.Unlock()
31
+
32
+	s.Total += i
33
+}
34
+
35
+// ChangeInvisible increments the invisible count
36
+func (s *Stats) ChangeInvisible(i int) {
37
+	s.Lock()
38
+	defer s.Unlock()
39
+
40
+	s.Invisible += i
41
+}
42
+
43
+// ChangeOperators increases the operator count
44
+func (s *Stats) ChangeOperators(i int) {
45
+	s.Lock()
46
+	defer s.Unlock()
47
+
48
+	s.Operators += i
49
+}
50
+
51
+// GetStats retrives total, invisible and oper count
52
+func (s *Stats) GetStats() (int, int, int) {
53
+	s.Lock()
54
+	defer s.Unlock()
55
+
56
+	return s.Total, s.Invisible, s.Operators
57
+}

Loading…
Cancel
Save