3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-01-05 09:32:32 +01:00

Fix some notes and add some comments.

This commit is contained in:
Daniel Oaks 2017-10-05 23:29:34 +10:00
parent 797b9f0996
commit eac6a69782
5 changed files with 36 additions and 16 deletions

View File

@ -224,17 +224,21 @@ func (clients *ClientLookupSet) Find(userhost string) *Client {
//TODO(dan): move this over to generally using glob syntax instead? //TODO(dan): move this over to generally using glob syntax instead?
// kinda more expected in normal ban/etc masks, though regex is useful (probably as an extban?) // kinda more expected in normal ban/etc masks, though regex is useful (probably as an extban?)
// UserMaskSet holds a set of client masks and lets you match hostnames to them.
type UserMaskSet struct { type UserMaskSet struct {
masks map[string]bool masks map[string]bool
regexp *regexp.Regexp regexp *regexp.Regexp
} }
// NewUserMaskSet returns a new UserMaskSet.
func NewUserMaskSet() *UserMaskSet { func NewUserMaskSet() *UserMaskSet {
return &UserMaskSet{ return &UserMaskSet{
masks: make(map[string]bool), masks: make(map[string]bool),
} }
} }
// Add adds the given mask to this set.
func (set *UserMaskSet) Add(mask string) bool { func (set *UserMaskSet) Add(mask string) bool {
casefoldedMask, err := Casefold(mask) casefoldedMask, err := Casefold(mask)
if err != nil { if err != nil {
@ -249,6 +253,7 @@ func (set *UserMaskSet) Add(mask string) bool {
return true return true
} }
// AddAll adds the given masks to this set.
func (set *UserMaskSet) AddAll(masks []string) (added bool) { func (set *UserMaskSet) AddAll(masks []string) (added bool) {
for _, mask := range masks { for _, mask := range masks {
if !added && !set.masks[mask] { if !added && !set.masks[mask] {
@ -260,6 +265,7 @@ func (set *UserMaskSet) AddAll(masks []string) (added bool) {
return return
} }
// Remove removes the given mask from this set.
func (set *UserMaskSet) Remove(mask string) bool { func (set *UserMaskSet) Remove(mask string) bool {
if !set.masks[mask] { if !set.masks[mask] {
return false return false
@ -269,6 +275,7 @@ func (set *UserMaskSet) Remove(mask string) bool {
return true return true
} }
// Match matches the given n!u@h.
func (set *UserMaskSet) Match(userhost string) bool { func (set *UserMaskSet) Match(userhost string) bool {
if set.regexp == nil { if set.regexp == nil {
return false return false
@ -276,17 +283,18 @@ func (set *UserMaskSet) Match(userhost string) bool {
return set.regexp.MatchString(userhost) return set.regexp.MatchString(userhost)
} }
// String returns the masks in this set.
func (set *UserMaskSet) String() string { func (set *UserMaskSet) String() string {
masks := make([]string, len(set.masks)) masks := make([]string, len(set.masks))
index := 0 index := 0
for mask := range set.masks { for mask := range set.masks {
masks[index] = mask masks[index] = mask
index += 1 index++
} }
return strings.Join(masks, " ") return strings.Join(masks, " ")
} }
// Generate a regular expression from the set of user mask // setRegexp generates a regular expression from the set of user mask
// strings. Masks are split at the two types of wildcards, `*` and // strings. Masks are split at the two types of wildcards, `*` and
// `?`. All the pieces are meta-escaped. `*` is replaced with `.*`, // `?`. All the pieces are meta-escaped. `*` is replaced with `.*`,
// the regexp equivalent. Likewise, `?` is replaced with `.`. The // the regexp equivalent. Likewise, `?` is replaced with `.`. The

View File

@ -53,7 +53,7 @@ func InitDB(path string) {
} }
} }
// open an existing database, performing a schema version check // OpenDatabase returns an existing database, performing a schema version check.
func OpenDatabase(path string) (*buntdb.DB, error) { func OpenDatabase(path string) (*buntdb.DB, error) {
// open data store // open data store
db, err := buntdb.Open(path) db, err := buntdb.Open(path)
@ -65,7 +65,7 @@ func OpenDatabase(path string) (*buntdb.DB, error) {
err = db.View(func(tx *buntdb.Tx) error { err = db.View(func(tx *buntdb.Tx) error {
version, _ := tx.Get(keySchemaVersion) version, _ := tx.Get(keySchemaVersion)
if version != latestDbSchema { if version != latestDbSchema {
return fmt.Errorf("Database must be updated. Expected schema v%s, got v%s.", latestDbSchema, version) return fmt.Errorf("Database must be updated. Expected schema v%s, got v%s", latestDbSchema, version)
} }
return nil return nil
}) })

View File

@ -12,6 +12,7 @@ import (
"github.com/goshuirc/irc-go/ircmsg" "github.com/goshuirc/irc-go/ircmsg"
) )
// MonitorManager keeps track of who's monitoring which nicks.
type MonitorManager struct { type MonitorManager struct {
sync.RWMutex sync.RWMutex
// client -> nicks it's watching // client -> nicks it's watching
@ -21,6 +22,7 @@ type MonitorManager struct {
// (all nicks must be normalized externally by casefolding) // (all nicks must be normalized externally by casefolding)
} }
// NewMonitorManager returns a new MonitorManager.
func NewMonitorManager() *MonitorManager { func NewMonitorManager() *MonitorManager {
mm := MonitorManager{ mm := MonitorManager{
watching: make(map[*Client]map[string]bool), watching: make(map[*Client]map[string]bool),
@ -29,7 +31,8 @@ func NewMonitorManager() *MonitorManager {
return &mm return &mm
} }
var MonitorLimitExceeded = errors.New("Monitor limit exceeded") // ErrMonitorLimitExceeded is used when the monitor list exceeds our limit.
var ErrMonitorLimitExceeded = errors.New("Monitor limit exceeded")
// AlertAbout alerts everyone monitoring `client`'s nick that `client` is now {on,off}line. // AlertAbout alerts everyone monitoring `client`'s nick that `client` is now {on,off}line.
func (manager *MonitorManager) AlertAbout(client *Client, online bool) { func (manager *MonitorManager) AlertAbout(client *Client, online bool) {
@ -69,7 +72,7 @@ func (manager *MonitorManager) Add(client *Client, nick string, limit int) error
} }
if len(manager.watching[client]) >= limit { if len(manager.watching[client]) >= limit {
return MonitorLimitExceeded return ErrMonitorLimitExceeded
} }
manager.watching[client][nick] = true manager.watching[client][nick] = true
@ -92,7 +95,7 @@ func (manager *MonitorManager) RemoveAll(client *Client) {
manager.Lock() manager.Lock()
defer manager.Unlock() defer manager.Unlock()
for nick, _ := range manager.watching[client] { for nick := range manager.watching[client] {
delete(manager.watchedby[nick], client) delete(manager.watchedby[nick], client)
} }
delete(manager.watching, client) delete(manager.watching, client)
@ -172,7 +175,7 @@ func monitorAddHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bo
} }
err = server.monitorManager.Add(client, casefoldedTarget, limit) err = server.monitorManager.Add(client, casefoldedTarget, limit)
if err == MonitorLimitExceeded { if err == ErrMonitorLimitExceeded {
client.Send(nil, server.name, ERR_MONLISTFULL, client.getNick(), strconv.Itoa(server.limits.MonitorEntries), strings.Join(targets, ",")) client.Send(nil, server.name, ERR_MONLISTFULL, client.getNick(), strconv.Itoa(server.limits.MonitorEntries), strings.Join(targets, ","))
break break
} else if err != nil { } else if err != nil {
@ -206,7 +209,7 @@ func monitorListHandler(server *Server, client *Client, msg ircmsg.IrcMessage) b
monitorList := server.monitorManager.List(client) monitorList := server.monitorManager.List(client)
var nickList []string var nickList []string
for _, cfnick := range(monitorList) { for _, cfnick := range monitorList {
replynick := cfnick replynick := cfnick
// report the uncasefolded nick if it's available, i.e., the client is online // report the uncasefolded nick if it's available, i.e., the client is online
if mclient := server.clients.Get(cfnick); mclient != nil { if mclient := server.clients.Get(cfnick); mclient != nil {

View File

@ -1592,6 +1592,7 @@ func (server *Server) setupListeners(config *Config) {
} }
} }
// GetDefaultChannelModes returns our default channel modes.
func (server *Server) GetDefaultChannelModes() Modes { func (server *Server) GetDefaultChannelModes() Modes {
server.configurableStateMutex.RLock() server.configurableStateMutex.RLock()
defer server.configurableStateMutex.RUnlock() defer server.configurableStateMutex.RUnlock()

View File

@ -64,8 +64,10 @@ func (channels *ChannelNameMap) Len() int {
return len(channels.Chans) return len(channels.Chans)
} }
// ModeSet holds a set of modes.
type ModeSet map[Mode]bool type ModeSet map[Mode]bool
// String returns the modes in this set.
func (set ModeSet) String() string { func (set ModeSet) String() string {
if len(set) == 0 { if len(set) == 0 {
return "" return ""
@ -79,35 +81,44 @@ func (set ModeSet) String() string {
return strings.Join(strs, "") return strings.Join(strs, "")
} }
// ClientSet is a set of clients.
type ClientSet map[*Client]bool type ClientSet map[*Client]bool
// Add adds the given client to this set.
func (clients ClientSet) Add(client *Client) { func (clients ClientSet) Add(client *Client) {
clients[client] = true clients[client] = true
} }
// Remove removes the given client from this set.
func (clients ClientSet) Remove(client *Client) { func (clients ClientSet) Remove(client *Client) {
delete(clients, client) delete(clients, client)
} }
// Has returns true if the given client is in this set.
func (clients ClientSet) Has(client *Client) bool { func (clients ClientSet) Has(client *Client) bool {
return clients[client] return clients[client]
} }
// MemberSet is a set of members with modes.
type MemberSet map[*Client]ModeSet type MemberSet map[*Client]ModeSet
// Add adds the given client to this set.
func (members MemberSet) Add(member *Client) { func (members MemberSet) Add(member *Client) {
members[member] = make(ModeSet) members[member] = make(ModeSet)
} }
// Remove removes the given client from this set.
func (members MemberSet) Remove(member *Client) { func (members MemberSet) Remove(member *Client) {
delete(members, member) delete(members, member)
} }
// Has returns true if the given client is in this set.
func (members MemberSet) Has(member *Client) bool { func (members MemberSet) Has(member *Client) bool {
_, ok := members[member] _, ok := members[member]
return ok return ok
} }
// HasMode returns true if the given client is in this set with the given mode.
func (members MemberSet) HasMode(member *Client, mode Mode) bool { func (members MemberSet) HasMode(member *Client, mode Mode) bool {
modes, ok := members[member] modes, ok := members[member]
if !ok { if !ok {
@ -116,6 +127,7 @@ func (members MemberSet) HasMode(member *Client, mode Mode) bool {
return modes[mode] return modes[mode]
} }
// AnyHasMode returns true if any of our clients has the given mode.
func (members MemberSet) AnyHasMode(mode Mode) bool { func (members MemberSet) AnyHasMode(mode Mode) bool {
for _, modes := range members { for _, modes := range members {
if modes[mode] { if modes[mode] {
@ -125,19 +137,15 @@ func (members MemberSet) AnyHasMode(mode Mode) bool {
return false return false
} }
// ChannelSet is a set of channels.
type ChannelSet map[*Channel]bool type ChannelSet map[*Channel]bool
// Add adds the given channel to this set.
func (channels ChannelSet) Add(channel *Channel) { func (channels ChannelSet) Add(channel *Channel) {
channels[channel] = true channels[channel] = true
} }
// Remove removes the given channel from this set.
func (channels ChannelSet) Remove(channel *Channel) { func (channels ChannelSet) Remove(channel *Channel) {
delete(channels, channel) delete(channels, channel)
} }
func (channels ChannelSet) First() *Channel {
for channel := range channels {
return channel
}
return nil
}