-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
100 lines (80 loc) · 2.46 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import "fmt"
// Server holds information about a linked server. Local and remote.
type Server struct {
// Each server has a unique ID. SID. This is part of TS6. 3 characters.
SID TS6SID
// Each server has a unique name. e.g., irc.example.com.
Name string
// Each server has a one line description.
Description string
// Number of hops from us to this server.
HopCount int
// Capabilities. TS6 servers must support at least QS (quit storm) and
// ENCAP. There are several others. ratbox servers offer for example:
// QS EX CHW IE GLN KNOCK TB ENCAP SAVE SAVETS_100
// Primarily I record this as on link to a server we pass along the capabs
// of all known servers to the other side (as part of TS6 burst). This ensures
// all servers know each other's capabilities.
Capabs map[string]struct{}
// If this server is directly connected to us (local), then LocalServer is
// set.
LocalServer *LocalServer
// This is the server we heard about the server through.
// If the server is not directly connected to us (remote), then we know how
// it is connected to us. Through this LocalServer.
ClosestServer *LocalServer
// We know what server it is linked to. The SID message tells us.
LinkedTo *Server
}
func (s *Server) String() string {
return fmt.Sprintf("%s %s", s.SID, s.Name)
}
func (s *Server) isLocal() bool {
return s.LocalServer != nil
}
// Turn our capabilities into a single space separated string.
func (s *Server) capabsString() string {
str := ""
for capab := range s.Capabs {
if len(str) > 0 {
str += " " + capab
} else {
str += capab
}
}
return str
}
// Check if the server supports a given capability.
func (s *Server) hasCapability(cap string) bool {
_, exists := s.Capabs[cap]
return exists
}
// Find all servers linked to us, directly or not.
func (s *Server) getLinkedServers(allServers map[TS6SID]*Server) []*Server {
linkedServers := []*Server{}
for _, server := range allServers {
if server == s {
continue
}
if server.LinkedTo != s {
continue
}
// It's linked to us.
linkedServers = append(linkedServers, server)
// Find any servers linked to it. They are linked to us too (indirectly).
linkedServers = append(linkedServers,
server.getLinkedServers(allServers)...)
}
return linkedServers
}
// Count how many users are on this server.
func (s *Server) getLocalUserCount(users map[TS6UID]*User) int {
count := 0
for _, u := range users {
if u.Server == s {
count++
}
}
return count
}